Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3681 строка
158 KiB

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