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