raylib/examples/text/text_input_box.c

131 lines
4.7 KiB
C
Raw Normal View History

2017-04-14 16:37:36 +03:00
/*******************************************************************************************
*
* raylib [text] example - Input Box
*
* This example has been created using raylib 3.5 (www.raylib.com)
2017-04-14 16:37:36 +03:00
* 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
2022-06-21 20:53:18 +03:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
int main(void)
2017-04-14 16:37:36 +03:00
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
const int screenWidth = 800;
const int screenHeight = 450;
2017-04-14 16:37:36 +03:00
InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for null terminator char '\0'
2017-04-14 16:37:36 +03:00
int letterCount = 0;
2021-10-25 11:21:16 +03:00
Rectangle textBox = { screenWidth/2.0f - 100, 180, 225, 50 };
2017-04-14 16:37:36 +03:00
bool mouseOnText = false;
int framesCounter = 0;
SetTargetFPS(10); // Set our game to run at 10 frames-per-second
2017-04-14 16:37:36 +03: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 17:36:42 +03:00
2017-04-14 16:37:36 +03:00
if (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();
2019-05-20 17:36:42 +03:00
// Check if more characters have been pressed on the same frame
while (key > 0)
2017-04-14 16:37:36 +03:00
{
// NOTE: Only allow keys in range [32..125]
if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
{
name[letterCount] = (char)key;
name[letterCount+1] = '\0'; // Add null terminator at the end of the string.
letterCount++;
}
2021-04-22 19:55:24 +03:00
key = GetCharPressed(); // Check next character in the queue
2017-04-14 16:37:36 +03:00
}
2019-05-20 17:36:42 +03:00
2018-01-01 01:50:22 +03:00
if (IsKeyPressed(KEY_BACKSPACE))
2017-04-14 16:37:36 +03:00
{
letterCount--;
if (letterCount < 0) letterCount = 0;
name[letterCount] = '\0';
2017-04-14 16:37:36 +03:00
}
}
2021-03-19 21:12:08 +03:00
else SetMouseCursor(MOUSE_CURSOR_DEFAULT);
2019-05-20 17:36:42 +03:00
2017-04-14 16:37:36 +03:00
if (mouseOnText) framesCounter++;
else framesCounter = 0;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
2019-05-20 17:36:42 +03:00
2017-04-14 16:37:36 +03:00
DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
DrawRectangleRec(textBox, LIGHTGRAY);
2021-10-25 11:21:16 +03: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 17:36:42 +03:00
2021-10-25 11:21:16 +03:00
DrawText(name, (int)textBox.x + 5, (int)textBox.y + 8, 40, MAROON);
2019-05-20 17:36:42 +03:00
DrawText(TextFormat("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
2017-04-14 16:37:36 +03:00
if (mouseOnText)
{
if (letterCount < MAX_INPUT_CHARS)
{
// Draw blinking underscore char
2021-10-25 11:21:16 +03:00
if (((framesCounter/20)%2) == 0) DrawText("_", (int)textBox.x + 8 + MeasureText(name, 40), (int)textBox.y + 12, 40, MAROON);
2017-04-14 16:37:36 +03:00
}
else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
}
2019-05-20 17:36:42 +03:00
2017-04-14 16:37:36 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
2019-05-20 17:36:42 +03:00
//--------------------------------------------------------------------------------------
2017-04-14 16:37:36 +03: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;
}