Skip to content

Commit

Permalink
Cleaned up input listener ID naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
afritz1 committed Jan 10, 2025
1 parent c8386f9 commit 745ef19
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion OpenTESArena/src/Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Game
bool shouldRenderScene;
private:
// Listener IDs are optional in case of failed Game construction.
std::optional<InputManager::ListenerID> applicationExitListenerID, windowResizedListenerID,
std::optional<InputListenerID> applicationExitListenerID, windowResizedListenerID,
renderTargetsResetListenerID, takeScreenshotListenerID, debugProfilerListenerID;

bool requestedSubPanelPop;
Expand Down
35 changes: 17 additions & 18 deletions OpenTESArena/src/Input/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ bool InputManager::isTextInput(const SDL_Event &e) const
return e.type == SDL_TEXTINPUT;
}

InputManager::ListenerID InputManager::getNextListenerID()
InputListenerID InputManager::getNextListenerID()
{
ListenerID listenerID;
InputListenerID listenerID;
if (!this->freedListenerIDs.empty())
{
listenerID = this->freedListenerIDs.back();
Expand Down Expand Up @@ -336,8 +336,8 @@ bool InputManager::setInputActionMapActive(const std::string &name, bool active)
}
}

template <typename EntryType, typename CallbackType>
InputManager::ListenerID InputManager::addListenerInternal(CallbackType &&callback, ListenerType listenerType,
template<typename EntryType, typename CallbackType>
InputListenerID InputManager::addListenerInternal(CallbackType &&callback, ListenerType listenerType,
std::vector<EntryType> &listeners, std::vector<int> &freedListenerIndices)
{
int insertIndex;
Expand All @@ -356,7 +356,7 @@ InputManager::ListenerID InputManager::addListenerInternal(CallbackType &&callba
EntryType &listenerEntry = listeners[insertIndex];
listenerEntry.init(callback);

const ListenerID listenerID = this->getNextListenerID();
const InputListenerID listenerID = this->getNextListenerID();

ListenerLookupEntry lookupEntry;
lookupEntry.init(listenerType, insertIndex);
Expand All @@ -365,8 +365,7 @@ InputManager::ListenerID InputManager::addListenerInternal(CallbackType &&callba
return listenerID;
}

InputManager::ListenerID InputManager::addInputActionListener(const std::string_view actionName,
const InputActionCallback &callback)
InputListenerID InputManager::addInputActionListener(const std::string_view actionName, const InputActionCallback &callback)
{
int insertIndex;
if (!this->freedInputActionListenerIndices.empty())
Expand All @@ -384,7 +383,7 @@ InputManager::ListenerID InputManager::addInputActionListener(const std::string_
InputActionListenerEntry &listenerEntry = this->inputActionListeners[insertIndex];
listenerEntry.init(actionName, callback);

const ListenerID listenerID = this->getNextListenerID();
const InputListenerID listenerID = this->getNextListenerID();

ListenerLookupEntry lookupEntry;
lookupEntry.init(ListenerType::InputAction, insertIndex);
Expand All @@ -393,49 +392,49 @@ InputManager::ListenerID InputManager::addInputActionListener(const std::string_
return listenerID;
}

InputManager::ListenerID InputManager::addMouseButtonChangedListener(const MouseButtonChangedCallback &callback)
InputListenerID InputManager::addMouseButtonChangedListener(const MouseButtonChangedCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::MouseButtonChanged,
this->mouseButtonChangedListeners, this->freedMouseButtonChangedListenerIndices);
}

InputManager::ListenerID InputManager::addMouseButtonHeldListener(const MouseButtonHeldCallback &callback)
InputListenerID InputManager::addMouseButtonHeldListener(const MouseButtonHeldCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::MouseButtonHeld,
this->mouseButtonHeldListeners, this->freedMouseButtonHeldListenerIndices);
}

InputManager::ListenerID InputManager::addMouseScrollChangedListener(const MouseScrollChangedCallback &callback)
InputListenerID InputManager::addMouseScrollChangedListener(const MouseScrollChangedCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::MouseScrollChanged,
this->mouseScrollChangedListeners, this->freedMouseScrollChangedListenerIndices);
}

InputManager::ListenerID InputManager::addMouseMotionListener(const MouseMotionCallback &callback)
InputListenerID InputManager::addMouseMotionListener(const MouseMotionCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::MouseMotion,
this->mouseMotionListeners, this->freedMouseMotionListenerIndices);
}

InputManager::ListenerID InputManager::addApplicationExitListener(const ApplicationExitCallback &callback)
InputListenerID InputManager::addApplicationExitListener(const ApplicationExitCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::ApplicationExit,
this->applicationExitListeners, this->freedApplicationExitListenerIndices);
}

InputManager::ListenerID InputManager::addWindowResizedListener(const WindowResizedCallback &callback)
InputListenerID InputManager::addWindowResizedListener(const WindowResizedCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::WindowResized,
this->windowResizedListeners, this->freedWindowResizedListenerIndices);
}

InputManager::ListenerID InputManager::addRenderTargetsResetListener(const RenderTargetsResetCallback &callback)
InputListenerID InputManager::addRenderTargetsResetListener(const RenderTargetsResetCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::RenderTargetsReset,
this->renderTargetsResetListeners, this->freedRenderTargetsResetListenerIndices);
}

InputManager::ListenerID InputManager::addTextInputListener(const TextInputCallback &callback)
InputListenerID InputManager::addTextInputListener(const TextInputCallback &callback)
{
return this->addListenerInternal(callback, ListenerType::TextInput,
this->textInputListeners, this->freedTextInputListenerIndices);
Expand All @@ -453,7 +452,7 @@ void InputManager::setTextInputMode(bool active)
}
}

void InputManager::removeListener(ListenerID id)
void InputManager::removeListener(InputListenerID id)
{
auto resetListenerEntry = [](auto &listeners, auto &freedIndices, int removeIndex)
{
Expand Down Expand Up @@ -524,7 +523,7 @@ void InputManager::removeListener(ListenerID id)
}
}

void InputManager::setListenerEnabled(ListenerID id, bool enabled)
void InputManager::setListenerEnabled(InputListenerID id, bool enabled)
{
const auto iter = this->listenerLookupEntries.find(id);
if (iter == this->listenerLookupEntries.end())
Expand Down
36 changes: 18 additions & 18 deletions OpenTESArena/src/Input/InputManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

struct ButtonProxy;

using InputListenerID = int;

// Handles active input action maps, input listeners, and pointer input events.
class InputManager
{
public:
using ListenerID = int;
private:
enum class ListenerType
{
Expand Down Expand Up @@ -145,7 +145,7 @@ class InputManager
std::vector<TextInputListenerEntry> textInputListeners;

// Look-up values for valid listener entries, shared by all listener containers.
std::unordered_map<ListenerID, ListenerLookupEntry> listenerLookupEntries;
std::unordered_map<InputListenerID, ListenerLookupEntry> listenerLookupEntries;

// Indices to listener entries that were used but can be reclaimed by a future registration.
std::vector<int> freedInputActionListenerIndices;
Expand All @@ -158,17 +158,17 @@ class InputManager
std::vector<int> freedRenderTargetsResetListenerIndices;
std::vector<int> freedTextInputListenerIndices;

ListenerID nextListenerID;
std::vector<ListenerID> freedListenerIDs;
InputListenerID nextListenerID;
std::vector<InputListenerID> freedListenerIDs;

Int2 mouseDelta;

ListenerID getNextListenerID();
InputListenerID getNextListenerID();

bool isInTextEntryMode() const;

template <typename EntryType, typename CallbackType>
ListenerID addListenerInternal(CallbackType &&callback, ListenerType listenerType, std::vector<EntryType> &listeners,
InputListenerID addListenerInternal(CallbackType &&callback, ListenerType listenerType, std::vector<EntryType> &listeners,
std::vector<int> &freedListenerIndices);

void handleHeldInputs(Game &game, BufferView<const InputActionMap*> activeMaps,
Expand Down Expand Up @@ -203,20 +203,20 @@ class InputManager

bool setInputActionMapActive(const std::string &name, bool active);

ListenerID addInputActionListener(const std::string_view actionName, const InputActionCallback &callback);
ListenerID addMouseButtonChangedListener(const MouseButtonChangedCallback &callback);
ListenerID addMouseButtonHeldListener(const MouseButtonHeldCallback &callback);
ListenerID addMouseScrollChangedListener(const MouseScrollChangedCallback &callback);
ListenerID addMouseMotionListener(const MouseMotionCallback &callback);
ListenerID addApplicationExitListener(const ApplicationExitCallback &callback);
ListenerID addWindowResizedListener(const WindowResizedCallback &callback);
ListenerID addRenderTargetsResetListener(const RenderTargetsResetCallback &callback);
ListenerID addTextInputListener(const TextInputCallback &callback);
InputListenerID addInputActionListener(const std::string_view actionName, const InputActionCallback &callback);
InputListenerID addMouseButtonChangedListener(const MouseButtonChangedCallback &callback);
InputListenerID addMouseButtonHeldListener(const MouseButtonHeldCallback &callback);
InputListenerID addMouseScrollChangedListener(const MouseScrollChangedCallback &callback);
InputListenerID addMouseMotionListener(const MouseMotionCallback &callback);
InputListenerID addApplicationExitListener(const ApplicationExitCallback &callback);
InputListenerID addWindowResizedListener(const WindowResizedCallback &callback);
InputListenerID addRenderTargetsResetListener(const RenderTargetsResetCallback &callback);
InputListenerID addTextInputListener(const TextInputCallback &callback);

void removeListener(ListenerID id);
void removeListener(InputListenerID id);

// Sets whether a valid listener can hear input callbacks.
void setListenerEnabled(ListenerID id, bool enabled);
void setListenerEnabled(InputListenerID id, bool enabled);

// Sets whether the mouse should move during motion events (for player camera).
void setRelativeMouseMode(bool active);
Expand Down
16 changes: 8 additions & 8 deletions OpenTESArena/src/Interface/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ Panel::~Panel()
InputManager &inputManager = this->game.inputManager;

// Free all the input listener IDs.
for (const InputManager::ListenerID listenerID : this->inputActionListenerIDs)
for (const InputListenerID listenerID : this->inputActionListenerIDs)
{
inputManager.removeListener(listenerID);
}

for (const InputManager::ListenerID listenerID : this->mouseButtonChangedListenerIDs)
for (const InputListenerID listenerID : this->mouseButtonChangedListenerIDs)
{
inputManager.removeListener(listenerID);
}

for (const InputManager::ListenerID listenerID : this->mouseButtonHeldListenerIDs)
for (const InputListenerID listenerID : this->mouseButtonHeldListenerIDs)
{
inputManager.removeListener(listenerID);
}

for (const InputManager::ListenerID listenerID : this->mouseScrollChangedListenerIDs)
for (const InputListenerID listenerID : this->mouseScrollChangedListenerIDs)
{
inputManager.removeListener(listenerID);
}

for (const InputManager::ListenerID listenerID : this->mouseMotionListenerIDs)
for (const InputListenerID listenerID : this->mouseMotionListenerIDs)
{
inputManager.removeListener(listenerID);
}

for (const InputManager::ListenerID listenerID : this->textInputListenerIDs)
for (const InputListenerID listenerID : this->textInputListenerIDs)
{
inputManager.removeListener(listenerID);
}
Expand All @@ -78,9 +78,9 @@ void Panel::onPauseChanged(bool paused)
this->paused = paused;

InputManager &inputManager = this->game.inputManager;
auto setListenersEnabled = [paused, &inputManager](std::vector<InputManager::ListenerID> &listenerIDs)
auto setListenersEnabled = [paused, &inputManager](std::vector<InputListenerID> &listenerIDs)
{
for (const InputManager::ListenerID listenerID : listenerIDs)
for (const InputListenerID listenerID : listenerIDs)
{
inputManager.setListenerEnabled(listenerID, !paused);
}
Expand Down
12 changes: 6 additions & 6 deletions OpenTESArena/src/Interface/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class Panel
Game &game;

// Allocated input listener IDs that must be freed when the panel is done with them.
std::vector<InputManager::ListenerID> inputActionListenerIDs;
std::vector<InputManager::ListenerID> mouseButtonChangedListenerIDs;
std::vector<InputManager::ListenerID> mouseButtonHeldListenerIDs;
std::vector<InputManager::ListenerID> mouseScrollChangedListenerIDs;
std::vector<InputManager::ListenerID> mouseMotionListenerIDs;
std::vector<InputManager::ListenerID> textInputListenerIDs;
std::vector<InputListenerID> inputActionListenerIDs;
std::vector<InputListenerID> mouseButtonChangedListenerIDs;
std::vector<InputListenerID> mouseButtonHeldListenerIDs;
std::vector<InputListenerID> mouseScrollChangedListenerIDs;
std::vector<InputListenerID> mouseMotionListenerIDs;
std::vector<InputListenerID> textInputListenerIDs;

std::vector<ButtonProxy> buttonProxies;

Expand Down

0 comments on commit 745ef19

Please sign in to comment.