Repository navigation
Expand file tree
/
Copy pathDekiInput.h
More file actions
81 lines (62 loc) · 2.77 KB
/
Copy pathDekiInput.h
File metadata and controls
81 lines (62 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <map>
#include "IDekiInput.h"
namespace DekiInput
{
/// The registry of input sources (mouse, keyboard, touch, gamepad and so on).
/// Several can be active at once; events from every active source go into
/// one global event stream.
///
/// Drivers live in platform integration packages and register with
/// SetInput(); this package owns the interface and the dispatch logic.
class DekiInput
{
private:
static std::map<std::string, std::unique_ptr<IDekiInput>> s_ActiveInputs;
static std::vector<InputEventCallback> s_GlobalCallbacks;
static bool s_Initialized;
static bool s_ShouldExit;
public:
/// Adds an initialized input source under `name` and takes ownership of
/// it. A name already registered is left as it is. Returns false only for
/// a null input.
static bool SetInput(std::unique_ptr<IDekiInput> input, const std::string& name);
/// Shuts down and removes every input source and callback.
static void Shutdown();
/// The input source registered under `name`, or nullptr.
static IDekiInput* GetInput(const std::string& name);
/// Adds a callback that receives events from ALL input sources. Returns
/// an id for UnregisterEventCallback.
static size_t RegisterEventCallback(const InputEventCallback& callback);
/// Removes the callback RegisterEventCallback returned `id` for. A
/// component that listens must stop before it is destroyed (OnDestroy),
/// or the callback is called on a freed object. Safe from inside a
/// callback, including the one being removed.
static void UnregisterEventCallback(size_t id);
/// Removes every global event callback. Call it before freeing a DLL that
/// registered callbacks, or the std::function objects point into freed
/// code.
static void ClearEventCallbacks();
/// Updates every active input source, once a frame, which delivers their
/// events.
static void Update();
/// The pointer position from the first input source that has one. Returns
/// false when none does.
static bool GetPointerPosition(int32_t* x, int32_t* y);
/// True when `key` (see Keys.h) is down on any active input source.
static bool IsKeyPressed(uint32_t key);
/// True once any input source has sent a quit event.
static bool ShouldExit();
/// Sets the exit flag. Input packages call it on a quit event.
static void SetShouldExit(bool exit);
static bool IsInitialized() { return s_Initialized; }
/// The names of all active input sources.
static std::vector<std::string> GetActiveInputs();
private:
// Passes an event from any source to the global callbacks.
static void DistributeEvent(const InputEvent& event);
};
} // namespace DekiInput