Jelajahi Sumber

mostly finished shortcut api

vlod 3 tahun lalu
induk
melakukan
51992249e4

+ 65 - 0
Pika/core/pikaEditor/editShortcuts/editShortcuts.cpp

@@ -0,0 +1,65 @@
+#include "editShortcuts.h"
+#include <imgui.h>
+
+void pika::EditShortcutsWindow::init()
+{
+}
+
+void pika::EditShortcutsWindow::update(pika::ShortcutManager &shortcutManager, bool &open)
+{
+
+	//todo push pop id
+
+	ImGui::SetNextWindowSize({400, 500});
+
+	if (ImGui::Begin(ICON_NAME, &open,
+		ImGuiWindowFlags_NoDocking | 
+		ImGuiWindowFlags_NoResize |
+		ImGuiWindowFlags_NoCollapse
+		))
+	{
+		
+		ImGui::Text("Edit shortcuts\n");
+
+		//todo unique id
+		if (ImGui::BeginChild(12, {}, true))
+		{
+
+
+			ImGui::Columns(2, 0, false);
+
+			for (auto &shortcut : shortcutManager.registeredShortcuts)
+			{
+
+				ImGui::Text(shortcut.first.c_str());
+
+				ImGui::NextColumn();
+
+
+				char input[256] = {};
+				std::strncpy(input, shortcut.second.shortcut.c_str(), sizeof(input));
+				
+				if (
+					ImGui::InputText(("##" + shortcut.first).c_str(),
+					input, sizeof(input), ImGuiInputTextFlags_EnterReturnsTrue)
+					)
+				{
+					shortcut.second.shortcut = pika::normalizeShortcutName(input);
+
+				}
+
+				ImGui::NextColumn();
+			}
+
+			ImGui::Columns(1);
+
+			ImGui::EndChild();
+		}
+
+
+
+	}
+	ImGui::End();
+
+}
+

+ 26 - 0
Pika/core/pikaEditor/editShortcuts/editShortcuts.h

@@ -0,0 +1,26 @@
+#pragma once
+#include <IconsForkAwesome.h>
+#include <shortcutApi/shortcutApi.h>
+
+namespace pika
+{
+
+	struct EditShortcutsWindow
+	{
+
+
+		void init();
+
+		void update(pika::ShortcutManager &shortcutManager, bool &open);
+
+		static constexpr char *ICON = ICON_FK_PENCIL_SQUARE;
+		static constexpr char *NAME = "Edit Shortcuts...";
+		static constexpr char *ICON_NAME = ICON_FK_PENCIL_SQUARE " Edit Shortcuts...";
+
+	};
+
+
+
+
+
+};

+ 46 - 6
Pika/core/pikaEditor/editor/editor.cpp

@@ -2,20 +2,32 @@
 #include <iostream>
 #include "IconsForkAwesome.h"
 #include "shortcutApi/shortcutApi.h"
+#include <editShortcuts/editShortcuts.h>
+
+#define DOCK_MAIN_WINDOW_SHORTCUT "Dock main window"
+#define LOGS_SHORTCUT "Logs window"
+#define EDIT_SHORTCUTS "Edit shortcuts window"
 
 void pika::Editor::init(pika::ShortcutManager &shortcutManager)
 {
 
-	shortcutManager.registerShortcut("Ctrl+Alt+D", &optionsFlags.dockMainWindow);
-	shortcutManager.registerShortcut("Ctrl+L", &windowFlags.logsWindow);
+	shortcutManager.registerShortcut(DOCK_MAIN_WINDOW_SHORTCUT, "Ctrl+Alt+D", &optionsFlags.dockMainWindow);
+	shortcutManager.registerShortcut(LOGS_SHORTCUT, "Ctrl+L", &windowFlags.logsWindow);
+	shortcutManager.registerShortcut(EDIT_SHORTCUTS, "", &windowFlags.editShortcutsWindow);
+
 
 
+	logWindow.init();
+	editShortcutsWindow.init();
 }
 
 
 
-void pika::Editor::update(const pika::Input &input)
+void pika::Editor::update(const pika::Input &input,
+	pika::ShortcutManager &shortcutManager, pika::LogManager &logs)
 {
+
+#pragma region docking space init
 	ImGuiWindowFlags mainWindowFlags = ImGuiWindowFlags_MenuBar;
 	if (optionsFlags.dockMainWindow)
 	{
@@ -32,8 +44,11 @@ void pika::Editor::update(const pika::Input &input)
 		ImGui::SetNextWindowPos(ImVec2((float)vPos0.x, (float)vPos0.y), ImGuiCond_Always);
 		ImGui::SetNextWindowSize(ImVec2((float)vWindowSize.x, (float)vWindowSize.y), 0);
 	}
+#pragma endregion
 
+#pragma region main editor window
 
+	
 	//ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.2f, 0.2f, 0.3f, 1.0f));
 	
 	//todo imgui push pop id for main window
@@ -70,20 +85,31 @@ void pika::Editor::update(const pika::Input &input)
 			{
 
 
-				ImGui::MenuItem(ICON_FK_WINDOW_RESTORE " Dock main window", "Ctrl+Alt+D", &optionsFlags.dockMainWindow);
+				ImGui::MenuItem(ICON_FK_WINDOW_RESTORE " " DOCK_MAIN_WINDOW_SHORTCUT,
+					shortcutManager.getShortcut(DOCK_MAIN_WINDOW_SHORTCUT), &optionsFlags.dockMainWindow);
 
 				ImGui::EndMenu();
 			}
 
-			if (ImGui::BeginMenu("Windows"))
+			if (ImGui::BeginMenu(ICON_FK_WINDOW_MAXIMIZE " Windows"))
 			{
-				ImGui::MenuItem(pika::LogWindow::ICON_NAME, "Ctrl+L", &windowFlags.logsWindow);
+				ImGui::MenuItem(pika::LogWindow::ICON_NAME, 
+					shortcutManager.getShortcut(LOGS_SHORTCUT), &windowFlags.logsWindow);
 
 
 				ImGui::EndMenu();
 
 			}
 
+			if (ImGui::BeginMenu(ICON_FK_COG " Settings"))
+			{
+				ImGui::MenuItem(pika::EditShortcutsWindow::ICON_NAME ,
+					shortcutManager.getShortcut(EDIT_SHORTCUTS), &windowFlags.editShortcutsWindow);
+
+
+				ImGui::EndMenu();
+			}
+
 			ImGui::EndMenuBar();
 		}
 	#pragma endregion
@@ -94,7 +120,21 @@ void pika::Editor::update(const pika::Input &input)
 		//ImGui::PopStyleColor();
 	}
 	ImGui::End();
+#pragma endregion
+
+#pragma region log window
+
+	if (windowFlags.logsWindow)
+	{
+		logWindow.update(logs, windowFlags.logsWindow);
+	}
+
+	if (windowFlags.editShortcutsWindow)
+	{
+		editShortcutsWindow.update(shortcutManager, windowFlags.editShortcutsWindow);
+	}
 
+#pragma endregion
 
 
 }

+ 7 - 2
Pika/core/pikaEditor/editor/editor.h

@@ -4,6 +4,7 @@
 #include <logs/logWindow.h>
 #include <windowSystemm/input.h>
 #include <shortcutApi/shortcutApi.h>
+#include <editShortcuts/editShortcuts.h>
 
 namespace pika
 {
@@ -13,7 +14,8 @@ namespace pika
 
 		void init(pika::ShortcutManager &shortcutManager);
 
-		void update(const pika::Input &input);
+		void update(const pika::Input &input, pika::ShortcutManager &shortcutManager
+			,pika::LogManager &logs);
 
 		struct
 		{
@@ -22,9 +24,12 @@ namespace pika
 
 		struct
 		{
-			bool logsWindow;
+			bool logsWindow = 0;
+			bool editShortcutsWindow = 0;
 		}windowFlags;
 
+		pika::LogWindow logWindow;
+		pika::EditShortcutsWindow editShortcutsWindow;
 	};
 
 

+ 2 - 2
Pika/core/pikaEditor/logs/logWindow.cpp

@@ -8,12 +8,12 @@ void pika::LogWindow::init()
 
 }
 
-void pika::LogWindow::update(pika::LogManager &logManager)
+void pika::LogWindow::update(pika::LogManager &logManager, bool &open)
 {
 	//todo push pop id
 
 
-	if (ImGui::Begin(ICON_NAME))
+	if (ImGui::Begin(ICON_NAME, &open))
 	{
 
 

+ 1 - 2
Pika/core/pikaEditor/logs/logWindow.h

@@ -11,8 +11,7 @@ namespace pika
 
 		void init();
 
-		void update(pika::LogManager &logManager);
-
+		void update(pika::LogManager &logManager, bool &open);
 
 		static constexpr char *ICON = ICON_FK_COMMENT_O;
 		static constexpr char *NAME = "logs";

+ 1 - 7
Pika/core/pikaRuntime/pikaMain.cpp

@@ -27,9 +27,6 @@ int main()
 	pika::LogManager logs;
 	logs.init("logs.txt");
 
-	pika::LogWindow logWindow;
-	logWindow.init();
-
 #pragma endregion
 
 #pragma region load dll
@@ -98,16 +95,13 @@ int main()
 
 	#pragma region editor stuff
 
-		editor.update(window.input);
+		editor.update(window.input, shortcutManager, logs);
 
-		logWindow.update(logs);
 
 	#pragma endregion
 
 	
 
-
-
 		container.pointer->update(window.input, window.deltaTime, window.windowState);
 
 	#pragma region end imgui frame

+ 109 - 57
Pika/core/sharedRuntime/shortcutApi/shortcutApi.cpp

@@ -4,47 +4,55 @@
 #include <unordered_map>
 #include <set>
 #include <imgui.h>
+#include <imgui.h>
+
+struct Mapping
+{
+	short normal = 0;
+	short imgui = 0;
+};
 
-std::unordered_map<std::string, int> buttonMapping =
+std::unordered_map<std::string, Mapping> buttonMapping =
 {
-	{"a", pika::Button::A},
-	{ "b", pika::Button::B },
-	{ "c", pika::Button::C },
-	{ "d", pika::Button::D },
-	{ "e", pika::Button::E },
-	{ "f", pika::Button::F },
-	{ "g", pika::Button::G },
-	{ "h", pika::Button::H },
-	{ "i", pika::Button::I },
-	{ "j", pika::Button::J },
-	{ "k", pika::Button::K },
-	{ "l", pika::Button::L },
-	{ "m", pika::Button::M },
-	{ "n", pika::Button::N },
-	{ "o", pika::Button::O },
-	{ "p", pika::Button::P },
-	{ "q", pika::Button::Q },
-	{ "r", pika::Button::R },
-	{ "s", pika::Button::S },
-	{ "t", pika::Button::T },
-	{ "u", pika::Button::U },
-	{ "v", pika::Button::V },
-	{ "w", pika::Button::W },
-	{ "x", pika::Button::X },
-	{ "y", pika::Button::Y },
-	{ "z", pika::Button::Z },
-	{ "0", pika::Button::NR0 }, { "1", pika::Button::NR1 }, { "2", pika::Button::NR2 }, { "3", pika::Button::NR3 },
-	{ "4", pika::Button::NR4 }, { "5", pika::Button::NR5 }, { "6", pika::Button::NR6 }, { "7", pika::Button::NR7 },
-	{ "8", pika::Button::NR8 }, { "9", pika::Button::NR9 },
-	{ "space", pika::Button::Space },
-	{ "enter", pika::Button::Enter },
-	{ "escape", pika::Button::Escape },
-	{ "up", pika::Button::Up },
-	{ "down", pika::Button::Down },
-	{ "left", pika::Button::Left },
-	{ "right", pika::Button::Right },
-	{ "ctrl", pika::Button::LeftCtrl },
-	{ "tab", pika::Button::Tab },
+	{"a", {pika::Button::A, ImGuiKey_A}},
+	{ "b", {pika::Button::B, ImGuiKey_B} },
+	{ "c", {pika::Button::C, ImGuiKey_C} },
+	{ "d", {pika::Button::D, ImGuiKey_D} },
+	{ "e", {pika::Button::E, ImGuiKey_E} },
+	{ "f", {pika::Button::F, ImGuiKey_F} },
+	{ "g", {pika::Button::G, ImGuiKey_G} },
+	{ "h", {pika::Button::H, ImGuiKey_H} },
+	{ "i", {pika::Button::I, ImGuiKey_I} },
+	{ "j", {pika::Button::J, ImGuiKey_J} },
+	{ "k", {pika::Button::K, ImGuiKey_K} },
+	{ "l", {pika::Button::L, ImGuiKey_L} },
+	{ "m", {pika::Button::M, ImGuiKey_M} },
+	{ "n", {pika::Button::N, ImGuiKey_N} },
+	{ "o", {pika::Button::O, ImGuiKey_O} },
+	{ "p", {pika::Button::P, ImGuiKey_P} },
+	{ "q", {pika::Button::Q, ImGuiKey_Q} },
+	{ "r", {pika::Button::R, ImGuiKey_R} },
+	{ "s", {pika::Button::S, ImGuiKey_S} },
+	{ "t", {pika::Button::T, ImGuiKey_T} },
+	{ "u", {pika::Button::U, ImGuiKey_U} },
+	{ "v", {pika::Button::V, ImGuiKey_V} },
+	{ "w", {pika::Button::W, ImGuiKey_W} },
+	{ "x", {pika::Button::X, ImGuiKey_X} },
+	{ "y", {pika::Button::Y, ImGuiKey_Y} },
+	{ "z", {pika::Button::Z, ImGuiKey_Z} },
+	{ "0", {pika::Button::NR0, ImGuiKey_0}}, { "1", {pika::Button::NR1, ImGuiKey_1} }, { "2", {pika::Button::NR2, ImGuiKey_2} }, { "3", {pika::Button::NR3, ImGuiKey_3} },
+	{ "4", {pika::Button::NR4, ImGuiKey_0}}, { "5", {pika::Button::NR5, ImGuiKey_5} }, { "6", {pika::Button::NR6, ImGuiKey_6} }, { "7", {pika::Button::NR7, ImGuiKey_7} },
+	{ "8", {pika::Button::NR8, ImGuiKey_8}}, { "9", {pika::Button::NR9, ImGuiKey_9} },
+	{ "space", {pika::Button::Space , ImGuiKey_Space}},
+	{ "enter", {pika::Button::Enter, ImGuiKey_Enter} },
+	{ "escape", {pika::Button::Escape, ImGuiKey_Escape} },
+	{ "up", {pika::Button::Up, ImGuiKey_UpArrow} },
+	{ "down", {pika::Button::Down , ImGuiKey_DownArrow}},
+	{ "left", {pika::Button::Left , ImGuiKey_LeftArrow}},
+	{ "right", {pika::Button::Right , ImGuiKey_RightArrow}},
+	{ "ctrl", {pika::Button::LeftCtrl , ImGuiKey_LeftCtrl}},
+	{ "tab", {pika::Button::Tab , ImGuiKey_Tab}},
+	{ "alt", {pika::Button::LeftAlt , ImGuiKey_LeftAlt}},
 
 };
 
@@ -103,9 +111,11 @@ std::string normalizeShortcutName(const char *shortcut)
 	std::string ret = "";
 	for (int i = 0; i < t.size(); i++)
 	{
+		t[i][0] = std::toupper(t[i][0]);
+
 		ret += t[i];
 
-		if (i < t.size())
+		if (i < t.size()-1)
 		{
 			ret += "+";
 		}
@@ -114,31 +124,56 @@ std::string normalizeShortcutName(const char *shortcut)
 	return ret;
 }
 
+
+//todo shortcut should rely on glfw backend when imgui is disabeled in production build
 bool shortcut(const pika::Input &input, const char *shortcut)
 {
-	
 	auto token = tokenizeShortcutSimple(shortcut);
+	
+	if (token.empty()) { return 0; }
 
-	bool pressed = false;
-
-	for (auto &t : token)
-	{
 
-		auto it = buttonMapping.find(t);
+	bool pressed = false;
 
-		if (it != buttonMapping.end())
+	if (0)
+	{	//noraml implementation
+		for (auto &t : token)
 		{
-			if (input.buttons[it->second].pressed())
+			auto it = buttonMapping.find(t);
+			if (it != buttonMapping.end())
 			{
-				pressed = true;
+				if (input.buttons[it->second.normal].pressed())
+				{
+					pressed = true;
+				}
+				else if (!input.buttons[it->second.normal].held())
+				{
+					return false;
+				}
 			}
-			else if (!input.buttons[it->second].held())
+		}
+	}
+	else
+	{	//imgui backend
+		for (auto &t : token)
+		{
+			auto it = buttonMapping.find(t);
+			if (it != buttonMapping.end())
 			{
-				return false;
+				if (ImGui::IsKeyPressed(it->second.imgui, false))
+				{
+					pressed = true;
+				}
+				else if (!ImGui::IsKeyDown(it->second.imgui))
+				{
+					return false;
+				}
 			}
 		}
 	}
 
+
+
 	return pressed;
 }
 
@@ -158,21 +193,38 @@ void ShortcutManager::update(const pika::Input &input)
 {
 	for (auto &i : registeredShortcuts)
 	{
-		if (shortcut(input, i.first.c_str()))
+		if (shortcut(input, i.second.shortcut.c_str()))
 		{
-			*i.second = !*i.second;
+			*i.second.toggle = !*i.second.toggle;
 		}
 	}
 
-
 }
 
-void ShortcutManager::registerShortcut(const char *s, bool *toggle)
+void ShortcutManager::registerShortcut(const char *name, const char *s, bool *toggle)
 {
 
-	auto normalized = normalizeShortcutName(s);
 
-	registeredShortcuts[normalized] = toggle;
+	if (registeredShortcuts.find(name) != registeredShortcuts.end())
+	{
+		//todo log error
+	}
+	else
+	{
+		registeredShortcuts[name] 
+			= Shortcut{std::move(normalizeShortcutName(s)), toggle};
+	}
+
+
+}
+
+const char *ShortcutManager::getShortcut(const char *name)
+{
+	auto it = registeredShortcuts.find(name);
+
+	if (it == registeredShortcuts.end()) { return ""; }
+	else 
+	{ return it->second.shortcut.c_str(); };
 
 }
 

+ 10 - 2
Pika/core/sharedRuntime/shortcutApi/shortcutApi.h

@@ -7,12 +7,19 @@ namespace pika
 
 	struct ShortcutManager
 	{
+		struct Shortcut
+		{
+			std::string shortcut = "";
+			bool *toggle = 0;
+		};
 
 		void update(const pika::Input &input);
 
-		std::unordered_map<std::string, bool *> registeredShortcuts;
+		std::unordered_map<std::string, Shortcut> registeredShortcuts;
 
-		void registerShortcut(const char *s, bool *toggle);
+		void registerShortcut(const char *name, const char *s, bool *toggle);
+
+		const char *getShortcut(const char *name);
 	};
 
 
@@ -20,5 +27,6 @@ bool shortcut(const pika::Input &input, const char *shortcut);
 
 bool MenuItem(const pika::Input &input, const char *label, const char *shortcut, bool *p_selected, bool enabled = true);
 
+std::string normalizeShortcutName(const char *shortcut);
 
 }

+ 5 - 1
Pika/core/sharedRuntime/windowSystemm/callbacks.cpp

@@ -89,10 +89,14 @@ void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods
 		if (key == GLFW_KEY_LEFT_CONTROL)
 		{
 			processAButton(pikaWindow.input.buttons[pika::Button::LeftCtrl], action);
-		}
+		}else
 		if (key == GLFW_KEY_TAB)
 		{
 			processAButton(pikaWindow.input.buttons[pika::Button::Tab], action);
+		}else
+		if (key == GLFW_KEY_LEFT_ALT)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::LeftAlt], action);
 		}
 	}
 

+ 1 - 1
Pika/core/sharedRuntime/windowSystemm/input.h

@@ -47,7 +47,7 @@ namespace pika
 			NR0, NR1, NR2, NR3, NR4, NR5, NR6, NR7, NR8, NR9,
 			Space, Enter, Escape,
 			Up, Down, Left, Right,
-			LeftCtrl, Tab,
+			LeftCtrl, Tab, LeftAlt,
 			BUTTONS_COUNT, //
 		};