KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
input.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../../ktech.hpp"
25#undef KTECH_DEFINITION
26
27#include <functional>
28#include <string>
29
30#ifdef _WIN32
31#include <Windows.h>
32#undef RGB
33#else
34#include <termio.h>
35#endif
36#include <memory>
37#include <mutex>
38#include <thread>
39
44{
45public:
46 class CallbackGroup;
47
48 std::string input;
49 std::string quitKey{"\x03"};
50
51 [[nodiscard]] auto Is(const std::string& stringKey) const -> bool;
52 [[nodiscard]] auto Is(char charKey) const -> bool;
53 [[nodiscard]] auto Bigger(char charKey) const -> bool;
54 [[nodiscard]] auto Smaller(char charKey) const -> bool;
55 [[nodiscard]] auto Between(char start, char end) const -> bool;
56 [[nodiscard]] auto GetInt() const -> uint8_t;
57
58 void CallCallbacks();
59
60private:
61 struct Handler;
62 struct Callback;
63
64 Engine& engine;
65#ifdef _WIN32
66 HANDLE m_stdinHandle;
67 DWORD m_oldMode;
68#else
69 termios m_oldTerminalAttributes;
70#endif
71 bool m_changedThisTick = false;
72 std::thread m_inputLoop;
73 std::vector<std::string> m_inputQueue;
74 std::mutex m_inputQueueMutex;
75 // Handlers cannot be deleted; their callbacks can be deleted
76 std::vector<std::shared_ptr<Handler>> m_stringHandlers;
77 std::vector<std::shared_ptr<Handler>> m_rangeHandlers;
78 // Groups can be deleted
79 std::vector<CallbackGroup*> m_groups;
80
81 Input(Engine& engine, bool noGameLoopMode);
82 ~Input();
83
84 /*
85 Create new `Callback` at the appropriate `Handler`.
86 If appropriate `Handler` doesn't exist, first creates a new one.
87
88 Registering `Callback`s (using this function) is indirect (the `Callback` is registered at its `Handler`, not at `Input`).
89 On the other hand, removing `Callback`s is direct, because `Callbacks` directly unregisters from its `Handler`.
90 This is why `Callback` has just the register function here.
91 */
92 auto CreateCallback(const std::string& stringKey, const std::function<bool()>& callback) -> std::shared_ptr<Callback>;
93 auto CrateRangedCallback(char start, char end, const std::function<bool()>& callback) -> std::shared_ptr<Callback>;
94
95 /*
96 Register `CallbackGroup`.
97
98 Unlike registering `Callback`s, registering and removing `CallbackGroup`s is direct (the `CallbackGroup` is registered at `Input`).
99 This is why `CallbackGroup` has a register and a remove function here.
100 */
101 void RegisterCallbackGroup(CallbackGroup* callbackGroup);
102 /*
103 Since neither `Handler`, `Callback` nor `CallbackGroup` are expected to be removed at any point, this function prepares to remove the given pointer from `m_groups`.
104
105 This function sets `nullptr` where the given `CallbackGroup*` is given. `Input::Update` sees this and removes it from `m_groups`.
106 */
107 void SetCallbackGroupToBeRemoved(CallbackGroup* callbackGroup);
108
109 void Update();
110 void CallStringHandlers();
111 void CallRangeHandlers();
112 void Get();
113 void Loop();
114
115 friend class Engine;
116 friend class Output;
117};
Complete engine containing all engine components.
Definition engine.hpp:41
Input callbacks creator and manager.
Definition callbackgroup.hpp:33
Engine component responsible for distributing user input.
Definition input.hpp:44
auto Between(char start, char end) const -> bool
Checks if input is between range of characters.
Definition input.cpp:119
std::string quitKey
Input that if received, breaks the input loop and sets Engine::running to false.
Definition input.hpp:49
auto Bigger(char charKey) const -> bool
Checks if given character is bigger than input.
Definition input.cpp:92
auto Is(const std::string &stringKey) const -> bool
Checks if input equals given string.
Definition input.cpp:54
auto Smaller(char charKey) const -> bool
Checks if given character is smaller than input.
Definition input.cpp:105
std::string input
Input for the last-called callback function.
Definition input.hpp:48
auto GetInt() const -> uint8_t
Get the first character of input as a 1-digit number.
Definition input.cpp:79
void CallCallbacks()
Distribute accumulated inputs.
Definition input.cpp:159
Engine component responsible for outputting rendered images.
Definition output.hpp:42