R-Type
R-Type project
Menu.hpp
1 #pragma once
2 
3 #include "KapEngine.hpp"
4 #include "UiFactory.hpp"
5 
6 namespace RType {
7  class Menu {
8  protected:
9  KapEngine::KEngine &engine;
11  std::shared_ptr<KapEngine::GameObject> canvas;
12 
13  std::string tmpCanvasToDisplay = "";
14 
15  public:
16  explicit Menu(KapEngine::SceneManagement::Scene &_scene) : engine(_scene.getEngine()), scene(_scene) {}
17 
18  ~Menu() = default;
19 
20  void __initCanvas(const std::string &name) {
21  canvas = KapEngine::UI::UiFactory::createCanvas(scene, "Canvas" + name);
22  canvas->setActive(false);
23  }
24 
25  virtual void init() = 0;
26 
27  void show() { canvas->setActive(true); }
28 
29  void hide() {
30  canvas->setActive(false);
31  canvas->getScene().dump(false);
32  }
33 
34  protected:
35  void switchMenu(std::string const &menuName) {
36  tmpCanvasToDisplay = "Canvas" + menuName;
37  hide();
39  try {
40  auto menuCanvas = scene.findFirstGameObject(tmpCanvasToDisplay);
41  menuCanvas->setActive(true);
42  } catch (...) {
43  show();
44  KAP_DEBUG_ERROR("Failed to switch menu: " + tmpCanvasToDisplay);
45  }
46  });
47  }
48  };
49 } // namespace RType
KapEngine::SceneManagement::Scene::findFirstGameObject
std::shared_ptr< GameObject > findFirstGameObject(std::string const &name)
get first GameObject with it's name
Definition: Scene.cpp:136
RType::Menu
Definition: Menu.hpp:7
KapEngine::SceneManagement::Scene::registerTmpActionAfterUpdate
void registerTmpActionAfterUpdate(std::function< void(Scene &scene)> action)
add temporary action when scene updated
Definition: Scene.hpp:257
KapEngine::SceneManagement::Scene
Definition: Scene.hpp:37
KapEngine::KEngine
Class of engine.
Definition: Engine.hpp:60