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.

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