You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.8 KiB

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