MainMenu.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "MainMenu.h"
  2. #include "RootActor.h"
  3. #include "GameActor.h"
  4. #include "Tweener.h"
  5. #include "OptionsMenu.h"
  6. #include "shared.h"
  7. spMainMenu MainMenu::instance;
  8. ButtonWithText::ButtonWithText():enabled(true)
  9. {
  10. _text = new TextActor;
  11. addChild(_text);
  12. setAnchor(Vector2(0.5f, 0.5f));
  13. }
  14. ButtonWithText::~ButtonWithText()
  15. {
  16. }
  17. void ButtonWithText::init(const string &text, EventCallback &cb, const Vector2 &pos, const string &name)
  18. {
  19. setName(name);
  20. setPosition(pos);
  21. addEventListener(TouchEvent::CLICK, cb);
  22. setChildrenRelative(false);
  23. setResAnim(gameResources.getResAnim("button"));
  24. TextStyle style = basicStyle;
  25. style.hAlign = TextStyle::HALIGN_CENTER;
  26. style.vAlign = TextStyle::VALIGN_MIDDLE;
  27. style.multiline = false;
  28. _text->setStyle(style);
  29. setText(text);
  30. }
  31. void ButtonWithText::setText(const string &str)
  32. {
  33. _text->setText(str);
  34. }
  35. MainMenu::MainMenu()
  36. {
  37. spClock clock = new Clock();
  38. clock->setFixedStep(1000/50.0f);
  39. _actor->setClock(clock);
  40. _actor->setSize(getRoot()->getSize());
  41. spSprite bg = new Sprite();
  42. bg->setAnimFrame(gameResources.getResAnim("bg"));
  43. _actor->addChild(bg);
  44. EventCallback cb = CLOSURE(this, &MainMenu::clickedButton);
  45. int dy = 70;
  46. Vector2 pos(virtualSize.x/2.0f, 100);
  47. _play = new ButtonWithText;
  48. _play->init(getString("main_menu", "play"), cb, pos, "play");
  49. pos.y += dy;
  50. _options = new ButtonWithText;
  51. _options->init(getString("main_menu", "options"), cb, pos, "options");
  52. pos.y += dy;
  53. _menu = new Actor;
  54. _menu->addChild(_play);
  55. _menu->addChild(_options);
  56. _actor->addChild(_menu);
  57. }
  58. MainMenu::~MainMenu()
  59. {
  60. }
  61. void MainMenu::doLoop()
  62. {
  63. while(1)
  64. {
  65. string action = waitAction();
  66. if (action == "play")
  67. {
  68. hiding();
  69. spActor parent = _actor->detach();
  70. spGameActor game = new GameActor();
  71. getRoot()->addChild(game->_actor);
  72. game->loop();
  73. game->_actor->detach();
  74. parent->addChild(this->_actor);
  75. showing();
  76. }
  77. if (action == "options")
  78. {
  79. _menu->setInputEnabled(false);
  80. _menu->setAlpha(255);
  81. spTween t = _menu->addTween(Actor::TweenAlpha(0), 250);
  82. blocking::waitTween(t);
  83. _menu->setInputEnabled(true);
  84. spOptionsMenu opt = OptionsMenu::instance;
  85. getRoot()->addChild(opt->_actor);
  86. opt->loop();
  87. opt->_actor->detach();
  88. _menu->setInputEnabled(false);
  89. _menu->setAlpha(0);
  90. t = _menu->addTween(Actor::TweenAlpha(255), 250);
  91. blocking::waitTween(t);
  92. _menu->setInputEnabled(true);
  93. }
  94. }
  95. }
  96. void MainMenu::postHiding()
  97. {
  98. }
  99. void MainMenu::postShowing()
  100. {
  101. }
  102. void MainMenu::doUpdate(const UpdateState &us)
  103. {
  104. }
  105. void MainMenu::clickedButton(Event *es)
  106. {
  107. generateActionByEvent(es);
  108. }