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.

1471 lines
52KB

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