1 | /******************************************************************************************* |
2 | * |
3 | * raylib [text] example - Input Box |
4 | * |
5 | * This example has been created using raylib 1.7 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2017 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #define MAX_INPUT_CHARS 9 |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [text] example - input box" ); |
24 | |
25 | char name[MAX_INPUT_CHARS + 1] = "\0" ; // NOTE: One extra space required for line ending char '\0' |
26 | int letterCount = 0; |
27 | |
28 | Rectangle textBox = { screenWidth/2 - 100, 180, 225, 50 }; |
29 | bool mouseOnText = false; |
30 | |
31 | int framesCounter = 0; |
32 | |
33 | SetTargetFPS(10); // Set our game to run at 60 frames-per-second |
34 | //-------------------------------------------------------------------------------------- |
35 | |
36 | // Main game loop |
37 | while (!WindowShouldClose()) // Detect window close button or ESC key |
38 | { |
39 | // Update |
40 | //---------------------------------------------------------------------------------- |
41 | if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true; |
42 | else mouseOnText = false; |
43 | |
44 | if (mouseOnText) |
45 | { |
46 | // Get pressed key (character) on the queue |
47 | int key = GetKeyPressed(); |
48 | |
49 | // Check if more characters have been pressed on the same frame |
50 | while (key > 0) |
51 | { |
52 | // NOTE: Only allow keys in range [32..125] |
53 | if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS)) |
54 | { |
55 | name[letterCount] = (char)key; |
56 | letterCount++; |
57 | } |
58 | |
59 | key = GetKeyPressed(); // Check next character in the queue |
60 | } |
61 | |
62 | if (IsKeyPressed(KEY_BACKSPACE)) |
63 | { |
64 | letterCount--; |
65 | name[letterCount] = '\0'; |
66 | |
67 | if (letterCount < 0) letterCount = 0; |
68 | } |
69 | } |
70 | |
71 | if (mouseOnText) framesCounter++; |
72 | else framesCounter = 0; |
73 | //---------------------------------------------------------------------------------- |
74 | |
75 | // Draw |
76 | //---------------------------------------------------------------------------------- |
77 | BeginDrawing(); |
78 | |
79 | ClearBackground(RAYWHITE); |
80 | |
81 | DrawText("PLACE MOUSE OVER INPUT BOX!" , 240, 140, 20, GRAY); |
82 | |
83 | DrawRectangleRec(textBox, LIGHTGRAY); |
84 | if (mouseOnText) DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, RED); |
85 | else DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY); |
86 | |
87 | DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON); |
88 | |
89 | DrawText(FormatText("INPUT CHARS: %i/%i" , letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY); |
90 | |
91 | if (mouseOnText) |
92 | { |
93 | if (letterCount < MAX_INPUT_CHARS) |
94 | { |
95 | // Draw blinking underscore char |
96 | if (((framesCounter/20)%2) == 0) DrawText("_" , textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON); |
97 | } |
98 | else DrawText("Press BACKSPACE to delete chars..." , 230, 300, 20, GRAY); |
99 | } |
100 | |
101 | EndDrawing(); |
102 | //---------------------------------------------------------------------------------- |
103 | } |
104 | |
105 | // De-Initialization |
106 | //-------------------------------------------------------------------------------------- |
107 | CloseWindow(); // Close window and OpenGL context |
108 | //-------------------------------------------------------------------------------------- |
109 | |
110 | return 0; |
111 | } |
112 | |
113 | // Check if any key is pressed |
114 | // NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126 |
115 | bool IsAnyKeyPressed() |
116 | { |
117 | bool keyPressed = false; |
118 | int key = GetKeyPressed(); |
119 | |
120 | if ((key >= 32) && (key <= 126)) keyPressed = true; |
121 | |
122 | return keyPressed; |
123 | } |