| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include "MainMenu.h"
- #include "RootActor.h"
- #include "GameActor.h"
- #include "Tweener.h"
- #include "OptionsMenu.h"
- #include "shared.h"
- spMainMenu MainMenu::instance;
- ButtonWithText::ButtonWithText():enabled(true)
- {
- _text = new TextActor;
- addChild(_text);
- setAnchor(Vector2(0.5f, 0.5f));
- }
- ButtonWithText::~ButtonWithText()
- {
- }
- void ButtonWithText::init(const string &text, EventCallback &cb, const Vector2 &pos, const string &name)
- {
- setName(name);
- setPosition(pos);
- addEventListener(TouchEvent::CLICK, cb);
- setChildrenRelative(false);
- setResAnim(gameResources.getResAnim("button"));
- TextStyle style = basicStyle;
- style.hAlign = TextStyle::HALIGN_CENTER;
- style.vAlign = TextStyle::VALIGN_MIDDLE;
- style.multiline = false;
- _text->setStyle(style);
- setText(text);
- }
- void ButtonWithText::setText(const string &str)
- {
- _text->setText(str);
- }
- MainMenu::MainMenu()
- {
- spClock clock = new Clock();
- clock->setFixedStep(1000/50.0f);
- _actor->setClock(clock);
-
- _actor->setSize(getRoot()->getSize());
- spSprite bg = new Sprite();
- bg->setAnimFrame(gameResources.getResAnim("bg"));
- _actor->addChild(bg);
-
- EventCallback cb = CLOSURE(this, &MainMenu::clickedButton);
- int dy = 70;
- Vector2 pos(virtualSize.x/2.0f, 100);
- _play = new ButtonWithText;
- _play->init(getString("main_menu", "play"), cb, pos, "play");
-
- pos.y += dy;
- _options = new ButtonWithText;
- _options->init(getString("main_menu", "options"), cb, pos, "options");
-
- pos.y += dy;
-
-
- _menu = new Actor;
- _menu->addChild(_play);
- _menu->addChild(_options);
- _actor->addChild(_menu);
- }
- MainMenu::~MainMenu()
- {
- }
- void MainMenu::doLoop()
- {
- while(1)
- {
- string action = waitAction();
- if (action == "play")
- {
- hiding();
- spActor parent = _actor->detach();
- spGameActor game = new GameActor();
- getRoot()->addChild(game->_actor);
-
- game->loop();
- game->_actor->detach();
- parent->addChild(this->_actor);
- showing();
- }
- if (action == "options")
- {
- _menu->setInputEnabled(false);
- _menu->setAlpha(255);
- spTween t = _menu->addTween(Actor::TweenAlpha(0), 250);
- blocking::waitTween(t);
- _menu->setInputEnabled(true);
- spOptionsMenu opt = OptionsMenu::instance;
- getRoot()->addChild(opt->_actor);
- opt->loop();
- opt->_actor->detach();
-
- _menu->setInputEnabled(false);
- _menu->setAlpha(0);
- t = _menu->addTween(Actor::TweenAlpha(255), 250);
- blocking::waitTween(t);
- _menu->setInputEnabled(true);
- }
-
- }
- }
- void MainMenu::postHiding()
- {
- }
- void MainMenu::postShowing()
- {
- }
- void MainMenu::doUpdate(const UpdateState &us)
- {
- }
- void MainMenu::clickedButton(Event *es)
- {
- generateActionByEvent(es);
- }
|