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/TouchEvent.h" 00028 #include "cinder/app/AccelEvent.h" 00029 00030 00031 namespace cinder { namespace app { 00032 00033 struct AppCocoaTouchState; 00034 00035 class AppCocoaTouch : public App { 00036 public: 00037 class Settings : public App::Settings { 00038 public: 00039 Settings() 00040 : App::Settings(), mEnableMultiTouch( true ) {} 00041 00043 void enableMultiTouch( bool enable = true ) { mEnableMultiTouch = enable; } 00045 bool isMultiTouchEnabled() const { return mEnableMultiTouch; } 00046 00047 private: 00048 bool mEnableMultiTouch; 00049 }; 00050 00051 AppCocoaTouch(); 00052 virtual ~AppCocoaTouch() {} 00053 00054 virtual void prepareSettings( Settings *settings ) {} 00056 virtual void didBecomeActive() {} 00057 virtual void willResignActive() {} 00058 00060 virtual void touchesBegan( TouchEvent event ) {} 00062 virtual void touchesMoved( TouchEvent event ) {} 00064 virtual void touchesEnded( TouchEvent event ) {} 00066 const std::vector<TouchEvent::Touch>& getActiveTouches() const { return mActiveTouches; } 00068 virtual void accelerated( AccelEvent event ) {} 00069 00071 CallbackId registerTouchesBegan( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesBegan.registerCb( callback ); } 00073 template<typename T> 00074 CallbackId registerTouchesBegan( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesBegan.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00076 void unregisterTouchesBegan( CallbackId id ) { mCallbacksTouchesBegan.unregisterCb( id ); } 00077 00079 CallbackId registerTouchesMoved( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesMoved.registerCb( callback ); } 00081 template<typename T> 00082 CallbackId registerTouchesMoved( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesMoved.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00084 void unregisterTouchesMoved( CallbackId id ) { mCallbacksTouchesMoved.unregisterCb( id ); } 00085 00087 CallbackId registerTouchesEnded( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesEnded.registerCb( callback ); } 00089 template<typename T> 00090 CallbackId registerTouchesEnded( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesEnded.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00092 void unregisterTouchesEnded( CallbackId id ) { mCallbacksTouchesEnded.unregisterCb( id ); } 00093 00095 CallbackId registerAccelerated( std::function<bool (AccelEvent)> callback ) { return mCallbacksAccelerated.registerCb( callback ); } 00097 template<typename T> 00098 CallbackId registerAccelerated( T *obj, bool (T::*callback)(AccelEvent) ) { return mCallbacksAccelerated.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00100 void unregisterAccelerated( CallbackId id ) { mCallbacksAccelerated.unregisterCb( id ); } 00101 00102 00104 virtual int getWindowWidth() const; 00106 virtual int getWindowHeight() const; 00108 void setWindowWidth( int windowWidth ) {} 00110 void setWindowHeight( int windowHeight ) {} 00112 void setWindowSize( int windowWidth, int windowHeight ) {} 00113 00115 void enableAccelerometer( float updateFrequency = 30.0f, float filterFactor = 0.1f ); 00117 void disableAccelerometer(); 00118 00120 virtual float getFrameRate() const; 00122 virtual void setFrameRate( float aFrameRate ); 00124 virtual bool isFullScreen() const; 00126 virtual void setFullScreen( bool aFullScreen ); 00127 00129 virtual double getElapsedSeconds() const; 00130 00132 virtual std::string getAppPath(); 00133 00135 virtual void quit(); 00136 00138 static AppCocoaTouch* get() { return sInstance; } 00140 virtual const Settings& getSettings() const { return mSettings; } 00141 00142 00144 // These are called by application instantation macros and are only used in the launch process 00145 static void prepareLaunch() { App::prepareLaunch(); } 00146 static void executeLaunch( AppCocoaTouch *app, class Renderer *renderer, const char *title, int argc, char * const argv[] ) { sInstance = app; App::executeLaunch( app, renderer, title, argc, argv ); } 00147 static void cleanupLaunch() { App::cleanupLaunch(); } 00148 00149 virtual void launch( const char *title, int argc, char * const argv[] ); 00151 00152 // DO NOT CALL - should be private but aren't for esoteric reasons 00154 // Internal handlers - these are called into by AppImpl's. If you are calling one of these, you have likely strayed far off the path. 00155 void privatePrepareSettings__(); 00156 void privateTouchesBegan__( const TouchEvent &event ); 00157 void privateTouchesMoved__( const TouchEvent &event ); 00158 void privateTouchesEnded__( const TouchEvent &event ); 00159 void privateSetActiveTouches__( const std::vector<TouchEvent::Touch> &touches ) { mActiveTouches = touches; } 00160 void privateAccelerated__( const Vec3f &direction ); 00162 00163 // The state is contained in a struct in order to avoid this .h needing to be compiled as Objective-C++ 00164 std::shared_ptr<AppCocoaTouchState> mState; 00165 00166 private: 00167 friend void setupCocoaTouchWindow( AppCocoaTouch *app ); 00168 00169 00170 static AppCocoaTouch *sInstance; 00171 Settings mSettings; 00172 00173 std::vector<TouchEvent::Touch> mActiveTouches; 00174 00175 CallbackMgr<bool (TouchEvent)> mCallbacksTouchesBegan, mCallbacksTouchesMoved, mCallbacksTouchesEnded; 00176 CallbackMgr<bool (AccelEvent)> mCallbacksAccelerated; 00177 00178 float mAccelFilterFactor; 00179 Vec3f mLastAccel, mLastRawAccel; 00180 }; 00181 00182 } } // namespace cinder::app 00183 00184 #define CINDER_APP_COCOA_TOUCH( APP, RENDERER ) \ 00185 int main( int argc, char *argv[] ) { \ 00186 cinder::app::AppCocoaTouch::prepareLaunch(); \ 00187 cinder::app::AppCocoaTouch *app = new APP; \ 00188 cinder::app::Renderer *ren = new RENDERER; \ 00189 cinder::app::AppCocoaTouch::executeLaunch( app, ren, #APP, argc, argv );\ 00190 cinder::app::AppCocoaTouch::cleanupLaunch(); \ 00191 return 0; \ 00192 }