瀏覽代碼

Support raygui as standalone library

pull/124/head
Ray 8 年之前
父節點
當前提交
7afa0b09ab
共有 2 個檔案被更改,包括 60 行新增55 行删除
  1. +34
    -23
      src/raygui.c
  2. +26
    -32
      src/raygui.h

+ 34
- 23
src/raygui.c 查看文件

@ -5,13 +5,6 @@
* Initial design by Kevin Gato and Daniel Nicolás
* 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
* will the authors be held liable for any damages arising from the use of this software.
*
@ -29,25 +22,30 @@
*
**********************************************************************************************/
cp">#define RAYGUI_STANDALONE // NOTE: To use the raygui module as standalone lib, just uncomment this line
o">//#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
#if defined(RAYGUI_STANDALONE)
#include "raygui.h"
#else
#if !defined(RAYGUI_STANDALONE)
#include "raylib.h"
#endif
#include "raygui.h"
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fprintf(), feof(), fscanf()
// NOTE: Those functions are only used in SaveGuiStyle() and LoadGuiStyle()
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strcmp()
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
//----------------------------------------------------------------------------------
// 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
@ -65,7 +63,7 @@ typedef enum { SLIDER_DEFAULT, SLIDER_HOVER, SLIDER_ACTIVE } SliderState;
// Global Variables Definition
//----------------------------------------------------------------------------------
//Current GUI style (default light)
// Current GUI style (default light)
static int style[NUM_PROPERTIES] = {
0xf5f5f5ff, // GLOBAL_BASE_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 int GetHexValue(Color color); // Returns hexadecimal value for a Color
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
// TODO: Set your own input functions (used in ProcessCamera())
// TODO: Set your own functions
static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
static int IsMouseButtonDown(int button) { return 0; }
static int IsMouseButtonPressed(int button) { return 0; }
static int IsMouseButtonReleased(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
//----------------------------------------------------------------------------------
@ -194,6 +201,8 @@ static int IsKeyDown(int key) { return 0; }
// Label element, show 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);
}
@ -203,7 +212,7 @@ void GuiLabelEx(Rectangle bounds, const char *text, Color textColor, Color borde
// Update control
//--------------------------------------------------------------------
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.height < textHeight) bounds.height = textHeight + style[LABEL_TEXT_PADDING]/2;
@ -224,7 +233,7 @@ bool GuiButton(Rectangle bounds, const char *text)
Vector2 mousePoint = GetMousePosition();
int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = GetDefaultFont().size;
int textHeight = style[GLOBAL_TEXT_FONTSIZE];
// Update control
//--------------------------------------------------------------------
@ -282,7 +291,7 @@ bool GuiToggleButton(Rectangle bounds, const char *text, bool toggle)
Vector2 mousePoint = GetMousePosition();
int textWidth = MeasureText(text, style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = GetDefaultFont().size;
int textHeight = style[GLOBAL_TEXT_FONTSIZE];
// 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] };
Vector2 mousePoint = GetMousePosition();
int textHeight = GetDefaultFont().size;
int textHeight = style[GLOBAL_TEXT_FONTSIZE];
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 };
Vector2 mousePoint = GetMousePosition();
int textHeight = GetDefaultFont().size;
int textWidth = MeasureText(FormatText("%i", value), style[GLOBAL_TEXT_FONTSIZE]);
int textHeight = style[GLOBAL_TEXT_FONTSIZE];
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]));
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, n">initPos + 2, bounds.y + 10 + 15, GetColor(style[TEXTBOX_LINE_COLOR]));
if ((framesCounter/20)%2 && CheckCollisionPointRec(mousePoint, bounds)) DrawRectangle(initPos + 2, bounds.y + 5, mi">1, 20, GetColor(style[TEXTBOX_LINE_COLOR]));
//--------------------------------------------------------------------
return text;
@ -1116,6 +1125,8 @@ static bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
// Formatting of text with variables to 'embed'
static const char *FormatText(const char *text, ...)
{
#define MAX_FORMATTEXT_LENGTH 64
static char buffer[MAX_FORMATTEXT_LENGTH];
va_list args;

+ 26
- 32
src/raygui.h 查看文件

@ -30,46 +30,40 @@
//----------------------------------------------------------------------------------
#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
// NOTE: Some types are required for RAYGUI_STANDALONE usage
//----------------------------------------------------------------------------------
#ifndef __cplusplus
// Boolean type
#ifndef true
typedef enum { false, true } bool;
#if defined(RAYGUI_STANDALONE)
#ifndef __cplusplus
// Boolean type
#ifndef true
typedef enum { false, true } bool;
#endif
#endif
#endif
// Vector2 type
typedef struct Vector2 {
float x;
float y;
} Vector2;
// 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;
// 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;
// Rectangle type
typedef struct Rectangle {
int x;
int y;
int width;
int height;
} Rectangle;
#endif
// Gui properties enumeration
typedef enum GuiProperty {

Loading…
取消
儲存