Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AppCocoaTouch.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/app/App.h"
27 #include "cinder/app/Window.h"
28 #include "cinder/app/TouchEvent.h"
29 
30 #ifdef __OBJC__
31  @class AppImplCocoaTouch;
32  @class UITextView;
33 #else
34  class AppImplCocoaTouch;
35  class UITextView;
36 #endif
37 
38 namespace cinder { namespace app {
39 
40 struct AppCocoaTouchState;
41 
43  Unknown = 0,
44  Portrait = 1 << 0,
46  LandscapeLeft = 1 << 2,
47  LandscapeRight = 1 << 3,
51 };
52 
54 typedef signals::signal<uint32_t (), BitwiseAndEventCombiner<uint32_t> > EventSignalSupportedOrientations;
55 
56 class AppCocoaTouch : public App {
57  public:
58  class Settings : public App::Settings {
59  public:
61  : App::Settings(), mEnableStatusBar( false )
62  {
63  mPowerManagement = false;
64  }
65 
67  void enableStatusBar( bool enable = true ) { mEnableStatusBar = enable; }
69  bool isStatusBarEnabled() const { return mEnableStatusBar; }
70 
71  private:
72  bool mEnableStatusBar;
73  };
74 
75  AppCocoaTouch();
76  virtual ~AppCocoaTouch() {}
77 
78  virtual void prepareSettings( Settings *settings ) {}
79 
80  signals::signal<void()>& getSignalDidEnterBackground() { return mSignalDidEnterBackground; }
82  signals::signal<void()>& getSignalWillEnterForeground() { return mSignalWillEnterForeground; }
84  signals::signal<void()>& getSignalMemoryWarning() { return mSignalMemoryWarning; }
85  void emitMemoryWarning();
86 
88  EventSignalSupportedOrientations& getSignalSupportedOrientations() { return mSignalSupportedOrientations; }
90  uint32_t emitSupportedOrientations();
92  signals::signal<void()>& getSignalWillRotate() { return mSignalWillRotate; }
94  void emitWillRotate();
96  signals::signal<void()>& getSignalDidRotate() { return mSignalDidRotate; }
98  void emitDidRotate();
100  signals::signal<void()>& getSignalKeyboardWillShow() { return mSignalKeyboardWillShow; }
102  void emitKeyboardWillShow();
104  signals::signal<void()>& getSignalKeyboardWillHide() { return mSignalKeyboardWillHide; }
106  void emitKeyboardWillHide();
107 
109 
110  virtual WindowRef getWindow() const override;
111  virtual size_t getNumWindows() const override;
113  virtual app::WindowRef getWindowIndex( size_t index = 0 ) const override;
114 
119 
121  void enableProximitySensor();
123  void disableProximitySensor();
125  bool proximityIsClose() const;
127  signals::signal<void(bool)>& getSignalProximitySensor() { return mSignalProximitySensor; }
128  void emitSignalProximitySensor( bool isClose ) { mSignalProximitySensor( isClose ); }
129 
135  float getBatteryLevel() const;
137  bool isUnplugged() const;
139  signals::signal<void(bool)>& getSignalBatteryState() { return mSignalBatteryState; }
140  void emitSignalBatteryState( bool isUnplugged ) { mSignalBatteryState( isUnplugged ); }
141 
143  void enablePowerManagement( bool powerManagement = true ) override;
144 
146 
149  KeyboardOptions() : mType( KeyboardType::DEFAULT ), mCloseOnReturn( true ) {}
150 
152  KeyboardOptions& initialString( const std::string &str ) { mInitialString = str; return *this; }
154  KeyboardOptions& type( KeyboardType type ) { mType = type; return *this; }
156  KeyboardOptions& closeOnReturn( bool enable ) { mCloseOnReturn = enable; return *this; }
157 
159  const std::string& getInitialString() const { return mInitialString; }
161  KeyboardType getType() const { return mType; }
163  bool getCloseOnReturn() const { return mCloseOnReturn; }
164 
165  private:
166  std::string mInitialString;
167  KeyboardType mType;
168  bool mCloseOnReturn;
169  };
170 
172  void showKeyboard( const KeyboardOptions &options = KeyboardOptions() );
174  bool isKeyboardVisible() const;
176  void hideKeyboard();
180  void setKeyboardString( const std::string &keyboardString );
182  ::UITextView *getkeyboardTextView() const;
183 
186  void showStatusBar( StatusBarAnimation animation = StatusBarAnimation::NONE );
188  bool isStatusBarVisible() const;
190  void hideStatusBar( StatusBarAnimation animation = StatusBarAnimation::NONE );
191 
193  virtual float getFrameRate() const;
195  virtual void setFrameRate( float frameRate );
197  virtual bool isFullScreen() const;
199  virtual void setFullScreen( bool aFullScreen );
200 
202  virtual double getElapsedSeconds() const;
203 
205  virtual fs::path getAppPath() const;
206 
208  virtual void quit();
209 
211  static AppCocoaTouch* get() { return sInstance; }
213  virtual const Settings& getSettings() const { return mSettings; }
214 
215 
217  // These are called by application instantation macros and are only used in the launch process
218  static void prepareLaunch() { App::prepareLaunch(); }
219  static void executeLaunch( AppCocoaTouch *app, RendererRef renderer, const char *title, int argc, char * const argv[] ) { App::sInstance = sInstance = app; App::executeLaunch( app, renderer, title, argc, argv ); }
220  static void cleanupLaunch() { App::cleanupLaunch(); }
221 
222  virtual void launch( const char *title, int argc, char * const argv[] ) override;
224 
225  // DO NOT CALL - should be private but aren't for esoteric reasons
227  // Internal handlers - these are called into by AppImpl's. If you are calling one of these, you have likely strayed far off the path.
228  void privatePrepareSettings__();
229  void privateSetImpl__( AppImplCocoaTouch *impl ) { mImpl = impl; }
231 
232  private:
233  friend void setupCocoaTouchWindow( AppCocoaTouch *app );
234 
235 
236  static AppCocoaTouch *sInstance;
237  AppImplCocoaTouch *mImpl;
238  Settings mSettings;
239 
240  signals::signal<void()> mSignalDidEnterBackground, mSignalWillEnterForeground, mSignalMemoryWarning;
241 
242  signals::signal<void(bool)> mSignalProximitySensor, mSignalBatteryState;
243 
244  EventSignalSupportedOrientations mSignalSupportedOrientations;
245  signals::signal<void()> mSignalWillRotate, mSignalDidRotate;
246  signals::signal<void(void)> mSignalKeyboardWillShow, mSignalKeyboardWillHide;
247 };
248 
250 extern std::ostream& operator<<( std::ostream &lhs, const InterfaceOrientation &rhs );
252 float getOrientationDegrees( InterfaceOrientation orientation );
253 
254 } } // namespace cinder::app
255 
256 #define CINDER_APP_COCOA_TOUCH( APP, RENDERER ) \
257 int main( int argc, char *argv[] ) { \
258  cinder::app::AppCocoaTouch::prepareLaunch(); \
259  cinder::app::AppCocoaTouch *app = new APP; \
260  cinder::app::RendererRef ren( new RENDERER ); \
261  cinder::app::AppCocoaTouch::executeLaunch( app, ren, #APP, argc, argv );\
262  cinder::app::AppCocoaTouch::cleanupLaunch(); \
263  return 0; \
264 }
void showStatusBar(StatusBarAnimation animation=StatusBarAnimation::NONE)
Shows the system status bar.
Definition: AppCocoaTouch.mm:558
Definition: AppCocoaTouch.h:44
WindowRef createWindow(const Window::Format &format)
Definition: AppCocoaTouch.mm:444
void emitKeyboardWillHide()
Emits the signal used to notify when the virtual keyboard is about to animate off screen...
Definition: AppCocoaTouch.mm:661
void emitSignalBatteryState(bool isUnplugged)
Definition: AppCocoaTouch.h:140
virtual WindowRef getWindow() const override
Returns the the currently active Window. Throws ExcInvalidWindow if called with no active window...
Definition: AppCocoaTouch.mm:449
void enable(GLenum state)
Enables the OpenGL State state. Equivalent to calling to glEnable( state );.
Definition: dx.h:198
static App * sInstance
Definition: App.h:561
void hideKeyboard()
Hides the default iOS keyboard.
Definition: AppCocoaTouch.mm:537
GLsizei const GLchar ** string
Definition: GLee.h:2427
KeyboardOptions & closeOnReturn(bool enable)
Sets whether the keyboard closes (hides) when the return key is entered. Default is true...
Definition: AppCocoaTouch.h:156
float getOrientationDegrees(InterfaceOrientation orientation)
returns the degrees rotation from Portrait for the provided orientation
Definition: AppCocoaTouch.mm:678
std::ostream & operator<<(std::ostream &lhs, const InterfaceOrientation &rhs)
Stream InterfacefaceOrientation enum to std::ostream.
Definition: AppCocoaTouch.mm:666
void emitKeyboardWillShow()
Emits the signal used to notify when the virtual keyboard is about to animate on screen.
Definition: AppCocoaTouch.mm:656
Definition: AppCocoaTouch.h:45
virtual void prepareSettings(Settings *settings)
Definition: AppCocoaTouch.h:78
signals::signal< void()> & getSignalWillRotate()
Returns the signal emitted when the interface is about to rotate to a new orientation. At this time, the Window's bounds and orientation have already been updated.
Definition: AppCocoaTouch.h:92
void disableBatteryMonitoring()
Disables the device's battery monitor.
Definition: AppCocoaTouch.mm:512
void emitSignalProximitySensor(bool isClose)
Definition: AppCocoaTouch.h:128
void enableProximitySensor()
Enables the device's proximity sensor, which can return whether the device is close to the user or no...
Definition: AppCocoaTouch.mm:484
Definition: App.h:129
Definition: AppCocoaTouch.h:43
uint32_t emitSupportedOrientations()
Emits a signal to ask the user what orientations are supported.
Definition: AppCocoaTouch.mm:641
GLuint index
Definition: GLee.h:2259
void emitWillRotate()
Emits the signal to notify the user that the orientation will change.
Definition: AppCocoaTouch.mm:646
signals::signal< void()> & getSignalWillEnterForeground()
Definition: AppCocoaTouch.h:82
::UITextView * getkeyboardTextView() const
Retuens a pointer to the native UITextView, which can be used for further customization.
Definition: AppCocoaTouch.mm:552
bool isUnplugged() const
Returns true when the device is not plugged into power.
Definition: AppCocoaTouch.mm:522
signals::signal< void()> & getSignalKeyboardWillShow()
Returns the signal emitted when the virtual keyboard is about to animate on screen.
Definition: AppCocoaTouch.h:100
virtual double getElapsedSeconds() const
Returns the number seconds which have elapsed since the active App launched.
Definition: AppCocoaTouch.mm:599
bool isStatusBarVisible() const
Returns whether the system status bar is visible.
Definition: AppCocoaTouch.mm:565
Definition: AppCocoaTouch.h:46
signals::signal< void()> & getSignalKeyboardWillHide()
Returns the signal emitted when the virtual keyboard is about to animate off screen.
Definition: AppCocoaTouch.h:104
virtual float getFrameRate() const
Returns the maximum frame-rate the App will attempt to maintain.
Definition: AppCocoaTouch.mm:578
KeyboardType getType() const
Returns the keyboard layout type.
Definition: AppCocoaTouch.h:161
Definition: AppCocoaTouch.h:184
const std::string & getInitialString() const
Returns the keyboard's initial string.
Definition: AppCocoaTouch.h:159
std::shared_ptr< Window > WindowRef
Definition: Event.h:49
bool getCloseOnReturn() const
Returns whether the keyboard closes (hides) when the return key is entered.
Definition: AppCocoaTouch.h:163
Definition: AppCocoaTouch.h:145
Definition: AppCocoaTouch.h:184
EventSignalSupportedOrientations & getSignalSupportedOrientations()
Returns the signal emitted when an orientation change may occur, allowing the user to specify which o...
Definition: AppCocoaTouch.h:88
signals::signal< void(bool)> & getSignalProximitySensor()
Signal emitted when the device becomes close (true) or far (false). Must call enableProximitySensor()...
Definition: AppCocoaTouch.h:127
InterfaceOrientation getOrientation() const
Returns the InterfaceOrientation for the device Window.
Definition: AppCocoaTouch.mm:472
Definition: AppCocoaTouch.h:47
friend void setupCocoaTouchWindow(AppCocoaTouch *app)
virtual void setFrameRate(float frameRate)
Sets the maximum frame-rate the App will attempt to maintain. This will be rounded to the nearest fac...
Definition: AppCocoaTouch.mm:584
bool mPowerManagement
Definition: App.h:226
bool isStatusBarEnabled() const
Returns whether the system status bar is visible initially. Default is false.
Definition: AppCocoaTouch.h:69
Definition: App.h:127
void hideStatusBar(StatusBarAnimation animation=StatusBarAnimation::NONE)
Hides the system status bar.
Definition: AppCocoaTouch.mm:570
void enablePowerManagement(bool powerManagement=true) override
When disabled, the device will not sleep even after the idling threshold.
Definition: AppCocoaTouch.mm:620
std::shared_ptr< class Renderer > RendererRef
Definition: Renderer.h:85
signals::signal< void()> & getSignalDidEnterBackground()
Definition: AppCocoaTouch.h:80
Definition: AppCocoaTouch.h:48
std::string getKeyboardString() const
Returns the current text recorded since the most recent call to showKeyboard().
Definition: AppCocoaTouch.mm:542
signals::signal< void()> & getSignalMemoryWarning()
Definition: AppCocoaTouch.h:84
Optional params passed to showKeyboard();.
Definition: AppCocoaTouch.h:148
KeyboardOptions & initialString(const std::string &str)
Sets the initial value of the keyboard string. Default is an empty string.
Definition: AppCocoaTouch.h:152
void emitDidEnterBackground()
Definition: AppCocoaTouch.mm:626
virtual size_t getNumWindows() const override
Returns the number of Windows the app has open.
Definition: AppCocoaTouch.mm:457
StatusBarAnimation
Definition: AppCocoaTouch.h:184
void showKeyboard(const KeyboardOptions &options=KeyboardOptions())
Shows the iOS virtual keyboard with KeyboardOptions options. This method can be called with new optio...
Definition: AppCocoaTouch.mm:527
AppCocoaTouch()
Definition: AppCocoaTouch.mm:433
void enableBatteryMonitoring()
Enables the device's battery monitor. Use in conjunction with getBatteryLevel(), isUnplugged() and ge...
Definition: AppCocoaTouch.mm:501
virtual app::WindowRef getWindowIndex(size_t index=0) const override
Gets a Window by index, in the range [0, getNumWindows()).
Definition: AppCocoaTouch.mm:462
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:963
Definition: AppCocoaTouch.h:58
virtual const Settings & getSettings() const
Returns a pointer to the current global AppBasic.
Definition: AppCocoaTouch.h:213
Definition: AppCocoaTouch.h:184
Settings()
Definition: AppCocoaTouch.h:60
BitwiseAndEventCombiner< uint32_t > EventSignalSupportedOrientations
Definition: AppCocoaTouch.h:54
Definition: AppCocoaTouch.h:50
virtual void setFullScreen(bool aFullScreen)
Sets whether the active App is in full-screen mode based on fullScreen.
Definition: AppCocoaTouch.mm:594
InterfaceOrientation getWindowOrientation() const
Returns the current InterfaceOrientation for the active Window.
Definition: AppCocoaTouch.mm:478
void enableStatusBar(bool enable=true)
Determines whether the system status bar is visible initially. Default is false.
Definition: AppCocoaTouch.h:67
Definition: Window.h:140
void emitMemoryWarning()
Definition: AppCocoaTouch.mm:636
virtual void quit()
Not implemented on iPhone.
Definition: AppCocoaTouch.mm:610
signals::signal< void()> & getSignalDidRotate()
Returns the signal emitted when the interface is finished rotating to a new orientation.
Definition: AppCocoaTouch.h:96
GLenum GLsizei GLenum format
Definition: GLee.h:969
virtual bool isFullScreen() const
Returns whether the App is in full-screen mode or not.
Definition: AppCocoaTouch.mm:589
void emitWillEnterForeground()
Definition: AppCocoaTouch.mm:631
KeyboardOptions & type(KeyboardType type)
Sets the keyboard layout type to . Default is KeyboardType::DEFAULT.
Definition: AppCocoaTouch.h:154
virtual ~AppCocoaTouch()
Definition: AppCocoaTouch.h:76
signals::signal< void(bool)> & getSignalBatteryState()
Signal emitted when the device is unplugged (true) or plugged in (false). Use in place of polling isU...
Definition: AppCocoaTouch.h:139
Definition: AppCocoaTouch.h:56
void disableProximitySensor()
Disables the device's proximity sensor.
Definition: AppCocoaTouch.mm:491
bool isKeyboardVisible() const
Returns whether the iOS keyboard is visible.
Definition: AppCocoaTouch.mm:532
Definition: AppCocoaTouch.h:145
KeyboardOptions()
Definition: AppCocoaTouch.h:149
Definition: AppCocoaTouch.h:49
virtual fs::path getAppPath() const
Returns the path to the application on disk.
Definition: AppCocoaTouch.mm:605
bool proximityIsClose() const
Returns whether the device is close to the user or not. Must call enableProximitySensor() before usin...
Definition: AppCocoaTouch.mm:496
float getBatteryLevel() const
Returns the device's battery level, in the range of [0,1];.
Definition: AppCocoaTouch.mm:517
void emitDidRotate()
Emits the signal to notify the user that the orientation did change.
Definition: AppCocoaTouch.mm:651
void setKeyboardString(const std::string &keyboardString)
Sets the current text string that the keyboard is manipulating, overwriting any previous content...
Definition: AppCocoaTouch.mm:547
KeyboardType
Definition: AppCocoaTouch.h:145
InterfaceOrientation
Definition: AppCocoaTouch.h:42
Definition: AppCocoaTouch.h:145