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