include/cinder/app/AppCocoaTouch.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/app/App.h"
00026 #include "cinder/cocoa/CinderCocoaTouch.h"
00027 #include "cinder/app/Window.h"
00028 #include "cinder/app/TouchEvent.h"
00029 
00030 #ifdef __OBJC__
00031     @class AppImplCocoaTouch;
00032 #else
00033     class AppImplCocoaTouch;
00034 #endif
00035 
00036 namespace cinder { namespace app {
00037 
00038 struct AppCocoaTouchState;
00039 
00040 enum InterfaceOrientation {
00041     Unknown                 = 0,
00042     Portrait                = 1 << 0,
00043     PortraitUpsideDown      = 1 << 1,
00044     LandscapeLeft           = 1 << 2,
00045     LandscapeRight          = 1 << 3,
00046     PortraitAll             = (Portrait | PortraitUpsideDown),
00047     LandscapeAll            = (LandscapeLeft | LandscapeRight),
00048     All                     = (PortraitAll | LandscapeAll)
00049 };
00050 
00052 typedef signals::signal<uint32_t (), BitwiseAndEventCombiner<uint32_t> >        EventSignalSupportedOrientations;
00053 
00054 class AppCocoaTouch : public App {
00055   public:
00056     class Settings : public App::Settings {
00057       public:
00058         Settings()
00059             : App::Settings(), mEnableStatusBar( false )
00060         {
00061             mPowerManagement = false;
00062         }
00063 
00065         void        enableStatusBar( bool enable = true ) { mEnableStatusBar = enable; }
00067         bool        isStatusBarEnabled() const { return mEnableStatusBar; }
00068         
00069       private:
00070         bool        mEnableStatusBar;
00071     };
00072 
00073     AppCocoaTouch();
00074     virtual ~AppCocoaTouch() {}
00075 
00076     virtual void        prepareSettings( Settings *settings ) {}
00077 
00078     signals::signal<void()>&    getSignalDidEnterBackground() { return mSignalDidEnterBackground; }
00079     void                        emitDidEnterBackground();
00080     signals::signal<void()>&    getSignalWillEnterForeground() { return mSignalWillEnterForeground; }
00081     void                        emitWillEnterForeground();
00082     signals::signal<void()>&    getSignalWillResignActive() { return mSignalWillResignActive; }
00083     void                        emitWillResignActive();
00084     signals::signal<void()>&    getSignalDidBecomeActive() { return mSignalDidBecomeActive; }
00085     void                        emitDidBecomeActive();
00086     signals::signal<void()>&    getSignalMemoryWarning() { return mSignalMemoryWarning; }
00087     void                        emitMemoryWarning();
00088 
00090     EventSignalSupportedOrientations&   getSignalSupportedOrientations() { return mSignalSupportedOrientations; }
00092     uint32_t                            emitSupportedOrientations();
00094     signals::signal<void()>&            getSignalWillRotate() { return mSignalWillRotate; }
00096     void                                emitWillRotate();
00098     signals::signal<void()>&            getSignalDidRotate() { return mSignalDidRotate; }
00100     void                                emitDidRotate();
00101 
00102     WindowRef       createWindow( const Window::Format &format );
00103 
00104     virtual WindowRef getWindow() const override;
00105     virtual size_t getNumWindows() const override;
00107     virtual app::WindowRef  getWindowIndex( size_t index = 0 ) const override;
00108 
00110     InterfaceOrientation    getOrientation() const;
00112     InterfaceOrientation    getWindowOrientation() const;
00113 
00115     void enableProximitySensor();
00117     void disableProximitySensor();
00119     bool proximityIsClose() const;
00121     signals::signal<void(bool)>&    getSignalProximitySensor() { return mSignalProximitySensor; }
00122     void                            emitSignalProximitySensor( bool isClose ) { mSignalProximitySensor( isClose ); }
00123 
00125     void    enableBatteryMonitoring();
00127     void    disableBatteryMonitoring();
00129     float   getBatteryLevel() const;
00131     bool    isUnplugged() const;
00133     signals::signal<void(bool)>&    getSignalBatteryState() { return mSignalBatteryState; }
00134     void                            emitSignalBatteryState( bool isUnplugged ) { mSignalBatteryState( isUnplugged ); }
00135 
00137     void enablePowerManagement( bool powerManagement = true ) override;
00138 
00140     void        showKeyboard();
00142     bool        isKeyboardVisible() const;
00144     void        hideKeyboard();
00146     std::string getKeyboardString() const;
00147 
00148     typedef enum StatusBarAnimation { NONE, FADE, SLIDE } StatusBarAnimation;
00150     void    showStatusBar( StatusBarAnimation animation = StatusBarAnimation::NONE );
00152     bool    isStatusBarVisible() const;
00154     void    hideStatusBar( StatusBarAnimation animation = StatusBarAnimation::NONE );
00155     
00157     virtual float       getFrameRate() const;
00159     virtual void        setFrameRate( float frameRate );
00161     virtual bool        isFullScreen() const;
00163     virtual void        setFullScreen( bool aFullScreen );
00164 
00166     virtual double      getElapsedSeconds() const;
00167 
00169     virtual fs::path    getAppPath() const;
00170 
00172     virtual void    quit();
00173 
00175     static AppCocoaTouch*   get() { return sInstance; }
00177     virtual const Settings& getSettings() const { return mSettings; }
00178 
00179 
00181     // These are called by application instantation macros and are only used in the launch process
00182     static void     prepareLaunch() { App::prepareLaunch(); }
00183     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 ); }
00184     static void     cleanupLaunch() { App::cleanupLaunch(); }
00185     
00186     virtual void    launch( const char *title, int argc, char * const argv[] ) override;
00188 
00189     // DO NOT CALL - should be private but aren't for esoteric reasons
00191     // Internal handlers - these are called into by AppImpl's. If you are calling one of these, you have likely strayed far off the path.
00192     void        privatePrepareSettings__();
00193     void        privateSetImpl__( AppImplCocoaTouch *impl ) { mImpl = impl; }
00195 
00196   private:
00197     friend void     setupCocoaTouchWindow( AppCocoaTouch *app );
00198     
00199     
00200     static AppCocoaTouch    *sInstance; 
00201     AppImplCocoaTouch       *mImpl;
00202     Settings                mSettings;
00203 
00204     signals::signal<void()>     mSignalDidEnterBackground, mSignalWillEnterForeground, mSignalWillResignActive, mSignalDidBecomeActive, mSignalMemoryWarning;
00205 
00206     signals::signal<void(bool)>     mSignalProximitySensor, mSignalBatteryState;
00207 
00208     EventSignalSupportedOrientations        mSignalSupportedOrientations;
00209     signals::signal<void()>                 mSignalWillRotate, mSignalDidRotate;
00210 
00211     bool                    mIsKeyboardVisible;
00212 };
00213 
00215 extern std::ostream& operator<<( std::ostream &lhs, const InterfaceOrientation &rhs );
00217 float getOrientationDegrees( InterfaceOrientation orientation );
00218 
00219 } } // namespace cinder::app
00220 
00221 #define CINDER_APP_COCOA_TOUCH( APP, RENDERER )                             \
00222 int main( int argc, char *argv[] ) {                                        \
00223     cinder::app::AppCocoaTouch::prepareLaunch();                            \
00224     cinder::app::AppCocoaTouch *app = new APP;                              \
00225     cinder::app::RendererRef ren( new RENDERER );                           \
00226     cinder::app::AppCocoaTouch::executeLaunch( app, ren, #APP, argc, argv );\
00227     cinder::app::AppCocoaTouch::cleanupLaunch();                            \
00228     return 0;                                                               \
00229 }