00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include "guichan/widgets/button.hpp"
00062
00063 #include "guichan/exception.hpp"
00064 #include "guichan/font.hpp"
00065 #include "guichan/graphics.hpp"
00066 #include "guichan/key.hpp"
00067 #include "guichan/mouseinput.hpp"
00068
00069 namespace gcn
00070 {
00071 Button::Button()
00072 {
00073 mAlignment = Graphics::CENTER;
00074 setFocusable(true);
00075 adjustSize();
00076 setBorderSize(1);
00077
00078 mMouseDown = false;
00079 mKeyDown = false;
00080
00081 addMouseListener(this);
00082 addKeyListener(this);
00083 }
00084
00085 Button::Button(const std::string& caption)
00086 {
00087 mCaption = caption;
00088 mAlignment = Graphics::CENTER;
00089 setFocusable(true);
00090 adjustSize();
00091 setBorderSize(1);
00092
00093 mMouseDown = false;
00094 mKeyDown = false;
00095
00096 addMouseListener(this);
00097 addKeyListener(this);
00098 }
00099
00100 void Button::setCaption(const std::string& caption)
00101 {
00102 mCaption = caption;
00103 }
00104
00105 const std::string& Button::getCaption() const
00106 {
00107 return mCaption;
00108 }
00109
00110 void Button::setAlignment(unsigned int alignment)
00111 {
00112 mAlignment = alignment;
00113 }
00114
00115 unsigned int Button::getAlignment() const
00116 {
00117 return mAlignment;
00118 }
00119
00120 void Button::draw(Graphics* graphics)
00121 {
00122 Color faceColor = getBaseColor();
00123 Color highlightColor, shadowColor;
00124 int alpha = getBaseColor().a;
00125
00126 if (isPressed())
00127 {
00128 faceColor = faceColor - 0x303030;
00129 faceColor.a = alpha;
00130 highlightColor = faceColor - 0x303030;
00131 highlightColor.a = alpha;
00132 shadowColor = faceColor + 0x303030;
00133 shadowColor.a = alpha;
00134 }
00135 else
00136 {
00137 highlightColor = faceColor + 0x303030;
00138 highlightColor.a = alpha;
00139 shadowColor = faceColor - 0x303030;
00140 shadowColor.a = alpha;
00141 }
00142
00143 graphics->setColor(faceColor);
00144 graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
00145
00146 graphics->setColor(highlightColor);
00147 graphics->drawLine(0, 0, getWidth() - 1, 0);
00148 graphics->drawLine(0, 1, 0, getHeight() - 1);
00149
00150 graphics->setColor(shadowColor);
00151 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
00152 graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
00153
00154 graphics->setColor(getForegroundColor());
00155
00156 int textX;
00157 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00158
00159 switch (getAlignment())
00160 {
00161 case Graphics::LEFT:
00162 textX = 4;
00163 break;
00164 case Graphics::CENTER:
00165 textX = getWidth() / 2;
00166 break;
00167 case Graphics::RIGHT:
00168 textX = getWidth() - 4;
00169 break;
00170 default:
00171 throw GCN_EXCEPTION("Unknown alignment.");
00172 }
00173
00174 graphics->setFont(getFont());
00175
00176 if (isPressed())
00177 {
00178 graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment());
00179 }
00180 else
00181 {
00182 graphics->drawText(getCaption(), textX, textY, getAlignment());
00183
00184 if (isFocused())
00185 {
00186 graphics->drawRectangle(Rectangle(2, 2, getWidth() - 4,
00187 getHeight() - 4));
00188 }
00189 }
00190 }
00191
00192 void Button::drawBorder(Graphics* graphics)
00193 {
00194 Color faceColor = getBaseColor();
00195 Color highlightColor, shadowColor;
00196 int alpha = getBaseColor().a;
00197 int width = getWidth() + getBorderSize() * 2 - 1;
00198 int height = getHeight() + getBorderSize() * 2 - 1;
00199 highlightColor = faceColor + 0x303030;
00200 highlightColor.a = alpha;
00201 shadowColor = faceColor - 0x303030;
00202 shadowColor.a = alpha;
00203
00204 unsigned int i;
00205 for (i = 0; i < getBorderSize(); ++i)
00206 {
00207 graphics->setColor(shadowColor);
00208 graphics->drawLine(i,i, width - i, i);
00209 graphics->drawLine(i,i + 1, i, height - i - 1);
00210 graphics->setColor(highlightColor);
00211 graphics->drawLine(width - i,i + 1, width - i, height - i);
00212 graphics->drawLine(i,height - i, width - i - 1, height - i);
00213 }
00214 }
00215
00216 void Button::adjustSize()
00217 {
00218 setWidth(getFont()->getWidth(mCaption) + 8);
00219 setHeight(getFont()->getHeight() + 8);
00220 }
00221
00222 bool Button::isPressed() const
00223 {
00224 return (hasMouse() && mMouseDown) || mKeyDown;
00225 }
00226
00227 void Button::mouseClick(int x, int y, int button, int count)
00228 {
00229 if (button == MouseInput::LEFT)
00230 {
00231 generateAction();
00232 }
00233 }
00234
00235 void Button::mousePress(int x, int y, int button)
00236 {
00237 if (button == MouseInput::LEFT && hasMouse())
00238 {
00239 mMouseDown = true;
00240 }
00241 }
00242
00243 void Button::mouseRelease(int x, int y, int button)
00244 {
00245 if (button == MouseInput::LEFT)
00246 {
00247 mMouseDown = false;
00248 }
00249 }
00250
00251 void Button::keyPress(const Key& key)
00252 {
00253 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00254 {
00255 mKeyDown = true;
00256 }
00257
00258 mMouseDown = false;
00259 }
00260
00261 void Button::keyRelease(const Key& key)
00262 {
00263 if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyDown)
00264 {
00265 mKeyDown = false;
00266 generateAction();
00267 }
00268 }
00269
00270 void Button::lostFocus()
00271 {
00272 mMouseDown = false;
00273 mKeyDown = false;
00274 }
00275 }