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.

126 lines
4.4 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 line ending 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. letterCount++;
  48. }
  49. key = GetCharPressed(); // Check next character in the queue
  50. }
  51. if (IsKeyPressed(KEY_BACKSPACE))
  52. {
  53. letterCount--;
  54. if (letterCount < 0) letterCount = 0;
  55. name[letterCount] = '\0';
  56. }
  57. }
  58. else SetMouseCursor(MOUSE_CURSOR_DEFAULT);
  59. if (mouseOnText) framesCounter++;
  60. else framesCounter = 0;
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
  67. DrawRectangleRec(textBox, LIGHTGRAY);
  68. if (mouseOnText) DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, RED);
  69. else DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY);
  70. DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON);
  71. DrawText(TextFormat("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
  72. if (mouseOnText)
  73. {
  74. if (letterCount < MAX_INPUT_CHARS)
  75. {
  76. // Draw blinking underscore char
  77. if (((framesCounter/20)%2) == 0) DrawText("_", textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON);
  78. }
  79. else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
  80. }
  81. EndDrawing();
  82. //----------------------------------------------------------------------------------
  83. }
  84. // De-Initialization
  85. //--------------------------------------------------------------------------------------
  86. CloseWindow(); // Close window and OpenGL context
  87. //--------------------------------------------------------------------------------------
  88. return 0;
  89. }
  90. // Check if any key is pressed
  91. // NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
  92. bool IsAnyKeyPressed()
  93. {
  94. bool keyPressed = false;
  95. int key = GetKeyPressed();
  96. if ((key >= 32) && (key <= 126)) keyPressed = true;
  97. return keyPressed;
  98. }