Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
EngineImGui.cpp
Go to the documentation of this file.
2 #include "Engine/engine.hpp"
4 
5 #include <ThirdParty/glm/glm/gtx/string_cast.hpp>
6 #include <ThirdParty/glm/glm/gtc/matrix_transform.hpp>
7 
8 namespace Engine
9 {
10  bool open = true;
11 
20 
28 
30  float devMenuCooldown = 0.f;
31  bool showMenuBar = false;
32 
33  EngineImGui::EngineImGui()
34  {
35  Engine::GetEngine().lock()->GetRenderer().lock()->OnRender += Sharp::EventHandler::Bind(&EngineImGui::Render, this);
36  }
37 
39  {
40  Engine::GetEngine().lock()->GetRenderer().lock()->OnRender -= Sharp::EventHandler::Bind(&EngineImGui::Render, this);
41  }
42 
43  void EngineImGui::Render()
44  {
45  if (allowCameraMovement)
46  CameraMovement();
47 
48  const gainput::InputDevice* keyboard = Engine::GetEngine().lock()->GetInputManager().lock()->GetInputManager().GetDevice(
49  Engine::GetEngine().lock()->GetInputManager().lock()->GetKeyboardId());
50 
51  if (keyboard->GetBool(gainput::KeyCtrlL) && keyboard->GetBool(gainput::KeyShiftL) && keyboard->GetBool(gainput::KeyE))
52  {
53  if (devMenuCooldown < 0.f)
54  {
55  showMenuBar = !showMenuBar;
56  devMenuCooldown = 0.25f;
57  }
58  }
59  if (devMenuCooldown >= 0.f)
60  devMenuCooldown -= Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime();
61 
62  if (showMenuBar)
63  {
64  DevMenu();
65  }
66  }
67 
68  void EngineImGui::DevMenu()
69  {
70  ImGui::Begin("Menubar demo", &open, menuBarWindowFlags);
71 
73  if (ImGui::BeginMenu("Menu"))
74  {
75  if (ImGui::BeginMenu("Engine"))
76  {
77  if (ImGui::MenuItem("Close game"))
78  {
79  Engine::Engine::GetEngine().lock()->GetWindow().lock()->SetShouldClose(true);
80  }
81  if (ImGui::MenuItem("Toggle Camera Movement"))
82  {
83  allowCameraMovement = !allowCameraMovement;
84  }
85  if (ImGui::MenuItem("Toggle Play/Pause mode"))
86  {
87  Engine::GetEngine().lock()->SetIsPlaying(!Engine::GetEngine().lock()->GetIsPlaying());
88  }
90  }
92  }
94  ImGui::End();
95 
97  ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x - 175, 0));
98  ImGui::Begin("FPS window", &open, fpsWindowFlags);
99 
100  const eastl::vector<float> deltaTimes = Engine::GetEngine().lock()->GetTime().lock()->GetPreviousFramerates();
101  ImGui::PlotLines("FPS", deltaTimes.data(), Engine::GetEngine().lock()->GetTime().lock()->GetMaxIterations(), 0, std::to_string(ImGui::GetIO().Framerate).c_str());
102 
103  ImGui::End();
104  }
105 
106  void EngineImGui::CameraMovement() const
107  {
108  float camMoveSpeed = 100.f;
109  eastl::weak_ptr<Camera> camera = Engine::GetEngine().lock()->GetCamera();
110 
111  const gainput::InputDevice* keyboard = Engine::GetEngine().lock()->GetInputManager().lock()->GetInputManager().GetDevice(
112  Engine::GetEngine().lock()->GetInputManager().lock()->GetKeyboardId());
113 
114  if (keyboard->GetBool(gainput::KeyW) && !ImGui::GetIO().WantTextInput)
115  {
116  camera.lock()->MoveForwards(Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime() * camMoveSpeed);
117  }
118  if (keyboard->GetBool(gainput::KeyS) && !ImGui::GetIO().WantTextInput)
119  {
120  camera.lock()->MoveBackwards(Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime() * camMoveSpeed);
121  }
122  if (keyboard->GetBool(gainput::KeyA) && !ImGui::GetIO().WantTextInput)
123  {
124  camera.lock()->MoveLeft(Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime() * camMoveSpeed);
125  }
126  if (keyboard->GetBool(gainput::KeyD) && !ImGui::GetIO().WantTextInput)
127  {
128  camera.lock()->MoveRight(Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime() * camMoveSpeed);
129  }
130  if (keyboard->GetBool(gainput::KeyE) && !ImGui::GetIO().WantTextInput)
131  {
132  camera.lock()->SetRotation(camera.lock()->GetRotation() += glm::vec3(0, 1, 0) * Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime());
133  }
134  if (keyboard->GetBool(gainput::KeyQ) && !ImGui::GetIO().WantTextInput)
135  {
136  camera.lock()->SetRotation(camera.lock()->GetRotation() += glm::vec3(0, -1, 0) * Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime());
137  }
138  glm::vec3 target = glm::normalize(camera.lock()->GetPosition() + camera.lock()->GetRotation());
139  camera.lock()->SetView(glm::lookAt(camera.lock()->GetPosition(), camera.lock()->GetPosition() - target, camera.lock()->GetUp()));
140  }
141 } //namespace Engine
bool showMenuBar
Definition: EngineImGui.cpp:31
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:5190
ImGuiWindowFlags fpsWindowFlags
Definition: EngineImGui.cpp:21
bool open
Definition: EngineImGui.cpp:10
ImGuiWindowFlags menuBarWindowFlags
Definition: EngineImGui.cpp:12
Definition: imgui.h:96
IMGUI_API bool BeginMenuBar()
Definition: imgui.cpp:8992
float devMenuCooldown
Definition: EngineImGui.cpp:30
int ImGuiInputTextFlags
Definition: imgui.h:81
static EventHandlerImpl< T > * Bind(void(*nonMemberFunctionToCall)(T &))
Definition: Event.hpp:350
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:3988
int ImGuiWindowFlags
Definition: imgui.h:78
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:2144
IMGUI_API float GetTime()
Definition: imgui.cpp:2160
IMGUI_API void End()
Definition: imgui.cpp:4537
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:5175
IMGUI_API bool BeginMenu(const char *label, bool enabled=true)
Definition: imgui.cpp:9029
IMGUI_API bool MenuItem(const char *label, const char *shortcut=NULL, bool selected=false, bool enabled=true)
Definition: imgui.cpp:8929
bool WantTextInput
Definition: imgui.h:858
ImGuiInputTextFlags textFlags
Definition: EngineImGui.cpp:29
IMGUI_API void EndMenuBar()
Definition: imgui.cpp:9012
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
Definition: engine.cpp:130
IMGUI_API void EndMenu()
Definition: imgui.cpp:9139
IMGUI_API void PlotLines(const char *label, const float *values, int values_count, int values_offset=0, const char *overlay_text=NULL, float scale_min=FLT_MAX, float scale_max=FLT_MAX, ImVec2 graph_size=ImVec2(0, 0), int stride=sizeof(float))
Definition: imgui.cpp:7369