1 ///
2 module nanogui.common;
3 
4 import gfm.math : vec2i, vec2f, vec3f, vec4f, vec4i;
5 import arsd.nanovega : NVGContext;
6 public import arsd.nanovega;
7 
8 struct NanoContext
9 {
10 	import std.typecons : Rebindable;
11 	import nanogui.theme : Theme;
12 
13 	NVGContext nvg;
14 	alias nvg this;
15 
16 	@disable this();
17 
18 	this(NVGContextFlag flag)
19 	{
20 		nvg = nvgCreateContext(flag);
21 		algn.left = true;
22 		algn.top = true;
23 	}
24 
25 	NVGTextAlign algn;
26 	Vector2i position;
27 	Vector2i mouse;
28 	Rebindable!(const Theme) theme;
29 	// current width or height (other dimension is passed to drawing function explicitely)
30 	float current_size;
31 	// nesting level of current item
32 	int tree_view_nesting_level;
33 }
34 
35 alias Vector2i = vec2i;
36 alias Vector2f = vec2f;
37 alias Vector3f = vec3f;
38 alias Vector4i = vec4i;
39 alias Color = vec4f;
40 
41 enum MouseButton : int
42 {
43 	None      =  0,
44 	Left      =  1,
45 	Right     =  2,
46 	Middle    =  4,
47 	WheelUp   =  8,
48 	WheelDown = 16, 
49 }
50 
51 enum MouseAction : int
52 {
53 	Press   = 0,
54 	Release = 1,
55 	Motion  = 2,
56 }
57 
58 enum KeyAction : int
59 {
60 	Press   = 0,
61 	Release = 1,
62 	Repeat  = 2,
63 }
64 
65 enum KeyMod : int
66 {
67 	Shift = 1,
68 	Alt   = 2,
69 	Ctrl  = 4,
70 }
71 
72 enum Key : int
73 {
74 	Left,
75 	Right,
76 	Up,
77 	Down,
78 	Home,
79 	End,
80 	Backspace,
81 	Delete,
82 	Enter,
83 	Shift,
84 	System,
85 	A,
86 	X,
87 	C,
88 	V,
89 	Esc,
90 }
91 
92 /// Cursor shapes available to use in nanogui.  Shape of actual cursor determined by Operating System.
93 enum Cursor {
94 	Arrow = 0,  /// The arrow cursor.
95 	IBeam,      /// The I-beam cursor.
96 	Crosshair,  /// The crosshair cursor.
97 	Hand,       /// The hand cursor.
98 	HResize,    /// The horizontal resize cursor.
99 	VResize,    /// The vertical resize cursor.
100 }
101 
102 /// Sets current fill style to a solid color.
103 /// Group: render_styles
104 public void fillColor (NanoContext ctx, Color color) nothrow @trusted @nogc {
105   NVGColor clr = void;
106   clr.rgba = color[];
107   clr.rgba[] /= 255f;
108   arsd.nanovega.fillColor(ctx, clr);
109 }
110 
111 /** Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
112  * drop shadows or highlights for boxes. Parameters (x, y) define the top-left corner of the rectangle,
113  * (w, h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
114  * the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
115  * The gradient is transformed by the current transform when it is passed to [fillPaint] or [strokePaint].
116  *
117  * Group: paints
118  */
119 public NVGPaint boxGradient (NanoContext ctx, in float x, in float y, in float w, in float h, in float r, in float f, Color icol, Color ocol) nothrow @trusted @nogc
120 {
121 	NVGColor clr1 = void, clr2 = void;
122 	clr1.rgba = icol[];
123 	clr1.rgba[] /= 255f;
124 	clr2.rgba = ocol[];
125 	clr2.rgba[] /= 255f;
126 	return arsd.nanovega.boxGradient(ctx, x, y, w, h, r, f, clr1, clr2);
127 }
128 
129 /** Creates and returns a linear gradient. Parameters `(sx, sy) (ex, ey)` specify the start and end coordinates
130  * of the linear gradient, icol specifies the start color and ocol the end color.
131  * The gradient is transformed by the current transform when it is passed to [fillPaint] or [strokePaint].
132  *
133  * Group: paints
134  */
135 public NVGPaint linearGradient (NanoContext ctx, in float sx, in float sy, in float ex, in float ey, Color icol, Color ocol) nothrow @trusted @nogc
136 {
137 	NVGColor clr1 = void, clr2 = void;
138 	clr1.rgba = icol[];
139 	clr1.rgba[] /= 255f;
140 	clr2.rgba = ocol[];
141 	clr2.rgba[] /= 255f;
142 	return arsd.nanovega.linearGradient(ctx, sx, sy, ex, ey, clr1, clr2);
143 }
144 
145 /** Creates and returns a radial gradient. Parameters (cx, cy) specify the center, inr and outr specify
146  * the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
147  * The gradient is transformed by the current transform when it is passed to [fillPaint] or [strokePaint].
148  *
149  * Group: paints
150  */
151 public NVGPaint radialGradient (NanoContext ctx, in float cx, in float cy, in float inr, in float outr, Color icol, Color ocol) nothrow @trusted @nogc {
152 	NVGColor clr1 = void, clr2 = void;
153 	clr1.rgba = icol[];
154 	clr1.rgba[] /= 255f;
155 	clr2.rgba = ocol[];
156 	clr2.rgba[] /= 255f;
157 	return arsd.nanovega.radialGradient(ctx, cx, cy, inr, outr, clr1, clr2);
158 }
159 
160 /// Sets current stroke style to a solid color.
161 /// Group: render_styles
162 public void strokeColor (NanoContext ctx, Color color) nothrow @trusted @nogc
163 {
164 	NVGColor clr = void;
165 	clr.rgba = color[];
166 	clr.rgba[] /= 255f;
167 	arsd.nanovega.strokeColor(ctx, clr);
168 }