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/scrollarea.h"
00023
00024 #include "configuration.h"
00025 #include "graphics.h"
00026
00027 #include "resources/image.h"
00028 #include "resources/resourcemanager.h"
00029
00030 #include "utils/dtor.h"
00031
00032 int ScrollArea::instances = 0;
00033 float ScrollArea::mAlpha = 1.0;
00034 ImageRect ScrollArea::background;
00035 ImageRect ScrollArea::vMarker;
00036 Image *ScrollArea::buttons[4][2];
00037
00038 ScrollArea::ScrollArea():
00039 gcn::ScrollArea(),
00040 mOpaque(true)
00041 {
00042 init();
00043 }
00044
00045 ScrollArea::ScrollArea(gcn::Widget *widget):
00046 gcn::ScrollArea(widget),
00047 mOpaque(true)
00048 {
00049 init();
00050 }
00051
00052 ScrollArea::~ScrollArea()
00053 {
00054
00055 delete getContent();
00056
00057 instances--;
00058
00059 if (instances == 0)
00060 {
00061 for_each(background.grid, background.grid + 9, dtor<Image*>());
00062 for_each(vMarker.grid, vMarker.grid + 9, dtor<Image*>());
00063
00064 buttons[UP][0]->decRef();
00065 buttons[UP][1]->decRef();
00066 buttons[DOWN][0]->decRef();
00067 buttons[DOWN][1]->decRef();
00068 buttons[LEFT][0]->decRef();
00069 buttons[LEFT][1]->decRef();
00070 buttons[RIGHT][0]->decRef();
00071 buttons[RIGHT][1]->decRef();
00072 }
00073 }
00074
00075 void ScrollArea::init()
00076 {
00077
00078 setOpaque(true);
00079
00080 if (instances == 0)
00081 {
00082
00083 ResourceManager *resman = ResourceManager::getInstance();
00084 Image *textbox = resman->getImage("graphics/gui/deepbox.png");
00085 const int bggridx[4] = {0, 3, 28, 31};
00086 const int bggridy[4] = {0, 3, 28, 31};
00087 int a = 0, x, y;
00088
00089 for (y = 0; y < 3; y++)
00090 {
00091 for (x = 0; x < 3; x++)
00092 {
00093 background.grid[a] = textbox->getSubImage(
00094 bggridx[x], bggridy[y],
00095 bggridx[x + 1] - bggridx[x] + 1,
00096 bggridy[y + 1] - bggridy[y] + 1);
00097 background.grid[a]->setAlpha(config.getValue("guialpha", 0.8));
00098 a++;
00099 }
00100 }
00101
00102 textbox->decRef();
00103
00104
00105 Image *vscroll = resman->getImage("graphics/gui/vscroll_grey.png");
00106 int vsgridx[4] = {0, 4, 7, 11};
00107 int vsgridy[4] = {0, 4, 15, 19};
00108 a = 0;
00109
00110 for (y = 0; y < 3; y++)
00111 {
00112 for (x = 0; x < 3; x++)
00113 {
00114 vMarker.grid[a] = vscroll->getSubImage(
00115 vsgridx[x], vsgridy[y],
00116 vsgridx[x + 1] - vsgridx[x],
00117 vsgridy[y + 1] - vsgridy[y]);
00118 vMarker.grid[a]->setAlpha(config.getValue("guialpha", 0.8));
00119 a++;
00120 }
00121 }
00122
00123 vscroll->decRef();
00124
00125 buttons[UP][0] =
00126 resman->getImage("graphics/gui/vscroll_up_default.png");
00127 buttons[DOWN][0] =
00128 resman->getImage("graphics/gui/vscroll_down_default.png");
00129 buttons[LEFT][0] =
00130 resman->getImage("graphics/gui/hscroll_left_default.png");
00131 buttons[RIGHT][0] =
00132 resman->getImage("graphics/gui/hscroll_right_default.png");
00133 buttons[UP][1] =
00134 resman->getImage("graphics/gui/vscroll_up_pressed.png");
00135 buttons[DOWN][1] =
00136 resman->getImage("graphics/gui/vscroll_down_pressed.png");
00137 buttons[LEFT][1] =
00138 resman->getImage("graphics/gui/hscroll_left_pressed.png");
00139 buttons[RIGHT][1] =
00140 resman->getImage("graphics/gui/hscroll_right_pressed.png");
00141 }
00142
00143 instances++;
00144 }
00145
00146 void ScrollArea::logic()
00147 {
00148 if (!isVisible())
00149 return;
00150
00151 gcn::ScrollArea::logic();
00152 gcn::Widget *content = getContent();
00153
00154
00155
00156 if (content)
00157 {
00158 if (getHorizontalScrollPolicy() == gcn::ScrollArea::SHOW_NEVER)
00159 {
00160 content->setWidth(getChildrenArea().width -
00161 2 * content->getFrameSize());
00162 }
00163 if (getVerticalScrollPolicy() == gcn::ScrollArea::SHOW_NEVER)
00164 {
00165 content->setHeight(getChildrenArea().height -
00166 2 * content->getFrameSize());
00167 }
00168 }
00169 }
00170
00171 void ScrollArea::draw(gcn::Graphics *graphics)
00172 {
00173 if (mVBarVisible)
00174 {
00175 drawUpButton(graphics);
00176 drawDownButton(graphics);
00177 drawVBar(graphics);
00178 drawVMarker(graphics);
00179 }
00180
00181 if (mHBarVisible)
00182 {
00183 drawLeftButton(graphics);
00184 drawRightButton(graphics);
00185 drawHBar(graphics);
00186 drawHMarker(graphics);
00187 }
00188
00189 if (mHBarVisible && mVBarVisible)
00190 {
00191 graphics->setColor(getBaseColor());
00192 graphics->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth,
00193 getHeight() - mScrollbarWidth,
00194 mScrollbarWidth,
00195 mScrollbarWidth));
00196 }
00197
00198 if (config.getValue("guialpha", 0.8) != mAlpha)
00199 {
00200 mAlpha = config.getValue("guialpha", 0.8);
00201 for (int a = 0; a < 9; a++)
00202 {
00203 background.grid[a]->setAlpha(mAlpha);
00204 vMarker.grid[a]->setAlpha(mAlpha);
00205 }
00206 }
00207
00208 drawChildren(graphics);
00209 }
00210
00211 void ScrollArea::drawFrame(gcn::Graphics *graphics)
00212 {
00213 if (mOpaque)
00214 {
00215 const int bs = getFrameSize();
00216 const int w = getWidth() + bs * 2;
00217 const int h = getHeight() + bs * 2;
00218
00219 static_cast<Graphics*>(graphics)->
00220 drawImageRect(0, 0, w, h, background);
00221 }
00222 }
00223
00224 void ScrollArea::setOpaque(bool opaque)
00225 {
00226 mOpaque = opaque;
00227 setFrameSize(mOpaque ? 2 : 0);
00228 }
00229
00230 void ScrollArea::drawButton(gcn::Graphics *graphics, BUTTON_DIR dir)
00231 {
00232 int state = 0;
00233 gcn::Rectangle dim;
00234
00235 switch (dir)
00236 {
00237 case UP:
00238 state = mUpButtonPressed ? 1 : 0;
00239 dim = getUpButtonDimension();
00240 break;
00241 case DOWN:
00242 state = mDownButtonPressed ? 1 : 0;
00243 dim = getDownButtonDimension();
00244 break;
00245 case LEFT:
00246 state = mLeftButtonPressed ? 1 : 0;
00247 dim = getLeftButtonDimension();
00248 break;
00249 case RIGHT:
00250 state = mRightButtonPressed ? 1 : 0;
00251 dim = getRightButtonDimension();
00252 break;
00253 }
00254
00255 static_cast<Graphics*>(graphics)->
00256 drawImage(buttons[dir][state], dim.x, dim.y);
00257 }
00258
00259 void ScrollArea::drawUpButton(gcn::Graphics *graphics)
00260 {
00261 drawButton(graphics, UP);
00262 }
00263
00264 void ScrollArea::drawDownButton(gcn::Graphics *graphics)
00265 {
00266 drawButton(graphics, DOWN);
00267 }
00268
00269 void ScrollArea::drawLeftButton(gcn::Graphics *graphics)
00270 {
00271 drawButton(graphics, LEFT);
00272 }
00273
00274 void ScrollArea::drawRightButton(gcn::Graphics *graphics)
00275 {
00276 drawButton(graphics, RIGHT);
00277 }
00278
00279 void ScrollArea::drawVBar(gcn::Graphics *graphics)
00280 {
00281 gcn::Rectangle dim = getVerticalBarDimension();
00282 graphics->setColor(gcn::Color(0, 0, 0, 32));
00283 graphics->fillRectangle(dim);
00284 graphics->setColor(gcn::Color(255, 255, 255));
00285 }
00286
00287 void ScrollArea::drawHBar(gcn::Graphics *graphics)
00288 {
00289 gcn::Rectangle dim = getHorizontalBarDimension();
00290 graphics->setColor(gcn::Color(0, 0, 0, 32));
00291 graphics->fillRectangle(dim);
00292 graphics->setColor(gcn::Color(255, 255, 255));
00293 }
00294
00295 void ScrollArea::drawVMarker(gcn::Graphics *graphics)
00296 {
00297 gcn::Rectangle dim = getVerticalMarkerDimension();
00298
00299 static_cast<Graphics*>(graphics)->
00300 drawImageRect(dim.x, dim.y, dim.width, dim.height, vMarker);
00301 }
00302
00303 void ScrollArea::drawHMarker(gcn::Graphics *graphics)
00304 {
00305 gcn::Rectangle dim = getHorizontalMarkerDimension();
00306
00307 static_cast<Graphics*>(graphics)->
00308 drawImageRect(dim.x, dim.y, dim.width, dim.height, vMarker);
00309 }