Audio plugin host https://kx.studio/carla
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.

821 lines
25KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_NANO_WIDGET_HPP_INCLUDED
  17. #define DGL_NANO_WIDGET_HPP_INCLUDED
  18. #include "Widget.hpp"
  19. struct NVGcolor;
  20. struct NVGcontext;
  21. struct NVGpaint;
  22. START_NAMESPACE_DGL
  23. // -----------------------------------------------------------------------
  24. // NanoImage
  25. /**
  26. NanoVG Image class.
  27. This implements NanoVG images as a C++ class where deletion is handled automatically.
  28. Images need to be created within a NanoVG or NanoWidget class.
  29. */
  30. class NanoImage
  31. {
  32. public:
  33. /**
  34. Destructor.
  35. */
  36. ~NanoImage();
  37. /**
  38. Get size.
  39. */
  40. Size<int> getSize() const;
  41. /**
  42. Update image data.
  43. */
  44. void updateImage(const uchar* data);
  45. protected:
  46. /**
  47. Constructors are protected.
  48. NanoImages must be created within a NanoVG or NanoWidget class.
  49. */
  50. NanoImage(NVGcontext* context, int imageId) noexcept;
  51. private:
  52. NVGcontext* fContext;
  53. int fImageId;
  54. friend class NanoVG;
  55. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoImage)
  56. };
  57. // -----------------------------------------------------------------------
  58. // NanoVG
  59. /**
  60. NanoVG class.
  61. This class exposes the NanoVG drawing API.
  62. All calls should be wrapped in beginFrame() & endFrame().
  63. @section Color utils
  64. Colors in NanoVG are stored as uints in ABGR format.
  65. @section State Handling
  66. NanoVG contains state which represents how paths will be rendered.
  67. The state contains transform, fill and stroke styles, text and font styles, and scissor clipping.
  68. @section Render styles
  69. Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
  70. Solid color is simply defined as a color value, different kinds of paints can be created
  71. using linearGradient(), boxGradient(), radialGradient() and imagePattern().
  72. Current render style can be saved and restored using save() and restore().
  73. @section Transforms
  74. The paths, gradients, patterns and scissor region are transformed by an transformation
  75. matrix at the time when they are passed to the API.
  76. The current transformation matrix is a affine matrix:
  77. [sx kx tx]
  78. [ky sy ty]
  79. [ 0 0 1]
  80. Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
  81. The last row is assumed to be 0,0,1 and is not stored.
  82. Apart from resetTransform(), each transformation function first creates
  83. specific transformation matrix and pre-multiplies the current transformation by it.
  84. Current coordinate system (transformation) can be saved and restored using save() and restore().
  85. @section Images
  86. NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
  87. In addition you can upload your own image. The image loading is provided by stb_image.
  88. @section Paints
  89. NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
  90. These can be used as paints for strokes and fills.
  91. @section Scissoring
  92. Scissoring allows you to clip the rendering into a rectangle. This is useful for varius
  93. user interface cases like rendering a text edit or a timeline.
  94. @section Paths
  95. Drawing a new shape starts with beginPath(), it clears all the currently defined paths.
  96. Then you define one or more paths and sub-paths which describe the shape. The are functions
  97. to draw common shapes like rectangles and circles, and lower level step-by-step functions,
  98. which allow to define a path curve by curve.
  99. NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
  100. winding and holes should have counter clockwise order. To specify winding of a path you can
  101. call pathWinding(). This is useful especially for the common shapes, which are drawn CCW.
  102. Finally you can fill the path using current fill style by calling fill(), and stroke it
  103. with current stroke style by calling stroke().
  104. The curve segments and sub-paths are transformed by the current transform.
  105. @section Text
  106. NanoVG allows you to load .ttf files and use the font to render text.
  107. The appearance of the text can be defined by setting the current text style
  108. and by specifying the fill color. Common text and font settings such as
  109. font size, letter spacing and text align are supported. Font blur allows you
  110. to create simple text effects such as drop shadows.
  111. At render time the font face can be set based on the font handles or name.
  112. Font measure functions return values in local space, the calculations are
  113. carried in the same resolution as the final rendering. This is done because
  114. the text glyph positions are snapped to the nearest pixels sharp rendering.
  115. The local space means that values are not rotated or scale as per the current
  116. transformation. For example if you set font size to 12, which would mean that
  117. line height is 16, then regardless of the current scaling and rotation, the
  118. returned line height is always 16. Some measures may vary because of the scaling
  119. since aforementioned pixel snapping.
  120. While this may sound a little odd, the setup allows you to always render the
  121. same way regardless of scaling. I.e. following works regardless of scaling:
  122. @code
  123. const char* txt = "Text me up.";
  124. textBounds(vg, x,y, txt, NULL, bounds);
  125. beginPath(vg);
  126. roundedRect(vg, bounds[0],bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1]);
  127. fill(vg);
  128. @endcode
  129. Note: currently only solid color fill is supported for text.
  130. */
  131. class NanoVG
  132. {
  133. public:
  134. enum Align {
  135. // Horizontal align
  136. ALIGN_LEFT = 1 << 0, // Align horizontally to left (default).
  137. ALIGN_CENTER = 1 << 1, // Align horizontally to center.
  138. ALIGN_RIGHT = 1 << 2, // Align horizontally to right.
  139. // Vertical align
  140. ALIGN_TOP = 1 << 3, // Align vertically to top.
  141. ALIGN_MIDDLE = 1 << 4, // Align vertically to middle.
  142. ALIGN_BOTTOM = 1 << 5, // Align vertically to bottom.
  143. ALIGN_BASELINE = 1 << 6 // Align vertically to baseline (default).
  144. };
  145. enum Alpha {
  146. STRAIGHT_ALPHA,
  147. PREMULTIPLIED_ALPHA,
  148. };
  149. enum LineCap {
  150. BUTT,
  151. ROUND,
  152. SQUARE,
  153. BEVEL,
  154. MITER
  155. };
  156. enum PatternRepeat {
  157. REPEAT_NONE = 0x0, // No repeat
  158. REPEAT_X = 0x1, // Repeat in X direction
  159. REPEAT_Y = 0x2 // Repeat in Y direction
  160. };
  161. enum Solidity {
  162. SOLID = 1, // CCW
  163. HOLE = 2 // CW
  164. };
  165. enum Winding {
  166. CCW = 1, // Winding for solid shapes
  167. CW = 2 // Winding for holes
  168. };
  169. struct Color {
  170. union {
  171. float rgba[4];
  172. struct { float r,g,b,a; };
  173. };
  174. Color() noexcept;
  175. Color(const NVGcolor&) noexcept;
  176. operator NVGcolor() const noexcept;
  177. };
  178. struct Paint {
  179. float xform[6];
  180. float extent[2];
  181. float radius;
  182. float feather;
  183. Color innerColor;
  184. Color outerColor;
  185. int imageId;
  186. PatternRepeat repeat;
  187. Paint() noexcept;
  188. Paint(const NVGpaint&) noexcept;
  189. operator NVGpaint() const noexcept;
  190. };
  191. struct GlyphPosition {
  192. const char* str; // Position of the glyph in the input string.
  193. float x; // The x-coordinate of the logical glyph position.
  194. float minx, maxx; // The bounds of the glyph shape.
  195. };
  196. struct TextRow {
  197. const char* start; // Pointer to the input text where the row starts.
  198. const char* end; // Pointer to the input text where the row ends (one past the last character).
  199. const char* next; // Pointer to the beginning of the next row.
  200. float width; // Logical width of the row.
  201. float minx, maxx; // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
  202. };
  203. typedef int FontId;
  204. /**
  205. Constructor.
  206. Uses 512x512 as default atlas size.
  207. */
  208. NanoVG();
  209. /**
  210. Constructor using custom text atlas size.
  211. */
  212. NanoVG(int textAtlasWidth, int textAtlasHeight);
  213. /**
  214. Destructor.
  215. */
  216. ~NanoVG();
  217. /**
  218. Get the NanoVG context.
  219. You should not need this under normal circumstances.
  220. */
  221. NVGcontext* getContext() const noexcept
  222. {
  223. return fContext;
  224. }
  225. /**
  226. Begin drawing a new frame.
  227. @param withAlha Controls if drawing the shapes to the render target should be done using straight or pre-multiplied alpha.
  228. */
  229. void beginFrame(int width, int height, float scaleFactor = 1.0f, Alpha alpha = PREMULTIPLIED_ALPHA);
  230. /**
  231. Begin drawing a new frame inside a widget.
  232. */
  233. void beginFrame(Widget* widget);
  234. /**
  235. Ends drawing flushing remaining render state.
  236. */
  237. void endFrame();
  238. /* --------------------------------------------------------------------
  239. * Color utils */
  240. /**
  241. Returns a color value from red, green, blue values. Alpha will be set to 255 (1.0f).
  242. */
  243. static Color RGB(uchar r, uchar g, uchar b);
  244. /**
  245. Returns a color value from red, green, blue values. Alpha will be set to 1.0f.
  246. */
  247. static Color RGBf(float r, float g, float b);
  248. /**
  249. Returns a color value from red, green, blue and alpha values.
  250. */
  251. static Color RGBA(uchar r, uchar g, uchar b, uchar a);
  252. /**
  253. Returns a color value from red, green, blue and alpha values.
  254. */
  255. static Color RGBAf(float r, float g, float b, float a);
  256. /**
  257. Linearly interpolates from color c0 to c1, and returns resulting color value.
  258. */
  259. static Color lerpRGBA(const Color& c0, const Color& c1, float u);
  260. /**
  261. Returns color value specified by hue, saturation and lightness.
  262. HSL values are all in range [0..1], alpha will be set to 255.
  263. */
  264. static Color HSL(float h, float s, float l);
  265. /**
  266. Returns color value specified by hue, saturation and lightness and alpha.
  267. HSL values are all in range [0..1], alpha in range [0..255]
  268. */
  269. static Color HSLA(float h, float s, float l, uchar a);
  270. /* --------------------------------------------------------------------
  271. * State Handling */
  272. /**
  273. Pushes and saves the current render state into a state stack.
  274. A matching restore() must be used to restore the state.
  275. */
  276. void save();
  277. /**
  278. Pops and restores current render state.
  279. */
  280. void restore();
  281. /**
  282. Resets current render state to default values. Does not affect the render state stack.
  283. */
  284. void reset();
  285. /* --------------------------------------------------------------------
  286. * Render styles */
  287. /**
  288. Sets current stroke style to a solid color.
  289. */
  290. void strokeColor(const Color& color);
  291. /**
  292. Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
  293. */
  294. void strokePaint(const Paint& paint);
  295. /**
  296. Sets current fill style to a solid color.
  297. */
  298. void fillColor(const Color& color);
  299. /**
  300. Sets current fill style to a paint, which can be a one of the gradients or a pattern.
  301. */
  302. void fillPaint(const Paint& paint);
  303. /**
  304. Sets the miter limit of the stroke style.
  305. Miter limit controls when a sharp corner is beveled.
  306. */
  307. void miterLimit(float limit);
  308. /**
  309. Sets the stroke width of the stroke style.
  310. */
  311. void strokeWidth(float size);
  312. /**
  313. Sets how the end of the line (cap) is drawn,
  314. Can be one of: BUTT, ROUND, SQUARE.
  315. */
  316. void lineCap(LineCap cap = BUTT);
  317. /**
  318. Sets how sharp path corners are drawn.
  319. Can be one of MITER, ROUND, BEVEL.
  320. */
  321. void lineJoin(LineCap join = MITER);
  322. /* --------------------------------------------------------------------
  323. * Transforms */
  324. /**
  325. Resets current transform to a identity matrix.
  326. */
  327. void resetTransform();
  328. /**
  329. Pre-multiplies current coordinate system by specified matrix.
  330. The parameters are interpreted as matrix as follows:
  331. [a c e]
  332. [b d f]
  333. [0 0 1]
  334. */
  335. void transform(float a, float b, float c, float d, float e, float f);
  336. /**
  337. Translates current coordinate system.
  338. */
  339. void translate(float x, float y);
  340. /**
  341. Rotates current coordinate system. Angle is specified in radians.
  342. */
  343. void rotate(float angle);
  344. /**
  345. Skews the current coordinate system along X axis. Angle is specified in radians.
  346. */
  347. void skewX(float angle);
  348. /**
  349. Skews the current coordinate system along Y axis. Angle is specified in radians.
  350. */
  351. void skewY(float angle);
  352. /**
  353. Scales the current coordinate system.
  354. */
  355. void scale(float x, float y);
  356. /**
  357. Stores the top part (a-f) of the current transformation matrix in to the specified buffer.
  358. [a c e]
  359. [b d f]
  360. [0 0 1]
  361. */
  362. void currentTransform(float xform[6]);
  363. /**
  364. The following functions can be used to make calculations on 2x3 transformation matrices.
  365. A 2x3 matrix is represented as float[6]. */
  366. /**
  367. Sets the transform to identity matrix.
  368. */
  369. static void transformIdentity(float dst[6]);
  370. /**
  371. Sets the transform to translation matrix matrix.
  372. */
  373. static void transformTranslate(float dst[6], float tx, float ty);
  374. /**
  375. Sets the transform to scale matrix.
  376. */
  377. static void transformScale(float dst[6], float sx, float sy);
  378. /**
  379. Sets the transform to rotate matrix. Angle is specified in radians.
  380. */
  381. static void transformRotate(float dst[6], float a);
  382. /**
  383. Sets the transform to skew-x matrix. Angle is specified in radians.
  384. */
  385. static void transformSkewX(float dst[6], float a);
  386. /**
  387. Sets the transform to skew-y matrix. Angle is specified in radians.
  388. */
  389. static void transformSkewY(float dst[6], float a);
  390. /**
  391. Sets the transform to the result of multiplication of two transforms, of A = A*B.
  392. */
  393. static void transformMultiply(float dst[6], const float src[6]);
  394. /**
  395. Sets the transform to the result of multiplication of two transforms, of A = B*A.
  396. */
  397. static void transformPremultiply(float dst[6], const float src[6]);
  398. /**
  399. Sets the destination to inverse of specified transform.
  400. Returns 1 if the inverse could be calculated, else 0.
  401. */
  402. static int transformInverse(float dst[6], const float src[6]);
  403. /**
  404. Transform a point by given transform.
  405. */
  406. static void transformPoint(float& dstx, float& dsty, const float xform[6], float srcx, float srcy);
  407. /**
  408. Convert degrees to radians.
  409. */
  410. static float degToRad(float deg);
  411. /**
  412. Convert radians to degrees.
  413. */
  414. static float radToDeg(float rad);
  415. /* --------------------------------------------------------------------
  416. * Images */
  417. /**
  418. Creates image by loading it from the disk from specified file name.
  419. */
  420. NanoImage* createImage(const char* filename);
  421. /**
  422. Creates image by loading it from the specified chunk of memory.
  423. */
  424. NanoImage* createImageMem(uchar* data, int ndata);
  425. /**
  426. Creates image from specified image data.
  427. */
  428. NanoImage* createImageRGBA(int w, int h, const uchar* data);
  429. /* --------------------------------------------------------------------
  430. * Paints */
  431. /**
  432. Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
  433. of the linear gradient, icol specifies the start color and ocol the end color.
  434. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  435. */
  436. Paint linearGradient(float sx, float sy, float ex, float ey, const Color& icol, const Color& ocol);
  437. /**
  438. Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
  439. drop shadows or highlights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
  440. (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
  441. the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
  442. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  443. */
  444. Paint boxGradient(float x, float y, float w, float h, float r, float f, const Color& icol, const Color& ocol);
  445. /**
  446. Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
  447. the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
  448. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  449. */
  450. Paint radialGradient(float cx, float cy, float inr, float outr, const Color& icol, const Color& ocol);
  451. /**
  452. Creates and returns an image pattern. Parameters (ox,oy) specify the left-top location of the image pattern,
  453. (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render,
  454. and repeat tells if the image should be repeated across x or y.
  455. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  456. */
  457. Paint imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage* image, PatternRepeat repeat);
  458. /* --------------------------------------------------------------------
  459. * Scissoring */
  460. /**
  461. Sets the current
  462. The scissor rectangle is transformed by the current transform.
  463. */
  464. void scissor(float x, float y, float w, float h);
  465. /**
  466. Reset and disables scissoring.
  467. */
  468. void resetScissor();
  469. /* --------------------------------------------------------------------
  470. * Paths */
  471. /**
  472. Clears the current path and sub-paths.
  473. */
  474. void beginPath();
  475. /**
  476. Starts new sub-path with specified point as first point.
  477. */
  478. void moveTo(float x, float y);
  479. /**
  480. Adds line segment from the last point in the path to the specified point.
  481. */
  482. void lineTo(float x, float y);
  483. /**
  484. Adds bezier segment from last point in the path via two control points to the specified point.
  485. */
  486. void bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y);
  487. /**
  488. Adds an arc segment at the corner defined by the last path point, and two specified points.
  489. */
  490. void arcTo(float x1, float y1, float x2, float y2, float radius);
  491. /**
  492. Closes current sub-path with a line segment.
  493. */
  494. void closePath();
  495. /**
  496. Sets the current sub-path winding.
  497. */
  498. void pathWinding(Winding dir);
  499. /**
  500. Creates new arc shaped sub-path.
  501. */
  502. void arc(float cx, float cy, float r, float a0, float a1, Winding dir);
  503. /**
  504. Creates new rectangle shaped sub-path.
  505. */
  506. void rect(float x, float y, float w, float h);
  507. /**
  508. Creates new rounded rectangle shaped sub-path.
  509. */
  510. void roundedRect(float x, float y, float w, float h, float r);
  511. /**
  512. Creates new ellipse shaped sub-path.
  513. */
  514. void ellipse(float cx, float cy, float rx, float ry);
  515. /**
  516. Creates new circle shaped sub-path.
  517. */
  518. void circle(float cx, float cy, float r);
  519. /**
  520. Fills the current path with current fill style.
  521. */
  522. void fill();
  523. /**
  524. Fills the current path with current stroke style.
  525. */
  526. void stroke();
  527. /* --------------------------------------------------------------------
  528. * Text */
  529. /**
  530. Creates font by loading it from the disk from specified file name.
  531. Returns handle to the font.
  532. */
  533. FontId createFont(const char* name, const char* filename);
  534. /**
  535. Creates font by loading it from the specified memory chunk.
  536. Returns handle to the font.
  537. */
  538. FontId createFontMem(const char* name, uchar* data, int ndata, bool freeData);
  539. /**
  540. Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
  541. */
  542. FontId findFont(const char* name);
  543. /**
  544. Sets the font size of current text style.
  545. */
  546. void fontSize(float size);
  547. /**
  548. Sets the blur of current text style.
  549. */
  550. void fontBlur(float blur);
  551. /**
  552. Sets the letter spacing of current text style.
  553. */
  554. void textLetterSpacing(float spacing);
  555. /**
  556. Sets the proportional line height of current text style. The line height is specified as multiple of font size.
  557. */
  558. void textLineHeight(float lineHeight);
  559. /**
  560. Sets the text align of current text style.
  561. */
  562. void textAlign(Align align);
  563. /**
  564. Sets the text align of current text style.
  565. Overloaded function for convenience.
  566. @see Align
  567. */
  568. void textAlign(int align);
  569. /**
  570. Sets the font face based on specified id of current text style.
  571. */
  572. void fontFaceId(FontId font);
  573. /**
  574. Sets the font face based on specified name of current text style.
  575. */
  576. void fontFace(const char* font);
  577. /**
  578. Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
  579. */
  580. float text(float x, float y, const char* string, const char* end);
  581. /**
  582. Draws multi-line text string at specified location wrapped at the specified width. If end is specified only the sub-string up to the end is drawn.
  583. White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
  584. Words longer than the max width are slit at nearest character (i.e. no hyphenation).
  585. */
  586. void textBox(float x, float y, float breakRowWidth, const char* string, const char* end);
  587. /**
  588. Measures the specified text string. The bounds value are [xmin,ymin, xmax,ymax].
  589. Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
  590. Measured values are returned in local coordinate space.
  591. */
  592. float textBounds(float x, float y, const char* string, const char* end, Rectangle<float>& bounds);
  593. /**
  594. Measures the specified multi-text string. Parameter bounds should be a pointer to float[4],
  595. if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
  596. Measured values are returned in local coordinate space.
  597. */
  598. void textBoxBounds(float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds);
  599. /**
  600. Calculates the glyph x positions of the specified text. If end is specified only the sub-string will be used.
  601. Measured values are returned in local coordinate space.
  602. */
  603. int textGlyphPositions(float x, float y, const char* string, const char* end, GlyphPosition* positions, int maxPositions);
  604. /**
  605. Returns the vertical metrics based on the current text style.
  606. Measured values are returned in local coordinate space.
  607. */
  608. void textMetrics(float* ascender, float* descender, float* lineh);
  609. /**
  610. Breaks the specified text into lines. If end is specified only the sub-string will be used.
  611. White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
  612. Words longer than the max width are slit at nearest character (i.e. no hyphenation).
  613. */
  614. int textBreakLines(const char* string, const char* end, float breakRowWidth, TextRow* rows, int maxRows);
  615. private:
  616. NVGcontext* const fContext;
  617. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoVG)
  618. };
  619. // -----------------------------------------------------------------------
  620. // NanoWidget
  621. /**
  622. NanoVG Widget class.
  623. This class implements the NanoVG drawing API inside a DGL Widget.
  624. The drawing function onDisplay() is implemented internally but a
  625. new onNanoDisplay() needs to be overridden instead.
  626. */
  627. class NanoWidget : public Widget,
  628. public NanoVG
  629. {
  630. public:
  631. /**
  632. Constructor.
  633. */
  634. NanoWidget(Window& parent)
  635. : Widget(parent),
  636. NanoVG()
  637. {
  638. setNeedsScaling(true);
  639. }
  640. protected:
  641. /**
  642. New virtual onDisplay function.
  643. @see onDisplay
  644. */
  645. virtual void onNanoDisplay() = 0;
  646. private:
  647. /**
  648. Widget display function.
  649. Implemented internally to wrap begin/endFrame() automaticaly.
  650. */
  651. void onDisplay() override
  652. {
  653. //glPushAttrib(GL_PIXEL_MODE_BIT|GL_STENCIL_BUFFER_BIT|GL_ENABLE_BIT);
  654. beginFrame(getWidth(), getHeight());
  655. onNanoDisplay();
  656. endFrame();
  657. //glPopAttrib();
  658. glDisable(GL_CULL_FACE);
  659. }
  660. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoWidget)
  661. };
  662. // -----------------------------------------------------------------------
  663. END_NAMESPACE_DGL
  664. #endif // DGL_NANO_WIDGET_HPP_INCLUDED