nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
input.cc
Go to the documentation of this file.
1#include "client/input.h"
2
3#include <imgui.h>
4
5#include <numbers>
6#include <optional>
7
8namespace nanommo {
9
10namespace {
11
12// Half a turn a second.
13constexpr float kTurnSpeed = std::numbers::pi_v<float>;
14
15// For each pixel the mouse moves.
16constexpr float kDragTurn = 0.005f;
17
18constexpr float kZoomStep = 0.1f;
19
20} // namespace
21
22bool Typing() { return ImGui::GetIO().WantTextInput; }
23
24Vector3 Movement() {
25 const auto down = [](int key, int arrow) {
26 return IsKeyDown(key) || IsKeyDown(arrow);
27 };
28
29 Vector3 direction{0.0f, 0.0f, 0.0f};
30 if (Typing()) {
31 return direction;
32 }
33 if (down(KEY_W, KEY_UP)) {
34 direction.z -= 1.0f;
35 }
36 if (down(KEY_S, KEY_DOWN)) {
37 direction.z += 1.0f;
38 }
39 if (down(KEY_A, KEY_LEFT)) {
40 direction.x -= 1.0f;
41 }
42 if (down(KEY_D, KEY_RIGHT)) {
43 direction.x += 1.0f;
44 }
45 return direction;
46}
47
48bool Running() {
49 return !Typing() && (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT));
50}
51
52bool Attacking() { return !Typing() && IsKeyDown(KEY_SPACE); }
53
54std::optional<int> HotbarKey() {
55 if (Typing()) {
56 return std::nullopt;
57 }
58 const bool shift = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
59 // raylib's KEY_ONE to KEY_NINE follow each other, as in ASCII.
60 for (int slot = 0; slot < 9; ++slot) {
61 if (IsKeyPressed(KEY_ONE + slot)) {
62 return shift ? slot + 9 : slot;
63 }
64 }
65 return std::nullopt;
66}
67
69 return !ImGui::GetIO().WantCaptureMouse &&
70 IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
71}
72
73float CameraTurn(float seconds) {
74 float turn = 0.0f;
75 if (!Typing() && IsKeyDown(KEY_Q)) {
76 turn += kTurnSpeed * seconds;
77 }
78 if (!Typing() && IsKeyDown(KEY_E)) {
79 turn -= kTurnSpeed * seconds;
80 }
81 // The mouse is the GUI's while it's over one of its windows.
82 if (!ImGui::GetIO().WantCaptureMouse &&
83 IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
84 turn -= GetMouseDelta().x * kDragTurn;
85 }
86 return turn;
87}
88
89float CameraTilt() {
90 if (ImGui::GetIO().WantCaptureMouse ||
91 !IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
92 return 0.0f;
93 }
94 return GetMouseDelta().y * kDragTurn;
95}
96
97float CameraZoom() {
98 if (ImGui::GetIO().WantCaptureMouse) {
99 return 0.0f;
100 }
101 return GetMouseWheelMove() * kZoomStep;
102}
103
104} // namespace nanommo
Definition chat.cc:9
Vector3 Movement()
x is to the right and z is towards the camera, each -1, 0 or 1.
Definition input.cc:24
bool Running()
Definition input.cc:48
float CameraZoom()
Definition input.cc:97
bool Attacking()
Definition input.cc:52
float CameraTurn(float seconds)
Definition input.cc:73
bool Typing()
Definition input.cc:22
float CameraTilt()
Definition input.cc:89
bool ClickedWorld()
Definition input.cc:68
std::optional< int > HotbarKey()
Definition input.cc:54