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