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.

121 lines
5.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Draw text inside a rectangle
  4. *
  5. * This example has been created using raylib 2.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [text] example - draw text inside a rectangle");
  21. const char text[] = "Text cannot escape\tthis container\t...word wrap also works when active so here's \
  22. a long text for testing.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \
  23. tempor incididunt ut labore et dolore magna aliqua. Nec ullamcorper sit amet risus nullam eget felis eget.";
  24. bool resizing = false;
  25. bool wordWrap = true;
  26. Rectangle container = { 25, 25, screenWidth - 50, screenHeight - 250};
  27. Rectangle resizer = { container.x + container.width - 17, container.y + container.height - 17, 14, 14 };
  28. // Minimum width and heigh for the container rectangle
  29. const int minWidth = 60;
  30. const int minHeight = 60;
  31. const int maxWidth = screenWidth - 50;
  32. const int maxHeight = screenHeight - 160;
  33. Vector2 lastMouse = { 0.0f, 0.0f }; // Stores last mouse coordinates
  34. Color borderColor = MAROON; // Container border color
  35. Font font = GetFontDefault(); // Get default system font
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. if (IsKeyPressed(KEY_SPACE)) wordWrap = !wordWrap;
  44. Vector2 mouse = GetMousePosition();
  45. // Check if the mouse is inside the container and toggle border color
  46. if (CheckCollisionPointRec(mouse, container)) borderColor = Fade(MAROON, 0.4f);
  47. else if (!resizing) borderColor = MAROON;
  48. // Container resizing logic
  49. if (resizing)
  50. {
  51. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) resizing = false;
  52. int width = container.width + (mouse.x - lastMouse.x);
  53. container.width = (width > minWidth)? ((width < maxWidth)? width : maxWidth) : minWidth;
  54. int height = container.height + (mouse.y - lastMouse.y);
  55. container.height = (height > minHeight)? ((height < maxHeight)? height : maxHeight) : minHeight;
  56. }
  57. else
  58. {
  59. // Check if we're resizing
  60. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, resizer)) resizing = true;
  61. }
  62. // Move resizer rectangle properly
  63. resizer.x = container.x + container.width - 17;
  64. resizer.y = container.y + container.height - 17;
  65. lastMouse = mouse; // Update mouse
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginDrawing();
  70. ClearBackground(RAYWHITE);
  71. DrawRectangleLinesEx(container, 3, borderColor); // Draw container border
  72. // Draw text in container (add some padding)
  73. DrawTextRec(font, text,
  74. (Rectangle){ container.x + 4, container.y + 4, container.width - 4, container.height - 4 },
  75. 20.0f, 2.0f, wordWrap, GRAY);
  76. DrawRectangleRec(resizer, borderColor); // Draw the resize box
  77. // Draw bottom info
  78. DrawRectangle(0, screenHeight - 54, screenWidth, 54, GRAY);
  79. DrawRectangleRec((Rectangle){ 382, screenHeight - 34, 12, 12 }, MAROON);
  80. DrawText("Word Wrap: ", 313, screenHeight-115, 20, BLACK);
  81. if (wordWrap) DrawText("ON", 447, screenHeight - 115, 20, RED);
  82. else DrawText("OFF", 447, screenHeight - 115, 20, BLACK);
  83. DrawText("Press [SPACE] to toggle word wrap", 218, screenHeight - 86, 20, GRAY);
  84. DrawText("Click hold & drag the to resize the container", 155, screenHeight - 38, 20, RAYWHITE);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }