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 	/**
61 		Returns the current state of the mouse.
62 	*/
63 	public static MouseState GetState() {
64 		int x;
65 		int y;
66 		int mask = SDL_GetMouseState(&x, &y);
67 		float scroll = 0;
68 		foreach(SDL_Event ev; PPEvents.Events) {
69 			if (ev.type == SDL_MOUSEWHEEL) {
70 				scroll = ev.wheel.y;
71 			}
72 		}
73 		return new MouseState(x, y, mask, scroll);
74 	}
75 
76 	/**
77 		Gets the current state of the mouse.
78 	*/
79 	public static Vector2 Position() {
80 		int x;
81 		int y;
82 		SDL_GetMouseState(&x, &y);
83 		return Vector2(x, y);
84 	}
85 }