1 module polyplex.core.input.mouse;
2 import polyplex.core.events;
3 import derelict.sdl2.sdl;
4 import polyplex.math;
5 
6 import std.stdio;
7 
8 enum MouseButton {
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 	/**
31 		Returns true if the specified MouseButton is pressed.
32 	*/
33 	public bool IsButtonPressed(MouseButton button) {
34 		if (btn_mask & SDL_BUTTON(button)) return true;
35 		return false;
36 	}
37 
38 	/**
39 		Returns true if the specified MouseButton is released (not pressed).
40 	*/
41 	public bool IsButtonReleased(MouseButton button) {
42 		return !IsButtonPressed(button);
43 	}
44 
45 	/**
46 		Returns the position and scroll for the mouse.
47 		Z = Scrollwheel
48 	*/
49 	public Vector3 Position() {
50 		return pos;
51 	}
52 }
53 
54 public class Mouse {
55 
56 	/**
57 		Returns the current state of the mouse.
58 	*/
59 	public static MouseState GetState() {
60 		int x;
61 		int y;
62 		int mask = SDL_GetMouseState(&x, &y);
63 		float scroll = 0;
64 		foreach(SDL_Event ev; PPEvents.Events) {
65 			if (ev.type == SDL_MOUSEWHEEL) {
66 				scroll = ev.wheel.y;
67 			}
68 		}
69 		return new MouseState(x, y, mask, scroll);
70 	}
71 
72 	/**
73 		Gets the current state of the mouse.
74 	*/
75 	public static Vector2 Position() {
76 		int x;
77 		int y;
78 		SDL_GetMouseState(&x, &y);
79 		return Vector2(x, y);
80 	}
81 }