1 /*
2  *******************************************************************************************
3  * Dgame (a D game framework) - Copyright (c) Randy Schütt
4  * 
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from
7  * the use of this software.
8  * 
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  * 
13  * 1. The origin of this software must not be misrepresented; you must not claim
14  *    that you wrote the original software. If you use this software in a product,
15  *    an acknowledgment in the product documentation would be appreciated but is
16  *    not required.
17  * 
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 
21  * 3. This notice may not be removed or altered from any source distribution.
22  *******************************************************************************************
23  */
24 module Dgame.Window.Event;
25 
26 private import derelict.sdl2.types;
27 
28 public {
29 	import Dgame.System.Keyboard;
30 	import Dgame.System.Mouse;
31 }
32 
33 /**
34  * Specific Window Events.
35  */
36 enum WindowEventId : ubyte {
37 	None,           /** Nothing happens */
38 	Shown,          /** Window has been shown */
39 	Hidden,         /** Window has been hidden */
40 	Exposed,        /** Window has been exposed and should be redrawn */
41 	Moved,          /** Window has been moved to data1, data2  */
42 	Resized,        /** Window has been resized to data1Xdata2 */
43 	SizeChanged,    /** The window size has changed, 
44 	                 * either as a result of an API call or through 
45 	                 * the system or user changing the window size. */
46 	Minimized,      /** Window has been minimized. */
47 	Maximized,      /** Window has been maximized. */
48 	Restored,       /** Window has been restored to normal size and position. */
49 	Enter,          /** Window has gained mouse focus. */
50 	Leave,          /** Window has lost mouse focus. */
51 	FocusGained,    /** Window has gained keyboard focus. */
52 	FocusLost,      /** Window has lost keyboard focus. */
53 	Close           /** The window manager requests that the window be closed. */
54 }
55 
56 enum TextSize = 32;
57 
58 /**
59  * The Event structure.
60  * Event defines a system event and it's parameters
61  *
62  * Author: rschuett
63  */
64 struct Event {
65 	/**
66 	 * All supported Event Types.
67 	 */
68 	enum Type {
69 		Quit = SDL_QUIT,			/** Quit Event. Time to close the window. */
70 		Window  = SDL_WINDOWEVENT,	/** Something happens with the window. */
71 		KeyDown = SDL_KEYDOWN,		/** A key is pressed. */
72 		KeyUp = SDL_KEYUP,		/** A key is released. */
73 		MouseMotion = SDL_MOUSEMOTION,	/** The mouse has moved. */
74 		MouseButtonDown = SDL_MOUSEBUTTONDOWN,	/** A mouse button is pressed. */
75 		MouseButtonUp = SDL_MOUSEBUTTONUP,	/** A mouse button is released. */
76 		MouseWheel = SDL_MOUSEWHEEL,		/** The mouse wheel has scolled. */
77 		TextEdit   = SDL_TEXTEDITING,            /**< Keyboard text editing (composition) */
78 		TextInput  = SDL_TEXTINPUT              /**< Keyboard text input */
79 	}
80 	
81 	Type type; /** The Event Type. */
82 	
83 	uint timestamp; /** Milliseconds since the app is running. */
84 	uint windowId;   /** The window which has raised this event. */
85 	
86 	/**
87 	 * The Keyboard Event structure.
88 	 */
89 	static struct KeyboardEvent {
90 		Keyboard.State state;	/** Keyboard State. See: Dgame.Input.Keyboard. */
91 		Keyboard.Code code;	/** The Key which is released or pressed. */
92 		Keyboard.ScanCode scancode;	/** The Key which is released or pressed. */
93 		Keyboard.Mod mod;	/** The Key modifier. */
94 		
95 		alias key = code; /** An alias */
96 		
97 		bool repeat;	/** true, if this is a key repeat. */
98 	}
99 	
100 	/**
101 	 * Keyboard text editing event structure
102 	 */
103 	static struct TextEditEvent {
104 		char[TextSize] text = void; /**< The editing text */
105 		short start; /**< The start cursor of selected editing text */
106 		ushort length; /**< The length of selected editing text */
107 	}
108 	
109 	/**
110 	 * Keyboard text input event structure
111 	 */
112 	static struct TextInputEvent {
113 		char[TextSize] text = void; /**< The input text */
114 	}
115 	
116 	/**
117 	 * The Window Event structure.
118 	 */
119 	static struct WindowEvent {
120 		WindowEventId eventId; /** < The Window Event id. */
121 	}
122 	
123 	/**
124 	 * The Mouse button Event structure.
125 	 */
126 	static struct MouseButtonEvent {
127 		Mouse.Button button; /** The mouse button which is pressed or released. */
128 		
129 		short x; /** Current x position. */
130 		short y; /** Current y position. */
131 	}
132 	
133 	/**
134 	 * The Mouse motion Event structure.
135 	 */
136 	static struct MouseMotionEvent {
137 		Mouse.State state; /** Mouse State. See: Dgame.Input.Mouse. */
138 		
139 		short x; /** Current x position. */
140 		short y; /** Current y position. */
141 		
142 		short rel_x; /** Relative motion in the x direction. */
143 		short rel_y; /** Relative motion in the y direction. */
144 	}
145 	
146 	/**
147 	 * The Mouse wheel Event structure.
148 	 */
149 	static struct MouseWheelEvent {
150 		short x; /** Current x position. */
151 		short y; /** Current y position. */
152 		
153 		short delta_x; /** The amount scrolled horizontally. */
154 		short delta_y; /** The amount scrolled vertically. */
155 	}
156 	
157 	union {
158 		KeyboardEvent keyboard; /** Keyboard Event. */
159 		WindowEvent	  window;	/** Window Event. */
160 		MouseButtonEvent mouseButton; /** Mouse button Event. */
161 		MouseMotionEvent mouseMotion; /** Mouse motion Event. */
162 		MouseWheelEvent  mouseWheel;  /** Mouse wheel Event. */
163 		TextEditEvent	 textEdit;	  /** Text edit Event. */
164 		TextInputEvent	 textInput;	  /** Text input Event. */
165 	}
166 }