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.

1345 lines
48KB

  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 beveled border at position (x,y) with size (w,h) shaded with
  332. // lighter and darker versions of backgroundColor
  333. void bndBevel(NVGcontext *ctx, float x, float y, float w, float h);
  334. // Draw a lower inset for a rounded box at position (x,y) with size (w,h)
  335. // that gives the impression the surface has been pushed in.
  336. // cr2 and cr3 contain the radiuses of the bottom right and bottom left
  337. // corners of the rounded box.
  338. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  339. float cr2, float cr3);
  340. // Draw an icon with (x,y) as its upper left coordinate; the iconid selects
  341. // the icon from the sheet; use the BND_ICONID macro to build icon IDs.
  342. void bndIcon(NVGcontext *ctx, float x, float y, int iconid);
  343. // Draw a drop shadow around the rounded box at (x,y) with size (w,h) and
  344. // radius r, with feather as its maximum range in pixels.
  345. // No shadow will be painted inside the rounded box.
  346. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  347. float r, float feather, float alpha);
  348. // Draw the inner part of a widget box, with a gradient from shade_top to
  349. // shade_down. If h>w, the gradient will be horizontal instead of
  350. // vertical.
  351. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  352. float cr0, float cr1, float cr2, float cr3,
  353. NVGcolor shade_top, NVGcolor shade_down);
  354. // Draw the outline part of a widget box with the given color
  355. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  356. float cr0, float cr1, float cr2, float cr3, NVGcolor color);
  357. // Draw an optional icon specified by <iconid> and an optional label with
  358. // given alignment (BNDtextAlignment), fontsize and color within a widget box.
  359. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  360. // will be adjusted.
  361. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  362. // and color.
  363. // if value is not NULL, label and value will be drawn with a ":" separator
  364. // inbetween.
  365. void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
  366. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  367. const char *value);
  368. // Draw an optional icon specified by <iconid>, an optional label and
  369. // a caret with given fontsize and color within a widget box.
  370. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  371. // will be adjusted.
  372. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  373. // and color.
  374. // cbegin must be >= 0 and <= strlen(text) and denotes the beginning of the caret
  375. // cend must be >= cbegin and <= strlen(text) and denotes the end of the caret
  376. // if cend < cbegin, then no caret will be drawn
  377. void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
  378. int iconid, NVGcolor color, float fontsize, const char *label,
  379. NVGcolor caretcolor, int cbegin, int cend);
  380. // Draw a checkmark for an option box with the given upper left coordinates
  381. // (ox,oy) with the specified color.
  382. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color);
  383. // Draw a horizontal arrow for a number field with its center at (x,y) and
  384. // size s; if s is negative, the arrow points to the left.
  385. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  386. // Draw an up/down arrow for a choice box with its center at (x,y) and size s
  387. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  388. #ifdef __cplusplus
  389. };
  390. #endif
  391. #endif // BLENDISH_H
  392. ////////////////////////////////////////////////////////////////////////////////
  393. ////////////////////////////////////////////////////////////////////////////////
  394. #ifdef BLENDISH_IMPLEMENTATION
  395. #include <memory.h>
  396. #include <math.h>
  397. #ifdef _MSC_VER
  398. #pragma warning (disable: 4996) // Switch off security warnings
  399. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  400. #ifdef __cplusplus
  401. #define BND_INLINE inline
  402. #else
  403. #define BND_INLINE
  404. #endif
  405. #else
  406. #define BND_INLINE inline
  407. #endif
  408. ////////////////////////////////////////////////////////////////////////////////
  409. // default text size
  410. #define BND_LABEL_FONT_SIZE 13
  411. // default text padding in inner box
  412. #define BND_PAD_LEFT 8
  413. #define BND_PAD_RIGHT 8
  414. // label: value separator string
  415. #define BND_LABEL_SEPARATOR ": "
  416. // alpha intensity of transparent items (0xa4)
  417. #define BND_TRANSPARENT_ALPHA 0.643
  418. // shade intensity of beveled panels
  419. #define BND_BEVEL_SHADE 30
  420. // shade intensity of beveled insets
  421. #define BND_INSET_BEVEL_SHADE 30
  422. // shade intensity of hovered inner boxes
  423. #define BND_HOVER_SHADE 15
  424. // width of icon sheet
  425. #define BND_ICON_SHEET_WIDTH 602
  426. // height of icon sheet
  427. #define BND_ICON_SHEET_HEIGHT 640
  428. // gridsize of icon sheet in both dimensions
  429. #define BND_ICON_SHEET_GRID 21
  430. // offset of first icon tile relative to left border
  431. #define BND_ICON_SHEET_OFFSET_X 5
  432. // offset of first icon tile relative to top border
  433. #define BND_ICON_SHEET_OFFSET_Y 10
  434. // resolution of single icon
  435. #define BND_ICON_SHEET_RES 16
  436. // size of number field arrow
  437. #define BND_NUMBER_ARROW_SIZE 4
  438. // default text color
  439. #define BND_COLOR_TEXT {{{ 0,0,0,1 }}}
  440. // default highlighted text color
  441. #define BND_COLOR_TEXT_SELECTED {{{ 1,1,1,1 }}}
  442. // radius of tool button
  443. #define BND_TOOL_RADIUS 4
  444. // radius of option button
  445. #define BND_OPTION_RADIUS 4
  446. // width of option button checkbox
  447. #define BND_OPTION_WIDTH 14
  448. // height of option button checkbox
  449. #define BND_OPTION_HEIGHT 15
  450. // radius of text field
  451. #define BND_TEXT_RADIUS 4
  452. // radius of number button
  453. #define BND_NUMBER_RADIUS 10
  454. // radius of menu popup
  455. #define BND_MENU_RADIUS 3
  456. // feather of menu popup shadow
  457. #define BND_SHADOW_FEATHER 12
  458. // alpha of menu popup shadow
  459. #define BND_SHADOW_ALPHA 0.5
  460. // radius of scrollbar
  461. #define BND_SCROLLBAR_RADIUS 7
  462. // shade intensity of active scrollbar
  463. #define BND_SCROLLBAR_ACTIVE_SHADE 15
  464. // max glyphs for position testing
  465. #define BND_MAX_GLYPHS 1024
  466. // text distance from bottom
  467. #define BND_TEXT_PAD_DOWN 7
  468. ////////////////////////////////////////////////////////////////////////////////
  469. BND_INLINE float bnd_clamp(float v, float mn, float mx) {
  470. return (v > mx)?mx:(v < mn)?mn:v;
  471. }
  472. ////////////////////////////////////////////////////////////////////////////////
  473. // the initial theme
  474. static BNDtheme bnd_theme = {
  475. // backgroundColor
  476. {{{ 0.447, 0.447, 0.447, 1.0 }}},
  477. // regularTheme
  478. {
  479. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  480. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  481. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  482. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  483. BND_COLOR_TEXT, // color_text
  484. BND_COLOR_TEXT_SELECTED, // color_text_selected
  485. 0, // shade_top
  486. 0, // shade_down
  487. },
  488. // toolTheme
  489. {
  490. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  491. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  492. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  493. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  494. BND_COLOR_TEXT, // color_text
  495. BND_COLOR_TEXT_SELECTED, // color_text_selected
  496. 15, // shade_top
  497. -15, // shade_down
  498. },
  499. // radioTheme
  500. {
  501. {{{ 0,0,0,1 }}}, // color_outline
  502. {{{ 1,1,1,1 }}}, // color_item
  503. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  504. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  505. BND_COLOR_TEXT_SELECTED, // color_text
  506. BND_COLOR_TEXT, // color_text_selected
  507. 15, // shade_top
  508. -15, // shade_down
  509. },
  510. // textFieldTheme
  511. {
  512. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  513. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  514. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner
  515. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  516. BND_COLOR_TEXT, // color_text
  517. BND_COLOR_TEXT_SELECTED, // color_text_selected
  518. 0, // shade_top
  519. 25, // shade_down
  520. },
  521. // optionTheme
  522. {
  523. {{{ 0,0,0,1 }}}, // color_outline
  524. {{{ 1,1,1,1 }}}, // color_item
  525. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  526. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  527. BND_COLOR_TEXT, // color_text
  528. BND_COLOR_TEXT_SELECTED, // color_text_selected
  529. 15, // shade_top
  530. -15, // shade_down
  531. },
  532. // choiceTheme
  533. {
  534. {{{ 0,0,0,1 }}}, // color_outline
  535. {{{ 1,1,1,1 }}}, // color_item
  536. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  537. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  538. BND_COLOR_TEXT_SELECTED, // color_text
  539. {{{ 0.8,0.8,0.8,1 }}}, // color_text_selected
  540. 15, // shade_top
  541. -15, // shade_down
  542. },
  543. // numberFieldTheme
  544. {
  545. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  546. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  547. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  548. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  549. BND_COLOR_TEXT, // color_text
  550. BND_COLOR_TEXT_SELECTED, // color_text_selected
  551. -20, // shade_top
  552. 0, // shade_down
  553. },
  554. // sliderTheme
  555. {
  556. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  557. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  558. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  559. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  560. BND_COLOR_TEXT, // color_text
  561. BND_COLOR_TEXT_SELECTED, // color_text_selected
  562. -20, // shade_top
  563. 0, // shade_down
  564. },
  565. // scrollBarTheme
  566. {
  567. {{{ 0.196,0.196,0.196,1 }}}, // color_outline
  568. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  569. {{{ 0.314, 0.314, 0.314,0.706 }}}, // color_inner
  570. {{{ 0.392, 0.392, 0.392,0.706 }}}, // color_inner_selected
  571. BND_COLOR_TEXT, // color_text
  572. BND_COLOR_TEXT_SELECTED, // color_text_selected
  573. 5, // shade_top
  574. -5, // shade_down
  575. },
  576. // tooltipTheme
  577. {
  578. {{{ 0,0,0,1 }}}, // color_outline
  579. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  580. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  581. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  582. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  583. BND_COLOR_TEXT_SELECTED, // color_text_selected
  584. 0, // shade_top
  585. 0, // shade_down
  586. },
  587. // menuTheme
  588. {
  589. {{{ 0,0,0,1 }}}, // color_outline
  590. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  591. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  592. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  593. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  594. BND_COLOR_TEXT_SELECTED, // color_text_selected
  595. 0, // shade_top
  596. 0, // shade_down
  597. },
  598. // menuItemTheme
  599. {
  600. {{{ 0,0,0,1 }}}, // color_outline
  601. {{{ 0.675,0.675,0.675,0.502 }}}, // color_item
  602. {{{ 0,0,0,0 }}}, // color_inner
  603. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  604. BND_COLOR_TEXT_SELECTED, // color_text
  605. BND_COLOR_TEXT, // color_text_selected
  606. 38, // shade_top
  607. 0, // shade_down
  608. },
  609. };
  610. ////////////////////////////////////////////////////////////////////////////////
  611. void bndSetTheme(BNDtheme theme) {
  612. bnd_theme = theme;
  613. }
  614. const BNDtheme *bndGetTheme() {
  615. return &bnd_theme;
  616. }
  617. // the handle to the image containing the icon sheet
  618. static int bnd_icon_image = -1;
  619. void bndSetIconImage(int image) {
  620. bnd_icon_image = image;
  621. }
  622. // the handle to the UI font
  623. static int bnd_font = -1;
  624. void bndSetFont(int font) {
  625. bnd_font = font;
  626. }
  627. ////////////////////////////////////////////////////////////////////////////////
  628. void bndLabel(NVGcontext *ctx,
  629. float x, float y, float w, float h, int iconid, const char *label) {
  630. bndIconLabelValue(ctx,x,y,w,h,iconid,
  631. bnd_theme.regularTheme.textColor, BND_LEFT,
  632. BND_LABEL_FONT_SIZE, label, NULL);
  633. }
  634. void bndToolButton(NVGcontext *ctx,
  635. float x, float y, float w, float h, int flags, BNDwidgetState state,
  636. int iconid, const char *label) {
  637. float cr[4];
  638. NVGcolor shade_top, shade_down;
  639. bndSelectCorners(cr, BND_TOOL_RADIUS, flags);
  640. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  641. bndInnerColors(&shade_top, &shade_down, &bnd_theme.toolTheme, state, 1);
  642. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  643. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  644. bndTransparent(bnd_theme.toolTheme.outlineColor));
  645. bndIconLabelValue(ctx,x,y,w,h,iconid,
  646. bndTextColor(&bnd_theme.toolTheme, state), BND_CENTER,
  647. BND_LABEL_FONT_SIZE, label, NULL);
  648. }
  649. void bndRadioButton(NVGcontext *ctx,
  650. float x, float y, float w, float h, int flags, BNDwidgetState state,
  651. int iconid, const char *label) {
  652. float cr[4];
  653. NVGcolor shade_top, shade_down;
  654. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  655. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  656. bndInnerColors(&shade_top, &shade_down, &bnd_theme.radioTheme, state, 1);
  657. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  658. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  659. bndTransparent(bnd_theme.radioTheme.outlineColor));
  660. bndIconLabelValue(ctx,x,y,w,h,iconid,
  661. bndTextColor(&bnd_theme.radioTheme, state), BND_CENTER,
  662. BND_LABEL_FONT_SIZE, label, NULL);
  663. }
  664. void bndTextField(NVGcontext *ctx,
  665. float x, float y, float w, float h, int flags, BNDwidgetState state,
  666. int iconid, const char *text, int cbegin, int cend) {
  667. float cr[4];
  668. NVGcolor shade_top, shade_down;
  669. bndSelectCorners(cr, BND_TEXT_RADIUS, flags);
  670. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  671. bndInnerColors(&shade_top, &shade_down, &bnd_theme.textFieldTheme, state, 0);
  672. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  673. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  674. bndTransparent(bnd_theme.textFieldTheme.outlineColor));
  675. if (state != BND_ACTIVE) {
  676. cend = -1;
  677. }
  678. bndIconLabelCaret(ctx,x,y,w,h,iconid,
  679. bndTextColor(&bnd_theme.textFieldTheme, state), BND_LABEL_FONT_SIZE,
  680. text, bnd_theme.textFieldTheme.itemColor, cbegin, cend);
  681. }
  682. void bndOptionButton(NVGcontext *ctx,
  683. float x, float y, float w, float h, BNDwidgetState state,
  684. const char *label) {
  685. float ox, oy;
  686. NVGcolor shade_top, shade_down;
  687. ox = x;
  688. oy = y+h-BND_OPTION_HEIGHT-3;
  689. bndBevelInset(ctx,ox,oy,
  690. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  691. BND_OPTION_RADIUS,BND_OPTION_RADIUS);
  692. bndInnerColors(&shade_top, &shade_down, &bnd_theme.optionTheme, state, 1);
  693. bndInnerBox(ctx,ox,oy,
  694. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  695. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  696. shade_top, shade_down);
  697. bndOutlineBox(ctx,ox,oy,
  698. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  699. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  700. bndTransparent(bnd_theme.optionTheme.outlineColor));
  701. if (state == BND_ACTIVE) {
  702. bndCheck(ctx,ox,oy, bndTransparent(bnd_theme.optionTheme.itemColor));
  703. }
  704. bndIconLabelValue(ctx,x+12,y,w-12,h,-1,
  705. bndTextColor(&bnd_theme.optionTheme, state), BND_LEFT,
  706. BND_LABEL_FONT_SIZE, label, NULL);
  707. }
  708. void bndChoiceButton(NVGcontext *ctx,
  709. float x, float y, float w, float h, int flags, BNDwidgetState state,
  710. int iconid, const char *label) {
  711. float cr[4];
  712. NVGcolor shade_top, shade_down;
  713. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  714. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  715. bndInnerColors(&shade_top, &shade_down, &bnd_theme.choiceTheme, state, 1);
  716. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  717. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  718. bndTransparent(bnd_theme.choiceTheme.outlineColor));
  719. bndIconLabelValue(ctx,x,y,w,h,iconid,
  720. bndTextColor(&bnd_theme.choiceTheme, state), BND_LEFT,
  721. BND_LABEL_FONT_SIZE, label, NULL);
  722. bndUpDownArrow(ctx,x+w-10,y+10,5,
  723. bndTransparent(bnd_theme.choiceTheme.itemColor));
  724. }
  725. void bndNumberField(NVGcontext *ctx,
  726. float x, float y, float w, float h, int flags, BNDwidgetState state,
  727. const char *label, const char *value) {
  728. float cr[4];
  729. NVGcolor shade_top, shade_down;
  730. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  731. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  732. bndInnerColors(&shade_top, &shade_down, &bnd_theme.numberFieldTheme, state, 0);
  733. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  734. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  735. bndTransparent(bnd_theme.numberFieldTheme.outlineColor));
  736. bndIconLabelValue(ctx,x,y,w,h,-1,
  737. bndTextColor(&bnd_theme.numberFieldTheme, state), BND_CENTER,
  738. BND_LABEL_FONT_SIZE, label, value);
  739. bndArrow(ctx,x+8,y+10,-BND_NUMBER_ARROW_SIZE,
  740. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  741. bndArrow(ctx,x+w-8,y+10,BND_NUMBER_ARROW_SIZE,
  742. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  743. }
  744. void bndSlider(NVGcontext *ctx,
  745. float x, float y, float w, float h, int flags, BNDwidgetState state,
  746. float progress, const char *label, const char *value) {
  747. float cr[4];
  748. NVGcolor shade_top, shade_down;
  749. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  750. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  751. bndInnerColors(&shade_top, &shade_down, &bnd_theme.sliderTheme, state, 0);
  752. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  753. if (state == BND_ACTIVE) {
  754. shade_top = bndOffsetColor(
  755. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  756. shade_down = bndOffsetColor(
  757. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  758. } else {
  759. shade_top = bndOffsetColor(
  760. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  761. shade_down = bndOffsetColor(
  762. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  763. }
  764. nvgScissor(ctx,x,y,8+(w-8)*bnd_clamp(progress,0,1),h);
  765. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  766. nvgResetScissor(ctx);
  767. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  768. bndTransparent(bnd_theme.sliderTheme.outlineColor));
  769. bndIconLabelValue(ctx,x,y,w,h,-1,
  770. bndTextColor(&bnd_theme.sliderTheme, state), BND_CENTER,
  771. BND_LABEL_FONT_SIZE, label, value);
  772. }
  773. void bndScrollBar(NVGcontext *ctx,
  774. float x, float y, float w, float h, BNDwidgetState state,
  775. float offset, float size) {
  776. bndBevelInset(ctx,x,y,w,h,
  777. BND_SCROLLBAR_RADIUS, BND_SCROLLBAR_RADIUS);
  778. bndInnerBox(ctx,x,y,w,h,
  779. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  780. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  781. bndOffsetColor(
  782. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeDown),
  783. bndOffsetColor(
  784. bnd_theme.scrollBarTheme.innerColor, 3*bnd_theme.scrollBarTheme.shadeTop));
  785. bndOutlineBox(ctx,x,y,w,h,
  786. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  787. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  788. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  789. NVGcolor itemColor = bndOffsetColor(
  790. bnd_theme.scrollBarTheme.itemColor,
  791. (state == BND_ACTIVE)?BND_SCROLLBAR_ACTIVE_SHADE:0);
  792. bndScrollHandleRect(&x,&y,&w,&h,offset,size);
  793. bndInnerBox(ctx,x,y,w,h,
  794. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  795. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  796. bndOffsetColor(
  797. itemColor, 3*bnd_theme.scrollBarTheme.shadeTop),
  798. bndOffsetColor(
  799. itemColor, 3*bnd_theme.scrollBarTheme.shadeDown));
  800. bndOutlineBox(ctx,x,y,w,h,
  801. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  802. BND_SCROLLBAR_RADIUS,BND_SCROLLBAR_RADIUS,
  803. bndTransparent(bnd_theme.scrollBarTheme.outlineColor));
  804. }
  805. void bndMenuBackground(NVGcontext *ctx,
  806. float x, float y, float w, float h, int flags) {
  807. float cr[4];
  808. NVGcolor shade_top, shade_down;
  809. bndSelectCorners(cr, BND_MENU_RADIUS, flags);
  810. bndInnerColors(&shade_top, &shade_down, &bnd_theme.menuTheme,
  811. BND_DEFAULT, 0);
  812. bndInnerBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  813. bndOutlineBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3],
  814. bndTransparent(bnd_theme.menuTheme.outlineColor));
  815. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  816. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  817. }
  818. void bndTooltipBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  819. NVGcolor shade_top, shade_down;
  820. bndInnerColors(&shade_top, &shade_down, &bnd_theme.tooltipTheme,
  821. BND_DEFAULT, 0);
  822. bndInnerBox(ctx,x,y,w,h+1,
  823. BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,
  824. shade_top, shade_down);
  825. bndOutlineBox(ctx,x,y,w,h+1,
  826. BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,BND_MENU_RADIUS,
  827. bndTransparent(bnd_theme.tooltipTheme.outlineColor));
  828. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  829. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  830. }
  831. void bndMenuLabel(NVGcontext *ctx,
  832. float x, float y, float w, float h, int iconid, const char *label) {
  833. bndIconLabelValue(ctx,x,y,w,h,iconid,
  834. bnd_theme.menuTheme.textColor, BND_LEFT,
  835. BND_LABEL_FONT_SIZE, label, NULL);
  836. }
  837. void bndMenuItem(NVGcontext *ctx,
  838. float x, float y, float w, float h, BNDwidgetState state,
  839. int iconid, const char *label) {
  840. if (state != BND_DEFAULT) {
  841. bndInnerBox(ctx,x,y,w,h,0,0,0,0,
  842. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  843. bnd_theme.menuItemTheme.shadeTop),
  844. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  845. bnd_theme.menuItemTheme.shadeDown));
  846. state = BND_ACTIVE;
  847. }
  848. bndIconLabelValue(ctx,x,y,w,h,iconid,
  849. bndTextColor(&bnd_theme.menuItemTheme, state), BND_LEFT,
  850. BND_LABEL_FONT_SIZE, label, NULL);
  851. }
  852. ////////////////////////////////////////////////////////////////////////////////
  853. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  854. float cr0, float cr1, float cr2, float cr3) {
  855. float d;
  856. w = fmaxf(0, w);
  857. h = fmaxf(0, h);
  858. d = fminf(w, h);
  859. nvgMoveTo(ctx, x,y+h*0.5f);
  860. nvgArcTo(ctx, x,y, x+w,y, fminf(cr0, d/2));
  861. nvgArcTo(ctx, x+w,y, x+w,y+h, fminf(cr1, d/2));
  862. nvgArcTo(ctx, x+w,y+h, x,y+h, fminf(cr2, d/2));
  863. nvgArcTo(ctx, x,y+h, x,y, fminf(cr3, d/2));
  864. nvgClosePath(ctx);
  865. }
  866. NVGcolor bndTransparent(NVGcolor color) {
  867. color.a *= BND_TRANSPARENT_ALPHA;
  868. return color;
  869. }
  870. NVGcolor bndOffsetColor(NVGcolor color, int delta) {
  871. float offset = (float)delta / 255.0f;
  872. return delta?(
  873. nvgRGBAf(
  874. bnd_clamp(color.r+offset,0,1),
  875. bnd_clamp(color.g+offset,0,1),
  876. bnd_clamp(color.b+offset,0,1),
  877. color.a)
  878. ):color;
  879. }
  880. void bndBevel(NVGcontext *ctx, float x, float y, float w, float h) {
  881. nvgStrokeWidth(ctx, 1);
  882. x += 0.5f;
  883. y += 0.5f;
  884. w -= 1;
  885. h -= 1;
  886. nvgBeginPath(ctx);
  887. nvgMoveTo(ctx, x, y+h);
  888. nvgLineTo(ctx, x+w, y+h);
  889. nvgLineTo(ctx, x+w, y);
  890. nvgStrokeColor(ctx, bndTransparent(
  891. bndOffsetColor(bnd_theme.backgroundColor, -BND_BEVEL_SHADE)));
  892. nvgStroke(ctx);
  893. nvgBeginPath(ctx);
  894. nvgMoveTo(ctx, x, y+h);
  895. nvgLineTo(ctx, x, y);
  896. nvgLineTo(ctx, x+w, y);
  897. nvgStrokeColor(ctx, bndTransparent(
  898. bndOffsetColor(bnd_theme.backgroundColor, BND_BEVEL_SHADE)));
  899. nvgStroke(ctx);
  900. }
  901. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  902. float cr2, float cr3) {
  903. float d;
  904. y -= 0.5f;
  905. d = fminf(w, h);
  906. cr2 = fminf(cr2, d/2);
  907. cr3 = fminf(cr3, d/2);
  908. nvgBeginPath(ctx);
  909. nvgMoveTo(ctx, x+w,y+h-cr2);
  910. nvgArcTo(ctx, x+w,y+h, x,y+h, cr2);
  911. nvgArcTo(ctx, x,y+h, x,y, cr3);
  912. NVGcolor bevelColor = bndOffsetColor(bnd_theme.backgroundColor,
  913. BND_INSET_BEVEL_SHADE);
  914. nvgStrokeWidth(ctx, 1);
  915. nvgStrokePaint(ctx,
  916. nvgLinearGradient(ctx,
  917. x,y+h-fmaxf(cr2,cr3)-1,
  918. x,y+h-1,
  919. nvgRGBAf(bevelColor.r, bevelColor.g, bevelColor.b, 0),
  920. bevelColor));
  921. nvgStroke(ctx);
  922. }
  923. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  924. nvgBeginPath(ctx);
  925. nvgRect(ctx, x, y, w, h);
  926. nvgFillColor(ctx, bnd_theme.backgroundColor);
  927. nvgFill(ctx);
  928. }
  929. void bndIcon(NVGcontext *ctx, float x, float y, int iconid) {
  930. int ix, iy, u, v;
  931. if (bnd_icon_image < 0) return; // no icons loaded
  932. ix = iconid & 0xff;
  933. iy = (iconid>>8) & 0xff;
  934. u = BND_ICON_SHEET_OFFSET_X + ix*BND_ICON_SHEET_GRID;
  935. v = BND_ICON_SHEET_OFFSET_Y + iy*BND_ICON_SHEET_GRID;
  936. nvgBeginPath(ctx);
  937. nvgRect(ctx,x,y,BND_ICON_SHEET_RES,BND_ICON_SHEET_RES);
  938. nvgFillPaint(ctx,
  939. nvgImagePattern(ctx,x-u,y-v,
  940. BND_ICON_SHEET_WIDTH,
  941. BND_ICON_SHEET_HEIGHT,
  942. 0,bnd_icon_image,0,1));
  943. nvgFill(ctx);
  944. }
  945. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  946. float r, float feather, float alpha) {
  947. nvgBeginPath(ctx);
  948. y += feather;
  949. h -= feather;
  950. nvgMoveTo(ctx, x-feather, y-feather);
  951. nvgLineTo(ctx, x, y-feather);
  952. nvgLineTo(ctx, x, y+h-feather);
  953. nvgArcTo(ctx, x,y+h,x+r,y+h,r);
  954. nvgArcTo(ctx, x+w,y+h,x+w,y+h-r,r);
  955. nvgLineTo(ctx, x+w, y-feather);
  956. nvgLineTo(ctx, x+w+feather, y-feather);
  957. nvgLineTo(ctx, x+w+feather, y+h+feather);
  958. nvgLineTo(ctx, x-feather, y+h+feather);
  959. nvgClosePath(ctx);
  960. nvgFillPaint(ctx, nvgBoxGradient(ctx,
  961. x - feather*0.5f,y - feather*0.5f,
  962. w + feather,h+feather,
  963. r+feather*0.5f,
  964. feather,
  965. nvgRGBAf(0,0,0,alpha*alpha),
  966. nvgRGBAf(0,0,0,0)));
  967. nvgFill(ctx);
  968. }
  969. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  970. float cr0, float cr1, float cr2, float cr3,
  971. NVGcolor shade_top, NVGcolor shade_down) {
  972. nvgBeginPath(ctx);
  973. bndRoundedBox(ctx,x+1,y+1,w-2,h-3,
  974. fmaxf(0,cr0-1),fmaxf(0,cr1-1),fmaxf(0,cr2-1),fmaxf(0,cr3-1));
  975. nvgFillPaint(ctx,((h-2)>w)?
  976. nvgLinearGradient(ctx,x,y,x+w,y,shade_top,shade_down):
  977. nvgLinearGradient(ctx,x,y,x,y+h,shade_top,shade_down));
  978. nvgFill(ctx);
  979. }
  980. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  981. float cr0, float cr1, float cr2, float cr3, NVGcolor color) {
  982. nvgBeginPath(ctx);
  983. bndRoundedBox(ctx,x+0.5f,y+0.5f,w-1,h-2,cr0,cr1,cr2,cr3);
  984. nvgStrokeColor(ctx,color);
  985. nvgStrokeWidth(ctx,1);
  986. nvgStroke(ctx);
  987. }
  988. void bndSelectCorners(float *radiuses, float r, int flags) {
  989. radiuses[0] = (flags & BND_CORNER_TOP_LEFT)?0:r;
  990. radiuses[1] = (flags & BND_CORNER_TOP_RIGHT)?0:r;
  991. radiuses[2] = (flags & BND_CORNER_DOWN_RIGHT)?0:r;
  992. radiuses[3] = (flags & BND_CORNER_DOWN_LEFT)?0:r;
  993. }
  994. void bndInnerColors(
  995. NVGcolor *shade_top, NVGcolor *shade_down,
  996. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive) {
  997. switch(state) {
  998. default:
  999. case BND_DEFAULT: {
  1000. *shade_top = bndOffsetColor(theme->innerColor, theme->shadeTop);
  1001. *shade_down = bndOffsetColor(theme->innerColor, theme->shadeDown);
  1002. } break;
  1003. case BND_HOVER: {
  1004. NVGcolor color = bndOffsetColor(theme->innerColor, BND_HOVER_SHADE);
  1005. *shade_top = bndOffsetColor(color, theme->shadeTop);
  1006. *shade_down = bndOffsetColor(color, theme->shadeDown);
  1007. } break;
  1008. case BND_ACTIVE: {
  1009. *shade_top = bndOffsetColor(theme->innerSelectedColor,
  1010. flipActive?theme->shadeDown:theme->shadeTop);
  1011. *shade_down = bndOffsetColor(theme->innerSelectedColor,
  1012. flipActive?theme->shadeTop:theme->shadeDown);
  1013. } break;
  1014. }
  1015. }
  1016. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state) {
  1017. return (state == BND_ACTIVE)?theme->textSelectedColor:theme->textColor;
  1018. }
  1019. void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
  1020. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  1021. const char *value) {
  1022. float pleft = BND_PAD_LEFT;
  1023. if (label) {
  1024. if (iconid >= 0) {
  1025. bndIcon(ctx,x+4,y+2,iconid);
  1026. pleft += BND_ICON_SHEET_RES;
  1027. }
  1028. if (bnd_font < 0) return;
  1029. nvgFontFaceId(ctx, bnd_font);
  1030. nvgFontSize(ctx, fontsize);
  1031. nvgBeginPath(ctx);
  1032. nvgFillColor(ctx, color);
  1033. if (value) {
  1034. float label_width = nvgTextBounds(ctx, 1, 1, label, NULL, NULL);
  1035. float sep_width = nvgTextBounds(ctx, 1, 1,
  1036. BND_LABEL_SEPARATOR, NULL, NULL);
  1037. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  1038. x += pleft;
  1039. if (align == BND_CENTER) {
  1040. float width = label_width + sep_width
  1041. + nvgTextBounds(ctx, 1, 1, value, NULL, NULL);
  1042. x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
  1043. }
  1044. y += h-BND_TEXT_PAD_DOWN;
  1045. nvgText(ctx, x, y, label, NULL);
  1046. x += label_width;
  1047. nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL);
  1048. x += sep_width;
  1049. nvgText(ctx, x, y, value, NULL);
  1050. } else {
  1051. nvgTextAlign(ctx,
  1052. (align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE):
  1053. (NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE));
  1054. nvgTextBox(ctx,x+pleft,y+h-BND_TEXT_PAD_DOWN,
  1055. w-BND_PAD_RIGHT-pleft,label, NULL);
  1056. }
  1057. } else if (iconid >= 0) {
  1058. bndIcon(ctx,x+2,y+2,iconid);
  1059. }
  1060. }
  1061. void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
  1062. int iconid, NVGcolor color, float fontsize, const char *label,
  1063. NVGcolor caretcolor, int cbegin, int cend) {
  1064. float bounds[4];
  1065. float pleft = BND_TEXT_RADIUS;
  1066. if (!label) return;
  1067. if (iconid >= 0) {
  1068. bndIcon(ctx,x+4,y+2,iconid);
  1069. pleft += BND_ICON_SHEET_RES;
  1070. }
  1071. if (bnd_font < 0) return;
  1072. x+=pleft;
  1073. y+=h-BND_TEXT_PAD_DOWN;
  1074. nvgFontFaceId(ctx, bnd_font);
  1075. nvgFontSize(ctx, fontsize);
  1076. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  1077. if (cend >= cbegin) {
  1078. float c0,c1;
  1079. const char *cb;const char *ce;
  1080. static NVGglyphPosition glyphs[BND_MAX_GLYPHS];
  1081. int nglyphs = nvgTextGlyphPositions(
  1082. ctx, x, y, label, label+cend+1, glyphs, BND_MAX_GLYPHS);
  1083. c0=glyphs[0].x;
  1084. c1=glyphs[nglyphs-1].x;
  1085. cb = label+cbegin; ce = label+cend;
  1086. // TODO: this is slow
  1087. for (int i=0; i < nglyphs; ++i) {
  1088. if (glyphs[i].str == cb)
  1089. c0 = glyphs[i].x;
  1090. if (glyphs[i].str == ce)
  1091. c1 = glyphs[i].x;
  1092. }
  1093. nvgTextBounds(ctx,x,y,label,NULL, bounds);
  1094. nvgBeginPath(ctx);
  1095. if (cbegin == cend) {
  1096. nvgFillColor(ctx, nvgRGBf(0.337,0.502,0.761));
  1097. nvgRect(ctx, c0-1, bounds[1], 2, bounds[3]-bounds[1]);
  1098. } else {
  1099. nvgFillColor(ctx, caretcolor);
  1100. nvgRect(ctx, c0-1, bounds[1], c1-c0+1, bounds[3]-bounds[1]);
  1101. }
  1102. nvgFill(ctx);
  1103. }
  1104. nvgBeginPath(ctx);
  1105. nvgFillColor(ctx, color);
  1106. nvgTextBox(ctx,x,y,w-BND_TEXT_RADIUS-pleft,label, NULL);
  1107. }
  1108. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color) {
  1109. nvgBeginPath(ctx);
  1110. nvgStrokeWidth(ctx,2);
  1111. nvgStrokeColor(ctx,color);
  1112. nvgLineCap(ctx,NVG_BUTT);
  1113. nvgLineJoin(ctx,NVG_MITER);
  1114. nvgMoveTo(ctx,ox+4,oy+5);
  1115. nvgLineTo(ctx,ox+7,oy+8);
  1116. nvgLineTo(ctx,ox+14,oy+1);
  1117. nvgStroke(ctx);
  1118. }
  1119. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  1120. nvgBeginPath(ctx);
  1121. nvgMoveTo(ctx,x,y);
  1122. nvgLineTo(ctx,x-s,y+s);
  1123. nvgLineTo(ctx,x-s,y-s);
  1124. nvgClosePath(ctx);
  1125. nvgFillColor(ctx,color);
  1126. nvgFill(ctx);
  1127. }
  1128. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  1129. float w;
  1130. nvgBeginPath(ctx);
  1131. w = 1.1f*s;
  1132. nvgMoveTo(ctx,x,y-1);
  1133. nvgLineTo(ctx,x+0.5*w,y-s-1);
  1134. nvgLineTo(ctx,x+w,y-1);
  1135. nvgClosePath(ctx);
  1136. nvgMoveTo(ctx,x,y+1);
  1137. nvgLineTo(ctx,x+0.5*w,y+s+1);
  1138. nvgLineTo(ctx,x+w,y+1);
  1139. nvgClosePath(ctx);
  1140. nvgFillColor(ctx,color);
  1141. nvgFill(ctx);
  1142. }
  1143. void bndScrollHandleRect(float *x, float *y, float *w, float *h,
  1144. float offset, float size) {
  1145. size = bnd_clamp(size,0,1);
  1146. offset = bnd_clamp(offset,0,1);
  1147. if ((*h) > (*w)) {
  1148. float hs = fmaxf(size*(*h), (*w)+1);
  1149. *y = (*y) + ((*h)-hs)*offset;
  1150. *h = hs;
  1151. } else {
  1152. float ws = fmaxf(size*(*w), (*h)-1);
  1153. *x = (*x) + ((*w)-ws)*offset;
  1154. *w = ws;
  1155. }
  1156. }
  1157. ////////////////////////////////////////////////////////////////////////////////
  1158. #ifdef BND_INLINE
  1159. #undef BND_INLINE
  1160. #endif
  1161. #endif // BLENDISH_IMPLEMENTATION