Skip to content
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

Setting to allow switching mobile joystick functions #418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,13 @@ <h3 translation="settings"></h3>
<input type="range" max="1" min="0.1" id="slider-joystick-transparency" step="0.1">
</label>
</div>
<div class="modal-item checkbox-setting">
<label>
<span class="setting-title"><span>Switch mobile joysticks</span><i></i></span>
<input type="checkbox" id="toggle-mobile-joysticks">
</label>
</div>
<p style="color: red; font-size: 15px" id="mb-joystick-info"><i>Left joystick moves, Right joystick aims</i></p>
<div class="modal-item checkbox-setting">
<label>
<span class="setting-title" translation="settings_autopickup"></span>
Expand Down
29 changes: 18 additions & 11 deletions client/src/scripts/managers/inputManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,28 +273,35 @@ export class InputManager {
const size = game.console.getBuiltInCVar("mb_joystick_size");
const transparency = game.console.getBuiltInCVar("mb_joystick_transparency");

const leftJoyStick = nipplejs.create({
const leftJoystick = nipplejs.create({
zone: $("#left-joystick-container")[0],
size,
color: `rgba(255, 255, 255, ${transparency})`
});

const rightJoyStick = nipplejs.create({
const rightJoystick = nipplejs.create({
zone: $("#right-joystick-container")[0],
size,
color: `rgba(255, 255, 255, ${transparency})`
});

let rightJoyStickUsed = false;
let movementJoystick = leftJoystick;
let aimJoystick = rightJoystick;
const mbJoystickInfo = document.getElementById("mb-joystick-info");
if (game.console.getBuiltInCVar("mb_switch_joysticks")) {
movementJoystick = rightJoystick;
aimJoystick = leftJoystick;
if (mbJoystickInfo) mbJoystickInfo.textContent = "Right joystick moves, Left joystick aims";
} else if (mbJoystickInfo) mbJoystickInfo.textContent = "Left joystick moves, Right joystick aims";
let aimJoystickUsed = false;
let shootOnRelease = false;

leftJoyStick.on("move", (_, data: JoystickOutputData) => {
movementJoystick.on("move", (_, data: JoystickOutputData) => {
const movementAngle = -Math.atan2(data.vector.y, data.vector.x);

this.movementAngle = movementAngle;
this.movement.moving = true;

if (!rightJoyStickUsed && !shootOnRelease) {
if (!aimJoystickUsed && !shootOnRelease) {
this.rotation = movementAngle;
this.turning = true;
if (game.console.getBuiltInCVar("cv_responsive_rotation") && !game.gameOver && game.activePlayer) {
Expand All @@ -303,12 +310,12 @@ export class InputManager {
}
});

leftJoyStick.on("end", () => {
movementJoystick.on("end", () => {
this.movement.moving = false;
});

rightJoyStick.on("move", (_, data) => {
rightJoyStickUsed = true;
aimJoystick.on("move", (_, data) => {
aimJoystickUsed = true;
this.rotation = -Math.atan2(data.vector.y, data.vector.x);
this.turning = true;
const activePlayer = game.activePlayer;
Expand All @@ -334,8 +341,8 @@ export class InputManager {
}
});

rightJoyStick.on("end", () => {
rightJoyStickUsed = false;
aimJoystick.on("end", () => {
aimJoystickUsed = false;
if (game.activePlayer) game.activePlayer.images.aimTrail.alpha = 0;
this.attacking = shootOnRelease;
this.resetAttacking = true;
Expand Down
1 change: 1 addition & 0 deletions client/src/scripts/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,7 @@ export async function setUpUI(game: Game): Promise<void> {
addCheckboxListener("#toggle-mobile-controls", "mb_controls_enabled");
addSliderListener("#slider-joystick-size", "mb_joystick_size");
addSliderListener("#slider-joystick-transparency", "mb_joystick_transparency");
addCheckboxListener("#toggle-mobile-joysticks", "mb_switch_joysticks");
addCheckboxListener("#toggle-high-res-mobile", "mb_high_res_textures");

function updateUiScale(): void {
Expand Down
2 changes: 2 additions & 0 deletions client/src/scripts/utils/console/defaultClientCVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const CVarCasters = Object.freeze({
mb_controls_enabled: Casters.toBoolean,
mb_joystick_size: Casters.toNumber,
mb_joystick_transparency: Casters.toNumber,
mb_switch_joysticks: Casters.toBoolean,
mb_high_res_textures: Casters.toBoolean,

dv_password: Casters.toString,
Expand Down Expand Up @@ -214,6 +215,7 @@ export const defaultClientCVars: SimpleCVarMapping = Object.freeze({
pf_show_ping: false,
pf_show_pos: false,

mb_switch_joysticks: false,
mb_controls_enabled: true,
mb_joystick_size: 150,
mb_joystick_transparency: 0.8,
Expand Down