CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
InputContext.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Action.hpp"
11#include "Range.hpp"
12
14
18
19namespace CeresEngine {
20
21 class InputManager;
22 class InputState;
23
25 private:
27 InputManager& manager [[maybe_unused]];
28
31
34
36 bool mActive = true;
37
38 public:
42 explicit InputContext(InputManager& manager) noexcept;
43
44 InputContext(const InputContext&) = delete;
45 InputContext& operator=(const InputContext&) = delete;
46
49
50 public:
57 template<typename T, typename... Args> T& add(const StringView& name, Args&&... args);
58
59 public: // Activation
63 CE_SCRIPT_EXPORT(property = "setter", name = "active")
64 void active(bool state) noexcept;
65
67 [[nodiscard]] CE_SCRIPT_EXPORT(property = "getter", name = "active")
68 bool isActive() const noexcept;
69
70 public: // Action
74 [[nodiscard]] CE_SCRIPT_EXPORT()
75 InputAction& action(const StringView& name);
76
80 [[nodiscard]] const InputAction& action(const StringView& name) const;
81
85 [[nodiscard]] CE_SCRIPT_EXPORT()
86 bool hasAction(const StringView& name) const;
87
88 private:
95 template<CInputAction Action, typename... Args> Action& createAction(const StringView& name, Args&&... args) {
96 return static_cast<Action&>(addAction(name, ce_unique_new<Action>(std::forward<Args>(args)...)));
97 }
98
104
105 public: // Range
109 [[nodiscard]] CE_SCRIPT_EXPORT()
110 InputRange& range(const StringView& name);
111
115 [[nodiscard]] const InputRange& range(const StringView& name) const;
116
120 [[nodiscard]] CE_SCRIPT_EXPORT()
121 bool hasRange(const StringView& name) const;
122
123 private:
130 template<CInputRange Range, typename... Args> Range& createRange(const StringView& name, Args&&... args) {
131 return static_cast<Range&>(addRange(name, ce_unique_new<Range>(std::forward<Args>(args)...)));
132 }
133
139
140 private: // InputManager interface
141 friend class InputManager;
142
145 void update(const InputState& state);
146 };
147
148} // namespace CeresEngine
149
150#include "Action.hpp"
151#include "Range.hpp"
152
153namespace CeresEngine {
154
155 template<typename T, typename... Args> T& InputContext::add(const StringView& name, Args&&... args) {
156 static_assert(std::is_base_of_v<InputAction, T> || std::is_base_of_v<InputRange, T>, "T must be either an Action or a Range!");
157 if constexpr(std::is_base_of_v<InputAction, T>) {
158 return createAction<T>(name, std::forward<Args>(args)...);
159 } else if constexpr(std::is_base_of_v<InputRange, T>) {
160 return createRange<T>(name, std::forward<Args>(args)...);
161 }
162 }
163
164} // namespace CeresEngine
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
Definition Action.hpp:23
Definition InputContext.hpp:24
T & add(const StringView &name, Args &&... args)
Add a new Action or Range to the context.
Definition InputContext.hpp:155
InputRange & addRange(const StringView &name, UPtr< InputRange > range)
Add a new range with the given name.
void update(const InputState &state)
Update all actions and ranges with the given input state.
Map< String, UPtr< InputRange > > ranges
A map of all ranges registered with the context.
Definition InputContext.hpp:33
Map< String, UPtr< InputAction > > actions
A map of all actions registered with the context.
Definition InputContext.hpp:30
InputAction & addAction(const StringView &name, UPtr< InputAction > action)
Add a new action with the given name.
Definition InputManager.hpp:29
Definition Range.hpp:18
Definition InputState.hpp:20
Definition Action.hpp:113
Definition Range.hpp:84
Definition Application.hpp:19
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
std::map< Key, T, Compare, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > Map
Map is a sorted associative container that contains key-value pairs with unique keys.
Definition Map.hpp:24