2
0

PolyUIButton.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * PolyUIButton.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 8/1/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyUIButton.h"
  10. using namespace Polycode;
  11. UIButton::UIButton(String text, float width, float height) : ScreenEntity() {
  12. Config *conf = CoreServices::getInstance()->getConfig();
  13. String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
  14. int fontSize = conf->getNumericValue("Polycode", "uiButtonFontSize");
  15. float st = conf->getNumericValue("Polycode", "uiButtonSkinT");
  16. float sr = conf->getNumericValue("Polycode", "uiButtonSkinR");
  17. float sb = conf->getNumericValue("Polycode", "uiButtonSkinB");
  18. float sl = conf->getNumericValue("Polycode", "uiButtonSkinL");
  19. buttonRect = new UIBox(conf->getStringValue("Polycode", "uiButtonSkin"),
  20. st,sr,sb,sl,
  21. width, height);
  22. buttonFocusedRect= new UIBox(conf->getStringValue("Polycode", "uiButtonFocusedSkin"),
  23. st,sr,sb,sl,
  24. width, height);
  25. addChild(buttonRect);
  26. addChild(buttonFocusedRect);
  27. buttonFocusedRect->visible = false;
  28. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
  29. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  30. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  31. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  32. pressedDown = false;
  33. buttonLabel = new ScreenLabel(fontName, text, fontSize, Label::ANTIALIAS_FULL);
  34. addChild(buttonLabel);
  35. labelXPos = floor((width-buttonLabel->getWidth())/2.0f);
  36. labelYPos = floor((height-(buttonLabel->getHeight()-5))/2.0f);
  37. buttonLabel->setPosition(labelXPos,labelYPos);
  38. this->width = width;
  39. this->height = height;
  40. focusable = true;
  41. }
  42. void UIButton::Update() {
  43. if(hasFocus) {
  44. buttonFocusedRect->visible = true;
  45. buttonRect->visible = false;
  46. } else {
  47. buttonFocusedRect->visible = false;
  48. buttonRect->visible = true;
  49. }
  50. }
  51. UIButton::~UIButton() {
  52. }
  53. void UIButton::handleEvent(Event *event) {
  54. if(event->getDispatcher() == buttonRect) {
  55. switch(event->getEventCode()) {
  56. case InputEvent::EVENT_MOUSEOVER:
  57. break;
  58. case InputEvent::EVENT_MOUSEOUT:
  59. buttonLabel->setPosition(labelXPos,labelYPos);
  60. buttonRect->setPosition(0,0);
  61. pressedDown = false;
  62. break;
  63. case InputEvent::EVENT_MOUSEUP:
  64. buttonLabel->setPosition(labelXPos,labelYPos);
  65. buttonRect->setPosition(0,0);
  66. if(pressedDown) {
  67. dispatchEvent(new UIEvent(), UIEvent::CLICK_EVENT);
  68. }
  69. pressedDown = false;
  70. break;
  71. case InputEvent::EVENT_MOUSEDOWN:
  72. pressedDown = true;
  73. if(parentEntity)
  74. ((ScreenEntity*)parentEntity)->focusChild(this);
  75. buttonLabel->setPosition(labelXPos+1,labelYPos+1);
  76. buttonRect->setPosition(1,1);
  77. break;
  78. }
  79. }
  80. }