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.

127 lines
4.5 KiB

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