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.

1752 lines
61KB

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