Support raygui as standalone library

This commit is contained in:
Ray 2016-06-02 01:24:27 +02:00
parent 0a27525a4b
commit 7afa0b09ab
2 changed files with 62 additions and 57 deletions

View file

@ -5,13 +5,6 @@
* Initial design by Kevin Gato and Daniel Nicolás * Initial design by Kevin Gato and Daniel Nicolás
* Reviewed by Albert Martos, Ian Eito, Sergio Martinez and Ramon Santamaria (@raysan5) * Reviewed by Albert Martos, Ian Eito, Sergio Martinez and Ramon Santamaria (@raysan5)
* *
* The following functions from raylib are required for drawing and input reading:
GetColor(), GetHexValue() --> Used on SetStyleProperty()
MeasureText(), GetDefaultFont()
DrawRectangleRec(), DrawRectangle(), DrawText(), DrawLine()
GetMousePosition(), (), IsMouseButtonDown(), IsMouseButtonReleased()
'FormatText(), IsKeyDown(), 'IsMouseButtonUp()
*
* This software is provided "as-is", without any express or implied warranty. In no event * 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. * will the authors be held liable for any damages arising from the use of this software.
* *
@ -29,25 +22,30 @@
* *
**********************************************************************************************/ **********************************************************************************************/
#define RAYGUI_STANDALONE // NOTE: To use the raygui module as standalone lib, just uncomment this line //#define RAYGUI_STANDALONE // To use the raygui module as standalone lib, just uncomment this line
// NOTE: Some external funtions are required for drawing and input management // NOTE: Some external funtions are required for drawing and input management
#if defined(RAYGUI_STANDALONE) #if !defined(RAYGUI_STANDALONE)
#include "raygui.h"
#else
#include "raylib.h" #include "raylib.h"
#endif #endif
#include "raygui.h"
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fprintf(), feof(), fscanf() #include <stdio.h> // Required for: FILE, fopen(), fclose(), fprintf(), feof(), fscanf()
// NOTE: Those functions are only used in SaveGuiStyle() and LoadGuiStyle() // NOTE: Those functions are only used in SaveGuiStyle() and LoadGuiStyle()
#include <stdlib.h> // Required for: malloc(), free() #include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strcmp() #include <string.h> // Required for: strcmp()
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
//... #if defined(RAYGUI_STANDALONE)
#define KEY_LEFT 263
#define KEY_RIGHT 262
#define MOUSE_LEFT_BUTTON 0
#endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
@ -65,7 +63,7 @@ typedef enum { SLIDER_DEFAULT, SLIDER_HOVER, SLIDER_ACTIVE } SliderState;
// Global Variables Definition // Global Variables Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
//Current GUI style (default light) // Current GUI style (default light)
static int style[NUM_PROPERTIES] = { static int style[NUM_PROPERTIES] = {
0xf5f5f5ff, // GLOBAL_BASE_COLOR, 0xf5f5f5ff, // GLOBAL_BASE_COLOR,
0xf5f5f5ff, // GLOBAL_BORDER_COLOR, 0xf5f5f5ff, // GLOBAL_BORDER_COLOR,
@ -176,15 +174,24 @@ static Color ColorMultiply(Color baseColor, float value);
static Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value static Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
static int GetHexValue(Color color); // Returns hexadecimal value for a Color static int GetHexValue(Color color); // Returns hexadecimal value for a Color
static bool CheckCollisionPointRec(Vector2 point, Rectangle rec); // Check if point is inside rectangle static bool CheckCollisionPointRec(Vector2 point, Rectangle rec); // Check if point is inside rectangle
static const char *FormatText(const char *text, ...); // Formatting of text with variables to 'embed'
// NOTE: raygui depend on some raylib input and drawing functions // NOTE: raygui depend on some raylib input and drawing functions
// TODO: Set your own input functions (used in ProcessCamera()) // TODO: Set your own functions
static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; } static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
static int IsMouseButtonDown(int button) { return 0; } static int IsMouseButtonDown(int button) { return 0; }
static int IsMouseButtonPressed(int button) { return 0; } static int IsMouseButtonPressed(int button) { return 0; }
static int IsMouseButtonReleased(int button) { return 0; } static int IsMouseButtonReleased(int button) { return 0; }
static int IsMouseButtonUp(int button) { return 0; } static int IsMouseButtonUp(int button) { return 0; }
static int IsKeyDown(int key) { return 0; }
static int GetKeyPressed(void) { return 0; } // NOTE: Only used by GuiTextBox()
static int IsKeyDown(int key) { return 0; } // NOTE: Only used by GuiSpinner()
static int MeasureText(const char *text, int fontSize) { return 0; }
static void DrawText(const char *text, int posX, int posY, int fontSize, Color color) { }
static void DrawRectangleRec(Rectangle rec, Color color) { }
static void DrawRectangle(int posX, int posY, int width, int height, Color color) { DrawRectangleRec((Rectangle){ posX, posY, width, height }, color); }
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -194,6 +201,8 @@ static int IsKeyDown(int key) { return 0; }
// Label element, show text // Label element, show text
void GuiLabel(Rectangle bounds, const char *text) void GuiLabel(Rectangle bounds, const char *text)
{ {
#define BLANK (Color){ 0, 0, 0, 0 } // Blank (Transparent)
GuiLabelEx(bounds, text, GetColor(style[LABEL_TEXT_COLOR]), BLANK, BLANK); GuiLabelEx(bounds, text, GetColor(style[LABEL_TEXT_COLOR]), BLANK, BLANK);
} }
@ -203,7 +212,7 @@ void GuiLabelEx(Rectangle bounds, const char *text, Color textColor, Color borde
// Update control // Update control
//-------------------------------------------------------------------- //--------------------------------------------------------------------
int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]); int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = GetDefaultFont().size; int textHeight = style[GLOBAL_TEXT_FONTSIZE];
if (bounds.width < textWidth) bounds.width = textWidth + style[LABEL_TEXT_PADDING]; if (bounds.width < textWidth) bounds.width = textWidth + style[LABEL_TEXT_PADDING];
if (bounds.height < textHeight) bounds.height = textHeight + style[LABEL_TEXT_PADDING]/2; if (bounds.height < textHeight) bounds.height = textHeight + style[LABEL_TEXT_PADDING]/2;
@ -224,7 +233,7 @@ bool GuiButton(Rectangle bounds, const char *text)
Vector2 mousePoint = GetMousePosition(); Vector2 mousePoint = GetMousePosition();
int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]); int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = GetDefaultFont().size; int textHeight = style[GLOBAL_TEXT_FONTSIZE];
// Update control // Update control
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -282,7 +291,7 @@ bool GuiToggleButton(Rectangle bounds, const char *text, bool toggle)
Vector2 mousePoint = GetMousePosition(); Vector2 mousePoint = GetMousePosition();
int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]); int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = GetDefaultFont().size; int textHeight = style[GLOBAL_TEXT_FONTSIZE];
// Update control // Update control
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -367,7 +376,7 @@ int GuiComboBox(Rectangle bounds, int comboNum, char **comboText, int comboActiv
Rectangle click = { bounds.x + bounds.width + style[COMBOBOX_PADDING], bounds.y, style[COMBOBOX_BUTTON_WIDTH], style[COMBOBOX_BUTTON_HEIGHT] }; Rectangle click = { bounds.x + bounds.width + style[COMBOBOX_PADDING], bounds.y, style[COMBOBOX_BUTTON_WIDTH], style[COMBOBOX_BUTTON_HEIGHT] };
Vector2 mousePoint = GetMousePosition(); Vector2 mousePoint = GetMousePosition();
int textHeight = GetDefaultFont().size; int textHeight = style[GLOBAL_TEXT_FONTSIZE];
for (int i = 0; i < comboNum; i++) for (int i = 0; i < comboNum; i++)
{ {
@ -657,9 +666,8 @@ int GuiSpinner(Rectangle bounds, int value, int minValue, int maxValue)
Rectangle rightButtonBound = { bounds.x + bounds.width - bounds.width/4 + 1, bounds.y, bounds.width/4, bounds.height }; Rectangle rightButtonBound = { bounds.x + bounds.width - bounds.width/4 + 1, bounds.y, bounds.width/4, bounds.height };
Vector2 mousePoint = GetMousePosition(); Vector2 mousePoint = GetMousePosition();
int textHeight = GetDefaultFont().size;
int textWidth = MeasureText(FormatText("%i", value), style[GLOBAL_TEXT_FONTSIZE]); int textWidth = MeasureText(FormatText("%i", value), style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = style[GLOBAL_TEXT_FONTSIZE];
int buttonSide = 0; int buttonSide = 0;
@ -894,10 +902,11 @@ char *GuiTextBox(Rectangle bounds, char *text)
DrawText(FormatText("%c", text[i]), initPos, bounds.y + style[TEXTBOX_TEXT_FONTSIZE], style[TEXTBOX_TEXT_FONTSIZE], GetColor(style[TEXTBOX_TEXT_COLOR])); DrawText(FormatText("%c", text[i]), initPos, bounds.y + style[TEXTBOX_TEXT_FONTSIZE], style[TEXTBOX_TEXT_FONTSIZE], GetColor(style[TEXTBOX_TEXT_COLOR]));
initPos += ((GetDefaultFont().charRecs[(int)text[i] - 32].width + 2)); initPos += (MeasureText(FormatText("%c", text[i]), style[GLOBAL_TEXT_FONTSIZE]) + 2);
//initPos += ((GetDefaultFont().charRecs[(int)text[i] - 32].width + 2));
} }
if ((framesCounter/20)%2 && CheckCollisionPointRec(mousePoint, bounds)) DrawLine(initPos + 2, bounds.y + 5, initPos + 2, bounds.y + 10 + 15, GetColor(style[TEXTBOX_LINE_COLOR])); if ((framesCounter/20)%2 && CheckCollisionPointRec(mousePoint, bounds)) DrawRectangle(initPos + 2, bounds.y + 5, 1, 20, GetColor(style[TEXTBOX_LINE_COLOR]));
//-------------------------------------------------------------------- //--------------------------------------------------------------------
return text; return text;
@ -1116,6 +1125,8 @@ static bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
// Formatting of text with variables to 'embed' // Formatting of text with variables to 'embed'
static const char *FormatText(const char *text, ...) static const char *FormatText(const char *text, ...)
{ {
#define MAX_FORMATTEXT_LENGTH 64
static char buffer[MAX_FORMATTEXT_LENGTH]; static char buffer[MAX_FORMATTEXT_LENGTH];
va_list args; va_list args;

View file

@ -30,47 +30,41 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#define NUM_PROPERTIES 98 #define NUM_PROPERTIES 98
#define BLANK (Color){ 0, 0, 0, 0 } // Blank (Transparent)
#define KEY_LEFT 263
#define KEY_RIGHT 262
#define MOUSE_LEFT_BUTTON 0
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
// NOTE: Some types are required for RAYGUI_STANDALONE usage // NOTE: Some types are required for RAYGUI_STANDALONE usage
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#ifndef __cplusplus #if defined(RAYGUI_STANDALONE)
// Boolean type #ifndef __cplusplus
#ifndef true // Boolean type
typedef enum { false, true } bool; #ifndef true
typedef enum { false, true } bool;
#endif
#endif #endif
// Vector2 type
typedef struct Vector2 {
float x;
float y;
} Vector2;
// Color type, RGBA (32bit)
typedef struct Color {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} Color;
// Rectangle type
typedef struct Rectangle {
int x;
int y;
int width;
int height;
} Rectangle;
#endif #endif
// Vector2 type
typedef struct Vector2 {
float x;
float y;
} Vector2;
// Color type, RGBA (32bit)
typedef struct Color {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} Color;
// Rectangle type
typedef struct Rectangle {
int x;
int y;
int width;
int height;
} Rectangle;
// Gui properties enumeration // Gui properties enumeration
typedef enum GuiProperty { typedef enum GuiProperty {
GLOBAL_BASE_COLOR = 0, GLOBAL_BASE_COLOR = 0,