twobutton.cpp
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
00032 #include "twobutton.h"
00033
00034 namespace gcn {
00035 TwoButton::TwoButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption):
00036 Button(),
00037 m_upImage(up_file),
00038 m_downImage(down_file),
00039 m_hoverImage(hover_file),
00040 x_downoffset(0),
00041 y_downoffset(0) {
00042 m_hoverImage = hover_file;
00043 setFrameSize(0);
00044 adjustSize();
00045 mCaption = caption;
00046 }
00047
00048 TwoButton::~TwoButton() {
00049 }
00050
00051 void TwoButton::setDownOffset(int x, int y) {
00052 x_downoffset = x;
00053 y_downoffset = y;
00054 }
00055
00056 void TwoButton::draw(Graphics *graphics) {
00057 Image* img = m_upImage;
00058 int xoffset = 0;
00059 int yoffset = 0;
00060
00061 if (isPressed()) {
00062 if( m_downImage ) {
00063 img = m_downImage;
00064 xoffset = x_downoffset;
00065 yoffset = y_downoffset;
00066 }
00067 } else if(mHasMouse) {
00068 if( m_hoverImage ) {
00069 img = m_hoverImage;
00070 }
00071 }
00072
00073 if (img) {
00074 graphics->drawImage(img, xoffset, yoffset);
00075 }
00076
00077 graphics->setColor(getForegroundColor());
00078 int textX;
00079 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00080 switch (getAlignment())
00081 {
00082 case Graphics::LEFT:
00083 textX = 4;
00084 break;
00085 case Graphics::CENTER:
00086 textX = getWidth() / 2;
00087 break;
00088 case Graphics::RIGHT:
00089 textX = getWidth() - 4;
00090 break;
00091 default:
00092 throw GCN_EXCEPTION("Unknown alignment.");
00093 }
00094
00095 graphics->setFont(getFont());
00096 if (mCaption.size() > 0) {
00097 if (isPressed())
00098 graphics->drawText(getCaption(), textX + 1,
00099 textY + 1, getAlignment());
00100 else
00101 graphics->drawText(getCaption(), textX, textY, getAlignment());
00102 }
00103 }
00104 void TwoButton::adjustSize() {
00105 int w = 0;
00106 int h = w;
00107 if( m_upImage ) {
00108 w = m_upImage->getWidth();
00109 h = m_upImage->getHeight();
00110 }
00111 if( m_downImage ) {
00112 w = std::max(m_downImage->getWidth(), w);
00113 h = std::max(m_downImage->getHeight(), h);
00114 }
00115 if( m_hoverImage ) {
00116 w = std::max(m_hoverImage->getWidth(), w);
00117 h = std::max(m_hoverImage->getHeight(), h);
00118 }
00119 setWidth(w);
00120 setHeight(h);
00121 }
00122 void TwoButton::setUpImage(Image* image) {
00123 m_upImage = image;
00124 adjustSize();
00125 }
00126 void TwoButton::setDownImage(Image* image) {
00127 m_downImage = image;
00128 adjustSize();
00129 }
00130 void TwoButton::setHoverImage(Image* image) {
00131 m_hoverImage = image;
00132 adjustSize();
00133 }
00134
00135 }
00136
00137