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/dropdown.h"
00023
00024 #include "gui/widgets/listbox.h"
00025 #include "gui/widgets/scrollarea.h"
00026
00027 #include "gui/palette.h"
00028 #include "gui/sdlinput.h"
00029
00030 #include "configuration.h"
00031 #include "graphics.h"
00032
00033 #include "resources/image.h"
00034 #include "resources/resourcemanager.h"
00035
00036 #include "utils/dtor.h"
00037
00038 #include <algorithm>
00039
00040 int DropDown::instances = 0;
00041 Image *DropDown::buttons[2][2];
00042 ImageRect DropDown::skin;
00043 float DropDown::mAlpha = 1.0;
00044
00045 DropDown::DropDown(gcn::ListModel *listModel):
00046 gcn::DropDown::DropDown(listModel,
00047 new ScrollArea,
00048 new ListBox(listModel))
00049 {
00050 setFrameSize(2);
00051
00052
00053 if (instances == 0)
00054 {
00055
00056 ResourceManager *resman = ResourceManager::getInstance();
00057
00058
00059 buttons[1][0] =
00060 resman->getImage("graphics/gui/vscroll_up_default.png");
00061 buttons[0][0] =
00062 resman->getImage("graphics/gui/vscroll_down_default.png");
00063 buttons[1][1] =
00064 resman->getImage("graphics/gui/vscroll_up_pressed.png");
00065 buttons[0][1] =
00066 resman->getImage("graphics/gui/vscroll_down_pressed.png");
00067
00068 buttons[0][0]->setAlpha(mAlpha);
00069 buttons[0][1]->setAlpha(mAlpha);
00070 buttons[1][0]->setAlpha(mAlpha);
00071 buttons[1][1]->setAlpha(mAlpha);
00072
00073
00074 Image *boxBorder = resman->getImage("graphics/gui/deepbox.png");
00075 int gridx[4] = {0, 3, 28, 31};
00076 int gridy[4] = {0, 3, 28, 31};
00077 int a = 0, x, y;
00078
00079 for (y = 0; y < 3; y++)
00080 {
00081 for (x = 0; x < 3; x++)
00082 {
00083 skin.grid[a] = boxBorder->getSubImage(gridx[x], gridy[y],
00084 gridx[x + 1] -
00085 gridx[x] + 1,
00086 gridy[y + 1] -
00087 gridy[y] + 1);
00088 skin.grid[a]->setAlpha(mAlpha);
00089 a++;
00090 }
00091 }
00092
00093 boxBorder->decRef();
00094 }
00095
00096 instances++;
00097 }
00098
00099 DropDown::~DropDown()
00100 {
00101 instances--;
00102
00103 if (instances == 0)
00104 {
00105 buttons[0][0]->decRef();
00106 buttons[0][1]->decRef();
00107 buttons[1][0]->decRef();
00108 buttons[1][1]->decRef();
00109
00110 for_each(skin.grid, skin.grid + 9, dtor<Image*>());
00111 }
00112
00113 delete mScrollArea;
00114 }
00115
00116 void DropDown::draw(gcn::Graphics* graphics)
00117 {
00118 int h;
00119
00120 if (mDroppedDown)
00121 h = mFoldedUpHeight;
00122 else
00123 h = getHeight();
00124
00125 if (config.getValue("guialpha", 0.8) != mAlpha)
00126 {
00127 mAlpha = config.getValue("guialpha", 0.8);
00128
00129 buttons[0][0]->setAlpha(mAlpha);
00130 buttons[0][1]->setAlpha(mAlpha);
00131 buttons[1][0]->setAlpha(mAlpha);
00132 buttons[1][1]->setAlpha(mAlpha);
00133
00134 for (int a = 0; a < 9; a++)
00135 {
00136 skin.grid[a]->setAlpha(mAlpha);
00137 }
00138 }
00139
00140 const int alpha = (int) (mAlpha * 255.0f);
00141 gcn::Color faceColor = getBaseColor();
00142 faceColor.a = alpha;
00143 const gcn::Color* highlightColor = &guiPalette->getColor(Palette::HIGHLIGHT,
00144 alpha);
00145 gcn::Color shadowColor = faceColor - 0x303030;
00146 shadowColor.a = alpha;
00147
00148 if (mListBox->getListModel() && mListBox->getSelected() >= 0)
00149 {
00150 graphics->setFont(getFont());
00151 graphics->setColor(guiPalette->getColor(Palette::TEXT, alpha));
00152 graphics->drawText(mListBox->getListModel()->getElementAt(mListBox->getSelected()), 1, 0);
00153 }
00154
00155 if (isFocused())
00156 {
00157 graphics->setColor(*highlightColor);
00158 graphics->drawRectangle(gcn::Rectangle(0, 0, getWidth() - h, h));
00159 }
00160
00161 drawButton(graphics);
00162
00163 if (mDroppedDown)
00164 {
00165 drawChildren(graphics);
00166
00167
00168
00169 graphics->setColor(*highlightColor);
00170 graphics->drawLine(0, h, getWidth(), h);
00171 graphics->setColor(shadowColor);
00172 graphics->drawLine(0, h + 1, getWidth(), h + 1);
00173 }
00174 }
00175
00176 void DropDown::drawFrame(gcn::Graphics *graphics)
00177 {
00178 const int bs = getFrameSize();
00179 const int w = getWidth() + bs * 2;
00180 const int h = getHeight() + bs * 2;
00181
00182 static_cast<Graphics*>(graphics)->drawImageRect(0, 0, w, h, skin);
00183 }
00184
00185 void DropDown::drawButton(gcn::Graphics *graphics)
00186 {
00187 int height = mDroppedDown ? mFoldedUpHeight : getHeight();
00188
00189 static_cast<Graphics*>(graphics)->
00190 drawImage(buttons[mDroppedDown][mPushed], getWidth() - height + 2, 1);
00191 }
00192
00193
00194 void DropDown::keyPressed(gcn::KeyEvent& keyEvent)
00195 {
00196 gcn::Key key = keyEvent.getKey();
00197
00198 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00199 {
00200 if (!mDroppedDown)
00201 dropDown();
00202 keyEvent.consume();
00203 }
00204 else if (key.getValue() == Key::UP)
00205 {
00206 setSelected(getSelected() - 1);
00207 keyEvent.consume();
00208 }
00209 else if (key.getValue() == Key::DOWN)
00210 {
00211 setSelected(getSelected() + 1);
00212 keyEvent.consume();
00213 }
00214 else if (key.getValue() == Key::HOME)
00215 {
00216 setSelected(0);
00217 keyEvent.consume();
00218 }
00219 else if (key.getValue() == Key::END)
00220 {
00221 setSelected(mListBox->getListModel()->getNumberOfElements() - 1);
00222 keyEvent.consume();
00223 }
00224 }
00225
00226 void DropDown::mouseWheelMovedUp(gcn::MouseEvent& mouseEvent)
00227 {
00228 setSelected(getSelected() - 1);
00229 mouseEvent.consume();
00230 }
00231
00232 void DropDown::mouseWheelMovedDown(gcn::MouseEvent& mouseEvent)
00233 {
00234 setSelected(getSelected() + 1);
00235 mouseEvent.consume();
00236 }