Skip to content

Commit

Permalink
fix a game controller issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Nov 20, 2023
1 parent 8db7d0d commit 54aa919
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Assets/Script/Lib/Dora/en/Controller.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local record Controller
"lefty"
"rightx"
"righty"
"lefttrigger"
"righttrigger"
end

-- Enumeration for defining the controller button names.
Expand All @@ -21,10 +23,8 @@ local record Controller
"dpup"
"leftshoulder"
"leftstick"
"lefttrigger"
"rightshoulder"
"rightstick"
"righttrigger"
"start"
"x"
"y"
Expand Down
4 changes: 2 additions & 2 deletions Assets/Script/Lib/Dora/zh-Hans/Controller.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local record Controller
"lefty"
"rightx"
"righty"
"lefttrigger"
"righttrigger"
end

-- 用于定义控制器按钮名称的枚举。
Expand All @@ -21,10 +23,8 @@ local record Controller
"dpup"
"leftshoulder"
"leftstick"
"lefttrigger"
"rightshoulder"
"rightstick"
"righttrigger"
"start"
"x"
"y"
Expand Down
7 changes: 5 additions & 2 deletions Source/Input/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void Controller::addControllerInRender(int deviceIndex) {
_deviceMap[joystickId] = New<Device>(deviceId, controller);
});
} else {
Error("failed to open a new controller! {}", SDL_GetError());
Warn("failed to open a new controller! {}", SDL_GetError());
}
}

Expand Down Expand Up @@ -156,10 +156,13 @@ void Controller::handleEventInRender(const SDL_Event& event) {
bool isDown = event.cbutton.state > 0;
SharedApplication.invokeInLogic([buttonName, joystickId, isDown, this]() {
if (auto it = _deviceMap.find(joystickId); it != _deviceMap.end()) {
Device::ButtonState state;
Device::ButtonState state{.oldState = false, .newState = false};
if (auto bit = it->second->buttonMap.find(buttonName); bit != it->second->buttonMap.end()) {
bit->second.newState = isDown;
state = bit->second;
} else {
state.newState = isDown;
it->second->buttonMap[buttonName] = state;
}
if (!state.oldState && state.newState) {
EventArgs<int, Slice> button("ButtonDown"_slice, it->second->id, buttonName);
Expand Down

0 comments on commit 54aa919

Please sign in to comment.