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.

1373 lines
49KB

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