DISTRHO Plugin Framework
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.

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