simple_vg.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef SIMPLE_VG_H
  2. #define SIMPLE_VG_H
  3. #include <memory>
  4. #include "nanovg_full.h"
  5. #include "simple/support/enum_flags_operators.hpp"
  6. #include "simple/geom/vector.hpp"
  7. #include "simple/graphical/color_vector.hpp"
  8. #include "simple/graphical/common_def.h"
  9. namespace simple::vg
  10. {
  11. using float2 = geom::vector<float, 2>;
  12. using float2x2 = geom::vector<geom::vector<float,2>, 2>;
  13. using int2 = geom::vector<int, 2>;
  14. using graphical::rgb_vector;
  15. using graphical::rgba_vector;
  16. using graphical::rgb_pixel;
  17. using graphical::rgba_pixel;
  18. using range2f = support::range<float2>;
  19. using rangef = support::range<float>;
  20. using rect2f = geom::segment<float2>;
  21. using anchored_rect2f = geom::anchored_segment<float2>;
  22. class frame;
  23. class framebuffer;
  24. class sketch;
  25. class paint
  26. {
  27. NVGpaint raw;
  28. paint(NVGpaint) noexcept;
  29. public:
  30. paint() = delete;
  31. static paint radial_gradient(float2 center, rangef radius, support::range<rgba_vector>) noexcept;
  32. static paint radial_gradient(range2f range, rangef radius, support::range<rgba_vector>) noexcept;
  33. static paint linear_gradient() noexcept; // TODO
  34. static paint range_gradient() noexcept; // TODO
  35. friend class sketch;
  36. friend class framebuffer;
  37. };
  38. class canvas
  39. {
  40. public:
  41. enum class flags
  42. {
  43. nothing = 0,
  44. antialias = NVG_ANTIALIAS,
  45. stencil_strokes = NVG_STENCIL_STROKES,
  46. debug = NVG_DEBUG
  47. };
  48. canvas(flags = flags::nothing) noexcept;
  49. canvas& clear(const rgba_vector& color = rgba_vector::white()) noexcept;
  50. canvas& clear(const rgba_pixel& color) noexcept;
  51. frame begin_frame(float2 size, float pixelRatio = 1) noexcept;
  52. frame begin_frame(framebuffer&) const noexcept;
  53. private:
  54. struct deleter
  55. {
  56. void operator()(NVGcontext* context) const noexcept;
  57. };
  58. std::unique_ptr<NVGcontext, deleter> raw;
  59. friend class framebuffer;
  60. };
  61. using ::operator |;
  62. using ::operator &;
  63. using ::operator &&;
  64. class framebuffer
  65. {
  66. public:
  67. enum class flags :
  68. std::underlying_type_t<NVGimageFlags>
  69. {
  70. none = 0,
  71. mipmap = NVG_IMAGE_GENERATE_MIPMAPS,
  72. repeat_x = NVG_IMAGE_REPEATX,
  73. repeat_y = NVG_IMAGE_REPEATY,
  74. flip_y = NVG_IMAGE_FLIPY,
  75. premultiplied = NVG_IMAGE_PREMULTIPLIED,
  76. nearest = NVG_IMAGE_NEAREST
  77. };
  78. const flags flags;
  79. const int2 size;
  80. framebuffer(int2 size, enum flags = flags::none) noexcept;
  81. framebuffer(const canvas&, int2 size, enum flags = flags::none);
  82. bool create(const canvas&);
  83. vg::paint paint(support::range<int2>, float opacity = 1, float angle = 0) const;
  84. vg::paint paint(float opacity = 1, float angle = 0) const;
  85. private:
  86. struct deleter
  87. {
  88. void operator()(NVGLUframebuffer*) const noexcept;
  89. };
  90. std::unique_ptr<NVGLUframebuffer, deleter> raw;
  91. friend class frame;
  92. };
  93. // TODO: prevent two frames with same context from coexisting
  94. class frame
  95. {
  96. public:
  97. sketch begin_sketch() noexcept;
  98. ~frame() noexcept;
  99. frame(const frame&) = delete;
  100. frame(frame&&) noexcept;
  101. const float2 size;
  102. const float pixelRatio;
  103. const framebuffer * const buffer;
  104. private:
  105. NVGcontext* context;
  106. frame(NVGcontext*, float2 size, float pixelRatio = 1) noexcept;
  107. frame(NVGcontext*, const framebuffer&) noexcept;
  108. friend class canvas;
  109. };
  110. class sketch
  111. {
  112. public:
  113. enum class cap
  114. {
  115. butt = NVG_BUTT,
  116. round = NVG_ROUND,
  117. square = NVG_SQUARE
  118. };
  119. enum class join
  120. {
  121. miter = NVG_MITER,
  122. round = NVG_ROUND,
  123. bevel = NVG_BEVEL
  124. };
  125. sketch& ellipse(const range2f&) noexcept;
  126. sketch& ellipse(float2 center, float2x2 trasnform, unsigned vertex_count = 100) noexcept;
  127. sketch& rectangle(const range2f&) noexcept;
  128. sketch& line(float2 from, float2 to) noexcept;
  129. sketch& arc(float2 center, rangef angle, float radius) noexcept;
  130. sketch& arc(float2 center, range2f sector, float edge = 5) noexcept;
  131. sketch& move(float2) noexcept;
  132. sketch& vertex(float2) noexcept;
  133. sketch& bezier(float2, float2, float2) noexcept;
  134. sketch& bezier(float2, float2) noexcept;
  135. sketch& scale(float2) noexcept;
  136. sketch& reset_matrix() noexcept;
  137. sketch& fill(const paint&) noexcept;
  138. sketch& fill(const rgba_vector&) noexcept;
  139. sketch& fill(const rgba_pixel&) noexcept;
  140. sketch& fill() noexcept;
  141. sketch& line_cap(cap) noexcept;
  142. sketch& line_join(join) noexcept;
  143. sketch& line_width(float) noexcept;
  144. sketch& miter_limit(float) noexcept;
  145. sketch& outline(const paint&) noexcept;
  146. sketch& outline(const rgba_vector&) noexcept;
  147. sketch& outline(const rgba_pixel&) noexcept;
  148. sketch& outline() noexcept;
  149. sketch(const sketch&) = delete;
  150. sketch(sketch&&) noexcept;
  151. ~sketch() noexcept;
  152. private:
  153. NVGcontext* context;
  154. sketch(NVGcontext*) noexcept;
  155. friend class frame;
  156. };
  157. } // namespace simple::vg
  158. template<> struct simple::support::define_enum_flags_operators<simple::vg::canvas::flags>
  159. : std::true_type {};
  160. template<> struct simple::support::define_enum_flags_operators<enum simple::vg::framebuffer::flags>
  161. : std::true_type {};
  162. #endif /* end of include guard */