File: window.h

package info (click to toggle)
megaglest 3.13.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,416 kB
  • sloc: cpp: 144,271; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 22
file content (249 lines) | stat: -rw-r--r-- 7,447 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// ==============================================================
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2005 Matthias Braun <matze@braunis.de>
//
//	You can redistribute this code and/or modify it under
//	the terms of the GNU General Public License as published
//	by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version
// ==============================================================

#ifndef _SHARED_PLATFORM_WINDOW_H_
#define _SHARED_PLATFORM_WINDOW_H_

#include <map>
#include <string>
#include <SDL.h>
#include <cassert>
#include "data_types.h"
#include "vec.h"
#include <vector>
#include "leak_dumper.h"

using std::map;
using std::vector;
using std::string;
using Shared::Graphics::Vec2i;

#if SDL_VERSION_ATLEAST(2,0,0)

typedef SDL_Keysym SDL_keysym;

#endif

namespace Shared{ namespace Platform{

class Timer;
//class PlatformContextGl;

enum MouseButton {
	mbUnknown,
	mbLeft,
	mbCenter,
	mbRight,
	mbButtonX1,
	mbButtonX2,

	mbCount
};

enum SizeState{
	ssMaximized,
	ssMinimized,
	ssRestored
};

class MouseState {
private:
	bool states[mbCount];


public:
	MouseState() {
		clear();
	}
	//MouseState(const MouseState &);
	//MouseState &operator=(const MouseState &);
	void clear() { memset(this, 0, sizeof(MouseState)); }

	bool get(MouseButton b) const {
		if(b > 0 && b < mbCount) {
			return states[b];
		}
		return false;
	}

	void set(MouseButton b, bool state) {
		if(b > 0 && b < mbCount) {
			states[b] = state;
		}
	}
};

enum WindowStyle{
	wsFullscreen,
	wsWindowedFixed,
	wsWindowedResizable
};

// =====================================================
//	class Window
// =====================================================

class Window {
private:
	static SDL_Window *sdlWindow;
	Uint32 lastMouseDown[mbCount];
	int lastMouseX[mbCount];
	int lastMouseY[mbCount];

    static int64 lastMouseEvent;	/** for use in mouse hover calculations */
    static MouseState mouseState;
    static Vec2i mousePos;
    static bool isKeyPressedDown;
	static bool isFullScreen;
	static SDL_keysym keystate;
	static bool tryVSynch;
	static int64 lastToggle ;

	static void setLastToggle(int64 lastToggle)	{Window::lastToggle = lastToggle;}
	static int64 getLastToggle() 				    {return Window::lastToggle;}

    static void setLastMouseEvent(int64 lastMouseEvent)	{Window::lastMouseEvent = lastMouseEvent;}
    static int64 getLastMouseEvent() 				    {return Window::lastMouseEvent;}

    static const MouseState &getMouseState() 				    {return Window::mouseState;}
    static void setMouseState(MouseButton b, bool state)		{Window::mouseState.set(b, state);}

    static const Vec2i &getMousePos() 					        {return Window::mousePos;}
    static void setMousePos(const Vec2i &mousePos)				{Window::mousePos = mousePos;}

    static void setKeystate(SDL_keysym state)					{ keystate = state; }

    //static bool masterserverMode;
    static map<wchar_t,bool> mapAllowedKeys;

protected:
	//int w, h;
	static bool isActive;
	static bool allowAltEnterFullscreenToggle;
	static int lastShowMouseState;

public:
	static SDL_Window *getSDLWindow();
	static bool handleEvent();
	static void revertMousePos();
	static Vec2i getOldMousePos();
	static bool isKeyDown() { return isKeyPressedDown; }
	static void setupGraphicsScreen(int depthBits=-1, int stencilBits=-1, bool hardware_acceleration=false, bool fullscreen_anti_aliasing=false);
	static const bool getIsFullScreen() { return isFullScreen; }
	static void setIsFullScreen(bool value) { isFullScreen = value; }
	//static SDL_keysym getKeystate() { return keystate; }
	static bool isKeyStateModPressed(int mod);
	static wchar_t extractLastKeyPressed();

	Window();
	Window(SDL_Window *sdlWindow);
	virtual ~Window();

	static void addAllowedKeys(string keyList);
	static void clearAllowedKeys();
	static bool isAllowedKey(wchar_t key);

	virtual int getScreenWidth() = 0;
	virtual int getScreenHeight() = 0;

	virtual bool ChangeVideoMode(bool preserveContext,int resWidth, int resHeight,
			bool fullscreenWindow, int colorBits, int depthBits, int stencilBits,
            bool hardware_acceleration, bool fullscreen_anti_aliasing,
            float gammaValue) = 0;
	//static void setMasterserverMode(bool value) { Window::masterserverMode = value;}
	//static bool getMasterserverMode() { return Window::masterserverMode;}

	static bool getTryVSynch() { return tryVSynch; }
	static void setTryVSynch(bool value) { tryVSynch = value; }

	WindowHandle getHandle()	{return 0;}
	string getText();
	int getX()					{ return 0; }
	int getY()					{ return 0; }
	int getW()					{ return getScreenWidth(); }
	int getH()					{ return getScreenHeight(); }

	//component state
	int getClientW()			{ return getW(); }
	int getClientH()			{ return getH(); }
	float getAspect();

	//object state
	void setText(string text);
	void setStyle(WindowStyle windowStyle);
	void setSize(int w, int h);
	void setPos(int x, int y);
	void setEnabled(bool enabled);
	void setVisible(bool visible);

	//misc
	void create();
	void destroy();
	void minimize();

	static void setAllowAltEnterFullscreenToggle(bool value) { allowAltEnterFullscreenToggle = value; }
	static bool getAllowAltEnterFullscreenToggle() { return allowAltEnterFullscreenToggle; }

	static char getRawKey(SDL_keysym keysym);

protected:

	void setSDLWindow(SDL_Window *window);

	virtual void eventCreate(){}
	virtual void eventMouseDown(int x, int y, MouseButton mouseButton){}
	virtual void eventMouseUp(int x, int y, MouseButton mouseButton){}
	virtual void eventMouseMove(int x, int y, const MouseState* mouseState){}
	virtual void eventMouseDoubleClick(int x, int y, MouseButton mouseButton){}
	virtual void eventMouseWheel(int x, int y, int zDelta) {}
	virtual void eventKeyDown(SDL_KeyboardEvent key) {}
	virtual void eventKeyUp(SDL_KeyboardEvent key) {}
	virtual void eventKeyPress(SDL_KeyboardEvent c) {}
	virtual bool eventTextInput(std::string text) { return false; }
	virtual bool eventSdlKeyDown(SDL_KeyboardEvent key) { return false; }
	virtual void eventResize() {};
	virtual void eventPaint() {}
	virtual void eventTimer(int timerId) {}
	virtual void eventActivate(bool activated) {};
	virtual void eventResize(SizeState sizeState) {};
	virtual void eventMenu(int menuId) {}
	virtual void eventClose() {};
	virtual void eventDestroy() {};
	virtual void eventToggleFullScreen(bool isFullscreen) {};
	virtual void eventWindowEvent(SDL_WindowEvent event) {}

private:
	/// needed to detect double clicks
	void handleMouseDown(SDL_Event event);
	void handleMouseWheel(SDL_Event event);

	static MouseButton getMouseButton(int sdlButton);
	//static char getKey(SDL_keysym keysym, bool skipSpecialKeys=false);
	//static char getNormalKey(SDL_keysym keysym,bool skipSpecialKeys=false);
	static void toggleFullscreen();

	static wchar_t convertStringtoSDLKey(const string &value);
};

bool isKeyPressed(SDL_Keycode compareKey, SDL_KeyboardEvent input, vector<int> modifiersToCheck);
bool isKeyPressed(SDL_Keycode compareKey, SDL_KeyboardEvent input, bool modifiersAllowed=true);

SDL_Keycode extractKeyPressed(SDL_KeyboardEvent input);
bool isAllowedInputTextKey(SDL_Keycode key);

wchar_t extractKeyPressedUnicode(SDL_KeyboardEvent input);
vector<int> extractKeyPressedUnicodeLength(string text);
bool isAllowedInputTextKey(wchar_t &key);

}}//end namespace

#endif