Skip to content

Commit

Permalink
mouse button to axis mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmanLP committed Dec 18, 2024
1 parent dd6569b commit 32a87a4
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ bool Controller::ProcessMouseEvent(bool isPressed, MouseBtn mouseButton) {
for (auto [bitmask, button] : GetAllButtons()) {
result = button->ProcessMouseEvent(isPressed, mouseButton) || result;
}
// FIXME: implement
// result = GetLeftStick()->ProcessMouseEvent(isPressed, mouseButton) || result;
// result = GetRightStick()->ProcessMouseEvent(isPressed, mouseButton) || result;
result = GetLeftStick()->ProcessMouseEvent(isPressed, mouseButton) || result;
result = GetRightStick()->ProcessMouseEvent(isPressed, mouseButton) || result;
return result;
}

Expand Down
53 changes: 43 additions & 10 deletions src/controller/controldevice/controller/ControllerStick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <spdlog/spdlog.h>

#include "controller/controldevice/controller/mapping/keyboard/KeyboardKeyToAxisDirectionMapping.h"
#include "controller/controldevice/controller/mapping/mouse/MouseKeyToAxisDirectionMapping.h"

#include "controller/controldevice/controller/mapping/factories/AxisDirectionMappingFactory.h"

Expand All @@ -11,6 +12,8 @@
#include <sstream>
#include <algorithm>

#include <imgui.h>

// for some reason windows isn't seeing M_PI
// this is copied from my system's math.h
#ifndef M_PI
Expand All @@ -22,8 +25,8 @@

namespace Ship {
ControllerStick::ControllerStick(uint8_t portIndex, StickIndex stickIndex)
: mPortIndex(portIndex), mStickIndex(stickIndex), mUseKeydownEventToCreateNewMapping(false),
mKeyboardScancodeForNewMapping(KbScancode::LUS_KB_UNKNOWN) {
: mPortIndex(portIndex), mStickIndex(stickIndex),, mUseKeydownEventToCreateNewMapping(false),
mKeyboardScancodeForNewMapping(KbScancode::LUS_KB_UNKNOWN), mMouseButtonForNewMapping(MouseBtn::MOUSE_BTN_UNKNOWN) {
mSensitivityPercentage = DEFAULT_STICK_SENSITIVITY_PERCENTAGE;
mSensitivity = 1.0f;
mDeadzonePercentage = DEFAULT_STICK_DEADZONE_PERCENTAGE;
Expand Down Expand Up @@ -272,18 +275,19 @@ bool ControllerStick::AddOrEditAxisDirectionMappingFromRawPress(Direction direct
if (mKeyboardScancodeForNewMapping != LUS_KB_UNKNOWN) {
mapping = std::make_shared<KeyboardKeyToAxisDirectionMapping>(mPortIndex, mStickIndex, direction,
mKeyboardScancodeForNewMapping);
}

if (mapping == nullptr) {
mapping =
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(mPortIndex, mStickIndex, direction);
} else if (!ImGui::IsAnyItemHovered() && mMouseButtonForNewMapping != MouseBtn::MOUSE_BTN_UNKNOWN) {
// TODO: I dont think direct ImGui calls should be here (again)
mapping = std::make_shared<MouseKeyToAxisDirectionMapping>(mPortIndex, mStickIndex, direction, mMouseButtonForNewMapping);
} else {
mapping = AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(mPortIndex, mStickIndex, direction);
}

if (mapping == nullptr) {
return false;
}

mKeyboardScancodeForNewMapping = LUS_KB_UNKNOWN;
mMouseButtonForNewMapping = MouseBtn::MOUSE_BTN_UNKNOWN;
mUseKeydownEventToCreateNewMapping = false;

if (id != "") {
Expand Down Expand Up @@ -319,9 +323,13 @@ void ControllerStick::UpdatePad(int8_t& x, int8_t& y) {
}

bool ControllerStick::ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode) {
if (mUseKeydownEventToCreateNewMapping && eventType == LUS_KB_EVENT_KEY_DOWN) {
mKeyboardScancodeForNewMapping = scancode;
return true;
if (mUseKeydownEventToCreateNewMapping) {
if (eventType == LUS_KB_EVENT_KEY_DOWN) {
mKeyboardScancodeForNewMapping = scancode;
return true;
} else {
mKeyboardScancodeForNewMapping = LUS_KB_UNKNOWN;
}
}

bool result = false;
Expand All @@ -339,6 +347,31 @@ bool ControllerStick::ProcessKeyboardEvent(KbEventType eventType, KbScancode sca
return result;
}

bool ControllerStick::ProcessMouseEvent(bool isPressed, MouseBtn button) {
if (mUseKeydownEventToCreateNewMapping) {
if (isPressed) {
mMouseButtonForNewMapping = button;
return true;
} else {
mMouseButtonForNewMapping = MouseBtn::MOUSE_BTN_UNKNOWN;
}
}

bool result = false;
for (auto [direction, mappings] : mAxisDirectionMappings) {
for (auto [id, mapping] : mappings) {
if (mapping->GetMappingType() == MAPPING_TYPE_MOUSE) {
std::shared_ptr<MouseKeyToAxisDirectionMapping> mtoadMapping =
std::dynamic_pointer_cast<MouseKeyToAxisDirectionMapping>(mapping);
if (mtoadMapping != nullptr) {
result = result || mtoadMapping->ProcessMouseEvent(isPressed, button);
}
}
}
}
return result;
}

void ControllerStick::SetSensitivity(uint8_t sensitivityPercentage) {
mSensitivityPercentage = sensitivityPercentage;
mSensitivity = sensitivityPercentage / 100.0f;
Expand Down
3 changes: 3 additions & 0 deletions src/controller/controldevice/controller/ControllerStick.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <unordered_map>
#include "controller/controldevice/controller/mapping/keyboard/KeyboardScancodes.h"
#include "window/MouseMeta.h"

namespace Ship {

Expand Down Expand Up @@ -52,6 +53,7 @@ class ControllerStick {
bool NotchSnapAngleIsDefault();

bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
bool ProcessMouseEvent(bool isPressed, Ship::MouseBtn button);

bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
StickIndex GetStickIndex();
Expand All @@ -77,5 +79,6 @@ class ControllerStick {

bool mUseKeydownEventToCreateNewMapping;
KbScancode mKeyboardScancodeForNewMapping;
MouseBtn mMouseButtonForNewMapping;
};
} // namespace Ship
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "Context.h"
#include "controller/deviceindex/ShipDeviceIndexToSDLDeviceIndexMapping.h"

#include "window/MouseMeta.h"

namespace Ship {
std::shared_ptr<ControllerAxisDirectionMapping>
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIndex, StickIndex stickIndex,
Expand Down Expand Up @@ -75,6 +77,22 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIn
portIndex, stickIndex, static_cast<Direction>(direction), static_cast<KbScancode>(scancode));
}

if (mappingClass == "MouseKeyToAxisDirectionMapping") {
int32_t direction = CVarGetInteger(StringHelper::Sprintf("%s.Direction", mappingCvarKey.c_str()).c_str(), -1);
int mouseButton =
CVarGetInteger(StringHelper::Sprintf("%s.MouseButton", mappingCvarKey.c_str()).c_str(), 0);

if (direction != LEFT && direction != RIGHT && direction != UP && direction != DOWN) {
// something about this mapping is invalid
CVarClear(mappingCvarKey.c_str());
CVarSave();
return nullptr;
}

return std::make_shared<MouseKeyToAxisDirectionMapping>(portIndex, stick, static_cast<Direction>(direction),
static_cast<MouseBtn>(mouseButton));
}

return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "MouseKeyToAxisDirectionMapping.h"
#include <spdlog/spdlog.h>
#include "utils/StringHelper.h"
#include "window/gui/IconsFontAwesome4.h"
#include "public/bridge/consolevariablebridge.h"
#include "Context.h"

namespace Ship {
KeyMouseToAxisDirectionMapping::MouseKeyToAxisDirectionMapping(uint8_t portIndex, Stick stick,
Direction direction, MouseBtn button)
: ControllerInputMapping(ShipDeviceIndex::Mouse), MouseKeyToAnyMapping(button),
ControllerAxisDirectionMapping(ShipDeviceIndex::Mouse, portIndex, stick, direction) {
}

float MouseKeyToAxisDirectionMapping::GetNormalizedAxisDirectionValue() {
if (Context::GetInstance()->GetControlDeck()->MouseGameInputBlocked()) {
return 0.0f;
}

return mKeyPressed ? MAX_AXIS_RANGE : 0.0f;
}

std::string MouseKeyToAxisDirectionMapping::GetAxisDirectionMappingId() {
return StringHelper::Sprintf("P%d-S%d-D%d-MOUSE%d", mPortIndex, mStick, mDirection, mButton);
}

void MouseKeyToAxisDirectionMapping::SaveToConfig() {
const std::string mappingCvarKey = CVAR_PREFIX_CONTROLLERS ".AxisDirectionMappings." + GetAxisDirectionMappingId();
CVarSetString(StringHelper::Sprintf("%s.AxisDirectionMappingClass", mappingCvarKey.c_str()).c_str(),
"MouseKeyToAxisDirectionMapping");
CVarSetInteger(StringHelper::Sprintf("%s.Stick", mappingCvarKey.c_str()).c_str(), mStick);
CVarSetInteger(StringHelper::Sprintf("%s.Direction", mappingCvarKey.c_str()).c_str(), mDirection);
CVarSetInteger(StringHelper::Sprintf("%s.MouseButton", mappingCvarKey.c_str()).c_str(), static_cast<int>(mButton));
CVarSave();
}

void MouseKeyToAxisDirectionMapping::EraseFromConfig() {
const std::string mappingCvarKey = CVAR_PREFIX_CONTROLLERS ".AxisDirectionMappings." + GetAxisDirectionMappingId();
CVarClear(StringHelper::Sprintf("%s.Stick", mappingCvarKey.c_str()).c_str());
CVarClear(StringHelper::Sprintf("%s.Direction", mappingCvarKey.c_str()).c_str());
CVarClear(StringHelper::Sprintf("%s.AxisDirectionMappingClass", mappingCvarKey.c_str()).c_str());
CVarClear(StringHelper::Sprintf("%s.MouseButton", mappingCvarKey.c_str()).c_str());
CVarSave();
}

uint8_t MouseKeyToAxisDirectionMapping::GetMappingType() {
return MAPPING_TYPE_MOUSE;
}
} // namespace Ship
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "controller/controldevice/controller/mapping/ControllerAxisDirectionMapping.h"
#include "MouseKeyToAnyMapping.h"
#include "window/MouseMeta.h"

namespace Ship {
class MouseKeyToAxisDirectionMapping final : public MouseKeyToAnyMapping, public ControllerAxisDirectionMapping {
public:
MouseKeyToAxisDirectionMapping(uint8_t portIndex, Stick stick, Direction direction, MouseBtn button);
float GetNormalizedAxisDirectionValue() override;
std::string GetAxisDirectionMappingId() override;
uint8_t GetMappingType() override;
void SaveToConfig() override;
void EraseFromConfig() override;
};
} // namespace Ship

0 comments on commit 32a87a4

Please sign in to comment.