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