3 import arsd
.simpledisplay
;
5 version(arsd
) import arsd
.nanovega
;
6 else import iv
.nanovega
;
10 //version = test_flatten;
11 //version = test_path_outline;
14 // ////////////////////////////////////////////////////////////////////////// //
15 version(arsd
) public class NVGWindow
: SimpleWindow
{
17 NVGContext nvg
; // our NanoVega context
18 NVGContextFlag nvgInitFlags
= NVGContextFlag
.Default
; // bitwise or
20 void delegate (NVGWindow self
, NVGContext nvg
) onInitOpenGL
; // will be called after creating nvg
21 void delegate (NVGWindow self
, NVGContext nvg
) onDeinitOpenGL
; // will be called before destroying nvg
22 void delegate (NVGWindow self
, NVGContext nvg
) onRedraw
; // will be called in [redrawOpenGlScene]
25 void setupCallbacks () {
26 visibleForTheFirstTime
= delegate () {
28 nvg
= nvgCreateContext(cast(NVGContextFlag
)nvgInitFlags
);
29 if (nvg
is null) assert(0, "cannot initialize NanoVega");
30 if (onInitOpenGL
!is null) onInitOpenGL(this, nvg
);
33 redrawOpenGlScene
= delegate () {
34 if (width
< 1 || height
< 1 || closed
) return;
35 if (onRedraw
!is null && nvg
!is null) {
36 setAsCurrentOpenGlContext();
37 scope(exit
) releaseCurrentOpenGlContext();
38 glViewport(0, 0, width
, height
);
44 void onBeforeInitNVG () {} // you can set [nvgInitFlags] here
47 this (int width
=800, int height
=800, string title
=null, Resizability resizable
=Resizability
.allowResizing
, WindowTypes windowType
=WindowTypes
.normal
, int customizationFlags
=WindowFlags
.normal
, SimpleWindow parent
=null) {
49 super(width
, height
, title
, OpenGlOptions
.yes
, resizable
, windowType
, customizationFlags
, parent
);
52 this (Size size
, string title
=null, Resizability resizable
=Resizability
.allowResizing
) {
54 super(size
, title
, OpenGlOptions
.yes
, resizable
);
57 override void close () {
58 if (!closed
&& nvg
!is null) {
59 setAsCurrentOpenGlContext();
60 scope(exit
) { flushGui(); releaseCurrentOpenGlContext(); }
61 if (onDeinitOpenGL
!is null) onDeinitOpenGL(this, nvg
);
67 final void forceRedraw () {
68 if (!closed
&& visible
) redrawOpenGlSceneNow();
73 // ////////////////////////////////////////////////////////////////////////// //
75 NVGContext nvg
; // our NanoVega context
77 // we need at least OpenGL3 with GLSL to use NanoVega,
78 // so let's tell simpledisplay about that
79 setOpenGLContextVersion(3, 0);
81 // now create OpenGL window
82 sdpyWindowClass
= "NVG_RASTER_TEST";
83 auto sdmain
= new NVGWindow(800, 600, "NanoVega Simple Sample");
85 sdmain
.onInitOpenGL
= delegate (self
, nvg
) {
86 nvg
.createFont("verdana", "verdana");
91 // this callback will be called when we will need to repaint our window
92 sdmain
.onRedraw
= delegate (self
, nvg
) {
93 glClearColor(0, 0, 0, 0);
94 glClear(glNVGClearFlags
);
97 nvg
.beginFrame(self
.width
, self
.height
);
98 scope(exit
) nvg
.endFrame();
101 nvg
.fontFace
= "verdana";
103 nvg
.charPathBounds('A', bounds
[]);
106 nvg
.beginPath(); // start new path
108 nvg
.translate(100, bounds
[3]-bounds
[1]+100);
112 version(test_path_outline
) {
113 nvg
.resetTransform();
116 nvg
.quadTo(30, 60, 50, 80);
119 auto ol
= nvg
.getCurrPathOutline();
121 //nvg.strokeWidth = 1;
123 auto ol
= nvg
.charOutline('Q');
126 version(test_flatten
) {
127 writeln("curved commands: ", ol
.length
);
129 writeln("flattened commands: ", ol
.length
);
132 foreach (const ref cmd
; ol
.commands
) {
134 final switch (cmd
.code
) {
135 case cmd
.Kind
.MoveTo
:
136 version(test_path_outline
) writeln("MoveTo(", cmd
.args
[0], ", ", cmd
.args
[1], ")");
137 nvg
.moveTo(cmd
.args
[0], cmd
.args
[1]);
139 case cmd
.Kind
.LineTo
:
140 version(test_path_outline
) writeln("LineTo(", cmd
.args
[0], ", ", cmd
.args
[1], ")");
141 nvg
.lineTo(cmd
.args
[0], cmd
.args
[1]);
143 case cmd
.Kind
.QuadTo
:
144 version(test_path_outline
) writeln("QuadTo(", cmd
.args
[0], ", ", cmd
.args
[1], ", ", cmd
.args
[2], ", ", cmd
.args
[3], ")");
145 version(test_flatten
) {
148 nvg
.quadTo(cmd
.args
[0], cmd
.args
[1], cmd
.args
[2], cmd
.args
[3]);
151 case cmd
.Kind
.BezierTo
:
152 version(test_path_outline
) writeln("BezierTo(", cmd
.args
[0], ", ", cmd
.args
[1], ", ", cmd
.args
[2], ", ", cmd
.args
[3], ", ", cmd
.args
[4], ", ", cmd
.args
[5], ")");
153 version(test_flatten
) {
156 nvg
.bezierTo(cmd
.args
[0], cmd
.args
[1], cmd
.args
[2], cmd
.args
[3], cmd
.args
[4], cmd
.args
[5]);
159 case cmd
.Kind
.End
: break; // the thing that should not be
165 nvg
.fillColor
= NVGColor
.red
;
166 nvg
.strokeColor
= NVGColor
.red
;
168 nvg
.lineDash
= [8, 2, 4, 2];
169 nvg
.lineDashStart
= dashStart
;
174 sdmain
.eventLoop(0, // no pulse timer required
175 delegate (KeyEvent event
) {
176 if (event
== "*-Q" || event
== "Escape") { sdmain
.close(); return; } // quit on Q, Ctrl+Q, and so on
177 if (event
== "D-Space") { dashStart
+= 0.5; sdmain
.forceRedraw(); }
181 flushGui(); // let OS do it's cleanup