00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "gui.h"
00012
00013
00014
00015
00016 GuiElement::GuiElement()
00017 {
00018 xoffset = 0;
00019 yoffset = 0;
00020 xmin = 0;
00021 xmax = 0;
00022 ymin = 0;
00023 ymax = 0;
00024 width = 0;
00025 height = 0;
00026 alpha = 255;
00027 scale = 1;
00028 state = STATE_DEFAULT;
00029 stateChan = -1;
00030 trigger[0] = NULL;
00031 trigger[1] = NULL;
00032 parentElement = NULL;
00033 rumble = true;
00034 selectable = false;
00035 clickable = false;
00036 holdable = false;
00037 visible = true;
00038 focus = -1;
00039 updateCB = NULL;
00040 yoffsetDyn = 0;
00041 xoffsetDyn = 0;
00042 alphaDyn = -1;
00043 scaleDyn = 1;
00044 effects = 0;
00045 effectAmount = 0;
00046 effectTarget = 0;
00047 effectsOver = 0;
00048 effectAmountOver = 0;
00049 effectTargetOver = 0;
00050
00051
00052 alignmentVert = ALIGN_TOP;
00053 alignmentHor = ALIGN_LEFT;
00054 }
00055
00056
00057
00058
00059 GuiElement::~GuiElement()
00060 {
00061 }
00062
00063 void GuiElement::SetParent(GuiElement * e)
00064 {
00065 parentElement = e;
00066 }
00067
00068 GuiElement * GuiElement::GetParent()
00069 {
00070 return parentElement;
00071 }
00072
00073
00074
00075
00076
00077
00078 int GuiElement::GetLeft()
00079 {
00080 int x = 0;
00081 int pWidth = 0;
00082 int pLeft = 0;
00083
00084 if(parentElement)
00085 {
00086 pWidth = parentElement->GetWidth();
00087 pLeft = parentElement->GetLeft();
00088 }
00089
00090 if(effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT))
00091 pLeft += xoffsetDyn;
00092
00093 switch(alignmentHor)
00094 {
00095 case ALIGN_LEFT:
00096 x = pLeft;
00097 break;
00098 case ALIGN_CENTRE:
00099 x = pLeft + (pWidth/2) - (width/2);
00100 break;
00101 case ALIGN_RIGHT:
00102 x = pLeft + pWidth - width;
00103 break;
00104 }
00105 return x + xoffset;
00106 }
00107
00108
00109
00110
00111
00112
00113 int GuiElement::GetTop()
00114 {
00115 int y = 0;
00116 int pHeight = 0;
00117 int pTop = 0;
00118
00119 if(parentElement)
00120 {
00121 pHeight = parentElement->GetHeight();
00122 pTop = parentElement->GetTop();
00123 }
00124
00125 if(effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT))
00126 pTop += yoffsetDyn;
00127
00128 switch(alignmentVert)
00129 {
00130 case ALIGN_TOP:
00131 y = pTop;
00132 break;
00133 case ALIGN_MIDDLE:
00134 y = pTop + (pHeight/2) - (height/2);
00135 break;
00136 case ALIGN_BOTTOM:
00137 y = pTop + pHeight - height;
00138 break;
00139 }
00140 return y + yoffset;
00141 }
00142
00143 void GuiElement::SetMinX(int x)
00144 {
00145 xmin = x;
00146 }
00147
00148 int GuiElement::GetMinX()
00149 {
00150 return xmin;
00151 }
00152
00153 void GuiElement::SetMaxX(int x)
00154 {
00155 xmax = x;
00156 }
00157
00158 int GuiElement::GetMaxX()
00159 {
00160 return xmax;
00161 }
00162
00163 void GuiElement::SetMinY(int y)
00164 {
00165 ymin = y;
00166 }
00167
00168 int GuiElement::GetMinY()
00169 {
00170 return ymin;
00171 }
00172
00173 void GuiElement::SetMaxY(int y)
00174 {
00175 ymax = y;
00176 }
00177
00178 int GuiElement::GetMaxY()
00179 {
00180 return ymax;
00181 }
00182
00183
00184
00185
00186
00187
00188 int GuiElement::GetWidth()
00189 {
00190 return width;
00191 }
00192
00193
00194
00195
00196
00197
00198 int GuiElement::GetHeight()
00199 {
00200 return height;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210 void GuiElement::SetSize(int w, int h)
00211 {
00212
00213 width = w;
00214 height = h;
00215 }
00216
00217
00218
00219
00220
00221
00222 bool GuiElement::IsVisible()
00223 {
00224 return visible;
00225 }
00226
00227
00228
00229
00230
00231
00232 void GuiElement::SetVisible(bool v)
00233 {
00234 visible = v;
00235 }
00236
00237 void GuiElement::SetAlpha(int a)
00238 {
00239 alpha = a;
00240 }
00241
00242 int GuiElement::GetAlpha()
00243 {
00244 int a;
00245
00246 if(alphaDyn >= 0)
00247 a = alphaDyn;
00248 else
00249 a = alpha;
00250
00251 if(parentElement)
00252 a *= parentElement->GetAlpha()/255.0;
00253
00254 return a;
00255 }
00256
00257 void GuiElement::SetScale(float s)
00258 {
00259 scale = s;
00260 }
00261
00262 float GuiElement::GetScale()
00263 {
00264 float s = scale * scaleDyn;
00265
00266 if(parentElement)
00267 s *= parentElement->GetScale();
00268
00269 return s;
00270 }
00271
00272 int GuiElement::GetState()
00273 {
00274 return state;
00275 }
00276
00277 int GuiElement::GetStateChan()
00278 {
00279 return stateChan;
00280 }
00281
00282 void GuiElement::SetState(int s, int c)
00283 {
00284 state = s;
00285 stateChan = c;
00286 }
00287
00288 void GuiElement::ResetState()
00289 {
00290 if(state != STATE_DISABLED)
00291 {
00292 state = STATE_DEFAULT;
00293 stateChan = -1;
00294 }
00295 }
00296
00297 void GuiElement::SetClickable(bool c)
00298 {
00299 clickable = c;
00300 }
00301
00302 void GuiElement::SetSelectable(bool s)
00303 {
00304 selectable = s;
00305 }
00306
00307 void GuiElement::SetHoldable(bool d)
00308 {
00309 holdable = d;
00310 }
00311
00312 bool GuiElement::IsSelectable()
00313 {
00314 if(state == STATE_DISABLED || state == STATE_CLICKED)
00315 return false;
00316 else
00317 return selectable;
00318 }
00319
00320 bool GuiElement::IsClickable()
00321 {
00322 if(state == STATE_DISABLED ||
00323 state == STATE_CLICKED ||
00324 state == STATE_HELD)
00325 return false;
00326 else
00327 return clickable;
00328 }
00329
00330 bool GuiElement::IsHoldable()
00331 {
00332 if(state == STATE_DISABLED)
00333 return false;
00334 else
00335 return holdable;
00336 }
00337
00338 void GuiElement::SetFocus(int f)
00339 {
00340 focus = f;
00341 }
00342
00343 int GuiElement::IsFocused()
00344 {
00345 return focus;
00346 }
00347
00348 void GuiElement::SetTrigger(GuiTrigger * t)
00349 {
00350 if(!trigger[0])
00351 trigger[0] = t;
00352 else if(!trigger[1])
00353 trigger[1] = t;
00354 else
00355 trigger[0] = t;
00356 }
00357
00358 void GuiElement::SetTrigger(u8 i, GuiTrigger * t)
00359 {
00360 trigger[i] = t;
00361 }
00362
00363 bool GuiElement::Rumble()
00364 {
00365 return rumble;
00366 }
00367
00368 void GuiElement::SetRumble(bool r)
00369 {
00370 rumble = r;
00371 }
00372
00373 int GuiElement::GetEffect()
00374 {
00375 return effects;
00376 }
00377
00378 void GuiElement::SetEffect(int eff, int amount, int target)
00379 {
00380 if(eff & EFFECT_SLIDE_IN)
00381 {
00382
00383 if(eff & EFFECT_SLIDE_TOP)
00384 yoffsetDyn = -screenheight;
00385 else if(eff & EFFECT_SLIDE_LEFT)
00386 xoffsetDyn = -screenwidth;
00387 else if(eff & EFFECT_SLIDE_BOTTOM)
00388 yoffsetDyn = screenheight;
00389 else if(eff & EFFECT_SLIDE_RIGHT)
00390 xoffsetDyn = screenwidth;
00391 }
00392 if(eff & EFFECT_FADE && amount > 0)
00393 {
00394 alphaDyn = 0;
00395 }
00396 else if(eff & EFFECT_FADE && amount < 0)
00397 {
00398 alphaDyn = alpha;
00399 }
00400
00401 effects |= eff;
00402 effectAmount = amount;
00403 effectTarget = target;
00404 }
00405
00406 void GuiElement::SetEffectOnOver(int eff, int amount, int target)
00407 {
00408 effectsOver |= eff;
00409 effectAmountOver = amount;
00410 effectTargetOver = target;
00411 }
00412
00413 void GuiElement::SetEffectGrow()
00414 {
00415 SetEffectOnOver(EFFECT_SCALE, 4, 110);
00416 }
00417
00418 void GuiElement::UpdateEffects()
00419 {
00420 if(effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT))
00421 {
00422 if(effects & EFFECT_SLIDE_IN)
00423 {
00424 if(effects & EFFECT_SLIDE_LEFT)
00425 {
00426 xoffsetDyn += effectAmount;
00427
00428 if(xoffsetDyn >= 0)
00429 {
00430 xoffsetDyn = 0;
00431 effects = 0;
00432 }
00433 }
00434 else if(effects & EFFECT_SLIDE_RIGHT)
00435 {
00436 xoffsetDyn -= effectAmount;
00437
00438 if(xoffsetDyn <= 0)
00439 {
00440 xoffsetDyn = 0;
00441 effects = 0;
00442 }
00443 }
00444 else if(effects & EFFECT_SLIDE_TOP)
00445 {
00446 yoffsetDyn += effectAmount;
00447
00448 if(yoffsetDyn >= 0)
00449 {
00450 yoffsetDyn = 0;
00451 effects = 0;
00452 }
00453 }
00454 else if(effects & EFFECT_SLIDE_BOTTOM)
00455 {
00456 yoffsetDyn -= effectAmount;
00457
00458 if(yoffsetDyn <= 0)
00459 {
00460 yoffsetDyn = 0;
00461 effects = 0;
00462 }
00463 }
00464 }
00465 else
00466 {
00467 if(effects & EFFECT_SLIDE_LEFT)
00468 {
00469 xoffsetDyn -= effectAmount;
00470
00471 if(xoffsetDyn <= -screenwidth)
00472 effects = 0;
00473 }
00474 else if(effects & EFFECT_SLIDE_RIGHT)
00475 {
00476 xoffsetDyn += effectAmount;
00477
00478 if(xoffsetDyn >= screenwidth)
00479 effects = 0;
00480 }
00481 else if(effects & EFFECT_SLIDE_TOP)
00482 {
00483 yoffsetDyn -= effectAmount;
00484
00485 if(yoffsetDyn <= -screenheight)
00486 effects = 0;
00487 }
00488 else if(effects & EFFECT_SLIDE_BOTTOM)
00489 {
00490 yoffsetDyn += effectAmount;
00491
00492 if(yoffsetDyn >= screenheight)
00493 effects = 0;
00494 }
00495 }
00496 }
00497 if(effects & EFFECT_FADE)
00498 {
00499 alphaDyn += effectAmount;
00500
00501 if(effectAmount < 0 && alphaDyn <= 0)
00502 {
00503 alphaDyn = 0;
00504 effects = 0;
00505 }
00506 else if(effectAmount > 0 && alphaDyn >= alpha)
00507 {
00508 alphaDyn = alpha;
00509 effects = 0;
00510 }
00511 }
00512 if(effects & EFFECT_SCALE)
00513 {
00514 scaleDyn += effectAmount/100.0;
00515
00516 if((effectAmount < 0 && scaleDyn <= effectTarget/100.0)
00517 || (effectAmount > 0 && scaleDyn >= effectTarget/100.0))
00518 {
00519 scaleDyn = effectTarget/100.0;
00520 effects = 0;
00521 }
00522 }
00523 }
00524
00525 void GuiElement::Update(GuiTrigger * t)
00526 {
00527 if(updateCB)
00528 updateCB(this);
00529 }
00530
00531 void GuiElement::SetUpdateCallback(UpdateCallback u)
00532 {
00533 updateCB = u;
00534 }
00535
00536 void GuiElement::SetPosition(int xoff, int yoff)
00537 {
00538 xoffset = xoff;
00539 yoffset = yoff;
00540 }
00541
00542 void GuiElement::SetAlignment(int hor, int vert)
00543 {
00544 alignmentHor = hor;
00545 alignmentVert = vert;
00546 }
00547
00548 int GuiElement::GetSelected()
00549 {
00550 return -1;
00551 }
00552
00553
00554
00555
00556 void GuiElement::Draw()
00557 {
00558 }
00559
00560
00561
00562
00563
00564
00565 bool GuiElement::IsInside(int x, int y)
00566 {
00567 if(x > this->GetLeft() && x < (this->GetLeft()+width)
00568 && y > this->GetTop() && y < (this->GetTop()+height))
00569 return true;
00570 return false;
00571 }