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.

115 lines
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Input Box
  4. *
  5. * This example has been created using raylib 1.7 (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(60); // Set our game to run at 60 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. int key = GetKeyPressed();
  37. // NOTE: Only allow keys in range [32..125]
  38. if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
  39. {
  40. name[letterCount] = (char)key;
  41. letterCount++;
  42. }
  43. if (IsKeyPressed(KEY_BACKSPACE))
  44. {
  45. letterCount--;
  46. name[letterCount] = '\0';
  47. if (letterCount < 0) letterCount = 0;
  48. }
  49. }
  50. if (mouseOnText) framesCounter++;
  51. else framesCounter = 0;
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
  58. DrawRectangleRec(textBox, LIGHTGRAY);
  59. if (mouseOnText) DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, RED);
  60. else DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY);
  61. DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON);
  62. DrawText(FormatText("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
  63. if (mouseOnText)
  64. {
  65. if (letterCount < MAX_INPUT_CHARS)
  66. {
  67. // Draw blinking underscore char
  68. if (((framesCounter/20)%2) == 0) DrawText("_", textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON);
  69. }
  70. else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
  71. }
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }
  81. // Check if any key is pressed
  82. // NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
  83. bool IsAnyKeyPressed()
  84. {
  85. bool keyPressed = false;
  86. int key = GetKeyPressed();
  87. if ((key >= 32) && (key <= 126)) keyPressed = true;
  88. return keyPressed;
  89. }