00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/widgets/button.h"
00023
00024 #include "gui/palette.h"
00025
00026 #include "configuration.h"
00027 #include "graphics.h"
00028
00029 #include "resources/image.h"
00030 #include "resources/resourcemanager.h"
00031
00032 #include "utils/dtor.h"
00033
00034 #include <guichan/exception.hpp>
00035 #include <guichan/font.hpp>
00036
00037 int Button::mInstances = 0;
00038 float Button::mAlpha = 1.0;
00039
00040 enum{
00041 BUTTON_STANDARD,
00042 BUTTON_HIGHLIGHTED,
00043 BUTTON_PRESSED,
00044 BUTTON_DISABLED,
00045 BUTTON_COUNT
00046 };
00047
00048 struct ButtonData
00049 {
00050 char const *file;
00051 int gridX;
00052 int gridY;
00053 };
00054
00055 static ButtonData const data[BUTTON_COUNT] = {
00056 { "graphics/gui/button.png", 0, 0 },
00057 { "graphics/gui/buttonhi.png", 9, 4 },
00058 { "graphics/gui/buttonpress.png", 16, 19 },
00059 { "graphics/gui/button_disabled.png", 25, 23 }
00060 };
00061
00062 ImageRect Button::button[BUTTON_COUNT];
00063
00064 Button::Button()
00065 {
00066 init();
00067 }
00068
00069 Button::Button(const std::string &caption, const std::string &actionEventId,
00070 gcn::ActionListener *listener):
00071 gcn::Button(caption)
00072 {
00073 init();
00074 setActionEventId(actionEventId);
00075
00076 if (listener)
00077 addActionListener(listener);
00078 }
00079
00080 void Button::init()
00081 {
00082 setFrameSize(0);
00083
00084 if (mInstances == 0)
00085 {
00086
00087 ResourceManager *resman = ResourceManager::getInstance();
00088 Image *btn[BUTTON_COUNT];
00089
00090 int a, x, y, mode;
00091
00092 for (mode = 0; mode < BUTTON_COUNT; mode++)
00093 {
00094 btn[mode] = resman->getImage(data[mode].file);
00095 a = 0;
00096 for (y = 0; y < 3; y++)
00097 {
00098 for (x = 0; x < 3; x++)
00099 {
00100 button[mode].grid[a] = btn[mode]->getSubImage(
00101 data[x].gridX, data[y].gridY,
00102 data[x + 1].gridX - data[x].gridX + 1,
00103 data[y + 1].gridY - data[y].gridY + 1);
00104 button[mode].grid[a]->setAlpha(mAlpha);
00105 a++;
00106 }
00107 }
00108 btn[mode]->decRef();
00109 }
00110 }
00111 mInstances++;
00112 }
00113
00114 Button::~Button()
00115 {
00116 mInstances--;
00117
00118 if (mInstances == 0)
00119 {
00120 for (int mode = 0; mode < BUTTON_COUNT; mode++)
00121 {
00122 for_each(button[mode].grid, button[mode].grid + 9, dtor<Image*>());
00123 }
00124 }
00125 }
00126
00127 void Button::draw(gcn::Graphics *graphics)
00128 {
00129 int mode;
00130
00131 if (!isEnabled())
00132 mode = BUTTON_DISABLED;
00133 else if (isPressed())
00134 mode = BUTTON_PRESSED;
00135 else if (mHasMouse || isFocused())
00136 mode = BUTTON_HIGHLIGHTED;
00137 else
00138 mode = BUTTON_STANDARD;
00139
00140 if (config.getValue("guialpha", 0.8) != mAlpha)
00141 {
00142 mAlpha = config.getValue("guialpha", 0.8);
00143 for (int a = 0; a < 9; a++)
00144 {
00145 button[BUTTON_DISABLED].grid[a]->setAlpha(mAlpha);
00146 button[BUTTON_PRESSED].grid[a]->setAlpha(mAlpha);
00147 button[BUTTON_HIGHLIGHTED].grid[a]->setAlpha(mAlpha);
00148 button[BUTTON_STANDARD].grid[a]->setAlpha(mAlpha);
00149 }
00150 }
00151
00152 static_cast<Graphics*>(graphics)->
00153 drawImageRect(0, 0, getWidth(), getHeight(), button[mode]);
00154
00155 graphics->setColor(guiPalette->getColor(Palette::TEXT));
00156
00157 int textX;
00158 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00159
00160 switch (getAlignment())
00161 {
00162 case gcn::Graphics::LEFT:
00163 textX = 4;
00164 break;
00165 case gcn::Graphics::CENTER:
00166 textX = getWidth() / 2;
00167 break;
00168 case gcn::Graphics::RIGHT:
00169 textX = getWidth() - 4;
00170 break;
00171 default:
00172 throw GCN_EXCEPTION("Button::draw. Unknown alignment.");
00173 }
00174
00175 graphics->setFont(getFont());
00176
00177 if (isPressed())
00178 graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment());
00179 else
00180 graphics->drawText(getCaption(), textX, textY, getAlignment());
00181 }