1 // ============================================================== 2 // ============================================================== 3 // This file is part of Glest Shared Library (www.glest.org) 4 // 5 // Copyright (C) 2005 Matthias Braun <matze@braunis.de> 6 // 7 // You can redistribute this code and/or modify it under 8 // the terms of the GNU General Public License as published 9 // by the Free Software Foundation; either version 2 of the 10 // License, or (at your option) any later version 11 // ============================================================== 12 13 #ifndef _SHARED_PLATFORM_WINDOW_H_ 14 #define _SHARED_PLATFORM_WINDOW_H_ 15 16 #include <map> 17 #include <string> 18 #include <SDL.h> 19 #include <cassert> 20 #include "data_types.h" 21 #include "vec.h" 22 #include <vector> 23 #include "leak_dumper.h" 24 25 using std::map; 26 using std::vector; 27 using std::string; 28 using Shared::Graphics::Vec2i; 29 30 #if SDL_VERSION_ATLEAST(2,0,0) 31 32 typedef SDL_Keysym SDL_keysym; 33 34 #endif 35 36 namespace Shared{ namespace Platform{ 37 38 class Timer; 39 //class PlatformContextGl; 40 41 enum MouseButton { 42 mbUnknown, 43 mbLeft, 44 mbCenter, 45 mbRight, 46 mbButtonX1, 47 mbButtonX2, 48 49 mbCount 50 }; 51 52 enum SizeState{ 53 ssMaximized, 54 ssMinimized, 55 ssRestored 56 }; 57 58 class MouseState { 59 private: 60 bool states[mbCount]; 61 62 63 public: MouseState()64 MouseState() { 65 clear(); 66 } 67 //MouseState(const MouseState &); 68 //MouseState &operator=(const MouseState &); clear()69 void clear() { memset(this, 0, sizeof(MouseState)); } 70 get(MouseButton b)71 bool get(MouseButton b) const { 72 if(b > 0 && b < mbCount) { 73 return states[b]; 74 } 75 return false; 76 } 77 set(MouseButton b,bool state)78 void set(MouseButton b, bool state) { 79 if(b > 0 && b < mbCount) { 80 states[b] = state; 81 } 82 } 83 }; 84 85 enum WindowStyle{ 86 wsFullscreen, 87 wsWindowedFixed, 88 wsWindowedResizable 89 }; 90 91 // ===================================================== 92 // class Window 93 // ===================================================== 94 95 class Window { 96 private: 97 static SDL_Window *sdlWindow; 98 Uint32 lastMouseDown[mbCount]; 99 int lastMouseX[mbCount]; 100 int lastMouseY[mbCount]; 101 102 static int64 lastMouseEvent; /** for use in mouse hover calculations */ 103 static MouseState mouseState; 104 static Vec2i mousePos; 105 static bool isKeyPressedDown; 106 static bool isFullScreen; 107 static SDL_keysym keystate; 108 static bool tryVSynch; 109 static int64 lastToggle ; 110 setLastToggle(int64 lastToggle)111 static void setLastToggle(int64 lastToggle) {Window::lastToggle = lastToggle;} getLastToggle()112 static int64 getLastToggle() {return Window::lastToggle;} 113 setLastMouseEvent(int64 lastMouseEvent)114 static void setLastMouseEvent(int64 lastMouseEvent) {Window::lastMouseEvent = lastMouseEvent;} getLastMouseEvent()115 static int64 getLastMouseEvent() {return Window::lastMouseEvent;} 116 getMouseState()117 static const MouseState &getMouseState() {return Window::mouseState;} setMouseState(MouseButton b,bool state)118 static void setMouseState(MouseButton b, bool state) {Window::mouseState.set(b, state);} 119 getMousePos()120 static const Vec2i &getMousePos() {return Window::mousePos;} setMousePos(const Vec2i & mousePos)121 static void setMousePos(const Vec2i &mousePos) {Window::mousePos = mousePos;} 122 setKeystate(SDL_keysym state)123 static void setKeystate(SDL_keysym state) { keystate = state; } 124 125 //static bool masterserverMode; 126 static map<wchar_t,bool> mapAllowedKeys; 127 128 protected: 129 //int w, h; 130 static bool isActive; 131 static bool allowAltEnterFullscreenToggle; 132 static int lastShowMouseState; 133 134 public: 135 static SDL_Window *getSDLWindow(); 136 static bool handleEvent(); 137 static void revertMousePos(); 138 static Vec2i getOldMousePos(); isKeyDown()139 static bool isKeyDown() { return isKeyPressedDown; } 140 static void setupGraphicsScreen(int depthBits=-1, int stencilBits=-1, bool hardware_acceleration=false, bool fullscreen_anti_aliasing=false); getIsFullScreen()141 static const bool getIsFullScreen() { return isFullScreen; } setIsFullScreen(bool value)142 static void setIsFullScreen(bool value) { isFullScreen = value; } 143 //static SDL_keysym getKeystate() { return keystate; } 144 static bool isKeyStateModPressed(int mod); 145 static wchar_t extractLastKeyPressed(); 146 147 Window(); 148 Window(SDL_Window *sdlWindow); 149 virtual ~Window(); 150 151 static void addAllowedKeys(string keyList); 152 static void clearAllowedKeys(); 153 static bool isAllowedKey(wchar_t key); 154 155 virtual int getScreenWidth() = 0; 156 virtual int getScreenHeight() = 0; 157 158 virtual bool ChangeVideoMode(bool preserveContext,int resWidth, int resHeight, 159 bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, 160 bool hardware_acceleration, bool fullscreen_anti_aliasing, 161 float gammaValue) = 0; 162 //static void setMasterserverMode(bool value) { Window::masterserverMode = value;} 163 //static bool getMasterserverMode() { return Window::masterserverMode;} 164 getTryVSynch()165 static bool getTryVSynch() { return tryVSynch; } setTryVSynch(bool value)166 static void setTryVSynch(bool value) { tryVSynch = value; } 167 getHandle()168 WindowHandle getHandle() {return 0;} 169 string getText(); getX()170 int getX() { return 0; } getY()171 int getY() { return 0; } getW()172 int getW() { return getScreenWidth(); } getH()173 int getH() { return getScreenHeight(); } 174 175 //component state getClientW()176 int getClientW() { return getW(); } getClientH()177 int getClientH() { return getH(); } 178 float getAspect(); 179 180 //object state 181 void setText(string text); 182 void setStyle(WindowStyle windowStyle); 183 void setSize(int w, int h); 184 void setPos(int x, int y); 185 void setEnabled(bool enabled); 186 void setVisible(bool visible); 187 188 //misc 189 void create(); 190 void destroy(); 191 void minimize(); 192 setAllowAltEnterFullscreenToggle(bool value)193 static void setAllowAltEnterFullscreenToggle(bool value) { allowAltEnterFullscreenToggle = value; } getAllowAltEnterFullscreenToggle()194 static bool getAllowAltEnterFullscreenToggle() { return allowAltEnterFullscreenToggle; } 195 196 static char getRawKey(SDL_keysym keysym); 197 198 protected: 199 200 void setSDLWindow(SDL_Window *window); 201 eventCreate()202 virtual void eventCreate(){} eventMouseDown(int x,int y,MouseButton mouseButton)203 virtual void eventMouseDown(int x, int y, MouseButton mouseButton){} eventMouseUp(int x,int y,MouseButton mouseButton)204 virtual void eventMouseUp(int x, int y, MouseButton mouseButton){} eventMouseMove(int x,int y,const MouseState * mouseState)205 virtual void eventMouseMove(int x, int y, const MouseState* mouseState){} eventMouseDoubleClick(int x,int y,MouseButton mouseButton)206 virtual void eventMouseDoubleClick(int x, int y, MouseButton mouseButton){} eventMouseWheel(int x,int y,int zDelta)207 virtual void eventMouseWheel(int x, int y, int zDelta) {} eventKeyDown(SDL_KeyboardEvent key)208 virtual void eventKeyDown(SDL_KeyboardEvent key) {} eventKeyUp(SDL_KeyboardEvent key)209 virtual void eventKeyUp(SDL_KeyboardEvent key) {} eventKeyPress(SDL_KeyboardEvent c)210 virtual void eventKeyPress(SDL_KeyboardEvent c) {} eventTextInput(std::string text)211 virtual bool eventTextInput(std::string text) { return false; } eventSdlKeyDown(SDL_KeyboardEvent key)212 virtual bool eventSdlKeyDown(SDL_KeyboardEvent key) { return false; } eventResize()213 virtual void eventResize() {}; eventPaint()214 virtual void eventPaint() {} eventTimer(int timerId)215 virtual void eventTimer(int timerId) {} eventActivate(bool activated)216 virtual void eventActivate(bool activated) {}; eventResize(SizeState sizeState)217 virtual void eventResize(SizeState sizeState) {}; eventMenu(int menuId)218 virtual void eventMenu(int menuId) {} eventClose()219 virtual void eventClose() {}; eventDestroy()220 virtual void eventDestroy() {}; eventToggleFullScreen(bool isFullscreen)221 virtual void eventToggleFullScreen(bool isFullscreen) {}; eventWindowEvent(SDL_WindowEvent event)222 virtual void eventWindowEvent(SDL_WindowEvent event) {} 223 224 private: 225 /// needed to detect double clicks 226 void handleMouseDown(SDL_Event event); 227 void handleMouseWheel(SDL_Event event); 228 229 static MouseButton getMouseButton(int sdlButton); 230 //static char getKey(SDL_keysym keysym, bool skipSpecialKeys=false); 231 //static char getNormalKey(SDL_keysym keysym,bool skipSpecialKeys=false); 232 static void toggleFullscreen(); 233 234 static wchar_t convertStringtoSDLKey(const string &value); 235 }; 236 237 bool isKeyPressed(SDL_Keycode compareKey, SDL_KeyboardEvent input, vector<int> modifiersToCheck); 238 bool isKeyPressed(SDL_Keycode compareKey, SDL_KeyboardEvent input, bool modifiersAllowed=true); 239 240 SDL_Keycode extractKeyPressed(SDL_KeyboardEvent input); 241 bool isAllowedInputTextKey(SDL_Keycode key); 242 243 wchar_t extractKeyPressedUnicode(SDL_KeyboardEvent input); 244 vector<int> extractKeyPressedUnicodeLength(string text); 245 bool isAllowedInputTextKey(wchar_t &key); 246 247 }}//end namespace 248 249 #endif 250