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 win.setAsCurrentOpenGlContext(); 32 nvg = nvgCreateContext(); 33 if(nvg is null) throw new Exception("cannot initialize NanoVega"); 34 }; 35 36 win.redrawOpenGlScene = delegate() { 37 if(redrawNVGScene is null) 38 return; 39 glViewport(0, 0, this.width, this.height); 40 if(clearOnEachFrame) { 41 glClearColor(0, 0, 0, 0); 42 glClear(glNVGClearFlags); 43 } 44 45 nvg.beginFrame(this.width, this.height); 46 scope(exit) nvg.endFrame(); 47 48 redrawNVGScene(nvg); 49 }; 50 } 51 /// Set this to draw your nanovega frame. 52 void delegate(NVGContext nvg) redrawNVGScene; 53 54 /// If true, it automatically clears the widget canvas between each redraw call. 55 bool clearOnEachFrame = true; 56 } 57 58 /// Nanovega requires at least OpenGL 3.0, so this sets that requirement. You can override it later still, of course. 59 shared static this() { 60 setOpenGLContextVersion(3, 0); 61 } 62