1 /**
2  * System cursor.
3  * Macros:
4  *     CURSOR_IMG = <img src="https://tronche.com/gui/x/xlib/appendix/b/$1" style="max-width: 16px; max-height: 16px; display: block; margin: auto">
5  */
6 
7 module rpui.cursor;
8 
9 import derelict.sdl2.sdl;
10 
11 enum CursorIcon {
12     none = -1,
13     inherit = -2,
14     hand = SDL_SYSTEM_CURSOR_HAND,  /// $(CURSOR_IMG 58.gif)
15     normal = SDL_SYSTEM_CURSOR_ARROW,  /// $(CURSOR_IMG 68.gif)
16     iBeam = SDL_SYSTEM_CURSOR_IBEAM,  /// $(CURSOR_IMG 152.gif)
17     vDoubleArrow = SDL_SYSTEM_CURSOR_SIZENS,  /// $(CURSOR_IMG 116.gif)
18     hDoubleArrow  = SDL_SYSTEM_CURSOR_SIZEWE,  /// $(CURSOR_IMG 108.gif)
19     crossHair = SDL_SYSTEM_CURSOR_CROSSHAIR,  /// $(CURSOR_IMG 34.gif)
20     // drag = XC_fleur,  /// $(CURSOR_IMG 52.gif)
21     // topSide = XC_top_side,  /// $(CURSOR_IMG 138.gif)
22     // bottomSide = XC_bottom_side,  /// $(CURSOR_IMG 16.gif)
23     // leftSide = XC_left_side,  /// $(CURSOR_IMG 70.gif)
24     // rightSide = XC_right_side,  /// $(CURSOR_IMG 96.gif)
25     // topLeftCorner = XC_top_left_corner,  /// $(CURSOR_IMG 134.gif)
26     // topRightCorner = XC_top_right_corner,  /// $(CURSOR_IMG 136.gif)
27     // bottomLeftCorner = XC_bottom_left_corner,  /// $(CURSOR_IMG 12.gif)
28     // bottomRightCorner = XC_bottom_right_corner  /// $(CURSOR_IMG 14.gif)
29 }
30 
31 /// System cursor.
32 final class CursorManager {
33     private SDL_Cursor* cursor = null;
34 
35     ~this() {
36         if (cursor !is null) {
37             SDL_FreeCursor(cursor);
38         }
39     }
40 
41     private CursorIcon icon = CursorIcon.normal;
42 
43     void setIcon(in CursorIcon newIcon) {
44         if (icon == newIcon || newIcon == CursorIcon.none)
45             return;
46 
47         icon = newIcon;
48         CursorIcon sdlIcon;
49 
50         if (newIcon == CursorIcon.inherit) {
51             sdlIcon = CursorIcon.normal;
52         } else {
53             sdlIcon = newIcon;
54         }
55 
56         if (cursor !is null) {
57             SDL_FreeCursor(cursor);
58         }
59 
60         cursor = SDL_CreateSystemCursor(cast(SDL_SystemCursor) sdlIcon);
61         SDL_SetCursor(cursor);
62     }
63 }