egra: agg will respect clip rect now
[iv.d.git] / nanovega / util.d
blobc32e107d4fe66cb1dbec093a08966192a958dcea
1 /* Invisible Vector Library
2 * ported by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module iv.nanovega.util;
18 private:
20 public import arsd.simpledisplay;
21 public import iv.nanovega.nanovega;
24 // ////////////////////////////////////////////////////////////////////////// //
25 public class NVGWindow : SimpleWindow {
26 public:
27 NVGContext nvg; // our NanoVega context
28 NVGContextFlag nvgInitFlags = NVGContextFlag.Default; // bitwise or
30 void delegate (NVGWindow self, NVGContext nvg) onInitOpenGL; // will be called after creating nvg
31 void delegate (NVGWindow self, NVGContext nvg) onDeinitOpenGL; // will be called before destroying nvg
32 void delegate (NVGWindow self, NVGContext nvg) onRedraw; // will be called in [redrawOpenGlScene]
34 protected:
35 void setupCallbacks () {
36 visibleForTheFirstTime = delegate () {
37 onBeforeInitNVG();
38 nvg = nvgCreateContext(cast(NVGContextFlag)nvgInitFlags);
39 if (nvg is null) assert(0, "cannot initialize NanoVega");
40 if (onInitOpenGL !is null) onInitOpenGL(this, nvg);
41 flushGui();
43 redrawOpenGlScene = delegate () {
44 if (width < 1 || height < 1 || closed) return;
45 if (onRedraw !is null && nvg !is null) {
46 setAsCurrentOpenGlContext();
47 scope(exit) releaseCurrentOpenGlContext();
48 glViewport(0, 0, width, height);
49 onRedraw(this, nvg);
54 void onBeforeInitNVG () {} // you can set [nvgInitFlags] here
56 public:
57 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) {
58 setupCallbacks();
59 super(width, height, title, OpenGlOptions.yes, resizable, windowType, customizationFlags, parent);
62 this (Size size, string title=null, Resizability resizable=Resizability.allowResizing) {
63 setupCallbacks();
64 super(size, title, OpenGlOptions.yes, resizable);
67 override void close () {
68 if (!closed && nvg !is null) {
69 setAsCurrentOpenGlContext();
70 scope(exit) { flushGui(); releaseCurrentOpenGlContext(); }
71 if (onDeinitOpenGL !is null) onDeinitOpenGL(this, nvg);
72 nvg.kill();
74 super.close();
77 final void forceRedraw () {
78 if (!closed && visible) redrawOpenGlSceneNow();