/******************************************************************************************* * * rGuiStyler - raygui Style Editor * * Compile this program using: * gcc -o $(NAME_PART).exe $(FILE_NAME) external/tinyfiledialogs.c -I..\.. \ * -lraylib -lglfw3 -lopengl32 -lgdi32 -lcomdlg32 -lole32 -std=c99 -Wall * * * LICENSE: zlib/libpng * * Copyright (c) 2014-2016 Sergio Martinez and Ramon Santamaria * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. * * Permission is granted to anyone to use this software for any purpose, including commercial * applications, and to alter it and redistribute it freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not claim that you * wrote the original software. If you use this software in a product, an acknowledgment * in the product documentation would be appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and must not be misrepresented * as being the original software. * * 3. This notice may not be removed or altered from any source distribution. * **********************************************************************************************/ #include "raylib.h" #define RAYGUI_IMPLEMENTATION #define RAYGUI_STYLE_SAVE_LOAD #include "raygui.h" #include "external/tinyfiledialogs.h" // Open/Save file dialogs #include #include #include #if defined(_WIN32) #include #define GetCurrentDir _getcwd #else #include #define GetCurrentDir getcwd #endif //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- #define FONT_SIZE 10 #define COLOR_REC GuiLinesColor() #define NUM_COLOR_SAMPLES 10 #define ELEMENT_HEIGHT 38 #define STATUS_BAR_HEIGHT 25 #define NUM_ELEMENTS 11 // NOTE: Be extremely careful when defining: NUM_ELEMENTS, GuiElement, guiElementText, guiPropertyNum, guiPropertyType and guiPropertyPos // All those variables must be coherent, one small mistake breaks the program (and it could take hours to find the error!) //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- typedef enum { LABEL = 0, BUTTON, TOGGLE, TOGGLEGROUP, SLIDER, SLIDERBAR, PROGRESSBAR, SPINNER, COMBOBOX, CHECKBOX, TEXTBOX } GuiElement; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- static char currentPath[256]; // Path to current working folder const char *guiElementText[NUM_ELEMENTS] = { "LABEL", "BUTTON", "TOGGLE", "TOGGLEGROUP", "SLIDER", "SLIDERBAR", "PROGRESSBAR", "SPINNER", "COMBOBOX", "CHECKBOX", "TEXTBOX" }; //---------------------------------------------------------------------------------- // Module Functions Declaration //---------------------------------------------------------------------------------- static void BtnLoadStyle(void); // Button load style function static void BtnSaveStyle(void); // Button save style function //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ int main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 1280; const int screenHeight = 720; const int guiPropertyNum[NUM_ELEMENTS] = { 3, 11, 14, 1, 7, 6, 4, 14, 18, 8, 6 }; // Defines if the property to change is a Color or a value to update it accordingly // NOTE: 0 - Color, 1 - value const unsigned char guiPropertyType[NUM_PROPERTIES] = { 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 }; int aux = 0; int guiPropertyPos[NUM_ELEMENTS]; for (int i = 0; i < NUM_ELEMENTS; i++) { guiPropertyPos[i] = aux; aux += guiPropertyNum[i]; } //SetConfigFlags(FLAG_FULLSCREEN_MODE); InitWindow(screenWidth, screenHeight, "rGuiStyler - raygui style editor"); int dropsCount = 0; char **droppedFiles; Rectangle guiElementRec[NUM_ELEMENTS]; for (int i = 0; i < NUM_ELEMENTS; i++) guiElementRec[i] = (Rectangle){ 0, 0 + i*ELEMENT_HEIGHT, 140, ELEMENT_HEIGHT }; int guiElementSelected = -1; int guiElementHover = -1; // Generate properties rectangles depending on guiPropertyNum[] and guiPropertyPos[] //----------------------------------------------------------------------------------- Rectangle propertyRec[NUM_PROPERTIES]; for (int j = 0; j < NUM_ELEMENTS; j++) { for (int i = 0; i < guiPropertyNum[j]; i++) { if ((j + guiPropertyNum[j]) > 18) propertyRec[guiPropertyPos[j] + i] = (Rectangle){ guiElementRec[0].width, guiElementRec[18 - guiPropertyNum[j]].y + i*ELEMENT_HEIGHT, 260, ELEMENT_HEIGHT }; else propertyRec[guiPropertyPos[j] + i] = (Rectangle){ guiElementRec[0].width, guiElementRec[j].y + i*ELEMENT_HEIGHT, 260, ELEMENT_HEIGHT }; } } int guiPropertySelected = -1; int guiPropertyHover = -1; //------------------------------------------------------------ // GUI //----------------------------------------------------------- int guiPosX = 455; int guiPosY = 35; int guiHeight = 30; int guiWidth = 150; int deltaY = 60; int selectPosX = 401; //int selectPosY = 0; int selectWidth = screenWidth - 723; //int selectHeight = screenHeight; //------------------------------------------------------------ // Color picker //----------------------------------------------------------- Vector2 colorPickerPos = { (float)screenWidth - 287, 20.0f }; Vector2 cursorPickerPos = colorPickerPos; Color colorPickerValue = RED; Color colorSample[NUM_COLOR_SAMPLES]; for (int i = 0; i < NUM_COLOR_SAMPLES; i++) colorSample[i] = RAYWHITE; int rgbWidthLabel = 30; int rgbHeightLabel = 20; int rgbDelta = 6; int redValue = 0; int greenValue = 0; int blueValue = 0; int alphaValue = 255; // Color samples Rectangle sampleBoundsRec[NUM_COLOR_SAMPLES]; int sampleHover = -1; int sampleSelected = 0; for (int i = 0; i < NUM_COLOR_SAMPLES/2; i++) sampleBoundsRec[i] = (Rectangle) {colorPickerPos.x + 2*rgbWidthLabel + i*rgbWidthLabel + 3*rgbDelta + i*rgbDelta, colorPickerPos.y + 300 + 2*rgbDelta, rgbWidthLabel, rgbWidthLabel - 2}; for (int i = NUM_COLOR_SAMPLES/2; i < NUM_COLOR_SAMPLES; i++) sampleBoundsRec[i] = (Rectangle) {colorPickerPos.x + 2*rgbWidthLabel + (i-5)*rgbWidthLabel + 3*rgbDelta + (i-5)*rgbDelta, colorPickerPos.y + 300 + 2*rgbDelta + rgbWidthLabel + 2, rgbWidthLabel, rgbWidthLabel - 2}; //------------------------------------------------------------ // Value size selection //----------------------------------------------------------- int sizeValueSelected = 10; //----------------------------------------------------------- Color bgColor = RAYWHITE; bool toggle = false; bool toggleValue = false; char *toggleGuiText[3] = { "toggle", "group", "selection" }; float sliderValue = 50.0f; float sliderBarValue = 50.0f; float progressValue = 0.0f; bool checked = false; int spinnerValue = 20; int comboNum = 5; char *comboText[5] = { "this", "is", "a" ,"combo", "box" }; int comboActive = 0; char guiText[16] = "\0"; bool isModified = false; //----------------------------------------------------------- // Get current directory // NOTE: Current working directory could not match current executable directory GetCurrentDir(currentPath, sizeof(currentPath)); currentPath[strlen(currentPath)] = '\\'; currentPath[strlen(currentPath) + 1] = '\0'; // Not really required LoadGuiStyleImage("rguistyle_default_dark.png"); SetTargetFPS(60); //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsFileDropped()) { droppedFiles = GetDroppedFiles(&dropsCount); LoadGuiStyle(droppedFiles[0]); ClearDroppedFiles(); } // NOTE: We must verify that guiElementSelected and guiPropertySelected are valid // Check gui element selected for (int i = 0; i < NUM_ELEMENTS; i++) { if (CheckCollisionPointRec(GetMousePosition(), guiElementRec[i])) { guiElementSelected = i; guiElementHover = i; guiPropertySelected = -1; guiPropertyHover = -1; break; } else guiElementHover = -1; } // Check gui property selected if (guiElementSelected != -1) { for (int i = guiPropertyPos[guiElementSelected]; i < guiPropertyPos[guiElementSelected] + guiPropertyNum[guiElementSelected]; i++) { if (CheckCollisionPointRec(GetMousePosition(), propertyRec[i])) { guiPropertyHover = i; // Show current value in color picker or spinner if (guiPropertySelected == -1) { if (guiPropertyType[guiPropertyHover] == 0) // Color type { // Update color picker color value colorPickerValue = GetColor(GetStyleProperty(guiPropertyHover)); redValue = colorPickerValue.r; greenValue = colorPickerValue.g; blueValue = colorPickerValue.b; cursorPickerPos = (Vector2){screenWidth, screenHeight}; } // Value Type else if (guiPropertyType[guiPropertyHover] == 1) sizeValueSelected = GetStyleProperty(guiPropertyHover); } // Check if gui property is clicked if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { if (guiPropertySelected == i) guiPropertySelected = -1; else { guiPropertySelected = i; if ((guiPropertyHover > -1) && (guiPropertyType[guiPropertyHover] == 0)) { if (guiPropertySelected > -1) colorPickerValue = GetColor(GetStyleProperty(guiPropertySelected)); redValue = colorPickerValue.r; greenValue = colorPickerValue.g; blueValue = colorPickerValue.b; } else if (guiPropertySelected > -1) sizeValueSelected = GetStyleProperty(guiPropertySelected); } } break; } else guiPropertyHover = -1; } } //if (guiElementSelected == PROGRESSBAR) { if (progressValue > 1.0f) progressValue = 0.0f; progressValue += 0.0005f; } if (guiPropertySelected != -1) { // Update style size value if (guiPropertyType[guiPropertySelected] == 1) { if (GetStyleProperty(guiPropertySelected) != sizeValueSelected) { isModified = true; SetStyleProperty(guiPropertySelected, sizeValueSelected); } } // Color samples /* if (CheckCollisionPointRec(GetMousePosition(), colorSelectedBoundsRec)) { colorSelectedHover = true; if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) colorSample[sampleSelected] = colorPickerValue; } else colorSelectedHover = false; for (int i = 0; i < NUM_COLOR_SAMPLES; i++) { if (CheckCollisionPointRec(GetMousePosition(), sampleBoundsRec[i])) { sampleHover = i; if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { sampleSelected = i; } if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { sampleSelected = i; colorPickerValue = colorSample[sampleSelected]; redValue = colorPickerValue.r; greenValue = colorPickerValue.g; blueValue = colorPickerValue.b; alphaValue = colorPickerValue.a; } } } */ // Update style color value if (guiPropertySelected == DEFAULT_BACKGROUND_COLOR) bgColor = colorPickerValue; else if ((guiPropertySelected >= 0) && (guiPropertyType[guiPropertySelected] == 0)) { bgColor = GetColor(GetStyleProperty(DEFAULT_BACKGROUND_COLOR)); SetStyleProperty(guiPropertySelected, GetHexValue(colorPickerValue)); } } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Show selected properties //if (guiPropertySelected >= 0) DrawText(FormatText("SELECTED PROPERTY: <%s>", guiPropertyName[guiPropertySelected]), 5, screenHeight - STATUS_BAR_HEIGHT + 8, FONT_SIZE , BLACK); //else DrawText("SELECTED PROPERTY: