1 /++
2 	An [arsd.minigui] widget that can embed [arsd.nanovega].
3 
4 	History:
5 		Added February 7, 2020 (version 9.2)
6 +/
7 module arsd.minigui_addons.nanovega;
8 
9 import arsd.minigui;
10 /// Since the nvg context uses UFCS, you probably want this anyway.
11 public import arsd.nanovega;
12 
13 static if(OpenGlEnabled)
14 /++
15 	The NanoVegaWidget has a class you can use with [arsd.nanovega].
16 
17 	History:
18 		Included in initial release on February 7, 2020 (dub package version 9.2).
19 +/
20 class NanoVegaWidget : OpenGlWidget {
21 	NVGContext nvg;
22 
23 	this(Widget parent) {
24 		super(parent);
25 
26 		win.onClosing = delegate() {
27 			nvg.kill();
28 		};
29 
30 		win.visibleForTheFirstTime = delegate() {
31 			nvg = nvgCreateContext();
32 			if(nvg is null) throw new Exception("cannot initialize NanoVega");
33 		};
34 
35 		win.redrawOpenGlScene = delegate() {
36 			if(redrawNVGScene is null)
37 				return;
38 			glViewport(0, 0, this.width, this.height);
39 			if(clearOnEachFrame) {
40 				glClearColor(0, 0, 0, 0);
41 				glClear(glNVGClearFlags);
42 			}
43 
44 			nvg.beginFrame(this.width, this.height);
45 			scope(exit) nvg.endFrame();
46 
47 			redrawNVGScene(nvg);
48 		};
49 	}
50 	/// Set this to draw your nanovega frame.
51 	void delegate(NVGContext nvg) redrawNVGScene;
52 
53 	/// If true, it automatically clears the widget canvas between each redraw call.
54 	bool clearOnEachFrame = true;
55 }
56 
57 /// Nanovega requires at least OpenGL 3.0, so this sets that requirement. You can override it later still, of course.
58 shared static this() {
59 	setOpenGLContextVersion(3, 0);
60 }
61