From 843731e048c803fe6837fd0bbc5c4bf4fbc6337e Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 14 Apr 2017 15:37:36 +0200 Subject: [PATCH] new example: text_input_box --- examples/text/text_input_box.c | 116 +++++++++++++++++++++++++++++++ examples/text/text_input_box.png | Bin 0 -> 15484 bytes 2 files changed, 116 insertions(+) create mode 100644 examples/text/text_input_box.c create mode 100644 examples/text/text_input_box.png 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 0000000000000000000000000000000000000000..42f9d73a37ca3bc8b0df15be4303999286e98a40 GIT binary patch literal 15484 zcmeHOeNa_U}TG%B==EtV%i929CC+6h{yTS6)VviH502m%k-8Qtm5+<)HW zecgBN@1Ec9oO{kBCu(&lV=`wlf*_3Wu;6F}aqvVC$~8I-T$!10t`0rK6T+jK1CV& z5x#Utzerl{;CHM!L@X6Jj&?B5p#DJ{M^sAE#B$@BOAj8zmnFqLmA)_H1(gV^ReXJK zSYznf6=B`}MV($tzCat%z(#!%J6|)RuP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