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.

1653 lines
58KB

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