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.

1314 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 4 (2014-07-09)
  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. // default vertical spacing
  165. #define BND_VSPACING 1
  166. // default vertical spacing between groups
  167. #define BND_VSPACING_GROUP 8
  168. // default horizontal spacing
  169. #define BND_HSPACING 8
  170. ////////////////////////////////////////////////////////////////////////////////
  171. // set the current theme all widgets will be drawn with.
  172. // the default Blender 2.6 theme is set by default.
  173. void bndSetTheme(BNDtheme theme);
  174. // Returns the currently set theme
  175. const BNDtheme *bndGetTheme();
  176. // designates an image handle as returned by nvgCreateImage*() as the themes'
  177. // icon sheet. The icon sheet format must be compatible to Blender 2.6's icon
  178. // sheet; the order of icons does not matter.
  179. // A valid icon sheet is e.g. shown at
  180. // http://wiki.blender.org/index.php/Dev:2.5/Doc/How_to/Add_an_icon
  181. void bndSetIconImage(int image);
  182. // designates an image handle as returned by nvgCreateFont*() as the themes'
  183. // UI font. Blender's original UI font Droid Sans is perfectly suited and
  184. // available here:
  185. // https://svn.blender.org/svnroot/bf-blender/trunk/blender/release/datafiles/fonts/
  186. void bndSetFont(int font);
  187. ////////////////////////////////////////////////////////////////////////////////
  188. // High Level Functions
  189. // --------------------
  190. // Use these functions to draw themed widgets with your NVGcontext.
  191. // Draw a label with its lower left origin at (x,y) and size of (w,h).
  192. // if iconid >= 0, an icon will be added to the widget
  193. // if label is not NULL, a label will be added to the widget
  194. // widget looks best when height is BND_WIDGET_HEIGHT
  195. void bndLabel(NVGcontext *ctx,
  196. float x, float y, float w, float h, int iconid, const char *label);
  197. // Draw a tool button with its lower left origin at (x,y) and size of (w,h),
  198. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  199. // the widgets current UI state.
  200. // if iconid >= 0, an icon will be added to the widget
  201. // if label is not NULL, a label will be added to the widget
  202. // widget looks best when height is BND_WIDGET_HEIGHT
  203. void bndToolButton(NVGcontext *ctx,
  204. float x, float y, float w, float h, int flags, BNDwidgetState state,
  205. int iconid, const char *label);
  206. // Draw a radio button with its lower left origin at (x,y) and size of (w,h),
  207. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  208. // the widgets current UI state.
  209. // if iconid >= 0, an icon will be added to the widget
  210. // if label is not NULL, a label will be added to the widget
  211. // widget looks best when height is BND_WIDGET_HEIGHT
  212. void bndRadioButton(NVGcontext *ctx,
  213. float x, float y, float w, float h, int flags, BNDwidgetState state,
  214. int iconid, const char *label);
  215. // Draw a text field with its lower left origin at (x,y) and size of (w,h),
  216. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  217. // the widgets current UI state.
  218. // if iconid >= 0, an icon will be added to the widget
  219. // if text is not NULL, text will be printed to the widget
  220. // cbegin must be >= 0 and <= strlen(text) and denotes the beginning of the caret
  221. // cend must be >= cbegin and <= strlen(text) and denotes the end of the caret
  222. // if cend < cbegin, then no caret will be drawn
  223. // widget looks best when height is BND_WIDGET_HEIGHT
  224. void bndTextField(NVGcontext *ctx,
  225. float x, float y, float w, float h, int flags, BNDwidgetState state,
  226. int iconid, const char *text, int cbegin, int cend);
  227. // Draw an option button with its lower left origin at (x,y) and size of (w,h),
  228. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  229. // the widgets current UI state.
  230. // if label is not NULL, a label will be added to the widget
  231. // widget looks best when height is BND_WIDGET_HEIGHT
  232. void bndOptionButton(NVGcontext *ctx,
  233. float x, float y, float w, float h, BNDwidgetState state,
  234. const char *label);
  235. // Draw a choice button with its lower left origin at (x,y) and size of (w,h),
  236. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  237. // the widgets current UI state.
  238. // if iconid >= 0, an icon will be added to the widget
  239. // if label is not NULL, a label will be added to the widget
  240. // widget looks best when height is BND_WIDGET_HEIGHT
  241. void bndChoiceButton(NVGcontext *ctx,
  242. float x, float y, float w, float h, int flags, BNDwidgetState state,
  243. int iconid, const char *label);
  244. // Draw a number field with its lower left origin at (x,y) and size of (w,h),
  245. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  246. // the widgets current UI state.
  247. // if label is not NULL, a label will be added to the widget
  248. // if value is not NULL, a value will be added to the widget, along with
  249. // a ":" separator
  250. // widget looks best when height is BND_WIDGET_HEIGHT
  251. void bndNumberField(NVGcontext *ctx,
  252. float x, float y, float w, float h, int flags, BNDwidgetState state,
  253. const char *label, const char *value);
  254. // Draw slider control with its lower left origin at (x,y) and size of (w,h),
  255. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  256. // the widgets current UI state.
  257. // progress must be in the range 0..1 and controls the size of the slider bar
  258. // if label is not NULL, a label will be added to the widget
  259. // if value is not NULL, a value will be added to the widget, along with
  260. // a ":" separator
  261. // widget looks best when height is BND_WIDGET_HEIGHT
  262. void bndSlider(NVGcontext *ctx,
  263. float x, float y, float w, float h, int flags, BNDwidgetState state,
  264. float progress, const char *label, const char *value);
  265. // Draw scrollbar with its lower left origin at (x,y) and size of (w,h),
  266. // where state denotes the widgets current UI state.
  267. // offset is in the range 0..1 and controls the position of the scroll handle
  268. // size is in the range 0..1 and controls the size of the scroll handle
  269. // horizontal widget looks best when height is BND_SCROLLBAR_HEIGHT,
  270. // vertical looks best when width is BND_SCROLLBAR_WIDTH
  271. void bndScrollBar(NVGcontext *ctx,
  272. float x, float y, float w, float h, BNDwidgetState state,
  273. float offset, float size);
  274. // Draw a menu background with its lower left origin at (x,y) and size of (w,h),
  275. // where flags is one or multiple flags from BNDcornerFlags.
  276. void bndMenuBackground(NVGcontext *ctx,
  277. float x, float y, float w, float h, int flags);
  278. // Draw a menu label with its lower left origin at (x,y) and size of (w,h).
  279. // if iconid >= 0, an icon will be added to the widget
  280. // if label is not NULL, a label will be added to the widget
  281. // widget looks best when height is BND_WIDGET_HEIGHT
  282. void bndMenuLabel(NVGcontext *ctx,
  283. float x, float y, float w, float h, int iconid, const char *label);
  284. // Draw a menu item with its lower left origin at (x,y) and size of (w,h),
  285. // where state denotes the widgets current UI state.
  286. // if iconid >= 0, an icon will be added to the widget
  287. // if label is not NULL, a label will be added to the widget
  288. // widget looks best when height is BND_WIDGET_HEIGHT
  289. void bndMenuItem(NVGcontext *ctx,
  290. float x, float y, float w, float h, BNDwidgetState state,
  291. int iconid, const char *label);
  292. // Draw a tooltip background with its lower left origin at (x,y) and size of (w,h)
  293. void bndTooltipBackground(NVGcontext *ctx, float x, float y, float w, float h);
  294. ////////////////////////////////////////////////////////////////////////////////
  295. // Low Level Functions
  296. // -------------------
  297. // these are part of the implementation detail and can be used to theme
  298. // new kinds of controls in a similar fashion.
  299. // make color transparent using the default alpha value
  300. NVGcolor bndTransparent(NVGcolor color);
  301. // offset a color by a given integer delta in the range -100 to 100
  302. NVGcolor bndOffsetColor(NVGcolor color, int delta);
  303. // assigns radius r to the four entries of array radiuses depending on whether
  304. // the corner is marked as sharp or not; see BNDcornerFlags for possible
  305. // flag values.
  306. void bndSelectCorners(float *radiuses, float r, int flags);
  307. // computes the upper and lower gradient colors for the inner box from a widget
  308. // theme and the widgets state. If flipActive is set and the state is
  309. // BND_ACTIVE, the upper and lower colors will be swapped.
  310. void bndInnerColors(NVGcolor *shade_top, NVGcolor *shade_down,
  311. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive);
  312. // computes the text color for a widget label from a widget theme and the
  313. // widgets state.
  314. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state);
  315. // computes the bounds of the scrollbar handle from the scrollbar size
  316. // and the handles offset and size.
  317. // offset is in the range 0..1 and defines the position of the scroll handle
  318. // size is in the range 0..1 and defines the size of the scroll handle
  319. void bndScrollHandleRect(float *x, float *y, float *w, float *h,
  320. float offset, float size);
  321. // Add a rounded box path at position (x,y) with size (w,h) and a separate
  322. // radius for each corner listed in clockwise order, so that cr0 = top left,
  323. // cr1 = top right, cr2 = bottom right, cr3 = bottom left;
  324. // this is a low level drawing function: the path must be stroked or filled
  325. // to become visible.
  326. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  327. float cr0, float cr1, float cr2, float cr3);
  328. // Draw a flat panel without any decorations at position (x,y) with size (w,h)
  329. // and fills it with backgroundColor
  330. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h);
  331. // Draw a lower inset for a rounded box at position (x,y) with size (w,h)
  332. // that gives the impression the surface has been pushed in.
  333. // cr2 and cr3 contain the radiuses of the bottom right and bottom left
  334. // corners of the rounded box.
  335. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  336. float cr2, float cr3);
  337. // Draw an icon with (x,y) as its upper left coordinate; the iconid selects
  338. // the icon from the sheet; use the BND_ICONID macro to build icon IDs.
  339. void bndIcon(NVGcontext *ctx, float x, float y, int iconid);
  340. // Draw a drop shadow around the rounded box at (x,y) with size (w,h) and
  341. // radius r, with feather as its maximum range in pixels.
  342. // No shadow will be painted inside the rounded box.
  343. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  344. float r, float feather, float alpha);
  345. // Draw the inner part of a widget box, with a gradient from shade_top to
  346. // shade_down. If h>w, the gradient will be horizontal instead of
  347. // vertical.
  348. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  349. float cr0, float cr1, float cr2, float cr3,
  350. NVGcolor shade_top, NVGcolor shade_down);
  351. // Draw the outline part of a widget box with the given color
  352. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  353. float cr0, float cr1, float cr2, float cr3, NVGcolor color);
  354. // Draw an optional icon specified by <iconid> and an optional label with
  355. // given alignment (BNDtextAlignment), fontsize and color within a widget box.
  356. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  357. // will be adjusted.
  358. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  359. // and color.
  360. // if value is not NULL, label and value will be drawn with a ":" separator
  361. // inbetween.
  362. void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
  363. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  364. const char *value);
  365. // Draw an optional icon specified by <iconid>, an optional label and
  366. // a caret with given fontsize and color within a widget box.
  367. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  368. // will be adjusted.
  369. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  370. // and color.
  371. // cbegin must be >= 0 and <= strlen(text) and denotes the beginning of the caret
  372. // cend must be >= cbegin and <= strlen(text) and denotes the end of the caret
  373. // if cend < cbegin, then no caret will be drawn
  374. void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
  375. int iconid, NVGcolor color, float fontsize, const char *label,
  376. NVGcolor caretcolor, int cbegin, int cend);
  377. // Draw a checkmark for an option box with the given upper left coordinates
  378. // (ox,oy) with the specified color.
  379. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color);
  380. // Draw a horizontal arrow for a number field with its center at (x,y) and
  381. // size s; if s is negative, the arrow points to the left.
  382. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  383. // Draw an up/down arrow for a choice box with its center at (x,y) and size s
  384. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  385. #ifdef __cplusplus
  386. };
  387. #endif
  388. #endif // BLENDISH_H
  389. ////////////////////////////////////////////////////////////////////////////////
  390. ////////////////////////////////////////////////////////////////////////////////
  391. #ifdef BLENDISH_IMPLEMENTATION
  392. #include <memory.h>
  393. #include <math.h>
  394. #ifdef _MSC_VER
  395. #pragma warning (disable: 4996) // Switch off security warnings
  396. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  397. #ifdef __cplusplus
  398. #define BND_INLINE inline
  399. #else
  400. #define BND_INLINE
  401. #endif
  402. #else
  403. #define BND_INLINE inline
  404. #endif
  405. ////////////////////////////////////////////////////////////////////////////////
  406. // default text size
  407. #define BND_LABEL_FONT_SIZE 13
  408. // default text padding in inner box
  409. #define BND_PAD_LEFT 8
  410. #define BND_PAD_RIGHT 8
  411. // label: value separator string
  412. #define BND_LABEL_SEPARATOR ": "
  413. // alpha intensity of transparent items (0xa4)
  414. #define BND_TRANSPARENT_ALPHA 0.643
  415. // shade intensity of beveled insets
  416. #define BND_BEVEL_SHADE 30
  417. // shade intensity of hovered inner boxes
  418. #define BND_HOVER_SHADE 15
  419. // width of icon sheet
  420. #define BND_ICON_SHEET_WIDTH 602
  421. // height of icon sheet
  422. #define BND_ICON_SHEET_HEIGHT 640
  423. // gridsize of icon sheet in both dimensions
  424. #define BND_ICON_SHEET_GRID 21
  425. // offset of first icon tile relative to left border
  426. #define BND_ICON_SHEET_OFFSET_X 5
  427. // offset of first icon tile relative to top border
  428. #define BND_ICON_SHEET_OFFSET_Y 10
  429. // resolution of single icon
  430. #define BND_ICON_SHEET_RES 16
  431. // size of number field arrow
  432. #define BND_NUMBER_ARROW_SIZE 4
  433. // default text color
  434. #define BND_COLOR_TEXT {{{ 0,0,0,1 }}}
  435. // default highlighted text color
  436. #define BND_COLOR_TEXT_SELECTED {{{ 1,1,1,1 }}}
  437. // radius of tool button
  438. #define BND_TOOL_RADIUS 4
  439. // radius of option button
  440. #define BND_OPTION_RADIUS 4
  441. // width of option button checkbox
  442. #define BND_OPTION_WIDTH 14
  443. // height of option button checkbox
  444. #define BND_OPTION_HEIGHT 15
  445. // radius of text field
  446. #define BND_TEXT_RADIUS 4
  447. // radius of number button
  448. #define BND_NUMBER_RADIUS 10
  449. // radius of menu popup
  450. #define BND_MENU_RADIUS 3
  451. // feather of menu popup shadow
  452. #define BND_SHADOW_FEATHER 12
  453. // alpha of menu popup shadow
  454. #define BND_SHADOW_ALPHA 0.5
  455. // radius of scrollbar
  456. #define BND_SCROLLBAR_RADIUS 7
  457. // shade intensity of active scrollbar
  458. #define BND_SCROLLBAR_ACTIVE_SHADE 15
  459. // max glyphs for position testing
  460. #define BND_MAX_GLYPHS 1024
  461. // text distance from bottom
  462. #define BND_TEXT_PAD_DOWN 7
  463. ////////////////////////////////////////////////////////////////////////////////
  464. BND_INLINE float bnd_clamp(float v, float mn, float mx) {
  465. return (v > mx)?mx:(v < mn)?mn:v;
  466. }
  467. ////////////////////////////////////////////////////////////////////////////////
  468. // the initial theme
  469. static BNDtheme bnd_theme = {
  470. // backgroundColor
  471. {{{ 0.447, 0.447, 0.447, 1.0 }}},
  472. // regularTheme
  473. {
  474. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  475. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  476. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  477. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  478. BND_COLOR_TEXT, // color_text
  479. BND_COLOR_TEXT_SELECTED, // color_text_selected
  480. 0, // shade_top
  481. 0, // shade_down
  482. },
  483. // toolTheme
  484. {
  485. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  486. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  487. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  488. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  489. BND_COLOR_TEXT, // color_text
  490. BND_COLOR_TEXT_SELECTED, // color_text_selected
  491. 15, // shade_top
  492. -15, // shade_down
  493. },
  494. // radioTheme
  495. {
  496. {{{ 0,0,0,1 }}}, // color_outline
  497. {{{ 1,1,1,1 }}}, // color_item
  498. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  499. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  500. BND_COLOR_TEXT_SELECTED, // color_text
  501. BND_COLOR_TEXT, // color_text_selected
  502. 15, // shade_top
  503. -15, // shade_down
  504. },
  505. // textFieldTheme
  506. {
  507. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  508. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  509. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner
  510. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  511. BND_COLOR_TEXT, // color_text
  512. BND_COLOR_TEXT_SELECTED, // color_text_selected
  513. 0, // shade_top
  514. 25, // shade_down
  515. },
  516. // optionTheme
  517. {
  518. {{{ 0,0,0,1 }}}, // color_outline
  519. {{{ 1,1,1,1 }}}, // color_item
  520. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  521. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  522. BND_COLOR_TEXT, // color_text
  523. BND_COLOR_TEXT_SELECTED, // color_text_selected
  524. 15, // shade_top
  525. -15, // shade_down
  526. },
  527. // choiceTheme
  528. {
  529. {{{ 0,0,0,1 }}}, // color_outline
  530. {{{ 1,1,1,1 }}}, // color_item
  531. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  532. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  533. BND_COLOR_TEXT_SELECTED, // color_text
  534. {{{ 0.8,0.8,0.8,1 }}}, // color_text_selected
  535. 15, // shade_top
  536. -15, // shade_down
  537. },
  538. // numberFieldTheme
  539. {
  540. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  541. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  542. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  543. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  544. BND_COLOR_TEXT, // color_text
  545. BND_COLOR_TEXT_SELECTED, // color_text_selected
  546. -20, // shade_top
  547. 0, // shade_down
  548. },
  549. // sliderTheme
  550. {
  551. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  552. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  553. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  554. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  555. BND_COLOR_TEXT, // color_text
  556. BND_COLOR_TEXT_SELECTED, // color_text_selected
  557. -20, // shade_top
  558. 0, // shade_down
  559. },
  560. // scrollBarTheme
  561. {
  562. {{{ 0.196,0.196,0.196,1 }}}, // color_outline
  563. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  564. {{{ 0.314, 0.314, 0.314,0.706 }}}, // color_inner
  565. {{{ 0.392, 0.392, 0.392,0.706 }}}, // color_inner_selected
  566. BND_COLOR_TEXT, // color_text
  567. BND_COLOR_TEXT_SELECTED, // color_text_selected
  568. 5, // shade_top
  569. -5, // shade_down
  570. },
  571. // tooltipTheme
  572. {
  573. {{{ 0,0,0,1 }}}, // color_outline
  574. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  575. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  576. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  577. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  578. BND_COLOR_TEXT_SELECTED, // color_text_selected
  579. 0, // shade_top
  580. 0, // shade_down
  581. },
  582. // menuTheme
  583. {
  584. {{{ 0,0,0,1 }}}, // color_outline
  585. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  586. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  587. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  588. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  589. BND_COLOR_TEXT_SELECTED, // color_text_selected
  590. 0, // shade_top
  591. 0, // shade_down
  592. },
  593. // menuItemTheme
  594. {
  595. {{{ 0,0,0,1 }}}, // color_outline
  596. {{{ 0.675,0.675,0.675,0.502 }}}, // color_item
  597. {{{ 0,0,0,0 }}}, // color_inner
  598. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  599. BND_COLOR_TEXT_SELECTED, // color_text
  600. BND_COLOR_TEXT, // color_text_selected
  601. 38, // shade_top
  602. 0, // shade_down
  603. },
  604. };
  605. ////////////////////////////////////////////////////////////////////////////////
  606. void bndSetTheme(BNDtheme theme) {
  607. bnd_theme = theme;
  608. }
  609. const BNDtheme *bndGetTheme() {
  610. return &bnd_theme;
  611. }
  612. // the handle to the image containing the icon sheet
  613. static int bnd_icon_image = -1;
  614. void bndSetIconImage(int image) {
  615. bnd_icon_image = image;
  616. }
  617. // the handle to the UI font
  618. static int bnd_font = -1;
  619. void bndSetFont(int font) {
  620. bnd_font = font;
  621. }
  622. ////////////////////////////////////////////////////////////////////////////////
  623. void bndLabel(NVGcontext *ctx,
  624. float x, float y, float w, float h, int iconid, const char *label) {
  625. bndIconLabelValue(ctx,x,y,w,h,iconid,
  626. bnd_theme.regularTheme.textColor, BND_LEFT,
  627. BND_LABEL_FONT_SIZE, label, NULL);
  628. }
  629. void bndToolButton(NVGcontext *ctx,
  630. float x, float y, float w, float h, int flags, BNDwidgetState state,
  631. int iconid, const char *label) {
  632. float cr[4];
  633. NVGcolor shade_top, shade_down;
  634. bndSelectCorners(cr, BND_TOOL_RADIUS, flags);
  635. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  636. bndInnerColors(&shade_top, &shade_down, &bnd_theme.toolTheme, state, 1);
  637. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  638. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  639. bndTransparent(bnd_theme.toolTheme.outlineColor));
  640. bndIconLabelValue(ctx,x,y,w,h,iconid,
  641. bndTextColor(&bnd_theme.toolTheme, state), BND_CENTER,
  642. BND_LABEL_FONT_SIZE, label, NULL);
  643. }
  644. void bndRadioButton(NVGcontext *ctx,
  645. float x, float y, float w, float h, int flags, BNDwidgetState state,
  646. int iconid, const char *label) {
  647. float cr[4];
  648. NVGcolor shade_top, shade_down;
  649. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  650. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  651. bndInnerColors(&shade_top, &shade_down, &bnd_theme.radioTheme, state, 1);
  652. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  653. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  654. bndTransparent(bnd_theme.radioTheme.outlineColor));
  655. bndIconLabelValue(ctx,x,y,w,h,iconid,
  656. bndTextColor(&bnd_theme.radioTheme, state), BND_CENTER,
  657. BND_LABEL_FONT_SIZE, label, NULL);
  658. }
  659. void bndTextField(NVGcontext *ctx,
  660. float x, float y, float w, float h, int flags, BNDwidgetState state,
  661. int iconid, const char *text, int cbegin, int cend) {
  662. float cr[4];
  663. NVGcolor shade_top, shade_down;
  664. bndSelectCorners(cr, BND_TEXT_RADIUS, flags);
  665. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  666. bndInnerColors(&shade_top, &shade_down, &bnd_theme.textFieldTheme, state, 0);
  667. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  668. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  669. bndTransparent(bnd_theme.textFieldTheme.outlineColor));
  670. if (state != BND_ACTIVE) {
  671. cend = -1;
  672. }
  673. bndIconLabelCaret(ctx,x,y,w,h,iconid,
  674. bndTextColor(&bnd_theme.textFieldTheme, state), BND_LABEL_FONT_SIZE,
  675. text, bnd_theme.textFieldTheme.itemColor, cbegin, cend);
  676. }
  677. void bndOptionButton(NVGcontext *ctx,
  678. float x, float y, float w, float h, BNDwidgetState state,
  679. const char *label) {
  680. float ox, oy;
  681. NVGcolor shade_top, shade_down;
  682. ox = x;
  683. oy = y+h-BND_OPTION_HEIGHT-3;
  684. bndBevelInset(ctx,ox,oy,
  685. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  686. BND_OPTION_RADIUS,BND_OPTION_RADIUS);
  687. bndInnerColors(&shade_top, &shade_down, &bnd_theme.optionTheme, state, 1);
  688. bndInnerBox(ctx,ox,oy,
  689. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  690. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  691. shade_top, shade_down);
  692. bndOutlineBox(ctx,ox,oy,
  693. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  694. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  695. bndTransparent(bnd_theme.optionTheme.outlineColor));
  696. if (state == BND_ACTIVE) {
  697. bndCheck(ctx,ox,oy, bndTransparent(bnd_theme.optionTheme.itemColor));
  698. }
  699. bndIconLabelValue(ctx,x+12,y,w-12,h,-1,
  700. bndTextColor(&bnd_theme.optionTheme, state), BND_LEFT,
  701. BND_LABEL_FONT_SIZE, label, NULL);
  702. }
  703. void bndChoiceButton(NVGcontext *ctx,
  704. float x, float y, float w, float h, int flags, BNDwidgetState state,
  705. int iconid, const char *label) {
  706. float cr[4];
  707. NVGcolor shade_top, shade_down;
  708. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  709. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  710. bndInnerColors(&shade_top, &shade_down, &bnd_theme.choiceTheme, state, 1);
  711. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  712. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  713. bndTransparent(bnd_theme.choiceTheme.outlineColor));
  714. bndIconLabelValue(ctx,x,y,w,h,iconid,
  715. bndTextColor(&bnd_theme.choiceTheme, state), BND_LEFT,
  716. BND_LABEL_FONT_SIZE, label, NULL);
  717. bndUpDownArrow(ctx,x+w-10,y+10,5,
  718. bndTransparent(bnd_theme.choiceTheme.itemColor));
  719. }
  720. void bndNumberField(NVGcontext *ctx,
  721. float x, float y, float w, float h, int flags, BNDwidgetState state,
  722. const char *label, const char *value) {
  723. float cr[4];
  724. NVGcolor shade_top, shade_down;
  725. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  726. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  727. bndInnerColors(&shade_top, &shade_down, &bnd_theme.numberFieldTheme, state, 0);
  728. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  729. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  730. bndTransparent(bnd_theme.numberFieldTheme.outlineColor));
  731. bndIconLabelValue(ctx,x,y,w,h,-1,
  732. bndTextColor(&bnd_theme.numberFieldTheme, state), BND_CENTER,
  733. BND_LABEL_FONT_SIZE, label, value);
  734. bndArrow(ctx,x+8,y+10,-BND_NUMBER_ARROW_SIZE,
  735. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  736. bndArrow(ctx,x+w-8,y+10,BND_NUMBER_ARROW_SIZE,
  737. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  738. }
  739. void bndSlider(NVGcontext *ctx,
  740. float x, float y, float w, float h, int flags, BNDwidgetState state,
  741. float progress, const char *label, const char *value) {
  742. float cr[4];
  743. NVGcolor shade_top, shade_down;
  744. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  745. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  746. bndInnerColors(&shade_top, &shade_down, &bnd_theme.sliderTheme, state, 0);
  747. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  748. if (state == BND_ACTIVE) {
  749. shade_top = bndOffsetColor(
  750. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  751. shade_down = bndOffsetColor(
  752. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  753. } else {
  754. shade_top = bndOffsetColor(
  755. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  756. shade_down = bndOffsetColor(
  757. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  758. }
  759. nvgScissor(ctx,x,y,8+(w-8)*bnd_clamp(progress,0,1),h);
  760. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  761. nvgResetScissor(ctx);
  762. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  763. bndTransparent(bnd_theme.sliderTheme.outlineColor));
  764. bndIconLabelValue(ctx,x,y,w,h,-1,
  765. bndTextColor(&bnd_theme.sliderTheme, state), BND_CENTER,
  766. BND_LABEL_FONT_SIZE, label, value);
  767. }
  768. void bndScrollBar(NVGcontext *ctx,
  769. float x, float y, float w, float h, BNDwidgetState state,
  770. float offset, float size) {
  771. bndBevelInset(ctx,x,y,w,h,
  772. BND_SCROLLBAR_RADIUS, BND_SCROLLBAR_RADIUS);
  773. bndInnerBox(ctx,x,y,w,h,
  774. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  775. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  776. bndOffsetColor(
  777. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeDown),
  778. bndOffsetColor(
  779. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeTop));
  780. bndOutlineBox(ctx,x,y,w,h,
  781. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  782. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  783. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  784. NVGcolor itemColor = bndOffsetColor(
  785. bnd_theme.scrollBarTheme.itemColor,
  786. (state == BND_ACTIVE)?BND_SCROLLBAR_ACTIVE_SHADE:0);
  787. bndScrollHandleRect(&x,&y,&w,&h,offset,size);
  788. bndInnerBox(ctx,x,y,w,h,
  789. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  790. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  791. bndOffsetColor(
  792. itemColor, 3*bnd_theme.scrollBarTheme.shadeTop),
  793. bndOffsetColor(
  794. itemColor, 3*bnd_theme.scrollBarTheme.shadeDown));
  795. bndOutlineBox(ctx,x,y,w,h,
  796. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  797. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  798. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  799. }
  800. void bndMenuBackground(NVGcontext *ctx,
  801. float x, float y, float w, float h, int flags) {
  802. float cr[4];
  803. NVGcolor shade_top, shade_down;
  804. bndSelectCorners(cr, BND_MENU_RADIUS, flags);
  805. bndInnerColors(&shade_top, &shade_down, &bnd_theme.menuTheme,
  806. BND_DEFAULT, 0);
  807. bndInnerBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  808. bndOutlineBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3],
  809. bndTransparent(bnd_theme.menuTheme.outlineColor));
  810. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  811. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  812. }
  813. void bndTooltipBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  814. NVGcolor shade_top, shade_down;
  815. bndInnerColors(&shade_top, &shade_down, &bnd_theme.tooltipTheme,
  816. BND_DEFAULT, 0);
  817. bndInnerBox(ctx,x,y,w,h+1,
  818. BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,
  819. shade_top, shade_down);
  820. bndOutlineBox(ctx,x,y,w,h+1,
  821. BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,
  822. bndTransparent(bnd_theme.tooltipTheme.outlineColor));
  823. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  824. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  825. }
  826. void bndMenuLabel(NVGcontext *ctx,
  827. float x, float y, float w, float h, int iconid, const char *label) {
  828. bndIconLabelValue(ctx,x,y,w,h,iconid,
  829. bnd_theme.menuTheme.textColor, BND_LEFT,
  830. BND_LABEL_FONT_SIZE, label, NULL);
  831. }
  832. void bndMenuItem(NVGcontext *ctx,
  833. float x, float y, float w, float h, BNDwidgetState state,
  834. int iconid, const char *label) {
  835. if (state != BND_DEFAULT) {
  836. bndInnerBox(ctx,x,y,w,h,0,0,0,0,
  837. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  838. bnd_theme.menuItemTheme.shadeTop),
  839. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  840. bnd_theme.menuItemTheme.shadeDown));
  841. state = BND_ACTIVE;
  842. }
  843. bndIconLabelValue(ctx,x,y,w,h,iconid,
  844. bndTextColor(&bnd_theme.menuItemTheme, state), BND_LEFT,
  845. BND_LABEL_FONT_SIZE, label, NULL);
  846. }
  847. ////////////////////////////////////////////////////////////////////////////////
  848. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  849. float cr0, float cr1, float cr2, float cr3) {
  850. float d;
  851. w = fmaxf(0, w);
  852. h = fmaxf(0, h);
  853. d = fminf(w, h);
  854. nvgMoveTo(ctx, x,y+h*0.5f);
  855. nvgArcTo(ctx, x,y, x+w,y, fminf(cr0, d/2));
  856. nvgArcTo(ctx, x+w,y, x+w,y+h, fminf(cr1, d/2));
  857. nvgArcTo(ctx, x+w,y+h, x,y+h, fminf(cr2, d/2));
  858. nvgArcTo(ctx, x,y+h, x,y, fminf(cr3, d/2));
  859. nvgClosePath(ctx);
  860. }
  861. NVGcolor bndTransparent(NVGcolor color) {
  862. color.a *= BND_TRANSPARENT_ALPHA;
  863. return color;
  864. }
  865. NVGcolor bndOffsetColor(NVGcolor color, int delta) {
  866. float offset = (float)delta / 255.0f;
  867. return delta?(
  868. nvgRGBAf(
  869. bnd_clamp(color.r+offset,0,1),
  870. bnd_clamp(color.g+offset,0,1),
  871. bnd_clamp(color.b+offset,0,1),
  872. color.a)
  873. ):color;
  874. }
  875. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  876. float cr2, float cr3) {
  877. float d;
  878. y -= 0.5f;
  879. d = fminf(w, h);
  880. cr2 = fminf(cr2, d/2);
  881. cr3 = fminf(cr3, d/2);
  882. nvgBeginPath(ctx);
  883. nvgMoveTo(ctx, x+w,y+h-cr2);
  884. nvgArcTo(ctx, x+w,y+h, x,y+h, cr2);
  885. nvgArcTo(ctx, x,y+h, x,y, cr3);
  886. NVGcolor bevelColor = bndOffsetColor(bnd_theme.backgroundColor,
  887. BND_BEVEL_SHADE);
  888. nvgStrokeWidth(ctx, 1);
  889. nvgStrokePaint(ctx,
  890. nvgLinearGradient(ctx,
  891. x,y+h-fmaxf(cr2,cr3)-1,
  892. x,y+h-1,
  893. nvgRGBAf(bevelColor.r, bevelColor.g, bevelColor.b, 0),
  894. bevelColor));
  895. nvgStroke(ctx);
  896. }
  897. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  898. nvgBeginPath(ctx);
  899. nvgRect(ctx, x, y, w, h);
  900. nvgFillColor(ctx, bnd_theme.backgroundColor);
  901. nvgFill(ctx);
  902. }
  903. void bndIcon(NVGcontext *ctx, float x, float y, int iconid) {
  904. int ix, iy, u, v;
  905. if (bnd_icon_image < 0) return; // no icons loaded
  906. ix = iconid & 0xff;
  907. iy = (iconid>>8) & 0xff;
  908. u = BND_ICON_SHEET_OFFSET_X + ix*BND_ICON_SHEET_GRID;
  909. v = BND_ICON_SHEET_OFFSET_Y + iy*BND_ICON_SHEET_GRID;
  910. nvgBeginPath(ctx);
  911. nvgRect(ctx,x,y,BND_ICON_SHEET_RES,BND_ICON_SHEET_RES);
  912. nvgFillPaint(ctx,
  913. nvgImagePattern(ctx,x-u,y-v,
  914. BND_ICON_SHEET_WIDTH,
  915. BND_ICON_SHEET_HEIGHT,
  916. 0,bnd_icon_image,0,1));
  917. nvgFill(ctx);
  918. }
  919. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  920. float r, float feather, float alpha) {
  921. nvgBeginPath(ctx);
  922. y += feather;
  923. h -= feather;
  924. nvgMoveTo(ctx, x-feather, y-feather);
  925. nvgLineTo(ctx, x, y-feather);
  926. nvgLineTo(ctx, x, y+h-feather);
  927. nvgArcTo(ctx, x,y+h,x+r,y+h,r);
  928. nvgArcTo(ctx, x+w,y+h,x+w,y+h-r,r);
  929. nvgLineTo(ctx, x+w, y-feather);
  930. nvgLineTo(ctx, x+w+feather, y-feather);
  931. nvgLineTo(ctx, x+w+feather, y+h+feather);
  932. nvgLineTo(ctx, x-feather, y+h+feather);
  933. nvgClosePath(ctx);
  934. nvgFillPaint(ctx, nvgBoxGradient(ctx,
  935. x - feather*0.5f,y - feather*0.5f,
  936. w + feather,h+feather,
  937. r+feather*0.5f,
  938. feather,
  939. nvgRGBAf(0,0,0,alpha*alpha),
  940. nvgRGBAf(0,0,0,0)));
  941. nvgFill(ctx);
  942. }
  943. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  944. float cr0, float cr1, float cr2, float cr3,
  945. NVGcolor shade_top, NVGcolor shade_down) {
  946. nvgBeginPath(ctx);
  947. bndRoundedBox(ctx,x+1,y+1,w-2,h-3,
  948. fmaxf(0,cr0-1),fmaxf(0,cr1-1),fmaxf(0,cr2-1),fmaxf(0,cr3-1));
  949. nvgFillPaint(ctx,((h-2)>w)?
  950. nvgLinearGradient(ctx,x,y,x+w,y,shade_top,shade_down):
  951. nvgLinearGradient(ctx,x,y,x,y+h,shade_top,shade_down));
  952. nvgFill(ctx);
  953. }
  954. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  955. float cr0, float cr1, float cr2, float cr3, NVGcolor color) {
  956. nvgBeginPath(ctx);
  957. bndRoundedBox(ctx,x+0.5f,y+0.5f,w-1,h-2,cr0,cr1,cr2,cr3);
  958. nvgStrokeColor(ctx,color);
  959. nvgStrokeWidth(ctx,1);
  960. nvgStroke(ctx);
  961. }
  962. void bndSelectCorners(float *radiuses, float r, int flags) {
  963. radiuses[0] = (flags & BND_CORNER_TOP_LEFT)?0:r;
  964. radiuses[1] = (flags & BND_CORNER_TOP_RIGHT)?0:r;
  965. radiuses[2] = (flags & BND_CORNER_DOWN_RIGHT)?0:r;
  966. radiuses[3] = (flags & BND_CORNER_DOWN_LEFT)?0:r;
  967. }
  968. void bndInnerColors(
  969. NVGcolor *shade_top, NVGcolor *shade_down,
  970. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive) {
  971. switch(state) {
  972. default:
  973. case BND_DEFAULT: {
  974. *shade_top = bndOffsetColor(theme->innerColor, theme->shadeTop);
  975. *shade_down = bndOffsetColor(theme->innerColor, theme->shadeDown);
  976. } break;
  977. case BND_HOVER: {
  978. NVGcolor color = bndOffsetColor(theme->innerColor, BND_HOVER_SHADE);
  979. *shade_top = bndOffsetColor(color, theme->shadeTop);
  980. *shade_down = bndOffsetColor(color, theme->shadeDown);
  981. } break;
  982. case BND_ACTIVE: {
  983. *shade_top = bndOffsetColor(theme->innerSelectedColor,
  984. flipActive?theme->shadeDown:theme->shadeTop);
  985. *shade_down = bndOffsetColor(theme->innerSelectedColor,
  986. flipActive?theme->shadeTop:theme->shadeDown);
  987. } break;
  988. }
  989. }
  990. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state) {
  991. return (state == BND_ACTIVE)?theme->textSelectedColor:theme->textColor;
  992. }
  993. void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
  994. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  995. const char *value) {
  996. float pleft = BND_PAD_LEFT;
  997. if (label) {
  998. if (iconid >= 0) {
  999. bndIcon(ctx,x+4,y+2,iconid);
  1000. pleft += BND_ICON_SHEET_RES;
  1001. }
  1002. if (bnd_font < 0) return;
  1003. nvgFontFaceId(ctx, bnd_font);
  1004. nvgFontSize(ctx, fontsize);
  1005. nvgBeginPath(ctx);
  1006. nvgFillColor(ctx, color);
  1007. if (value) {
  1008. float label_width = nvgTextBounds(ctx, 1, 1, label, NULL, NULL);
  1009. float sep_width = nvgTextBounds(ctx, 1, 1,
  1010. BND_LABEL_SEPARATOR, NULL, NULL);
  1011. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  1012. x += pleft;
  1013. if (align == BND_CENTER) {
  1014. float width = label_width + sep_width
  1015. + nvgTextBounds(ctx, 1, 1, value, NULL, NULL);
  1016. x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
  1017. }
  1018. y += h-BND_TEXT_PAD_DOWN;
  1019. nvgText(ctx, x, y, label, NULL);
  1020. x += label_width;
  1021. nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL);
  1022. x += sep_width;
  1023. nvgText(ctx, x, y, value, NULL);
  1024. } else {
  1025. nvgTextAlign(ctx,
  1026. (align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE):
  1027. (NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE));
  1028. nvgTextBox(ctx,x+pleft,y+h-BND_TEXT_PAD_DOWN,
  1029. w-BND_PAD_RIGHT-pleft,label, NULL);
  1030. }
  1031. } else if (iconid >= 0) {
  1032. bndIcon(ctx,x+2,y+2,iconid);
  1033. }
  1034. }
  1035. void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
  1036. int iconid, NVGcolor color, float fontsize, const char *label,
  1037. NVGcolor caretcolor, int cbegin, int cend) {
  1038. float bounds[4];
  1039. float pleft = BND_TEXT_RADIUS;
  1040. if (!label) return;
  1041. if (iconid >= 0) {
  1042. bndIcon(ctx,x+4,y+2,iconid);
  1043. pleft += BND_ICON_SHEET_RES;
  1044. }
  1045. if (bnd_font < 0) return;
  1046. x+=pleft;
  1047. y+=h-BND_TEXT_PAD_DOWN;
  1048. nvgFontFaceId(ctx, bnd_font);
  1049. nvgFontSize(ctx, fontsize);
  1050. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  1051. if (cend >= cbegin) {
  1052. float c0,c1;
  1053. const char *cb;const char *ce;
  1054. static NVGglyphPosition glyphs[BND_MAX_GLYPHS];
  1055. int nglyphs = nvgTextGlyphPositions(
  1056. ctx, x, y, label, label+cend+1, glyphs, BND_MAX_GLYPHS);
  1057. c0=glyphs[0].x;
  1058. c1=glyphs[nglyphs-1].x;
  1059. cb = label+cbegin; ce = label+cend;
  1060. // TODO: this is slow
  1061. for (int i=0; i < nglyphs; ++i) {
  1062. if (glyphs[i].str == cb)
  1063. c0 = glyphs[i].x;
  1064. if (glyphs[i].str == ce)
  1065. c1 = glyphs[i].x;
  1066. }
  1067. nvgTextBounds(ctx,x,y,label,NULL, bounds);
  1068. nvgBeginPath(ctx);
  1069. if (cbegin == cend) {
  1070. nvgFillColor(ctx, nvgRGBf(0.337,0.502,0.761));
  1071. nvgRect(ctx, c0-1, bounds[1], 2, bounds[3]-bounds[1]);
  1072. } else {
  1073. nvgFillColor(ctx, caretcolor);
  1074. nvgRect(ctx, c0-1, bounds[1], c1-c0+1, bounds[3]-bounds[1]);
  1075. }
  1076. nvgFill(ctx);
  1077. }
  1078. nvgBeginPath(ctx);
  1079. nvgFillColor(ctx, color);
  1080. nvgTextBox(ctx,x,y,w-BND_TEXT_RADIUS-pleft,label, NULL);
  1081. }
  1082. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color) {
  1083. nvgBeginPath(ctx);
  1084. nvgStrokeWidth(ctx,2);
  1085. nvgStrokeColor(ctx,color);
  1086. nvgLineCap(ctx,NVG_BUTT);
  1087. nvgLineJoin(ctx,NVG_MITER);
  1088. nvgMoveTo(ctx,ox+4,oy+5);
  1089. nvgLineTo(ctx,ox+7,oy+8);
  1090. nvgLineTo(ctx,ox+14,oy+1);
  1091. nvgStroke(ctx);
  1092. }
  1093. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  1094. nvgBeginPath(ctx);
  1095. nvgMoveTo(ctx,x,y);
  1096. nvgLineTo(ctx,x-s,y+s);
  1097. nvgLineTo(ctx,x-s,y-s);
  1098. nvgClosePath(ctx);
  1099. nvgFillColor(ctx,color);
  1100. nvgFill(ctx);
  1101. }
  1102. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  1103. float w;
  1104. nvgBeginPath(ctx);
  1105. w = 1.1f*s;
  1106. nvgMoveTo(ctx,x,y-1);
  1107. nvgLineTo(ctx,x+0.5*w,y-s-1);
  1108. nvgLineTo(ctx,x+w,y-1);
  1109. nvgClosePath(ctx);
  1110. nvgMoveTo(ctx,x,y+1);
  1111. nvgLineTo(ctx,x+0.5*w,y+s+1);
  1112. nvgLineTo(ctx,x+w,y+1);
  1113. nvgClosePath(ctx);
  1114. nvgFillColor(ctx,color);
  1115. nvgFill(ctx);
  1116. }
  1117. void bndScrollHandleRect(float *x, float *y, float *w, float *h,
  1118. float offset, float size) {
  1119. size = bnd_clamp(size,0,1);
  1120. offset = bnd_clamp(offset,0,1);
  1121. if ((*h) > (*w)) {
  1122. float hs = fmaxf(size*(*h), (*w)+1);
  1123. *y = (*y) + ((*h)-hs)*offset;
  1124. *h = hs;
  1125. } else {
  1126. float ws = fmaxf(size*(*w), (*h)-1);
  1127. *x = (*x) + ((*w)-ws)*offset;
  1128. *w = ws;
  1129. }
  1130. }
  1131. ////////////////////////////////////////////////////////////////////////////////
  1132. #ifdef BND_INLINE
  1133. #undef BND_INLINE
  1134. #endif
  1135. #endif // BLENDISH_IMPLEMENTATION