]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CIrrDeviceOSX.h
Merging r6145 through r6171 from trunk to ogl-es branch
[irrlicht.git] / source / Irrlicht / CIrrDeviceOSX.h
1 // Copyright (C) 2005-2006 Etienne Petitjean\r
2 // Copyright (C) 2007-2012 Christian Stehno\r
3 // This file is part of the "Irrlicht Engine".\r
4 // For conditions of distribution and use, see copyright notice in Irrlicht.h\r
5 \r
6 #ifndef __C_IRR_DEVICE_OSX_H_INCLUDED__\r
7 #define __C_IRR_DEVICE_OSX_H_INCLUDED__\r
8 \r
9 #include "IrrCompileConfig.h"\r
10 \r
11 #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_\r
12 \r
13 #include "CIrrDeviceStub.h"\r
14 #include "IrrlichtDevice.h"\r
15 #include "IImagePresenter.h"\r
16 #include "IGUIEnvironment.h"\r
17 #include "ICursorControl.h"\r
18 \r
19 #import <AppKit/NSWindow.h>\r
20 #import <AppKit/NSBitmapImageRep.h>\r
21 \r
22 #include <map>\r
23 \r
24 namespace irr\r
25 {\r
26     class CIrrDeviceMacOSX;\r
27 }\r
28 \r
29 @interface CIrrDelegateOSX : NSObject\r
30 \r
31 - (id)initWithDevice:(irr::CIrrDeviceMacOSX*)device;\r
32 - (void)terminate:(id)sender;\r
33 - (BOOL)isQuit;\r
34 \r
35 @end\r
36 \r
37 namespace irr\r
38 {\r
39         class CIrrDeviceMacOSX : public CIrrDeviceStub, video::IImagePresenter\r
40         {\r
41         public:\r
42 \r
43                 //! constructor\r
44                 CIrrDeviceMacOSX(const SIrrlichtCreationParameters& params);\r
45 \r
46                 //! destructor\r
47                 virtual ~CIrrDeviceMacOSX();\r
48 \r
49                 //! runs the device. Returns false if device wants to be deleted\r
50                 virtual bool run() _IRR_OVERRIDE_;\r
51 \r
52                 //! Cause the device to temporarily pause execution and let other processes to run\r
53                 // This should bring down processor usage without major performance loss for Irrlicht\r
54                 virtual void yield() _IRR_OVERRIDE_;\r
55 \r
56                 //! Pause execution and let other processes to run for a specified amount of time.\r
57                 virtual void sleep(u32 timeMs, bool pauseTimer) _IRR_OVERRIDE_;\r
58 \r
59                 //! sets the caption of the window\r
60                 virtual void setWindowCaption(const wchar_t* text) _IRR_OVERRIDE_;\r
61 \r
62                 //! returns if window is active. if not, nothing need to be drawn\r
63                 virtual bool isWindowActive() const _IRR_OVERRIDE_;\r
64 \r
65                 //! Checks if the Irrlicht window has focus\r
66                 virtual bool isWindowFocused() const _IRR_OVERRIDE_;\r
67 \r
68                 //! Checks if the Irrlicht window is minimized\r
69                 virtual bool isWindowMinimized() const _IRR_OVERRIDE_;\r
70 \r
71                 //! presents a surface in the client area\r
72                 virtual bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0 ) _IRR_OVERRIDE_;\r
73 \r
74                 //! notifies the device that it should close itself\r
75                 virtual void closeDevice() _IRR_OVERRIDE_;\r
76 \r
77                 //! Sets if the window should be resizable in windowed mode.\r
78                 virtual void setResizable(bool resize) _IRR_OVERRIDE_;\r
79 \r
80                 //! Returns true if the window is resizable, false if not\r
81                 virtual bool isResizable() const;\r
82 \r
83                 //! Minimizes the window if possible\r
84                 virtual void minimizeWindow() _IRR_OVERRIDE_;\r
85 \r
86                 //! Maximizes the window if possible.\r
87                 virtual void maximizeWindow() _IRR_OVERRIDE_;\r
88 \r
89                 //! Restore the window to normal size if possible.\r
90                 virtual void restoreWindow() _IRR_OVERRIDE_;\r
91 \r
92         //! Get the position of this window on screen\r
93         virtual core::position2di getWindowPosition() _IRR_OVERRIDE_;\r
94 \r
95                 //! Activate any joysticks, and generate events for them.\r
96                 virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo) _IRR_OVERRIDE_;\r
97 \r
98                 //! \return Returns a pointer to a list with all video modes\r
99                 //! supported by the gfx adapter.\r
100                 virtual video::IVideoModeList* getVideoModeList() _IRR_OVERRIDE_;\r
101 \r
102                 //! Get the device type\r
103                 virtual E_DEVICE_TYPE getType() const _IRR_OVERRIDE_\r
104                 {\r
105                                 return EIDT_OSX;\r
106                 }\r
107 \r
108                 void setMouseLocation(int x, int y);\r
109                 void setResize(int width, int height);\r
110                 void setCursorVisible(bool visible);\r
111         void setWindow(NSWindow* window);\r
112 \r
113         private:\r
114 \r
115                 //! create the driver\r
116                 void createDriver();\r
117 \r
118                 //! Implementation of the macos x cursor control\r
119                 class CCursorControl : public gui::ICursorControl\r
120                 {\r
121                 public:\r
122 \r
123                         CCursorControl(const core::dimension2d<u32>& wsize, CIrrDeviceMacOSX *device)\r
124                                 : WindowSize(wsize), InvWindowSize(0.0f, 0.0f), Device(device), IsVisible(true), UseReferenceRect(false)\r
125                         {\r
126                                 CursorPos.X = CursorPos.Y = 0;\r
127                                 if (WindowSize.Width!=0)\r
128                                         InvWindowSize.Width = 1.0f / WindowSize.Width;\r
129                                 if (WindowSize.Height!=0)\r
130                                         InvWindowSize.Height = 1.0f / WindowSize.Height;\r
131                         }\r
132 \r
133                         //! Changes the visible state of the mouse cursor.\r
134                         virtual void setVisible(bool visible)  _IRR_OVERRIDE_\r
135                         {\r
136                                 IsVisible = visible;\r
137                                 Device->setCursorVisible(visible);\r
138                         }\r
139 \r
140                         //! Returns if the cursor is currently visible.\r
141                         virtual bool isVisible() const  _IRR_OVERRIDE_\r
142                         {\r
143                                 return IsVisible;\r
144                         }\r
145 \r
146                         //! Sets the new position of the cursor.\r
147                         virtual void setPosition(const core::position2d<f32> &pos)  _IRR_OVERRIDE_\r
148                         {\r
149                                 setPosition(pos.X, pos.Y);\r
150                         }\r
151 \r
152                         //! Sets the new position of the cursor.\r
153                         virtual void setPosition(f32 x, f32 y)  _IRR_OVERRIDE_\r
154                         {\r
155                                 setPosition((s32)(x*WindowSize.Width), (s32)(y*WindowSize.Height));\r
156                         }\r
157 \r
158                         //! Sets the new position of the cursor.\r
159                         virtual void setPosition(const core::position2d<s32> &pos) _IRR_OVERRIDE_\r
160                         {\r
161                                 if (CursorPos.X != pos.X || CursorPos.Y != pos.Y)\r
162                                         setPosition(pos.X, pos.Y);\r
163                         }\r
164 \r
165                         //! Sets the new position of the cursor.\r
166                         virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_\r
167                         {\r
168                                 if (UseReferenceRect)\r
169                                 {\r
170                                         Device->setMouseLocation(ReferenceRect.UpperLeftCorner.X + x, ReferenceRect.UpperLeftCorner.Y + y);\r
171                                 }\r
172                                 else\r
173                                 {\r
174                                         Device->setMouseLocation(x,y);\r
175                                 }\r
176                         }\r
177 \r
178                         //! Returns the current position of the mouse cursor.\r
179                         virtual const core::position2d<s32>& getPosition(bool updateCursor) _IRR_OVERRIDE_\r
180                         {\r
181                                 return CursorPos;\r
182                         }\r
183 \r
184                         //! Returns the current position of the mouse cursor.\r
185                         virtual core::position2d<f32> getRelativePosition(bool updateCursor) _IRR_OVERRIDE_\r
186                         {\r
187                                 if (!UseReferenceRect)\r
188                                 {\r
189                                         return core::position2d<f32>(CursorPos.X * InvWindowSize.Width,\r
190                                                 CursorPos.Y * InvWindowSize.Height);\r
191                                 }\r
192 \r
193                                 return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(),\r
194                                                 CursorPos.Y / (f32)ReferenceRect.getHeight());\r
195                         }\r
196 \r
197                         //! Sets an absolute reference rect for calculating the cursor position.\r
198                         virtual void setReferenceRect(core::rect<s32>* rect=0)  _IRR_OVERRIDE_\r
199                         {\r
200                                 if (rect)\r
201                                 {\r
202                                         ReferenceRect = *rect;\r
203                                         UseReferenceRect = true;\r
204 \r
205                                         // prevent division through zero and uneven sizes\r
206 \r
207                                         if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2)\r
208                                                 ReferenceRect.LowerRightCorner.Y += 1;\r
209 \r
210                                         if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2)\r
211                                                 ReferenceRect.LowerRightCorner.X += 1;\r
212                                 }\r
213                                 else\r
214                                         UseReferenceRect = false;\r
215                         }\r
216 \r
217                         //! Updates the internal cursor position\r
218                         void updateInternalCursorPosition(int x,int y)\r
219                         {\r
220                                 CursorPos.X = x;\r
221                                 CursorPos.Y = y;\r
222                         }\r
223 \r
224                 private:\r
225 \r
226                         core::position2d<s32> CursorPos;\r
227                         core::dimension2d<s32> WindowSize;\r
228                         core::dimension2d<float> InvWindowSize;\r
229                         core::rect<s32> ReferenceRect;\r
230                         CIrrDeviceMacOSX *Device;\r
231                         bool IsVisible;\r
232                         bool UseReferenceRect;\r
233                 };\r
234 \r
235                 bool createWindow();\r
236                 void initKeycodes();\r
237                 void storeMouseLocation();\r
238                 void postMouseEvent(void *event, irr::SEvent &ievent);\r
239                 void postKeyEvent(void *event, irr::SEvent &ievent, bool pressed);\r
240                 void pollJoysticks();\r
241 \r
242                 NSWindow* Window;\r
243         CGDirectDisplayID Display;\r
244                 NSBitmapImageRep* SoftwareDriverTarget;\r
245                 std::map<int,int> KeyCodes;\r
246                 int DeviceWidth;\r
247                 int DeviceHeight;\r
248                 int ScreenWidth;\r
249                 int ScreenHeight;\r
250                 u32 MouseButtonStates;\r
251         u32 SoftwareRendererType;\r
252         bool IsFullscreen;\r
253                 bool IsActive;\r
254                 bool IsShiftDown;\r
255                 bool IsControlDown;\r
256                 bool IsResizable;\r
257         };\r
258 \r
259 \r
260 } // end namespace irr\r
261 \r
262 #endif // _IRR_COMPILE_WITH_OSX_DEVICE_\r
263 #endif // __C_IRR_DEVICE_MACOSX_H_INCLUDED__\r
264 \r