raylib/examples/text/text_rectangle_bounds.c

122 lines
5.1 KiB
C
Raw Normal View History

2018-12-29 16:44:28 +03:00
/*******************************************************************************************
*
* raylib [text] example - Draw text inside a rectangle
*
* This example has been created using raylib 2.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
2018-12-29 16:44:28 +03:00
*
********************************************************************************************/
#include "raylib.h"
2019-05-20 17:36:42 +03:00
int main(void)
2018-12-29 16:44:28 +03:00
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
const int screenWidth = 800;
const int screenHeight = 450;
2018-12-29 16:44:28 +03:00
InitWindow(screenWidth, screenHeight, "raylib [text] example - draw text inside a rectangle");
2019-05-20 17:36:42 +03:00
const char text[] = "Text cannot escape\tthis container\t...word wrap also works when active so here's \
a long text for testing.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \
tempor incididunt ut labore et dolore magna aliqua. Nec ullamcorper sit amet risus nullam eget felis eget.";
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
bool resizing = false;
bool wordWrap = true;
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
Rectangle container = { 25, 25, screenWidth - 50, screenHeight - 250};
Rectangle resizer = { container.x + container.width - 17, container.y + container.height - 17, 14, 14 };
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
// Minimum width and heigh for the container rectangle
const float minWidth = 60;
const float minHeight = 60;
const float maxWidth = screenWidth - 50;
const float maxHeight = screenHeight - 160;
2019-05-20 17:36:42 +03:00
2019-05-27 01:18:15 +03:00
Vector2 lastMouse = { 0.0f, 0.0f }; // Stores last mouse coordinates
Color borderColor = MAROON; // Container border color
Font font = GetFontDefault(); // Get default system font
2019-05-20 17:36:42 +03:00
2019-05-27 01:18:15 +03:00
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
2018-12-29 16:44:28 +03:00
//--------------------------------------------------------------------------------------
// Main game loop
2019-05-27 01:18:15 +03:00
while (!WindowShouldClose()) // Detect window close button or ESC key
2018-12-29 16:44:28 +03:00
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE)) wordWrap = !wordWrap;
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
Vector2 mouse = GetMousePosition();
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
// Check if the mouse is inside the container and toggle border color
if (CheckCollisionPointRec(mouse, container)) borderColor = Fade(MAROON, 0.4f);
else if (!resizing) borderColor = MAROON;
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
// Container resizing logic
2019-05-20 17:36:42 +03:00
if (resizing)
2018-12-29 16:44:28 +03:00
{
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) resizing = false;
2019-05-20 17:36:42 +03:00
float width = container.width + (mouse.x - lastMouse.x);
2018-12-29 16:44:28 +03:00
container.width = (width > minWidth)? ((width < maxWidth)? width : maxWidth) : minWidth;
2019-05-20 17:36:42 +03:00
float height = container.height + (mouse.y - lastMouse.y);
2018-12-29 16:44:28 +03:00
container.height = (height > minHeight)? ((height < maxHeight)? height : maxHeight) : minHeight;
2019-05-20 17:36:42 +03:00
}
else
2018-12-29 16:44:28 +03:00
{
// Check if we're resizing
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, resizer)) resizing = true;
}
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
// Move resizer rectangle properly
resizer.x = container.x + container.width - 17;
resizer.y = container.y + container.height - 17;
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
lastMouse = mouse; // Update mouse
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangleLinesEx(container, 3, borderColor); // Draw container border
2019-05-20 17:36:42 +03:00
2018-12-29 16:44:28 +03:00
// Draw text in container (add some padding)
2019-05-20 17:36:42 +03:00
DrawTextRec(font, text,
(Rectangle){ container.x + 4, container.y + 4, container.width - 4, container.height - 4 },
2018-12-29 16:44:28 +03:00
20.0f, 2.0f, wordWrap, GRAY);
DrawRectangleRec(resizer, borderColor); // Draw the resize box
// Draw bottom info
DrawRectangle(0, screenHeight - 54, screenWidth, 54, GRAY);
DrawRectangleRec((Rectangle){ 382, screenHeight - 34, 12, 12 }, MAROON);
2018-12-29 16:44:28 +03:00
DrawText("Word Wrap: ", 313, screenHeight-115, 20, BLACK);
if (wordWrap) DrawText("ON", 447, screenHeight - 115, 20, RED);
else DrawText("OFF", 447, screenHeight - 115, 20, BLACK);
DrawText("Press [SPACE] to toggle word wrap", 218, screenHeight - 86, 20, GRAY);
2018-12-29 16:44:28 +03:00
DrawText("Click hold & drag the to resize the container", 155, screenHeight - 38, 20, RAYWHITE);
2018-12-29 16:44:28 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
2019-05-20 17:36:42 +03:00
//--------------------------------------------------------------------------------------
2018-12-29 16:44:28 +03:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}