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.

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