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