MyButton.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "MyButton.h"
  2. #include "res.h"
  3. MyButton::MyButton()
  4. {
  5. //pressed button should be RED
  6. addEventListener(TouchEvent::TOUCH_DOWN, CLOSURE(this, &MyButton::onEvent));
  7. addEventListener(TouchEvent::TOUCH_UP, CLOSURE(this, &MyButton::onEvent));
  8. addEventListener(TouchEvent::CLICK, CLOSURE(this, &MyButton::onEvent));
  9. }
  10. void MyButton::onEvent(Event* ev)
  11. {
  12. TouchEvent* event = static_cast<TouchEvent*>(ev);
  13. if (ev->type == TouchEvent::TOUCH_DOWN)
  14. setColor(Color::Red);
  15. if (ev->type == TouchEvent::TOUCH_UP)
  16. setColor(Color::White);
  17. if (ev->type == TouchEvent::CLICK)
  18. {
  19. //clicked button should scale up and down
  20. setScale(1.0f);
  21. addTween(Actor::TweenScale(1.1f), 300, 1, true);
  22. }
  23. }
  24. void MyButton::setText(const string& txt)
  25. {
  26. if (!_text)
  27. {
  28. //create TextField if it wasn't created yet
  29. TextStyle style;
  30. style.font = res::ui.getResFont("normal");
  31. style.vAlign = TextStyle::VALIGN_MIDDLE;
  32. style.hAlign = TextStyle::HALIGN_MIDDLE;
  33. //attach it to MyButton and set the same size as button
  34. //text would be centered
  35. _text = initActor(new TextField,
  36. arg_style = style,
  37. arg_size = getSize(),
  38. arg_attachTo = this);
  39. }
  40. _text->setText(txt);
  41. }