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

1303 lines
47KB

  1. /*
  2. Blendish - Blender 2.5 UI based theming functions for NanoVG
  3. Copyright (c) 2014 Leonard Ritter <leonard.ritter@duangle.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef BLENDISH_H
  21. #define BLENDISH_H
  22. #ifndef NANOVG_H
  23. #error "nanovg.h must be included first."
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /*
  29. Revision 3 (2014-07-08)
  30. Summary
  31. -------
  32. Blendish is a small collection of drawing functions for NanoVG, designed to
  33. replicate the look of the Blender 2.5+ User Interface. You can use these
  34. functions to theme your UI library. Several metric constants for faithful
  35. reproduction are also included.
  36. Blendish supports the original Blender icon sheet; As the licensing of Blenders
  37. icons is unclear, they are not included in Blendishes repository, but a SVG
  38. template, "icons_template.svg" is provided, which you can use to build your own
  39. icon sheet.
  40. To use icons, you must first load the icon sheet using one of the
  41. nvgCreateImage*() functions and then pass the image handle to bndSetIconImage();
  42. otherwise, no icons will be drawn. See bndSetIconImage() for more information.
  43. Blendish will not render text until a suitable UI font has been passed to
  44. bndSetFont() has been called. See bndSetFont() for more information.
  45. Drawbacks
  46. ---------
  47. There is no support varying dpi resolutions yet. The library is hardcoded
  48. to the equivalent of 72 dpi in the Blender system settings.
  49. Support for label truncation is missing. Text rendering breaks when widgets are
  50. too short to contain their labels.
  51. Usage
  52. -----
  53. To use this header file in implementation mode, define BLENDISH_IMPLEMENTATION
  54. before including blendish.h, otherwise the file will be in header-only mode.
  55. */
  56. // if that typedef is provided elsewhere, you may define
  57. // BLENDISH_NO_NVG_TYPEDEFS before including the header.
  58. #ifndef BLENDISH_NO_NVG_TYPEDEFS
  59. typedef struct NVGcontext NVGcontext;
  60. typedef struct NVGcolor NVGcolor;
  61. typedef struct NVGglyphPosition NVGglyphPosition;
  62. #endif
  63. // describes the theme used to draw a single widget or widget box;
  64. // these values correspond to the same values that can be retrieved from
  65. // the Theme panel in the Blender preferences
  66. typedef struct BNDwidgetTheme {
  67. // color of widget box outline
  68. NVGcolor outlineColor;
  69. // color of widget item (meaning changes depending on class)
  70. NVGcolor itemColor;
  71. // fill color of widget box
  72. NVGcolor innerColor;
  73. // fill color of widget box when active
  74. NVGcolor innerSelectedColor;
  75. // color of text label
  76. NVGcolor textColor;
  77. // color of text label when active
  78. NVGcolor textSelectedColor;
  79. // delta modifier for upper part of gradient (-100 to 100)
  80. int shadeTop;
  81. // delta modifier for lower part of gradient (-100 to 100)
  82. int shadeDown;
  83. } BNDwidgetTheme;
  84. // describes the theme used to draw widgets
  85. typedef struct BNDtheme {
  86. // the background color of panels and windows
  87. NVGcolor backgroundColor;
  88. // theme for labels
  89. BNDwidgetTheme regularTheme;
  90. // theme for tool buttons
  91. BNDwidgetTheme toolTheme;
  92. // theme for radio buttons
  93. BNDwidgetTheme radioTheme;
  94. // theme for text fields
  95. BNDwidgetTheme textFieldTheme;
  96. // theme for option buttons (checkboxes)
  97. BNDwidgetTheme optionTheme;
  98. // theme for choice buttons (comboboxes)
  99. // Blender calls them "menu buttons"
  100. BNDwidgetTheme choiceTheme;
  101. // theme for number fields
  102. BNDwidgetTheme numberFieldTheme;
  103. // theme for slider controls
  104. BNDwidgetTheme sliderTheme;
  105. // theme for scrollbars
  106. BNDwidgetTheme scrollBarTheme;
  107. // theme for tooltips
  108. BNDwidgetTheme tooltipTheme;
  109. // theme for menu backgrounds
  110. BNDwidgetTheme menuTheme;
  111. // theme for menu items
  112. BNDwidgetTheme menuItemTheme;
  113. } BNDtheme;
  114. // how text on a control is aligned
  115. typedef enum BNDtextAlignment {
  116. BND_LEFT = 0,
  117. BND_CENTER,
  118. } BNDtextAlignment;
  119. // states altering the styling of a widget
  120. typedef enum BNDwidgetState {
  121. // not interacting
  122. BND_DEFAULT = 0,
  123. // the mouse is hovering over the control
  124. BND_HOVER,
  125. // the widget is activated (pressed) or in an active state (toggled)
  126. BND_ACTIVE
  127. } BNDwidgetState;
  128. // flags indicating which corners are sharp (for grouping widgets)
  129. typedef enum BNDcornerFlags {
  130. // all corners are round
  131. BND_CORNER_NONE = 0,
  132. // sharp top left corner
  133. BND_CORNER_TOP_LEFT = 1,
  134. // sharp top right corner
  135. BND_CORNER_TOP_RIGHT = 2,
  136. // sharp bottom right corner
  137. BND_CORNER_DOWN_RIGHT = 4,
  138. // sharp bottom left corner
  139. BND_CORNER_DOWN_LEFT = 8,
  140. // all corners are sharp;
  141. // you can invert a set of flags using ^= BND_CORNER_ALL
  142. BND_CORNER_ALL = 0xF,
  143. // top border is sharp
  144. BND_CORNER_TOP = 3,
  145. // bottom border is sharp
  146. BND_CORNER_DOWN = 0xC,
  147. // left border is sharp
  148. BND_CORNER_LEFT = 9,
  149. // right border is sharp
  150. BND_CORNER_RIGHT = 6
  151. } BNDcornerFlags;
  152. // build an icon ID from two coordinates into the icon sheet, where
  153. // (0,0) designates the upper-leftmost icon, (1,0) the one right next to it,
  154. // and so on.
  155. #define BND_ICONID(x,y) ((x)|((y)<<8))
  156. // default widget height
  157. #define BND_WIDGET_HEIGHT 21
  158. // default toolbutton width (if icon only)
  159. #define BND_TOOL_WIDTH 20
  160. // width of vertical scrollbar
  161. #define BND_SCROLLBAR_WIDTH 13
  162. // height of horizontal scrollbar
  163. #define BND_SCROLLBAR_HEIGHT 14
  164. ////////////////////////////////////////////////////////////////////////////////
  165. // set the current theme all widgets will be drawn with.
  166. // the default Blender 2.6 theme is set by default.
  167. void bndSetTheme(BNDtheme theme);
  168. // Returns the currently set theme
  169. const BNDtheme *bndGetTheme();
  170. // designates an image handle as returned by nvgCreateImage*() as the themes'
  171. // icon sheet. The icon sheet format must be compatible to Blender 2.6's icon
  172. // sheet; the order of icons does not matter.
  173. // A valid icon sheet is e.g. shown at
  174. // http://wiki.blender.org/index.php/Dev:2.5/Doc/How_to/Add_an_icon
  175. void bndSetIconImage(int image);
  176. // designates an image handle as returned by nvgCreateFont*() as the themes'
  177. // UI font. Blender's original UI font Droid Sans is perfectly suited and
  178. // available here:
  179. // https://svn.blender.org/svnroot/bf-blender/trunk/blender/release/datafiles/fonts/
  180. void bndSetFont(int font);
  181. ////////////////////////////////////////////////////////////////////////////////
  182. // High Level Functions
  183. // --------------------
  184. // Use these functions to draw themed widgets with your NVGcontext.
  185. // Draw a label with its lower left origin at (x,y) and size of (w,h).
  186. // if iconid >= 0, an icon will be added to the widget
  187. // if label is not NULL, a label will be added to the widget
  188. // widget looks best when height is BND_WIDGET_HEIGHT
  189. void bndLabel(NVGcontext *ctx,
  190. float x, float y, float w, float h, int iconid, const char *label);
  191. // Draw a tool button with its lower left origin at (x,y) and size of (w,h),
  192. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  193. // the widgets current UI state.
  194. // if iconid >= 0, an icon will be added to the widget
  195. // if label is not NULL, a label will be added to the widget
  196. // widget looks best when height is BND_WIDGET_HEIGHT
  197. void bndToolButton(NVGcontext *ctx,
  198. float x, float y, float w, float h, int flags, BNDwidgetState state,
  199. int iconid, const char *label);
  200. // Draw a radio button with its lower left origin at (x,y) and size of (w,h),
  201. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  202. // the widgets current UI state.
  203. // if iconid >= 0, an icon will be added to the widget
  204. // if label is not NULL, a label will be added to the widget
  205. // widget looks best when height is BND_WIDGET_HEIGHT
  206. void bndRadioButton(NVGcontext *ctx,
  207. float x, float y, float w, float h, int flags, BNDwidgetState state,
  208. int iconid, const char *label);
  209. // Draw a text field with its lower left origin at (x,y) and size of (w,h),
  210. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  211. // the widgets current UI state.
  212. // if iconid >= 0, an icon will be added to the widget
  213. // if text is not NULL, text will be printed to the widget
  214. // cbegin must be >= 0 and <= strlen(text) and denotes the beginning of the caret
  215. // cend must be >= cbegin and <= strlen(text) and denotes the end of the caret
  216. // if cend < cbegin, then no caret will be drawn
  217. // widget looks best when height is BND_WIDGET_HEIGHT
  218. void bndTextField(NVGcontext *ctx,
  219. float x, float y, float w, float h, int flags, BNDwidgetState state,
  220. int iconid, const char *text, int cbegin, int cend);
  221. // Draw an option button with its lower left origin at (x,y) and size of (w,h),
  222. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  223. // the widgets current UI state.
  224. // if label is not NULL, a label will be added to the widget
  225. // widget looks best when height is BND_WIDGET_HEIGHT
  226. void bndOptionButton(NVGcontext *ctx,
  227. float x, float y, float w, float h, BNDwidgetState state,
  228. const char *label);
  229. // Draw a choice button with its lower left origin at (x,y) and size of (w,h),
  230. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  231. // the widgets current UI state.
  232. // if iconid >= 0, an icon will be added to the widget
  233. // if label is not NULL, a label will be added to the widget
  234. // widget looks best when height is BND_WIDGET_HEIGHT
  235. void bndChoiceButton(NVGcontext *ctx,
  236. float x, float y, float w, float h, int flags, BNDwidgetState state,
  237. int iconid, const char *label);
  238. // Draw a number field with its lower left origin at (x,y) and size of (w,h),
  239. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  240. // the widgets current UI state.
  241. // if label is not NULL, a label will be added to the widget
  242. // if value is not NULL, a value will be added to the widget, along with
  243. // a ":" separator
  244. // widget looks best when height is BND_WIDGET_HEIGHT
  245. void bndNumberField(NVGcontext *ctx,
  246. float x, float y, float w, float h, int flags, BNDwidgetState state,
  247. const char *label, const char *value);
  248. // Draw slider control with its lower left origin at (x,y) and size of (w,h),
  249. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  250. // the widgets current UI state.
  251. // progress must be in the range 0..1 and controls the size of the slider bar
  252. // if label is not NULL, a label will be added to the widget
  253. // if value is not NULL, a value will be added to the widget, along with
  254. // a ":" separator
  255. // widget looks best when height is BND_WIDGET_HEIGHT
  256. void bndSlider(NVGcontext *ctx,
  257. float x, float y, float w, float h, int flags, BNDwidgetState state,
  258. float progress, const char *label, const char *value);
  259. // Draw scrollbar with its lower left origin at (x,y) and size of (w,h),
  260. // where state denotes the widgets current UI state.
  261. // offset is in the range 0..1 and controls the position of the scroll handle
  262. // size is in the range 0..1 and controls the size of the scroll handle
  263. // horizontal widget looks best when height is BND_SCROLLBAR_HEIGHT,
  264. // vertical looks best when width is BND_SCROLLBAR_WIDTH
  265. void bndScrollBar(NVGcontext *ctx,
  266. float x, float y, float w, float h, BNDwidgetState state,
  267. float offset, float size);
  268. // Draw a menu background with its lower left origin at (x,y) and size of (w,h),
  269. // where flags is one or multiple flags from BNDcornerFlags.
  270. void bndMenuBackground(NVGcontext *ctx,
  271. float x, float y, float w, float h, int flags);
  272. // Draw a menu label with its lower left origin at (x,y) and size of (w,h).
  273. // if iconid >= 0, an icon will be added to the widget
  274. // if label is not NULL, a label will be added to the widget
  275. // widget looks best when height is BND_WIDGET_HEIGHT
  276. void bndMenuLabel(NVGcontext *ctx,
  277. float x, float y, float w, float h, int iconid, const char *label);
  278. // Draw a menu item with its lower left origin at (x,y) and size of (w,h),
  279. // where state denotes the widgets current UI state.
  280. // if iconid >= 0, an icon will be added to the widget
  281. // if label is not NULL, a label will be added to the widget
  282. // widget looks best when height is BND_WIDGET_HEIGHT
  283. void bndMenuItem(NVGcontext *ctx,
  284. float x, float y, float w, float h, BNDwidgetState state,
  285. int iconid, const char *label);
  286. // Draw a tooltip background with its lower left origin at (x,y) and size of (w,h)
  287. void bndTooltipBackground(NVGcontext *ctx, float x, float y, float w, float h);
  288. ////////////////////////////////////////////////////////////////////////////////
  289. // Low Level Functions
  290. // -------------------
  291. // these are part of the implementation detail and can be used to theme
  292. // new kinds of controls in a similar fashion.
  293. // make color transparent using the default alpha value
  294. NVGcolor bndTransparent(NVGcolor color);
  295. // offset a color by a given integer delta in the range -100 to 100
  296. NVGcolor bndOffsetColor(NVGcolor color, int delta);
  297. // assigns radius r to the four entries of array radiuses depending on whether
  298. // the corner is marked as sharp or not; see BNDcornerFlags for possible
  299. // flag values.
  300. void bndSelectCorners(float *radiuses, float r, int flags);
  301. // computes the upper and lower gradient colors for the inner box from a widget
  302. // theme and the widgets state. If flipActive is set and the state is
  303. // BND_ACTIVE, the upper and lower colors will be swapped.
  304. void bndInnerColors(NVGcolor *shade_top, NVGcolor *shade_down,
  305. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive);
  306. // computes the text color for a widget label from a widget theme and the
  307. // widgets state.
  308. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state);
  309. // computes the bounds of the scrollbar handle from the scrollbar size
  310. // and the handles offset and size.
  311. // offset is in the range 0..1 and defines the position of the scroll handle
  312. // size is in the range 0..1 and defines the size of the scroll handle
  313. void bndScrollHandleRect(float *x, float *y, float *w, float *h,
  314. float offset, float size);
  315. // Add a rounded box path at position (x,y) with size (w,h) and a separate
  316. // radius for each corner listed in clockwise order, so that cr0 = top left,
  317. // cr1 = top right, cr2 = bottom right, cr3 = bottom left;
  318. // this is a low level drawing function: the path must be stroked or filled
  319. // to become visible.
  320. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  321. float cr0, float cr1, float cr2, float cr3);
  322. // Draw a flat panel without any decorations at position (x,y) with size (w,h)
  323. // and fills it with backgroundColor
  324. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h);
  325. // Draw a lower inset for a rounded box at position (x,y) with size (w,h)
  326. // that gives the impression the surface has been pushed in.
  327. // cr2 and cr3 contain the radiuses of the bottom right and bottom left
  328. // corners of the rounded box.
  329. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  330. float cr2, float cr3);
  331. // Draw an icon with (x,y) as its upper left coordinate; the iconid selects
  332. // the icon from the sheet; use the BND_ICONID macro to build icon IDs.
  333. void bndIcon(NVGcontext *ctx, float x, float y, int iconid);
  334. // Draw a drop shadow around the rounded box at (x,y) with size (w,h) and
  335. // radius r, with feather as its maximum range in pixels.
  336. // No shadow will be painted inside the rounded box.
  337. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  338. float r, float feather, float alpha);
  339. // Draw the inner part of a widget box, with a gradient from shade_top to
  340. // shade_down. If h>w, the gradient will be horizontal instead of
  341. // vertical.
  342. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  343. float cr0, float cr1, float cr2, float cr3,
  344. NVGcolor shade_top, NVGcolor shade_down);
  345. // Draw the outline part of a widget box with the given color
  346. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  347. float cr0, float cr1, float cr2, float cr3, NVGcolor color);
  348. // Draw an optional icon specified by <iconid> and an optional label with
  349. // given alignment (BNDtextAlignment), fontsize and color within a widget box.
  350. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  351. // will be adjusted.
  352. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  353. // and color.
  354. // if value is not NULL, label and value will be drawn with a ":" separator
  355. // inbetween.
  356. void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
  357. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  358. const char *value);
  359. // Draw an optional icon specified by <iconid>, an optional label and
  360. // a caret with given fontsize and color within a widget box.
  361. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  362. // will be adjusted.
  363. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  364. // and color.
  365. // cbegin must be >= 0 and <= strlen(text) and denotes the beginning of the caret
  366. // cend must be >= cbegin and <= strlen(text) and denotes the end of the caret
  367. // if cend < cbegin, then no caret will be drawn
  368. void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
  369. int iconid, NVGcolor color, float fontsize, const char *label,
  370. NVGcolor caretcolor, int cbegin, int cend);
  371. // Draw a checkmark for an option box with the given upper left coordinates
  372. // (ox,oy) with the specified color.
  373. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color);
  374. // Draw a horizontal arrow for a number field with its center at (x,y) and
  375. // size s; if s is negative, the arrow points to the left.
  376. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  377. // Draw an up/down arrow for a choice box with its center at (x,y) and size s
  378. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  379. #ifdef __cplusplus
  380. };
  381. #endif
  382. #endif // BLENDISH_H
  383. ////////////////////////////////////////////////////////////////////////////////
  384. ////////////////////////////////////////////////////////////////////////////////
  385. #ifdef BLENDISH_IMPLEMENTATION
  386. #include <memory.h>
  387. #include <math.h>
  388. #ifdef _MSC_VER
  389. #pragma warning (disable: 4996) // Switch off security warnings
  390. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  391. #ifdef __cplusplus
  392. #define BND_INLINE inline
  393. #else
  394. #define BND_INLINE
  395. #endif
  396. #else
  397. #define BND_INLINE inline
  398. #endif
  399. ////////////////////////////////////////////////////////////////////////////////
  400. // default text size
  401. #define BND_LABEL_FONT_SIZE 13
  402. // default text padding in inner box
  403. #define BND_PAD_LEFT 8
  404. #define BND_PAD_RIGHT 8
  405. // label: value separator string
  406. #define BND_LABEL_SEPARATOR ": "
  407. // alpha intensity of transparent items (0xa4)
  408. #define BND_TRANSPARENT_ALPHA 0.643
  409. // shade intensity of beveled insets
  410. #define BND_BEVEL_SHADE 30
  411. // shade intensity of hovered inner boxes
  412. #define BND_HOVER_SHADE 30
  413. // width of icon sheet
  414. #define BND_ICON_SHEET_WIDTH 602
  415. // height of icon sheet
  416. #define BND_ICON_SHEET_HEIGHT 640
  417. // gridsize of icon sheet in both dimensions
  418. #define BND_ICON_SHEET_GRID 21
  419. // offset of first icon tile relative to left border
  420. #define BND_ICON_SHEET_OFFSET_X 5
  421. // offset of first icon tile relative to top border
  422. #define BND_ICON_SHEET_OFFSET_Y 10
  423. // resolution of single icon
  424. #define BND_ICON_SHEET_RES 16
  425. // size of number field arrow
  426. #define BND_NUMBER_ARROW_SIZE 4
  427. // default text color
  428. #define BND_COLOR_TEXT {{{ 0,0,0,1 }}}
  429. // default highlighted text color
  430. #define BND_COLOR_TEXT_SELECTED {{{ 1,1,1,1 }}}
  431. // radius of tool button
  432. #define BND_TOOL_RADIUS 4
  433. // radius of option button
  434. #define BND_OPTION_RADIUS 4
  435. // width of option button checkbox
  436. #define BND_OPTION_WIDTH 14
  437. // height of option button checkbox
  438. #define BND_OPTION_HEIGHT 15
  439. // radius of text field
  440. #define BND_TEXT_RADIUS 4
  441. // radius of number button
  442. #define BND_NUMBER_RADIUS 10
  443. // radius of menu popup
  444. #define BND_MENU_RADIUS 3
  445. // feather of menu popup shadow
  446. #define BND_SHADOW_FEATHER 12
  447. // alpha of menu popup shadow
  448. #define BND_SHADOW_ALPHA 0.5
  449. // radius of scrollbar
  450. #define BND_SCROLLBAR_RADIUS 7
  451. // shade intensity of active scrollbar
  452. #define BND_SCROLLBAR_ACTIVE_SHADE 15
  453. // max glyphs for position testing
  454. #define BND_MAX_GLYPHS 1024
  455. ////////////////////////////////////////////////////////////////////////////////
  456. BND_INLINE float bnd_clamp(float v, float mn, float mx) {
  457. return (v > mx)?mx:(v < mn)?mn:v;
  458. }
  459. ////////////////////////////////////////////////////////////////////////////////
  460. // the initial theme
  461. static BNDtheme bnd_theme = {
  462. // backgroundColor
  463. {{{ 0.447, 0.447, 0.447, 1.0 }}},
  464. // regularTheme
  465. {
  466. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  467. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  468. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  469. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  470. BND_COLOR_TEXT, // color_text
  471. BND_COLOR_TEXT_SELECTED, // color_text_selected
  472. 0, // shade_top
  473. 0, // shade_down
  474. },
  475. // toolTheme
  476. {
  477. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  478. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  479. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  480. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  481. BND_COLOR_TEXT, // color_text
  482. BND_COLOR_TEXT_SELECTED, // color_text_selected
  483. 15, // shade_top
  484. -15, // shade_down
  485. },
  486. // radioTheme
  487. {
  488. {{{ 0,0,0,1 }}}, // color_outline
  489. {{{ 1,1,1,1 }}}, // color_item
  490. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  491. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  492. BND_COLOR_TEXT_SELECTED, // color_text
  493. BND_COLOR_TEXT, // color_text_selected
  494. 15, // shade_top
  495. -15, // shade_down
  496. },
  497. // textFieldTheme
  498. {
  499. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  500. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  501. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner
  502. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  503. BND_COLOR_TEXT, // color_text
  504. BND_COLOR_TEXT_SELECTED, // color_text_selected
  505. 0, // shade_top
  506. 25, // shade_down
  507. },
  508. // optionTheme
  509. {
  510. {{{ 0,0,0,1 }}}, // color_outline
  511. {{{ 1,1,1,1 }}}, // color_item
  512. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  513. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  514. BND_COLOR_TEXT, // color_text
  515. BND_COLOR_TEXT_SELECTED, // color_text_selected
  516. 15, // shade_top
  517. -15, // shade_down
  518. },
  519. // choiceTheme
  520. {
  521. {{{ 0,0,0,1 }}}, // color_outline
  522. {{{ 1,1,1,1 }}}, // color_item
  523. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  524. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  525. BND_COLOR_TEXT_SELECTED, // color_text
  526. {{{ 0.8,0.8,0.8,1 }}}, // color_text_selected
  527. 15, // shade_top
  528. -15, // shade_down
  529. },
  530. // numberFieldTheme
  531. {
  532. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  533. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  534. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  535. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  536. BND_COLOR_TEXT, // color_text
  537. BND_COLOR_TEXT_SELECTED, // color_text_selected
  538. -20, // shade_top
  539. 0, // shade_down
  540. },
  541. // sliderTheme
  542. {
  543. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  544. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  545. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  546. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  547. BND_COLOR_TEXT, // color_text
  548. BND_COLOR_TEXT_SELECTED, // color_text_selected
  549. -20, // shade_top
  550. 0, // shade_down
  551. },
  552. // scrollBarTheme
  553. {
  554. {{{ 0.196,0.196,0.196,1 }}}, // color_outline
  555. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  556. {{{ 0.314, 0.314, 0.314,0.706 }}}, // color_inner
  557. {{{ 0.392, 0.392, 0.392,0.706 }}}, // color_inner_selected
  558. BND_COLOR_TEXT, // color_text
  559. BND_COLOR_TEXT_SELECTED, // color_text_selected
  560. 5, // shade_top
  561. -5, // shade_down
  562. },
  563. // tooltipTheme
  564. {
  565. {{{ 0,0,0,1 }}}, // color_outline
  566. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  567. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  568. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  569. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  570. BND_COLOR_TEXT_SELECTED, // color_text_selected
  571. 0, // shade_top
  572. 0, // shade_down
  573. },
  574. // menuTheme
  575. {
  576. {{{ 0,0,0,1 }}}, // color_outline
  577. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  578. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  579. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  580. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  581. BND_COLOR_TEXT_SELECTED, // color_text_selected
  582. 0, // shade_top
  583. 0, // shade_down
  584. },
  585. // menuItemTheme
  586. {
  587. {{{ 0,0,0,1 }}}, // color_outline
  588. {{{ 0.675,0.675,0.675,0.502 }}}, // color_item
  589. {{{ 0,0,0,0 }}}, // color_inner
  590. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  591. BND_COLOR_TEXT_SELECTED, // color_text
  592. BND_COLOR_TEXT, // color_text_selected
  593. 38, // shade_top
  594. 0, // shade_down
  595. },
  596. };
  597. ////////////////////////////////////////////////////////////////////////////////
  598. void bndSetTheme(BNDtheme theme) {
  599. bnd_theme = theme;
  600. }
  601. const BNDtheme *bndGetTheme() {
  602. return &bnd_theme;
  603. }
  604. // the handle to the image containing the icon sheet
  605. static int bnd_icon_image = -1;
  606. void bndSetIconImage(int image) {
  607. bnd_icon_image = image;
  608. }
  609. // the handle to the UI font
  610. static int bnd_font = -1;
  611. void bndSetFont(int font) {
  612. bnd_font = font;
  613. }
  614. ////////////////////////////////////////////////////////////////////////////////
  615. void bndLabel(NVGcontext *ctx,
  616. float x, float y, float w, float h, int iconid, const char *label) {
  617. bndIconLabelValue(ctx,x,y,w,h,iconid,
  618. bnd_theme.regularTheme.textColor, BND_LEFT,
  619. BND_LABEL_FONT_SIZE, label, NULL);
  620. }
  621. void bndToolButton(NVGcontext *ctx,
  622. float x, float y, float w, float h, int flags, BNDwidgetState state,
  623. int iconid, const char *label) {
  624. float cr[4];
  625. NVGcolor shade_top, shade_down;
  626. bndSelectCorners(cr, BND_TOOL_RADIUS, flags);
  627. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  628. bndInnerColors(&shade_top, &shade_down, &bnd_theme.toolTheme, state, 1);
  629. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  630. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  631. bndTransparent(bnd_theme.toolTheme.outlineColor));
  632. bndIconLabelValue(ctx,x,y,w,h,iconid,
  633. bndTextColor(&bnd_theme.toolTheme, state), BND_CENTER,
  634. BND_LABEL_FONT_SIZE, label, NULL);
  635. }
  636. void bndRadioButton(NVGcontext *ctx,
  637. float x, float y, float w, float h, int flags, BNDwidgetState state,
  638. int iconid, const char *label) {
  639. float cr[4];
  640. NVGcolor shade_top, shade_down;
  641. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  642. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  643. bndInnerColors(&shade_top, &shade_down, &bnd_theme.radioTheme, state, 1);
  644. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  645. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  646. bndTransparent(bnd_theme.radioTheme.outlineColor));
  647. bndIconLabelValue(ctx,x,y,w,h,iconid,
  648. bndTextColor(&bnd_theme.radioTheme, state), BND_CENTER,
  649. BND_LABEL_FONT_SIZE, label, NULL);
  650. }
  651. void bndTextField(NVGcontext *ctx,
  652. float x, float y, float w, float h, int flags, BNDwidgetState state,
  653. int iconid, const char *text, int cbegin, int cend) {
  654. float cr[4];
  655. NVGcolor shade_top, shade_down;
  656. bndSelectCorners(cr, BND_TEXT_RADIUS, flags);
  657. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  658. bndInnerColors(&shade_top, &shade_down, &bnd_theme.textFieldTheme, state, 0);
  659. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  660. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  661. bndTransparent(bnd_theme.textFieldTheme.outlineColor));
  662. if (state != BND_ACTIVE) {
  663. cend = -1;
  664. }
  665. bndIconLabelCaret(ctx,x,y,w,h,iconid,
  666. bndTextColor(&bnd_theme.textFieldTheme, state), BND_LABEL_FONT_SIZE,
  667. text, bnd_theme.textFieldTheme.itemColor, cbegin, cend);
  668. }
  669. void bndOptionButton(NVGcontext *ctx,
  670. float x, float y, float w, float h, BNDwidgetState state,
  671. const char *label) {
  672. float ox, oy;
  673. NVGcolor shade_top, shade_down;
  674. ox = x;
  675. oy = y+h-BND_OPTION_HEIGHT-3;
  676. bndBevelInset(ctx,ox,oy,
  677. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  678. BND_OPTION_RADIUS,BND_OPTION_RADIUS);
  679. bndInnerColors(&shade_top, &shade_down, &bnd_theme.optionTheme, state, 1);
  680. bndInnerBox(ctx,ox,oy,
  681. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  682. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  683. shade_top, shade_down);
  684. bndOutlineBox(ctx,ox,oy,
  685. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  686. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  687. bndTransparent(bnd_theme.optionTheme.outlineColor));
  688. if (state == BND_ACTIVE) {
  689. bndCheck(ctx,ox,oy, bndTransparent(bnd_theme.optionTheme.itemColor));
  690. }
  691. bndIconLabelValue(ctx,x+12,y,w-12,h,-1,
  692. bndTextColor(&bnd_theme.optionTheme, state), BND_LEFT,
  693. BND_LABEL_FONT_SIZE, label, NULL);
  694. }
  695. void bndChoiceButton(NVGcontext *ctx,
  696. float x, float y, float w, float h, int flags, BNDwidgetState state,
  697. int iconid, const char *label) {
  698. float cr[4];
  699. NVGcolor shade_top, shade_down;
  700. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  701. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  702. bndInnerColors(&shade_top, &shade_down, &bnd_theme.choiceTheme, state, 1);
  703. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  704. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  705. bndTransparent(bnd_theme.choiceTheme.outlineColor));
  706. bndIconLabelValue(ctx,x,y,w,h,iconid,
  707. bndTextColor(&bnd_theme.choiceTheme, state), BND_LEFT,
  708. BND_LABEL_FONT_SIZE, label, NULL);
  709. bndUpDownArrow(ctx,x+w-10,y+10,5,
  710. bndTransparent(bnd_theme.choiceTheme.itemColor));
  711. }
  712. void bndNumberField(NVGcontext *ctx,
  713. float x, float y, float w, float h, int flags, BNDwidgetState state,
  714. const char *label, const char *value) {
  715. float cr[4];
  716. NVGcolor shade_top, shade_down;
  717. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  718. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  719. bndInnerColors(&shade_top, &shade_down, &bnd_theme.numberFieldTheme, state, 0);
  720. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  721. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  722. bndTransparent(bnd_theme.numberFieldTheme.outlineColor));
  723. bndIconLabelValue(ctx,x,y,w,h,-1,
  724. bndTextColor(&bnd_theme.numberFieldTheme, state), BND_CENTER,
  725. BND_LABEL_FONT_SIZE, label, value);
  726. bndArrow(ctx,x+8,y+10,-BND_NUMBER_ARROW_SIZE,
  727. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  728. bndArrow(ctx,x+w-8,y+10,BND_NUMBER_ARROW_SIZE,
  729. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  730. }
  731. void bndSlider(NVGcontext *ctx,
  732. float x, float y, float w, float h, int flags, BNDwidgetState state,
  733. float progress, const char *label, const char *value) {
  734. float cr[4];
  735. NVGcolor shade_top, shade_down;
  736. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  737. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  738. bndInnerColors(&shade_top, &shade_down, &bnd_theme.sliderTheme, state, 0);
  739. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  740. if (state == BND_ACTIVE) {
  741. shade_top = bndOffsetColor(
  742. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  743. shade_down = bndOffsetColor(
  744. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  745. } else {
  746. shade_top = bndOffsetColor(
  747. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  748. shade_down = bndOffsetColor(
  749. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  750. }
  751. nvgScissor(ctx,x,y,8+(w-8)*bnd_clamp(progress,0,1),h);
  752. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  753. nvgResetScissor(ctx);
  754. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  755. bndTransparent(bnd_theme.sliderTheme.outlineColor));
  756. bndIconLabelValue(ctx,x,y,w,h,-1,
  757. bndTextColor(&bnd_theme.sliderTheme, state), BND_CENTER,
  758. BND_LABEL_FONT_SIZE, label, value);
  759. }
  760. void bndScrollBar(NVGcontext *ctx,
  761. float x, float y, float w, float h, BNDwidgetState state,
  762. float offset, float size) {
  763. bndBevelInset(ctx,x,y,w,h,
  764. BND_SCROLLBAR_RADIUS, BND_SCROLLBAR_RADIUS);
  765. bndInnerBox(ctx,x,y,w,h,
  766. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  767. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  768. bndOffsetColor(
  769. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeDown),
  770. bndOffsetColor(
  771. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeTop));
  772. bndOutlineBox(ctx,x,y,w,h,
  773. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  774. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  775. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  776. NVGcolor itemColor = bndOffsetColor(
  777. bnd_theme.scrollBarTheme.itemColor,
  778. (state == BND_ACTIVE)?BND_SCROLLBAR_ACTIVE_SHADE:0);
  779. bndScrollHandleRect(&x,&y,&w,&h,offset,size);
  780. bndInnerBox(ctx,x,y,w,h,
  781. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  782. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  783. bndOffsetColor(
  784. itemColor, 3*bnd_theme.scrollBarTheme.shadeTop),
  785. bndOffsetColor(
  786. itemColor, 3*bnd_theme.scrollBarTheme.shadeDown));
  787. bndOutlineBox(ctx,x,y,w,h,
  788. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  789. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  790. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  791. }
  792. void bndMenuBackground(NVGcontext *ctx,
  793. float x, float y, float w, float h, int flags) {
  794. float cr[4];
  795. NVGcolor shade_top, shade_down;
  796. bndSelectCorners(cr, BND_MENU_RADIUS, flags);
  797. bndInnerColors(&shade_top, &shade_down, &bnd_theme.menuTheme,
  798. BND_DEFAULT, 0);
  799. bndInnerBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  800. bndOutlineBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3],
  801. bndTransparent(bnd_theme.menuTheme.outlineColor));
  802. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  803. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  804. }
  805. void bndTooltipBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  806. NVGcolor shade_top, shade_down;
  807. bndInnerColors(&shade_top, &shade_down, &bnd_theme.tooltipTheme,
  808. BND_DEFAULT, 0);
  809. bndInnerBox(ctx,x,y,w,h+1,
  810. BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,
  811. shade_top, shade_down);
  812. bndOutlineBox(ctx,x,y,w,h+1,
  813. BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,
  814. bndTransparent(bnd_theme.tooltipTheme.outlineColor));
  815. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  816. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  817. }
  818. void bndMenuLabel(NVGcontext *ctx,
  819. float x, float y, float w, float h, int iconid, const char *label) {
  820. bndIconLabelValue(ctx,x,y,w,h,iconid,
  821. bnd_theme.menuTheme.textColor, BND_LEFT,
  822. BND_LABEL_FONT_SIZE, label, NULL);
  823. }
  824. void bndMenuItem(NVGcontext *ctx,
  825. float x, float y, float w, float h, BNDwidgetState state,
  826. int iconid, const char *label) {
  827. if (state != BND_DEFAULT) {
  828. bndInnerBox(ctx,x,y,w,h,0,0,0,0,
  829. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  830. bnd_theme.menuItemTheme.shadeTop),
  831. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  832. bnd_theme.menuItemTheme.shadeDown));
  833. state = BND_ACTIVE;
  834. }
  835. bndIconLabelValue(ctx,x,y,w,h,iconid,
  836. bndTextColor(&bnd_theme.menuItemTheme, state), BND_LEFT,
  837. BND_LABEL_FONT_SIZE, label, NULL);
  838. }
  839. ////////////////////////////////////////////////////////////////////////////////
  840. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  841. float cr0, float cr1, float cr2, float cr3) {
  842. float d;
  843. w = fmaxf(0, w);
  844. h = fmaxf(0, h);
  845. d = fminf(w, h);
  846. nvgMoveTo(ctx, x,y+h*0.5f);
  847. nvgArcTo(ctx, x,y, x+w,y, fminf(cr0, d/2));
  848. nvgArcTo(ctx, x+w,y, x+w,y+h, fminf(cr1, d/2));
  849. nvgArcTo(ctx, x+w,y+h, x,y+h, fminf(cr2, d/2));
  850. nvgArcTo(ctx, x,y+h, x,y, fminf(cr3, d/2));
  851. nvgClosePath(ctx);
  852. }
  853. NVGcolor bndTransparent(NVGcolor color) {
  854. color.a *= BND_TRANSPARENT_ALPHA;
  855. return color;
  856. }
  857. NVGcolor bndOffsetColor(NVGcolor color, int delta) {
  858. float offset = (float)delta / 255.0f;
  859. return delta?(
  860. nvgRGBAf(
  861. bnd_clamp(color.r+offset,0,1),
  862. bnd_clamp(color.g+offset,0,1),
  863. bnd_clamp(color.b+offset,0,1),
  864. color.a)
  865. ):color;
  866. }
  867. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  868. float cr2, float cr3) {
  869. float d;
  870. y -= 0.5f;
  871. d = fminf(w, h);
  872. cr2 = fminf(cr2, d/2);
  873. cr3 = fminf(cr3, d/2);
  874. nvgBeginPath(ctx);
  875. nvgMoveTo(ctx, x+w,y+h-cr2);
  876. nvgArcTo(ctx, x+w,y+h, x,y+h, cr2);
  877. nvgArcTo(ctx, x,y+h, x,y, cr3);
  878. NVGcolor bevelColor = bndOffsetColor(bnd_theme.backgroundColor,
  879. BND_BEVEL_SHADE);
  880. nvgStrokeWidth(ctx, 1);
  881. nvgStrokePaint(ctx,
  882. nvgLinearGradient(ctx,
  883. x,y+h-fmaxf(cr2,cr3)-1,
  884. x,y+h-1,
  885. nvgRGBAf(bevelColor.r, bevelColor.g, bevelColor.b, 0),
  886. bevelColor));
  887. nvgStroke(ctx);
  888. }
  889. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  890. nvgBeginPath(ctx);
  891. nvgRect(ctx, x, y, w, h);
  892. nvgFillColor(ctx, bnd_theme.backgroundColor);
  893. nvgFill(ctx);
  894. }
  895. void bndIcon(NVGcontext *ctx, float x, float y, int iconid) {
  896. int ix, iy, u, v;
  897. if (bnd_icon_image < 0) return; // no icons loaded
  898. ix = iconid & 0xff;
  899. iy = (iconid>>8) & 0xff;
  900. u = BND_ICON_SHEET_OFFSET_X + ix*BND_ICON_SHEET_GRID;
  901. v = BND_ICON_SHEET_OFFSET_Y + iy*BND_ICON_SHEET_GRID;
  902. nvgBeginPath(ctx);
  903. nvgRect(ctx,x,y,BND_ICON_SHEET_RES,BND_ICON_SHEET_RES);
  904. nvgFillPaint(ctx,
  905. nvgImagePattern(ctx,x-u,y-v,
  906. BND_ICON_SHEET_WIDTH,
  907. BND_ICON_SHEET_HEIGHT,
  908. 0,bnd_icon_image,0,1));
  909. nvgFill(ctx);
  910. }
  911. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  912. float r, float feather, float alpha) {
  913. nvgBeginPath(ctx);
  914. y += feather;
  915. h -= feather;
  916. nvgMoveTo(ctx, x-feather, y-feather);
  917. nvgLineTo(ctx, x, y-feather);
  918. nvgLineTo(ctx, x, y+h-feather);
  919. nvgArcTo(ctx, x,y+h,x+r,y+h,r);
  920. nvgArcTo(ctx, x+w,y+h,x+w,y+h-r,r);
  921. nvgLineTo(ctx, x+w, y-feather);
  922. nvgLineTo(ctx, x+w+feather, y-feather);
  923. nvgLineTo(ctx, x+w+feather, y+h+feather);
  924. nvgLineTo(ctx, x-feather, y+h+feather);
  925. nvgClosePath(ctx);
  926. nvgFillPaint(ctx, nvgBoxGradient(ctx,
  927. x - feather*0.5f,y - feather*0.5f,
  928. w + feather,h+feather,
  929. r+feather*0.5f,
  930. feather,
  931. nvgRGBAf(0,0,0,alpha*alpha),
  932. nvgRGBAf(0,0,0,0)));
  933. nvgFill(ctx);
  934. }
  935. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  936. float cr0, float cr1, float cr2, float cr3,
  937. NVGcolor shade_top, NVGcolor shade_down) {
  938. nvgBeginPath(ctx);
  939. bndRoundedBox(ctx,x+1,y+1,w-2,h-3,
  940. fmaxf(0,cr0-1),fmaxf(0,cr1-1),fmaxf(0,cr2-1),fmaxf(0,cr3-1));
  941. nvgFillPaint(ctx,((h-2)>w)?
  942. nvgLinearGradient(ctx,x,y,x+w,y,shade_top,shade_down):
  943. nvgLinearGradient(ctx,x,y,x,y+h,shade_top,shade_down));
  944. nvgFill(ctx);
  945. }
  946. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  947. float cr0, float cr1, float cr2, float cr3, NVGcolor color) {
  948. nvgBeginPath(ctx);
  949. bndRoundedBox(ctx,x+0.5f,y+0.5f,w-1,h-2,cr0,cr1,cr2,cr3);
  950. nvgStrokeColor(ctx,color);
  951. nvgStrokeWidth(ctx,1);
  952. nvgStroke(ctx);
  953. }
  954. void bndSelectCorners(float *radiuses, float r, int flags) {
  955. radiuses[0] = (flags & BND_CORNER_TOP_LEFT)?0:r;
  956. radiuses[1] = (flags & BND_CORNER_TOP_RIGHT)?0:r;
  957. radiuses[2] = (flags & BND_CORNER_DOWN_RIGHT)?0:r;
  958. radiuses[3] = (flags & BND_CORNER_DOWN_LEFT)?0:r;
  959. }
  960. void bndInnerColors(
  961. NVGcolor *shade_top, NVGcolor *shade_down,
  962. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive) {
  963. switch(state) {
  964. default:
  965. case BND_DEFAULT: {
  966. *shade_top = bndOffsetColor(theme->innerColor, theme->shadeTop);
  967. *shade_down = bndOffsetColor(theme->innerColor, theme->shadeDown);
  968. } break;
  969. case BND_HOVER: {
  970. NVGcolor color = bndOffsetColor(theme->innerColor, BND_HOVER_SHADE);
  971. *shade_top = bndOffsetColor(color, theme->shadeTop);
  972. *shade_down = bndOffsetColor(color, theme->shadeDown);
  973. } break;
  974. case BND_ACTIVE: {
  975. *shade_top = bndOffsetColor(theme->innerSelectedColor,
  976. flipActive?theme->shadeDown:theme->shadeTop);
  977. *shade_down = bndOffsetColor(theme->innerSelectedColor,
  978. flipActive?theme->shadeTop:theme->shadeDown);
  979. } break;
  980. }
  981. }
  982. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state) {
  983. return (state == BND_ACTIVE)?theme->textSelectedColor:theme->textColor;
  984. }
  985. void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
  986. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  987. const char *value) {
  988. float pleft = BND_PAD_LEFT;
  989. if (label) {
  990. if (iconid >= 0) {
  991. bndIcon(ctx,x+4,y+2,iconid);
  992. pleft += BND_ICON_SHEET_RES;
  993. }
  994. if (bnd_font < 0) return;
  995. nvgFontFaceId(ctx, bnd_font);
  996. nvgFontSize(ctx, fontsize);
  997. nvgBeginPath(ctx);
  998. nvgFillColor(ctx, color);
  999. if (value) {
  1000. float label_width = nvgTextBounds(ctx, 1, 1, label, NULL, NULL);
  1001. float sep_width = nvgTextBounds(ctx, 1, 1,
  1002. BND_LABEL_SEPARATOR, NULL, NULL);
  1003. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  1004. x += pleft;
  1005. if (align == BND_CENTER) {
  1006. float width = label_width + sep_width
  1007. + nvgTextBounds(ctx, 1, 1, value, NULL, NULL);
  1008. x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
  1009. }
  1010. y += h-6;
  1011. nvgText(ctx, x, y, label, NULL);
  1012. x += label_width;
  1013. nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL);
  1014. x += sep_width;
  1015. nvgText(ctx, x, y, value, NULL);
  1016. } else {
  1017. nvgTextAlign(ctx,
  1018. (align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE):
  1019. (NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE));
  1020. nvgTextBox(ctx,x+pleft,y+h-6,w-BND_PAD_RIGHT-pleft,label, NULL);
  1021. }
  1022. } else if (iconid >= 0) {
  1023. bndIcon(ctx,x+2,y+2,iconid);
  1024. }
  1025. }
  1026. void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
  1027. int iconid, NVGcolor color, float fontsize, const char *label,
  1028. NVGcolor caretcolor, int cbegin, int cend) {
  1029. float bounds[4];
  1030. float pleft = BND_TEXT_RADIUS;
  1031. if (!label) return;
  1032. if (iconid >= 0) {
  1033. bndIcon(ctx,x+4,y+2,iconid);
  1034. pleft += BND_ICON_SHEET_RES;
  1035. }
  1036. if (bnd_font < 0) return;
  1037. x+=pleft;
  1038. y+=h-6;
  1039. nvgFontFaceId(ctx, bnd_font);
  1040. nvgFontSize(ctx, fontsize);
  1041. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  1042. if (cend >= cbegin) {
  1043. float c0,c1;
  1044. const char *cb;const char *ce;
  1045. static NVGglyphPosition glyphs[BND_MAX_GLYPHS];
  1046. int nglyphs = nvgTextGlyphPositions(
  1047. ctx, x, y, label, label+cend+1, glyphs, BND_MAX_GLYPHS);
  1048. c0=glyphs[0].x;
  1049. c1=glyphs[nglyphs-1].x;
  1050. cb = label+cbegin; ce = label+cend;
  1051. // TODO: this is slow
  1052. for (int i=0; i < nglyphs; ++i) {
  1053. if (glyphs[i].str == cb)
  1054. c0 = glyphs[i].x;
  1055. if (glyphs[i].str == ce)
  1056. c1 = glyphs[i].x;
  1057. }
  1058. nvgTextBounds(ctx,x,y,label,NULL, bounds);
  1059. nvgBeginPath(ctx);
  1060. if (cbegin == cend) {
  1061. nvgFillColor(ctx, nvgRGBf(0.337,0.502,0.761));
  1062. nvgRect(ctx, c0-1, bounds[1], 2, bounds[3]-bounds[1]);
  1063. } else {
  1064. nvgFillColor(ctx, caretcolor);
  1065. nvgRect(ctx, c0-1, bounds[1], c1-c0+1, bounds[3]-bounds[1]);
  1066. }
  1067. nvgFill(ctx);
  1068. }
  1069. nvgBeginPath(ctx);
  1070. nvgFillColor(ctx, color);
  1071. nvgTextBox(ctx,x,y,w-BND_TEXT_RADIUS-pleft,label, NULL);
  1072. }
  1073. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color) {
  1074. nvgBeginPath(ctx);
  1075. nvgStrokeWidth(ctx,2);
  1076. nvgStrokeColor(ctx,color);
  1077. nvgLineCap(ctx,NVG_BUTT);
  1078. nvgLineJoin(ctx,NVG_MITER);
  1079. nvgMoveTo(ctx,ox+4,oy+5);
  1080. nvgLineTo(ctx,ox+7,oy+8);
  1081. nvgLineTo(ctx,ox+14,oy+1);
  1082. nvgStroke(ctx);
  1083. }
  1084. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  1085. nvgBeginPath(ctx);
  1086. nvgMoveTo(ctx,x,y);
  1087. nvgLineTo(ctx,x-s,y+s);
  1088. nvgLineTo(ctx,x-s,y-s);
  1089. nvgClosePath(ctx);
  1090. nvgFillColor(ctx,color);
  1091. nvgFill(ctx);
  1092. }
  1093. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  1094. float w;
  1095. nvgBeginPath(ctx);
  1096. w = 1.1f*s;
  1097. nvgMoveTo(ctx,x,y-1);
  1098. nvgLineTo(ctx,x+0.5*w,y-s-1);
  1099. nvgLineTo(ctx,x+w,y-1);
  1100. nvgClosePath(ctx);
  1101. nvgMoveTo(ctx,x,y+1);
  1102. nvgLineTo(ctx,x+0.5*w,y+s+1);
  1103. nvgLineTo(ctx,x+w,y+1);
  1104. nvgClosePath(ctx);
  1105. nvgFillColor(ctx,color);
  1106. nvgFill(ctx);
  1107. }
  1108. void bndScrollHandleRect(float *x, float *y, float *w, float *h,
  1109. float offset, float size) {
  1110. size = bnd_clamp(size,0,1);
  1111. offset = bnd_clamp(offset,0,1);
  1112. if ((*h) > (*w)) {
  1113. float hs = fmaxf(size*(*h), (*w)+1);
  1114. *y = (*y) + ((*h)-hs)*offset;
  1115. *h = hs;
  1116. } else {
  1117. float ws = fmaxf(size*(*w), (*h)-1);
  1118. *x = (*x) + ((*w)-ws)*offset;
  1119. *w = ws;
  1120. }
  1121. }
  1122. ////////////////////////////////////////////////////////////////////////////////
  1123. #ifdef BND_INLINE
  1124. #undef BND_INLINE
  1125. #endif
  1126. #endif // BLENDISH_IMPLEMENTATION