You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3718 lines
159 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*******************************************************************************************
  2. *
  3. * raygui v2.8 - A simple and easy-to-use immediate-mode gui library
  4. *
  5. * DESCRIPTION:
  6. *
  7. * raygui is a tools-dev-focused immediate-mode-gui library based on raylib but also
  8. * available as a standalone library, as long as input and drawing functions are provided.
  9. *
  10. * Controls provided:
  11. *
  12. * # Container/separators Controls
  13. * - WindowBox
  14. * - GroupBox
  15. * - Line
  16. * - Panel
  17. *
  18. * # Basic Controls
  19. * - Label
  20. * - Button
  21. * - LabelButton --> Label
  22. * - ImageButton --> Button
  23. * - ImageButtonEx --> Button
  24. * - Toggle
  25. * - ToggleGroup --> Toggle
  26. * - CheckBox
  27. * - ComboBox
  28. * - DropdownBox
  29. * - TextBox
  30. * - TextBoxMulti
  31. * - ValueBox --> TextBox
  32. * - Spinner --> Button, ValueBox
  33. * - Slider
  34. * - SliderBar --> Slider
  35. * - ProgressBar
  36. * - StatusBar
  37. * - ScrollBar
  38. * - ScrollPanel
  39. * - DummyRec
  40. * - Grid
  41. *
  42. * # Advance Controls
  43. * - ListView
  44. * - ColorPicker --> ColorPanel, ColorBarHue
  45. * - MessageBox --> Window, Label, Button
  46. * - TextInputBox --> Window, Label, TextBox, Button
  47. *
  48. * It also provides a set of functions for styling the controls based on its properties (size, color).
  49. *
  50. * CONFIGURATION:
  51. *
  52. * #define RAYGUI_IMPLEMENTATION
  53. * Generates the implementation of the library into the included file.
  54. * If not defined, the library is in header only mode and can be included in other headers
  55. * or source files without problems. But only ONE file should hold the implementation.
  56. *
  57. * #define RAYGUI_STATIC (defined by default)
  58. * The generated implementation will stay private inside implementation file and all
  59. * internal symbols and functions will only be visible inside that file.
  60. *
  61. * #define RAYGUI_STANDALONE
  62. * Avoid raylib.h header inclusion in this file. Data types defined on raylib are defined
  63. * internally in the library and input management and drawing functions must be provided by
  64. * the user (check library implementation for further details).
  65. *
  66. * #define RAYGUI_SUPPORT_ICONS
  67. * Includes riconsdata.h header defining a set of 128 icons (binary format) to be used on
  68. * multiple controls and following raygui styles
  69. *
  70. *
  71. * VERSIONS HISTORY:
  72. * 2.8 (03-May-2020) Centralized rectangles drawing to GuiDrawRectangle()
  73. * 2.7 (20-Feb-2020) Added possible tooltips API
  74. * 2.6 (09-Sep-2019) ADDED: GuiTextInputBox()
  75. * REDESIGNED: GuiListView*(), GuiDropdownBox(), GuiSlider*(), GuiProgressBar(), GuiMessageBox()
  76. * REVIEWED: GuiTextBox(), GuiSpinner(), GuiValueBox(), GuiLoadStyle()
  77. * Replaced property INNER_PADDING by TEXT_PADDING, renamed some properties
  78. * Added 8 new custom styles ready to use
  79. * Multiple minor tweaks and bugs corrected
  80. * 2.5 (28-May-2019) Implemented extended GuiTextBox(), GuiValueBox(), GuiSpinner()
  81. * 2.3 (29-Apr-2019) Added rIcons auxiliar library and support for it, multiple controls reviewed
  82. * Refactor all controls drawing mechanism to use control state
  83. * 2.2 (05-Feb-2019) Added GuiScrollBar(), GuiScrollPanel(), reviewed GuiListView(), removed Gui*Ex() controls
  84. * 2.1 (26-Dec-2018) Redesign of GuiCheckBox(), GuiComboBox(), GuiDropdownBox(), GuiToggleGroup() > Use combined text string
  85. * Complete redesign of style system (breaking change)
  86. * 2.0 (08-Nov-2018) Support controls guiLock and custom fonts, reviewed GuiComboBox(), GuiListView()...
  87. * 1.9 (09-Oct-2018) Controls review: GuiGrid(), GuiTextBox(), GuiTextBoxMulti(), GuiValueBox()...
  88. * 1.8 (01-May-2018) Lot of rework and redesign to align with rGuiStyler and rGuiLayout
  89. * 1.5 (21-Jun-2017) Working in an improved styles system
  90. * 1.4 (15-Jun-2017) Rewritten all GUI functions (removed useless ones)
  91. * 1.3 (12-Jun-2017) Redesigned styles system
  92. * 1.1 (01-Jun-2017) Complete review of the library
  93. * 1.0 (07-Jun-2016) Converted to header-only by Ramon Santamaria.
  94. * 0.9 (07-Mar-2016) Reviewed and tested by Albert Martos, Ian Eito, Sergio Martinez and Ramon Santamaria.
  95. * 0.8 (27-Aug-2015) Initial release. Implemented by Kevin Gato, Daniel Nicolás and Ramon Santamaria.
  96. *
  97. * CONTRIBUTORS:
  98. * Ramon Santamaria: Supervision, review, redesign, update and maintenance...
  99. * Vlad Adrian: Complete rewrite of GuiTextBox() to support extended features (2019)
  100. * Sergio Martinez: Review, testing (2015) and redesign of multiple controls (2018)
  101. * Adria Arranz: Testing and Implementation of additional controls (2018)
  102. * Jordi Jorba: Testing and Implementation of additional controls (2018)
  103. * Albert Martos: Review and testing of the library (2015)
  104. * Ian Eito: Review and testing of the library (2015)
  105. * Kevin Gato: Initial implementation of basic components (2014)
  106. * Daniel Nicolas: Initial implementation of basic components (2014)
  107. *
  108. *
  109. * LICENSE: zlib/libpng
  110. *
  111. * Copyright (c) 2014-2020 Ramon Santamaria (@raysan5)
  112. *
  113. * This software is provided "as-is", without any express or implied warranty. In no event
  114. * will the authors be held liable for any damages arising from the use of this software.
  115. *
  116. * Permission is granted to anyone to use this software for any purpose, including commercial
  117. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  118. *
  119. * 1. The origin of this software must not be misrepresented; you must not claim that you
  120. * wrote the original software. If you use this software in a product, an acknowledgment
  121. * in the product documentation would be appreciated but is not required.
  122. *
  123. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  124. * as being the original software.
  125. *
  126. * 3. This notice may not be removed or altered from any source distribution.
  127. *
  128. **********************************************************************************************/
  129. #ifndef RAYGUI_H
  130. #define RAYGUI_H
  131. #define RAYGUI_VERSION "2.6-dev"
  132. #if !defined(RAYGUI_STANDALONE)
  133. #include "raylib.h"
  134. #endif
  135. // Define functions scope to be used internally (static) or externally (extern) to the module including this file
  136. #if defined(RAYGUI_STATIC)
  137. #define RAYGUIDEF static // Functions just visible to module including this file
  138. #else
  139. #ifdef __cplusplus
  140. #define RAYGUIDEF extern "C" // Functions visible from other files (no name mangling of functions in C++)
  141. #else
  142. // NOTE: By default any function declared in a C file is extern
  143. #define RAYGUIDEF extern // Functions visible from other files
  144. #endif
  145. #endif
  146. #if defined(_WIN32)
  147. #if defined(BUILD_LIBTYPE_SHARED)
  148. #define RAYGUIDEF __declspec(dllexport) // We are building raygui as a Win32 shared library (.dll).
  149. #elif defined(USE_LIBTYPE_SHARED)
  150. #define RAYGUIDEF __declspec(dllimport) // We are using raygui as a Win32 shared library (.dll)
  151. #endif
  152. #endif
  153. #if !defined(RAYGUI_MALLOC) && !defined(RAYGUI_CALLOC) && !defined(RAYGUI_FREE)
  154. #include <stdlib.h> // Required for: malloc(), calloc(), free()
  155. #endif
  156. // Allow custom memory allocators
  157. #ifndef RAYGUI_MALLOC
  158. #define RAYGUI_MALLOC(sz) malloc(sz)
  159. #endif
  160. #ifndef RAYGUI_CALLOC
  161. #define RAYGUI_CALLOC(n,sz) calloc(n,sz)
  162. #endif
  163. #ifndef RAYGUI_FREE
  164. #define RAYGUI_FREE(p) free(p)
  165. #endif
  166. //----------------------------------------------------------------------------------
  167. // Defines and Macros
  168. //----------------------------------------------------------------------------------
  169. #define NUM_CONTROLS 16 // Number of standard controls
  170. #define NUM_PROPS_DEFAULT 16 // Number of standard properties
  171. #define NUM_PROPS_EXTENDED 8 // Number of extended properties
  172. #define TEXTEDIT_CURSOR_BLINK_FRAMES 20 // Text edit controls cursor blink timming
  173. //----------------------------------------------------------------------------------
  174. // Types and Structures Definition
  175. // NOTE: Some types are required for RAYGUI_STANDALONE usage
  176. //----------------------------------------------------------------------------------
  177. #if defined(RAYGUI_STANDALONE)
  178. #ifndef __cplusplus
  179. // Boolean type
  180. #ifndef true
  181. typedef enum { false, true } bool;
  182. #endif
  183. #endif
  184. // Vector2 type
  185. typedef struct Vector2 {
  186. float x;
  187. float y;
  188. } Vector2;
  189. // Vector3 type
  190. typedef struct Vector3 {
  191. float x;
  192. float y;
  193. float z;
  194. } Vector3;
  195. // Color type, RGBA (32bit)
  196. typedef struct Color {
  197. unsigned char r;
  198. unsigned char g;
  199. unsigned char b;
  200. unsigned char a;
  201. } Color;
  202. // Rectangle type
  203. typedef struct Rectangle {
  204. float x;
  205. float y;
  206. float width;
  207. float height;
  208. } Rectangle;
  209. // TODO: Texture2D type is very coupled to raylib, mostly required by GuiImageButton()
  210. // It should be redesigned to be provided by user
  211. typedef struct Texture2D {
  212. unsigned int id; // OpenGL texture id
  213. int width; // Texture base width
  214. int height; // Texture base height
  215. int mipmaps; // Mipmap levels, 1 by default
  216. int format; // Data format (PixelFormat type)
  217. } Texture2D;
  218. // Font character info
  219. typedef struct CharInfo CharInfo;
  220. // TODO: Font type is very coupled to raylib, mostly required by GuiLoadStyle()
  221. // It should be redesigned to be provided by user
  222. typedef struct Font {
  223. int baseSize; // Base size (default chars height)
  224. int charsCount; // Number of characters
  225. Texture2D texture; // Characters texture atlas
  226. Rectangle *recs; // Characters rectangles in texture
  227. CharInfo *chars; // Characters info data
  228. } Font;
  229. #endif
  230. // Style property
  231. typedef struct GuiStyleProp {
  232. unsigned short controlId;
  233. unsigned short propertyId;
  234. int propertyValue;
  235. } GuiStyleProp;
  236. // Gui control state
  237. typedef enum {
  238. GUI_STATE_NORMAL = 0,
  239. GUI_STATE_FOCUSED,
  240. GUI_STATE_PRESSED,
  241. GUI_STATE_DISABLED,
  242. } GuiControlState;
  243. // Gui control text alignment
  244. typedef enum {
  245. GUI_TEXT_ALIGN_LEFT = 0,
  246. GUI_TEXT_ALIGN_CENTER,
  247. GUI_TEXT_ALIGN_RIGHT,
  248. } GuiTextAlignment;
  249. // Gui controls
  250. typedef enum {
  251. DEFAULT = 0,
  252. LABEL, // LABELBUTTON
  253. BUTTON, // IMAGEBUTTON
  254. TOGGLE, // TOGGLEGROUP
  255. SLIDER, // SLIDERBAR
  256. PROGRESSBAR,
  257. CHECKBOX,
  258. COMBOBOX,
  259. DROPDOWNBOX,
  260. TEXTBOX, // TEXTBOXMULTI
  261. VALUEBOX,
  262. SPINNER,
  263. LISTVIEW,
  264. COLORPICKER,
  265. SCROLLBAR,
  266. STATUSBAR
  267. } GuiControl;
  268. // Gui base properties for every control
  269. typedef enum {
  270. BORDER_COLOR_NORMAL = 0,
  271. BASE_COLOR_NORMAL,
  272. TEXT_COLOR_NORMAL,
  273. BORDER_COLOR_FOCUSED,
  274. BASE_COLOR_FOCUSED,
  275. TEXT_COLOR_FOCUSED,
  276. BORDER_COLOR_PRESSED,
  277. BASE_COLOR_PRESSED,
  278. TEXT_COLOR_PRESSED,
  279. BORDER_COLOR_DISABLED,
  280. BASE_COLOR_DISABLED,
  281. TEXT_COLOR_DISABLED,
  282. BORDER_WIDTH,
  283. TEXT_PADDING,
  284. TEXT_ALIGNMENT,
  285. RESERVED
  286. } GuiControlProperty;
  287. // Gui extended properties depend on control
  288. // NOTE: We reserve a fixed size of additional properties per control
  289. // DEFAULT properties
  290. typedef enum {
  291. TEXT_SIZE = 16,
  292. TEXT_SPACING,
  293. LINE_COLOR,
  294. BACKGROUND_COLOR,
  295. } GuiDefaultProperty;
  296. // Label
  297. //typedef enum { } GuiLabelProperty;
  298. // Button
  299. //typedef enum { } GuiButtonProperty;
  300. // Toggle / ToggleGroup
  301. typedef enum {
  302. GROUP_PADDING = 16,
  303. } GuiToggleProperty;
  304. // Slider / SliderBar
  305. typedef enum {
  306. SLIDER_WIDTH = 16,
  307. SLIDER_PADDING
  308. } GuiSliderProperty;
  309. // ProgressBar
  310. typedef enum {
  311. PROGRESS_PADDING = 16,
  312. } GuiProgressBarProperty;
  313. // CheckBox
  314. typedef enum {
  315. CHECK_PADDING = 16
  316. } GuiCheckBoxProperty;
  317. // ComboBox
  318. typedef enum {
  319. COMBO_BUTTON_WIDTH = 16,
  320. COMBO_BUTTON_PADDING
  321. } GuiComboBoxProperty;
  322. // DropdownBox
  323. typedef enum {
  324. ARROW_PADDING = 16,
  325. DROPDOWN_ITEMS_PADDING
  326. } GuiDropdownBoxProperty;
  327. // TextBox / TextBoxMulti / ValueBox / Spinner
  328. typedef enum {
  329. TEXT_INNER_PADDING = 16,
  330. TEXT_LINES_PADDING,
  331. COLOR_SELECTED_FG,
  332. COLOR_SELECTED_BG
  333. } GuiTextBoxProperty;
  334. // Spinner
  335. typedef enum {
  336. SPIN_BUTTON_WIDTH = 16,
  337. SPIN_BUTTON_PADDING,
  338. } GuiSpinnerProperty;
  339. // ScrollBar
  340. typedef enum {
  341. ARROWS_SIZE = 16,
  342. ARROWS_VISIBLE,
  343. SCROLL_SLIDER_PADDING,
  344. SCROLL_SLIDER_SIZE,
  345. SCROLL_PADDING,
  346. SCROLL_SPEED,
  347. } GuiScrollBarProperty;
  348. // ScrollBar side
  349. typedef enum {
  350. SCROLLBAR_LEFT_SIDE = 0,
  351. SCROLLBAR_RIGHT_SIDE
  352. } GuiScrollBarSide;
  353. // ListView
  354. typedef enum {
  355. LIST_ITEMS_HEIGHT = 16,
  356. LIST_ITEMS_PADDING,
  357. SCROLLBAR_WIDTH,
  358. SCROLLBAR_SIDE,
  359. } GuiListViewProperty;
  360. // ColorPicker
  361. typedef enum {
  362. COLOR_SELECTOR_SIZE = 16,
  363. HUEBAR_WIDTH, // Right hue bar width
  364. HUEBAR_PADDING, // Right hue bar separation from panel
  365. HUEBAR_SELECTOR_HEIGHT, // Right hue bar selector height
  366. HUEBAR_SELECTOR_OVERFLOW // Right hue bar selector overflow
  367. } GuiColorPickerProperty;
  368. //----------------------------------------------------------------------------------
  369. // Global Variables Definition
  370. //----------------------------------------------------------------------------------
  371. // ...
  372. //----------------------------------------------------------------------------------
  373. // Module Functions Declaration
  374. //----------------------------------------------------------------------------------
  375. // State modification functions
  376. RAYGUIDEF void GuiEnable(void); // Enable gui controls (global state)
  377. RAYGUIDEF void GuiDisable(void); // Disable gui controls (global state)
  378. RAYGUIDEF void GuiLock(void); // Lock gui controls (global state)
  379. RAYGUIDEF void GuiUnlock(void); // Unlock gui controls (global state)
  380. RAYGUIDEF void GuiFade(float alpha); // Set gui controls alpha (global state), alpha goes from 0.0f to 1.0f
  381. RAYGUIDEF void GuiSetState(int state); // Set gui state (global state)
  382. RAYGUIDEF int GuiGetState(void); // Get gui state (global state)
  383. // Font set/get functions
  384. RAYGUIDEF void GuiSetFont(Font font); // Set gui custom font (global state)
  385. RAYGUIDEF Font GuiGetFont(void); // Get gui custom font (global state)
  386. // Style set/get functions
  387. RAYGUIDEF void GuiSetStyle(int control, int property, int value); // Set one style property
  388. RAYGUIDEF int GuiGetStyle(int control, int property); // Get one style property
  389. // Tooltips set functions
  390. RAYGUIDEF void GuiEnableTooltip(void); // Enable gui tooltips
  391. RAYGUIDEF void GuiDisableTooltip(void); // Disable gui tooltips
  392. RAYGUIDEF void GuiSetTooltip(const char *tooltip); // Set current tooltip for display
  393. RAYGUIDEF void GuiClearTooltip(void); // Clear any tooltip registered
  394. // Container/separator controls, useful for controls organization
  395. RAYGUIDEF bool GuiWindowBox(Rectangle bounds, const char *title); // Window Box control, shows a window that can be closed
  396. RAYGUIDEF void GuiGroupBox(Rectangle bounds, const char *text); // Group Box control with text name
  397. RAYGUIDEF void GuiLine(Rectangle bounds, const char *text); // Line separator control, could contain text
  398. RAYGUIDEF void GuiPanel(Rectangle bounds); // Panel control, useful to group controls
  399. RAYGUIDEF Rectangle GuiScrollPanel(Rectangle bounds, Rectangle content, Vector2 *scroll); // Scroll Panel control
  400. // Basic controls set
  401. RAYGUIDEF void GuiLabel(Rectangle bounds, const char *text); // Label control, shows text
  402. RAYGUIDEF bool GuiButton(Rectangle bounds, const char *text); // Button control, returns true when clicked
  403. RAYGUIDEF bool GuiLabelButton(Rectangle bounds, const char *text); // Label button control, show true when clicked
  404. RAYGUIDEF bool GuiImageButton(Rectangle bounds, const char *text, Texture2D texture); // Image button control, returns true when clicked
  405. RAYGUIDEF bool GuiImageButtonEx(Rectangle bounds, const char *text, Texture2D texture, Rectangle texSource); // Image button extended control, returns true when clicked
  406. RAYGUIDEF bool GuiToggle(Rectangle bounds, const char *text, bool active); // Toggle Button control, returns true when active
  407. RAYGUIDEF int GuiToggleGroup(Rectangle bounds, const char *text, int active); // Toggle Group control, returns active toggle index
  408. RAYGUIDEF bool GuiCheckBox(Rectangle bounds, const char *text, bool checked); // Check Box control, returns true when active
  409. RAYGUIDEF int GuiComboBox(Rectangle bounds, const char *text, int active); // Combo Box control, returns selected item index
  410. RAYGUIDEF bool GuiDropdownBox(Rectangle bounds, const char *text, int *active, bool editMode); // Dropdown Box control, returns selected item
  411. RAYGUIDEF bool GuiSpinner(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Spinner control, returns selected value
  412. RAYGUIDEF bool GuiValueBox(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Value Box control, updates input text with numbers
  413. RAYGUIDEF bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode); // Text Box control, updates input text
  414. RAYGUIDEF bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool editMode); // Text Box control with multiple lines
  415. RAYGUIDEF float GuiSlider(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue); // Slider control, returns selected value
  416. RAYGUIDEF float GuiSliderBar(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue); // Slider Bar control, returns selected value
  417. RAYGUIDEF float GuiProgressBar(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue); // Progress Bar control, shows current progress value
  418. RAYGUIDEF void GuiStatusBar(Rectangle bounds, const char *text); // Status Bar control, shows info text
  419. RAYGUIDEF void GuiDummyRec(Rectangle bounds, const char *text); // Dummy control for placeholders
  420. RAYGUIDEF int GuiScrollBar(Rectangle bounds, int value, int minValue, int maxValue); // Scroll Bar control
  421. RAYGUIDEF Vector2 GuiGrid(Rectangle bounds, float spacing, int subdivs); // Grid control
  422. // Advance controls set
  423. RAYGUIDEF int GuiListView(Rectangle bounds, const char *text, int *scrollIndex, int active); // List View control, returns selected list item index
  424. RAYGUIDEF int GuiListViewEx(Rectangle bounds, const char **text, int count, int *focus, int *scrollIndex, int active); // List View with extended parameters
  425. RAYGUIDEF int GuiMessageBox(Rectangle bounds, const char *title, const char *message, const char *buttons); // Message Box control, displays a message
  426. RAYGUIDEF int GuiTextInputBox(Rectangle bounds, const char *title, const char *message, const char *buttons, char *text); // Text Input Box control, ask for text
  427. RAYGUIDEF Color GuiColorPicker(Rectangle bounds, Color color); // Color Picker control (multiple color controls)
  428. RAYGUIDEF Color GuiColorPanel(Rectangle bounds, Color color); // Color Panel control
  429. RAYGUIDEF float GuiColorBarAlpha(Rectangle bounds, float alpha); // Color Bar Alpha control
  430. RAYGUIDEF float GuiColorBarHue(Rectangle bounds, float value); // Color Bar Hue control
  431. // Styles loading functions
  432. RAYGUIDEF void GuiLoadStyle(const char *fileName); // Load style file (.rgs)
  433. RAYGUIDEF void GuiLoadStyleDefault(void); // Load style default over global style
  434. /*
  435. typedef GuiStyle (unsigned int *)
  436. RAYGUIDEF GuiStyle LoadGuiStyle(const char *fileName); // Load style from file (.rgs)
  437. RAYGUIDEF void UnloadGuiStyle(GuiStyle style); // Unload style
  438. */
  439. RAYGUIDEF const char *GuiIconText(int iconId, const char *text); // Get text with icon id prepended (if supported)
  440. #if defined(RAYGUI_SUPPORT_ICONS)
  441. // Gui icons functionality
  442. RAYGUIDEF void GuiDrawIcon(int iconId, Vector2 position, int pixelSize, Color color);
  443. RAYGUIDEF unsigned int *GuiGetIcons(void); // Get full icons data pointer
  444. RAYGUIDEF unsigned int *GuiGetIconData(int iconId); // Get icon bit data
  445. RAYGUIDEF void GuiSetIconData(int iconId, unsigned int *data); // Set icon bit data
  446. RAYGUIDEF void GuiSetIconPixel(int iconId, int x, int y); // Set icon pixel value
  447. RAYGUIDEF void GuiClearIconPixel(int iconId, int x, int y); // Clear icon pixel value
  448. RAYGUIDEF bool GuiCheckIconPixel(int iconId, int x, int y); // Check icon pixel value
  449. #endif
  450. #endif // RAYGUI_H
  451. /***********************************************************************************
  452. *
  453. * RAYGUI IMPLEMENTATION
  454. *
  455. ************************************************************************************/
  456. #if defined(RAYGUI_IMPLEMENTATION)
  457. #if defined(RAYGUI_SUPPORT_ICONS)
  458. #define RICONS_IMPLEMENTATION
  459. #include "ricons.h" // Required for: raygui icons data
  460. #endif
  461. #include <stdio.h> // Required for: FILE, fopen(), fclose(), fprintf(), feof(), fscanf(), vsprintf()
  462. #include <string.h> // Required for: strlen() on GuiTextBox()
  463. #include <math.h> // Required for: roundf() on GuiColorPicker()
  464. #if defined(RAYGUI_STANDALONE)
  465. #include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
  466. #endif
  467. #ifdef __cplusplus
  468. #define RAYGUI_CLITERAL(name) name
  469. #else
  470. #define RAYGUI_CLITERAL(name) (name)
  471. #endif
  472. //----------------------------------------------------------------------------------
  473. // Defines and Macros
  474. //----------------------------------------------------------------------------------
  475. //...
  476. //----------------------------------------------------------------------------------
  477. // Types and Structures Definition
  478. //----------------------------------------------------------------------------------
  479. // Gui control property style color element
  480. typedef enum { BORDER = 0, BASE, TEXT, OTHER } GuiPropertyElement;
  481. //----------------------------------------------------------------------------------
  482. // Global Variables Definition
  483. //----------------------------------------------------------------------------------
  484. static GuiControlState guiState = GUI_STATE_NORMAL;
  485. static Font guiFont = { 0 }; // Gui current font (WARNING: highly coupled to raylib)
  486. static bool guiLocked = false; // Gui lock state (no inputs processed)
  487. static float guiAlpha = 1.0f; // Gui element transpacency on drawing
  488. // Global gui style array (allocated on data segment by default)
  489. // NOTE: In raygui we manage a single int array with all the possible style properties.
  490. // When a new style is loaded, it loads over the global style... but default gui style
  491. // could always be recovered with GuiLoadStyleDefault()
  492. static unsigned int guiStyle[NUM_CONTROLS*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED)] = { 0 };
  493. static bool guiStyleLoaded = false; // Style loaded flag for lazy style initialization
  494. // Tooltips required variables
  495. static const char *guiTooltip = NULL; // Gui tooltip currently active (user provided)
  496. static bool guiTooltipEnabled = true; // Gui tooltips enabled
  497. //----------------------------------------------------------------------------------
  498. // Standalone Mode Functions Declaration
  499. //
  500. // NOTE: raygui depend on some raylib input and drawing functions
  501. // To use raygui as standalone library, below functions must be defined by the user
  502. //----------------------------------------------------------------------------------
  503. #if defined(RAYGUI_STANDALONE)
  504. #define KEY_RIGHT 262
  505. #define KEY_LEFT 263
  506. #define KEY_DOWN 264
  507. #define KEY_UP 265
  508. #define KEY_BACKSPACE 259
  509. #define KEY_ENTER 257
  510. #define MOUSE_LEFT_BUTTON 0
  511. // Input required functions
  512. //-------------------------------------------------------------------------------
  513. static Vector2 GetMousePosition(void);
  514. static int GetMouseWheelMove(void);
  515. static bool IsMouseButtonDown(int button);
  516. static bool IsMouseButtonPressed(int button);
  517. static bool IsMouseButtonReleased(int button);
  518. static bool IsKeyDown(int key);
  519. static bool IsKeyPressed(int key);
  520. static int GetCharPressed(void); // -- GuiTextBox(), GuiTextBoxMulti(), GuiValueBox()
  521. //-------------------------------------------------------------------------------
  522. // Drawing required functions
  523. //-------------------------------------------------------------------------------
  524. static void DrawRectangle(int x, int y, int width, int height, Color color); // -- GuiDrawRectangle(), GuiDrawIcon()
  525. static void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4); // -- GuiColorPicker()
  526. static void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color); // -- GuiDropdownBox(), GuiScrollBar()
  527. static void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint); // -- GuiImageButtonEx()
  528. static void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint); // -- GuiTextBoxMulti()
  529. //-------------------------------------------------------------------------------
  530. // Text required functions
  531. //-------------------------------------------------------------------------------
  532. static Font GetFontDefault(void); // -- GuiLoadStyleDefault()
  533. static Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // -- GetTextWidth(), GuiTextBoxMulti()
  534. static void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // -- GuiDrawText()
  535. static Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCount); // -- GuiLoadStyle()
  536. static char *LoadText(const char *fileName); // -- GuiLoadStyle()
  537. static const char *GetDirectoryPath(const char *filePath); // -- GuiLoadStyle()
  538. //-------------------------------------------------------------------------------
  539. // raylib functions already implemented in raygui
  540. //-------------------------------------------------------------------------------
  541. static Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
  542. static int ColorToInt(Color color); // Returns hexadecimal value for a Color
  543. static Color Fade(Color color, float alpha); // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
  544. static bool CheckCollisionPointRec(Vector2 point, Rectangle rec); // Check if point is inside rectangle
  545. static const char *TextFormat(const char *text, ...); // Formatting of text with variables to 'embed'
  546. static const char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings
  547. static int TextToInteger(const char *text); // Get integer value from text
  548. static void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2); // Draw rectangle vertical gradient
  549. //-------------------------------------------------------------------------------
  550. #endif // RAYGUI_STANDALONE
  551. //----------------------------------------------------------------------------------
  552. // Module specific Functions Declaration
  553. //----------------------------------------------------------------------------------
  554. static int GetTextWidth(const char *text); // Gui get text width using default font
  555. static Rectangle GetTextBounds(int control, Rectangle bounds); // Get text bounds considering control bounds
  556. static const char *GetTextIcon(const char *text, int *iconId); // Get text icon if provided and move text cursor
  557. static void GuiDrawText(const char *text, Rectangle bounds, int alignment, Color tint); // Gui draw text using default font
  558. static void GuiDrawRectangle(Rectangle rec, int borderWidth, Color borderColor, Color color); // Gui draw rectangle using default raygui style
  559. static void GuiDrawTooltip(Rectangle bounds); // Draw tooltip relatively to bounds
  560. static const char **GuiTextSplit(const char *text, int *count, int *textRow); // Split controls text into multiple strings
  561. static Vector3 ConvertHSVtoRGB(Vector3 hsv); // Convert color data from HSV to RGB
  562. static Vector3 ConvertRGBtoHSV(Vector3 rgb); // Convert color data from RGB to HSV
  563. //----------------------------------------------------------------------------------
  564. // Gui Setup Functions Definition
  565. //----------------------------------------------------------------------------------
  566. // Enable gui global state
  567. void GuiEnable(void) { guiState = GUI_STATE_NORMAL; }
  568. // Disable gui global state
  569. void GuiDisable(void) { guiState = GUI_STATE_DISABLED; }
  570. // Lock gui global state
  571. void GuiLock(void) { guiLocked = true; }
  572. // Unlock gui global state
  573. void GuiUnlock(void) { guiLocked = false; }
  574. // Set gui controls alpha global state
  575. void GuiFade(float alpha)
  576. {
  577. if (alpha < 0.0f) alpha = 0.0f;
  578. else if (alpha > 1.0f) alpha = 1.0f;
  579. guiAlpha = alpha;
  580. }
  581. // Set gui state (global state)
  582. void GuiSetState(int state) { guiState = (GuiControlState)state; }
  583. // Get gui state (global state)
  584. int GuiGetState(void) { return guiState; }
  585. // Set custom gui font
  586. // NOTE: Font loading/unloading is external to raygui
  587. void GuiSetFont(Font font)
  588. {
  589. if (font.texture.id > 0)
  590. {
  591. // NOTE: If we try to setup a font but default style has not been
  592. // lazily loaded before, it will be overwritten, so we need to force
  593. // default style loading first
  594. if (!guiStyleLoaded) GuiLoadStyleDefault();
  595. guiFont = font;
  596. GuiSetStyle(DEFAULT, TEXT_SIZE, font.baseSize);
  597. }
  598. }
  599. // Get custom gui font
  600. Font GuiGetFont(void)
  601. {
  602. return guiFont;
  603. }
  604. // Set control style property value
  605. void GuiSetStyle(int control, int property, int value)
  606. {
  607. if (!guiStyleLoaded) GuiLoadStyleDefault();
  608. guiStyle[control*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property] = value;
  609. // Default properties are propagated to all controls
  610. if ((control == 0) && (property < NUM_PROPS_DEFAULT))
  611. {
  612. for (int i = 1; i < NUM_CONTROLS; i++) guiStyle[i*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property] = value;
  613. }
  614. }
  615. // Get control style property value
  616. int GuiGetStyle(int control, int property)
  617. {
  618. if (!guiStyleLoaded) GuiLoadStyleDefault();
  619. return guiStyle[control*(NUM_PROPS_DEFAULT + NUM_PROPS_EXTENDED) + property];
  620. }
  621. // Enable gui tooltips
  622. void GuiEnableTooltip(void) { guiTooltipEnabled = true; }
  623. // Disable gui tooltips
  624. void GuiDisableTooltip(void) { guiTooltipEnabled = false; }
  625. // Set current tooltip for display
  626. void GuiSetTooltip(const char *tooltip) { guiTooltip = tooltip; }
  627. // Clear any tooltip registered
  628. void GuiClearTooltip(void) { guiTooltip = NULL; }
  629. //----------------------------------------------------------------------------------
  630. // Gui Controls Functions Definition
  631. //----------------------------------------------------------------------------------
  632. // Window Box control
  633. bool GuiWindowBox(Rectangle bounds, const char *title)
  634. {
  635. // NOTE: This define is also used by GuiMessageBox() and GuiTextInputBox()
  636. #define WINDOW_STATUSBAR_HEIGHT 22
  637. //GuiControlState state = guiState;
  638. bool clicked = false;
  639. int statusBarHeight = WINDOW_STATUSBAR_HEIGHT + 2*GuiGetStyle(STATUSBAR, BORDER_WIDTH);
  640. statusBarHeight += (statusBarHeight%2);
  641. Rectangle statusBar = { bounds.x, bounds.y, bounds.width, statusBarHeight };
  642. if (bounds.height < statusBarHeight*2) bounds.height = statusBarHeight*2;
  643. Rectangle windowPanel = { bounds.x, bounds.y + statusBarHeight - 1, bounds.width, bounds.height - statusBarHeight };
  644. Rectangle closeButtonRec = { statusBar.x + statusBar.width - GuiGetStyle(STATUSBAR, BORDER_WIDTH) - 20,
  645. statusBar.y + statusBarHeight/2 - 18/2, 18, 18 };
  646. // Update control
  647. //--------------------------------------------------------------------
  648. // NOTE: Logic is directly managed by button
  649. //--------------------------------------------------------------------
  650. // Draw control
  651. //--------------------------------------------------------------------
  652. GuiStatusBar(statusBar, title); // Draw window header as status bar
  653. GuiPanel(windowPanel); // Draw window base
  654. // Draw window close button
  655. int tempBorderWidth = GuiGetStyle(BUTTON, BORDER_WIDTH);
  656. int tempTextAlignment = GuiGetStyle(BUTTON, TEXT_ALIGNMENT);
  657. GuiSetStyle(BUTTON, BORDER_WIDTH, 1);
  658. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  659. #if defined(RAYGUI_SUPPORT_ICONS)
  660. clicked = GuiButton(closeButtonRec, GuiIconText(RICON_CROSS_SMALL, NULL));
  661. #else
  662. clicked = GuiButton(closeButtonRec, "x");
  663. #endif
  664. GuiSetStyle(BUTTON, BORDER_WIDTH, tempBorderWidth);
  665. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, tempTextAlignment);
  666. //--------------------------------------------------------------------
  667. return clicked;
  668. }
  669. // Group Box control with text name
  670. void GuiGroupBox(Rectangle bounds, const char *text)
  671. {
  672. #define GROUPBOX_LINE_THICK 1
  673. #define GROUPBOX_TEXT_PADDING 10
  674. GuiControlState state = guiState;
  675. // Draw control
  676. //--------------------------------------------------------------------
  677. GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x, bounds.y, GROUPBOX_LINE_THICK, bounds.height }, 0, BLANK, Fade(GetColor(GuiGetStyle(DEFAULT, (state == GUI_STATE_DISABLED)? BORDER_COLOR_DISABLED : LINE_COLOR)), guiAlpha));
  678. GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x, bounds.y + bounds.height - 1, bounds.width, GROUPBOX_LINE_THICK }, 0, BLANK, Fade(GetColor(GuiGetStyle(DEFAULT, (state == GUI_STATE_DISABLED)? BORDER_COLOR_DISABLED : LINE_COLOR)), guiAlpha));
  679. GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x + bounds.width - 1, bounds.y, GROUPBOX_LINE_THICK, bounds.height }, 0, BLANK, Fade(GetColor(GuiGetStyle(DEFAULT, (state == GUI_STATE_DISABLED)? BORDER_COLOR_DISABLED : LINE_COLOR)), guiAlpha));
  680. GuiLine(RAYGUI_CLITERAL(Rectangle){ bounds.x, bounds.y, bounds.width, 1 }, text);
  681. //--------------------------------------------------------------------
  682. }
  683. // Line control
  684. void GuiLine(Rectangle bounds, const char *text)
  685. {
  686. #define LINE_TEXT_PADDING 10
  687. GuiControlState state = guiState;
  688. Color color = Fade(GetColor(GuiGetStyle(DEFAULT, (state == GUI_STATE_DISABLED)? BORDER_COLOR_DISABLED : LINE_COLOR)), guiAlpha);
  689. // Draw control
  690. //--------------------------------------------------------------------
  691. if (text == NULL) GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x, bounds.y + bounds.height/2, bounds.width, 1 }, 0, BLANK, color);
  692. else
  693. {
  694. Rectangle textBounds = { 0 };
  695. textBounds.width = GetTextWidth(text); // TODO: Consider text icon
  696. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  697. textBounds.x = bounds.x + LINE_TEXT_PADDING;
  698. textBounds.y = bounds.y - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  699. // Draw line with embedded text label: "--- text --------------"
  700. GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x, bounds.y, LINE_TEXT_PADDING - 2, 1 }, 0, BLANK, color);
  701. GuiLabel(textBounds, text);
  702. GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x + LINE_TEXT_PADDING + textBounds.width + 4, bounds.y, bounds.width - textBounds.width - LINE_TEXT_PADDING - 4, 1 }, 0, BLANK, color);
  703. }
  704. //--------------------------------------------------------------------
  705. }
  706. // Panel control
  707. void GuiPanel(Rectangle bounds)
  708. {
  709. #define PANEL_BORDER_WIDTH 1
  710. GuiControlState state = guiState;
  711. // Draw control
  712. //--------------------------------------------------------------------
  713. GuiDrawRectangle(bounds, PANEL_BORDER_WIDTH, Fade(GetColor(GuiGetStyle(DEFAULT, (state == GUI_STATE_DISABLED)? BORDER_COLOR_DISABLED: LINE_COLOR)), guiAlpha),
  714. Fade(GetColor(GuiGetStyle(DEFAULT, (state == GUI_STATE_DISABLED)? BASE_COLOR_DISABLED : BACKGROUND_COLOR)), guiAlpha));
  715. //--------------------------------------------------------------------
  716. }
  717. // Scroll Panel control
  718. Rectangle GuiScrollPanel(Rectangle bounds, Rectangle content, Vector2 *scroll)
  719. {
  720. GuiControlState state = guiState;
  721. Vector2 scrollPos = { 0.0f, 0.0f };
  722. if (scroll != NULL) scrollPos = *scroll;
  723. bool hasHorizontalScrollBar = (content.width > bounds.width - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH))? true : false;
  724. bool hasVerticalScrollBar = (content.height > bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH))? true : false;
  725. // Recheck to account for the other scrollbar being visible
  726. if (!hasHorizontalScrollBar) hasHorizontalScrollBar = (hasVerticalScrollBar && (content.width > (bounds.width - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH))))? true : false;
  727. if (!hasVerticalScrollBar) hasVerticalScrollBar = (hasHorizontalScrollBar && (content.height > (bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH))))? true : false;
  728. const int horizontalScrollBarWidth = hasHorizontalScrollBar? GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH) : 0;
  729. const int verticalScrollBarWidth = hasVerticalScrollBar? GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH) : 0;
  730. const Rectangle horizontalScrollBar = { (float)((GuiGetStyle(LISTVIEW, SCROLLBAR_SIDE) == SCROLLBAR_LEFT_SIDE)? (float)bounds.x + verticalScrollBarWidth : (float)bounds.x) + GuiGetStyle(DEFAULT, BORDER_WIDTH), (float)bounds.y + bounds.height - horizontalScrollBarWidth - GuiGetStyle(DEFAULT, BORDER_WIDTH), (float)bounds.width - verticalScrollBarWidth - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH), (float)horizontalScrollBarWidth };
  731. const Rectangle verticalScrollBar = { (float)((GuiGetStyle(LISTVIEW, SCROLLBAR_SIDE) == SCROLLBAR_LEFT_SIDE)? (float)bounds.x + GuiGetStyle(DEFAULT, BORDER_WIDTH) : (float)bounds.x + bounds.width - verticalScrollBarWidth - GuiGetStyle(DEFAULT, BORDER_WIDTH)), (float)bounds.y + GuiGetStyle(DEFAULT, BORDER_WIDTH), (float)verticalScrollBarWidth, (float)bounds.height - horizontalScrollBarWidth - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) };
  732. // Calculate view area (area without the scrollbars)
  733. Rectangle view = (GuiGetStyle(LISTVIEW, SCROLLBAR_SIDE) == SCROLLBAR_LEFT_SIDE)?
  734. RAYGUI_CLITERAL(Rectangle){ bounds.x + verticalScrollBarWidth + GuiGetStyle(DEFAULT, BORDER_WIDTH), bounds.y + GuiGetStyle(DEFAULT, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - verticalScrollBarWidth, bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - horizontalScrollBarWidth } :
  735. RAYGUI_CLITERAL(Rectangle){ bounds.x + GuiGetStyle(DEFAULT, BORDER_WIDTH), bounds.y + GuiGetStyle(DEFAULT, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - verticalScrollBarWidth, bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - horizontalScrollBarWidth };
  736. // Clip view area to the actual content size
  737. if (view.width > content.width) view.width = content.width;
  738. if (view.height > content.height) view.height = content.height;
  739. // TODO: Review!
  740. const int horizontalMin = hasHorizontalScrollBar? ((GuiGetStyle(LISTVIEW, SCROLLBAR_SIDE) == SCROLLBAR_LEFT_SIDE)? -verticalScrollBarWidth : 0) - GuiGetStyle(DEFAULT, BORDER_WIDTH) : ((GuiGetStyle(LISTVIEW, SCROLLBAR_SIDE) == SCROLLBAR_LEFT_SIDE)? -verticalScrollBarWidth : 0) - GuiGetStyle(DEFAULT, BORDER_WIDTH);
  741. const int horizontalMax = hasHorizontalScrollBar? content.width - bounds.width + verticalScrollBarWidth + GuiGetStyle(DEFAULT, BORDER_WIDTH) - ((GuiGetStyle(LISTVIEW, SCROLLBAR_SIDE) == SCROLLBAR_LEFT_SIDE)? verticalScrollBarWidth : 0) : -GuiGetStyle(DEFAULT, BORDER_WIDTH);
  742. const int verticalMin = hasVerticalScrollBar? -GuiGetStyle(DEFAULT, BORDER_WIDTH) : -GuiGetStyle(DEFAULT, BORDER_WIDTH);
  743. const int verticalMax = hasVerticalScrollBar? content.height - bounds.height + horizontalScrollBarWidth + GuiGetStyle(DEFAULT, BORDER_WIDTH) : -GuiGetStyle(DEFAULT, BORDER_WIDTH);
  744. // Update control
  745. //--------------------------------------------------------------------
  746. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  747. {
  748. Vector2 mousePoint = GetMousePosition();
  749. // Check button state
  750. if (CheckCollisionPointRec(mousePoint, bounds))
  751. {
  752. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  753. else state = GUI_STATE_FOCUSED;
  754. if (hasHorizontalScrollBar)
  755. {
  756. if (IsKeyDown(KEY_RIGHT)) scrollPos.x -= GuiGetStyle(SCROLLBAR, SCROLL_SPEED);
  757. if (IsKeyDown(KEY_LEFT)) scrollPos.x += GuiGetStyle(SCROLLBAR, SCROLL_SPEED);
  758. }
  759. if (hasVerticalScrollBar)
  760. {
  761. if (IsKeyDown(KEY_DOWN)) scrollPos.y -= GuiGetStyle(SCROLLBAR, SCROLL_SPEED);
  762. if (IsKeyDown(KEY_UP)) scrollPos.y += GuiGetStyle(SCROLLBAR, SCROLL_SPEED);
  763. }
  764. scrollPos.y += GetMouseWheelMove()*20;
  765. }
  766. }
  767. // Normalize scroll values
  768. if (scrollPos.x > -horizontalMin) scrollPos.x = -horizontalMin;
  769. if (scrollPos.x < -horizontalMax) scrollPos.x = -horizontalMax;
  770. if (scrollPos.y > -verticalMin) scrollPos.y = -verticalMin;
  771. if (scrollPos.y < -verticalMax) scrollPos.y = -verticalMax;
  772. //--------------------------------------------------------------------
  773. // Draw control
  774. //--------------------------------------------------------------------
  775. GuiDrawRectangle(bounds, 0, BLANK, GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR))); // Draw background
  776. // Save size of the scrollbar slider
  777. const int slider = GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE);
  778. // Draw horizontal scrollbar if visible
  779. if (hasHorizontalScrollBar)
  780. {
  781. // Change scrollbar slider size to show the diff in size between the content width and the widget width
  782. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE, ((bounds.width - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - verticalScrollBarWidth)/content.width)*(bounds.width - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - verticalScrollBarWidth));
  783. scrollPos.x = -GuiScrollBar(horizontalScrollBar, -scrollPos.x, horizontalMin, horizontalMax);
  784. }
  785. // Draw vertical scrollbar if visible
  786. if (hasVerticalScrollBar)
  787. {
  788. // Change scrollbar slider size to show the diff in size between the content height and the widget height
  789. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE, ((bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - horizontalScrollBarWidth)/content.height)* (bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH) - horizontalScrollBarWidth));
  790. scrollPos.y = -GuiScrollBar(verticalScrollBar, -scrollPos.y, verticalMin, verticalMax);
  791. }
  792. // Draw detail corner rectangle if both scroll bars are visible
  793. if (hasHorizontalScrollBar && hasVerticalScrollBar)
  794. {
  795. // TODO: Consider scroll bars side
  796. Rectangle corner = { horizontalScrollBar.x + horizontalScrollBar.width + 2, verticalScrollBar.y + verticalScrollBar.height + 2, horizontalScrollBarWidth - 4, verticalScrollBarWidth - 4 };
  797. GuiDrawRectangle(corner, 0, BLANK, Fade(GetColor(GuiGetStyle(LISTVIEW, TEXT + (state*3))), guiAlpha));
  798. }
  799. // Draw scrollbar lines depending on current state
  800. GuiDrawRectangle(bounds, GuiGetStyle(DEFAULT, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(LISTVIEW, (float)BORDER + (state*3))), guiAlpha), BLANK);
  801. // Set scrollbar slider size back to the way it was before
  802. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE, slider);
  803. //--------------------------------------------------------------------
  804. if (scroll != NULL) *scroll = scrollPos;
  805. return view;
  806. }
  807. // Label control
  808. void GuiLabel(Rectangle bounds, const char *text)
  809. {
  810. GuiControlState state = guiState;
  811. // Update control
  812. //--------------------------------------------------------------------
  813. // ...
  814. //--------------------------------------------------------------------
  815. // Draw control
  816. //--------------------------------------------------------------------
  817. GuiDrawText(text, GetTextBounds(LABEL, bounds), GuiGetStyle(LABEL, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(LABEL, (state == GUI_STATE_DISABLED)? TEXT_COLOR_DISABLED : TEXT_COLOR_NORMAL)), guiAlpha));
  818. //--------------------------------------------------------------------
  819. }
  820. // Button control, returns true when clicked
  821. bool GuiButton(Rectangle bounds, const char *text)
  822. {
  823. GuiControlState state = guiState;
  824. bool pressed = false;
  825. // Update control
  826. //--------------------------------------------------------------------
  827. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  828. {
  829. Vector2 mousePoint = GetMousePosition();
  830. // Check button state
  831. if (CheckCollisionPointRec(mousePoint, bounds))
  832. {
  833. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  834. else state = GUI_STATE_FOCUSED;
  835. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) pressed = true;
  836. }
  837. }
  838. //--------------------------------------------------------------------
  839. // Draw control
  840. //--------------------------------------------------------------------
  841. GuiDrawRectangle(bounds, GuiGetStyle(BUTTON, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(BUTTON, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(BUTTON, BASE + (state*3))), guiAlpha));
  842. GuiDrawText(text, GetTextBounds(BUTTON, bounds), GuiGetStyle(BUTTON, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(BUTTON, TEXT + (state*3))), guiAlpha));
  843. //------------------------------------------------------------------
  844. return pressed;
  845. }
  846. // Label button control
  847. bool GuiLabelButton(Rectangle bounds, const char *text)
  848. {
  849. GuiControlState state = guiState;
  850. bool pressed = false;
  851. // NOTE: We force bounds.width to be all text
  852. int textWidth = MeasureTextEx(guiFont, text, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING)).x;
  853. if (bounds.width < textWidth) bounds.width = textWidth;
  854. // Update control
  855. //--------------------------------------------------------------------
  856. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  857. {
  858. Vector2 mousePoint = GetMousePosition();
  859. // Check checkbox state
  860. if (CheckCollisionPointRec(mousePoint, bounds))
  861. {
  862. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  863. else state = GUI_STATE_FOCUSED;
  864. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) pressed = true;
  865. }
  866. }
  867. //--------------------------------------------------------------------
  868. // Draw control
  869. //--------------------------------------------------------------------
  870. GuiDrawText(text, GetTextBounds(LABEL, bounds), GuiGetStyle(LABEL, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(LABEL, TEXT + (state*3))), guiAlpha));
  871. //--------------------------------------------------------------------
  872. return pressed;
  873. }
  874. // Image button control, returns true when clicked
  875. bool GuiImageButton(Rectangle bounds, const char *text, Texture2D texture)
  876. {
  877. return GuiImageButtonEx(bounds, text, texture, RAYGUI_CLITERAL(Rectangle){ 0, 0, (float)texture.width, (float)texture.height });
  878. }
  879. // Image button control, returns true when clicked
  880. bool GuiImageButtonEx(Rectangle bounds, const char *text, Texture2D texture, Rectangle texSource)
  881. {
  882. GuiControlState state = guiState;
  883. bool clicked = false;
  884. // Update control
  885. //--------------------------------------------------------------------
  886. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  887. {
  888. Vector2 mousePoint = GetMousePosition();
  889. // Check button state
  890. if (CheckCollisionPointRec(mousePoint, bounds))
  891. {
  892. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  893. else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) clicked = true;
  894. else state = GUI_STATE_FOCUSED;
  895. }
  896. }
  897. //--------------------------------------------------------------------
  898. // Draw control
  899. //--------------------------------------------------------------------
  900. GuiDrawRectangle(bounds, GuiGetStyle(BUTTON, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(BUTTON, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(BUTTON, BASE + (state*3))), guiAlpha));
  901. if (text != NULL) GuiDrawText(text, GetTextBounds(BUTTON, bounds), GuiGetStyle(BUTTON, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(BUTTON, TEXT + (state*3))), guiAlpha));
  902. if (texture.id > 0) DrawTextureRec(texture, texSource, RAYGUI_CLITERAL(Vector2){ bounds.x + bounds.width/2 - texSource.width/2, bounds.y + bounds.height/2 - texSource.height/2 }, Fade(GetColor(GuiGetStyle(BUTTON, TEXT + (state*3))), guiAlpha));
  903. //------------------------------------------------------------------
  904. return clicked;
  905. }
  906. // Toggle Button control, returns true when active
  907. bool GuiToggle(Rectangle bounds, const char *text, bool active)
  908. {
  909. GuiControlState state = guiState;
  910. // Update control
  911. //--------------------------------------------------------------------
  912. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  913. {
  914. Vector2 mousePoint = GetMousePosition();
  915. // Check toggle button state
  916. if (CheckCollisionPointRec(mousePoint, bounds))
  917. {
  918. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  919. else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
  920. {
  921. state = GUI_STATE_NORMAL;
  922. active = !active;
  923. }
  924. else state = GUI_STATE_FOCUSED;
  925. }
  926. }
  927. //--------------------------------------------------------------------
  928. // Draw control
  929. //--------------------------------------------------------------------
  930. if (state == GUI_STATE_NORMAL)
  931. {
  932. GuiDrawRectangle(bounds, GuiGetStyle(TOGGLE, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TOGGLE, (active? BORDER_COLOR_PRESSED : (BORDER + state*3)))), guiAlpha), Fade(GetColor(GuiGetStyle(TOGGLE, (active? BASE_COLOR_PRESSED : (BASE + state*3)))), guiAlpha));
  933. GuiDrawText(text, GetTextBounds(TOGGLE, bounds), GuiGetStyle(TOGGLE, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(TOGGLE, (active? TEXT_COLOR_PRESSED : (TEXT + state*3)))), guiAlpha));
  934. }
  935. else
  936. {
  937. GuiDrawRectangle(bounds, GuiGetStyle(TOGGLE, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TOGGLE, BORDER + state*3)), guiAlpha), Fade(GetColor(GuiGetStyle(TOGGLE, BASE + state*3)), guiAlpha));
  938. GuiDrawText(text, GetTextBounds(TOGGLE, bounds), GuiGetStyle(TOGGLE, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(TOGGLE, TEXT + state*3)), guiAlpha));
  939. }
  940. //--------------------------------------------------------------------
  941. return active;
  942. }
  943. // Toggle Group control, returns toggled button index
  944. int GuiToggleGroup(Rectangle bounds, const char *text, int active)
  945. {
  946. #if !defined(TOGGLEGROUP_MAX_ELEMENTS)
  947. #define TOGGLEGROUP_MAX_ELEMENTS 32
  948. #endif
  949. float initBoundsX = bounds.x;
  950. // Get substrings items from text (items pointers)
  951. int rows[TOGGLEGROUP_MAX_ELEMENTS] = { 0 };
  952. int itemsCount = 0;
  953. const char **items = GuiTextSplit(text, &itemsCount, rows);
  954. int prevRow = rows[0];
  955. for (int i = 0; i < itemsCount; i++)
  956. {
  957. if (prevRow != rows[i])
  958. {
  959. bounds.x = initBoundsX;
  960. bounds.y += (bounds.height + GuiGetStyle(TOGGLE, GROUP_PADDING));
  961. prevRow = rows[i];
  962. }
  963. if (i == active) GuiToggle(bounds, items[i], true);
  964. else if (GuiToggle(bounds, items[i], false) == true) active = i;
  965. bounds.x += (bounds.width + GuiGetStyle(TOGGLE, GROUP_PADDING));
  966. }
  967. return active;
  968. }
  969. // Check Box control, returns true when active
  970. bool GuiCheckBox(Rectangle bounds, const char *text, bool checked)
  971. {
  972. GuiControlState state = guiState;
  973. Rectangle textBounds = { 0 };
  974. if (text != NULL)
  975. {
  976. textBounds.width = GetTextWidth(text);
  977. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  978. textBounds.x = bounds.x + bounds.width + GuiGetStyle(CHECKBOX, TEXT_PADDING);
  979. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  980. if (GuiGetStyle(CHECKBOX, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_LEFT) textBounds.x = bounds.x - textBounds.width - GuiGetStyle(CHECKBOX, TEXT_PADDING);
  981. }
  982. // Update control
  983. //--------------------------------------------------------------------
  984. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  985. {
  986. Vector2 mousePoint = GetMousePosition();
  987. Rectangle totalBounds = {
  988. (GuiGetStyle(CHECKBOX, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_LEFT)? textBounds.x : bounds.x,
  989. bounds.y,
  990. bounds.width + textBounds.width + GuiGetStyle(CHECKBOX, TEXT_PADDING),
  991. bounds.height,
  992. };
  993. // Check checkbox state
  994. if (CheckCollisionPointRec(mousePoint, totalBounds))
  995. {
  996. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  997. else state = GUI_STATE_FOCUSED;
  998. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) checked = !checked;
  999. }
  1000. }
  1001. //--------------------------------------------------------------------
  1002. // Draw control
  1003. //--------------------------------------------------------------------
  1004. GuiDrawRectangle(bounds, GuiGetStyle(CHECKBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(CHECKBOX, BORDER + (state*3))), guiAlpha), BLANK);
  1005. if (checked)
  1006. {
  1007. Rectangle check = { bounds.x + GuiGetStyle(CHECKBOX, BORDER_WIDTH) + GuiGetStyle(CHECKBOX, CHECK_PADDING),
  1008. bounds.y + GuiGetStyle(CHECKBOX, BORDER_WIDTH) + GuiGetStyle(CHECKBOX, CHECK_PADDING),
  1009. bounds.width - 2*(GuiGetStyle(CHECKBOX, BORDER_WIDTH) + GuiGetStyle(CHECKBOX, CHECK_PADDING)),
  1010. bounds.height - 2*(GuiGetStyle(CHECKBOX, BORDER_WIDTH) + GuiGetStyle(CHECKBOX, CHECK_PADDING)) };
  1011. GuiDrawRectangle(check, 0, BLANK, Fade(GetColor(GuiGetStyle(CHECKBOX, TEXT + state*3)), guiAlpha));
  1012. }
  1013. if (text != NULL) GuiDrawText(text, textBounds, (GuiGetStyle(CHECKBOX, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_RIGHT)? GUI_TEXT_ALIGN_LEFT : GUI_TEXT_ALIGN_RIGHT, Fade(GetColor(GuiGetStyle(LABEL, TEXT + (state*3))), guiAlpha));
  1014. //--------------------------------------------------------------------
  1015. return checked;
  1016. }
  1017. // Combo Box control, returns selected item index
  1018. int GuiComboBox(Rectangle bounds, const char *text, int active)
  1019. {
  1020. GuiControlState state = guiState;
  1021. bounds.width -= (GuiGetStyle(COMBOBOX, COMBO_BUTTON_WIDTH) + GuiGetStyle(COMBOBOX, COMBO_BUTTON_PADDING));
  1022. Rectangle selector = { (float)bounds.x + bounds.width + GuiGetStyle(COMBOBOX, COMBO_BUTTON_PADDING),
  1023. (float)bounds.y, (float)GuiGetStyle(COMBOBOX, COMBO_BUTTON_WIDTH), (float)bounds.height };
  1024. // Get substrings items from text (items pointers, lengths and count)
  1025. int itemsCount = 0;
  1026. const char **items = GuiTextSplit(text, &itemsCount, NULL);
  1027. if (active < 0) active = 0;
  1028. else if (active > itemsCount - 1) active = itemsCount - 1;
  1029. // Update control
  1030. //--------------------------------------------------------------------
  1031. if ((state != GUI_STATE_DISABLED) && !guiLocked && (itemsCount > 1))
  1032. {
  1033. Vector2 mousePoint = GetMousePosition();
  1034. if (CheckCollisionPointRec(mousePoint, bounds) ||
  1035. CheckCollisionPointRec(mousePoint, selector))
  1036. {
  1037. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  1038. {
  1039. active += 1;
  1040. if (active >= itemsCount) active = 0;
  1041. }
  1042. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  1043. else state = GUI_STATE_FOCUSED;
  1044. }
  1045. }
  1046. //--------------------------------------------------------------------
  1047. // Draw control
  1048. //--------------------------------------------------------------------
  1049. // Draw combo box main
  1050. GuiDrawRectangle(bounds, GuiGetStyle(COMBOBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(COMBOBOX, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(COMBOBOX, BASE + (state*3))), guiAlpha));
  1051. GuiDrawText(items[active], GetTextBounds(COMBOBOX, bounds), GuiGetStyle(COMBOBOX, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(COMBOBOX, TEXT + (state*3))), guiAlpha));
  1052. // Draw selector using a custom button
  1053. // NOTE: BORDER_WIDTH and TEXT_ALIGNMENT forced values
  1054. int tempBorderWidth = GuiGetStyle(BUTTON, BORDER_WIDTH);
  1055. int tempTextAlign = GuiGetStyle(BUTTON, TEXT_ALIGNMENT);
  1056. GuiSetStyle(BUTTON, BORDER_WIDTH, 1);
  1057. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  1058. GuiButton(selector, TextFormat("%i/%i", active + 1, itemsCount));
  1059. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, tempTextAlign);
  1060. GuiSetStyle(BUTTON, BORDER_WIDTH, tempBorderWidth);
  1061. //--------------------------------------------------------------------
  1062. return active;
  1063. }
  1064. // Dropdown Box control
  1065. // NOTE: Returns mouse click
  1066. bool GuiDropdownBox(Rectangle bounds, const char *text, int *active, bool editMode)
  1067. {
  1068. GuiControlState state = guiState;
  1069. int itemSelected = *active;
  1070. int itemFocused = -1;
  1071. // Get substrings items from text (items pointers, lengths and count)
  1072. int itemsCount = 0;
  1073. const char **items = GuiTextSplit(text, &itemsCount, NULL);
  1074. Rectangle boundsOpen = bounds;
  1075. boundsOpen.height = (itemsCount + 1)*(bounds.height + GuiGetStyle(DROPDOWNBOX, DROPDOWN_ITEMS_PADDING));
  1076. Rectangle itemBounds = bounds;
  1077. bool pressed = false; // Check mouse button pressed
  1078. // Update control
  1079. //--------------------------------------------------------------------
  1080. if ((state != GUI_STATE_DISABLED) && !guiLocked && (itemsCount > 1))
  1081. {
  1082. Vector2 mousePoint = GetMousePosition();
  1083. if (editMode)
  1084. {
  1085. state = GUI_STATE_PRESSED;
  1086. // Check if mouse has been pressed or released outside limits
  1087. if (!CheckCollisionPointRec(mousePoint, boundsOpen))
  1088. {
  1089. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) pressed = true;
  1090. }
  1091. // Check if already selected item has been pressed again
  1092. if (CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) pressed = true;
  1093. // Check focused and selected item
  1094. for (int i = 0; i < itemsCount; i++)
  1095. {
  1096. // Update item rectangle y position for next item
  1097. itemBounds.y += (bounds.height + GuiGetStyle(DROPDOWNBOX, DROPDOWN_ITEMS_PADDING));
  1098. if (CheckCollisionPointRec(mousePoint, itemBounds))
  1099. {
  1100. itemFocused = i;
  1101. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
  1102. {
  1103. itemSelected = i;
  1104. pressed = true; // Item selected, change to editMode = false
  1105. }
  1106. break;
  1107. }
  1108. }
  1109. itemBounds = bounds;
  1110. }
  1111. else
  1112. {
  1113. if (CheckCollisionPointRec(mousePoint, bounds))
  1114. {
  1115. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  1116. {
  1117. pressed = true;
  1118. state = GUI_STATE_PRESSED;
  1119. }
  1120. else state = GUI_STATE_FOCUSED;
  1121. }
  1122. }
  1123. }
  1124. //--------------------------------------------------------------------
  1125. // Draw control
  1126. //--------------------------------------------------------------------
  1127. if (editMode) GuiPanel(boundsOpen);
  1128. GuiDrawRectangle(bounds, GuiGetStyle(DROPDOWNBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, BORDER + state*3)), guiAlpha), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, BASE + state*3)), guiAlpha));
  1129. GuiDrawText(items[itemSelected], GetTextBounds(DEFAULT, bounds), GuiGetStyle(DROPDOWNBOX, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, TEXT + state*3)), guiAlpha));
  1130. if (editMode)
  1131. {
  1132. // Draw visible items
  1133. for (int i = 0; i < itemsCount; i++)
  1134. {
  1135. // Update item rectangle y position for next item
  1136. itemBounds.y += (bounds.height + GuiGetStyle(DROPDOWNBOX, DROPDOWN_ITEMS_PADDING));
  1137. if (i == itemSelected)
  1138. {
  1139. GuiDrawRectangle(itemBounds, GuiGetStyle(DROPDOWNBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, BORDER_COLOR_PRESSED)), guiAlpha), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, BASE_COLOR_PRESSED)), guiAlpha));
  1140. GuiDrawText(items[i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(DROPDOWNBOX, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, TEXT_COLOR_PRESSED)), guiAlpha));
  1141. }
  1142. else if (i == itemFocused)
  1143. {
  1144. GuiDrawRectangle(itemBounds, GuiGetStyle(DROPDOWNBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, BORDER_COLOR_FOCUSED)), guiAlpha), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, BASE_COLOR_FOCUSED)), guiAlpha));
  1145. GuiDrawText(items[i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(DROPDOWNBOX, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, TEXT_COLOR_FOCUSED)), guiAlpha));
  1146. }
  1147. else GuiDrawText(items[i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(DROPDOWNBOX, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(DROPDOWNBOX, TEXT_COLOR_NORMAL)), guiAlpha));
  1148. }
  1149. }
  1150. // TODO: Avoid this function, use icon instead or 'v'
  1151. DrawTriangle(RAYGUI_CLITERAL(Vector2){ bounds.x + bounds.width - GuiGetStyle(DROPDOWNBOX, ARROW_PADDING), bounds.y + bounds.height/2 - 2 },
  1152. RAYGUI_CLITERAL(Vector2){ bounds.x + bounds.width - GuiGetStyle(DROPDOWNBOX, ARROW_PADDING) + 5, bounds.y + bounds.height/2 - 2 + 5 },
  1153. RAYGUI_CLITERAL(Vector2){ bounds.x + bounds.width - GuiGetStyle(DROPDOWNBOX, ARROW_PADDING) + 10, bounds.y + bounds.height/2 - 2 },
  1154. Fade(GetColor(GuiGetStyle(DROPDOWNBOX, TEXT + (state*3))), guiAlpha));
  1155. //GuiDrawText("v", RAYGUI_CLITERAL(Rectangle){ bounds.x + bounds.width - GuiGetStyle(DROPDOWNBOX, ARROW_PADDING), bounds.y + bounds.height/2 - 2, 10, 10 },
  1156. // GUI_TEXT_ALIGN_CENTER, Fade(GetColor(GuiGetStyle(DROPDOWNBOX, TEXT + (state*3))), guiAlpha));
  1157. //--------------------------------------------------------------------
  1158. *active = itemSelected;
  1159. return pressed;
  1160. }
  1161. // Text Box control, updates input text
  1162. // NOTE 1: Requires static variables: framesCounter
  1163. // NOTE 2: Returns if KEY_ENTER pressed (useful for data validation)
  1164. bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
  1165. {
  1166. static int framesCounter = 0; // Required for blinking cursor
  1167. GuiControlState state = guiState;
  1168. bool pressed = false;
  1169. Rectangle cursor = {
  1170. bounds.x + GuiGetStyle(TEXTBOX, TEXT_PADDING) + GetTextWidth(text) + 2,
  1171. bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE),
  1172. 1,
  1173. GuiGetStyle(DEFAULT, TEXT_SIZE)*2
  1174. };
  1175. // Update control
  1176. //--------------------------------------------------------------------
  1177. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1178. {
  1179. Vector2 mousePoint = GetMousePosition();
  1180. if (editMode)
  1181. {
  1182. state = GUI_STATE_PRESSED;
  1183. framesCounter++;
  1184. int key = GetCharPressed(); // Returns codepoint as Unicode
  1185. int keyCount = strlen(text);
  1186. // Only allow keys in range [32..125]
  1187. if (keyCount < (textSize - 1))
  1188. {
  1189. int maxWidth = (bounds.width - (GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)*2));
  1190. if ((GetTextWidth(text) < (maxWidth - GuiGetStyle(DEFAULT, TEXT_SIZE))) && (key >= 32))
  1191. {
  1192. int byteLength = 0;
  1193. const char *textUtf8 = CodepointToUtf8(key, &byteLength);
  1194. for (int i = 0; i < byteLength; i++)
  1195. {
  1196. text[keyCount] = textUtf8[i];
  1197. keyCount++;
  1198. }
  1199. text[keyCount] = '\0';
  1200. }
  1201. }
  1202. // Delete text
  1203. if (keyCount > 0)
  1204. {
  1205. if (IsKeyPressed(KEY_BACKSPACE))
  1206. {
  1207. keyCount--;
  1208. text[keyCount] = '\0';
  1209. framesCounter = 0;
  1210. if (keyCount < 0) keyCount = 0;
  1211. }
  1212. else if (IsKeyDown(KEY_BACKSPACE))
  1213. {
  1214. if ((framesCounter > TEXTEDIT_CURSOR_BLINK_FRAMES) && (framesCounter%2) == 0) keyCount--;
  1215. text[keyCount] = '\0';
  1216. if (keyCount < 0) keyCount = 0;
  1217. }
  1218. }
  1219. if (IsKeyPressed(KEY_ENTER) || (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) pressed = true;
  1220. // Check text alignment to position cursor properly
  1221. int textAlignment = GuiGetStyle(TEXTBOX, TEXT_ALIGNMENT);
  1222. if (textAlignment == GUI_TEXT_ALIGN_CENTER) cursor.x = bounds.x + GetTextWidth(text)/2 + bounds.width/2 + 1;
  1223. else if (textAlignment == GUI_TEXT_ALIGN_RIGHT) cursor.x = bounds.x + bounds.width - GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING);
  1224. }
  1225. else
  1226. {
  1227. if (CheckCollisionPointRec(mousePoint, bounds))
  1228. {
  1229. state = GUI_STATE_FOCUSED;
  1230. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) pressed = true;
  1231. }
  1232. }
  1233. if (pressed) framesCounter = 0;
  1234. }
  1235. //--------------------------------------------------------------------
  1236. // Draw control
  1237. //--------------------------------------------------------------------
  1238. if (state == GUI_STATE_PRESSED)
  1239. {
  1240. GuiDrawRectangle(bounds, GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(TEXTBOX, BASE_COLOR_PRESSED)), guiAlpha));
  1241. // Draw blinking cursor
  1242. if (editMode && ((framesCounter/20)%2 == 0)) GuiDrawRectangle(cursor, 0, BLANK, Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER_COLOR_PRESSED)), guiAlpha));
  1243. }
  1244. else if (state == GUI_STATE_DISABLED)
  1245. {
  1246. GuiDrawRectangle(bounds, GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(TEXTBOX, BASE_COLOR_DISABLED)), guiAlpha));
  1247. }
  1248. else GuiDrawRectangle(bounds, 1, Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha), BLANK);
  1249. GuiDrawText(text, GetTextBounds(TEXTBOX, bounds), GuiGetStyle(TEXTBOX, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(TEXTBOX, TEXT + (state*3))), guiAlpha));
  1250. //--------------------------------------------------------------------
  1251. return pressed;
  1252. }
  1253. // Spinner control, returns selected value
  1254. bool GuiSpinner(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode)
  1255. {
  1256. GuiControlState state = guiState;
  1257. bool pressed = false;
  1258. int tempValue = *value;
  1259. Rectangle spinner = { bounds.x + GuiGetStyle(SPINNER, SPIN_BUTTON_WIDTH) + GuiGetStyle(SPINNER, SPIN_BUTTON_PADDING), bounds.y,
  1260. bounds.width - 2*(GuiGetStyle(SPINNER, SPIN_BUTTON_WIDTH) + GuiGetStyle(SPINNER, SPIN_BUTTON_PADDING)), bounds.height };
  1261. Rectangle leftButtonBound = { (float)bounds.x, (float)bounds.y, (float)GuiGetStyle(SPINNER, SPIN_BUTTON_WIDTH), (float)bounds.height };
  1262. Rectangle rightButtonBound = { (float)bounds.x + bounds.width - GuiGetStyle(SPINNER, SPIN_BUTTON_WIDTH), (float)bounds.y, (float)GuiGetStyle(SPINNER, SPIN_BUTTON_WIDTH), (float)bounds.height };
  1263. Rectangle textBounds = { 0 };
  1264. if (text != NULL)
  1265. {
  1266. textBounds.width = GetTextWidth(text);
  1267. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  1268. textBounds.x = bounds.x + bounds.width + GuiGetStyle(SPINNER, TEXT_PADDING);
  1269. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  1270. if (GuiGetStyle(SPINNER, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_LEFT) textBounds.x = bounds.x - textBounds.width - GuiGetStyle(SPINNER, TEXT_PADDING);
  1271. }
  1272. // Update control
  1273. //--------------------------------------------------------------------
  1274. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1275. {
  1276. Vector2 mousePoint = GetMousePosition();
  1277. // Check spinner state
  1278. if (CheckCollisionPointRec(mousePoint, bounds))
  1279. {
  1280. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  1281. else state = GUI_STATE_FOCUSED;
  1282. }
  1283. }
  1284. if (!editMode)
  1285. {
  1286. if (tempValue < minValue) tempValue = minValue;
  1287. if (tempValue > maxValue) tempValue = maxValue;
  1288. }
  1289. //--------------------------------------------------------------------
  1290. // Draw control
  1291. //--------------------------------------------------------------------
  1292. // TODO: Set Spinner properties for ValueBox
  1293. pressed = GuiValueBox(spinner, NULL, &tempValue, minValue, maxValue, editMode);
  1294. // Draw value selector custom buttons
  1295. // NOTE: BORDER_WIDTH and TEXT_ALIGNMENT forced values
  1296. int tempBorderWidth = GuiGetStyle(BUTTON, BORDER_WIDTH);
  1297. int tempTextAlign = GuiGetStyle(BUTTON, TEXT_ALIGNMENT);
  1298. GuiSetStyle(BUTTON, BORDER_WIDTH, GuiGetStyle(SPINNER, BORDER_WIDTH));
  1299. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  1300. #if defined(RAYGUI_SUPPORT_ICONS)
  1301. if (GuiButton(leftButtonBound, GuiIconText(RICON_ARROW_LEFT_FILL, NULL))) tempValue--;
  1302. if (GuiButton(rightButtonBound, GuiIconText(RICON_ARROW_RIGHT_FILL, NULL))) tempValue++;
  1303. #else
  1304. if (GuiButton(leftButtonBound, "<")) tempValue--;
  1305. if (GuiButton(rightButtonBound, ">")) tempValue++;
  1306. #endif
  1307. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, tempTextAlign);
  1308. GuiSetStyle(BUTTON, BORDER_WIDTH, tempBorderWidth);
  1309. // Draw text label if provided
  1310. if (text != NULL) GuiDrawText(text, textBounds, (GuiGetStyle(SPINNER, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_RIGHT)? GUI_TEXT_ALIGN_LEFT : GUI_TEXT_ALIGN_RIGHT, Fade(GetColor(GuiGetStyle(LABEL, TEXT + (state*3))), guiAlpha));
  1311. //--------------------------------------------------------------------
  1312. *value = tempValue;
  1313. return pressed;
  1314. }
  1315. // Value Box control, updates input text with numbers
  1316. // NOTE: Requires static variables: framesCounter
  1317. bool GuiValueBox(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode)
  1318. {
  1319. #if !defined(VALUEBOX_MAX_CHARS)
  1320. #define VALUEBOX_MAX_CHARS 32
  1321. #endif
  1322. static int framesCounter = 0; // Required for blinking cursor
  1323. GuiControlState state = guiState;
  1324. bool pressed = false;
  1325. char textValue[VALUEBOX_MAX_CHARS + 1] = "\0";
  1326. sprintf(textValue, "%i", *value);
  1327. Rectangle textBounds = { 0 };
  1328. if (text != NULL)
  1329. {
  1330. textBounds.width = GetTextWidth(text);
  1331. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  1332. textBounds.x = bounds.x + bounds.width + GuiGetStyle(VALUEBOX, TEXT_PADDING);
  1333. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  1334. if (GuiGetStyle(VALUEBOX, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_LEFT) textBounds.x = bounds.x - textBounds.width - GuiGetStyle(VALUEBOX, TEXT_PADDING);
  1335. }
  1336. // Update control
  1337. //--------------------------------------------------------------------
  1338. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1339. {
  1340. Vector2 mousePoint = GetMousePosition();
  1341. bool valueHasChanged = false;
  1342. if (editMode)
  1343. {
  1344. state = GUI_STATE_PRESSED;
  1345. framesCounter++;
  1346. int keyCount = strlen(textValue);
  1347. // Only allow keys in range [48..57]
  1348. if (keyCount < VALUEBOX_MAX_CHARS)
  1349. {
  1350. int maxWidth = bounds.width;
  1351. if (GetTextWidth(textValue) < maxWidth)
  1352. {
  1353. int key = GetCharPressed();
  1354. if ((key >= 48) && (key <= 57))
  1355. {
  1356. textValue[keyCount] = (char)key;
  1357. keyCount++;
  1358. valueHasChanged = true;
  1359. }
  1360. }
  1361. }
  1362. // Delete text
  1363. if (keyCount > 0)
  1364. {
  1365. if (IsKeyPressed(KEY_BACKSPACE))
  1366. {
  1367. keyCount--;
  1368. textValue[keyCount] = '\0';
  1369. framesCounter = 0;
  1370. if (keyCount < 0) keyCount = 0;
  1371. valueHasChanged = true;
  1372. }
  1373. else if (IsKeyDown(KEY_BACKSPACE))
  1374. {
  1375. if ((framesCounter > TEXTEDIT_CURSOR_BLINK_FRAMES) && (framesCounter%2) == 0) keyCount--;
  1376. textValue[keyCount] = '\0';
  1377. if (keyCount < 0) keyCount = 0;
  1378. valueHasChanged = true;
  1379. }
  1380. }
  1381. if (valueHasChanged) *value = TextToInteger(textValue);
  1382. if (IsKeyPressed(KEY_ENTER) || (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) pressed = true;
  1383. }
  1384. else
  1385. {
  1386. if (*value > maxValue) *value = maxValue;
  1387. else if (*value < minValue) *value = minValue;
  1388. if (CheckCollisionPointRec(mousePoint, bounds))
  1389. {
  1390. state = GUI_STATE_FOCUSED;
  1391. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) pressed = true;
  1392. }
  1393. }
  1394. if (pressed) framesCounter = 0;
  1395. }
  1396. //--------------------------------------------------------------------
  1397. // Draw control
  1398. //--------------------------------------------------------------------
  1399. Color baseColor = BLANK;
  1400. if (state == GUI_STATE_PRESSED) baseColor = GetColor(GuiGetStyle(VALUEBOX, BASE_COLOR_PRESSED));
  1401. else if (state == GUI_STATE_DISABLED) baseColor = GetColor(GuiGetStyle(VALUEBOX, BASE_COLOR_DISABLED));
  1402. // WARNING: BLANK color does not work properly with Fade()
  1403. GuiDrawRectangle(bounds, GuiGetStyle(VALUEBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(VALUEBOX, BORDER + (state*3))), guiAlpha), baseColor);
  1404. GuiDrawText(textValue, GetTextBounds(VALUEBOX, bounds), GUI_TEXT_ALIGN_CENTER, Fade(GetColor(GuiGetStyle(VALUEBOX, TEXT + (state*3))), guiAlpha));
  1405. // Draw blinking cursor
  1406. if ((state == GUI_STATE_PRESSED) && (editMode && ((framesCounter/20)%2 == 0)))
  1407. {
  1408. // NOTE: ValueBox internal text is always centered
  1409. Rectangle cursor = { bounds.x + GetTextWidth(textValue)/2 + bounds.width/2 + 2, bounds.y + 2*GuiGetStyle(VALUEBOX, BORDER_WIDTH), 1, bounds.height - 4*GuiGetStyle(VALUEBOX, BORDER_WIDTH) };
  1410. GuiDrawRectangle(cursor, 0, BLANK, Fade(GetColor(GuiGetStyle(VALUEBOX, BORDER_COLOR_PRESSED)), guiAlpha));
  1411. }
  1412. // Draw text label if provided
  1413. if (text != NULL) GuiDrawText(text, textBounds, (GuiGetStyle(VALUEBOX, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_RIGHT)? GUI_TEXT_ALIGN_LEFT : GUI_TEXT_ALIGN_RIGHT, Fade(GetColor(GuiGetStyle(LABEL, TEXT + (state*3))), guiAlpha));
  1414. //--------------------------------------------------------------------
  1415. return pressed;
  1416. }
  1417. // Text Box control with multiple lines
  1418. bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool editMode)
  1419. {
  1420. static int framesCounter = 0; // Required for blinking cursor
  1421. GuiControlState state = guiState;
  1422. bool pressed = false;
  1423. Rectangle textAreaBounds = {
  1424. bounds.x + GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING),
  1425. bounds.y + GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING),
  1426. bounds.width - 2*GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING),
  1427. bounds.height - 2*GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)
  1428. };
  1429. // Cursor position, [x, y] values should be updated
  1430. Rectangle cursor = { 0, 0, 1, GuiGetStyle(DEFAULT, TEXT_SIZE) + 2 };
  1431. int textWidth = 0;
  1432. int currentLine = 0;
  1433. // Update control
  1434. //--------------------------------------------------------------------
  1435. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1436. {
  1437. Vector2 mousePoint = GetMousePosition();
  1438. if (editMode)
  1439. {
  1440. state = GUI_STATE_PRESSED;
  1441. framesCounter++;
  1442. int character = GetCharPressed();
  1443. int keyCount = strlen(text);
  1444. // Introduce characters
  1445. if (keyCount < (textSize - 1))
  1446. {
  1447. Vector2 textSize = MeasureTextEx(guiFont, text, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING));
  1448. if (textSize.y < (textAreaBounds.height - GuiGetStyle(DEFAULT, TEXT_SIZE)))
  1449. {
  1450. if (IsKeyPressed(KEY_ENTER))
  1451. {
  1452. text[keyCount] = '\n';
  1453. keyCount++;
  1454. }
  1455. else if (((character >= 32) && (character < 255))) // TODO: Support Unicode inputs
  1456. {
  1457. text[keyCount] = (char)character;
  1458. keyCount++;
  1459. }
  1460. }
  1461. }
  1462. // Delete characters
  1463. if (keyCount > 0)
  1464. {
  1465. if (IsKeyPressed(KEY_BACKSPACE))
  1466. {
  1467. keyCount--;
  1468. text[keyCount] = '\0';
  1469. framesCounter = 0;
  1470. if (keyCount < 0) keyCount = 0;
  1471. }
  1472. else if (IsKeyDown(KEY_BACKSPACE))
  1473. {
  1474. if ((framesCounter > TEXTEDIT_CURSOR_BLINK_FRAMES) && (framesCounter%2) == 0) keyCount--;
  1475. text[keyCount] = '\0';
  1476. if (keyCount < 0) keyCount = 0;
  1477. }
  1478. }
  1479. // Calculate cursor position considering text
  1480. char oneCharText[2] = { 0 };
  1481. int lastBreakingPos = -1;
  1482. for (int i = 0; i < keyCount && currentLine < keyCount; i++)
  1483. {
  1484. oneCharText[0] = text[i];
  1485. textWidth += (GetTextWidth(oneCharText) + GuiGetStyle(DEFAULT, TEXT_SPACING));
  1486. if (text[i] == ' ' || text[i] == '\n') lastBreakingPos = i;
  1487. if ( text[i] == '\n' || textWidth >= textAreaBounds.width)
  1488. {
  1489. currentLine++;
  1490. textWidth = 0;
  1491. if (lastBreakingPos > 0) i = lastBreakingPos;
  1492. else textWidth += (GetTextWidth(oneCharText) + GuiGetStyle(DEFAULT, TEXT_SPACING));
  1493. lastBreakingPos = -1;
  1494. }
  1495. }
  1496. cursor.x = bounds.x + GuiGetStyle(TEXTBOX, BORDER_WIDTH) + GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING) + textWidth - GuiGetStyle(DEFAULT, TEXT_SPACING);
  1497. cursor.y = bounds.y + GuiGetStyle(TEXTBOX, BORDER_WIDTH) + GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)/2 + ((GuiGetStyle(DEFAULT, TEXT_SIZE) + GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING))*currentLine);
  1498. // Exit edit mode
  1499. if (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) pressed = true;
  1500. }
  1501. else
  1502. {
  1503. if (CheckCollisionPointRec(mousePoint, bounds))
  1504. {
  1505. state = GUI_STATE_FOCUSED;
  1506. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) pressed = true;
  1507. }
  1508. }
  1509. if (pressed) framesCounter = 0; // Reset blinking cursor
  1510. }
  1511. //--------------------------------------------------------------------
  1512. // Draw control
  1513. //--------------------------------------------------------------------
  1514. if (state == GUI_STATE_PRESSED)
  1515. {
  1516. GuiDrawRectangle(bounds, GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(TEXTBOX, BASE_COLOR_PRESSED)), guiAlpha));
  1517. // Draw blinking cursor
  1518. if (editMode && ((framesCounter/20)%2 == 0)) GuiDrawRectangle(cursor, 0, BLANK, Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER_COLOR_PRESSED)), guiAlpha));
  1519. }
  1520. else if (state == GUI_STATE_DISABLED)
  1521. {
  1522. GuiDrawRectangle(bounds, GuiGetStyle(TEXTBOX, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(TEXTBOX, BASE_COLOR_DISABLED)), guiAlpha));
  1523. }
  1524. else GuiDrawRectangle(bounds, 1, Fade(GetColor(GuiGetStyle(TEXTBOX, BORDER + (state*3))), guiAlpha), BLANK);
  1525. DrawTextRec(guiFont, text, textAreaBounds, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING), true, Fade(GetColor(GuiGetStyle(TEXTBOX, TEXT + (state*3))), guiAlpha));
  1526. //--------------------------------------------------------------------
  1527. return pressed;
  1528. }
  1529. // Slider control with pro parameters
  1530. // NOTE: Other GuiSlider*() controls use this one
  1531. float GuiSliderPro(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue, int sliderWidth)
  1532. {
  1533. GuiControlState state = guiState;
  1534. int sliderValue = (int)(((value - minValue)/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH)));
  1535. Rectangle slider = { bounds.x, bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, SLIDER_PADDING),
  1536. 0, bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2*GuiGetStyle(SLIDER, SLIDER_PADDING) };
  1537. if (sliderWidth > 0) // Slider
  1538. {
  1539. slider.x += (sliderValue - sliderWidth/2);
  1540. slider.width = sliderWidth;
  1541. }
  1542. else if (sliderWidth == 0) // SliderBar
  1543. {
  1544. slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH);
  1545. slider.width = sliderValue;
  1546. }
  1547. // Update control
  1548. //--------------------------------------------------------------------
  1549. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1550. {
  1551. Vector2 mousePoint = GetMousePosition();
  1552. if (CheckCollisionPointRec(mousePoint, bounds))
  1553. {
  1554. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  1555. {
  1556. state = GUI_STATE_PRESSED;
  1557. // Get equivalent value and slider position from mousePoint.x
  1558. value = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + sliderWidth/2)))/(float)(bounds.width - sliderWidth) + minValue;
  1559. if (sliderWidth > 0) slider.x = mousePoint.x - slider.width/2; // Slider
  1560. else if (sliderWidth == 0) slider.width = sliderValue; // SliderBar
  1561. }
  1562. else state = GUI_STATE_FOCUSED;
  1563. }
  1564. if (value > maxValue) value = maxValue;
  1565. else if (value < minValue) value = minValue;
  1566. }
  1567. // Bar limits check
  1568. if (sliderWidth > 0) // Slider
  1569. {
  1570. if (slider.x <= (bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH))) slider.x = bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH);
  1571. else if ((slider.x + slider.width) >= (bounds.x + bounds.width)) slider.x = bounds.x + bounds.width - slider.width - GuiGetStyle(SLIDER, BORDER_WIDTH);
  1572. }
  1573. else if (sliderWidth == 0) // SliderBar
  1574. {
  1575. if (slider.width > bounds.width) slider.width = bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH);
  1576. }
  1577. //--------------------------------------------------------------------
  1578. // Draw control
  1579. //--------------------------------------------------------------------
  1580. GuiDrawRectangle(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(SLIDER, (state != GUI_STATE_DISABLED)? BASE_COLOR_NORMAL : BASE_COLOR_DISABLED)), guiAlpha));
  1581. // Draw slider internal bar (depends on state)
  1582. if ((state == GUI_STATE_NORMAL) || (state == GUI_STATE_PRESSED)) GuiDrawRectangle(slider, 0, BLANK, Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_PRESSED)), guiAlpha));
  1583. else if (state == GUI_STATE_FOCUSED) GuiDrawRectangle(slider, 0, BLANK, Fade(GetColor(GuiGetStyle(SLIDER, TEXT_COLOR_FOCUSED)), guiAlpha));
  1584. // Draw left/right text if provided
  1585. if (textLeft != NULL)
  1586. {
  1587. Rectangle textBounds = { 0 };
  1588. textBounds.width = GetTextWidth(textLeft); // TODO: Consider text icon
  1589. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  1590. textBounds.x = bounds.x - textBounds.width - GuiGetStyle(SLIDER, TEXT_PADDING);
  1591. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  1592. GuiDrawText(textLeft, textBounds, GUI_TEXT_ALIGN_RIGHT, Fade(GetColor(GuiGetStyle(SLIDER, TEXT + (state*3))), guiAlpha));
  1593. }
  1594. if (textRight != NULL)
  1595. {
  1596. Rectangle textBounds = { 0 };
  1597. textBounds.width = GetTextWidth(textRight); // TODO: Consider text icon
  1598. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  1599. textBounds.x = bounds.x + bounds.width + GuiGetStyle(SLIDER, TEXT_PADDING);
  1600. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  1601. GuiDrawText(textRight, textBounds, GUI_TEXT_ALIGN_LEFT, Fade(GetColor(GuiGetStyle(SLIDER, TEXT + (state*3))), guiAlpha));
  1602. }
  1603. //--------------------------------------------------------------------
  1604. return value;
  1605. }
  1606. // Slider control extended, returns selected value and has text
  1607. float GuiSlider(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue)
  1608. {
  1609. return GuiSliderPro(bounds, textLeft, textRight, value, minValue, maxValue, GuiGetStyle(SLIDER, SLIDER_WIDTH));
  1610. }
  1611. // Slider Bar control extended, returns selected value
  1612. float GuiSliderBar(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue)
  1613. {
  1614. return GuiSliderPro(bounds, textLeft, textRight, value, minValue, maxValue, 0);
  1615. }
  1616. // Progress Bar control extended, shows current progress value
  1617. float GuiProgressBar(Rectangle bounds, const char *textLeft, const char *textRight, float value, float minValue, float maxValue)
  1618. {
  1619. GuiControlState state = guiState;
  1620. Rectangle progress = { bounds.x + GuiGetStyle(PROGRESSBAR, BORDER_WIDTH),
  1621. bounds.y + GuiGetStyle(PROGRESSBAR, BORDER_WIDTH) + GuiGetStyle(PROGRESSBAR, PROGRESS_PADDING), 0,
  1622. bounds.height - 2*GuiGetStyle(PROGRESSBAR, BORDER_WIDTH) - 2*GuiGetStyle(PROGRESSBAR, PROGRESS_PADDING) };
  1623. // Update control
  1624. //--------------------------------------------------------------------
  1625. if (state != GUI_STATE_DISABLED) progress.width = (int)(value/(maxValue - minValue)*(float)(bounds.width - 2*GuiGetStyle(PROGRESSBAR, BORDER_WIDTH)));
  1626. //--------------------------------------------------------------------
  1627. // Draw control
  1628. //--------------------------------------------------------------------
  1629. GuiDrawRectangle(bounds, GuiGetStyle(PROGRESSBAR, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(PROGRESSBAR, BORDER + (state*3))), guiAlpha), BLANK);
  1630. // Draw slider internal progress bar (depends on state)
  1631. if ((state == GUI_STATE_NORMAL) || (state == GUI_STATE_PRESSED)) GuiDrawRectangle(progress, 0, BLANK, Fade(GetColor(GuiGetStyle(PROGRESSBAR, BASE_COLOR_PRESSED)), guiAlpha));
  1632. else if (state == GUI_STATE_FOCUSED) GuiDrawRectangle(progress, 0, BLANK, Fade(GetColor(GuiGetStyle(PROGRESSBAR, TEXT_COLOR_FOCUSED)), guiAlpha));
  1633. // Draw left/right text if provided
  1634. if (textLeft != NULL)
  1635. {
  1636. Rectangle textBounds = { 0 };
  1637. textBounds.width = GetTextWidth(textLeft); // TODO: Consider text icon
  1638. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  1639. textBounds.x = bounds.x - textBounds.width - GuiGetStyle(PROGRESSBAR, TEXT_PADDING);
  1640. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  1641. GuiDrawText(textLeft, textBounds, GUI_TEXT_ALIGN_RIGHT, Fade(GetColor(GuiGetStyle(PROGRESSBAR, TEXT + (state*3))), guiAlpha));
  1642. }
  1643. if (textRight != NULL)
  1644. {
  1645. Rectangle textBounds = { 0 };
  1646. textBounds.width = GetTextWidth(textRight); // TODO: Consider text icon
  1647. textBounds.height = GuiGetStyle(DEFAULT, TEXT_SIZE);
  1648. textBounds.x = bounds.x + bounds.width + GuiGetStyle(PROGRESSBAR, TEXT_PADDING);
  1649. textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
  1650. GuiDrawText(textRight, textBounds, GUI_TEXT_ALIGN_LEFT, Fade(GetColor(GuiGetStyle(PROGRESSBAR, TEXT + (state*3))), guiAlpha));
  1651. }
  1652. //--------------------------------------------------------------------
  1653. return value;
  1654. }
  1655. // Status Bar control
  1656. void GuiStatusBar(Rectangle bounds, const char *text)
  1657. {
  1658. GuiControlState state = guiState;
  1659. // Draw control
  1660. //--------------------------------------------------------------------
  1661. GuiDrawRectangle(bounds, GuiGetStyle(STATUSBAR, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(STATUSBAR, (state != GUI_STATE_DISABLED)? BORDER_COLOR_NORMAL : BORDER_COLOR_DISABLED)), guiAlpha),
  1662. Fade(GetColor(GuiGetStyle(STATUSBAR, (state != GUI_STATE_DISABLED)? BASE_COLOR_NORMAL : BASE_COLOR_DISABLED)), guiAlpha));
  1663. GuiDrawText(text, GetTextBounds(STATUSBAR, bounds), GuiGetStyle(STATUSBAR, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(STATUSBAR, (state != GUI_STATE_DISABLED)? TEXT_COLOR_NORMAL : TEXT_COLOR_DISABLED)), guiAlpha));
  1664. //--------------------------------------------------------------------
  1665. }
  1666. // Dummy rectangle control, intended for placeholding
  1667. void GuiDummyRec(Rectangle bounds, const char *text)
  1668. {
  1669. GuiControlState state = guiState;
  1670. // Update control
  1671. //--------------------------------------------------------------------
  1672. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1673. {
  1674. Vector2 mousePoint = GetMousePosition();
  1675. // Check button state
  1676. if (CheckCollisionPointRec(mousePoint, bounds))
  1677. {
  1678. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = GUI_STATE_PRESSED;
  1679. else state = GUI_STATE_FOCUSED;
  1680. }
  1681. }
  1682. //--------------------------------------------------------------------
  1683. // Draw control
  1684. //--------------------------------------------------------------------
  1685. GuiDrawRectangle(bounds, 0, BLANK, Fade(GetColor(GuiGetStyle(DEFAULT, (state != GUI_STATE_DISABLED)? BASE_COLOR_NORMAL : BASE_COLOR_DISABLED)), guiAlpha));
  1686. GuiDrawText(text, GetTextBounds(DEFAULT, bounds), GUI_TEXT_ALIGN_CENTER, Fade(GetColor(GuiGetStyle(BUTTON, (state != GUI_STATE_DISABLED)? TEXT_COLOR_NORMAL : TEXT_COLOR_DISABLED)), guiAlpha));
  1687. //------------------------------------------------------------------
  1688. }
  1689. // Scroll Bar control
  1690. // TODO: I feel GuiScrollBar could be simplified...
  1691. int GuiScrollBar(Rectangle bounds, int value, int minValue, int maxValue)
  1692. {
  1693. GuiControlState state = guiState;
  1694. // Is the scrollbar horizontal or vertical?
  1695. bool isVertical = (bounds.width > bounds.height)? false : true;
  1696. // The size (width or height depending on scrollbar type) of the spinner buttons
  1697. const int spinnerSize = GuiGetStyle(SCROLLBAR, ARROWS_VISIBLE)? (isVertical? bounds.width - 2*GuiGetStyle(SCROLLBAR, BORDER_WIDTH) : bounds.height - 2*GuiGetStyle(SCROLLBAR, BORDER_WIDTH)) : 0;
  1698. // Arrow buttons [<] [>] [∧] [∨]
  1699. Rectangle arrowUpLeft = { 0 };
  1700. Rectangle arrowDownRight = { 0 };
  1701. // Actual area of the scrollbar excluding the arrow buttons
  1702. Rectangle scrollbar = { 0 };
  1703. // Slider bar that moves --[///]-----
  1704. Rectangle slider = { 0 };
  1705. // Normalize value
  1706. if (value > maxValue) value = maxValue;
  1707. if (value < minValue) value = minValue;
  1708. const int range = maxValue - minValue;
  1709. int sliderSize = GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE);
  1710. // Calculate rectangles for all of the components
  1711. arrowUpLeft = RAYGUI_CLITERAL(Rectangle){ (float)bounds.x + GuiGetStyle(SCROLLBAR, BORDER_WIDTH), (float)bounds.y + GuiGetStyle(SCROLLBAR, BORDER_WIDTH), (float)spinnerSize, (float)spinnerSize };
  1712. if (isVertical)
  1713. {
  1714. arrowDownRight = RAYGUI_CLITERAL(Rectangle){ (float)bounds.x + GuiGetStyle(SCROLLBAR, BORDER_WIDTH), (float)bounds.y + bounds.height - spinnerSize - GuiGetStyle(SCROLLBAR, BORDER_WIDTH), (float)spinnerSize, (float)spinnerSize};
  1715. scrollbar = RAYGUI_CLITERAL(Rectangle){ bounds.x + GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_PADDING), arrowUpLeft.y + arrowUpLeft.height, bounds.width - 2*(GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_PADDING)), bounds.height - arrowUpLeft.height - arrowDownRight.height - 2*GuiGetStyle(SCROLLBAR, BORDER_WIDTH) };
  1716. sliderSize = (sliderSize >= scrollbar.height)? (scrollbar.height - 2) : sliderSize; // Make sure the slider won't get outside of the scrollbar
  1717. slider = RAYGUI_CLITERAL(Rectangle){ (float)bounds.x + GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_PADDING), (float)scrollbar.y + (int)(((float)(value - minValue)/range)*(scrollbar.height - sliderSize)), (float)bounds.width - 2*(GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_PADDING)), (float)sliderSize };
  1718. }
  1719. else
  1720. {
  1721. arrowDownRight = RAYGUI_CLITERAL(Rectangle){ (float)bounds.x + bounds.width - spinnerSize - GuiGetStyle(SCROLLBAR, BORDER_WIDTH), (float)bounds.y + GuiGetStyle(SCROLLBAR, BORDER_WIDTH), (float)spinnerSize, (float)spinnerSize};
  1722. scrollbar = RAYGUI_CLITERAL(Rectangle){ arrowUpLeft.x + arrowUpLeft.width, bounds.y + GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_PADDING), bounds.width - arrowUpLeft.width - arrowDownRight.width - 2*GuiGetStyle(SCROLLBAR, BORDER_WIDTH), bounds.height - 2*(GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_PADDING))};
  1723. sliderSize = (sliderSize >= scrollbar.width)? (scrollbar.width - 2) : sliderSize; // Make sure the slider won't get outside of the scrollbar
  1724. slider = RAYGUI_CLITERAL(Rectangle){ (float)scrollbar.x + (int)(((float)(value - minValue)/range)*(scrollbar.width - sliderSize)), (float)bounds.y + GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_PADDING), (float)sliderSize, (float)bounds.height - 2*(GuiGetStyle(SCROLLBAR, BORDER_WIDTH) + GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_PADDING)) };
  1725. }
  1726. // Update control
  1727. //--------------------------------------------------------------------
  1728. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1729. {
  1730. Vector2 mousePoint = GetMousePosition();
  1731. if (CheckCollisionPointRec(mousePoint, bounds))
  1732. {
  1733. state = GUI_STATE_FOCUSED;
  1734. // Handle mouse wheel
  1735. int wheel = GetMouseWheelMove();
  1736. if (wheel != 0) value += wheel;
  1737. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  1738. {
  1739. if (CheckCollisionPointRec(mousePoint, arrowUpLeft)) value -= range/GuiGetStyle(SCROLLBAR, SCROLL_SPEED);
  1740. else if (CheckCollisionPointRec(mousePoint, arrowDownRight)) value += range/GuiGetStyle(SCROLLBAR, SCROLL_SPEED);
  1741. state = GUI_STATE_PRESSED;
  1742. }
  1743. else if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  1744. {
  1745. if (!isVertical)
  1746. {
  1747. Rectangle scrollArea = { arrowUpLeft.x + arrowUpLeft.width, arrowUpLeft.y, scrollbar.width, bounds.height - 2*GuiGetStyle(SCROLLBAR, BORDER_WIDTH)};
  1748. if (CheckCollisionPointRec(mousePoint, scrollArea)) value = ((float)(mousePoint.x - scrollArea.x - slider.width/2)*range)/(scrollArea.width - slider.width) + minValue;
  1749. }
  1750. else
  1751. {
  1752. Rectangle scrollArea = { arrowUpLeft.x, arrowUpLeft.y+arrowUpLeft.height, bounds.width - 2*GuiGetStyle(SCROLLBAR, BORDER_WIDTH), scrollbar.height};
  1753. if (CheckCollisionPointRec(mousePoint, scrollArea)) value = ((float)(mousePoint.y - scrollArea.y - slider.height/2)*range)/(scrollArea.height - slider.height) + minValue;
  1754. }
  1755. }
  1756. }
  1757. // Normalize value
  1758. if (value > maxValue) value = maxValue;
  1759. if (value < minValue) value = minValue;
  1760. }
  1761. //--------------------------------------------------------------------
  1762. // Draw control
  1763. //--------------------------------------------------------------------
  1764. GuiDrawRectangle(bounds, GuiGetStyle(SCROLLBAR, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(LISTVIEW, BORDER + state*3)), guiAlpha), Fade(GetColor(GuiGetStyle(DEFAULT, BORDER_COLOR_DISABLED)), guiAlpha)); // Draw the background
  1765. GuiDrawRectangle(scrollbar, 0, BLANK, Fade(GetColor(GuiGetStyle(BUTTON, BASE_COLOR_NORMAL)), guiAlpha)); // Draw the scrollbar active area background
  1766. GuiDrawRectangle(slider, 0, BLANK, Fade(GetColor(GuiGetStyle(SLIDER, BORDER + state*3)), guiAlpha)); // Draw the slider bar
  1767. // Draw arrows
  1768. const int padding = (spinnerSize - GuiGetStyle(SCROLLBAR, ARROWS_SIZE))/2;
  1769. const Vector2 lineCoords[] =
  1770. {
  1771. // Coordinates for < 0,1,2
  1772. { arrowUpLeft.x + padding, arrowUpLeft.y + spinnerSize/2 },
  1773. { arrowUpLeft.x + spinnerSize - padding, arrowUpLeft.y + padding },
  1774. { arrowUpLeft.x + spinnerSize - padding, arrowUpLeft.y + spinnerSize - padding },
  1775. // Coordinates for > 3,4,5
  1776. { arrowDownRight.x + padding, arrowDownRight.y + padding },
  1777. { arrowDownRight.x + spinnerSize - padding, arrowDownRight.y + spinnerSize/2 },
  1778. { arrowDownRight.x + padding, arrowDownRight.y + spinnerSize - padding },
  1779. // Coordinates for ∧ 6,7,8
  1780. { arrowUpLeft.x + spinnerSize/2, arrowUpLeft.y + padding },
  1781. { arrowUpLeft.x + padding, arrowUpLeft.y + spinnerSize - padding },
  1782. { arrowUpLeft.x + spinnerSize - padding, arrowUpLeft.y + spinnerSize - padding },
  1783. // Coordinates for ∨ 9,10,11
  1784. { arrowDownRight.x + padding, arrowDownRight.y + padding },
  1785. { arrowDownRight.x + spinnerSize/2, arrowDownRight.y + spinnerSize - padding },
  1786. { arrowDownRight.x + spinnerSize - padding, arrowDownRight.y + padding }
  1787. };
  1788. Color lineColor = Fade(GetColor(GuiGetStyle(BUTTON, TEXT + state*3)), guiAlpha);
  1789. if (GuiGetStyle(SCROLLBAR, ARROWS_VISIBLE))
  1790. {
  1791. if (isVertical)
  1792. {
  1793. DrawTriangle(lineCoords[6], lineCoords[7], lineCoords[8], lineColor);
  1794. DrawTriangle(lineCoords[9], lineCoords[10], lineCoords[11], lineColor);
  1795. }
  1796. else
  1797. {
  1798. DrawTriangle(lineCoords[2], lineCoords[1], lineCoords[0], lineColor);
  1799. DrawTriangle(lineCoords[5], lineCoords[4], lineCoords[3], lineColor);
  1800. }
  1801. }
  1802. //--------------------------------------------------------------------
  1803. return value;
  1804. }
  1805. // List View control
  1806. int GuiListView(Rectangle bounds, const char *text, int *scrollIndex, int active)
  1807. {
  1808. int itemsCount = 0;
  1809. const char **items = NULL;
  1810. if (text != NULL) items = GuiTextSplit(text, &itemsCount, NULL);
  1811. return GuiListViewEx(bounds, items, itemsCount, NULL, scrollIndex, active);
  1812. }
  1813. // List View control with extended parameters
  1814. int GuiListViewEx(Rectangle bounds, const char **text, int count, int *focus, int *scrollIndex, int active)
  1815. {
  1816. GuiControlState state = guiState;
  1817. int itemFocused = (focus == NULL)? -1 : *focus;
  1818. int itemSelected = active;
  1819. // Check if we need a scroll bar
  1820. bool useScrollBar = false;
  1821. if ((GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING))*count > bounds.height) useScrollBar = true;
  1822. // Define base item rectangle [0]
  1823. Rectangle itemBounds = { 0 };
  1824. itemBounds.x = bounds.x + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING);
  1825. itemBounds.y = bounds.y + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING) + GuiGetStyle(DEFAULT, BORDER_WIDTH);
  1826. itemBounds.width = bounds.width - 2*GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING) - GuiGetStyle(DEFAULT, BORDER_WIDTH);
  1827. itemBounds.height = GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT);
  1828. if (useScrollBar) itemBounds.width -= GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH);
  1829. // Get items on the list
  1830. int visibleItems = bounds.height/(GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING));
  1831. if (visibleItems > count) visibleItems = count;
  1832. int startIndex = (scrollIndex == NULL)? 0 : *scrollIndex;
  1833. if ((startIndex < 0) || (startIndex > (count - visibleItems))) startIndex = 0;
  1834. int endIndex = startIndex + visibleItems;
  1835. // Update control
  1836. //--------------------------------------------------------------------
  1837. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1838. {
  1839. Vector2 mousePoint = GetMousePosition();
  1840. // Check mouse inside list view
  1841. if (CheckCollisionPointRec(mousePoint, bounds))
  1842. {
  1843. state = GUI_STATE_FOCUSED;
  1844. // Check focused and selected item
  1845. for (int i = 0; i < visibleItems; i++)
  1846. {
  1847. if (CheckCollisionPointRec(mousePoint, itemBounds))
  1848. {
  1849. itemFocused = startIndex + i;
  1850. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  1851. {
  1852. if (itemSelected == (startIndex + i)) itemSelected = -1;
  1853. else itemSelected = startIndex + i;
  1854. }
  1855. break;
  1856. }
  1857. // Update item rectangle y position for next item
  1858. itemBounds.y += (GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING));
  1859. }
  1860. if (useScrollBar)
  1861. {
  1862. int wheelMove = GetMouseWheelMove();
  1863. startIndex -= wheelMove;
  1864. if (startIndex < 0) startIndex = 0;
  1865. else if (startIndex > (count - visibleItems)) startIndex = count - visibleItems;
  1866. endIndex = startIndex + visibleItems;
  1867. if (endIndex > count) endIndex = count;
  1868. }
  1869. }
  1870. else itemFocused = -1;
  1871. // Reset item rectangle y to [0]
  1872. itemBounds.y = bounds.y + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING) + GuiGetStyle(DEFAULT, BORDER_WIDTH);
  1873. }
  1874. //--------------------------------------------------------------------
  1875. // Draw control
  1876. //--------------------------------------------------------------------
  1877. GuiDrawRectangle(bounds, GuiGetStyle(DEFAULT, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(LISTVIEW, BORDER + state*3)), guiAlpha), GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR))); // Draw background
  1878. // Draw visible items
  1879. for (int i = 0; ((i < visibleItems) && (text != NULL)); i++)
  1880. {
  1881. if (state == GUI_STATE_DISABLED)
  1882. {
  1883. if ((startIndex + i) == itemSelected) GuiDrawRectangle(itemBounds, GuiGetStyle(LISTVIEW, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(LISTVIEW, BORDER_COLOR_DISABLED)), guiAlpha), Fade(GetColor(GuiGetStyle(LISTVIEW, BASE_COLOR_DISABLED)), guiAlpha));
  1884. GuiDrawText(text[startIndex + i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(LISTVIEW, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(LISTVIEW, TEXT_COLOR_DISABLED)), guiAlpha));
  1885. }
  1886. else
  1887. {
  1888. if ((startIndex + i) == itemSelected)
  1889. {
  1890. // Draw item selected
  1891. GuiDrawRectangle(itemBounds, GuiGetStyle(LISTVIEW, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(LISTVIEW, BORDER_COLOR_PRESSED)), guiAlpha), Fade(GetColor(GuiGetStyle(LISTVIEW, BASE_COLOR_PRESSED)), guiAlpha));
  1892. GuiDrawText(text[startIndex + i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(LISTVIEW, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(LISTVIEW, TEXT_COLOR_PRESSED)), guiAlpha));
  1893. }
  1894. else if ((startIndex + i) == itemFocused)
  1895. {
  1896. // Draw item focused
  1897. GuiDrawRectangle(itemBounds, GuiGetStyle(LISTVIEW, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(LISTVIEW, BORDER_COLOR_FOCUSED)), guiAlpha), Fade(GetColor(GuiGetStyle(LISTVIEW, BASE_COLOR_FOCUSED)), guiAlpha));
  1898. GuiDrawText(text[startIndex + i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(LISTVIEW, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(LISTVIEW, TEXT_COLOR_FOCUSED)), guiAlpha));
  1899. }
  1900. else
  1901. {
  1902. // Draw item normal
  1903. GuiDrawText(text[startIndex + i], GetTextBounds(DEFAULT, itemBounds), GuiGetStyle(LISTVIEW, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(LISTVIEW, TEXT_COLOR_NORMAL)), guiAlpha));
  1904. }
  1905. }
  1906. // Update item rectangle y position for next item
  1907. itemBounds.y += (GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING));
  1908. }
  1909. if (useScrollBar)
  1910. {
  1911. Rectangle scrollBarBounds = {
  1912. bounds.x + bounds.width - GuiGetStyle(LISTVIEW, BORDER_WIDTH) - GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH),
  1913. bounds.y + GuiGetStyle(LISTVIEW, BORDER_WIDTH), (float)GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH),
  1914. bounds.height - 2*GuiGetStyle(DEFAULT, BORDER_WIDTH)
  1915. };
  1916. // Calculate percentage of visible items and apply same percentage to scrollbar
  1917. float percentVisible = (float)(endIndex - startIndex)/count;
  1918. float sliderSize = bounds.height*percentVisible;
  1919. int prevSliderSize = GuiGetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE); // Save default slider size
  1920. int prevScrollSpeed = GuiGetStyle(SCROLLBAR, SCROLL_SPEED); // Save default scroll speed
  1921. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE, sliderSize); // Change slider size
  1922. GuiSetStyle(SCROLLBAR, SCROLL_SPEED, count - visibleItems); // Change scroll speed
  1923. startIndex = GuiScrollBar(scrollBarBounds, startIndex, 0, count - visibleItems);
  1924. GuiSetStyle(SCROLLBAR, SCROLL_SPEED, prevScrollSpeed); // Reset scroll speed to default
  1925. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE, prevSliderSize); // Reset slider size to default
  1926. }
  1927. //--------------------------------------------------------------------
  1928. if (focus != NULL) *focus = itemFocused;
  1929. if (scrollIndex != NULL) *scrollIndex = startIndex;
  1930. return itemSelected;
  1931. }
  1932. // Color Panel control
  1933. Color GuiColorPanelEx(Rectangle bounds, Color color, float hue)
  1934. {
  1935. GuiControlState state = guiState;
  1936. Vector2 pickerSelector = { 0 };
  1937. Vector3 vcolor = { (float)color.r/255.0f, (float)color.g/255.0f, (float)color.b/255.0f };
  1938. Vector3 hsv = ConvertRGBtoHSV(vcolor);
  1939. pickerSelector.x = bounds.x + (float)hsv.y*bounds.width; // HSV: Saturation
  1940. pickerSelector.y = bounds.y + (1.0f - (float)hsv.z)*bounds.height; // HSV: Value
  1941. Vector3 maxHue = { hue >= 0.0f ? hue : hsv.x, 1.0f, 1.0f };
  1942. Vector3 rgbHue = ConvertHSVtoRGB(maxHue);
  1943. Color maxHueCol = { (unsigned char)(255.0f*rgbHue.x),
  1944. (unsigned char)(255.0f*rgbHue.y),
  1945. (unsigned char)(255.0f*rgbHue.z), 255 };
  1946. const Color colWhite = { 255, 255, 255, 255 };
  1947. const Color colBlack = { 0, 0, 0, 255 };
  1948. // Update control
  1949. //--------------------------------------------------------------------
  1950. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  1951. {
  1952. Vector2 mousePoint = GetMousePosition();
  1953. if (CheckCollisionPointRec(mousePoint, bounds))
  1954. {
  1955. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  1956. {
  1957. state = GUI_STATE_PRESSED;
  1958. pickerSelector = mousePoint;
  1959. // Calculate color from picker
  1960. Vector2 colorPick = { pickerSelector.x - bounds.x, pickerSelector.y - bounds.y };
  1961. colorPick.x /= (float)bounds.width; // Get normalized value on x
  1962. colorPick.y /= (float)bounds.height; // Get normalized value on y
  1963. hsv.y = colorPick.x;
  1964. hsv.z = 1.0f - colorPick.y;
  1965. Vector3 rgb = ConvertHSVtoRGB(hsv);
  1966. // NOTE: Vector3ToColor() only available on raylib 1.8.1
  1967. color = RAYGUI_CLITERAL(Color){ (unsigned char)(255.0f*rgb.x),
  1968. (unsigned char)(255.0f*rgb.y),
  1969. (unsigned char)(255.0f*rgb.z),
  1970. (unsigned char)(255.0f*(float)color.a/255.0f) };
  1971. }
  1972. else state = GUI_STATE_FOCUSED;
  1973. }
  1974. }
  1975. //--------------------------------------------------------------------
  1976. // Draw control
  1977. //--------------------------------------------------------------------
  1978. if (state != GUI_STATE_DISABLED)
  1979. {
  1980. DrawRectangleGradientEx(bounds, Fade(colWhite, guiAlpha), Fade(colWhite, guiAlpha), Fade(maxHueCol, guiAlpha), Fade(maxHueCol, guiAlpha));
  1981. DrawRectangleGradientEx(bounds, Fade(colBlack, 0), Fade(colBlack, guiAlpha), Fade(colBlack, guiAlpha), Fade(colBlack, 0));
  1982. // Draw color picker: selector
  1983. Rectangle selector = { pickerSelector.x - GuiGetStyle(COLORPICKER, COLOR_SELECTOR_SIZE)/2, pickerSelector.y - GuiGetStyle(COLORPICKER, COLOR_SELECTOR_SIZE)/2, GuiGetStyle(COLORPICKER, COLOR_SELECTOR_SIZE), GuiGetStyle(COLORPICKER, COLOR_SELECTOR_SIZE) };
  1984. GuiDrawRectangle(selector, 0, BLANK, Fade(colWhite, guiAlpha));
  1985. }
  1986. else
  1987. {
  1988. DrawRectangleGradientEx(bounds, Fade(Fade(GetColor(GuiGetStyle(COLORPICKER, BASE_COLOR_DISABLED)), 0.1f), guiAlpha), Fade(Fade(colBlack, 0.6f), guiAlpha), Fade(Fade(colBlack, 0.6f), guiAlpha), Fade(Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER_COLOR_DISABLED)), 0.6f), guiAlpha));
  1989. }
  1990. GuiDrawRectangle(bounds, 1, Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER + state*3)), guiAlpha), BLANK);
  1991. //--------------------------------------------------------------------
  1992. return color;
  1993. }
  1994. Color GuiColorPanel(Rectangle bounds, Color color)
  1995. {
  1996. return GuiColorPanelEx(bounds, color, -1.0f);
  1997. }
  1998. // Color Bar Alpha control
  1999. // NOTE: Returns alpha value normalized [0..1]
  2000. float GuiColorBarAlpha(Rectangle bounds, float alpha)
  2001. {
  2002. #define COLORBARALPHA_CHECKED_SIZE 10
  2003. GuiControlState state = guiState;
  2004. Rectangle selector = { (float)bounds.x + alpha*bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (float)bounds.y - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (float)GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_HEIGHT), (float)bounds.height + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)*2 };
  2005. // Update control
  2006. //--------------------------------------------------------------------
  2007. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  2008. {
  2009. Vector2 mousePoint = GetMousePosition();
  2010. if (CheckCollisionPointRec(mousePoint, bounds) ||
  2011. CheckCollisionPointRec(mousePoint, selector))
  2012. {
  2013. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  2014. {
  2015. state = GUI_STATE_PRESSED;
  2016. selector.x = mousePoint.x - selector.width/2;
  2017. alpha = (mousePoint.x - bounds.x)/bounds.width;
  2018. if (alpha <= 0.0f) alpha = 0.0f;
  2019. if (alpha >= 1.0f) alpha = 1.0f;
  2020. //selector.x = bounds.x + (int)(((alpha - 0)/(100 - 0))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH))) - selector.width/2;
  2021. }
  2022. else state = GUI_STATE_FOCUSED;
  2023. }
  2024. }
  2025. //--------------------------------------------------------------------
  2026. // Draw control
  2027. //--------------------------------------------------------------------
  2028. // Draw alpha bar: checked background
  2029. if (state != GUI_STATE_DISABLED)
  2030. {
  2031. int checksX = bounds.width/COLORBARALPHA_CHECKED_SIZE;
  2032. int checksY = bounds.height/COLORBARALPHA_CHECKED_SIZE;
  2033. for (int x = 0; x < checksX; x++)
  2034. {
  2035. for (int y = 0; y < checksY; y++)
  2036. {
  2037. Rectangle check = { bounds.x + x*COLORBARALPHA_CHECKED_SIZE, bounds.y + y*COLORBARALPHA_CHECKED_SIZE, COLORBARALPHA_CHECKED_SIZE, COLORBARALPHA_CHECKED_SIZE };
  2038. GuiDrawRectangle(check, 0, BLANK, ((x + y)%2)? Fade(Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER_COLOR_DISABLED)), 0.4f), guiAlpha) : Fade(Fade(GetColor(GuiGetStyle(COLORPICKER, BASE_COLOR_DISABLED)), 0.4f), guiAlpha));
  2039. }
  2040. }
  2041. DrawRectangleGradientEx(bounds, RAYGUI_CLITERAL(Color){ 255, 255, 255, 0 }, RAYGUI_CLITERAL(Color){ 255, 255, 255, 0 }, Fade(RAYGUI_CLITERAL(Color){ 0, 0, 0, 255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 0, 0, 0, 255 }, guiAlpha));
  2042. }
  2043. else DrawRectangleGradientEx(bounds, Fade(GetColor(GuiGetStyle(COLORPICKER, BASE_COLOR_DISABLED)), 0.1f), Fade(GetColor(GuiGetStyle(COLORPICKER, BASE_COLOR_DISABLED)), 0.1f), Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER_COLOR_DISABLED)), guiAlpha), Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER_COLOR_DISABLED)), guiAlpha));
  2044. GuiDrawRectangle(bounds, 1, Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER + state*3)), guiAlpha), BLANK);
  2045. // Draw alpha bar: selector
  2046. GuiDrawRectangle(selector, 0, BLANK, Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER + state*3)), guiAlpha));
  2047. //--------------------------------------------------------------------
  2048. return alpha;
  2049. }
  2050. // Color Bar Hue control
  2051. // NOTE: Returns hue value normalized [0..1]
  2052. float GuiColorBarHue(Rectangle bounds, float hue)
  2053. {
  2054. GuiControlState state = guiState;
  2055. Rectangle selector = { (float)bounds.x - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (float)bounds.y + hue/360.0f*bounds.height - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (float)bounds.width + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)*2, (float)GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_HEIGHT) };
  2056. // Update control
  2057. //--------------------------------------------------------------------
  2058. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  2059. {
  2060. Vector2 mousePoint = GetMousePosition();
  2061. if (CheckCollisionPointRec(mousePoint, bounds) ||
  2062. CheckCollisionPointRec(mousePoint, selector))
  2063. {
  2064. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  2065. {
  2066. state = GUI_STATE_PRESSED;
  2067. selector.y = mousePoint.y - selector.height/2;
  2068. hue = (mousePoint.y - bounds.y)*360/bounds.height;
  2069. if (hue <= 0.0f) hue = 0.0f;
  2070. if (hue >= 359.0f) hue = 359.0f;
  2071. }
  2072. else state = GUI_STATE_FOCUSED;
  2073. /*if (IsKeyDown(KEY_UP))
  2074. {
  2075. hue -= 2.0f;
  2076. if (hue <= 0.0f) hue = 0.0f;
  2077. }
  2078. else if (IsKeyDown(KEY_DOWN))
  2079. {
  2080. hue += 2.0f;
  2081. if (hue >= 360.0f) hue = 360.0f;
  2082. }*/
  2083. }
  2084. }
  2085. //--------------------------------------------------------------------
  2086. // Draw control
  2087. //--------------------------------------------------------------------
  2088. if (state != GUI_STATE_DISABLED)
  2089. {
  2090. // Draw hue bar:color bars
  2091. DrawRectangleGradientV(bounds.x + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.y + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (int)bounds.height/6, Fade(RAYGUI_CLITERAL(Color){ 255,0,0,255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 255,255,0,255 }, guiAlpha));
  2092. DrawRectangleGradientV(bounds.x + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.y + (int)bounds.height/6 + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (int)bounds.height/6, Fade(RAYGUI_CLITERAL(Color){ 255,255,0,255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 0,255,0,255 }, guiAlpha));
  2093. DrawRectangleGradientV(bounds.x + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.y + 2*((int)bounds.height/6) + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (int)bounds.height/6, Fade(RAYGUI_CLITERAL(Color){ 0,255,0,255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 0,255,255,255 }, guiAlpha));
  2094. DrawRectangleGradientV(bounds.x + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.y + 3*((int)bounds.height/6) + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (int)bounds.height/6, Fade(RAYGUI_CLITERAL(Color){ 0,255,255,255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 0,0,255,255 }, guiAlpha));
  2095. DrawRectangleGradientV(bounds.x + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.y + 4*((int)bounds.height/6) + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (int)bounds.height/6, Fade(RAYGUI_CLITERAL(Color){ 0,0,255,255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 255,0,255,255 }, guiAlpha));
  2096. DrawRectangleGradientV(bounds.x + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.y + 5*((int)bounds.height/6) + GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW)/2, bounds.width - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), (int)bounds.height/6 - GuiGetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW), Fade(RAYGUI_CLITERAL(Color){ 255,0,255,255 }, guiAlpha), Fade(RAYGUI_CLITERAL(Color){ 255,0,0,255 }, guiAlpha));
  2097. }
  2098. else DrawRectangleGradientV(bounds.x, bounds.y, bounds.width, bounds.height, Fade(Fade(GetColor(GuiGetStyle(COLORPICKER, BASE_COLOR_DISABLED)), 0.1f), guiAlpha), Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER_COLOR_DISABLED)), guiAlpha));
  2099. GuiDrawRectangle(bounds, 1, Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER + state*3)), guiAlpha), BLANK);
  2100. // Draw hue bar: selector
  2101. GuiDrawRectangle(selector, 0, BLANK, Fade(GetColor(GuiGetStyle(COLORPICKER, BORDER + state*3)), guiAlpha));
  2102. //--------------------------------------------------------------------
  2103. return hue;
  2104. }
  2105. // TODO: Color GuiColorBarSat() [WHITE->color]
  2106. // TODO: Color GuiColorBarValue() [BLACK->color], HSV / HSL
  2107. // TODO: float GuiColorBarLuminance() [BLACK->WHITE]
  2108. // Color Picker control
  2109. // NOTE: It's divided in multiple controls:
  2110. // Color GuiColorPanel(Rectangle bounds, Color color)
  2111. // float GuiColorBarAlpha(Rectangle bounds, float alpha)
  2112. // float GuiColorBarHue(Rectangle bounds, float value)
  2113. // NOTE: bounds define GuiColorPanel() size
  2114. Color GuiColorPicker(Rectangle bounds, Color color)
  2115. {
  2116. color = GuiColorPanel(bounds, color);
  2117. Rectangle boundsHue = { (float)bounds.x + bounds.width + GuiGetStyle(COLORPICKER, HUEBAR_PADDING), (float)bounds.y, (float)GuiGetStyle(COLORPICKER, HUEBAR_WIDTH), (float)bounds.height };
  2118. //Rectangle boundsAlpha = { bounds.x, bounds.y + bounds.height + GuiGetStyle(COLORPICKER, BARS_PADDING), bounds.width, GuiGetStyle(COLORPICKER, BARS_THICK) };
  2119. Vector3 hsv = ConvertRGBtoHSV(RAYGUI_CLITERAL(Vector3){ color.r/255.0f, color.g/255.0f, color.b/255.0f });
  2120. hsv.x = GuiColorBarHue(boundsHue, hsv.x);
  2121. //color.a = (unsigned char)(GuiColorBarAlpha(boundsAlpha, (float)color.a/255.0f)*255.0f);
  2122. Vector3 rgb = ConvertHSVtoRGB(hsv);
  2123. color = RAYGUI_CLITERAL(Color){ (unsigned char)roundf(rgb.x*255.0f), (unsigned char)roundf(rgb.y*255.0f), (unsigned char)roundf(rgb.z*255.0f), color.a };
  2124. return color;
  2125. }
  2126. // Message Box control
  2127. int GuiMessageBox(Rectangle bounds, const char *title, const char *message, const char *buttons)
  2128. {
  2129. #define MESSAGEBOX_BUTTON_HEIGHT 24
  2130. #define MESSAGEBOX_BUTTON_PADDING 10
  2131. int clicked = -1; // Returns clicked button from buttons list, 0 refers to closed window button
  2132. int buttonsCount = 0;
  2133. const char **buttonsText = GuiTextSplit(buttons, &buttonsCount, NULL);
  2134. Rectangle buttonBounds = { 0 };
  2135. buttonBounds.x = bounds.x + MESSAGEBOX_BUTTON_PADDING;
  2136. buttonBounds.y = bounds.y + bounds.height - MESSAGEBOX_BUTTON_HEIGHT - MESSAGEBOX_BUTTON_PADDING;
  2137. buttonBounds.width = (bounds.width - MESSAGEBOX_BUTTON_PADDING*(buttonsCount + 1))/buttonsCount;
  2138. buttonBounds.height = MESSAGEBOX_BUTTON_HEIGHT;
  2139. Vector2 textSize = MeasureTextEx(guiFont, message, GuiGetStyle(DEFAULT, TEXT_SIZE), 1);
  2140. Rectangle textBounds = { 0 };
  2141. textBounds.x = bounds.x + bounds.width/2 - textSize.x/2;
  2142. textBounds.y = bounds.y + WINDOW_STATUSBAR_HEIGHT + (bounds.height - WINDOW_STATUSBAR_HEIGHT - MESSAGEBOX_BUTTON_HEIGHT - MESSAGEBOX_BUTTON_PADDING)/2 - textSize.y/2;
  2143. textBounds.width = textSize.x;
  2144. textBounds.height = textSize.y;
  2145. // Draw control
  2146. //--------------------------------------------------------------------
  2147. if (GuiWindowBox(bounds, title)) clicked = 0;
  2148. int prevTextAlignment = GuiGetStyle(LABEL, TEXT_ALIGNMENT);
  2149. GuiSetStyle(LABEL, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  2150. GuiLabel(textBounds, message);
  2151. GuiSetStyle(LABEL, TEXT_ALIGNMENT, prevTextAlignment);
  2152. prevTextAlignment = GuiGetStyle(BUTTON, TEXT_ALIGNMENT);
  2153. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  2154. for (int i = 0; i < buttonsCount; i++)
  2155. {
  2156. if (GuiButton(buttonBounds, buttonsText[i])) clicked = i + 1;
  2157. buttonBounds.x += (buttonBounds.width + MESSAGEBOX_BUTTON_PADDING);
  2158. }
  2159. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, prevTextAlignment);
  2160. //--------------------------------------------------------------------
  2161. return clicked;
  2162. }
  2163. // Text Input Box control, ask for text
  2164. int GuiTextInputBox(Rectangle bounds, const char *title, const char *message, const char *buttons, char *text)
  2165. {
  2166. #define TEXTINPUTBOX_BUTTON_HEIGHT 24
  2167. #define TEXTINPUTBOX_BUTTON_PADDING 10
  2168. #define TEXTINPUTBOX_HEIGHT 30
  2169. #define TEXTINPUTBOX_MAX_TEXT_LENGTH 256
  2170. // Used to enable text edit mode
  2171. // WARNING: No more than one GuiTextInputBox() should be open at the same time
  2172. static bool textEditMode = false;
  2173. int btnIndex = -1;
  2174. int buttonsCount = 0;
  2175. const char **buttonsText = GuiTextSplit(buttons, &buttonsCount, NULL);
  2176. Rectangle buttonBounds = { 0 };
  2177. buttonBounds.x = bounds.x + TEXTINPUTBOX_BUTTON_PADDING;
  2178. buttonBounds.y = bounds.y + bounds.height - TEXTINPUTBOX_BUTTON_HEIGHT - TEXTINPUTBOX_BUTTON_PADDING;
  2179. buttonBounds.width = (bounds.width - TEXTINPUTBOX_BUTTON_PADDING*(buttonsCount + 1))/buttonsCount;
  2180. buttonBounds.height = TEXTINPUTBOX_BUTTON_HEIGHT;
  2181. int messageInputHeight = bounds.height - WINDOW_STATUSBAR_HEIGHT - GuiGetStyle(STATUSBAR, BORDER_WIDTH) - TEXTINPUTBOX_BUTTON_HEIGHT - 2*TEXTINPUTBOX_BUTTON_PADDING;
  2182. Rectangle textBounds = { 0 };
  2183. if (message != NULL)
  2184. {
  2185. Vector2 textSize = MeasureTextEx(guiFont, message, GuiGetStyle(DEFAULT, TEXT_SIZE), 1);
  2186. textBounds.x = bounds.x + bounds.width/2 - textSize.x/2;
  2187. textBounds.y = bounds.y + WINDOW_STATUSBAR_HEIGHT + messageInputHeight/4 - textSize.y/2;
  2188. textBounds.width = textSize.x;
  2189. textBounds.height = textSize.y;
  2190. }
  2191. Rectangle textBoxBounds = { 0 };
  2192. textBoxBounds.x = bounds.x + TEXTINPUTBOX_BUTTON_PADDING;
  2193. textBoxBounds.y = bounds.y + WINDOW_STATUSBAR_HEIGHT - TEXTINPUTBOX_HEIGHT/2;
  2194. if (message == NULL) textBoxBounds.y += messageInputHeight/2;
  2195. else textBoxBounds.y += (messageInputHeight/2 + messageInputHeight/4);
  2196. textBoxBounds.width = bounds.width - TEXTINPUTBOX_BUTTON_PADDING*2;
  2197. textBoxBounds.height = TEXTINPUTBOX_HEIGHT;
  2198. // Draw control
  2199. //--------------------------------------------------------------------
  2200. if (GuiWindowBox(bounds, title)) btnIndex = 0;
  2201. // Draw message if available
  2202. if (message != NULL)
  2203. {
  2204. int prevTextAlignment = GuiGetStyle(LABEL, TEXT_ALIGNMENT);
  2205. GuiSetStyle(LABEL, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  2206. GuiLabel(textBounds, message);
  2207. GuiSetStyle(LABEL, TEXT_ALIGNMENT, prevTextAlignment);
  2208. }
  2209. if (GuiTextBox(textBoxBounds, text, TEXTINPUTBOX_MAX_TEXT_LENGTH, textEditMode)) textEditMode = !textEditMode;
  2210. int prevBtnTextAlignment = GuiGetStyle(BUTTON, TEXT_ALIGNMENT);
  2211. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  2212. for (int i = 0; i < buttonsCount; i++)
  2213. {
  2214. if (GuiButton(buttonBounds, buttonsText[i])) btnIndex = i + 1;
  2215. buttonBounds.x += (buttonBounds.width + MESSAGEBOX_BUTTON_PADDING);
  2216. }
  2217. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, prevBtnTextAlignment);
  2218. //--------------------------------------------------------------------
  2219. return btnIndex;
  2220. }
  2221. // Grid control
  2222. // NOTE: Returns grid mouse-hover selected cell
  2223. // About drawing lines at subpixel spacing, simple put, not easy solution:
  2224. // https://stackoverflow.com/questions/4435450/2d-opengl-drawing-lines-that-dont-exactly-fit-pixel-raster
  2225. Vector2 GuiGrid(Rectangle bounds, float spacing, int subdivs)
  2226. {
  2227. #if !defined(GRID_COLOR_ALPHA)
  2228. #define GRID_COLOR_ALPHA 0.15f // Grid lines alpha amount
  2229. #endif
  2230. GuiControlState state = guiState;
  2231. Vector2 mousePoint = GetMousePosition();
  2232. Vector2 currentCell = { -1, -1 };
  2233. int linesV = ((int)(bounds.width/spacing))*subdivs + 1;
  2234. int linesH = ((int)(bounds.height/spacing))*subdivs + 1;
  2235. // Update control
  2236. //--------------------------------------------------------------------
  2237. if ((state != GUI_STATE_DISABLED) && !guiLocked)
  2238. {
  2239. if (CheckCollisionPointRec(mousePoint, bounds))
  2240. {
  2241. currentCell.x = (int)((mousePoint.x - bounds.x)/spacing);
  2242. currentCell.y = (int)((mousePoint.y - bounds.y)/spacing);
  2243. }
  2244. }
  2245. //--------------------------------------------------------------------
  2246. // Draw control
  2247. //--------------------------------------------------------------------
  2248. switch (state)
  2249. {
  2250. case GUI_STATE_NORMAL:
  2251. {
  2252. if (subdivs > 0)
  2253. {
  2254. // Draw vertical grid lines
  2255. for (int i = 0; i < linesV; i++)
  2256. {
  2257. Rectangle lineV = { bounds.x + spacing * i / subdivs, bounds.y, 1, bounds.height };
  2258. GuiDrawRectangle(lineV, 0, BLANK, ((i%subdivs) == 0) ? Fade(GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)), GRID_COLOR_ALPHA * 4) : Fade(GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)), GRID_COLOR_ALPHA));
  2259. }
  2260. // Draw horizontal grid lines
  2261. for (int i = 0; i < linesH; i++)
  2262. {
  2263. Rectangle lineH = { bounds.x, bounds.y + spacing * i / subdivs, bounds.width, 1 };
  2264. GuiDrawRectangle(lineH, 0, BLANK, ((i%subdivs) == 0) ? Fade(GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)), GRID_COLOR_ALPHA * 4) : Fade(GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)), GRID_COLOR_ALPHA));
  2265. }
  2266. }
  2267. } break;
  2268. default: break;
  2269. }
  2270. return currentCell;
  2271. }
  2272. //----------------------------------------------------------------------------------
  2273. // Styles loading functions
  2274. //----------------------------------------------------------------------------------
  2275. // Load raygui style file (.rgs)
  2276. void GuiLoadStyle(const char *fileName)
  2277. {
  2278. bool tryBinary = false;
  2279. // Try reading the files as text file first
  2280. FILE *rgsFile = fopen(fileName, "rt");
  2281. if (rgsFile != NULL)
  2282. {
  2283. char buffer[256] = { 0 };
  2284. fgets(buffer, 256, rgsFile);
  2285. if (buffer[0] == '#')
  2286. {
  2287. int controlId = 0;
  2288. int propertyId = 0;
  2289. unsigned int propertyValue = 0;
  2290. while (!feof(rgsFile))
  2291. {
  2292. switch (buffer[0])
  2293. {
  2294. case 'p':
  2295. {
  2296. // Style property: p <control_id> <property_id> <property_value> <property_name>
  2297. sscanf(buffer, "p %d %d 0x%x", &controlId, &propertyId, &propertyValue);
  2298. GuiSetStyle(controlId, propertyId, (int)propertyValue);
  2299. } break;
  2300. case 'f':
  2301. {
  2302. // Style font: f <gen_font_size> <charmap_file> <font_file>
  2303. int fontSize = 0;
  2304. char charmapFileName[256] = { 0 };
  2305. char fontFileName[256] = { 0 };
  2306. sscanf(buffer, "f %d %s %[^\r\n]s", &fontSize, charmapFileName, fontFileName);
  2307. Font font = { 0 };
  2308. if (charmapFileName[0] != '0')
  2309. {
  2310. // Load characters from charmap file,
  2311. // expected '\n' separated list of integer values
  2312. char *charValues = LoadText(charmapFileName);
  2313. if (charValues != NULL)
  2314. {
  2315. int charsCount = 0;
  2316. const char **chars = TextSplit(charValues, '\n', &charsCount);
  2317. int *values = (int *)RAYGUI_MALLOC(charsCount*sizeof(int));
  2318. for (int i = 0; i < charsCount; i++) values[i] = TextToInteger(chars[i]);
  2319. font = LoadFontEx(TextFormat("%s/%s", GetDirectoryPath(fileName), fontFileName), fontSize, values, charsCount);
  2320. RAYGUI_FREE(values);
  2321. }
  2322. }
  2323. else font = LoadFontEx(TextFormat("%s/%s", GetDirectoryPath(fileName), fontFileName), fontSize, NULL, 0);
  2324. if ((font.texture.id > 0) && (font.charsCount > 0)) GuiSetFont(font);
  2325. } break;
  2326. default: break;
  2327. }
  2328. fgets(buffer, 256, rgsFile);
  2329. }
  2330. }
  2331. else tryBinary = true;
  2332. fclose(rgsFile);
  2333. }
  2334. if (tryBinary)
  2335. {
  2336. rgsFile = fopen(fileName, "rb");
  2337. if (rgsFile == NULL) return;
  2338. char signature[5] = "";
  2339. short version = 0;
  2340. short reserved = 0;
  2341. int propertiesCount = 0;
  2342. fread(signature, 1, 4, rgsFile);
  2343. fread(&version, 1, sizeof(short), rgsFile);
  2344. fread(&reserved, 1, sizeof(short), rgsFile);
  2345. fread(&propertiesCount, 1, sizeof(int), rgsFile);
  2346. if ((signature[0] == 'r') &&
  2347. (signature[1] == 'G') &&
  2348. (signature[2] == 'S') &&
  2349. (signature[3] == ' '))
  2350. {
  2351. short controlId = 0;
  2352. short propertyId = 0;
  2353. int propertyValue = 0;
  2354. for (int i = 0; i < propertiesCount; i++)
  2355. {
  2356. fread(&controlId, 1, sizeof(short), rgsFile);
  2357. fread(&propertyId, 1, sizeof(short), rgsFile);
  2358. fread(&propertyValue, 1, sizeof(int), rgsFile);
  2359. if (controlId == 0) // DEFAULT control
  2360. {
  2361. // If a DEFAULT property is loaded, it is propagated to all controls
  2362. // NOTE: All DEFAULT properties should be defined first in the file
  2363. GuiSetStyle(0, (int)propertyId, propertyValue);
  2364. if (propertyId < NUM_PROPS_DEFAULT) for (int i = 1; i < NUM_CONTROLS; i++) GuiSetStyle(i, (int)propertyId, propertyValue);
  2365. }
  2366. else GuiSetStyle((int)controlId, (int)propertyId, propertyValue);
  2367. }
  2368. // Font loading is highly dependant on raylib API to load font data and image
  2369. // TODO: Find some mechanism to support it in standalone mode
  2370. #if !defined(RAYGUI_STANDALONE)
  2371. // Load custom font if available
  2372. int fontDataSize = 0;
  2373. fread(&fontDataSize, 1, sizeof(int), rgsFile);
  2374. if (fontDataSize > 0)
  2375. {
  2376. Font font = { 0 };
  2377. int fontType = 0; // 0-Normal, 1-SDF
  2378. Rectangle whiteRec = { 0 };
  2379. fread(&font.baseSize, 1, sizeof(int), rgsFile);
  2380. fread(&font.charsCount, 1, sizeof(int), rgsFile);
  2381. fread(&fontType, 1, sizeof(int), rgsFile);
  2382. // Load font white rectangle
  2383. fread(&whiteRec, 1, sizeof(Rectangle), rgsFile);
  2384. // Load font image parameters
  2385. int fontImageSize = 0;
  2386. fread(&fontImageSize, 1, sizeof(int), rgsFile);
  2387. if (fontImageSize > 0)
  2388. {
  2389. Image imFont = { 0 };
  2390. imFont.mipmaps = 1;
  2391. fread(&imFont.width, 1, sizeof(int), rgsFile);
  2392. fread(&imFont.height, 1, sizeof(int), rgsFile);
  2393. fread(&imFont.format, 1, sizeof(int), rgsFile);
  2394. imFont.data = (unsigned char *)RAYGUI_MALLOC(fontImageSize);
  2395. fread(imFont.data, 1, fontImageSize, rgsFile);
  2396. font.texture = LoadTextureFromImage(imFont);
  2397. UnloadImage(imFont);
  2398. }
  2399. // Load font recs data
  2400. font.recs = (Rectangle *)RAYGUI_CALLOC(font.charsCount, sizeof(Rectangle));
  2401. for (int i = 0; i < font.charsCount; i++) fread(&font.recs[i], 1, sizeof(Rectangle), rgsFile);
  2402. // Load font chars info data
  2403. font.chars = (CharInfo *)RAYGUI_CALLOC(font.charsCount, sizeof(CharInfo));
  2404. for (int i = 0; i < font.charsCount; i++)
  2405. {
  2406. fread(&font.chars[i].value, 1, sizeof(int), rgsFile);
  2407. fread(&font.chars[i].offsetX, 1, sizeof(int), rgsFile);
  2408. fread(&font.chars[i].offsetY, 1, sizeof(int), rgsFile);
  2409. fread(&font.chars[i].advanceX, 1, sizeof(int), rgsFile);
  2410. }
  2411. GuiSetFont(font);
  2412. // Set font texture source rectangle to be used as white texture to draw shapes
  2413. // NOTE: This way, all gui can be draw using a single draw call
  2414. if ((whiteRec.width != 0) && (whiteRec.height != 0)) SetShapesTexture(font.texture, whiteRec);
  2415. }
  2416. #endif
  2417. }
  2418. fclose(rgsFile);
  2419. }
  2420. }
  2421. // Load style default over global style
  2422. void GuiLoadStyleDefault(void)
  2423. {
  2424. // We set this variable first to avoid cyclic function calls
  2425. // when calling GuiSetStyle() and GuiGetStyle()
  2426. guiStyleLoaded = true;
  2427. // Initialize default LIGHT style property values
  2428. GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, 0x838383ff);
  2429. GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, 0xc9c9c9ff);
  2430. GuiSetStyle(DEFAULT, TEXT_COLOR_NORMAL, 0x686868ff);
  2431. GuiSetStyle(DEFAULT, BORDER_COLOR_FOCUSED, 0x5bb2d9ff);
  2432. GuiSetStyle(DEFAULT, BASE_COLOR_FOCUSED, 0xc9effeff);
  2433. GuiSetStyle(DEFAULT, TEXT_COLOR_FOCUSED, 0x6c9bbcff);
  2434. GuiSetStyle(DEFAULT, BORDER_COLOR_PRESSED, 0x0492c7ff);
  2435. GuiSetStyle(DEFAULT, BASE_COLOR_PRESSED, 0x97e8ffff);
  2436. GuiSetStyle(DEFAULT, TEXT_COLOR_PRESSED, 0x368bafff);
  2437. GuiSetStyle(DEFAULT, BORDER_COLOR_DISABLED, 0xb5c1c2ff);
  2438. GuiSetStyle(DEFAULT, BASE_COLOR_DISABLED, 0xe6e9e9ff);
  2439. GuiSetStyle(DEFAULT, TEXT_COLOR_DISABLED, 0xaeb7b8ff);
  2440. GuiSetStyle(DEFAULT, BORDER_WIDTH, 1); // WARNING: Some controls use other values
  2441. GuiSetStyle(DEFAULT, TEXT_PADDING, 0); // WARNING: Some controls use other values
  2442. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER); // WARNING: Some controls use other values
  2443. // Initialize control-specific property values
  2444. // NOTE: Those properties are in default list but require specific values by control type
  2445. GuiSetStyle(LABEL, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  2446. GuiSetStyle(BUTTON, BORDER_WIDTH, 2);
  2447. GuiSetStyle(SLIDER, TEXT_PADDING, 5);
  2448. GuiSetStyle(CHECKBOX, TEXT_PADDING, 5);
  2449. GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_RIGHT);
  2450. GuiSetStyle(TEXTBOX, TEXT_PADDING, 5);
  2451. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  2452. GuiSetStyle(VALUEBOX, TEXT_PADDING, 4);
  2453. GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  2454. GuiSetStyle(SPINNER, TEXT_PADDING, 4);
  2455. GuiSetStyle(SPINNER, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  2456. GuiSetStyle(STATUSBAR, TEXT_PADDING, 6);
  2457. GuiSetStyle(STATUSBAR, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  2458. // Initialize extended property values
  2459. // NOTE: By default, extended property values are initialized to 0
  2460. GuiSetStyle(DEFAULT, TEXT_SIZE, 10); // DEFAULT, shared by all controls
  2461. GuiSetStyle(DEFAULT, TEXT_SPACING, 1); // DEFAULT, shared by all controls
  2462. GuiSetStyle(DEFAULT, LINE_COLOR, 0x90abb5ff); // DEFAULT specific property
  2463. GuiSetStyle(DEFAULT, BACKGROUND_COLOR, 0xf5f5f5ff); // DEFAULT specific property
  2464. GuiSetStyle(TOGGLE, GROUP_PADDING, 2);
  2465. GuiSetStyle(SLIDER, SLIDER_WIDTH, 15);
  2466. GuiSetStyle(SLIDER, SLIDER_PADDING, 1);
  2467. GuiSetStyle(PROGRESSBAR, PROGRESS_PADDING, 1);
  2468. GuiSetStyle(CHECKBOX, CHECK_PADDING, 1);
  2469. GuiSetStyle(COMBOBOX, COMBO_BUTTON_WIDTH, 30);
  2470. GuiSetStyle(COMBOBOX, COMBO_BUTTON_PADDING, 2);
  2471. GuiSetStyle(DROPDOWNBOX, ARROW_PADDING, 16);
  2472. GuiSetStyle(DROPDOWNBOX, DROPDOWN_ITEMS_PADDING, 2);
  2473. GuiSetStyle(TEXTBOX, TEXT_LINES_PADDING, 5);
  2474. GuiSetStyle(TEXTBOX, TEXT_INNER_PADDING, 4);
  2475. GuiSetStyle(TEXTBOX, COLOR_SELECTED_FG, 0xf0fffeff);
  2476. GuiSetStyle(TEXTBOX, COLOR_SELECTED_BG, 0x839affe0);
  2477. GuiSetStyle(SPINNER, SPIN_BUTTON_WIDTH, 20);
  2478. GuiSetStyle(SPINNER, SPIN_BUTTON_PADDING, 2);
  2479. GuiSetStyle(SCROLLBAR, BORDER_WIDTH, 0);
  2480. GuiSetStyle(SCROLLBAR, ARROWS_VISIBLE, 0);
  2481. GuiSetStyle(SCROLLBAR, ARROWS_SIZE, 6);
  2482. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_PADDING, 0);
  2483. GuiSetStyle(SCROLLBAR, SCROLL_SLIDER_SIZE, 16);
  2484. GuiSetStyle(SCROLLBAR, SCROLL_PADDING, 0);
  2485. GuiSetStyle(SCROLLBAR, SCROLL_SPEED, 10);
  2486. GuiSetStyle(LISTVIEW, LIST_ITEMS_HEIGHT, 0x1e);
  2487. GuiSetStyle(LISTVIEW, LIST_ITEMS_PADDING, 2);
  2488. GuiSetStyle(LISTVIEW, SCROLLBAR_WIDTH, 10);
  2489. GuiSetStyle(LISTVIEW, SCROLLBAR_SIDE, SCROLLBAR_RIGHT_SIDE);
  2490. GuiSetStyle(COLORPICKER, COLOR_SELECTOR_SIZE, 6);
  2491. GuiSetStyle(COLORPICKER, HUEBAR_WIDTH, 0x14);
  2492. GuiSetStyle(COLORPICKER, HUEBAR_PADDING, 0xa);
  2493. GuiSetStyle(COLORPICKER, HUEBAR_SELECTOR_HEIGHT, 6);
  2494. GuiSetStyle(COLORPICKER, HUEBAR_SELECTOR_OVERFLOW, 2);
  2495. guiFont = GetFontDefault(); // Initialize default font
  2496. }
  2497. // Get text with icon id prepended
  2498. // NOTE: Useful to add icons by name id (enum) instead of
  2499. // a number that can change between ricon versions
  2500. const char *GuiIconText(int iconId, const char *text)
  2501. {
  2502. #if defined(RAYGUI_SUPPORT_ICONS)
  2503. static char buffer[1024] = { 0 };
  2504. memset(buffer, 0, 1024);
  2505. sprintf(buffer, "#%03i#", iconId);
  2506. if (text != NULL)
  2507. {
  2508. for (int i = 5; i < 1024; i++)
  2509. {
  2510. buffer[i] = text[i - 5];
  2511. if (text[i - 5] == '\0') break;
  2512. }
  2513. }
  2514. return buffer;
  2515. #else
  2516. return NULL;
  2517. #endif
  2518. }
  2519. #if defined(RAYGUI_SUPPORT_ICONS)
  2520. // Get full icons data pointer
  2521. unsigned int *GuiGetIcons(void) { return guiIcons; }
  2522. // Load raygui icons file (.rgi)
  2523. // NOTE: In case nameIds are required, they can be requested with loadIconsName,
  2524. // they are returned as a guiIconsName[iconsCount][RICON_MAX_NAME_LENGTH],
  2525. // guiIconsName[]][] memory should be manually freed!
  2526. char **GuiLoadIcons(const char *fileName, bool loadIconsName)
  2527. {
  2528. // Style File Structure (.rgi)
  2529. // ------------------------------------------------------
  2530. // Offset | Size | Type | Description
  2531. // ------------------------------------------------------
  2532. // 0 | 4 | char | Signature: "rGI "
  2533. // 4 | 2 | short | Version: 100
  2534. // 6 | 2 | short | reserved
  2535. // 8 | 2 | short | Num icons (N)
  2536. // 10 | 2 | short | Icons size (Options: 16, 32, 64) (S)
  2537. // Icons name id (32 bytes per name id)
  2538. // foreach (icon)
  2539. // {
  2540. // 12+32*i | 32 | char | Icon NameId
  2541. // }
  2542. // Icons data: One bit per pixel, stored as unsigned int array (depends on icon size)
  2543. // S*S pixels/32bit per unsigned int = K unsigned int per icon
  2544. // foreach (icon)
  2545. // {
  2546. // ... | K | unsigned int | Icon Data
  2547. // }
  2548. FILE *rgiFile = fopen(fileName, "rb");
  2549. char **guiIconsName = NULL;
  2550. if (rgiFile != NULL)
  2551. {
  2552. char signature[5] = "";
  2553. short version = 0;
  2554. short reserved = 0;
  2555. short iconsCount = 0;
  2556. short iconsSize = 0;
  2557. fread(signature, 1, 4, rgiFile);
  2558. fread(&version, 1, sizeof(short), rgiFile);
  2559. fread(&reserved, 1, sizeof(short), rgiFile);
  2560. fread(&iconsCount, 1, sizeof(short), rgiFile);
  2561. fread(&iconsSize, 1, sizeof(short), rgiFile);
  2562. if ((signature[0] == 'r') &&
  2563. (signature[1] == 'G') &&
  2564. (signature[2] == 'I') &&
  2565. (signature[3] == ' '))
  2566. {
  2567. if (loadIconsName)
  2568. {
  2569. guiIconsName = (char **)RAYGUI_MALLOC(iconsCount*sizeof(char **));
  2570. for (int i = 0; i < iconsCount; i++)
  2571. {
  2572. guiIconsName[i] = (char *)RAYGUI_MALLOC(RICON_MAX_NAME_LENGTH);
  2573. fread(guiIconsName[i], 32, 1, rgiFile);
  2574. }
  2575. }
  2576. // Read icons data directly over guiIcons data array
  2577. fread(guiIcons, iconsCount*(iconsSize*iconsSize/32), sizeof(unsigned int), rgiFile);
  2578. }
  2579. fclose(rgiFile);
  2580. }
  2581. return guiIconsName;
  2582. }
  2583. // Draw selected icon using rectangles pixel-by-pixel
  2584. void GuiDrawIcon(int iconId, Vector2 position, int pixelSize, Color color)
  2585. {
  2586. #define BIT_CHECK(a,b) ((a) & (1<<(b)))
  2587. for (int i = 0, y = 0; i < RICON_SIZE*RICON_SIZE/32; i++)
  2588. {
  2589. for (int k = 0; k < 32; k++)
  2590. {
  2591. if (BIT_CHECK(guiIcons[iconId*RICON_DATA_ELEMENTS + i], k))
  2592. {
  2593. #if !defined(RAYGUI_STANDALONE)
  2594. DrawRectangle(position.x + (k%RICON_SIZE)*pixelSize, position.y + y*pixelSize, pixelSize, pixelSize, color);
  2595. #endif
  2596. }
  2597. if ((k == 15) || (k == 31)) y++;
  2598. }
  2599. }
  2600. }
  2601. // Get icon bit data
  2602. // NOTE: Bit data array grouped as unsigned int (ICON_SIZE*ICON_SIZE/32 elements)
  2603. unsigned int *GuiGetIconData(int iconId)
  2604. {
  2605. static unsigned int iconData[RICON_DATA_ELEMENTS] = { 0 };
  2606. memset(iconData, 0, RICON_DATA_ELEMENTS*sizeof(unsigned int));
  2607. if (iconId < RICON_MAX_ICONS) memcpy(iconData, &guiIcons[iconId*RICON_DATA_ELEMENTS], RICON_DATA_ELEMENTS*sizeof(unsigned int));
  2608. return iconData;
  2609. }
  2610. // Set icon bit data
  2611. // NOTE: Data must be provided as unsigned int array (ICON_SIZE*ICON_SIZE/32 elements)
  2612. void GuiSetIconData(int iconId, unsigned int *data)
  2613. {
  2614. if (iconId < RICON_MAX_ICONS) memcpy(&guiIcons[iconId*RICON_DATA_ELEMENTS], data, RICON_DATA_ELEMENTS*sizeof(unsigned int));
  2615. }
  2616. // Set icon pixel value
  2617. void GuiSetIconPixel(int iconId, int x, int y)
  2618. {
  2619. #define BIT_SET(a,b) ((a) |= (1<<(b)))
  2620. // This logic works for any RICON_SIZE pixels icons,
  2621. // For example, in case of 16x16 pixels, every 2 lines fit in one unsigned int data element
  2622. BIT_SET(guiIcons[iconId*RICON_DATA_ELEMENTS + y/(sizeof(unsigned int)*8/RICON_SIZE)], x + (y%(sizeof(unsigned int)*8/RICON_SIZE)*RICON_SIZE));
  2623. }
  2624. // Clear icon pixel value
  2625. void GuiClearIconPixel(int iconId, int x, int y)
  2626. {
  2627. #define BIT_CLEAR(a,b) ((a) &= ~((1)<<(b)))
  2628. // This logic works for any RICON_SIZE pixels icons,
  2629. // For example, in case of 16x16 pixels, every 2 lines fit in one unsigned int data element
  2630. BIT_CLEAR(guiIcons[iconId*RICON_DATA_ELEMENTS + y/(sizeof(unsigned int)*8/RICON_SIZE)], x + (y%(sizeof(unsigned int)*8/RICON_SIZE)*RICON_SIZE));
  2631. }
  2632. // Check icon pixel value
  2633. bool GuiCheckIconPixel(int iconId, int x, int y)
  2634. {
  2635. #define BIT_CHECK(a,b) ((a) & (1<<(b)))
  2636. return (BIT_CHECK(guiIcons[iconId*8 + y/2], x + (y%2*16)));
  2637. }
  2638. #endif // RAYGUI_SUPPORT_ICONS
  2639. //----------------------------------------------------------------------------------
  2640. // Module specific Functions Definition
  2641. //----------------------------------------------------------------------------------
  2642. // Gui get text width using default font
  2643. static int GetTextWidth(const char *text)
  2644. {
  2645. Vector2 size = { 0 };
  2646. if ((text != NULL) && (text[0] != '\0')) size = MeasureTextEx(guiFont, text, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING));
  2647. // TODO: Consider text icon width here???
  2648. return (int)size.x;
  2649. }
  2650. // Get text bounds considering control bounds
  2651. static Rectangle GetTextBounds(int control, Rectangle bounds)
  2652. {
  2653. Rectangle textBounds = bounds;
  2654. textBounds.x = bounds.x + GuiGetStyle(control, BORDER_WIDTH);
  2655. textBounds.y = bounds.y + GuiGetStyle(control, BORDER_WIDTH);
  2656. textBounds.width = bounds.width - 2*GuiGetStyle(control, BORDER_WIDTH);
  2657. textBounds.height = bounds.height - 2*GuiGetStyle(control, BORDER_WIDTH);
  2658. // Consider TEXT_PADDING properly, depends on control type and TEXT_ALIGNMENT
  2659. switch (control)
  2660. {
  2661. case COMBOBOX: bounds.width -= (GuiGetStyle(control, COMBO_BUTTON_WIDTH) + GuiGetStyle(control, COMBO_BUTTON_PADDING)); break;
  2662. case VALUEBOX: break; // NOTE: ValueBox text value always centered, text padding applies to label
  2663. default:
  2664. {
  2665. if (GuiGetStyle(control, TEXT_ALIGNMENT) == GUI_TEXT_ALIGN_RIGHT) textBounds.x -= GuiGetStyle(control, TEXT_PADDING);
  2666. else textBounds.x += GuiGetStyle(control, TEXT_PADDING);
  2667. } break;
  2668. }
  2669. // TODO: Special cases (no label): COMBOBOX, DROPDOWNBOX, LISTVIEW (scrollbar?)
  2670. // More special cases (label side): CHECKBOX, SLIDER, VALUEBOX, SPINNER
  2671. return textBounds;
  2672. }
  2673. // Get text icon if provided and move text cursor
  2674. // NOTE: We support up to 999 values for iconId
  2675. static const char *GetTextIcon(const char *text, int *iconId)
  2676. {
  2677. #if defined(RAYGUI_SUPPORT_ICONS)
  2678. *iconId = -1;
  2679. if (text[0] == '#') // Maybe we have an icon!
  2680. {
  2681. char iconValue[4] = { 0 }; // Maximum length for icon value: 3 digits + '\0'
  2682. int pos = 1;
  2683. while ((pos < 4) && (text[pos] >= '0') && (text[pos] <= '9'))
  2684. {
  2685. iconValue[pos - 1] = text[pos];
  2686. pos++;
  2687. }
  2688. if (text[pos] == '#')
  2689. {
  2690. *iconId = TextToInteger(iconValue);
  2691. // Move text pointer after icon
  2692. // WARNING: If only icon provided, it could point to EOL character!
  2693. if (*iconId >= 0) text += (pos + 1);
  2694. }
  2695. }
  2696. #endif
  2697. return text;
  2698. }
  2699. // Gui draw text using default font
  2700. static void GuiDrawText(const char *text, Rectangle bounds, int alignment, Color tint)
  2701. {
  2702. #define TEXT_VALIGN_PIXEL_OFFSET(h) ((int)h%2) // Vertical alignment for pixel perfect
  2703. if ((text != NULL) && (text[0] != '\0'))
  2704. {
  2705. int iconId = 0;
  2706. text = GetTextIcon(text, &iconId); // Check text for icon and move cursor
  2707. // Get text position depending on alignment and iconId
  2708. //---------------------------------------------------------------------------------
  2709. #define ICON_TEXT_PADDING 4
  2710. Vector2 position = { bounds.x, bounds.y };
  2711. // NOTE: We get text size after icon been processed
  2712. int textWidth = GetTextWidth(text);
  2713. int textHeight = GuiGetStyle(DEFAULT, TEXT_SIZE);
  2714. #if defined(RAYGUI_SUPPORT_ICONS)
  2715. if (iconId >= 0)
  2716. {
  2717. textWidth += RICON_SIZE;
  2718. // WARNING: If only icon provided, text could be pointing to eof character!
  2719. if ((text != NULL) && (text[0] != '\0')) textWidth += ICON_TEXT_PADDING;
  2720. }
  2721. #endif
  2722. // Check guiTextAlign global variables
  2723. switch (alignment)
  2724. {
  2725. case GUI_TEXT_ALIGN_LEFT:
  2726. {
  2727. position.x = bounds.x;
  2728. position.y = bounds.y + bounds.height/2 - textHeight/2 + TEXT_VALIGN_PIXEL_OFFSET(bounds.height);
  2729. } break;
  2730. case GUI_TEXT_ALIGN_CENTER:
  2731. {
  2732. position.x = bounds.x + bounds.width/2 - textWidth/2;
  2733. position.y = bounds.y + bounds.height/2 - textHeight/2 + TEXT_VALIGN_PIXEL_OFFSET(bounds.height);
  2734. } break;
  2735. case GUI_TEXT_ALIGN_RIGHT:
  2736. {
  2737. position.x = bounds.x + bounds.width - textWidth;
  2738. position.y = bounds.y + bounds.height/2 - textHeight/2 + TEXT_VALIGN_PIXEL_OFFSET(bounds.height);
  2739. } break;
  2740. default: break;
  2741. }
  2742. // NOTE: Make sure we get pixel-perfect coordinates,
  2743. // In case of decimals we got weird text positioning
  2744. position.x = (float)((int)position.x);
  2745. position.y = (float)((int)position.y);
  2746. //---------------------------------------------------------------------------------
  2747. // Draw text (with icon if available)
  2748. //---------------------------------------------------------------------------------
  2749. #if defined(RAYGUI_SUPPORT_ICONS)
  2750. if (iconId >= 0)
  2751. {
  2752. // NOTE: We consider icon height, probably different than text size
  2753. GuiDrawIcon(iconId, RAYGUI_CLITERAL(Vector2){ position.x, bounds.y + bounds.height/2 - RICON_SIZE/2 + TEXT_VALIGN_PIXEL_OFFSET(bounds.height) }, 1, tint);
  2754. position.x += (RICON_SIZE + ICON_TEXT_PADDING);
  2755. }
  2756. #endif
  2757. DrawTextEx(guiFont, text, position, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING), tint);
  2758. //---------------------------------------------------------------------------------
  2759. }
  2760. }
  2761. // Gui draw rectangle using default raygui plain style with borders
  2762. static void GuiDrawRectangle(Rectangle rec, int borderWidth, Color borderColor, Color color)
  2763. {
  2764. if (color.a > 0)
  2765. {
  2766. // Draw rectangle filled with color
  2767. DrawRectangle(rec.x, rec.y, rec.width, rec.height, color);
  2768. }
  2769. if (borderWidth > 0)
  2770. {
  2771. // Draw rectangle border lines with color
  2772. DrawRectangle(rec.x, rec.y, rec.width, borderWidth, borderColor);
  2773. DrawRectangle(rec.x, rec.y + borderWidth, borderWidth, rec.height - 2*borderWidth, borderColor);
  2774. DrawRectangle(rec.x + rec.width - borderWidth, rec.y + borderWidth, borderWidth, rec.height - 2*borderWidth, borderColor);
  2775. DrawRectangle(rec.x, rec.y + rec.height - borderWidth, rec.width, borderWidth, borderColor);
  2776. }
  2777. // TODO: For n-patch-based style we would need: [state] and maybe [control]
  2778. // In this case all controls drawing logic should be moved to this function... I don't like it...
  2779. }
  2780. // Draw tooltip relatively to bounds
  2781. static void GuiDrawTooltip(Rectangle bounds)
  2782. {
  2783. //static int tooltipFramesCounter = 0; // Not possible gets reseted at second function call!
  2784. if (guiTooltipEnabled && (guiTooltip != NULL) && CheckCollisionPointRec(GetMousePosition(), bounds))
  2785. {
  2786. Vector2 mousePosition = GetMousePosition();
  2787. Vector2 textSize = MeasureTextEx(guiFont, guiTooltip, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING));
  2788. Rectangle tooltipBounds = { mousePosition.x, mousePosition.y, textSize.x + 20, textSize.y*2 };
  2789. GuiDrawRectangle(tooltipBounds, 1, Fade(GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)), guiAlpha), Fade(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)), guiAlpha));
  2790. tooltipBounds.x += 10;
  2791. GuiLabel(tooltipBounds, guiTooltip);
  2792. }
  2793. }
  2794. // Split controls text into multiple strings
  2795. // Also check for multiple columns (required by GuiToggleGroup())
  2796. static const char **GuiTextSplit(const char *text, int *count, int *textRow)
  2797. {
  2798. // NOTE: Current implementation returns a copy of the provided string with '\0' (string end delimiter)
  2799. // inserted between strings defined by "delimiter" parameter. No memory is dynamically allocated,
  2800. // all used memory is static... it has some limitations:
  2801. // 1. Maximum number of possible split strings is set by TEXTSPLIT_MAX_TEXT_ELEMENTS
  2802. // 2. Maximum size of text to split is TEXTSPLIT_MAX_TEXT_LENGTH
  2803. // NOTE: Those definitions could be externally provided if required
  2804. #if !defined(TEXTSPLIT_MAX_TEXT_LENGTH)
  2805. #define TEXTSPLIT_MAX_TEXT_LENGTH 1024
  2806. #endif
  2807. #if !defined(TEXTSPLIT_MAX_TEXT_ELEMENTS)
  2808. #define TEXTSPLIT_MAX_TEXT_ELEMENTS 128
  2809. #endif
  2810. static const char *result[TEXTSPLIT_MAX_TEXT_ELEMENTS] = { NULL };
  2811. static char buffer[TEXTSPLIT_MAX_TEXT_LENGTH] = { 0 };
  2812. memset(buffer, 0, TEXTSPLIT_MAX_TEXT_LENGTH);
  2813. result[0] = buffer;
  2814. int counter = 1;
  2815. if (textRow != NULL) textRow[0] = 0;
  2816. // Count how many substrings we have on text and point to every one
  2817. for (int i = 0; i < TEXTSPLIT_MAX_TEXT_LENGTH; i++)
  2818. {
  2819. buffer[i] = text[i];
  2820. if (buffer[i] == '\0') break;
  2821. else if ((buffer[i] == ';') || (buffer[i] == '\n'))
  2822. {
  2823. result[counter] = buffer + i + 1;
  2824. if (textRow != NULL)
  2825. {
  2826. if (buffer[i] == '\n') textRow[counter] = textRow[counter - 1] + 1;
  2827. else textRow[counter] = textRow[counter - 1];
  2828. }
  2829. buffer[i] = '\0'; // Set an end of string at this point
  2830. counter++;
  2831. if (counter == TEXTSPLIT_MAX_TEXT_ELEMENTS) break;
  2832. }
  2833. }
  2834. *count = counter;
  2835. return result;
  2836. }
  2837. // Convert color data from RGB to HSV
  2838. // NOTE: Color data should be passed normalized
  2839. static Vector3 ConvertRGBtoHSV(Vector3 rgb)
  2840. {
  2841. Vector3 hsv = { 0 };
  2842. float min = 0.0f;
  2843. float max = 0.0f;
  2844. float delta = 0.0f;
  2845. min = (rgb.x < rgb.y)? rgb.x : rgb.y;
  2846. min = (min < rgb.z)? min : rgb.z;
  2847. max = (rgb.x > rgb.y)? rgb.x : rgb.y;
  2848. max = (max > rgb.z)? max : rgb.z;
  2849. hsv.z = max; // Value
  2850. delta = max - min;
  2851. if (delta < 0.00001f)
  2852. {
  2853. hsv.y = 0.0f;
  2854. hsv.x = 0.0f; // Undefined, maybe NAN?
  2855. return hsv;
  2856. }
  2857. if (max > 0.0f)
  2858. {
  2859. // NOTE: If max is 0, this divide would cause a crash
  2860. hsv.y = (delta/max); // Saturation
  2861. }
  2862. else
  2863. {
  2864. // NOTE: If max is 0, then r = g = b = 0, s = 0, h is undefined
  2865. hsv.y = 0.0f;
  2866. hsv.x = 0.0f; // Undefined, maybe NAN?
  2867. return hsv;
  2868. }
  2869. // NOTE: Comparing float values could not work properly
  2870. if (rgb.x >= max) hsv.x = (rgb.y - rgb.z)/delta; // Between yellow & magenta
  2871. else
  2872. {
  2873. if (rgb.y >= max) hsv.x = 2.0f + (rgb.z - rgb.x)/delta; // Between cyan & yellow
  2874. else hsv.x = 4.0f + (rgb.x - rgb.y)/delta; // Between magenta & cyan
  2875. }
  2876. hsv.x *= 60.0f; // Convert to degrees
  2877. if (hsv.x < 0.0f) hsv.x += 360.0f;
  2878. return hsv;
  2879. }
  2880. // Convert color data from HSV to RGB
  2881. // NOTE: Color data should be passed normalized
  2882. static Vector3 ConvertHSVtoRGB(Vector3 hsv)
  2883. {
  2884. Vector3 rgb = { 0 };
  2885. float hh = 0.0f, p = 0.0f, q = 0.0f, t = 0.0f, ff = 0.0f;
  2886. long i = 0;
  2887. // NOTE: Comparing float values could not work properly
  2888. if (hsv.y <= 0.0f)
  2889. {
  2890. rgb.x = hsv.z;
  2891. rgb.y = hsv.z;
  2892. rgb.z = hsv.z;
  2893. return rgb;
  2894. }
  2895. hh = hsv.x;
  2896. if (hh >= 360.0f) hh = 0.0f;
  2897. hh /= 60.0f;
  2898. i = (long)hh;
  2899. ff = hh - i;
  2900. p = hsv.z*(1.0f - hsv.y);
  2901. q = hsv.z*(1.0f - (hsv.y*ff));
  2902. t = hsv.z*(1.0f - (hsv.y*(1.0f - ff)));
  2903. switch (i)
  2904. {
  2905. case 0:
  2906. {
  2907. rgb.x = hsv.z;
  2908. rgb.y = t;
  2909. rgb.z = p;
  2910. } break;
  2911. case 1:
  2912. {
  2913. rgb.x = q;
  2914. rgb.y = hsv.z;
  2915. rgb.z = p;
  2916. } break;
  2917. case 2:
  2918. {
  2919. rgb.x = p;
  2920. rgb.y = hsv.z;
  2921. rgb.z = t;
  2922. } break;
  2923. case 3:
  2924. {
  2925. rgb.x = p;
  2926. rgb.y = q;
  2927. rgb.z = hsv.z;
  2928. } break;
  2929. case 4:
  2930. {
  2931. rgb.x = t;
  2932. rgb.y = p;
  2933. rgb.z = hsv.z;
  2934. } break;
  2935. case 5:
  2936. default:
  2937. {
  2938. rgb.x = hsv.z;
  2939. rgb.y = p;
  2940. rgb.z = q;
  2941. } break;
  2942. }
  2943. return rgb;
  2944. }
  2945. #if defined(RAYGUI_STANDALONE)
  2946. // Returns a Color struct from hexadecimal value
  2947. static Color GetColor(int hexValue)
  2948. {
  2949. Color color;
  2950. color.r = (unsigned char)(hexValue >> 24) & 0xFF;
  2951. color.g = (unsigned char)(hexValue >> 16) & 0xFF;
  2952. color.b = (unsigned char)(hexValue >> 8) & 0xFF;
  2953. color.a = (unsigned char)hexValue & 0xFF;
  2954. return color;
  2955. }
  2956. // Returns hexadecimal value for a Color
  2957. static int ColorToInt(Color color)
  2958. {
  2959. return (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) | (int)color.a);
  2960. }
  2961. // Check if point is inside rectangle
  2962. static bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
  2963. {
  2964. bool collision = false;
  2965. if ((point.x >= rec.x) && (point.x <= (rec.x + rec.width)) &&
  2966. (point.y >= rec.y) && (point.y <= (rec.y + rec.height))) collision = true;
  2967. return collision;
  2968. }
  2969. // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
  2970. static Color Fade(Color color, float alpha)
  2971. {
  2972. if (alpha < 0.0f) alpha = 0.0f;
  2973. else if (alpha > 1.0f) alpha = 1.0f;
  2974. Color result = { color.r, color.g, color.b, (unsigned char)(255.0f*alpha) };
  2975. return result;
  2976. }
  2977. // Formatting of text with variables to 'embed'
  2978. static const char *TextFormat(const char *text, ...)
  2979. {
  2980. #define MAX_FORMATTEXT_LENGTH 64
  2981. static char buffer[MAX_FORMATTEXT_LENGTH];
  2982. va_list args;
  2983. va_start(args, text);
  2984. vsprintf(buffer, text, args);
  2985. va_end(args);
  2986. return buffer;
  2987. }
  2988. // Draw rectangle with vertical gradient fill color
  2989. // NOTE: This function is only used by GuiColorPicker()
  2990. static void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2)
  2991. {
  2992. Rectangle bounds = { (float)posX, (float)posY, (float)width, (float)height };
  2993. DrawRectangleGradientEx(bounds, color1, color2, color2, color1);
  2994. }
  2995. #define TEXTSPLIT_MAX_TEXT_BUFFER_LENGTH 1024 // Size of static buffer: TextSplit()
  2996. #define TEXTSPLIT_MAX_SUBSTRINGS_COUNT 128 // Size of static pointers array: TextSplit()
  2997. // Split string into multiple strings
  2998. const char **TextSplit(const char *text, char delimiter, int *count)
  2999. {
  3000. // NOTE: Current implementation returns a copy of the provided string with '\0' (string end delimiter)
  3001. // inserted between strings defined by "delimiter" parameter. No memory is dynamically allocated,
  3002. // all used memory is static... it has some limitations:
  3003. // 1. Maximum number of possible split strings is set by TEXTSPLIT_MAX_SUBSTRINGS_COUNT
  3004. // 2. Maximum size of text to split is TEXTSPLIT_MAX_TEXT_BUFFER_LENGTH
  3005. static const char *result[TEXTSPLIT_MAX_SUBSTRINGS_COUNT] = { NULL };
  3006. static char buffer[TEXTSPLIT_MAX_TEXT_BUFFER_LENGTH] = { 0 };
  3007. memset(buffer, 0, TEXTSPLIT_MAX_TEXT_BUFFER_LENGTH);
  3008. result[0] = buffer;
  3009. int counter = 0;
  3010. if (text != NULL)
  3011. {
  3012. counter = 1;
  3013. // Count how many substrings we have on text and point to every one
  3014. for (int i = 0; i < TEXTSPLIT_MAX_TEXT_BUFFER_LENGTH; i++)
  3015. {
  3016. buffer[i] = text[i];
  3017. if (buffer[i] == '\0') break;
  3018. else if (buffer[i] == delimiter)
  3019. {
  3020. buffer[i] = '\0'; // Set an end of string at this point
  3021. result[counter] = buffer + i + 1;
  3022. counter++;
  3023. if (counter == TEXTSPLIT_MAX_SUBSTRINGS_COUNT) break;
  3024. }
  3025. }
  3026. }
  3027. *count = counter;
  3028. return result;
  3029. }
  3030. // Get integer value from text
  3031. // NOTE: This function replaces atoi() [stdlib.h]
  3032. static int TextToInteger(const char *text)
  3033. {
  3034. int value = 0;
  3035. int sign = 1;
  3036. if ((text[0] == '+') || (text[0] == '-'))
  3037. {
  3038. if (text[0] == '-') sign = -1;
  3039. text++;
  3040. }
  3041. for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); ++i) value = value*10 + (int)(text[i] - '0');
  3042. return value*sign;
  3043. }
  3044. // Encode codepoint into utf8 text (char array length returned as parameter)
  3045. static const char *CodepointToUtf8(int codepoint, int *byteLength)
  3046. {
  3047. static char utf8[6] = { 0 };
  3048. int length = 0;
  3049. if (codepoint <= 0x7f)
  3050. {
  3051. utf8[0] = (char)codepoint;
  3052. length = 1;
  3053. }
  3054. else if (codepoint <= 0x7ff)
  3055. {
  3056. utf8[0] = (char)(((codepoint >> 6) & 0x1f) | 0xc0);
  3057. utf8[1] = (char)((codepoint & 0x3f) | 0x80);
  3058. length = 2;
  3059. }
  3060. else if (codepoint <= 0xffff)
  3061. {
  3062. utf8[0] = (char)(((codepoint >> 12) & 0x0f) | 0xe0);
  3063. utf8[1] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
  3064. utf8[2] = (char)((codepoint & 0x3f) | 0x80);
  3065. length = 3;
  3066. }
  3067. else if (codepoint <= 0x10ffff)
  3068. {
  3069. utf8[0] = (char)(((codepoint >> 18) & 0x07) | 0xf0);
  3070. utf8[1] = (char)(((codepoint >> 12) & 0x3f) | 0x80);
  3071. utf8[2] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
  3072. utf8[3] = (char)((codepoint & 0x3f) | 0x80);
  3073. length = 4;
  3074. }
  3075. *byteLength = length;
  3076. return utf8;
  3077. }
  3078. #endif // RAYGUI_STANDALONE
  3079. #endif // RAYGUI_IMPLEMENTATION