Skip to content

Commit 362323c

Browse files
committedJan 20, 2018
Game/Input refactor [1/X]: make RealInputHandler handle joystick inputs with standard input
Joystick input is a RealInputHandler only usage, make it intelligent and handle the joystick with keyboard direct. This permits to remove many getters in game which should be owned by RealInputHandler
1 parent 9649e47 commit 362323c

File tree

2 files changed

+66
-63
lines changed

2 files changed

+66
-63
lines changed
 

‎src/client/inputhandler.h

+47-12
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,53 @@ class RealInputHandler : public InputHandler
259259
}
260260
}
261261

262-
virtual bool getLeftState() { return m_receiver->left_active; }
263-
virtual bool getRightState() { return m_receiver->right_active; }
264-
265-
virtual bool getLeftClicked() { return m_receiver->leftclicked; }
266-
virtual bool getRightClicked() { return m_receiver->rightclicked; }
267-
virtual void resetLeftClicked() { m_receiver->leftclicked = false; }
268-
virtual void resetRightClicked() { m_receiver->rightclicked = false; }
269-
270-
virtual bool getLeftReleased() { return m_receiver->leftreleased; }
271-
virtual bool getRightReleased() { return m_receiver->rightreleased; }
272-
virtual void resetLeftReleased() { m_receiver->leftreleased = false; }
273-
virtual void resetRightReleased() { m_receiver->rightreleased = false; }
262+
virtual bool getLeftState()
263+
{
264+
return m_receiver->left_active || joystick.isKeyDown(KeyType::MOUSE_L);
265+
}
266+
virtual bool getRightState()
267+
{
268+
return m_receiver->right_active || joystick.isKeyDown(KeyType::MOUSE_R);
269+
}
270+
271+
virtual bool getLeftClicked()
272+
{
273+
return m_receiver->leftclicked || joystick.getWasKeyDown(KeyType::MOUSE_L);
274+
}
275+
virtual bool getRightClicked()
276+
{
277+
return m_receiver->rightclicked || joystick.getWasKeyDown(KeyType::MOUSE_R);
278+
}
279+
280+
virtual void resetLeftClicked()
281+
{
282+
m_receiver->leftclicked = false;
283+
joystick.clearWasKeyDown(KeyType::MOUSE_L);
284+
}
285+
virtual void resetRightClicked() {
286+
m_receiver->rightclicked = false;
287+
joystick.clearWasKeyDown(KeyType::MOUSE_R);
288+
}
289+
290+
virtual bool getLeftReleased()
291+
{
292+
return m_receiver->leftreleased || joystick.wasKeyReleased(KeyType::MOUSE_L);
293+
}
294+
virtual bool getRightReleased()
295+
{
296+
return m_receiver->rightreleased || joystick.wasKeyReleased(KeyType::MOUSE_R);
297+
}
298+
299+
virtual void resetLeftReleased()
300+
{
301+
m_receiver->leftreleased = false;
302+
joystick.clearWasKeyReleased(KeyType::MOUSE_L);
303+
}
304+
virtual void resetRightReleased()
305+
{
306+
m_receiver->rightreleased = false;
307+
joystick.clearWasKeyReleased(KeyType::MOUSE_R);
308+
}
274309

275310
virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
276311

‎src/game.cpp

+19-51
Original file line numberDiff line numberDiff line change
@@ -1211,32 +1211,6 @@ class Game {
12111211
static void settingChangedCallback(const std::string &setting_name, void *data);
12121212
void readSettings();
12131213

1214-
inline bool getLeftClicked()
1215-
{
1216-
return input->getLeftClicked() ||
1217-
input->joystick.getWasKeyDown(KeyType::MOUSE_L);
1218-
}
1219-
inline bool getRightClicked()
1220-
{
1221-
return input->getRightClicked() ||
1222-
input->joystick.getWasKeyDown(KeyType::MOUSE_R);
1223-
}
1224-
inline bool isLeftPressed()
1225-
{
1226-
return input->getLeftState() ||
1227-
input->joystick.isKeyDown(KeyType::MOUSE_L);
1228-
}
1229-
inline bool isRightPressed()
1230-
{
1231-
return input->getRightState() ||
1232-
input->joystick.isKeyDown(KeyType::MOUSE_R);
1233-
}
1234-
inline bool getLeftReleased()
1235-
{
1236-
return input->getLeftReleased() ||
1237-
input->joystick.wasKeyReleased(KeyType::MOUSE_L);
1238-
}
1239-
12401214
inline bool isKeyDown(GameKeyType k)
12411215
{
12421216
return input->isKeyDown(keycache.key[k]) || input->joystick.isKeyDown(k);
@@ -2882,8 +2856,8 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
28822856
isKeyDown(KeyType::SPECIAL1),
28832857
isKeyDown(KeyType::SNEAK),
28842858
isKeyDown(KeyType::ZOOM),
2885-
isLeftPressed(),
2886-
isRightPressed(),
2859+
input->getLeftState(),
2860+
input->getRightState(),
28872861
cam.camera_pitch,
28882862
cam.camera_yaw,
28892863
input->joystick.getAxisWithoutDead(JA_SIDEWARD_MOVE),
@@ -2898,8 +2872,8 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
28982872
( (u32)(isKeyDown(KeyType::JUMP) & 0x1) << 4) |
28992873
( (u32)(isKeyDown(KeyType::SPECIAL1) & 0x1) << 5) |
29002874
( (u32)(isKeyDown(KeyType::SNEAK) & 0x1) << 6) |
2901-
( (u32)(isLeftPressed() & 0x1) << 7) |
2902-
( (u32)(isRightPressed() & 0x1) << 8
2875+
( (u32)(input->getLeftState() & 0x1) << 7) |
2876+
( (u32)(input->getRightState() & 0x1) << 8
29032877
);
29042878

29052879
#ifdef ANDROID
@@ -3441,7 +3415,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
34413415
hud->updateSelectionMesh(camera_offset);
34423416
}
34433417

3444-
if (runData.digging_blocked && !isLeftPressed()) {
3418+
if (runData.digging_blocked && !input->getLeftState()) {
34453419
// allow digging again if button is not pressed
34463420
runData.digging_blocked = false;
34473421
}
@@ -3452,7 +3426,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
34523426
- pointing away from node
34533427
*/
34543428
if (runData.digging) {
3455-
if (getLeftReleased()) {
3429+
if (input->getLeftReleased()) {
34563430
infostream << "Left button released"
34573431
<< " (stopped digging)" << std::endl;
34583432
runData.digging = false;
@@ -3476,13 +3450,13 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
34763450
client->setCrack(-1, v3s16(0, 0, 0));
34773451
runData.dig_time = 0.0;
34783452
}
3479-
} else if (runData.dig_instantly && getLeftReleased()) {
3453+
} else if (runData.dig_instantly && input->getLeftReleased()) {
34803454
// Remove e.g. torches faster when clicking instead of holding LMB
34813455
runData.nodig_delay_timer = 0;
34823456
runData.dig_instantly = false;
34833457
}
34843458

3485-
if (!runData.digging && runData.ldown_for_dig && !isLeftPressed()) {
3459+
if (!runData.digging && runData.ldown_for_dig && !input->getLeftState()) {
34863460
runData.ldown_for_dig = false;
34873461
}
34883462

@@ -3491,13 +3465,13 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
34913465
soundmaker->m_player_leftpunch_sound.name = "";
34923466

34933467
// Prepare for repeating, unless we're not supposed to
3494-
if (isRightPressed() && !g_settings->getBool("safe_dig_and_place"))
3468+
if (input->getRightState() && !g_settings->getBool("safe_dig_and_place"))
34953469
runData.repeat_rightclick_timer += dtime;
34963470
else
34973471
runData.repeat_rightclick_timer = 0;
34983472

3499-
if (playeritem_def.usable && isLeftPressed()) {
3500-
if (getLeftClicked() && (!client->moddingEnabled()
3473+
if (playeritem_def.usable && input->getLeftState()) {
3474+
if (input->getLeftClicked() && (!client->moddingEnabled()
35013475
|| !client->getScript()->on_item_use(playeritem, pointed)))
35023476
client->interact(4, pointed);
35033477
} else if (pointed.type == POINTEDTHING_NODE) {
@@ -3515,29 +3489,23 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
35153489
playeritem_toolcap, dtime);
35163490
} else if (pointed.type == POINTEDTHING_OBJECT) {
35173491
handlePointingAtObject(pointed, playeritem, player_position, show_debug);
3518-
} else if (isLeftPressed()) {
3492+
} else if (input->getLeftState()) {
35193493
// When button is held down in air, show continuous animation
35203494
runData.left_punch = true;
3521-
} else if (getRightClicked()) {
3495+
} else if (input->getRightClicked()) {
35223496
handlePointingAtNothing(playeritem);
35233497
}
35243498

35253499
runData.pointed_old = pointed;
35263500

3527-
if (runData.left_punch || getLeftClicked())
3501+
if (runData.left_punch || input->getLeftClicked())
35283502
camera->setDigging(0); // left click animation
35293503

35303504
input->resetLeftClicked();
35313505
input->resetRightClicked();
35323506

3533-
input->joystick.clearWasKeyDown(KeyType::MOUSE_L);
3534-
input->joystick.clearWasKeyDown(KeyType::MOUSE_R);
3535-
35363507
input->resetLeftReleased();
35373508
input->resetRightReleased();
3538-
3539-
input->joystick.clearWasKeyReleased(KeyType::MOUSE_L);
3540-
input->joystick.clearWasKeyReleased(KeyType::MOUSE_R);
35413509
}
35423510

35433511

@@ -3654,7 +3622,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
36543622

36553623
ClientMap &map = client->getEnv().getClientMap();
36563624

3657-
if (runData.nodig_delay_timer <= 0.0 && isLeftPressed()
3625+
if (runData.nodig_delay_timer <= 0.0 && input->getLeftState()
36583626
&& !runData.digging_blocked
36593627
&& client->checkPrivilege("interact")) {
36603628
handleDigging(pointed, nodepos, playeritem_toolcap, dtime);
@@ -3675,7 +3643,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
36753643
}
36763644
}
36773645

3678-
if ((getRightClicked() ||
3646+
if ((input->getRightClicked() ||
36793647
runData.repeat_rightclick_timer >= m_repeat_right_click_time) &&
36803648
client->checkPrivilege("interact")) {
36813649
runData.repeat_rightclick_timer = 0;
@@ -3754,7 +3722,7 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &
37543722

37553723
m_game_ui->setInfoText(infotext);
37563724

3757-
if (isLeftPressed()) {
3725+
if (input->getLeftState()) {
37583726
bool do_punch = false;
37593727
bool do_punch_damage = false;
37603728

@@ -3764,7 +3732,7 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &
37643732
runData.object_hit_delay_timer = object_hit_delay;
37653733
}
37663734

3767-
if (getLeftClicked())
3735+
if (input->getLeftClicked())
37683736
do_punch = true;
37693737

37703738
if (do_punch) {
@@ -3791,7 +3759,7 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &
37913759
if (!disable_send)
37923760
client->interact(0, pointed);
37933761
}
3794-
} else if (getRightClicked()) {
3762+
} else if (input->getRightClicked()) {
37953763
infostream << "Right-clicked object" << std::endl;
37963764
client->interact(3, pointed); // place
37973765
}

0 commit comments

Comments
 (0)
Please sign in to comment.