text_input_box.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #if defined(PLATFORM_WEB)
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. //----------------------------------------------------------------------------------
  17. // Global Variables Definition
  18. //----------------------------------------------------------------------------------
  19. int screenWidth = 800;
  20. int screenHeight = 450;
  21. char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for line ending char '\0'
  22. int letterCount = 0;
  23. Rectangle textBox;
  24. bool mouseOnText = false;
  25. int framesCounter = 0;
  26. //----------------------------------------------------------------------------------
  27. // Module Functions Declaration
  28. //----------------------------------------------------------------------------------
  29. void UpdateDrawFrame(void); // Update and Draw one frame
  30. //----------------------------------------------------------------------------------
  31. // Main Enry Point
  32. //----------------------------------------------------------------------------------
  33. int main()
  34. {
  35. // Initialization
  36. //--------------------------------------------------------------------------------------
  37. InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
  38. textBox = (Rectangle){ screenWidth/2 - 100, 180, 225, 50 };
  39. #if defined(PLATFORM_WEB)
  40. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  41. #else
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. UpdateDrawFrame();
  48. }
  49. #endif
  50. // De-Initialization
  51. //--------------------------------------------------------------------------------------
  52. CloseWindow(); // Close window and OpenGL context
  53. //--------------------------------------------------------------------------------------
  54. return 0;
  55. }
  56. //----------------------------------------------------------------------------------
  57. // Module Functions Definition
  58. //----------------------------------------------------------------------------------
  59. void UpdateDrawFrame(void)
  60. {
  61. // Update
  62. //----------------------------------------------------------------------------------
  63. if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true;
  64. else mouseOnText = false;
  65. if (mouseOnText)
  66. {
  67. int key = GetKeyPressed();
  68. // NOTE: Only allow keys in range [32..125]
  69. if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
  70. {
  71. name[letterCount] = (char)key;
  72. letterCount++;
  73. }
  74. if (key == KEY_BACKSPACE)
  75. {
  76. letterCount--;
  77. name[letterCount] = '\0';
  78. if (letterCount < 0) letterCount = 0;
  79. }
  80. }
  81. if (mouseOnText) framesCounter++;
  82. else framesCounter = 0;
  83. //----------------------------------------------------------------------------------
  84. // Draw
  85. //----------------------------------------------------------------------------------
  86. BeginDrawing();
  87. ClearBackground(RAYWHITE);
  88. DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
  89. DrawRectangleRec(textBox, LIGHTGRAY);
  90. if (mouseOnText) DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, RED);
  91. else DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY);
  92. DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON);
  93. DrawText(FormatText("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
  94. if (mouseOnText)
  95. {
  96. if (letterCount < MAX_INPUT_CHARS)
  97. {
  98. // Draw blinking underscore char
  99. if (((framesCounter/20)%2) == 0) DrawText("_", textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON);
  100. }
  101. else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
  102. }
  103. EndDrawing();
  104. //----------------------------------------------------------------------------------
  105. }
  106. // Check if any key is pressed
  107. // NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
  108. bool IsAnyKeyPressed()
  109. {
  110. bool keyPressed = false;
  111. int key = GetKeyPressed();
  112. if ((key >= 32) && (key <= 126)) keyPressed = true;
  113. return keyPressed;
  114. }