Fixed Input IsKeyPressed system, F6 to pause the game while running, F6 to start/stop

This commit is contained in:
Antoine Pilote
2024-04-27 15:17:58 -04:00
parent 4e8824bc1d
commit bf8557310e
9 changed files with 40 additions and 36 deletions

View File

@@ -3,6 +3,8 @@
#include "Windows/EditorInterface.h"
#include "Misc/GizmoDrawer.h"
#include "src/Core/Input.h"
#include <glad/glad.h>
@@ -65,6 +67,8 @@ void EditorLayer::OnUpdate()
m_EditorInterface->Draw();
m_EditorInterface->Update(Nuake::Engine::GetTimestep());
Nuake::Engine::EndDraw();
Nuake::Input::Update();
}
void EditorLayer::OnDetach()

View File

@@ -31,7 +31,7 @@ namespace Nuake
bool dragging2 = false;
void NewEditor::Update(Timestep ts)
{
if (Input::IsKeyPressed(GLFW_KEY_F1))
if (Input::IsKeyPressed(Key::F1))
{
m_UserInterface->Reload();
FetchNodes();

View File

@@ -327,7 +327,7 @@ namespace Nuake {
if (Engine::IsPlayMode() && Engine::GetTimeScale() != 0.0f)
{
if (ImGui::Button(ICON_FA_PAUSE, ImVec2(30, 30)) || (Input::IsKeyDown(GLFW_KEY_F5)))
if (ImGui::Button(ICON_FA_PAUSE, ImVec2(30, 30)) || (Input::IsKeyPressed(Key::F6)))
{
Engine::SetGameState(GameState::Paused);
@@ -336,7 +336,7 @@ namespace Nuake {
}
else
{
if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30)))
if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30)) || Input::IsKeyPressed(Key::F5))
{
if (Engine::GetGameState() == GameState::Paused)
{
@@ -380,7 +380,7 @@ namespace Nuake {
ImGui::BeginDisabled();
}
if ((ImGui::Button(ICON_FA_STOP, ImVec2(30, 30)) || Input::IsKeyPressed(GLFW_KEY_F5)) && Engine::IsPlayMode())
if ((ImGui::Button(ICON_FA_STOP, ImVec2(30, 30)) || Input::IsKeyPressed(Key::F5)) && Engine::IsPlayMode())
{
Engine::ExitPlayMode();
@@ -1967,17 +1967,17 @@ namespace Nuake {
if (ImGui::Begin("Example: Simple overlay", &m_ShowOverlay, window_flags))
{
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 100);
if (ImGui::Button(ICON_FA_ARROWS_ALT) || Input::IsKeyDown(GLFW_KEY_W))
if (ImGui::Button(ICON_FA_ARROWS_ALT) || Input::IsKeyDown(Key::W))
{
CurrentOperation = ImGuizmo::OPERATION::TRANSLATE;
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_SYNC_ALT) || Input::IsKeyDown(GLFW_KEY_E))
if (ImGui::Button(ICON_FA_SYNC_ALT) || Input::IsKeyDown(Key::E))
{
CurrentOperation = ImGuizmo::OPERATION::ROTATE;
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_EXPAND_ALT) || Input::IsKeyDown(GLFW_KEY_R))
if (ImGui::Button(ICON_FA_EXPAND_ALT) || Input::IsKeyDown(Key::R))
{
CurrentOperation = ImGuizmo::OPERATION::SCALE;
}
@@ -2388,13 +2388,13 @@ namespace Nuake {
editorCam->Update(ts, m_IsHoveringViewport && m_IsViewportFocused);
const bool entityIsSelected = Selection.Type == EditorSelectionType::Entity && Selection.Entity.IsValid();
if (editorCam->IsFlying() && entityIsSelected && Input::IsKeyPressed(GLFW_KEY_F))
if (editorCam->IsFlying() && entityIsSelected && Input::IsKeyPressed(Key::F))
{
editorCam->IsMoving = true;
editorCam->TargetPos = Selection.Entity.GetComponent<TransformComponent>().GetGlobalPosition();
}
if (entityIsSelected && Input::IsKeyPressed(GLFW_KEY_ESCAPE))
if (entityIsSelected && Input::IsKeyPressed(Key::ESCAPE))
{
Selection = EditorSelection();
}

View File

@@ -71,9 +71,11 @@ namespace Nuake
s_FixedUpdateDifference -= s_FixedUpdateRate;
}
Input::Update();
}
Input::Update();
}
void Engine::EnterPlayMode()

View File

@@ -22,29 +22,27 @@ namespace Nuake
#pragma region Keys
// Only true if the key is currently being pressed
bool Input::IsKeyDown(int keycode)
bool Input::IsKeyDown(Key keycode)
{
auto window = Window::Get()->GetHandle();
int state = glfwGetKey(window, keycode);
int state = glfwGetKey(window, (int)keycode);
bool result = state == GLFW_PRESS;
m_Keys[keycode] = state;
return result;
}
// Only true if the key is pressed for the first frame. no repeat.
bool Input::IsKeyPressed(int keycode)
bool Input::IsKeyPressed(Key keycode)
{
auto window = Window::Get()->GetHandle();
int state = glfwGetKey(window, keycode);
int state = glfwGetKey(window, (int)keycode);
bool result = state == GLFW_PRESS;
// First time pressed?
if (m_Keys.find(keycode) == m_Keys.end() || m_Keys[keycode] == false)
if (m_Keys.find((int)keycode) == m_Keys.end() || m_Keys[(int)keycode] == false)
{
if (result)
m_Keys[keycode] = true;
m_Keys[(int)keycode] = true;
return result;
}
@@ -172,7 +170,7 @@ namespace Nuake
// Reset all input to false.
for (auto& k : m_Keys)
{
if (!IsKeyDown(k.first))
if (!IsKeyDown((Key)k.first))
{
k.second = false;
}

View File

@@ -138,8 +138,8 @@ namespace Nuake
static bool m_MouseButtons[5];
static std::map<int, bool> m_Keys;
public:
static bool IsKeyPressed(int keycode);
static bool IsKeyDown(int keycode);
static bool IsKeyPressed(Key keycode);
static bool IsKeyDown(Key keycode);
static bool IsKeyReleased(int keycode);
static float YScroll;

View File

@@ -61,7 +61,7 @@ namespace Nuake
Input::HideMouse();
}
if (Input::IsKeyDown(GLFW_KEY_LEFT_ALT))
if (Input::IsKeyDown(Key::LEFT_ALT))
{
if (Input::YScroll != 0.0f)
{
@@ -96,31 +96,31 @@ namespace Nuake
if (m_Type == CAMERA_TYPE::ORTHO)
{
if (Input::IsKeyDown(GLFW_KEY_RIGHT))
if (Input::IsKeyDown(Key::RIGHT))
Translation.x += Speed * ts;
if (Input::IsKeyDown(GLFW_KEY_LEFT))
if (Input::IsKeyDown(Key::LEFT))
Translation.x -= Speed * ts;
if (Input::IsKeyDown(GLFW_KEY_UP))
if (Input::IsKeyDown(Key::UP))
Translation.y += Speed * ts;
if (Input::IsKeyDown(GLFW_KEY_DOWN))
if (Input::IsKeyDown(Key::DOWN))
Translation.y -= Speed * ts;
}
else
{
auto movement = Vector3(0, 0, 0);
if (Input::IsKeyDown(GLFW_KEY_D))
if (Input::IsKeyDown(Key::D))
movement -= Right * (Speed * ts);
if (Input::IsKeyDown(GLFW_KEY_A))
if (Input::IsKeyDown(Key::A))
movement += Right * (Speed * ts);
if (Input::IsKeyDown(GLFW_KEY_W))
if (Input::IsKeyDown(Key::W))
movement += Direction * (Speed * ts);
if (Input::IsKeyDown(GLFW_KEY_S))
if (Input::IsKeyDown(Key::S))
movement -= Direction * (Speed * ts);
if (Input::IsKeyDown(GLFW_KEY_LEFT_SHIFT))
if (Input::IsKeyDown(Key::LEFT_SHIFT))
movement -= Up * (Speed * ts);
if (Input::IsKeyDown(GLFW_KEY_SPACE))
if (Input::IsKeyDown(Key::SPACE))
movement += Up * (Speed * ts);
Translation += Vector3(movement);

View File

@@ -69,14 +69,14 @@ namespace Nuake {
static void IsKeyDown(WrenVM* vm)
{
int key = (int)wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyDown(key);
bool result = Input::IsKeyDown((Key)key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyPressed(WrenVM* vm)
{
int key = (int)wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyPressed(key);
bool result = Input::IsKeyPressed((Key)key);
wrenSetSlotBool(vm, 0, result);
}

View File

@@ -25,12 +25,12 @@ namespace Nuake {
bool IsKeyDown(int keyCode)
{
return Input::IsKeyDown(keyCode);
return Input::IsKeyDown((Key)keyCode);
}
bool IsKeyPressed(int keyCode)
{
return Input::IsKeyPressed(keyCode);
return Input::IsKeyPressed((Key)keyCode);
}
Coral::Array<float> GetMousePosition()