2017-04-14 15:37:36 +02:00
|
|
|
/*******************************************************************************************
|
|
|
|
|
*
|
2025-08-25 20:57:18 +02:00
|
|
|
* raylib [text] example - input box
|
2017-04-14 15:37:36 +02:00
|
|
|
*
|
2025-01-17 03:42:30 -06:00
|
|
|
* Example complexity rating: [★★☆☆] 2/4
|
|
|
|
|
*
|
2022-07-20 01:28:37 +02:00
|
|
|
* Example originally created with raylib 1.7, last time updated with raylib 3.5
|
2017-04-14 15:37:36 +02:00
|
|
|
*
|
2022-07-20 01:28:37 +02:00
|
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
|
|
|
* BSD-like license that allows static linking with closed source software
|
|
|
|
|
*
|
2025-01-18 10:41:56 -08:00
|
|
|
* Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
|
2017-04-14 15:37:36 +02:00
|
|
|
*
|
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
|
|
#define MAX_INPUT_CHARS 9
|
|
|
|
|
|
2022-06-21 19:53:18 +02:00
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
// Program main entry point
|
|
|
|
|
//------------------------------------------------------------------------------------
|
2019-05-20 16:36:42 +02:00
|
|
|
int main(void)
|
2017-04-14 15:37:36 +02:00
|
|
|
{
|
|
|
|
|
// Initialization
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 16:36:42 +02:00
|
|
|
const int screenWidth = 800;
|
|
|
|
|
const int screenHeight = 450;
|
2017-04-14 15:37:36 +02:00
|
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
|
|
|
|
|
|
2021-06-10 14:24:08 -04:00
|
|
|
char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for null terminator char '\0'
|
2017-04-14 15:37:36 +02:00
|
|
|
int letterCount = 0;
|
|
|
|
|
|
2021-10-25 01:21:16 -07:00
|
|
|
Rectangle textBox = { screenWidth/2.0f - 100, 180, 225, 50 };
|
2017-04-14 15:37:36 +02:00
|
|
|
bool mouseOnText = false;
|
|
|
|
|
|
|
|
|
|
int framesCounter = 0;
|
|
|
|
|
|
2024-08-09 02:18:00 -05:00
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2017-04-14 15:37:36 +02:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
|
{
|
|
|
|
|
// Update
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true;
|
|
|
|
|
else mouseOnText = false;
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2017-04-14 15:37:36 +02:00
|
|
|
if (mouseOnText)
|
|
|
|
|
{
|
2020-10-21 03:55:52 -05:00
|
|
|
// Set the window's cursor to the I-Beam
|
|
|
|
|
SetMouseCursor(MOUSE_CURSOR_IBEAM);
|
|
|
|
|
|
2020-12-18 18:58:02 +01:00
|
|
|
// Get char pressed (unicode character) on the queue
|
|
|
|
|
int key = GetCharPressed();
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2019-11-24 13:39:45 +01:00
|
|
|
// Check if more characters have been pressed on the same frame
|
|
|
|
|
while (key > 0)
|
2017-04-14 15:37:36 +02:00
|
|
|
{
|
2019-11-24 13:39:45 +01:00
|
|
|
// NOTE: Only allow keys in range [32..125]
|
|
|
|
|
if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
|
|
|
|
|
{
|
|
|
|
|
name[letterCount] = (char)key;
|
2025-08-07 18:23:20 +02:00
|
|
|
name[letterCount+1] = '\0'; // Add null terminator at the end of the string
|
2019-11-24 13:39:45 +01:00
|
|
|
letterCount++;
|
|
|
|
|
}
|
2021-04-22 18:55:24 +02:00
|
|
|
|
2020-12-18 18:58:02 +01:00
|
|
|
key = GetCharPressed(); // Check next character in the queue
|
2017-04-14 15:37:36 +02:00
|
|
|
}
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2017-12-31 23:50:22 +01:00
|
|
|
if (IsKeyPressed(KEY_BACKSPACE))
|
2017-04-14 15:37:36 +02:00
|
|
|
{
|
|
|
|
|
letterCount--;
|
|
|
|
|
if (letterCount < 0) letterCount = 0;
|
2020-12-13 16:06:55 +01:00
|
|
|
name[letterCount] = '\0';
|
2017-04-14 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-19 19:12:08 +01:00
|
|
|
else SetMouseCursor(MOUSE_CURSOR_DEFAULT);
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2017-04-14 15:37:36 +02:00
|
|
|
if (mouseOnText) framesCounter++;
|
|
|
|
|
else framesCounter = 0;
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2017-04-14 15:37:36 +02:00
|
|
|
DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
|
|
|
|
|
|
|
|
|
|
DrawRectangleRec(textBox, LIGHTGRAY);
|
2021-10-25 01:21:16 -07:00
|
|
|
if (mouseOnText) DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, RED);
|
|
|
|
|
else DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, DARKGRAY);
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2021-10-25 01:21:16 -07:00
|
|
|
DrawText(name, (int)textBox.x + 5, (int)textBox.y + 8, 40, MAROON);
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2020-08-16 11:28:15 +02:00
|
|
|
DrawText(TextFormat("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
|
2017-04-14 15:37:36 +02:00
|
|
|
|
|
|
|
|
if (mouseOnText)
|
|
|
|
|
{
|
|
|
|
|
if (letterCount < MAX_INPUT_CHARS)
|
|
|
|
|
{
|
|
|
|
|
// Draw blinking underscore char
|
2021-10-25 01:21:16 -07:00
|
|
|
if (((framesCounter/20)%2) == 0) DrawText("_", (int)textBox.x + 8 + MeasureText(name, 40), (int)textBox.y + 12, 40, MAROON);
|
2017-04-14 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
|
|
|
|
|
}
|
2019-05-20 16:36:42 +02:00
|
|
|
|
2017-04-14 15:37:36 +02:00
|
|
|
EndDrawing();
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// De-Initialization
|
2019-05-20 16:36:42 +02:00
|
|
|
//--------------------------------------------------------------------------------------
|
2017-04-14 15:37:36 +02:00
|
|
|
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;
|
2020-12-13 16:06:55 +01:00
|
|
|
}
|