2
0

PolyUIImageButton.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * PolyUIImageButton.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 7/29/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyUIImageButton.h"
  10. using namespace Polycode;
  11. UIImageButton::UIImageButton(String imageName) : ScreenEntity() {
  12. setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  13. buttonImage = new ScreenImage(imageName.c_str());
  14. addChild(buttonImage);
  15. buttonRect = new ScreenShape(ScreenShape::SHAPE_RECT, buttonImage->getWidth(),buttonImage->getHeight(),0,0);
  16. buttonRect->setColor(1,1,1,0);
  17. buttonRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  18. addChild(buttonRect);
  19. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
  20. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  21. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  22. buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  23. pressedDown = false;
  24. width = buttonRect->getWidth();
  25. height = buttonRect->getHeight();
  26. }
  27. void UIImageButton::handleEvent(Event *event) {
  28. if(event->getDispatcher() == buttonRect) {
  29. switch(event->getEventCode()) {
  30. case InputEvent::EVENT_MOUSEOVER:
  31. this->setColor(0.7,0.7,0.7,1);
  32. break;
  33. case InputEvent::EVENT_MOUSEOUT:
  34. pressedDown = false;
  35. this->setColor(1,1,1,1);
  36. break;
  37. case InputEvent::EVENT_MOUSEUP:
  38. this->setColor(0.7,0.7,0.7,1);
  39. if(pressedDown) {
  40. dispatchEvent(new UIEvent(), UIEvent::CLICK_EVENT);
  41. }
  42. pressedDown = false;
  43. break;
  44. case InputEvent::EVENT_MOUSEDOWN:
  45. pressedDown = true;
  46. this->setColor(0.5,0.5,0.5,1);
  47. break;
  48. }
  49. }
  50. }
  51. UIImageButton::~UIImageButton() {
  52. }