您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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