aboutsummaryrefslogtreecommitdiff
path: root/source/Game/EditorCommandPalette.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-06-03 23:26:44 -0700
committerrtk0c <[email protected]>2022-06-03 23:26:44 -0700
commit60ccc62f4934e44ad5b905fdbcf458302b8d8a09 (patch)
tree02ec83cc8387abfd08bd5ee7ea4e8115f1bfb8d0 /source/Game/EditorCommandPalette.hpp
parentc2ef7737536bf1f8c81fcfae95c0183b21c9753f (diff)
Changeset: 63 [WIP] Rename directories
Diffstat (limited to 'source/Game/EditorCommandPalette.hpp')
-rw-r--r--source/Game/EditorCommandPalette.hpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/source/Game/EditorCommandPalette.hpp b/source/Game/EditorCommandPalette.hpp
new file mode 100644
index 0000000..101344d
--- /dev/null
+++ b/source/Game/EditorCommandPalette.hpp
@@ -0,0 +1,94 @@
+#pragma once
+
+#include <imgui.h>
+#include <cstddef>
+#include <functional>
+#include <string>
+#include <string_view>
+
+class EditorCommandExecuteContext;
+class EditorCommand {
+public:
+ std::string name;
+ std::function<void(EditorCommandExecuteContext& ctx)> callback;
+ std::function<void(EditorCommandExecuteContext& ctx, size_t selectedOptionId)> subsequentCallback;
+ std::function<void()> terminate;
+};
+
+class EditorCommandExecuteContext {
+ friend class EditorCommandPalette;
+
+private:
+ const EditorCommand* mCommand = nullptr;
+ std::vector<std::string> mCurrentOptions;
+ int mDepth = 0;
+
+public:
+ bool IsInitiated() const;
+ const EditorCommand* GetCurrentCommand() const;
+ void Initiate(const EditorCommand& command);
+
+ void Prompt(std::vector<std::string> options);
+ void Finish();
+
+ /// Return the number of prompts that the user is currently completing. For example, when the user opens command
+ /// palette fresh and selects a command, 0 is returned. If the command asks some prompt, and then the user selects
+ /// again, 1 is returned.
+ int GetExecutionDepth() const;
+};
+
+class EditorCommandPalette {
+private:
+ struct SearchResult;
+ struct Item;
+
+ std::vector<EditorCommand> mCommands;
+ std::vector<Item> mItems;
+ std::vector<SearchResult> mSearchResults;
+ std::string mSearchText;
+ EditorCommandExecuteContext mExeCtx;
+ int mFocusedItemId = 0;
+ bool mFocusSearchBox = false;
+ bool mShouldCloseNextFrame = false;
+
+public:
+ EditorCommandPalette();
+ ~EditorCommandPalette();
+
+ EditorCommandPalette(const EditorCommandPalette&) = delete;
+ EditorCommandPalette& operator=(const EditorCommandPalette&) = delete;
+ EditorCommandPalette(EditorCommandPalette&&) = default;
+ EditorCommandPalette& operator=(EditorCommandPalette&&) = default;
+
+ void AddCommand(std::string_view category, std::string_view name, EditorCommand command);
+ void RemoveCommand(std::string_view category, std::string_view name);
+ void RemoveCommand(const std::string& commandName);
+
+ void Show(bool* open = nullptr);
+
+ enum ItemType {
+ CommandItem,
+ CommandOptionItem,
+ };
+
+ enum IndexType {
+ DirectIndex,
+ SearchResultIndex,
+ };
+
+ struct ItemInfo {
+ const char* text;
+ const EditorCommand* command;
+ int itemId;
+ ItemType itemType;
+ IndexType indexType;
+ };
+
+ size_t GetItemCount() const;
+ ItemInfo GetItem(size_t idx) const;
+
+ void SelectFocusedItem();
+
+private:
+ void InvalidateSearchResults();
+};