1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module g4d.glfw.cursor;
8 import g4d.glfw.lib;
9 import std.string;
10 
11 /// A type of cursor shape.
12 enum CursorShape
13 {
14     Arrow     = GLFW_ARROW_CURSOR,
15     IBeam     = GLFW_IBEAM_CURSOR,
16     Crosshair = GLFW_CROSSHAIR_CURSOR,
17     Hand      = GLFW_HAND_CURSOR,
18     HResize   = GLFW_HRESIZE_CURSOR,
19     VResize   = GLFW_VRESIZE_CURSOR,
20 }
21 
22 private template StandardCursors ()
23 {
24     static foreach ( s; __traits(allMembers,CursorShape) )
25     {
26         mixin( q{
27             protected static Cursor _$name$;
28             static @property const(Cursor) $name$ () { return _$name$; }
29         }.replace( "$name$", s ) );
30     }
31 
32     static void createStandardCursors ()
33     {
34         static foreach ( s; __traits(allMembers,CursorShape) )
35         {
36             mixin( q{
37                 _$name$ = new Cursor( CursorShape.$name$ );
38             }.replace( "$name$", s ) );
39         }
40     }
41 }
42 
43 /// An object of cursor image.
44 /// You can access standard cursor without creating instance.
45 /// Examples: setCursor( Cursor.IBeam );
46 class Cursor
47 {
48     mixin StandardCursors;
49 
50     protected GLFWcursor* _ptr;
51     /// Pointer to glfw cursor.
52     const @property ptr () { return _ptr; }
53 
54     ///
55     this ( CursorShape shape )
56     {
57         _ptr = enforce!glfwCreateStandardCursor( shape );
58     }
59 
60     ///
61     ~this ()
62     {
63         dispose();
64     }
65     /// Checks if the cursor is disposed.
66     const @property disposed ()
67     {
68         return !_ptr;
69     }
70     /// Deletes glfw cursor.
71     void dispose ()
72     {
73         if ( !disposed ) {
74             enforce!glfwDestroyCursor( _ptr );
75         }
76         _ptr = null;
77     }
78 }