1 module polyplex.core.input.mouse;
2 import polyplex.core.events;
3 import bindbc.sdl;
4 import polyplex.math;
5 
6 import std.stdio;
7 
8 enum MouseButton : ubyte {
9 
10 	//Left Mouse Button
11 	Left = SDL_BUTTON_LEFT,
12 
13 	//Middle Mouse Button
14 	Middle = SDL_BUTTON_MIDDLE,
15 
16 	//Right Mouse Button
17 	Right = SDL_BUTTON_RIGHT
18 }
19 
20 public class MouseState {
21 	private Vector3 pos;
22 	private float scroll;
23 	private int btn_mask;
24 
25 	this(int x, int y, int btn_mask, float scrolled) {
26 		this.pos = Vector3(x, y, scrolled);
27 		this.btn_mask = btn_mask;
28 	}
29 
30 	ubyte SDLButton(ubyte x) {
31 		return cast(ubyte)(1 << (x-1));
32 	}
33 
34 	/**
35 		Returns true if the specified MouseButton is pressed.
36 	*/
37 	public bool IsButtonPressed(MouseButton button) {
38 		if (btn_mask & SDLButton(button)) return true;
39 		return false;
40 	}
41 
42 	/**
43 		Returns true if the specified MouseButton is released (not pressed).
44 	*/
45 	public bool IsButtonReleased(MouseButton button) {
46 		return !IsButtonPressed(button);
47 	}
48 
49 	/**
50 		Returns the position and scroll for the mouse.
51 		Z = Scrollwheel
52 	*/
53 	public Vector3 Position() {
54 		return pos;
55 	}
56 }
57 
58 public class Mouse {
59 
60 	private static int lastX = 0;
61 	private static int lastY = 0;
62 
63 	/**
64 		Returns the current state of the mouse.
65 	*/
66 	public static MouseState GetState() {
67 		
68 		// Cursed allocation
69 		int* x = new int;
70 		int* y = new int;
71 		immutable(int) mask = SDL_GetMouseState(x, y);
72 
73 		// Get last state if we're null
74 		if (x is null) x = &lastX;
75 		if (y is null) y = &lastY;
76 
77 		// Update last state to newest state
78 		lastX = *x;
79 		lastY = *y;
80 
81 		float scroll = 0;
82 		foreach(SDL_Event ev; PPEvents.Events) {
83 			if (ev.type == SDL_MOUSEWHEEL) {
84 				scroll = ev.wheel.y;
85 			}
86 		}
87 		return new MouseState(*x, *y, mask, scroll);
88 	}
89 
90 	/**
91 		Gets the current state of the mouse.
92 	*/
93 	public static Vector2 Position() {
94 		
95 		// Cursed allocation
96 		int* x = new int;
97 		int* y = new int;
98 		SDL_GetMouseState(x, y);
99 
100 		// Get last state if we're null
101 		if (x is null) x = &lastX;
102 		if (y is null) y = &lastY;
103 
104 		// Update last state to newest state
105 		lastX = *x;
106 		lastY = *y;
107 
108 		return Vector2(*x, *y);
109 	}
110 }