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.

268 rivejä
12 KiB

11 kuukautta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
5 vuotta sitten
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Rectangle bounds
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 4.0
  6. *
  7. * Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2018-2024 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. static void DrawTextBoxed(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint); // Draw text using font inside rectangle limits
  17. static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, Color selectTint, Color selectBackTint); // Draw text using font inside rectangle limits with support for text selection
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [text] example - draw text inside a rectangle");
  28. const char text[] = "Text cannot escape\tthis container\t...word wrap also works when active so here's \
  29. a long text for testing.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \
  30. tempor incididunt ut labore et dolore magna aliqua. Nec ullamcorper sit amet risus nullam eget felis eget.";
  31. bool resizing = false;
  32. bool wordWrap = true;
  33. Rectangle container = { 25.0f, 25.0f, screenWidth - 50.0f, screenHeight - 250.0f };
  34. Rectangle resizer = { container.x + container.width - 17, container.y + container.height - 17, 14, 14 };
  35. // Minimum width and heigh for the container rectangle
  36. const float minWidth = 60;
  37. const float minHeight = 60;
  38. const float maxWidth = screenWidth - 50.0f;
  39. const float maxHeight = screenHeight - 160.0f;
  40. Vector2 lastMouse = { 0.0f, 0.0f }; // Stores last mouse coordinates
  41. Color borderColor = MAROON; // Container border color
  42. Font font = GetFontDefault(); // Get default system font
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. if (IsKeyPressed(KEY_SPACE)) wordWrap = !wordWrap;
  51. Vector2 mouse = GetMousePosition();
  52. // Check if the mouse is inside the container and toggle border color
  53. if (CheckCollisionPointRec(mouse, container)) borderColor = Fade(MAROON, 0.4f);
  54. else if (!resizing) borderColor = MAROON;
  55. // Container resizing logic
  56. if (resizing)
  57. {
  58. if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) resizing = false;
  59. float width = container.width + (mouse.x - lastMouse.x);
  60. container.width = (width > minWidth)? ((width < maxWidth)? width : maxWidth) : minWidth;
  61. float height = container.height + (mouse.y - lastMouse.y);
  62. container.height = (height > minHeight)? ((height < maxHeight)? height : maxHeight) : minHeight;
  63. }
  64. else
  65. {
  66. // Check if we're resizing
  67. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && CheckCollisionPointRec(mouse, resizer)) resizing = true;
  68. }
  69. // Move resizer rectangle properly
  70. resizer.x = container.x + container.width - 17;
  71. resizer.y = container.y + container.height - 17;
  72. lastMouse = mouse; // Update mouse
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. DrawRectangleLinesEx(container, 3, borderColor); // Draw container border
  79. // Draw text in container (add some padding)
  80. DrawTextBoxed(font, text, (Rectangle){ container.x + 4, container.y + 4, container.width - 4, container.height - 4 }, 20.0f, 2.0f, wordWrap, GRAY);
  81. DrawRectangleRec(resizer, borderColor); // Draw the resize box
  82. // Draw bottom info
  83. DrawRectangle(0, screenHeight - 54, screenWidth, 54, GRAY);
  84. DrawRectangleRec((Rectangle){ 382.0f, screenHeight - 34.0f, 12.0f, 12.0f }, MAROON);
  85. DrawText("Word Wrap: ", 313, screenHeight-115, 20, BLACK);
  86. if (wordWrap) DrawText("ON", 447, screenHeight - 115, 20, RED);
  87. else DrawText("OFF", 447, screenHeight - 115, 20, BLACK);
  88. DrawText("Press [SPACE] to toggle word wrap", 218, screenHeight - 86, 20, GRAY);
  89. DrawText("Click hold & drag the to resize the container", 155, screenHeight - 38, 20, RAYWHITE);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. //--------------------------------------------------------------------------------------
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }
  99. //--------------------------------------------------------------------------------------
  100. // Module functions definition
  101. //--------------------------------------------------------------------------------------
  102. // Draw text using font inside rectangle limits
  103. static void DrawTextBoxed(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint)
  104. {
  105. DrawTextBoxedSelectable(font, text, rec, fontSize, spacing, wordWrap, tint, 0, 0, WHITE, WHITE);
  106. }
  107. // Draw text using font inside rectangle limits with support for text selection
  108. static void DrawTextBoxedSelectable(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, Color selectTint, Color selectBackTint)
  109. {
  110. int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
  111. float textOffsetY = 0; // Offset between lines (on line break '\n')
  112. float textOffsetX = 0.0f; // Offset X to next character to draw
  113. float scaleFactor = fontSize/(float)font.baseSize; // Character rectangle scaling factor
  114. // Word/character wrapping mechanism variables
  115. enum { MEASURE_STATE = 0, DRAW_STATE = 1 };
  116. int state = wordWrap? MEASURE_STATE : DRAW_STATE;
  117. int startLine = -1; // Index where to begin drawing (where a line begins)
  118. int endLine = -1; // Index where to stop drawing (where a line ends)
  119. int lastk = -1; // Holds last value of the character position
  120. for (int i = 0, k = 0; i < length; i++, k++)
  121. {
  122. // Get next codepoint from byte string and glyph index in font
  123. int codepointByteCount = 0;
  124. int codepoint = GetCodepoint(&text[i], &codepointByteCount);
  125. int index = GetGlyphIndex(font, codepoint);
  126. // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
  127. // but we need to draw all of the bad bytes using the '?' symbol moving one byte
  128. if (codepoint == 0x3f) codepointByteCount = 1;
  129. i += (codepointByteCount - 1);
  130. float glyphWidth = 0;
  131. if (codepoint != '\n')
  132. {
  133. glyphWidth = (font.glyphs[index].advanceX == 0) ? font.recs[index].width*scaleFactor : font.glyphs[index].advanceX*scaleFactor;
  134. if (i + 1 < length) glyphWidth = glyphWidth + spacing;
  135. }
  136. // NOTE: When wordWrap is ON we first measure how much of the text we can draw before going outside of the rec container
  137. // We store this info in startLine and endLine, then we change states, draw the text between those two variables
  138. // and change states again and again recursively until the end of the text (or until we get outside of the container).
  139. // When wordWrap is OFF we don't need the measure state so we go to the drawing state immediately
  140. // and begin drawing on the next line before we can get outside the container.
  141. if (state == MEASURE_STATE)
  142. {
  143. // TODO: There are multiple types of spaces in UNICODE, maybe it's a good idea to add support for more
  144. // Ref: http://jkorpela.fi/chars/spaces.html
  145. if ((codepoint == ' ') || (codepoint == '\t') || (codepoint == '\n')) endLine = i;
  146. if ((textOffsetX + glyphWidth) > rec.width)
  147. {
  148. endLine = (endLine < 1)? i : endLine;
  149. if (i == endLine) endLine -= codepointByteCount;
  150. if ((startLine + codepointByteCount) == endLine) endLine = (i - codepointByteCount);
  151. state = !state;
  152. }
  153. else if ((i + 1) == length)
  154. {
  155. endLine = i;
  156. state = !state;
  157. }
  158. else if (codepoint == '\n') state = !state;
  159. if (state == DRAW_STATE)
  160. {
  161. textOffsetX = 0;
  162. i = startLine;
  163. glyphWidth = 0;
  164. // Save character position when we switch states
  165. int tmp = lastk;
  166. lastk = k - 1;
  167. k = tmp;
  168. }
  169. }
  170. else
  171. {
  172. if (codepoint == '\n')
  173. {
  174. if (!wordWrap)
  175. {
  176. textOffsetY += (font.baseSize + font.baseSize/2)*scaleFactor;
  177. textOffsetX = 0;
  178. }
  179. }
  180. else
  181. {
  182. if (!wordWrap && ((textOffsetX + glyphWidth) > rec.width))
  183. {
  184. textOffsetY += (font.baseSize + font.baseSize/2)*scaleFactor;
  185. textOffsetX = 0;
  186. }
  187. // When text overflows rectangle height limit, just stop drawing
  188. if ((textOffsetY + font.baseSize*scaleFactor) > rec.height) break;
  189. // Draw selection background
  190. bool isGlyphSelected = false;
  191. if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength)))
  192. {
  193. DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint);
  194. isGlyphSelected = true;
  195. }
  196. // Draw current character glyph
  197. if ((codepoint != ' ') && (codepoint != '\t'))
  198. {
  199. DrawTextCodepoint(font, codepoint, (Vector2){ rec.x + textOffsetX, rec.y + textOffsetY }, fontSize, isGlyphSelected? selectTint : tint);
  200. }
  201. }
  202. if (wordWrap && (i == endLine))
  203. {
  204. textOffsetY += (font.baseSize + font.baseSize/2)*scaleFactor;
  205. textOffsetX = 0;
  206. startLine = endLine;
  207. endLine = -1;
  208. glyphWidth = 0;
  209. selectStart += lastk - k;
  210. k = lastk;
  211. state = !state;
  212. }
  213. }
  214. if ((textOffsetX != 0) || (codepoint != ' ')) textOffsetX += glyphWidth; // avoid leading spaces
  215. }
  216. }