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.

1140 lines
40KB

  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. #endif
  62. // describes the theme used to draw a single widget or widget box;
  63. // these values correspond to the same values that can be retrieved from
  64. // the Theme panel in the Blender preferences
  65. typedef struct BNDwidgetTheme {
  66. // color of widget box outline
  67. NVGcolor outlineColor;
  68. // color of widget item (meaning changes depending on class)
  69. NVGcolor itemColor;
  70. // fill color of widget box
  71. NVGcolor innerColor;
  72. // fill color of widget box when active
  73. NVGcolor innerSelectedColor;
  74. // color of text label
  75. NVGcolor textColor;
  76. // color of text label when active
  77. NVGcolor textSelectedColor;
  78. // delta modifier for upper part of gradient (-100 to 100)
  79. int shadeTop;
  80. // delta modifier for lower part of gradient (-100 to 100)
  81. int shadeDown;
  82. } BNDwidgetTheme;
  83. // describes the theme used to draw widgets
  84. typedef struct BNDtheme {
  85. // the background color of panels and windows
  86. NVGcolor backgroundColor;
  87. // theme for labels
  88. BNDwidgetTheme regularTheme;
  89. // theme for tool buttons
  90. BNDwidgetTheme toolTheme;
  91. // theme for radio buttons
  92. BNDwidgetTheme radioTheme;
  93. // theme for option buttons (checkboxes)
  94. BNDwidgetTheme optionTheme;
  95. // theme for choice buttons (comboboxes)
  96. // Blender calls them "menu buttons"
  97. BNDwidgetTheme choiceTheme;
  98. // theme for number fields
  99. BNDwidgetTheme numberFieldTheme;
  100. // theme for slider controls
  101. BNDwidgetTheme sliderTheme;
  102. // theme for scrollbars
  103. BNDwidgetTheme scrollBarTheme;
  104. // theme for menu backgrounds and tooltips
  105. BNDwidgetTheme menuTheme;
  106. // theme for menu items
  107. BNDwidgetTheme menuItemTheme;
  108. } BNDtheme;
  109. // how text on a control is aligned
  110. typedef enum BNDtextAlignment {
  111. BND_LEFT = 0,
  112. BND_CENTER,
  113. } BNDtextAlignment;
  114. // states altering the styling of a widget
  115. typedef enum BNDwidgetState {
  116. // not interacting
  117. BND_DEFAULT = 0,
  118. // the mouse is hovering over the control
  119. BND_HOVER,
  120. // the widget is activated (pressed) or in an active state (toggled)
  121. BND_ACTIVE
  122. } BNDwidgetState;
  123. // flags indicating which corners are sharp (for grouping widgets)
  124. typedef enum BNDcornerFlags {
  125. // all corners are round
  126. BND_CORNER_NONE = 0,
  127. // sharp top left corner
  128. BND_CORNER_TOP_LEFT = 1,
  129. // sharp top right corner
  130. BND_CORNER_TOP_RIGHT = 2,
  131. // sharp bottom right corner
  132. BND_CORNER_DOWN_RIGHT = 4,
  133. // sharp bottom left corner
  134. BND_CORNER_DOWN_LEFT = 8,
  135. // all corners are sharp;
  136. // you can invert a set of flags using ^= BND_CORNER_ALL
  137. BND_CORNER_ALL = 0xF,
  138. // top border is sharp
  139. BND_CORNER_TOP = 3,
  140. // bottom border is sharp
  141. BND_CORNER_DOWN = 0xC,
  142. // left border is sharp
  143. BND_CORNER_LEFT = 9,
  144. // right border is sharp
  145. BND_CORNER_RIGHT = 6
  146. } BNDcornerFlags;
  147. // build an icon ID from two coordinates into the icon sheet, where
  148. // (0,0) designates the upper-leftmost icon, (1,0) the one right next to it,
  149. // and so on.
  150. #define BND_ICONID(x,y) ((x)|((y)<<8))
  151. // default widget height
  152. #define BND_WIDGET_HEIGHT 21
  153. // default toolbutton width (if icon only)
  154. #define BND_TOOL_WIDTH 20
  155. // width of vertical scrollbar
  156. #define BND_SCROLLBAR_WIDTH 13
  157. // height of horizontal scrollbar
  158. #define BND_SCROLLBAR_HEIGHT 14
  159. ////////////////////////////////////////////////////////////////////////////////
  160. // set the current theme all widgets will be drawn with.
  161. // the default Blender 2.6 theme is set by default.
  162. void bndSetTheme(BNDtheme theme);
  163. // Returns the currently set theme
  164. const BNDtheme *bndGetTheme();
  165. // designates an image handle as returned by nvgCreateImage*() as the themes'
  166. // icon sheet. The icon sheet format must be compatible to Blender 2.6's icon
  167. // sheet; the order of icons does not matter.
  168. // A valid icon sheet is e.g. shown at
  169. // http://wiki.blender.org/index.php/Dev:2.5/Doc/How_to/Add_an_icon
  170. void bndSetIconImage(int image);
  171. // designates an image handle as returned by nvgCreateFont*() as the themes'
  172. // UI font. Blender's original UI font Droid Sans is perfectly suited and
  173. // available here:
  174. // https://svn.blender.org/svnroot/bf-blender/trunk/blender/release/datafiles/fonts/
  175. void bndSetFont(int font);
  176. ////////////////////////////////////////////////////////////////////////////////
  177. // High Level Functions
  178. // --------------------
  179. // Use these functions to draw themed widgets with your NVGcontext.
  180. // Draw a label with its lower left origin at (x,y) and size of (w,h).
  181. // if iconid >= 0, an icon will be added to the widget
  182. // if label is not NULL, a label will be added to the widget
  183. // widget looks best when height is BND_WIDGET_HEIGHT
  184. void bndLabel(NVGcontext *ctx,
  185. float x, float y, float w, float h, int iconid, const char *label);
  186. // Draw a tool button with its lower left origin at (x,y) and size of (w,h),
  187. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  188. // the widgets current UI state.
  189. // if iconid >= 0, an icon will be added to the widget
  190. // if label is not NULL, a label will be added to the widget
  191. // widget looks best when height is BND_WIDGET_HEIGHT
  192. void bndToolButton(NVGcontext *ctx,
  193. float x, float y, float w, float h, int flags, BNDwidgetState state,
  194. int iconid, const char *label);
  195. // Draw a radio button with its lower left origin at (x,y) and size of (w,h),
  196. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  197. // the widgets current UI state.
  198. // if iconid >= 0, an icon will be added to the widget
  199. // if label is not NULL, a label will be added to the widget
  200. // widget looks best when height is BND_WIDGET_HEIGHT
  201. void bndRadioButton(NVGcontext *ctx,
  202. float x, float y, float w, float h, int flags, BNDwidgetState state,
  203. int iconid, const char *label);
  204. // Draw an option button with its lower left origin at (x,y) and size of (w,h),
  205. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  206. // the widgets current UI state.
  207. // if label is not NULL, a label will be added to the widget
  208. // widget looks best when height is BND_WIDGET_HEIGHT
  209. void bndOptionButton(NVGcontext *ctx,
  210. float x, float y, float w, float h, BNDwidgetState state,
  211. const char *label);
  212. // Draw a choice button with its lower left origin at (x,y) and size of (w,h),
  213. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  214. // the widgets current UI state.
  215. // if iconid >= 0, an icon will be added to the widget
  216. // if label is not NULL, a label will be added to the widget
  217. // widget looks best when height is BND_WIDGET_HEIGHT
  218. void bndChoiceButton(NVGcontext *ctx,
  219. float x, float y, float w, float h, int flags, BNDwidgetState state,
  220. int iconid, const char *label);
  221. // Draw a number field 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. // if value is not NULL, a value will be added to the widget, along with
  226. // a ":" separator
  227. // widget looks best when height is BND_WIDGET_HEIGHT
  228. void bndNumberField(NVGcontext *ctx,
  229. float x, float y, float w, float h, int flags, BNDwidgetState state,
  230. const char *label, const char *value);
  231. // Draw slider control with its lower left origin at (x,y) and size of (w,h),
  232. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  233. // the widgets current UI state.
  234. // progress must be in the range 0..1 and controls the size of the slider bar
  235. // if label is not NULL, a label will be added to the widget
  236. // if value is not NULL, a value will be added to the widget, along with
  237. // a ":" separator
  238. // widget looks best when height is BND_WIDGET_HEIGHT
  239. void bndSlider(NVGcontext *ctx,
  240. float x, float y, float w, float h, int flags, BNDwidgetState state,
  241. float progress, const char *label, const char *value);
  242. // Draw scrollbar with its lower left origin at (x,y) and size of (w,h),
  243. // where state denotes the widgets current UI state.
  244. // offset is in the range 0..1 and controls the position of the scroll handle
  245. // size is in the range 0..1 and controls the size of the scroll handle
  246. // horizontal widget looks best when height is BND_SCROLLBAR_HEIGHT,
  247. // vertical looks best when width is BND_SCROLLBAR_WIDTH
  248. void bndScrollBar(NVGcontext *ctx,
  249. float x, float y, float w, float h, BNDwidgetState state,
  250. float offset, float size);
  251. // Draw a menu background with its lower left origin at (x,y) and size of (w,h),
  252. // where flags is one or multiple flags from BNDcornerFlags.
  253. void bndMenuBackground(NVGcontext *ctx,
  254. float x, float y, float w, float h, int flags);
  255. // Draw a menu label with its lower left origin at (x,y) and size of (w,h).
  256. // if iconid >= 0, an icon will be added to the widget
  257. // if label is not NULL, a label will be added to the widget
  258. // widget looks best when height is BND_WIDGET_HEIGHT
  259. void bndMenuLabel(NVGcontext *ctx,
  260. float x, float y, float w, float h, int iconid, const char *label);
  261. // Draw a menu item with its lower left origin at (x,y) and size of (w,h),
  262. // where state denotes the widgets current UI state.
  263. // if iconid >= 0, an icon will be added to the widget
  264. // if label is not NULL, a label will be added to the widget
  265. // widget looks best when height is BND_WIDGET_HEIGHT
  266. void bndMenuItem(NVGcontext *ctx,
  267. float x, float y, float w, float h, BNDwidgetState state,
  268. int iconid, const char *label);
  269. ////////////////////////////////////////////////////////////////////////////////
  270. // Low Level Functions
  271. // -------------------
  272. // these are part of the implementation detail and can be used to theme
  273. // new kinds of controls in a similar fashion.
  274. // make color transparent using the default alpha value
  275. NVGcolor bndTransparent(NVGcolor color);
  276. // offset a color by a given integer delta in the range -100 to 100
  277. NVGcolor bndOffsetColor(NVGcolor color, int delta);
  278. // assigns radius r to the four entries of array radiuses depending on whether
  279. // the corner is marked as sharp or not; see BNDcornerFlags for possible
  280. // flag values.
  281. void bndSelectCorners(float *radiuses, float r, int flags);
  282. // computes the upper and lower gradient colors for the inner box from a widget
  283. // theme and the widgets state. If flipActive is set and the state is
  284. // BND_ACTIVE, the upper and lower colors will be swapped.
  285. void bndInnerColors(NVGcolor *shade_top, NVGcolor *shade_down,
  286. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive);
  287. // computes the text color for a widget label from a widget theme and the
  288. // widgets state.
  289. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state);
  290. // Add a rounded box path at position (x,y) with size (w,h) and a separate
  291. // radius for each corner listed in clockwise order, so that cr0 = top left,
  292. // cr1 = top right, cr2 = bottom right, cr3 = bottom left;
  293. // this is a low level drawing function: the path must be stroked or filled
  294. // to become visible.
  295. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  296. float cr0, float cr1, float cr2, float cr3);
  297. // Draw a flat panel without any decorations at position (x,y) with size (w,h)
  298. // and fills it with backgroundColor
  299. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h);
  300. // Draw a lower inset for a rounded box at position (x,y) with size (w,h)
  301. // that gives the impression the surface has been pushed in.
  302. // cr2 and cr3 contain the radiuses of the bottom right and bottom left
  303. // corners of the rounded box.
  304. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  305. float cr2, float cr3);
  306. // Draw an icon with (x,y) as its upper left coordinate; the iconid selects
  307. // the icon from the sheet; use the BND_ICONID macro to build icon IDs.
  308. void bndIcon(NVGcontext *ctx, float x, float y, int iconid);
  309. // Draw a drop shadow around the rounded box at (x,y) with size (w,h) and
  310. // radius r, with feather as its maximum range in pixels.
  311. // No shadow will be painted inside the rounded box.
  312. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  313. float r, float feather, float alpha);
  314. // Draw the inner part of a widget box, with a gradient from shade_top to
  315. // shade_down. If h>w, the gradient will be horizontal instead of
  316. // vertical.
  317. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  318. float cr0, float cr1, float cr2, float cr3,
  319. NVGcolor shade_top, NVGcolor shade_down);
  320. // Draw the outline part of a widget box with the given color
  321. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  322. float cr0, float cr1, float cr2, float cr3, NVGcolor color);
  323. // Draw an optional icon specified by <iconid> and an optional label with
  324. // given alignment (BNDtextAlignment), fontsize and color within a widget box.
  325. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  326. // will be adjusted.
  327. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  328. // and color.
  329. // if value is not NULL, label and value will be drawn with a ":" separator
  330. // inbetween.
  331. void bndIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
  332. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  333. const char *value);
  334. // Draw a checkmark for an option box with the given upper left coordinates
  335. // (ox,oy) with the specified color.
  336. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color);
  337. // Draw a horizontal arrow for a number field with its center at (x,y) and
  338. // size s; if s is negative, the arrow points to the left.
  339. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  340. // Draw an up/down arrow for a choice box with its center at (x,y) and size s
  341. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  342. #ifdef __cplusplus
  343. };
  344. #endif
  345. #endif // BLENDISH_H
  346. ////////////////////////////////////////////////////////////////////////////////
  347. ////////////////////////////////////////////////////////////////////////////////
  348. #ifdef BLENDISH_IMPLEMENTATION
  349. #include <memory.h>
  350. #include <math.h>
  351. #ifdef _MSC_VER
  352. #pragma warning (disable: 4996) // Switch off security warnings
  353. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  354. #ifdef __cplusplus
  355. #define BND_INLINE inline
  356. #else
  357. #define BND_INLINE
  358. #endif
  359. #else
  360. #define BND_INLINE inline
  361. #endif
  362. ////////////////////////////////////////////////////////////////////////////////
  363. // default text size
  364. #define BND_LABEL_FONT_SIZE 13
  365. // default text padding in inner box
  366. #define BND_PAD_LEFT 8
  367. #define BND_PAD_RIGHT 8
  368. // label: value separator string
  369. #define BND_LABEL_SEPARATOR ": "
  370. // alpha intensity of transparent items (0xa4)
  371. #define BND_TRANSPARENT_ALPHA 0.643
  372. // shade intensity of beveled insets
  373. #define BND_BEVEL_SHADE 30
  374. // shade intensity of hovered inner boxes
  375. #define BND_HOVER_SHADE 30
  376. // width of icon sheet
  377. #define BND_ICON_SHEET_WIDTH 602
  378. // height of icon sheet
  379. #define BND_ICON_SHEET_HEIGHT 640
  380. // gridsize of icon sheet in both dimensions
  381. #define BND_ICON_SHEET_GRID 21
  382. // offset of first icon tile relative to left border
  383. #define BND_ICON_SHEET_OFFSET_X 5
  384. // offset of first icon tile relative to top border
  385. #define BND_ICON_SHEET_OFFSET_Y 10
  386. // resolution of single icon
  387. #define BND_ICON_SHEET_RES 16
  388. // size of number field arrow
  389. #define BND_NUMBER_ARROW_SIZE 4
  390. // default text color
  391. #define BND_COLOR_TEXT {{{ 0,0,0,1 }}}
  392. // default highlighted text color
  393. #define BND_COLOR_TEXT_SELECTED {{{ 1,1,1,1 }}}
  394. // radius of tool button
  395. #define BND_TOOL_RADIUS 4
  396. // radius of option button
  397. #define BND_OPTION_RADIUS 4
  398. // width of option button checkbox
  399. #define BND_OPTION_WIDTH 14
  400. // height of option button checkbox
  401. #define BND_OPTION_HEIGHT 15
  402. // radius of number button
  403. #define BND_NUMBER_RADIUS 10
  404. // radius of menu popup
  405. #define BND_MENU_RADIUS 3
  406. // feather of menu popup shadow
  407. #define BND_SHADOW_FEATHER 12
  408. // alpha of menu popup shadow
  409. #define BND_SHADOW_ALPHA 0.5
  410. // radius of scrollbar
  411. #define BND_SCROLLBAR_RADIUS 7
  412. // shade intensity of active scrollbar
  413. #define BND_SCROLLBAR_ACTIVE_SHADE 15
  414. ////////////////////////////////////////////////////////////////////////////////
  415. BND_INLINE float bnd_clamp(float v, float mn, float mx) {
  416. return (v > mx)?mx:(v < mn)?mn:v;
  417. }
  418. ////////////////////////////////////////////////////////////////////////////////
  419. // the initial theme
  420. static BNDtheme bnd_theme = {
  421. // backgroundColor
  422. {{{ 0.447, 0.447, 0.447, 1.0 }}},
  423. // regularTheme
  424. {
  425. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  426. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  427. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  428. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  429. BND_COLOR_TEXT, // color_text
  430. BND_COLOR_TEXT_SELECTED, // color_text_selected
  431. 0, // shade_top
  432. 0, // shade_down
  433. },
  434. // toolTheme
  435. {
  436. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  437. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  438. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  439. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  440. BND_COLOR_TEXT, // color_text
  441. BND_COLOR_TEXT_SELECTED, // color_text_selected
  442. 15, // shade_top
  443. -15, // shade_down
  444. },
  445. // radioTheme
  446. {
  447. {{{ 0,0,0,1 }}}, // color_outline
  448. {{{ 1,1,1,1 }}}, // color_item
  449. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  450. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  451. BND_COLOR_TEXT_SELECTED, // color_text
  452. BND_COLOR_TEXT, // color_text_selected
  453. 15, // shade_top
  454. -15, // shade_down
  455. },
  456. // optionTheme
  457. {
  458. {{{ 0,0,0,1 }}}, // color_outline
  459. {{{ 1,1,1,1 }}}, // color_item
  460. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  461. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  462. BND_COLOR_TEXT, // color_text
  463. BND_COLOR_TEXT_SELECTED, // color_text_selected
  464. 15, // shade_top
  465. -15, // shade_down
  466. },
  467. // choiceTheme
  468. {
  469. {{{ 0,0,0,1 }}}, // color_outline
  470. {{{ 1,1,1,1 }}}, // color_item
  471. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  472. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  473. BND_COLOR_TEXT_SELECTED, // color_text
  474. {{{ 0.8,0.8,0.8,1 }}}, // color_text_selected
  475. 15, // shade_top
  476. -15, // shade_down
  477. },
  478. // numberFieldTheme
  479. {
  480. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  481. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  482. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  483. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  484. BND_COLOR_TEXT, // color_text
  485. BND_COLOR_TEXT_SELECTED, // color_text_selected
  486. -20, // shade_top
  487. 0, // shade_down
  488. },
  489. // sliderTheme
  490. {
  491. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  492. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  493. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  494. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  495. BND_COLOR_TEXT, // color_text
  496. BND_COLOR_TEXT_SELECTED, // color_text_selected
  497. -20, // shade_top
  498. 0, // shade_down
  499. },
  500. // scrollBarTheme
  501. {
  502. {{{ 0.196,0.196,0.196,1 }}}, // color_outline
  503. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  504. {{{ 0.314, 0.314, 0.314,0.706 }}}, // color_inner
  505. {{{ 0.392, 0.392, 0.392,0.706 }}}, // color_inner_selected
  506. BND_COLOR_TEXT, // color_text
  507. BND_COLOR_TEXT_SELECTED, // color_text_selected
  508. 5, // shade_top
  509. -5, // shade_down
  510. },
  511. // menuTheme
  512. {
  513. {{{ 0,0,0,1 }}}, // color_outline
  514. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  515. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  516. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  517. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  518. BND_COLOR_TEXT_SELECTED, // color_text_selected
  519. 0, // shade_top
  520. 0, // shade_down
  521. },
  522. // menuItemTheme
  523. {
  524. {{{ 0,0,0,1 }}}, // color_outline
  525. {{{ 0.675,0.675,0.675,0.502 }}}, // color_item
  526. {{{ 0,0,0,0 }}}, // color_inner
  527. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  528. BND_COLOR_TEXT_SELECTED, // color_text
  529. BND_COLOR_TEXT, // color_text_selected
  530. 38, // shade_top
  531. 0, // shade_down
  532. },
  533. };
  534. ////////////////////////////////////////////////////////////////////////////////
  535. void bndSetTheme(BNDtheme theme) {
  536. bnd_theme = theme;
  537. }
  538. const BNDtheme *bndGetTheme() {
  539. return &bnd_theme;
  540. }
  541. // the handle to the image containing the icon sheet
  542. static int bnd_icon_image = -1;
  543. void bndSetIconImage(int image) {
  544. bnd_icon_image = image;
  545. }
  546. // the handle to the UI font
  547. static int bnd_font = -1;
  548. void bndSetFont(int font) {
  549. bnd_font = font;
  550. }
  551. ////////////////////////////////////////////////////////////////////////////////
  552. void bndLabel(NVGcontext *ctx,
  553. float x, float y, float w, float h, int iconid, const char *label) {
  554. bndIconLabel(ctx,x,y,w,h,iconid,
  555. bnd_theme.regularTheme.textColor, BND_LEFT,
  556. BND_LABEL_FONT_SIZE, label, NULL);
  557. }
  558. void bndToolButton(NVGcontext *ctx,
  559. float x, float y, float w, float h, int flags, BNDwidgetState state,
  560. int iconid, const char *label) {
  561. float cr[4];
  562. NVGcolor shade_top, shade_down;
  563. bndSelectCorners(cr, BND_TOOL_RADIUS, flags);
  564. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  565. bndInnerColors(&shade_top, &shade_down, &bnd_theme.toolTheme, state, 1);
  566. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  567. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  568. bndTransparent(bnd_theme.toolTheme.outlineColor));
  569. bndIconLabel(ctx,x,y,w,h,iconid,
  570. bndTextColor(&bnd_theme.toolTheme, state), BND_CENTER,
  571. BND_LABEL_FONT_SIZE, label, NULL);
  572. }
  573. void bndRadioButton(NVGcontext *ctx,
  574. float x, float y, float w, float h, int flags, BNDwidgetState state,
  575. int iconid, const char *label) {
  576. float cr[4];
  577. NVGcolor shade_top, shade_down;
  578. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  579. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  580. bndInnerColors(&shade_top, &shade_down, &bnd_theme.radioTheme, state, 1);
  581. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  582. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  583. bndTransparent(bnd_theme.radioTheme.outlineColor));
  584. bndIconLabel(ctx,x,y,w,h,iconid,
  585. bndTextColor(&bnd_theme.radioTheme, state), BND_CENTER,
  586. BND_LABEL_FONT_SIZE, label, NULL);
  587. }
  588. void bndOptionButton(NVGcontext *ctx,
  589. float x, float y, float w, float h, BNDwidgetState state,
  590. const char *label) {
  591. float ox, oy;
  592. NVGcolor shade_top, shade_down;
  593. ox = x;
  594. oy = y+h-BND_OPTION_HEIGHT-3;
  595. bndBevelInset(ctx,ox,oy,
  596. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  597. BND_OPTION_RADIUS,BND_OPTION_RADIUS);
  598. bndInnerColors(&shade_top, &shade_down, &bnd_theme.optionTheme, state, 1);
  599. bndInnerBox(ctx,ox,oy,
  600. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  601. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  602. shade_top, shade_down);
  603. bndOutlineBox(ctx,ox,oy,
  604. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  605. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  606. bndTransparent(bnd_theme.optionTheme.outlineColor));
  607. if (state == BND_ACTIVE) {
  608. bndCheck(ctx,ox,oy, bndTransparent(bnd_theme.optionTheme.itemColor));
  609. }
  610. bndIconLabel(ctx,x+12,y,w-12,h,-1,
  611. bndTextColor(&bnd_theme.optionTheme, state), BND_LEFT,
  612. BND_LABEL_FONT_SIZE, label, NULL);
  613. }
  614. void bndChoiceButton(NVGcontext *ctx,
  615. float x, float y, float w, float h, int flags, BNDwidgetState state,
  616. int iconid, const char *label) {
  617. float cr[4];
  618. NVGcolor shade_top, shade_down;
  619. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  620. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  621. bndInnerColors(&shade_top, &shade_down, &bnd_theme.choiceTheme, state, 1);
  622. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  623. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  624. bndTransparent(bnd_theme.choiceTheme.outlineColor));
  625. bndIconLabel(ctx,x,y,w,h,iconid,
  626. bndTextColor(&bnd_theme.choiceTheme, state), BND_LEFT,
  627. BND_LABEL_FONT_SIZE, label, NULL);
  628. bndUpDownArrow(ctx,x+w-10,y+10,5,
  629. bndTransparent(bnd_theme.choiceTheme.itemColor));
  630. }
  631. void bndNumberField(NVGcontext *ctx,
  632. float x, float y, float w, float h, int flags, BNDwidgetState state,
  633. const char *label, const char *value) {
  634. float cr[4];
  635. NVGcolor shade_top, shade_down;
  636. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  637. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  638. bndInnerColors(&shade_top, &shade_down, &bnd_theme.numberFieldTheme, state, 0);
  639. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  640. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  641. bndTransparent(bnd_theme.numberFieldTheme.outlineColor));
  642. bndIconLabel(ctx,x,y,w,h,-1,
  643. bndTextColor(&bnd_theme.numberFieldTheme, state), BND_CENTER,
  644. BND_LABEL_FONT_SIZE, label, value);
  645. bndArrow(ctx,x+8,y+10,-BND_NUMBER_ARROW_SIZE,
  646. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  647. bndArrow(ctx,x+w-8,y+10,BND_NUMBER_ARROW_SIZE,
  648. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  649. }
  650. void bndSlider(NVGcontext *ctx,
  651. float x, float y, float w, float h, int flags, BNDwidgetState state,
  652. float progress, const char *label, const char *value) {
  653. float cr[4];
  654. NVGcolor shade_top, shade_down;
  655. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  656. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  657. bndInnerColors(&shade_top, &shade_down, &bnd_theme.sliderTheme, state, 0);
  658. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  659. if (state == BND_ACTIVE) {
  660. shade_top = bndOffsetColor(
  661. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  662. shade_down = bndOffsetColor(
  663. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  664. } else {
  665. shade_top = bndOffsetColor(
  666. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  667. shade_down = bndOffsetColor(
  668. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  669. }
  670. nvgScissor(ctx,x,y,8+(w-8)*bnd_clamp(progress,0,1),h);
  671. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  672. nvgResetScissor(ctx);
  673. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  674. bndTransparent(bnd_theme.sliderTheme.outlineColor));
  675. bndIconLabel(ctx,x,y,w,h,-1,
  676. bndTextColor(&bnd_theme.sliderTheme, state), BND_CENTER,
  677. BND_LABEL_FONT_SIZE, label, value);
  678. }
  679. void bndScrollBar(NVGcontext *ctx,
  680. float x, float y, float w, float h, BNDwidgetState state,
  681. float offset, float size) {
  682. bndBevelInset(ctx,x,y,w,h,
  683. BND_SCROLLBAR_RADIUS, BND_SCROLLBAR_RADIUS);
  684. bndInnerBox(ctx,x,y,w,h,
  685. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  686. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  687. bndOffsetColor(
  688. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeDown),
  689. bndOffsetColor(
  690. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeTop));
  691. bndOutlineBox(ctx,x,y,w,h,
  692. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  693. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  694. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  695. NVGcolor itemColor = bndOffsetColor(
  696. bnd_theme.scrollBarTheme.itemColor,
  697. (state == BND_ACTIVE)?BND_SCROLLBAR_ACTIVE_SHADE:0);
  698. size = bnd_clamp(size,0,1);
  699. offset = bnd_clamp(offset,0,1);
  700. if (h>w) {
  701. float hs = fmaxf(size*h, w+1);
  702. y = y + (h-hs)*offset;
  703. h = hs;
  704. } else {
  705. float ws = fmaxf(size*w, h-1);
  706. x = x + (w-ws)*offset;
  707. w = ws;
  708. }
  709. bndInnerBox(ctx,x,y,w,h,
  710. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  711. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  712. bndOffsetColor(
  713. itemColor, 3*bnd_theme.scrollBarTheme.shadeTop),
  714. bndOffsetColor(
  715. itemColor, 3*bnd_theme.scrollBarTheme.shadeDown));
  716. bndOutlineBox(ctx,x,y,w,h,
  717. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  718. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  719. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  720. }
  721. void bndMenuBackground(NVGcontext *ctx,
  722. float x, float y, float w, float h, int flags) {
  723. float cr[4];
  724. NVGcolor shade_top, shade_down;
  725. bndSelectCorners(cr, BND_MENU_RADIUS, flags);
  726. bndInnerColors(&shade_top, &shade_down, &bnd_theme.menuTheme,
  727. BND_DEFAULT, 0);
  728. bndInnerBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  729. bndOutlineBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3],
  730. bndTransparent(bnd_theme.menuTheme.outlineColor));
  731. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  732. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  733. }
  734. void bndMenuLabel(NVGcontext *ctx,
  735. float x, float y, float w, float h, int iconid, const char *label) {
  736. bndIconLabel(ctx,x,y,w,h,iconid,
  737. bnd_theme.menuTheme.textColor, BND_LEFT,
  738. BND_LABEL_FONT_SIZE, label, NULL);
  739. }
  740. void bndMenuItem(NVGcontext *ctx,
  741. float x, float y, float w, float h, BNDwidgetState state,
  742. int iconid, const char *label) {
  743. if (state != BND_DEFAULT) {
  744. bndInnerBox(ctx,x,y,w,h,0,0,0,0,
  745. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  746. bnd_theme.menuItemTheme.shadeTop),
  747. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  748. bnd_theme.menuItemTheme.shadeDown));
  749. state = BND_ACTIVE;
  750. }
  751. bndIconLabel(ctx,x,y,w,h,iconid,
  752. bndTextColor(&bnd_theme.menuItemTheme, state), BND_LEFT,
  753. BND_LABEL_FONT_SIZE, label, NULL);
  754. }
  755. ////////////////////////////////////////////////////////////////////////////////
  756. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  757. float cr0, float cr1, float cr2, float cr3) {
  758. float d;
  759. w = fmaxf(0, w);
  760. h = fmaxf(0, h);
  761. d = fminf(w, h);
  762. nvgMoveTo(ctx, x,y+h*0.5f);
  763. nvgArcTo(ctx, x,y, x+w,y, fminf(cr0, d/2));
  764. nvgArcTo(ctx, x+w,y, x+w,y+h, fminf(cr1, d/2));
  765. nvgArcTo(ctx, x+w,y+h, x,y+h, fminf(cr2, d/2));
  766. nvgArcTo(ctx, x,y+h, x,y, fminf(cr3, d/2));
  767. nvgClosePath(ctx);
  768. }
  769. NVGcolor bndTransparent(NVGcolor color) {
  770. color.a *= BND_TRANSPARENT_ALPHA;
  771. return color;
  772. }
  773. NVGcolor bndOffsetColor(NVGcolor color, int delta) {
  774. float offset = (float)delta / 255.0f;
  775. return delta?(
  776. nvgRGBAf(
  777. bnd_clamp(color.r+offset,0,1),
  778. bnd_clamp(color.g+offset,0,1),
  779. bnd_clamp(color.b+offset,0,1),
  780. color.a)
  781. ):color;
  782. }
  783. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  784. float cr2, float cr3) {
  785. float d;
  786. y -= 0.5f;
  787. d = fminf(w, h);
  788. cr2 = fminf(cr2, d/2);
  789. cr3 = fminf(cr3, d/2);
  790. nvgBeginPath(ctx);
  791. nvgMoveTo(ctx, x+w,y+h-cr2);
  792. nvgArcTo(ctx, x+w,y+h, x,y+h, cr2);
  793. nvgArcTo(ctx, x,y+h, x,y, cr3);
  794. NVGcolor bevelColor = bndOffsetColor(bnd_theme.backgroundColor,
  795. BND_BEVEL_SHADE);
  796. nvgStrokeWidth(ctx, 1);
  797. nvgStrokePaint(ctx,
  798. nvgLinearGradient(ctx,
  799. x,y+h-fmaxf(cr2,cr3)-1,
  800. x,y+h-1,
  801. nvgRGBAf(bevelColor.r, bevelColor.g, bevelColor.b, 0),
  802. bevelColor));
  803. nvgStroke(ctx);
  804. }
  805. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  806. nvgBeginPath(ctx);
  807. nvgRect(ctx, x, y, w, h);
  808. nvgFillColor(ctx, bnd_theme.backgroundColor);
  809. nvgFill(ctx);
  810. }
  811. void bndIcon(NVGcontext *ctx, float x, float y, int iconid) {
  812. int ix, iy, u, v;
  813. if (bnd_icon_image < 0) return; // no icons loaded
  814. ix = iconid & 0xff;
  815. iy = (iconid>>8) & 0xff;
  816. u = BND_ICON_SHEET_OFFSET_X + ix*BND_ICON_SHEET_GRID;
  817. v = BND_ICON_SHEET_OFFSET_Y + iy*BND_ICON_SHEET_GRID;
  818. nvgBeginPath(ctx);
  819. nvgRect(ctx,x,y,BND_ICON_SHEET_RES,BND_ICON_SHEET_RES);
  820. nvgFillPaint(ctx,
  821. nvgImagePattern(ctx,x-u,y-v,
  822. BND_ICON_SHEET_WIDTH,
  823. BND_ICON_SHEET_HEIGHT,
  824. 0,bnd_icon_image,0,1));
  825. nvgFill(ctx);
  826. }
  827. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  828. float r, float feather, float alpha) {
  829. nvgBeginPath(ctx);
  830. y += feather;
  831. h -= feather;
  832. nvgMoveTo(ctx, x-feather, y-feather);
  833. nvgLineTo(ctx, x, y-feather);
  834. nvgLineTo(ctx, x, y+h-feather);
  835. nvgArcTo(ctx, x,y+h,x+r,y+h,r);
  836. nvgArcTo(ctx, x+w,y+h,x+w,y+h-r,r);
  837. nvgLineTo(ctx, x+w, y-feather);
  838. nvgLineTo(ctx, x+w+feather, y-feather);
  839. nvgLineTo(ctx, x+w+feather, y+h+feather);
  840. nvgLineTo(ctx, x-feather, y+h+feather);
  841. nvgClosePath(ctx);
  842. nvgFillPaint(ctx, nvgBoxGradient(ctx,
  843. x - feather*0.5f,y - feather*0.5f,
  844. w + feather,h+feather,
  845. r+feather*0.5f,
  846. feather,
  847. nvgRGBAf(0,0,0,alpha*alpha),
  848. nvgRGBAf(0,0,0,0)));
  849. nvgFill(ctx);
  850. }
  851. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  852. float cr0, float cr1, float cr2, float cr3,
  853. NVGcolor shade_top, NVGcolor shade_down) {
  854. nvgBeginPath(ctx);
  855. bndRoundedBox(ctx,x+1,y+1,w-2,h-3,
  856. fmaxf(0,cr0-1),fmaxf(0,cr1-1),fmaxf(0,cr2-1),fmaxf(0,cr3-1));
  857. nvgFillPaint(ctx,((h-2)>w)?
  858. nvgLinearGradient(ctx,x,y,x+w,y,shade_top,shade_down):
  859. nvgLinearGradient(ctx,x,y,x,y+h,shade_top,shade_down));
  860. nvgFill(ctx);
  861. }
  862. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  863. float cr0, float cr1, float cr2, float cr3, NVGcolor color) {
  864. nvgBeginPath(ctx);
  865. bndRoundedBox(ctx,x+0.5f,y+0.5f,w-1,h-2,cr0,cr1,cr2,cr3);
  866. nvgStrokeColor(ctx,color);
  867. nvgStrokeWidth(ctx,1);
  868. nvgStroke(ctx);
  869. }
  870. void bndSelectCorners(float *radiuses, float r, int flags) {
  871. radiuses[0] = (flags & BND_CORNER_TOP_LEFT)?0:r;
  872. radiuses[1] = (flags & BND_CORNER_TOP_RIGHT)?0:r;
  873. radiuses[2] = (flags & BND_CORNER_DOWN_RIGHT)?0:r;
  874. radiuses[3] = (flags & BND_CORNER_DOWN_LEFT)?0:r;
  875. }
  876. void bndInnerColors(
  877. NVGcolor *shade_top, NVGcolor *shade_down,
  878. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive) {
  879. switch(state) {
  880. default:
  881. case BND_DEFAULT: {
  882. *shade_top = bndOffsetColor(theme->innerColor, theme->shadeTop);
  883. *shade_down = bndOffsetColor(theme->innerColor, theme->shadeDown);
  884. } break;
  885. case BND_HOVER: {
  886. NVGcolor color = bndOffsetColor(theme->innerColor, BND_HOVER_SHADE);
  887. *shade_top = bndOffsetColor(color, theme->shadeTop);
  888. *shade_down = bndOffsetColor(color, theme->shadeDown);
  889. } break;
  890. case BND_ACTIVE: {
  891. *shade_top = bndOffsetColor(theme->innerSelectedColor,
  892. flipActive?theme->shadeDown:theme->shadeTop);
  893. *shade_down = bndOffsetColor(theme->innerSelectedColor,
  894. flipActive?theme->shadeTop:theme->shadeDown);
  895. } break;
  896. }
  897. }
  898. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state) {
  899. return (state == BND_ACTIVE)?theme->textSelectedColor:theme->textColor;
  900. }
  901. void bndIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
  902. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  903. const char *value) {
  904. float pleft = BND_PAD_LEFT;
  905. if (label) {
  906. if (iconid >= 0) {
  907. bndIcon(ctx,x+4,y+2,iconid);
  908. pleft += BND_ICON_SHEET_RES;
  909. }
  910. if (bnd_font < 0) return;
  911. nvgFontFaceId(ctx, bnd_font);
  912. nvgFontSize(ctx, fontsize);
  913. nvgBeginPath(ctx);
  914. nvgFillColor(ctx, color);
  915. if (value) {
  916. float label_width = nvgTextBounds(ctx, 1, 1, label, NULL, NULL);
  917. float sep_width = nvgTextBounds(ctx, 1, 1,
  918. BND_LABEL_SEPARATOR, NULL, NULL);
  919. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  920. x += pleft;
  921. if (align == BND_CENTER) {
  922. float width = label_width + sep_width
  923. + nvgTextBounds(ctx, 1, 1, value, NULL, NULL);
  924. x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
  925. }
  926. y += h-6;
  927. nvgText(ctx, x, y, label, NULL);
  928. x += label_width;
  929. nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL);
  930. x += sep_width;
  931. nvgText(ctx, x, y, value, NULL);
  932. } else {
  933. nvgTextAlign(ctx,
  934. (align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE):
  935. (NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE));
  936. nvgTextBox(ctx,x+pleft,y+h-6,w-BND_PAD_RIGHT-pleft,label, NULL);
  937. }
  938. } else if (iconid >= 0) {
  939. bndIcon(ctx,x+2,y+2,iconid);
  940. }
  941. }
  942. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color) {
  943. nvgBeginPath(ctx);
  944. nvgStrokeWidth(ctx,2);
  945. nvgStrokeColor(ctx,color);
  946. nvgLineCap(ctx,NVG_BUTT);
  947. nvgLineJoin(ctx,NVG_MITER);
  948. nvgMoveTo(ctx,ox+4,oy+5);
  949. nvgLineTo(ctx,ox+7,oy+8);
  950. nvgLineTo(ctx,ox+14,oy+1);
  951. nvgStroke(ctx);
  952. }
  953. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  954. nvgBeginPath(ctx);
  955. nvgMoveTo(ctx,x,y);
  956. nvgLineTo(ctx,x-s,y+s);
  957. nvgLineTo(ctx,x-s,y-s);
  958. nvgClosePath(ctx);
  959. nvgFillColor(ctx,color);
  960. nvgFill(ctx);
  961. }
  962. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  963. float w;
  964. nvgBeginPath(ctx);
  965. w = 1.1f*s;
  966. nvgMoveTo(ctx,x,y-1);
  967. nvgLineTo(ctx,x+0.5*w,y-s-1);
  968. nvgLineTo(ctx,x+w,y-1);
  969. nvgClosePath(ctx);
  970. nvgMoveTo(ctx,x,y+1);
  971. nvgLineTo(ctx,x+0.5*w,y+s+1);
  972. nvgLineTo(ctx,x+w,y+1);
  973. nvgClosePath(ctx);
  974. nvgFillColor(ctx,color);
  975. nvgFill(ctx);
  976. }
  977. ////////////////////////////////////////////////////////////////////////////////
  978. #ifdef BND_INLINE
  979. #undef BND_INLINE
  980. #endif
  981. #endif // BLENDISH_IMPLEMENTATION