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.

1724 lines
60KB

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