Skip to content

Commit

Permalink
move Touchpad toggle to controller section + enable per-game profiles (
Browse files Browse the repository at this point in the history
…#11)

* update backend to support setting touchpad

* move FE toggle to controller section
  • Loading branch information
aarron-lee authored Nov 28, 2023
1 parent 0d64437 commit e6733a0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 28 deletions.
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ async def save_controller_settings(self, controller, currentGameId):
result = settings.set_all_controller_profiles(controllerProfiles)
if currentGameId:
settings.sync_controller_profile_settings(currentGameId)
# also sync touchpad
settings.sync_touchpad(currentGameId)
return result

async def save_rgb_settings(self, rgbProfiles, currentGameId):
Expand Down
15 changes: 13 additions & 2 deletions py_modules/controller_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,23 @@ def sync_controller_profile_settings(current_game_id):
elif remappable_button_name in ['Y1', 'Y2']:
controller_code = Controller['LEFT'].value
if not controller_code:
return
continue

btn_code = RemappableButtons[remappable_button_name].value
action_code = RemapActions[remap_action].value
remap_command = legion_configurator.create_button_remap_command(controller_code, btn_code, action_code)

legion_configurator.send_command(remap_command)
except Exception as e:
decky_plugin.logger.error(f"sync_controller_profile_settings: {e}")
decky_plugin.logger.error(f"sync_controller_profile_settings: {e}")

def sync_touchpad(current_game_id):
settings = get_settings()
controller_profile = settings.get('controller').get(current_game_id, {})

try:
new_touchpad_state = bool(controller_profile.get('TOUCHPAD'))
t_toggle = legion_configurator.create_touchpad_command(new_touchpad_state)
legion_configurator.send_command(t_toggle)
except Exception as e:
decky_plugin.logger.error(f"sync_touchpad: {e}")
11 changes: 10 additions & 1 deletion src/components/controller/RemapButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import RemapActionDropdown from './RemapActionDropdown';
import { PanelSection, PanelSectionRow, ToggleField } from 'decky-frontend-lib';
import {
useControllerPerGameEnabled,
useControllerProfileDisplayName
useControllerProfileDisplayName,
useTouchpadEnabled
} from '../../hooks/controller';
import { IconRow } from '../IconRow';

Expand All @@ -14,6 +15,8 @@ const RemapButtons: FC = () => {
const { controllerPerGameEnabled, setControllerPerGameEnabled } =
useControllerPerGameEnabled();

const { touchpadEnabled, setTouchpad } = useTouchpadEnabled();

const title = controllerPerGameEnabled
? `Remap Buttons -\n${displayName.substring(0, 20)}${
displayName.length > 20 ? '...' : ''
Expand All @@ -37,6 +40,12 @@ const RemapButtons: FC = () => {
</IconRow>
);
})}

<ToggleField
label="Touchpad"
checked={touchpadEnabled}
onChange={(value) => setTouchpad(value)}
></ToggleField>
</PanelSection>
);
};
Expand Down
25 changes: 3 additions & 22 deletions src/components/rgb/ControllerLightingPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { ToggleField, PanelSection, ServerAPI } from 'decky-frontend-lib';
import { PanelSection } from 'decky-frontend-lib';
import { VFC } from 'react';
import { useState } from 'react';
// import { useState } from 'react';
import { useRgbProfileDisplayName } from '../../hooks/rgb';
import { RgbPerGameProfilesToggle } from './RgbPerGameProfilesToggle';
import { RgbSettings } from './RgbSettings';

const DEFAULT_STATE = {
isTouchpad: true
};

const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
serverAPI
}) => {
const ControllerLightingPanel: VFC = () => {
const displayName = useRgbProfileDisplayName();
const [isTouchpad, setIsTouchpad] = useState(DEFAULT_STATE.isTouchpad);

const TPadToggleChange = (value: boolean) => {
setIsTouchpad(value);
console.log(`Toggle value: ${value}`);
serverAPI!.callPluginMethod('set_touchpad', { enable: value });
};

let title =
displayName === 'Default'
Expand All @@ -32,12 +19,6 @@ const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
<RgbPerGameProfilesToggle />
<RgbSettings controller="RIGHT" />
<RgbSettings controller="LEFT" />

<ToggleField
label="Touchpad"
checked={isTouchpad}
onChange={(value) => TPadToggleChange(value)}
></ToggleField>
</div>
</PanelSection>
);
Expand Down
13 changes: 12 additions & 1 deletion src/hooks/controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import {
controllerSlice,
selectButtonRemapAction,
selectControllerPerGameProfilesEnabled,
selectControllerProfileDisplayName
selectControllerProfileDisplayName,
selectTouchpadEnabled
} from '../redux-modules/controllerSlice';
import { RemapActions, RemappableButtons } from '../backend/constants';

export const useTouchpadEnabled = () => {
const touchpadEnabled = useSelector(selectTouchpadEnabled);
const dispatch = useDispatch();

const setTouchpad = (enabled: boolean) => {
return dispatch(controllerSlice.actions.setTouchpad(enabled));
};
return { touchpadEnabled, setTouchpad };
};

export const useControllerPerGameEnabled = () => {
const controllerPerGameEnabled = useSelector(
selectControllerPerGameProfilesEnabled
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Content: VFC<{ serverAPI: ServerAPI }> = memo(({ serverAPI }) => {
}
return (
<>
<ControllerLightingPanel serverAPI={serverAPI} />
<ControllerLightingPanel />
<RemapButtons />
</>
);
Expand Down
24 changes: 23 additions & 1 deletion src/redux-modules/controllerSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const DEFAULT_CONTROLLER_VALUES = {
Y2: RemapActions.DISABLED,
Y3: RemapActions.DISABLED,
M2: RemapActions.DISABLED,
M3: RemapActions.DISABLED
M3: RemapActions.DISABLED,
TOUCHPAD: true
};

type ControllerProfile = {
Expand All @@ -21,6 +22,7 @@ type ControllerProfile = {
Y3: RemapActions;
M2: RemapActions;
M3: RemapActions;
TOUCHPAD: boolean;
};

type ControllerProfiles = {
Expand Down Expand Up @@ -67,6 +69,14 @@ export const controllerSlice = createSlice({
action: PayloadAction<ControllerProfiles>
) => {
merge(state.controllerProfiles, action.payload);
},
setTouchpad: (state, action: PayloadAction<boolean>) => {
const touchpadEnabled = action.payload;
setStateValue({
sliceState: state,
key: 'TOUCHPAD',
value: touchpadEnabled
});
}
},
extraReducers: (builder) => {
Expand Down Expand Up @@ -100,6 +110,17 @@ export const selectControllerPerGameProfilesEnabled = (state: RootState) => {
return state.controller.perGameProfilesEnabled;
};

export const selectTouchpadEnabled = (state: RootState) => {
if (state.controller.perGameProfilesEnabled) {
return get(
state,
`controller.controllerProfiles.${extractCurrentGameId()}.TOUCHPAD`
);
} else {
return get(state, `controller.controllerProfiles.default.TOUCHPAD`);
}
};

export const selectButtonRemapAction =
(button: RemappableButtons) => (state: RootState) => {
if (state.controller.perGameProfilesEnabled) {
Expand Down Expand Up @@ -128,6 +149,7 @@ const mutatingActionTypes = [
controllerSlice.actions.setPerGameProfilesEnabled.type,
controllerSlice.actions.remapButton.type,
controllerSlice.actions.updateControllerProfiles.type,
controllerSlice.actions.setTouchpad.type,
setCurrentGameId.type
];

Expand Down

0 comments on commit e6733a0

Please sign in to comment.