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
6 #ifndef __C_IRR_DEVICE_OSX_H_INCLUDED__
\r
7 #define __C_IRR_DEVICE_OSX_H_INCLUDED__
\r
10 #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
\r
12 #include "CIrrDeviceStub.h"
\r
13 #include "IrrlichtDevice.h"
\r
14 #include "IGUIEnvironment.h"
\r
15 #include "ICursorControl.h"
\r
17 #import <AppKit/NSWindow.h>
\r
18 #import <AppKit/NSBitmapImageRep.h>
\r
24 class CIrrDeviceMacOSX;
\r
27 @interface CIrrDelegateOSX : NSObject
\r
29 - (id)initWithDevice:(irr::CIrrDeviceMacOSX*)device;
\r
30 - (void)terminate:(id)sender;
\r
37 class CIrrDeviceMacOSX : public CIrrDeviceStub
\r
42 CIrrDeviceMacOSX(const SIrrlichtCreationParameters& params);
\r
45 virtual ~CIrrDeviceMacOSX();
\r
47 //! runs the device. Returns false if device wants to be deleted
\r
48 bool run() override;
\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
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
57 //! sets the caption of the window
\r
58 void setWindowCaption(const wchar_t* text) override;
\r
60 //! returns if window is active. if not, nothing need to be drawn
\r
61 bool isWindowActive() const override;
\r
63 //! Checks if the Irrlicht window has focus
\r
64 bool isWindowFocused() const override;
\r
66 //! Checks if the Irrlicht window is minimized
\r
67 bool isWindowMinimized() const override;
\r
69 //! notifies the device that it should close itself
\r
70 void closeDevice() override;
\r
72 //! Sets if the window should be resizable in windowed mode.
\r
73 void setResizable(bool resize) override;
\r
75 //! Returns true if the window is resizable, false if not
\r
76 virtual bool isResizable() const;
\r
78 //! Minimizes the window if possible
\r
79 void minimizeWindow() override;
\r
81 //! Maximizes the window if possible.
\r
82 void maximizeWindow() override;
\r
84 //! Restore the window to normal size if possible.
\r
85 void restoreWindow() override;
\r
87 //! Get the position of this window on screen
\r
88 core::position2di getWindowPosition() override;
\r
90 //! Activate any joysticks, and generate events for them.
\r
91 bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo) override;
\r
93 //! Get the device type
\r
94 E_DEVICE_TYPE getType() const override
\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
106 //! create the driver
\r
107 void createDriver();
\r
109 //! Implementation of the macos x cursor control
\r
110 class CCursorControl : public gui::ICursorControl
\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
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
124 //! Changes the visible state of the mouse cursor.
\r
125 void setVisible(bool visible) override
\r
127 IsVisible = visible;
\r
128 Device->setCursorVisible(visible);
\r
131 //! Returns if the cursor is currently visible.
\r
132 bool isVisible() const override
\r
137 //! Sets the new position of the cursor.
\r
138 void setPosition(const core::position2d<f32> &pos) override
\r
140 setPosition(pos.X, pos.Y);
\r
143 //! Sets the new position of the cursor.
\r
144 void setPosition(f32 x, f32 y) override
\r
146 setPosition((s32)(x*WindowSize.Width), (s32)(y*WindowSize.Height));
\r
149 //! Sets the new position of the cursor.
\r
150 void setPosition(const core::position2d<s32> &pos) override
\r
152 if (CursorPos.X != pos.X || CursorPos.Y != pos.Y)
\r
153 setPosition(pos.X, pos.Y);
\r
156 //! Sets the new position of the cursor.
\r
157 void setPosition(s32 x, s32 y) override
\r
159 if (UseReferenceRect)
\r
161 Device->setMouseLocation(ReferenceRect.UpperLeftCorner.X + x, ReferenceRect.UpperLeftCorner.Y + y);
\r
165 Device->setMouseLocation(x,y);
\r
169 //! Returns the current position of the mouse cursor.
\r
170 const core::position2d<s32>& getPosition(bool updateCursor) override
\r
175 //! Returns the current position of the mouse cursor.
\r
176 core::position2d<f32> getRelativePosition(bool updateCursor) override
\r
178 if (!UseReferenceRect)
\r
180 return core::position2d<f32>(CursorPos.X * InvWindowSize.Width,
\r
181 CursorPos.Y * InvWindowSize.Height);
\r
184 return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(),
\r
185 CursorPos.Y / (f32)ReferenceRect.getHeight());
\r
188 //! Sets an absolute reference rect for calculating the cursor position.
\r
189 void setReferenceRect(core::rect<s32>* rect=0) override
\r
193 ReferenceRect = *rect;
\r
194 UseReferenceRect = true;
\r
196 // prevent division through zero and uneven sizes
\r
198 if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2)
\r
199 ReferenceRect.LowerRightCorner.Y += 1;
\r
201 if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2)
\r
202 ReferenceRect.LowerRightCorner.X += 1;
\r
205 UseReferenceRect = false;
\r
208 //! Updates the internal cursor position
\r
209 void updateInternalCursorPosition(int x,int y)
\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
223 bool UseReferenceRect;
\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
234 CGDirectDisplayID Display;
\r
235 std::map<int,int> KeyCodes;
\r
240 u32 MouseButtonStates;
\r
244 bool IsControlDown;
\r
249 } // end namespace irr
\r
251 #endif // _IRR_COMPILE_WITH_OSX_DEVICE_
\r
252 #endif // __C_IRR_DEVICE_MACOSX_H_INCLUDED__
\r