libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
ControllerStick.h
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5#include <memory>
6#include <unordered_map>
8
9namespace Ship {
10class ConsoleVariable;
11class ControlDeck;
12class Window;
13
15#define DEFAULT_STICK_SENSITIVITY_PERCENTAGE 100
17#define DEFAULT_STICK_DEADZONE_PERCENTAGE 20
19#define DEFAULT_NOTCH_SNAP_ANGLE 0
20
33 public:
35 void SetControlDeck(std::shared_ptr<ControlDeck> controlDeck) {
36 mControlDeck = std::move(controlDeck);
37 }
49 ControllerStick(uint8_t portIndex, StickIndex stickIndex,
50 std::shared_ptr<ConsoleVariable> consoleVariable = nullptr,
51 std::shared_ptr<ControlDeck> controlDeck = nullptr, std::shared_ptr<Window> window = nullptr);
53
56
61 void AddDefaultMappings(PhysicalDeviceType physicalDeviceType);
62
65
71
80 void UpdatePad(int8_t& x, int8_t& y);
81
87 std::shared_ptr<ControllerAxisDirectionMapping> GetAxisDirectionMappingById(Direction direction, std::string id);
88
92 std::unordered_map<Direction, std::unordered_map<std::string, std::shared_ptr<ControllerAxisDirectionMapping>>>
94
99 std::unordered_map<std::string, std::shared_ptr<ControllerAxisDirectionMapping>>
101
107 void AddAxisDirectionMapping(Direction direction, std::shared_ptr<ControllerAxisDirectionMapping> mapping);
108
114 void ClearAxisDirectionMappingId(Direction direction, std::string id);
115
121 void ClearAxisDirectionMapping(Direction direction, std::string id);
122
128 void ClearAxisDirectionMapping(Direction direction, std::shared_ptr<ControllerAxisDirectionMapping> mapping);
129
132
140
146 void Process(int8_t& x, int8_t& y);
147
150
155 void SetSensitivity(uint8_t sensitivityPercentage);
156
159
162
165
170 void SetDeadzone(uint8_t deadzonePercentage);
171
174
177
180
185 void SetNotchSnapAngle(uint8_t notchSnapAngle);
186
189
192
199 bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
200
207 bool ProcessMouseButtonEvent(bool isPressed, Ship::MouseBtn button);
208
214
217
218 private:
219 double GetClosestNotch(double angle, double approximationThreshold);
220 void LoadAxisDirectionMappingFromConfig(std::string id);
221 float GetAxisDirectionValue(Direction direction);
222
223 uint8_t mPortIndex;
224 StickIndex mStickIndex;
225
226 uint8_t mSensitivityPercentage;
227 float mSensitivity;
228
229 // TODO: handle deadzones separately for X and Y?
230 uint8_t mDeadzonePercentage;
231 float mDeadzone;
232 uint8_t mNotchSnapAngle;
233
234 std::unordered_map<Direction, std::unordered_map<std::string, std::shared_ptr<ControllerAxisDirectionMapping>>>
235 mAxisDirectionMappings;
236
237 bool mUseEventInputToCreateNewMapping;
238 KbScancode mKeyboardScancodeForNewMapping;
239 MouseBtn mMouseButtonForNewMapping;
240
241 std::shared_ptr<ConsoleVariable> mConsoleVariable;
242 std::shared_ptr<ControlDeck> mControlDeck;
243 std::shared_ptr<Window> mWindow;
244};
245} // namespace Ship
Aggregates axis-direction mappings and applies sensitivity, dead-zone, and notch-snap to a stick.
Definition ControllerStick.h:32
std::unordered_map< std::string, std::shared_ptr< ControllerAxisDirectionMapping > > GetAllAxisDirectionMappingByDirection(Direction direction)
Returns the id → mapping map for a single direction.
bool ProcessMouseButtonEvent(bool isPressed, Ship::MouseBtn button)
Forwards a mouse-button event to all directional mappings that handle mouse input.
void ClearAxisDirectionMapping(Direction direction, std::shared_ptr< ControllerAxisDirectionMapping > mapping)
Removes the given mapping instance from both in-memory and Config.
uint8_t GetDeadzonePercentage()
Returns the current dead-zone percentage.
void UpdatePad(int8_t &x, int8_t &y)
Evaluates all axis mappings and writes the processed stick values to x and y.
bool DeadzoneIsDefault()
Returns true if dead-zone is at the default value.
void SetNotchSnapAngle(uint8_t notchSnapAngle)
Sets the notch snap angle and saves to Config.
bool NotchSnapAngleIsDefault()
Returns true if the notch snap angle is at the default value.
void ResetDeadzoneToDefault()
Resets dead-zone to DEFAULT_STICK_DEADZONE_PERCENTAGE and saves to Config.
void SetControlDeck(std::shared_ptr< ControlDeck > controlDeck)
Injects the owning ControlDeck.
Definition ControllerStick.h:35
bool HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType)
Returns true if any directional mapping targets the given device type.
std::unordered_map< Direction, std::unordered_map< std::string, std::shared_ptr< ControllerAxisDirectionMapping > > > GetAllAxisDirectionMappings()
Returns a copy of the full direction → (id → mapping) map.
void ClearAxisDirectionMapping(Direction direction, std::string id)
Removes the mapping by ID from both in-memory and Config.
void SetDeadzone(uint8_t deadzonePercentage)
Sets the dead-zone and saves to Config.
uint8_t GetSensitivityPercentage()
Returns the current sensitivity percentage.
void AddDefaultMappings(PhysicalDeviceType physicalDeviceType)
Applies the default axis-direction mappings for the given device type.
void ResetSensitivityToDefault()
Resets sensitivity to DEFAULT_STICK_SENSITIVITY_PERCENTAGE and saves to Config.
uint8_t GetNotchSnapAngle()
Returns the current notch snap angle in degrees.
void Process(int8_t &x, int8_t &y)
Reads raw axis values, applies processing, and writes to x and y.
void ClearAllMappingsForDeviceType(PhysicalDeviceType physicalDeviceType)
Removes all axis-direction mappings that target the given device type.
void ReloadAllMappingsFromConfig()
Clears all in-memory mappings and reloads them from Config.
void ResetNotchSnapAngleToDefault()
Resets notch snap angle to DEFAULT_NOTCH_SNAP_ANGLE and saves to Config.
void SetSensitivity(uint8_t sensitivityPercentage)
Sets the stick sensitivity and saves to Config.
std::shared_ptr< ControllerAxisDirectionMapping > GetAxisDirectionMappingById(Direction direction, std::string id)
Returns the mapping for the given direction and ID, or nullptr if not found.
bool SensitivityIsDefault()
Returns true if sensitivity is at the default value.
void ClearAxisDirectionMappingId(Direction direction, std::string id)
Removes the mapping ID from Config without destroying the in-memory object.
void AddAxisDirectionMapping(Direction direction, std::shared_ptr< ControllerAxisDirectionMapping > mapping)
Adds a mapping for the given direction.
StickIndex GetStickIndex()
Returns the stick index (left or right).
void SaveAxisDirectionMappingIdsToConfig()
Writes the current set of mapping IDs for all directions to Config.
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode)
Forwards a keyboard event to all directional mappings that handle keyboard input.
void ClearAllMappings()
Removes all axis-direction mappings for all directions.
bool AddOrEditAxisDirectionMappingFromRawPress(Direction direction, std::string id)
Listens for the next raw physical input and adds or replaces a directional mapping.
ControllerStick(uint8_t portIndex, StickIndex stickIndex, std::shared_ptr< ConsoleVariable > consoleVariable=nullptr, std::shared_ptr< ControlDeck > controlDeck=nullptr, std::shared_ptr< Window > window=nullptr)
Constructs a ControllerStick for a given port and stick index.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
PhysicalDeviceType
Identifies the category of a physical input device.
Definition PhysicalDeviceType.h:11
KbScancode
Platform-independent keyboard scan codes used by the input mapping system.
Definition KeyboardScancodes.h:26
KbEventType
Identifies the type of a keyboard event delivered to the input mapping system.
Definition KeyboardScancodes.h:12
Direction
Identifies the direction component of an axis mapping.
Definition ControllerAxisDirectionMapping.h:18
MouseBtn
Identifies a mouse button delivered to the input mapping system.
Definition KeyboardScancodes.h:136
StickIndex
Identifies which analog stick an axis mapping applies to.
Definition ControllerAxisDirectionMapping.h:15