-
Notifications
You must be signed in to change notification settings - Fork 1
Implement input action abstraction in pad_handler #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Souzooka
wants to merge
4
commits into
main
Choose a base branch
from
minor/pad-control
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
566937b
pad_control: Mostly complete implementation of CPadControl
Souzooka 5f8a9bb
pad_handler: Mostly migrate support for input actions to host/pad_han…
Souzooka 9ecd150
pad_handler: Use enums to represent input actions rather than strings
Souzooka ad8045e
pad_handler: Implement support for registering and updating analog in…
Souzooka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #include "common/debug.h" | ||
| #include "common/log.h" | ||
|
|
||
| #include "dc2/pad_control.h" | ||
|
|
||
| #include "host/host_interface_dwm.h" | ||
|
|
||
| set_log_channel("pad_control"); | ||
|
|
||
| // 002ED3E0 | ||
| // Clears all recorded actions | ||
| void CPadControl::Reset() | ||
| { | ||
| log_trace("CPadControl::{}()", __func__); | ||
|
|
||
| m_buttons.clear(); | ||
| m_analog.clear(); | ||
| } | ||
|
|
||
| // 002ED460 | ||
| // Registers up to two buttons for a particular action | ||
| // TODO: Accept a vector for an arbitrary number of buttons | ||
| void CPadControl::RegisterBtn(const std::string_view& action_name, host::pad_handler::buttons btn1, host::pad_handler::buttons btn2) | ||
| { | ||
| log_trace("CPadControl::{}({}, {:#X}, {:#X})", __func__, action_name, std::to_underlying(btn1), std::to_underlying(btn2)); | ||
|
|
||
| m_buttons[action_name.data()] = { | ||
| .m_value = false, | ||
| .m_input_key = btn1 | btn2 | ||
| }; | ||
| } | ||
|
|
||
| // 002ED4A0 | ||
| // Registers an axis for a particular aciton | ||
| void CPadControl::RegisterAnalog(const std::string_view& action_name, EPadAxis axis) | ||
| { | ||
| log_trace("CPadControl::{}({}, {})", __func__, action_name, std::to_underlying(axis)); | ||
|
|
||
| m_analog[action_name.data()] = { | ||
| .m_value = 0.0f, | ||
| .m_input_axis = axis | ||
| }; | ||
| } | ||
|
|
||
| // 002ED4E0 | ||
| // Checks the button value for a particular action | ||
| bool CPadControl::Btn(const std::string_view& action_name) | ||
| { | ||
| log_trace("CPadControl::{}({})", __func__, action_name); | ||
|
|
||
| if (!m_buttons.contains(action_name.data())) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return m_buttons[action_name.data()].m_value; | ||
| } | ||
|
|
||
| // 002ED520 | ||
| // Checks the analog value for a particular action | ||
| f32 CPadControl::Analog(const std::string_view& action_name) | ||
| { | ||
| log_trace("CPadControl::{}({})", __func__, action_name); | ||
|
|
||
| if (!m_analog.contains(action_name.data())) | ||
| { | ||
| return 0.0f; | ||
| } | ||
|
|
||
| return m_analog[action_name.data()].m_value; | ||
raSTARgfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // 002ED550 | ||
| // Updates each action's button pressed/analog value | ||
| void CPadControl::Update() | ||
| { | ||
| log_trace("CPadControl::{}()", __func__); | ||
|
|
||
| for (auto& button : m_buttons) | ||
| { | ||
| // Update recorded values for buttons | ||
| button.second.m_value = g_host_interface->pad_button_any_pressed(button.second.m_input_key); | ||
| } | ||
raSTARgfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // TODO: Check analog values | ||
| todo; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #pragma once | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "common/types.h" | ||
|
|
||
| #include "host/pad_handler.h" | ||
|
|
||
| /* | ||
| * CPadControl creates an abstraction by allowing the programmer to register | ||
| * raw inputs for a particular action the player may perform, then get the value | ||
| * of the inputs by checking the action. | ||
| * | ||
| * For example, a simple usage may look something like this: | ||
| * | ||
| * PadCtrl.RegisterBtn("photo-take", SQUARE); | ||
| * ... | ||
| * PadCtrl.Update(); | ||
| * ... | ||
| * if (PadCtrl.Btn("photo_take")) | ||
| * { | ||
| * // The user is pressing the button associated with taking a photo | ||
| * } | ||
| * else | ||
| * { | ||
| * // The user is not pressing the button associated with taking a photo | ||
| * } | ||
| * | ||
| * Note that the second argument for RegisterBtn need not necessarily be SQUARE, | ||
| * but could come from some other source like a settings file. Therefore, | ||
| * the action "photo-take" could be associated with any number of arbitrary | ||
| * inputs, but the programmer does not have to worry about the raw inputs. | ||
| * | ||
| * NOTE: This MVP is designed only for gamepads, at the moment. | ||
| */ | ||
|
|
||
| enum class EPadAxis | ||
| { | ||
| _1 = 1, | ||
| _2 = 2, | ||
| _3 = 3, | ||
| _4 = 4, | ||
| }; | ||
|
|
||
| struct PAD_ANALOG | ||
| { | ||
| f32 m_value{ 0.0f }; | ||
| EPadAxis m_input_axis{}; | ||
| }; | ||
|
|
||
| struct PAD_BUTTON | ||
| { | ||
| bool m_value{ false }; | ||
| host::pad_handler::buttons m_input_key{}; | ||
| }; | ||
|
|
||
| class CPadControl | ||
| { | ||
| public: | ||
|
|
||
| // 002ED3E0 | ||
| void Reset(); | ||
|
|
||
| // 002ED460 | ||
| void RegisterBtn(const std::string_view& action_name, host::pad_handler::buttons btn1, host::pad_handler::buttons btn2 = host::pad_handler::buttons::none); | ||
|
|
||
| // 002ED4A0 | ||
| void RegisterAnalog(const std::string_view& action_name, EPadAxis axis); | ||
|
|
||
| // 002ED4E0 | ||
| bool Btn(const std::string_view& action_name); | ||
|
|
||
| // 002ED520 | ||
| f32 Analog(const std::string_view& action_name); | ||
|
|
||
| // 002ED550 | ||
| void Update(); | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, PAD_BUTTON> m_buttons{}; | ||
| std::unordered_map<std::string, PAD_ANALOG> m_analog{}; | ||
| }; | ||
|
|
||
| extern std::unique_ptr<CPadControl> PadCtrl = std::make_unique<CPadControl>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.