nngn
Loading...
Searching...
No Matches
input.h
Go to the documentation of this file.
1
5#ifndef NNGN_INPUT_INPUT_H
6#define NNGN_INPUT_INPUT_H
7
8#include <bitset>
9#include <cstddef>
10#include <cstdint>
11#include <memory>
12#include <span>
13#include <unordered_map>
14#include <vector>
15
16#include "utils/def.h"
17#include "utils/utils.h"
18
19struct lua_State;
20
21namespace nngn {
22
23using nngn::i32;
24
25class BindingGroup;
26struct Graphics;
27
29class Input {
30public:
36 enum Selector : std::uint8_t {
37 PRESS = 1u << 0, CTRL = 1u << 1, ALT = 1u << 2,
38 };
39 enum Action : std::uint8_t {
41 };
42 enum Modifier : std::uint8_t {
43 MOD_SHIFT = 0b1, MOD_CTRL = 0b10, MOD_ALT = 0b100,
44 };
45 enum {
49 KEY_MAX = 348,
50 };
51 struct Source {
52 virtual ~Source() = 0;
53 virtual void get_keys(std::span<i32> keys) const;
54 virtual bool update(Input*) { return true; }
55 protected:
57 };
58 enum TerminalFlag : std::uint8_t {
60 };
61 void init(lua_State *L_) { this->L = L_; }
62 void get_keys(std::span<i32> keys) const;
63 void has_override(std::size_t n, i32 *keys) const;
64 BindingGroup *binding_group() const { return this->m_binding_group; }
66 void add_source(std::unique_ptr<Source> p);
67 bool remove_source(Source *p);
68 bool override_keys(bool pressed, std::span<const i32> keys);
69 bool register_callback();
70 bool remove_callback();
71 bool update();
72 bool key_callback(int key, Action action, Modifier mods) const;
73private:
74 lua_State *L = {};
75 int callback_ref = {};
77 std::bitset<KEY_MAX + 1> overrides = {};
78 std::vector<std::unique_ptr<Source>> sources = {};
79};
80
81std::unique_ptr<Input::Source> input_terminal_source(
82 int fd, Input::TerminalFlag flags);
83std::unique_ptr<Input::Source> input_graphics_source(Graphics *g);
84
85}
86
87#endif
Definition group.h:10
Dispatches keyboard input events to pre-registered Lua functions.
Definition input.h:29
void add_source(std::unique_ptr< Source > p)
Definition input.cpp:39
Modifier
Definition input.h:42
@ MOD_ALT
Definition input.h:43
@ MOD_SHIFT
Definition input.h:43
@ MOD_CTRL
Definition input.h:43
bool override_keys(bool pressed, std::span< const i32 > keys)
Definition input.cpp:63
TerminalFlag
Definition input.h:58
@ OUTPUT_PROCESSING
Definition input.h:59
bool register_callback()
Definition input.cpp:81
void set_binding_group(BindingGroup *g)
Definition input.h:65
void get_keys(std::span< i32 > keys) const
Definition input.cpp:22
@ KEY_ENTER
Definition input.h:46
@ KEY_DOWN
Definition input.h:47
@ KEY_UP
Definition input.h:47
@ KEY_ESC
Definition input.h:46
@ KEY_PAGE_DOWN
Definition input.h:48
@ KEY_MAX
Definition input.h:49
@ KEY_LEFT
Definition input.h:47
@ KEY_RIGHT
Definition input.h:47
@ KEY_PAGE_UP
Definition input.h:48
@ KEY_TAB
Definition input.h:46
bool remove_callback()
Definition input.cpp:92
bool update()
Definition input.cpp:103
bool remove_source(Source *p)
Definition input.cpp:43
BindingGroup * m_binding_group
Definition input.h:76
std::bitset< KEY_MAX+1 > overrides
Definition input.h:77
std::vector< std::unique_ptr< Source > > sources
Definition input.h:78
void has_override(std::size_t n, i32 *keys) const
Definition input.cpp:56
Selector
Indicates that a callback should only handle a sub-set of events.
Definition input.h:36
@ CTRL
Definition input.h:37
@ ALT
Definition input.h:37
@ PRESS
Definition input.h:37
Action
Definition input.h:39
@ KEY_PRESS
Definition input.h:40
@ KEY_RELEASE
Definition input.h:40
void init(lua_State *L_)
Definition input.h:61
int callback_ref
Definition input.h:75
lua_State * L
Definition input.h:74
BindingGroup * binding_group() const
Definition input.h:64
local n
Definition dump_lights.lua:5
local g
Definition gamma.lua:7
key_callback
Definition harvest_moon.lua:31
Definition audio.cpp:7
std::unique_ptr< Input::Source > input_terminal_source(int fd, Input::TerminalFlag flags)
Definition terminal.cpp:122
std::int32_t i32
Definition def.h:10
std::unique_ptr< Input::Source > input_graphics_source(Graphics *g)
Definition graphics.cpp:79
#define NNGN_DEFAULT_CONSTRUCT(x)
Definition utils.h:20
Definition input.h:51
virtual ~Source()=0
virtual bool update(Input *)
Definition input.h:54
virtual void get_keys(std::span< i32 > keys) const
Definition input.cpp:18
std::chrono::duration< float, typename T::period > fd
Definition timing.cpp:8