00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <cassert>
00024
00025
00026
00027
00028
00029
00030
00031 #include "util/log/logger.h"
00032
00033 #include "twobutton.h"
00034
00035 namespace gcn {
00036 static FIFE::Logger _log(LM_GUI);
00037
00038 TwoButton::TwoButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption):
00039 Button(),
00040 m_upImage(up_file),
00041 m_downImage(down_file),
00042 m_hoverImage(hover_file),
00043 x_downoffset(0),
00044 y_downoffset(0) {
00045 m_hoverImage = hover_file;
00046 setFrameSize(0);
00047 adjustSize();
00048 mCaption = caption;
00049 }
00050
00051 TwoButton::~TwoButton() {
00052 }
00053
00054 void TwoButton::setDownOffset(int32_t x, int32_t y) {
00055 x_downoffset = x;
00056 y_downoffset = y;
00057 }
00058
00059 void TwoButton::draw(Graphics *graphics) {
00060 Image* img = m_upImage;
00061 int32_t xoffset = 0;
00062 int32_t yoffset = 0;
00063
00064 if (isPressed()) {
00065 if( m_downImage ) {
00066 img = m_downImage;
00067 xoffset = x_downoffset;
00068 yoffset = y_downoffset;
00069 }
00070 } else if(mHasMouse) {
00071 if( m_hoverImage ) {
00072 img = m_hoverImage;
00073 }
00074 }
00075
00076 if (img) {
00077 graphics->drawImage(img, xoffset, yoffset);
00078 }
00079
00080 graphics->setColor(getForegroundColor());
00081 int32_t textX;
00082 int32_t textY = getHeight() / 2 - getFont()->getHeight() / 2;
00083 switch (getAlignment())
00084 {
00085 case Graphics::LEFT:
00086 textX = 4;
00087 break;
00088 case Graphics::CENTER:
00089 textX = getWidth() / 2;
00090 break;
00091 case Graphics::RIGHT:
00092 textX = getWidth() - 4;
00093 break;
00094 default:
00095 textX = 4;
00096 FL_WARN(_log, FIFE::LMsg("TwoButton::draw() - ") << "Unknown alignment: "
00097 << getAlignment() << ". Using the default of Graphics::LEFT");
00098 }
00099
00100 graphics->setFont(getFont());
00101 if (mCaption.size() > 0) {
00102 if (isPressed())
00103 graphics->drawText(getCaption(), textX + 1,
00104 textY + 1, getAlignment());
00105 else
00106 graphics->drawText(getCaption(), textX, textY, getAlignment());
00107 }
00108 }
00109 void TwoButton::adjustSize() {
00110 int32_t w = 0;
00111 int32_t h = w;
00112 if( m_upImage ) {
00113 w = m_upImage->getWidth();
00114 h = m_upImage->getHeight();
00115 }
00116 if( m_downImage ) {
00117 w = std::max(m_downImage->getWidth(), w);
00118 h = std::max(m_downImage->getHeight(), h);
00119 }
00120 if( m_hoverImage ) {
00121 w = std::max(m_hoverImage->getWidth(), w);
00122 h = std::max(m_hoverImage->getHeight(), h);
00123 }
00124 setWidth(w);
00125 setHeight(h);
00126 }
00127 void TwoButton::setUpImage(Image* image) {
00128 m_upImage = image;
00129 adjustSize();
00130 }
00131 void TwoButton::setDownImage(Image* image) {
00132 m_downImage = image;
00133 adjustSize();
00134 }
00135 void TwoButton::setHoverImage(Image* image) {
00136 m_hoverImage = image;
00137 adjustSize();
00138 }
00139
00140 }
00141
00142