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
9 #include "IrrCompileConfig.h"
\r
11 #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
\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
19 #import <AppKit/NSWindow.h>
\r
20 #import <AppKit/NSBitmapImageRep.h>
\r
26 class CIrrDeviceMacOSX;
\r
29 @interface CIrrDelegateOSX : NSObject
\r
31 - (id)initWithDevice:(irr::CIrrDeviceMacOSX*)device;
\r
32 - (void)terminate:(id)sender;
\r
39 class CIrrDeviceMacOSX : public CIrrDeviceStub, video::IImagePresenter
\r
44 CIrrDeviceMacOSX(const SIrrlichtCreationParameters& params);
\r
47 virtual ~CIrrDeviceMacOSX();
\r
49 //! runs the device. Returns false if device wants to be deleted
\r
50 bool run() override;
\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 void yield() override;
\r
56 //! Pause execution and let other processes to run for a specified amount of time.
\r
57 void sleep(u32 timeMs, bool pauseTimer) override;
\r
59 //! sets the caption of the window
\r
60 void setWindowCaption(const wchar_t* text) override;
\r
62 //! returns if window is active. if not, nothing need to be drawn
\r
63 bool isWindowActive() const override;
\r
65 //! Checks if the Irrlicht window has focus
\r
66 bool isWindowFocused() const override;
\r
68 //! Checks if the Irrlicht window is minimized
\r
69 bool isWindowMinimized() const override;
\r
71 //! presents a surface in the client area
\r
72 bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0 ) override;
\r
74 //! notifies the device that it should close itself
\r
75 void closeDevice() override;
\r
77 //! Sets if the window should be resizable in windowed mode.
\r
78 void setResizable(bool resize) override;
\r
80 //! Returns true if the window is resizable, false if not
\r
81 virtual bool isResizable() const;
\r
83 //! Minimizes the window if possible
\r
84 void minimizeWindow() override;
\r
86 //! Maximizes the window if possible.
\r
87 void maximizeWindow() override;
\r
89 //! Restore the window to normal size if possible.
\r
90 void restoreWindow() override;
\r
92 //! Get the position of this window on screen
\r
93 core::position2di getWindowPosition() override;
\r
95 //! Activate any joysticks, and generate events for them.
\r
96 bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo) override;
\r
98 //! Get the device type
\r
99 E_DEVICE_TYPE getType() const override
\r
104 void setMouseLocation(int x, int y);
\r
105 void setResize(int width, int height);
\r
106 void setCursorVisible(bool visible);
\r
107 void setWindow(NSWindow* window);
\r
111 //! create the driver
\r
112 void createDriver();
\r
114 //! Implementation of the macos x cursor control
\r
115 class CCursorControl : public gui::ICursorControl
\r
119 CCursorControl(const core::dimension2d<u32>& wsize, CIrrDeviceMacOSX *device)
\r
120 : WindowSize(wsize), InvWindowSize(0.0f, 0.0f), Device(device), IsVisible(true), UseReferenceRect(false)
\r
122 CursorPos.X = CursorPos.Y = 0;
\r
123 if (WindowSize.Width!=0)
\r
124 InvWindowSize.Width = 1.0f / WindowSize.Width;
\r
125 if (WindowSize.Height!=0)
\r
126 InvWindowSize.Height = 1.0f / WindowSize.Height;
\r
129 //! Changes the visible state of the mouse cursor.
\r
130 void setVisible(bool visible) override
\r
132 IsVisible = visible;
\r
133 Device->setCursorVisible(visible);
\r
136 //! Returns if the cursor is currently visible.
\r
137 bool isVisible() const override
\r
142 //! Sets the new position of the cursor.
\r
143 void setPosition(const core::position2d<f32> &pos) override
\r
145 setPosition(pos.X, pos.Y);
\r
148 //! Sets the new position of the cursor.
\r
149 void setPosition(f32 x, f32 y) override
\r
151 setPosition((s32)(x*WindowSize.Width), (s32)(y*WindowSize.Height));
\r
154 //! Sets the new position of the cursor.
\r
155 void setPosition(const core::position2d<s32> &pos) override
\r
157 if (CursorPos.X != pos.X || CursorPos.Y != pos.Y)
\r
158 setPosition(pos.X, pos.Y);
\r
161 //! Sets the new position of the cursor.
\r
162 void setPosition(s32 x, s32 y) override
\r
164 if (UseReferenceRect)
\r
166 Device->setMouseLocation(ReferenceRect.UpperLeftCorner.X + x, ReferenceRect.UpperLeftCorner.Y + y);
\r
170 Device->setMouseLocation(x,y);
\r
174 //! Returns the current position of the mouse cursor.
\r
175 const core::position2d<s32>& getPosition(bool updateCursor) override
\r
180 //! Returns the current position of the mouse cursor.
\r
181 core::position2d<f32> getRelativePosition(bool updateCursor) override
\r
183 if (!UseReferenceRect)
\r
185 return core::position2d<f32>(CursorPos.X * InvWindowSize.Width,
\r
186 CursorPos.Y * InvWindowSize.Height);
\r
189 return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(),
\r
190 CursorPos.Y / (f32)ReferenceRect.getHeight());
\r
193 //! Sets an absolute reference rect for calculating the cursor position.
\r
194 void setReferenceRect(core::rect<s32>* rect=0) override
\r
198 ReferenceRect = *rect;
\r
199 UseReferenceRect = true;
\r
201 // prevent division through zero and uneven sizes
\r
203 if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2)
\r
204 ReferenceRect.LowerRightCorner.Y += 1;
\r
206 if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2)
\r
207 ReferenceRect.LowerRightCorner.X += 1;
\r
210 UseReferenceRect = false;
\r
213 //! Updates the internal cursor position
\r
214 void updateInternalCursorPosition(int x,int y)
\r
222 core::position2d<s32> CursorPos;
\r
223 core::dimension2d<s32> WindowSize;
\r
224 core::dimension2d<float> InvWindowSize;
\r
225 core::rect<s32> ReferenceRect;
\r
226 CIrrDeviceMacOSX *Device;
\r
228 bool UseReferenceRect;
\r
231 bool createWindow();
\r
232 void initKeycodes();
\r
233 void storeMouseLocation();
\r
234 void postMouseEvent(void *event, irr::SEvent &ievent);
\r
235 void postKeyEvent(void *event, irr::SEvent &ievent, bool pressed);
\r
236 void pollJoysticks();
\r
239 CGDirectDisplayID Display;
\r
240 NSBitmapImageRep* SoftwareDriverTarget;
\r
241 std::map<int,int> KeyCodes;
\r
246 u32 MouseButtonStates;
\r
247 u32 SoftwareRendererType;
\r
251 bool IsControlDown;
\r
256 } // end namespace irr
\r
258 #endif // _IRR_COMPILE_WITH_OSX_DEVICE_
\r
259 #endif // __C_IRR_DEVICE_MACOSX_H_INCLUDED__
\r