From f4d243ea885cd354834ab54ef8cd560dfc9736a1 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 May 2019 18:21:10 +0200 Subject: [PATCH] Added extended GuiTextBox() example --- examples/text_box_selection/gui_text_box.c | 309 +++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 examples/text_box_selection/gui_text_box.c diff --git a/examples/text_box_selection/gui_text_box.c b/examples/text_box_selection/gui_text_box.c new file mode 100644 index 0000000..4eded64 --- /dev/null +++ b/examples/text_box_selection/gui_text_box.c @@ -0,0 +1,309 @@ +/******************************************************************************************* +* +* raygui - Controls test +* +* TEST CONTROLS: +* - GuiScrollPanel() +* +* DEPENDENCIES: +* raylib 2.5 - Windowing/input management and drawing. +* raygui 2.0 - Immediate-mode GUI controls. +* +* COMPILATION (Windows - MinGW): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99 +* +* COMPILATION (Linux - gcc): +* gcc -o $(NAME_PART) $(FILE_NAME) -I../../src -lraylib -std=c99 +* +* LICENSE: zlib/libpng +* +* Copyright (c) 2019 Vlad Adrian (@Demizdor) and Ramon Santamaria (@raysan5) +* +**********************************************************************************************/ + +#include "raylib.h" + +#define RAYGUI_IMPLEMENTATION +#define RAYGUI_RICONS_SUPPORT +#define RAYGUI_TEXTBOX_EXTENDED +#include "../src/raygui.h" + +#include +#include + +// ----------------- +// DEFINES +// ----------------- +#define SIZEOF(A) (sizeof(A)/sizeof(A[0])) // Get number of elements in array `A`. Total size of `A` should be known at compile time. +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 450 +#define TEXTBOX_MAX_HEIGHT 55 + +// ----------------- +// GLOBAL VARIABLES +// ----------------- +char text0[92] = "Lorem ipsum dolor sit amet, \xE7\x8C\xBF\xE3\x82\x82\xE6\x9C\xA8\xE3\x81\x8B\xE3\x82\x89\xE8\x90\xBD\xE3\x81\xA1\xE3\x82\x8B consectetur adipiscing elit"; // including some hiragana/kanji +char text1[128] = "Here's another, much bigger textbox." "\xf4\xa1\xa1\xff" " TIP: try COPY/PASTE ;)"; // including some invalid UTF8 +int spinnerValue = 0, boxValue = 0; +int textboxActive = 0; // Keeps traks of the active textbox + +struct { + Rectangle bounds; + int maxWidth; +} textbox[] = { // Params for each of the 4 textboxes + { .bounds = (Rectangle){20,60,160,25}, .maxWidth = 160 }, + { .bounds = (Rectangle){220,60,300,25}, .maxWidth = 300 }, + { .bounds = (Rectangle){565,60,95,25}, .maxWidth = 95 }, + { .bounds = (Rectangle){680,60,100,25}, .maxWidth = 100 } +}; + + +int fontSize = 10, fontSpacing = 1, padding = 0, border = 0; +Font font = {0}; +Color colorBG = {0}, colorFG = {0}, *colorSelected = NULL; + +bool showMenu = false; +Rectangle menuRect = {0}; +Texture2D pattern = {0}; + + + +// ----------------- +// FUNCTIONS +// ----------------- + +// Draw a color button, if clicked return true. +bool ColorButton(Rectangle bounds, Color color) +{ + Rectangle body = {bounds.x + 2, bounds.y + 2, bounds.width - 4, bounds.height - 4}; + bool pressed = false; + + // Update control + Vector2 mouse = GetMousePosition(); + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, bounds)) pressed = true; + + // Draw control + DrawTexture(pattern, body.x, body.y, WHITE); + DrawRectangleRec(body, color); + DrawRectangleLinesEx(bounds, 1, BLACK); + + return pressed; +} + +// Handles GUI logic +void UpdateGUI() +{ + // Check all of the 4 textboxes to get the active textbox + for(int i=0; ia; + GuiSetStyle(SLIDER, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT); // Slider for the selected color alpha value + colorSelected->a = GuiSlider((Rectangle){664,420,100,20}, GuiIconText(RICON_CROP_ALPHA, "Alpha"), alpha, 0.f, 255.f, true); +} + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(int argc, char **argv) +{ + // Initialization + //--------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raygui - GuiTextBoxEx()"); + + // Generate a checked pattern used by the color buttons + Image timg = GenImageChecked(26, 26, 5, 5, RAYWHITE, DARKGRAY); + pattern = LoadTextureFromImage(timg); + UnloadImage(timg); + + // Load initial style + GuiLoadStyleDefault(); + //font = LoadFont("/home/adrian/Descărcări/raylib/examples/text/resources/notoCJK.fnt"); + //GuiFont(font); + + fontSize = GuiGetStyle(DEFAULT, TEXT_SIZE); + fontSpacing = GuiGetStyle(DEFAULT, TEXT_SPACING); + padding = GuiGetStyle(TEXTBOX, INNER_PADDING); + border = GuiGetStyle(TEXTBOX, BORDER_WIDTH); + colorFG = GetColor(GuiGetStyle(TEXTBOX, COLOR_SELECTED_FG)); + colorBG = GetColor(GuiGetStyle(TEXTBOX, COLOR_SELECTED_BG)); + colorSelected = &colorFG; + + SetTargetFPS(60); + //--------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Implement required update logic + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + UpdateGUI(); + DrawGUI(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file