GameMenu.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "GameMenu.h"
  2. #include "MainMenuScene.h"
  3. #include "MyButton.h"
  4. #include "res.h"
  5. spGameMenu GameMenu::instance;
  6. GameMenu::GameMenu()
  7. {
  8. //initialize dialog background
  9. _bg = initActor(new Box9Sprite,
  10. arg_attachTo = _view,
  11. arg_resAnim = res::ui.getResAnim("box9"),
  12. arg_pos = _view->getSize() / 2 - Vector2(300, 300) / 2);
  13. _bg->setGuides(30, 30, 30, 30);
  14. //create holder for button and text
  15. _buttons = new Actor;
  16. _buttons->attachTo(_view);
  17. float y = _view->getHeight() / 2 - 80;
  18. //initialize TextStyle for TextField
  19. //TextStyle is plain struct with "setting" for Text
  20. TextStyle style;
  21. style.font = res::ui.getResFont("big");
  22. //vertical align
  23. style.vAlign = TextStyle::VALIGN_BOTTOM;
  24. //horizontal align
  25. style.hAlign = TextStyle::HALIGN_CENTER;
  26. spTextField paused = initActor(new TextField,
  27. arg_style = style,
  28. //colored text by "html" tags
  29. arg_htmlText = "Paused<div c='00FF00'>!</div>",
  30. arg_x = _view->getWidth() / 2,
  31. arg_y = y,
  32. arg_attachTo = _buttons,
  33. arg_alpha = 128);
  34. //animate with infinity loops "Paused!" text
  35. paused->addTween(Actor::TweenAlpha(255), 1200, -1, true);
  36. paused->addTween(Actor::TweenScale(1.1f), 1200, -1, true);
  37. y += 70;
  38. //'buttons' image has 2 columns and 5 rows
  39. //game actually uses only 2 selected randomly
  40. const ResAnim* btns = res::ui.getResAnim("buttons");
  41. //create 2 buttons
  42. const char* txt[2] = {"Continue", "Exit"};
  43. for (int i = 0; i < 2; ++i)
  44. {
  45. //select random frame of image
  46. const AnimationFrame& frame = btns->getFrame(rand() % btns->getColumns(), rand() % btns->getRows());
  47. //MyButton is user class inherited from Sprite
  48. spMyButton button = initActor(new MyButton,
  49. arg_resAnim = frame,
  50. arg_anchor = Vector2(0.5f, 0.5f),
  51. arg_attachTo = _buttons);
  52. button->setX(_view->getWidth() / 2);
  53. button->setY(y);
  54. button->setText(txt[i]);
  55. y += button->getHeight() + 10;
  56. button->setName(txt[i]);
  57. //handle click on button
  58. //each Object could have unique name. In this example button has the same name as text
  59. button->addEventListener(TouchEvent::CLICK, CLOSURE(this, &GameMenu::onEvent));
  60. }
  61. }
  62. void GameMenu::_show()
  63. {
  64. //before showing dialog hide buttons
  65. _buttons->setAlpha(0);
  66. //animate background
  67. //set default small size
  68. _bg->setSize(_bg->getAnimFrame().getSize());
  69. //and modify height and then width by tween
  70. spTweenQueue tq = new TweenQueue;
  71. tq->add(Actor::TweenHeight(300), 500);
  72. tq->add(Actor::TweenWidth(300), 500);
  73. //and show buttons when done
  74. tq->addDoneCallback(CLOSURE(this, &GameMenu::showButtons));
  75. _bg->addTween(tq);
  76. }
  77. void GameMenu::showButtons(Event* ev)
  78. {
  79. //tween activated from GameMenu::_show is done
  80. //fade in buttons and text
  81. _buttons->addTween(Actor::TweenAlpha(255), 300);
  82. }
  83. void GameMenu::onEvent(Event* ev)
  84. {
  85. //button clicked
  86. //remember it's name. It would asked later from GameScene.cpp
  87. _lastClicked = ev->currentTarget->getName();
  88. //hide dialog
  89. hide();
  90. }