-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTTInput.cpp
88 lines (72 loc) · 2.51 KB
/
TTTInput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "TTTInput.hpp"
Input::Input() {};
// Function for updating the mouse position
void Input::UpdateMousePosition(Window *window, Time *time)
{
position = GetScreenToWorld2D(GetMousePosition(), window->camera);
}
// Check if any key is pressed
// NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
bool Input::IsAnyKeyPressed()
{
bool keyPressed = false;
int key = GetKeyPressed();
if ((key >= 32) && (key <= 126)) keyPressed = true;
return keyPressed;
}
// Function for drawing the text box
void Input::DrawTextBox()
{
DrawRectangleRec(tb.textBox, LIGHTGRAY);
if (tb.mouseOnText) DrawRectangleLines(tb.textBox.x, tb.textBox.y, tb.textBox.width, tb.textBox.height, RED);
else DrawRectangleLines(tb.textBox.x, tb.textBox.y, tb.textBox.width, tb.textBox.height, DARKGRAY);
DrawText(tb.choice, tb.textBox.x + 5, tb.textBox.y + 8, 40, MAROON);
if (tb.mouseOnText)
{
if (tb.letterCount < 2)
{
// Draw blinking underscore char
if (((tb.framesCounter / 20) % 2) == 0) DrawText("_", tb.textBox.x + 8 + MeasureText(tb.choice, 40), tb.textBox.y + 12, 40, MAROON);
}
}
}
// Function to update the text box
void Input::UpdateTextBox()
{
if (CheckCollisionPointRec(GetMousePosition(), tb.textBox)) tb.mouseOnText = true;
else tb.mouseOnText = false;
if (tb.mouseOnText)
{
// Set the window's cursor to the I-Beam
SetMouseCursor(MOUSE_CURSOR_IBEAM);
// Get char pressed (unicode character) on the queue
int key = GetCharPressed();
// Check if more characters have been pressed on the same frame
while (key > 0)
{
// NOTE: Only allow keys in range [32..125]
if ((key >= 32) && (key <= 125) && (tb.letterCount < 2))
{
tb.choice[tb.letterCount] = (char)key;
tb.letterCount++;
}
key = GetCharPressed(); // Check next character in the queue
}
if (IsKeyPressed(KEY_BACKSPACE))
{
tb.letterCount--;
if (tb.letterCount < 0) tb.letterCount = 0;
tb.choice[tb.letterCount] = '\0';
}
}
else if (GetMouseCursor() != MOUSE_CURSOR_DEFAULT) SetMouseCursor(MOUSE_CURSOR_DEFAULT);
if (tb.mouseOnText) tb.framesCounter++;
else tb.framesCounter = 0;
}
// Function that updates all inputs
void Input::UpdateInput(Window *window, Time *time)
{
UpdateMousePosition(window, time);
UpdateTextBox();
IsAnyKeyPressed();
}