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.

1056 lines
37KB

  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 1 (2014-07-08)
  30. Summary
  31. -------
  32. Blendish is a small collection of drawing functions for NanoVG, designed to
  33. replicate the look of the Blender 2.5+ User Interface. You can use these
  34. functions to theme your UI library. Several metric constants for faithful
  35. reproduction are also included.
  36. Blendish supports the original Blender icon sheet; As the licensing of Blenders
  37. icons is unclear, they are not included in Blendishes repository, but a SVG
  38. template, "icons_template.svg" is provided, which you can use to build your own
  39. icon sheet.
  40. To use icons, you must first load the icon sheet using one of the
  41. nvgCreateImage*() functions and then pass the image handle to bndSetIconImage();
  42. otherwise, no icons will be drawn. See bndSetIconImage() for more information.
  43. Blendish will not render text until a suitable UI font has been passed to
  44. bndSetFont() has been called. See bndSetFont() for more information.
  45. Drawbacks
  46. ---------
  47. There is no support varying dpi resolutions yet. The library is hardcoded
  48. to the equivalent of 72 dpi in the Blender system settings.
  49. Support for label truncation is missing. Text rendering breaks when widgets are
  50. too short to contain their labels.
  51. Usage
  52. -----
  53. To use this header file in implementation mode, define BLENDISH_IMPLEMENTATION
  54. before including blendish.h, otherwise the file will be in header-only mode.
  55. */
  56. // if that typedef is provided elsewhere, you may define
  57. // BLENDISH_NO_NVG_TYPEDEFS before including the header.
  58. #ifndef BLENDISH_NO_NVG_TYPEDEFS
  59. typedef struct NVGcontext NVGcontext;
  60. typedef struct NVGcolor NVGcolor;
  61. #endif
  62. // describes the theme used to draw a single widget or widget box;
  63. // these values correspond to the same values that can be retrieved from
  64. // the Theme panel in the Blender preferences
  65. typedef struct BNDwidgetTheme {
  66. // color of widget box outline
  67. NVGcolor outlineColor;
  68. // color of widget item (meaning changes depending on class)
  69. NVGcolor itemColor;
  70. // fill color of widget box
  71. NVGcolor innerColor;
  72. // fill color of widget box when active
  73. NVGcolor innerSelectedColor;
  74. // color of text label
  75. NVGcolor textColor;
  76. // color of text label when active
  77. NVGcolor textSelectedColor;
  78. // delta modifier for upper part of gradient (-100 to 100)
  79. int shadeTop;
  80. // delta modifier for lower part of gradient (-100 to 100)
  81. int shadeDown;
  82. } BNDwidgetTheme;
  83. // describes the theme used to draw widgets
  84. typedef struct BNDtheme {
  85. // the background color of panels and windows
  86. NVGcolor backgroundColor;
  87. // theme for labels
  88. BNDwidgetTheme regularTheme;
  89. // theme for tool buttons
  90. BNDwidgetTheme toolTheme;
  91. // theme for radio buttons
  92. BNDwidgetTheme radioTheme;
  93. // theme for option buttons (checkboxes)
  94. BNDwidgetTheme optionTheme;
  95. // theme for choice buttons (comboboxes)
  96. // Blender calls them "menu buttons"
  97. BNDwidgetTheme choiceTheme;
  98. // theme for number fields
  99. BNDwidgetTheme numberFieldTheme;
  100. // theme for slider controls
  101. BNDwidgetTheme sliderTheme;
  102. // theme for menu backgrounds and tooltips
  103. BNDwidgetTheme menuTheme;
  104. // theme for menu items
  105. BNDwidgetTheme menuItemTheme;
  106. } BNDtheme;
  107. // how text on a control is aligned
  108. typedef enum BNDtextAlignment {
  109. BND_LEFT = 0,
  110. BND_CENTER,
  111. } BNDtextAlignment;
  112. // states altering the styling of a widget
  113. typedef enum BNDwidgetState {
  114. // not interacting
  115. BND_DEFAULT = 0,
  116. // the mouse is hovering over the control
  117. BND_HOVER,
  118. // the widget is activated (pressed) or in an active state (toggled)
  119. BND_ACTIVE
  120. } BNDwidgetState;
  121. // flags indicating which corners are sharp (for grouping widgets)
  122. typedef enum BNDcornerFlags {
  123. // all corners are round
  124. BND_CORNER_NONE = 0,
  125. // sharp top left corner
  126. BND_CORNER_TOP_LEFT = 1,
  127. // sharp top right corner
  128. BND_CORNER_TOP_RIGHT = 2,
  129. // sharp bottom right corner
  130. BND_CORNER_DOWN_RIGHT = 4,
  131. // sharp bottom left corner
  132. BND_CORNER_DOWN_LEFT = 8,
  133. // all corners are sharp;
  134. // you can invert a set of flags using ^= BND_CORNER_ALL
  135. BND_CORNER_ALL = 0xF,
  136. // top border is sharp
  137. BND_CORNER_TOP = 3,
  138. // bottom border is sharp
  139. BND_CORNER_DOWN = 0xC,
  140. // left border is sharp
  141. BND_CORNER_LEFT = 9,
  142. // right border is sharp
  143. BND_CORNER_RIGHT = 6
  144. } BNDcornerFlags;
  145. // build an icon ID from two coordinates into the icon sheet, where
  146. // (0,0) designates the upper-leftmost icon, (1,0) the one right next to it,
  147. // and so on.
  148. #define BND_ICONID(x,y) ((x)|((y)<<8))
  149. // default widget height
  150. #define BND_WIDGET_HEIGHT 21
  151. ////////////////////////////////////////////////////////////////////////////////
  152. // set the current theme all widgets will be drawn with.
  153. // the default Blender 2.6 theme is set by default.
  154. void bndSetTheme(BNDtheme theme);
  155. // Returns the currently set theme
  156. const BNDtheme *bndGetTheme();
  157. // designates an image handle as returned by nvgCreateImage*() as the themes'
  158. // icon sheet. The icon sheet format must be compatible to Blender 2.6's icon
  159. // sheet; the order of icons does not matter.
  160. // A valid icon sheet is e.g. shown at
  161. // http://wiki.blender.org/index.php/Dev:2.5/Doc/How_to/Add_an_icon
  162. void bndSetIconImage(int image);
  163. // designates an image handle as returned by nvgCreateFont*() as the themes'
  164. // UI font. Blender's original UI font Droid Sans is perfectly suited and
  165. // available here:
  166. // https://svn.blender.org/svnroot/bf-blender/trunk/blender/release/datafiles/fonts/
  167. void bndSetFont(int font);
  168. ////////////////////////////////////////////////////////////////////////////////
  169. // High Level Functions
  170. // --------------------
  171. // Use these functions to draw themed widgets with your NVGcontext.
  172. // Draw a label with its lower left origin at (x,y) and size of (w,h).
  173. // if iconid >= 0, an icon will be added to the widget
  174. // if label is not NULL, a label will be added to the widget
  175. // widget looks best when height is BND_WIDGET_HEIGHT
  176. void bndLabel(NVGcontext *ctx,
  177. float x, float y, float w, float h, int iconid, const char *label);
  178. // Draw a tool button with its lower left origin at (x,y) and size of (w,h),
  179. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  180. // the widgets current UI state.
  181. // if iconid >= 0, an icon will be added to the widget
  182. // if label is not NULL, a label will be added to the widget
  183. // widget looks best when height is BND_WIDGET_HEIGHT
  184. void bndToolButton(NVGcontext *ctx,
  185. float x, float y, float w, float h, int flags, BNDwidgetState state,
  186. int iconid, const char *label);
  187. // Draw a radio button with its lower left origin at (x,y) and size of (w,h),
  188. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  189. // the widgets current UI state.
  190. // if iconid >= 0, an icon will be added to the widget
  191. // if label is not NULL, a label will be added to the widget
  192. // widget looks best when height is BND_WIDGET_HEIGHT
  193. void bndRadioButton(NVGcontext *ctx,
  194. float x, float y, float w, float h, int flags, BNDwidgetState state,
  195. int iconid, const char *label);
  196. // Draw an option button with its lower left origin at (x,y) and size of (w,h),
  197. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  198. // the widgets current UI state.
  199. // if label is not NULL, a label will be added to the widget
  200. // widget looks best when height is BND_WIDGET_HEIGHT
  201. void bndOptionButton(NVGcontext *ctx,
  202. float x, float y, float w, float h, BNDwidgetState state,
  203. const char *label);
  204. // Draw a choice button with its lower left origin at (x,y) and size of (w,h),
  205. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  206. // the widgets current UI state.
  207. // if iconid >= 0, an icon will be added to the widget
  208. // if label is not NULL, a label will be added to the widget
  209. // widget looks best when height is BND_WIDGET_HEIGHT
  210. void bndChoiceButton(NVGcontext *ctx,
  211. float x, float y, float w, float h, int flags, BNDwidgetState state,
  212. int iconid, const char *label);
  213. // Draw a number field with its lower left origin at (x,y) and size of (w,h),
  214. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  215. // the widgets current UI state.
  216. // if label is not NULL, a label will be added to the widget
  217. // if value is not NULL, a value will be added to the widget, along with
  218. // a ":" separator
  219. // widget looks best when height is BND_WIDGET_HEIGHT
  220. void bndNumberField(NVGcontext *ctx,
  221. float x, float y, float w, float h, int flags, BNDwidgetState state,
  222. const char *label, const char *value);
  223. // Draw slider control with its lower left origin at (x,y) and size of (w,h),
  224. // where flags is one or multiple flags from BNDcornerFlags and state denotes
  225. // the widgets current UI state.
  226. // progress must be in the range 0..1 and controls the size of the slider bar
  227. // if label is not NULL, a label will be added to the widget
  228. // if value is not NULL, a value will be added to the widget, along with
  229. // a ":" separator
  230. // widget looks best when height is BND_WIDGET_HEIGHT
  231. void bndSlider(NVGcontext *ctx,
  232. float x, float y, float w, float h, int flags, BNDwidgetState state,
  233. float progress, const char *label, const char *value);
  234. // Draw a menu background with its lower left origin at (x,y) and size of (w,h),
  235. // where flags is one or multiple flags from BNDcornerFlags.
  236. void bndMenuBackground(NVGcontext *ctx,
  237. float x, float y, float w, float h, int flags);
  238. // Draw a menu label with its lower left origin at (x,y) and size of (w,h).
  239. // if iconid >= 0, an icon will be added to the widget
  240. // if label is not NULL, a label will be added to the widget
  241. // widget looks best when height is BND_WIDGET_HEIGHT
  242. void bndMenuLabel(NVGcontext *ctx,
  243. float x, float y, float w, float h, int iconid, const char *label);
  244. // Draw a menu item with its lower left origin at (x,y) and size of (w,h),
  245. // where state denotes 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 bndMenuItem(NVGcontext *ctx,
  250. float x, float y, float w, float h, BNDwidgetState state,
  251. int iconid, const char *label);
  252. ////////////////////////////////////////////////////////////////////////////////
  253. // Low Level Functions
  254. // -------------------
  255. // these are part of the implementation detail and can be used to theme
  256. // new kinds of controls in a similar fashion.
  257. // make color transparent using the default alpha value
  258. NVGcolor bndTransparent(NVGcolor color);
  259. // offset a color by a given integer delta in the range -100 to 100
  260. NVGcolor bndOffsetColor(NVGcolor color, int delta);
  261. // assigns radius r to the four entries of array radiuses depending on whether
  262. // the corner is marked as sharp or not; see BNDcornerFlags for possible
  263. // flag values.
  264. void bndSelectCorners(float *radiuses, float r, int flags);
  265. // computes the upper and lower gradient colors for the inner box from a widget
  266. // theme and the widgets state. If flipActive is set and the state is
  267. // BND_ACTIVE, the upper and lower colors will be swapped.
  268. void bndInnerColors(NVGcolor *shade_top, NVGcolor *shade_down,
  269. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive);
  270. // computes the text color for a widget label from a widget theme and the
  271. // widgets state.
  272. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state);
  273. // Add a rounded box path at position (x,y) with size (w,h) and a separate
  274. // radius for each corner listed in clockwise order, so that cr0 = top left,
  275. // cr1 = top right, cr2 = bottom right, cr3 = bottom left;
  276. // this is a low level drawing function: the path must be stroked or filled
  277. // to become visible.
  278. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  279. float cr0, float cr1, float cr2, float cr3);
  280. // Draw a flat panel without any decorations at position (x,y) with size (w,h)
  281. // and fills it with backgroundColor
  282. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h);
  283. // Draw a lower inset for a rounded box at position (x,y) with size (w,h)
  284. // that gives the impression the surface has been pushed in.
  285. // cr2 and cr3 contain the radiuses of the bottom right and bottom left
  286. // corners of the rounded box.
  287. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  288. float cr2, float cr3);
  289. // Draw an icon with (x,y) as its upper left coordinate; the iconid selects
  290. // the icon from the sheet; use the BND_ICONID macro to build icon IDs.
  291. void bndIcon(NVGcontext *ctx, float x, float y, int iconid);
  292. // Draw a drop shadow around the rounded box at (x,y) with size (w,h) and
  293. // radius r, with feather as its maximum range in pixels.
  294. // No shadow will be painted inside the rounded box.
  295. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  296. float r, float feather, float alpha);
  297. // Draw the inner part of a widget box, with a gradient from shade_top to
  298. // shade_down.
  299. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  300. float cr0, float cr1, float cr2, float cr3,
  301. NVGcolor shade_top, NVGcolor shade_down);
  302. // Draw the outline part of a widget box with the given color
  303. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  304. float cr0, float cr1, float cr2, float cr3, NVGcolor color);
  305. // Draw an optional icon specified by <iconid> and an optional label with
  306. // given alignment (BNDtextAlignment), fontsize and color within a widget box.
  307. // if iconid is >= 0, an icon will be drawn and the labels remaining space
  308. // will be adjusted.
  309. // if label is not NULL, it will be drawn with the specified alignment, fontsize
  310. // and color.
  311. // if value is not NULL, label and value will be drawn with a ":" separator
  312. // inbetween.
  313. void bndIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
  314. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  315. const char *value);
  316. // Draw a checkmark for an option box with the given upper left coordinates
  317. // (ox,oy) with the specified color.
  318. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color);
  319. // Draw a horizontal arrow for a number field with its center at (x,y) and
  320. // size s; if s is negative, the arrow points to the left.
  321. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  322. // Draw an up/down arrow for a choice box with its center at (x,y) and size s
  323. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);
  324. #ifdef __cplusplus
  325. };
  326. #endif
  327. #endif // BLENDISH_H
  328. ////////////////////////////////////////////////////////////////////////////////
  329. ////////////////////////////////////////////////////////////////////////////////
  330. #ifdef BLENDISH_IMPLEMENTATION
  331. #include <math.h>
  332. #ifdef _MSC_VER
  333. #pragma warning (disable: 4996) // Switch off security warnings
  334. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  335. #ifdef __cplusplus
  336. #define BND_INLINE inline
  337. #else
  338. #define BND_INLINE
  339. #endif
  340. #else
  341. #define BND_INLINE inline
  342. #endif
  343. ////////////////////////////////////////////////////////////////////////////////
  344. // default text size
  345. #define BND_LABEL_FONT_SIZE 13
  346. // default text padding in inner box
  347. #define BND_PAD_LEFT 8
  348. #define BND_PAD_RIGHT 8
  349. // label: value separator string
  350. #define BND_LABEL_SEPARATOR ": "
  351. // alpha intensity of transparent items (0xa4)
  352. #define BND_TRANSPARENT_ALPHA 0.643
  353. // shade intensity of beveled insets
  354. #define BND_BEVEL_SHADE 30
  355. // shade intensity of hovered inner boxes
  356. #define BND_HOVER_SHADE 30
  357. // width of icon sheet
  358. #define BND_ICON_SHEET_WIDTH 602
  359. // height of icon sheet
  360. #define BND_ICON_SHEET_HEIGHT 640
  361. // gridsize of icon sheet in both dimensions
  362. #define BND_ICON_SHEET_GRID 21
  363. // offset of first icon tile relative to left border
  364. #define BND_ICON_SHEET_OFFSET_X 5
  365. // offset of first icon tile relative to top border
  366. #define BND_ICON_SHEET_OFFSET_Y 10
  367. // resolution of single icon
  368. #define BND_ICON_SHEET_RES 16
  369. // size of number field arrow
  370. #define BND_NUMBER_ARROW_SIZE 4
  371. // default text color
  372. #define BND_COLOR_TEXT {{{ 0,0,0,1 }}}
  373. // default highlighted text color
  374. #define BND_COLOR_TEXT_SELECTED {{{ 1,1,1,1 }}}
  375. // radius of tool button
  376. #define BND_TOOL_RADIUS 4
  377. // radius of option button
  378. #define BND_OPTION_RADIUS 4
  379. // width of option button checkbox
  380. #define BND_OPTION_WIDTH 14
  381. // height of option button checkbox
  382. #define BND_OPTION_HEIGHT 15
  383. // radius of number button
  384. #define BND_NUMBER_RADIUS 10
  385. // radius of menu popup
  386. #define BND_MENU_RADIUS 3
  387. // feather of menu popup shadow
  388. #define BND_SHADOW_FEATHER 12
  389. // alpha of menu popup shadow
  390. #define BND_SHADOW_ALPHA 0.5
  391. ////////////////////////////////////////////////////////////////////////////////
  392. BND_INLINE float bnd_clamp(float v, float mn, float mx) {
  393. return (v > mx)?mx:(v < mn)?mn:v;
  394. }
  395. ////////////////////////////////////////////////////////////////////////////////
  396. // the initial theme
  397. static BNDtheme bnd_theme = {
  398. // backgroundColor
  399. {{{ 0.447, 0.447, 0.447, 1.0 }}},
  400. // regularTheme
  401. {
  402. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  403. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  404. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  405. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  406. BND_COLOR_TEXT, // color_text
  407. BND_COLOR_TEXT_SELECTED, // color_text_selected
  408. 0, // shade_top
  409. 0, // shade_down
  410. },
  411. // toolTheme
  412. {
  413. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  414. {{{ 0.098,0.098,0.098,1 }}}, // color_item
  415. {{{ 0.6,0.6,0.6,1 }}}, // color_inner
  416. {{{ 0.392,0.392,0.392,1 }}}, // color_inner_selected
  417. BND_COLOR_TEXT, // color_text
  418. BND_COLOR_TEXT_SELECTED, // color_text_selected
  419. 15, // shade_top
  420. -15, // shade_down
  421. },
  422. // radioTheme
  423. {
  424. {{{ 0,0,0,1 }}}, // color_outline
  425. {{{ 1,1,1,1 }}}, // color_item
  426. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  427. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  428. BND_COLOR_TEXT_SELECTED, // color_text
  429. BND_COLOR_TEXT, // color_text_selected
  430. 15, // shade_top
  431. -15, // shade_down
  432. },
  433. // optionTheme
  434. {
  435. {{{ 0,0,0,1 }}}, // color_outline
  436. {{{ 1,1,1,1 }}}, // color_item
  437. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  438. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  439. BND_COLOR_TEXT, // color_text
  440. BND_COLOR_TEXT_SELECTED, // color_text_selected
  441. 15, // shade_top
  442. -15, // shade_down
  443. },
  444. // choiceTheme
  445. {
  446. {{{ 0,0,0,1 }}}, // color_outline
  447. {{{ 1,1,1,1 }}}, // color_item
  448. {{{ 0.275,0.275,0.275,1 }}}, // color_inner
  449. {{{ 0.275,0.275,0.275,1 }}}, // color_inner_selected
  450. BND_COLOR_TEXT_SELECTED, // color_text
  451. {{{ 0.8,0.8,0.8,1 }}}, // color_text_selected
  452. 15, // shade_top
  453. -15, // shade_down
  454. },
  455. // numberFieldTheme
  456. {
  457. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  458. {{{ 0.353, 0.353, 0.353,1 }}}, // color_item
  459. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  460. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  461. BND_COLOR_TEXT, // color_text
  462. BND_COLOR_TEXT_SELECTED, // color_text_selected
  463. -20, // shade_top
  464. 0, // shade_down
  465. },
  466. // sliderTheme
  467. {
  468. {{{ 0.098,0.098,0.098,1 }}}, // color_outline
  469. {{{ 0.502,0.502,0.502,1 }}}, // color_item
  470. {{{ 0.706, 0.706, 0.706,1 }}}, // color_inner
  471. {{{ 0.6, 0.6, 0.6,1 }}}, // color_inner_selected
  472. BND_COLOR_TEXT, // color_text
  473. BND_COLOR_TEXT_SELECTED, // color_text_selected
  474. -20, // shade_top
  475. 0, // shade_down
  476. },
  477. // menuTheme
  478. {
  479. {{{ 0,0,0,1 }}}, // color_outline
  480. {{{ 0.392,0.392,0.392,1 }}}, // color_item
  481. {{{ 0.098, 0.098, 0.098, 0.902 }}}, // color_inner
  482. {{{ 0.176, 0.176, 0.176, 0.902 }}}, // color_inner_selected
  483. {{{ 0.627, 0.627, 0.627, 1 }}}, // color_text
  484. BND_COLOR_TEXT_SELECTED, // color_text_selected
  485. 0, // shade_top
  486. 0, // shade_down
  487. },
  488. // menuItemTheme
  489. {
  490. {{{ 0,0,0,1 }}}, // color_outline
  491. {{{ 0.675,0.675,0.675,0.502 }}}, // color_item
  492. {{{ 0,0,0,0 }}}, // color_inner
  493. {{{ 0.337,0.502,0.761,1 }}}, // color_inner_selected
  494. BND_COLOR_TEXT_SELECTED, // color_text
  495. BND_COLOR_TEXT, // color_text_selected
  496. 38, // shade_top
  497. 0, // shade_down
  498. },
  499. };
  500. ////////////////////////////////////////////////////////////////////////////////
  501. void bndSetTheme(BNDtheme theme) {
  502. bnd_theme = theme;
  503. }
  504. const BNDtheme *bndGetTheme() {
  505. return &bnd_theme;
  506. }
  507. // the handle to the image containing the icon sheet
  508. static int bnd_icon_image = -1;
  509. void bndSetIconImage(int image) {
  510. bnd_icon_image = image;
  511. }
  512. // the handle to the UI font
  513. static int bnd_font = -1;
  514. void bndSetFont(int font) {
  515. bnd_font = font;
  516. }
  517. ////////////////////////////////////////////////////////////////////////////////
  518. void bndLabel(NVGcontext *ctx,
  519. float x, float y, float w, float h, int iconid, const char *label) {
  520. bndIconLabel(ctx,x,y,w,h,iconid,
  521. bnd_theme.regularTheme.textColor, BND_LEFT,
  522. BND_LABEL_FONT_SIZE, label, NULL);
  523. }
  524. void bndToolButton(NVGcontext *ctx,
  525. float x, float y, float w, float h, int flags, BNDwidgetState state,
  526. int iconid, const char *label) {
  527. float cr[4];
  528. NVGcolor shade_top, shade_down;
  529. bndSelectCorners(cr, BND_TOOL_RADIUS, flags);
  530. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  531. bndInnerColors(&shade_top, &shade_down, &bnd_theme.toolTheme, state, 1);
  532. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  533. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  534. bndTransparent(bnd_theme.toolTheme.outlineColor));
  535. bndIconLabel(ctx,x,y,w,h,iconid,
  536. bndTextColor(&bnd_theme.toolTheme, state), BND_CENTER,
  537. BND_LABEL_FONT_SIZE, label, NULL);
  538. }
  539. void bndRadioButton(NVGcontext *ctx,
  540. float x, float y, float w, float h, int flags, BNDwidgetState state,
  541. int iconid, const char *label) {
  542. float cr[4];
  543. NVGcolor shade_top, shade_down;
  544. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  545. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  546. bndInnerColors(&shade_top, &shade_down, &bnd_theme.radioTheme, state, 1);
  547. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  548. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  549. bndTransparent(bnd_theme.radioTheme.outlineColor));
  550. bndIconLabel(ctx,x,y,w,h,iconid,
  551. bndTextColor(&bnd_theme.radioTheme, state), BND_CENTER,
  552. BND_LABEL_FONT_SIZE, label, NULL);
  553. }
  554. void bndOptionButton(NVGcontext *ctx,
  555. float x, float y, float w, float h, BNDwidgetState state,
  556. const char *label) {
  557. float ox, oy;
  558. NVGcolor shade_top, shade_down;
  559. ox = x;
  560. oy = y+h-BND_OPTION_HEIGHT-3;
  561. bndBevelInset(ctx,ox,oy,
  562. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  563. BND_OPTION_RADIUS,BND_OPTION_RADIUS);
  564. bndInnerColors(&shade_top, &shade_down, &bnd_theme.optionTheme, state, 1);
  565. bndInnerBox(ctx,ox,oy,
  566. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  567. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  568. shade_top, shade_down);
  569. bndOutlineBox(ctx,ox,oy,
  570. BND_OPTION_WIDTH,BND_OPTION_HEIGHT,
  571. BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,BND_OPTION_RADIUS,
  572. bndTransparent(bnd_theme.optionTheme.outlineColor));
  573. if (state == BND_ACTIVE) {
  574. bndCheck(ctx,ox,oy, bndTransparent(bnd_theme.optionTheme.itemColor));
  575. }
  576. bndIconLabel(ctx,x+12,y,w-12,h,-1,
  577. bndTextColor(&bnd_theme.optionTheme, state), BND_LEFT,
  578. BND_LABEL_FONT_SIZE, label, NULL);
  579. }
  580. void bndChoiceButton(NVGcontext *ctx,
  581. float x, float y, float w, float h, int flags, BNDwidgetState state,
  582. int iconid, const char *label) {
  583. float cr[4];
  584. NVGcolor shade_top, shade_down;
  585. bndSelectCorners(cr, BND_OPTION_RADIUS, flags);
  586. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  587. bndInnerColors(&shade_top, &shade_down, &bnd_theme.choiceTheme, state, 1);
  588. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  589. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  590. bndTransparent(bnd_theme.choiceTheme.outlineColor));
  591. bndIconLabel(ctx,x,y,w,h,iconid,
  592. bndTextColor(&bnd_theme.choiceTheme, state), BND_LEFT,
  593. BND_LABEL_FONT_SIZE, label, NULL);
  594. bndUpDownArrow(ctx,x+w-10,y+10,5,
  595. bndTransparent(bnd_theme.choiceTheme.itemColor));
  596. }
  597. void bndNumberField(NVGcontext *ctx,
  598. float x, float y, float w, float h, int flags, BNDwidgetState state,
  599. const char *label, const char *value) {
  600. float cr[4];
  601. NVGcolor shade_top, shade_down;
  602. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  603. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  604. bndInnerColors(&shade_top, &shade_down, &bnd_theme.numberFieldTheme, state, 0);
  605. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  606. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  607. bndTransparent(bnd_theme.numberFieldTheme.outlineColor));
  608. bndIconLabel(ctx,x,y,w,h,-1,
  609. bndTextColor(&bnd_theme.numberFieldTheme, state), BND_CENTER,
  610. BND_LABEL_FONT_SIZE, label, value);
  611. bndArrow(ctx,x+8,y+10,-BND_NUMBER_ARROW_SIZE,
  612. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  613. bndArrow(ctx,x+w-8,y+10,BND_NUMBER_ARROW_SIZE,
  614. bndTransparent(bnd_theme.numberFieldTheme.itemColor));
  615. }
  616. void bndSlider(NVGcontext *ctx,
  617. float x, float y, float w, float h, int flags, BNDwidgetState state,
  618. float progress, const char *label, const char *value) {
  619. float cr[4];
  620. NVGcolor shade_top, shade_down;
  621. bndSelectCorners(cr, BND_NUMBER_RADIUS, flags);
  622. bndBevelInset(ctx,x,y,w,h,cr[2],cr[3]);
  623. bndInnerColors(&shade_top, &shade_down, &bnd_theme.sliderTheme, state, 0);
  624. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  625. if (state == BND_ACTIVE) {
  626. shade_top = bndOffsetColor(
  627. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  628. shade_down = bndOffsetColor(
  629. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  630. } else {
  631. shade_top = bndOffsetColor(
  632. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeDown);
  633. shade_down = bndOffsetColor(
  634. bnd_theme.sliderTheme.itemColor, bnd_theme.sliderTheme.shadeTop);
  635. }
  636. nvgScissor(ctx,x,y,8+(w-8)*bnd_clamp(progress,0,1),h);
  637. bndInnerBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  638. nvgResetScissor(ctx);
  639. bndOutlineBox(ctx,x,y,w,h,cr[0],cr[1],cr[2],cr[3],
  640. bndTransparent(bnd_theme.sliderTheme.outlineColor));
  641. bndIconLabel(ctx,x,y,w,h,-1,
  642. bndTextColor(&bnd_theme.sliderTheme, state), BND_CENTER,
  643. BND_LABEL_FONT_SIZE, label, value);
  644. }
  645. void bndMenuBackground(NVGcontext *ctx,
  646. float x, float y, float w, float h, int flags) {
  647. float cr[4];
  648. NVGcolor shade_top, shade_down;
  649. bndSelectCorners(cr, BND_MENU_RADIUS, flags);
  650. bndInnerColors(&shade_top, &shade_down, &bnd_theme.menuTheme,
  651. BND_DEFAULT, 0);
  652. bndInnerBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3], shade_top, shade_down);
  653. bndOutlineBox(ctx,x,y,w,h+1,cr[0],cr[1],cr[2],cr[3],
  654. bndTransparent(bnd_theme.menuTheme.outlineColor));
  655. bndDropShadow(ctx,x,y,w,h,BND_MENU_RADIUS,
  656. BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
  657. }
  658. void bndMenuLabel(NVGcontext *ctx,
  659. float x, float y, float w, float h, int iconid, const char *label) {
  660. bndIconLabel(ctx,x,y,w,h,iconid,
  661. bnd_theme.menuTheme.textColor, BND_LEFT,
  662. BND_LABEL_FONT_SIZE, label, NULL);
  663. }
  664. void bndMenuItem(NVGcontext *ctx,
  665. float x, float y, float w, float h, BNDwidgetState state,
  666. int iconid, const char *label) {
  667. NVGcolor shade_top, shade_down;
  668. if (state != BND_DEFAULT) {
  669. bndInnerBox(ctx,x,y,w,h,0,0,0,0,
  670. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  671. bnd_theme.menuItemTheme.shadeTop),
  672. bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
  673. bnd_theme.menuItemTheme.shadeDown));
  674. state = BND_ACTIVE;
  675. }
  676. bndIconLabel(ctx,x,y,w,h,iconid,
  677. bndTextColor(&bnd_theme.menuItemTheme, state), BND_LEFT,
  678. BND_LABEL_FONT_SIZE, label, NULL);
  679. }
  680. ////////////////////////////////////////////////////////////////////////////////
  681. void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h,
  682. float cr0, float cr1, float cr2, float cr3) {
  683. float d;
  684. w = fmaxf(0, w);
  685. h = fmaxf(0, h);
  686. d = fminf(w, h);
  687. nvgMoveTo(ctx, x,y+h*0.5f);
  688. nvgArcTo(ctx, x,y, x+w,y, fminf(cr0, d/2));
  689. nvgArcTo(ctx, x+w,y, x+w,y+h, fminf(cr1, d/2));
  690. nvgArcTo(ctx, x+w,y+h, x,y+h, fminf(cr2, d/2));
  691. nvgArcTo(ctx, x,y+h, x,y, fminf(cr3, d/2));
  692. nvgClosePath(ctx);
  693. }
  694. NVGcolor bndTransparent(NVGcolor color) {
  695. color.a *= BND_TRANSPARENT_ALPHA;
  696. return color;
  697. }
  698. NVGcolor bndOffsetColor(NVGcolor color, int delta) {
  699. float offset = (float)delta / 255.0f;
  700. return delta?(
  701. nvgRGBAf(
  702. bnd_clamp(color.r+offset,0,1),
  703. bnd_clamp(color.g+offset,0,1),
  704. bnd_clamp(color.b+offset,0,1),
  705. color.a)
  706. ):color;
  707. }
  708. void bndBevelInset(NVGcontext *ctx, float x, float y, float w, float h,
  709. float cr2, float cr3) {
  710. float d;
  711. y -= 0.5f;
  712. d = fminf(w, h);
  713. cr2 = fminf(cr2, d/2);
  714. cr3 = fminf(cr3, d/2);
  715. nvgBeginPath(ctx);
  716. nvgMoveTo(ctx, x+w,y+h-cr2);
  717. nvgArcTo(ctx, x+w,y+h, x,y+h, cr2);
  718. nvgArcTo(ctx, x,y+h, x,y, cr3);
  719. NVGcolor bevelColor = bndOffsetColor(bnd_theme.backgroundColor,
  720. BND_BEVEL_SHADE);
  721. nvgStrokeWidth(ctx, 1);
  722. nvgStrokePaint(ctx,
  723. nvgLinearGradient(ctx,
  724. x,y+h-fmaxf(cr2,cr3)-1,
  725. x,y+h-1,
  726. nvgRGBAf(bevelColor.r, bevelColor.g, bevelColor.b, 0),
  727. bevelColor));
  728. nvgStroke(ctx);
  729. }
  730. void bndBackground(NVGcontext *ctx, float x, float y, float w, float h) {
  731. nvgBeginPath(ctx);
  732. nvgRect(ctx, x, y, w, h);
  733. nvgFillColor(ctx, bnd_theme.backgroundColor);
  734. nvgFill(ctx);
  735. }
  736. void bndIcon(NVGcontext *ctx, float x, float y, int iconid) {
  737. int ix, iy, u, v;
  738. if (bnd_icon_image < 0) return; // no icons loaded
  739. ix = iconid & 0xff;
  740. iy = (iconid>>8) & 0xff;
  741. u = BND_ICON_SHEET_OFFSET_X + ix*BND_ICON_SHEET_GRID;
  742. v = BND_ICON_SHEET_OFFSET_Y + iy*BND_ICON_SHEET_GRID;
  743. nvgBeginPath(ctx);
  744. nvgRect(ctx,x,y,BND_ICON_SHEET_RES,BND_ICON_SHEET_RES);
  745. nvgFillPaint(ctx,
  746. nvgImagePattern(ctx,x-u,y-v,
  747. BND_ICON_SHEET_WIDTH,
  748. BND_ICON_SHEET_HEIGHT,
  749. 0,bnd_icon_image,0));
  750. nvgFill(ctx);
  751. }
  752. void bndDropShadow(NVGcontext *ctx, float x, float y, float w, float h,
  753. float r, float feather, float alpha) {
  754. nvgBeginPath(ctx);
  755. y += feather;
  756. h -= feather;
  757. nvgMoveTo(ctx, x-feather, y-feather);
  758. nvgLineTo(ctx, x, y-feather);
  759. nvgLineTo(ctx, x, y+h-feather);
  760. nvgArcTo(ctx, x,y+h,x+r,y+h,r);
  761. nvgArcTo(ctx, x+w,y+h,x+w,y+h-r,r);
  762. nvgLineTo(ctx, x+w, y-feather);
  763. nvgLineTo(ctx, x+w+feather, y-feather);
  764. nvgLineTo(ctx, x+w+feather, y+h+feather);
  765. nvgLineTo(ctx, x-feather, y+h+feather);
  766. nvgClosePath(ctx);
  767. nvgFillPaint(ctx, nvgBoxGradient(ctx,
  768. x - feather*0.5f,y - feather*0.5f,
  769. w + feather,h+feather,
  770. r+feather*0.5f,
  771. feather,
  772. nvgRGBAf(0,0,0,alpha*alpha),
  773. nvgRGBAf(0,0,0,0)));
  774. nvgFill(ctx);
  775. }
  776. void bndInnerBox(NVGcontext *ctx, float x, float y, float w, float h,
  777. float cr0, float cr1, float cr2, float cr3,
  778. NVGcolor shade_top, NVGcolor shade_down) {
  779. nvgBeginPath(ctx);
  780. bndRoundedBox(ctx,x+1,y+1,w-2,h-3,
  781. fmaxf(0,cr0-1),fmaxf(0,cr1-1),fmaxf(0,cr2-1),fmaxf(0,cr3-1));
  782. nvgFillPaint(ctx,
  783. nvgLinearGradient(ctx,x,y,x,y+h,shade_top,shade_down));
  784. nvgFill(ctx);
  785. }
  786. void bndOutlineBox(NVGcontext *ctx, float x, float y, float w, float h,
  787. float cr0, float cr1, float cr2, float cr3, NVGcolor color) {
  788. nvgBeginPath(ctx);
  789. bndRoundedBox(ctx,x+0.5f,y+0.5f,w-1,h-2,cr0,cr1,cr2,cr3);
  790. nvgStrokeColor(ctx,color);
  791. nvgStrokeWidth(ctx,1);
  792. nvgStroke(ctx);
  793. }
  794. void bndSelectCorners(float *radiuses, float r, int flags) {
  795. radiuses[0] = (flags & BND_CORNER_TOP_LEFT)?0:r;
  796. radiuses[1] = (flags & BND_CORNER_TOP_RIGHT)?0:r;
  797. radiuses[2] = (flags & BND_CORNER_DOWN_RIGHT)?0:r;
  798. radiuses[3] = (flags & BND_CORNER_DOWN_LEFT)?0:r;
  799. }
  800. void bndInnerColors(
  801. NVGcolor *shade_top, NVGcolor *shade_down,
  802. const BNDwidgetTheme *theme, BNDwidgetState state, int flipActive) {
  803. switch(state) {
  804. default:
  805. case BND_DEFAULT: {
  806. *shade_top = bndOffsetColor(theme->innerColor, theme->shadeTop);
  807. *shade_down = bndOffsetColor(theme->innerColor, theme->shadeDown);
  808. } break;
  809. case BND_HOVER: {
  810. NVGcolor color = bndOffsetColor(theme->innerColor, BND_HOVER_SHADE);
  811. *shade_top = bndOffsetColor(color, theme->shadeTop);
  812. *shade_down = bndOffsetColor(color, theme->shadeDown);
  813. } break;
  814. case BND_ACTIVE: {
  815. *shade_top = bndOffsetColor(theme->innerSelectedColor,
  816. flipActive?theme->shadeDown:theme->shadeTop);
  817. *shade_down = bndOffsetColor(theme->innerSelectedColor,
  818. flipActive?theme->shadeTop:theme->shadeDown);
  819. } break;
  820. }
  821. }
  822. NVGcolor bndTextColor(const BNDwidgetTheme *theme, BNDwidgetState state) {
  823. return (state == BND_ACTIVE)?theme->textSelectedColor:theme->textColor;
  824. }
  825. void bndIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
  826. int iconid, NVGcolor color, int align, float fontsize, const char *label,
  827. const char *value) {
  828. float pleft = BND_PAD_LEFT;
  829. if (label) {
  830. if (iconid >= 0) {
  831. bndIcon(ctx,x+4,y+2,iconid);
  832. pleft += BND_ICON_SHEET_RES;
  833. }
  834. if (bnd_font < 0) return;
  835. nvgFontFaceId(ctx, bnd_font);
  836. nvgFontSize(ctx, fontsize);
  837. nvgBeginPath(ctx);
  838. nvgFillColor(ctx, color);
  839. if (value) {
  840. float label_width = nvgTextBounds(ctx, label, NULL, NULL);
  841. float sep_width = nvgTextBounds(ctx,
  842. BND_LABEL_SEPARATOR, NULL, NULL);
  843. nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
  844. x += pleft;
  845. if (align == BND_CENTER) {
  846. float width = label_width + sep_width
  847. + nvgTextBounds(ctx, value, NULL, NULL);
  848. x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
  849. }
  850. y += h-6;
  851. nvgText(ctx, x, y, label, NULL);
  852. x += label_width;
  853. nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL);
  854. x += sep_width;
  855. nvgText(ctx, x, y, value, NULL);
  856. } else {
  857. nvgTextAlign(ctx,
  858. (align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE):
  859. (NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE));
  860. nvgTextBox(ctx,x+pleft,y+h-6,w-BND_PAD_RIGHT-pleft,label, NULL);
  861. }
  862. } else if (iconid >= 0) {
  863. bndIcon(ctx,x+2,y+2,iconid);
  864. }
  865. }
  866. void bndCheck(NVGcontext *ctx, float ox, float oy, NVGcolor color) {
  867. nvgBeginPath(ctx);
  868. nvgStrokeWidth(ctx,2);
  869. nvgStrokeColor(ctx,color);
  870. nvgLineCap(ctx,NVG_BUTT);
  871. nvgLineJoin(ctx,NVG_MITER);
  872. nvgMoveTo(ctx,ox+4,oy+5);
  873. nvgLineTo(ctx,ox+7,oy+8);
  874. nvgLineTo(ctx,ox+14,oy+1);
  875. nvgStroke(ctx);
  876. }
  877. void bndArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  878. nvgBeginPath(ctx);
  879. nvgMoveTo(ctx,x,y);
  880. nvgLineTo(ctx,x-s,y+s);
  881. nvgLineTo(ctx,x-s,y-s);
  882. nvgClosePath(ctx);
  883. nvgFillColor(ctx,color);
  884. nvgFill(ctx);
  885. }
  886. void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color) {
  887. float w;
  888. nvgBeginPath(ctx);
  889. w = 1.1f*s;
  890. nvgMoveTo(ctx,x,y-1);
  891. nvgLineTo(ctx,x+0.5*w,y-s-1);
  892. nvgLineTo(ctx,x+w,y-1);
  893. nvgClosePath(ctx);
  894. nvgMoveTo(ctx,x,y+1);
  895. nvgLineTo(ctx,x+0.5*w,y+s+1);
  896. nvgLineTo(ctx,x+w,y+1);
  897. nvgClosePath(ctx);
  898. nvgFillColor(ctx,color);
  899. nvgFill(ctx);
  900. }
  901. ////////////////////////////////////////////////////////////////////////////////
  902. #ifdef BND_INLINE
  903. #undef BND_INLINE
  904. #endif
  905. #endif // BLENDISH_IMPLEMENTATION