aboutsummaryrefslogtreecommitdiff
path: root/examples/text
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2017-04-14 15:37:36 +0200
committerraysan5 <raysan5@gmail.com>2017-04-14 15:37:36 +0200
commit843731e048c803fe6837fd0bbc5c4bf4fbc6337e (patch)
tree3485b96cc3203e234ae60207b2bc5039d77d8557 /examples/text
parent4315b82ea7b42b68572f14c595aa46f030d82a43 (diff)
downloadraylib-843731e048c803fe6837fd0bbc5c4bf4fbc6337e.tar.gz
raylib-843731e048c803fe6837fd0bbc5c4bf4fbc6337e.zip
new example: text_input_box
Diffstat (limited to 'examples/text')
-rw-r--r--examples/text/text_input_box.c116
-rw-r--r--examples/text/text_input_box.pngbin0 -> 15484 bytes
2 files changed, 116 insertions, 0 deletions
diff --git a/examples/text/text_input_box.c b/examples/text/text_input_box.c
new file mode 100644
index 00000000..54eebf40
--- /dev/null
+++ b/examples/text/text_input_box.c
@@ -0,0 +1,116 @@
+/*******************************************************************************************
+*
+* raylib [text] example - Input Box
+*
+* This example has been created using raylib 1.7 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2017 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+#define MAX_INPUT_CHARS 9
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
+
+ char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for line ending char '\0'
+ int letterCount = 0;
+
+ Rectangle textBox = { screenWidth/2 - 100, 180, 225, 50 };
+ bool mouseOnText = false;
+
+ int framesCounter = 0;
+
+ SetTargetFPS(60);
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true;
+ else mouseOnText = false;
+
+ if (mouseOnText)
+ {
+ int key = GetKeyPressed();
+
+ // NOTE: Only allow keys in range [32..125]
+ if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
+ {
+ name[letterCount] = (char)key;
+ letterCount++;
+ }
+
+ if (key == KEY_BACKSPACE)
+ {
+ letterCount--;
+ name[letterCount] = '\0';
+
+ if (letterCount < 0) letterCount = 0;
+ }
+ }
+
+ if (mouseOnText) framesCounter++;
+ else framesCounter = 0;
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
+
+ DrawRectangleRec(textBox, LIGHTGRAY);
+ if (mouseOnText) DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, RED);
+ else DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY);
+
+ DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON);
+
+ DrawText(FormatText("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
+
+ if (mouseOnText)
+ {
+ if (letterCount < MAX_INPUT_CHARS)
+ {
+ // Draw blinking underscore char
+ if (((framesCounter/20)%2) == 0) DrawText("_", textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON);
+ }
+ else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
+ }
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+// Check if any key is pressed
+// NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
+bool IsAnyKeyPressed()
+{
+ bool keyPressed = false;
+ int key = GetKeyPressed();
+
+ if ((key >= 32) && (key <= 126)) keyPressed = true;
+
+ return keyPressed;
+} \ No newline at end of file
diff --git a/examples/text/text_input_box.png b/examples/text/text_input_box.png
new file mode 100644
index 00000000..42f9d73a
--- /dev/null
+++ b/examples/text/text_input_box.png
Binary files differ