├── .gitignore
├── example-scene
├── bin
│ └── data
│ │ └── .gitignore
├── .gitignore
├── src
│ ├── main.cpp
│ ├── testApp.h
│ └── testApp.cpp
├── Project.xcconfig
└── openFrameworks-Info.plist
├── example-timer
├── bin
│ └── data
│ │ └── .gitignore
├── .gitignore
├── src
│ ├── main.cpp
│ ├── testApp.h
│ └── testApp.cpp
├── Project.xcconfig
└── openFrameworks-Info.plist
├── example-sequence
├── bin
│ └── data
│ │ └── .gitignore
├── .gitignore
├── src
│ ├── main.cpp
│ ├── testApp.h
│ └── testApp.cpp
├── Project.xcconfig
└── openFrameworks-Info.plist
├── example-composition
├── bin
│ └── data
│ │ └── .gitignore
├── .gitignore
├── src
│ ├── main.cpp
│ ├── testApp.h
│ └── testApp.cpp
├── Project.xcconfig
├── openFrameworks-Info.plist
└── example-composition.xcodeproj
│ └── project.pbxproj
├── example-instance-manager
├── bin
│ └── data
│ │ └── .gitignore
├── .gitignore
├── src
│ ├── main.cpp
│ ├── testApp.h
│ └── testApp.cpp
├── Project.xcconfig
├── openFrameworks-Info.plist
└── example-instance-manager.xcodeproj
│ └── project.pbxproj
└── src
├── ofxAnimationPrimitives.h
└── ofxAnimationPrimitives
├── Generator.h
├── Timer.cpp
├── Constants.h
├── detail
├── coroutine.hpp
└── Composition.inc.h
├── Timer.h
├── Lerp.h
├── Sequence.h
├── Replicator.h
├── Easing.h
├── CommandQueue.h
├── Composition.h
├── Pattern.h
├── InstanceManager.h
└── SceneManager.h
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
3 |
--------------------------------------------------------------------------------
/example-scene/bin/data/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in here apart from the .gitignore file
2 | *
3 | !.gitignore
--------------------------------------------------------------------------------
/example-timer/bin/data/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in here apart from the .gitignore file
2 | *
3 | !.gitignore
--------------------------------------------------------------------------------
/example-sequence/bin/data/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in here apart from the .gitignore file
2 | *
3 | !.gitignore
--------------------------------------------------------------------------------
/example-composition/bin/data/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in here apart from the .gitignore file
2 | *
3 | !.gitignore
--------------------------------------------------------------------------------
/example-instance-manager/bin/data/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in here apart from the .gitignore file
2 | *
3 | !.gitignore
--------------------------------------------------------------------------------
/example-scene/.gitignore:
--------------------------------------------------------------------------------
1 | .svn
2 | .hg
3 | .cvs
4 |
5 | # osx
6 | *.app
7 | *.mode1v3
8 | *.pbxuser
9 | .DS_Store
10 | build/
11 | xcuserdata/
12 | DerivedData/
13 | project.xcworkspace
14 |
15 | # vs2010
16 | ipch/
17 | obj/
18 | *.sdf
19 |
--------------------------------------------------------------------------------
/example-timer/.gitignore:
--------------------------------------------------------------------------------
1 | .svn
2 | .hg
3 | .cvs
4 |
5 | # osx
6 | *.app
7 | *.mode1v3
8 | *.pbxuser
9 | .DS_Store
10 | build/
11 | xcuserdata/
12 | DerivedData/
13 | project.xcworkspace
14 |
15 | # vs2010
16 | ipch/
17 | obj/
18 | *.sdf
19 |
--------------------------------------------------------------------------------
/example-composition/.gitignore:
--------------------------------------------------------------------------------
1 | .svn
2 | .hg
3 | .cvs
4 |
5 | # osx
6 | *.app
7 | *.mode1v3
8 | *.pbxuser
9 | .DS_Store
10 | build/
11 | xcuserdata/
12 | DerivedData/
13 | project.xcworkspace
14 |
15 | # vs2010
16 | ipch/
17 | obj/
18 | *.sdf
19 |
--------------------------------------------------------------------------------
/example-sequence/.gitignore:
--------------------------------------------------------------------------------
1 | .svn
2 | .hg
3 | .cvs
4 |
5 | # osx
6 | *.app
7 | *.mode1v3
8 | *.pbxuser
9 | .DS_Store
10 | build/
11 | xcuserdata/
12 | DerivedData/
13 | project.xcworkspace
14 |
15 | # vs2010
16 | ipch/
17 | obj/
18 | *.sdf
19 |
--------------------------------------------------------------------------------
/example-instance-manager/.gitignore:
--------------------------------------------------------------------------------
1 | .svn
2 | .hg
3 | .cvs
4 |
5 | # osx
6 | *.app
7 | *.mode1v3
8 | *.pbxuser
9 | .DS_Store
10 | build/
11 | xcuserdata/
12 | DerivedData/
13 | project.xcworkspace
14 |
15 | # vs2010
16 | ipch/
17 | obj/
18 | *.sdf
19 |
--------------------------------------------------------------------------------
/example-scene/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 | #include "ofAppGlutWindow.h"
3 |
4 | //--------------------------------------------------------------
5 | int main()
6 | {
7 | ofAppGlutWindow window; // create a window
8 | // set width, height, mode (OF_WINDOW or OF_FULLSCREEN)
9 | ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
10 | ofRunApp(new testApp()); // start the app
11 | }
--------------------------------------------------------------------------------
/example-timer/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 | #include "ofAppGlutWindow.h"
3 |
4 | //--------------------------------------------------------------
5 | int main()
6 | {
7 | ofAppGlutWindow window; // create a window
8 | // set width, height, mode (OF_WINDOW or OF_FULLSCREEN)
9 | ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
10 | ofRunApp(new testApp()); // start the app
11 | }
--------------------------------------------------------------------------------
/example-sequence/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 | #include "ofAppGlutWindow.h"
3 |
4 | //--------------------------------------------------------------
5 | int main()
6 | {
7 | ofAppGlutWindow window; // create a window
8 | // set width, height, mode (OF_WINDOW or OF_FULLSCREEN)
9 | ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
10 | ofRunApp(new testApp()); // start the app
11 | }
--------------------------------------------------------------------------------
/example-composition/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 | #include "ofAppGlutWindow.h"
3 |
4 | //--------------------------------------------------------------
5 | int main()
6 | {
7 | ofAppGlutWindow window; // create a window
8 | // set width, height, mode (OF_WINDOW or OF_FULLSCREEN)
9 | ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
10 | ofRunApp(new testApp()); // start the app
11 | }
--------------------------------------------------------------------------------
/example-instance-manager/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 | #include "ofAppGlutWindow.h"
3 |
4 | //--------------------------------------------------------------
5 | int main()
6 | {
7 | ofAppGlutWindow window; // create a window
8 | // set width, height, mode (OF_WINDOW or OF_FULLSCREEN)
9 | ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
10 | ofRunApp(new testApp()); // start the app
11 | }
--------------------------------------------------------------------------------
/example-scene/Project.xcconfig:
--------------------------------------------------------------------------------
1 | //THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
2 | //THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
3 | OF_PATH = ../../..
4 |
5 | //THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
6 | #include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
7 |
8 | OTHER_LDFLAGS = $(OF_CORE_LIBS)
9 | HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
10 |
--------------------------------------------------------------------------------
/example-sequence/Project.xcconfig:
--------------------------------------------------------------------------------
1 | //THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
2 | //THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
3 | OF_PATH = ../../..
4 |
5 | //THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
6 | #include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
7 |
8 | OTHER_LDFLAGS = $(OF_CORE_LIBS)
9 | HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
10 |
--------------------------------------------------------------------------------
/example-timer/Project.xcconfig:
--------------------------------------------------------------------------------
1 | //THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
2 | //THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
3 | OF_PATH = ../../..
4 |
5 | //THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
6 | #include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
7 |
8 | OTHER_LDFLAGS = $(OF_CORE_LIBS)
9 | HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
10 |
--------------------------------------------------------------------------------
/example-composition/Project.xcconfig:
--------------------------------------------------------------------------------
1 | //THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
2 | //THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
3 | OF_PATH = ../../..
4 |
5 | //THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
6 | #include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
7 |
8 | OTHER_LDFLAGS = $(OF_CORE_LIBS)
9 | HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
10 |
--------------------------------------------------------------------------------
/example-instance-manager/Project.xcconfig:
--------------------------------------------------------------------------------
1 | //THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
2 | //THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
3 | OF_PATH = ../../..
4 |
5 | //THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
6 | #include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
7 |
8 | OTHER_LDFLAGS = $(OF_CORE_LIBS)
9 | HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
10 |
--------------------------------------------------------------------------------
/example-instance-manager/src/testApp.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | class testApp : public ofBaseApp
6 | {
7 | public:
8 | void setup();
9 | void update();
10 | void draw();
11 |
12 | void keyPressed(int key);
13 | void keyReleased(int key);
14 | void mouseMoved(int x, int y);
15 | void mouseDragged(int x, int y, int button);
16 | void mousePressed(int x, int y, int button);
17 | void mouseReleased(int x, int y, int button);
18 | void windowResized(int w, int h);
19 | void dragEvent(ofDragInfo dragInfo);
20 | void gotMessage(ofMessage msg);
21 | };
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofxAnimationPrimitives/Constants.h"
4 | #include "ofxAnimationPrimitives/Timer.h"
5 | #include "ofxAnimationPrimitives/Pattern.h"
6 | #include "ofxAnimationPrimitives/Replicator.h"
7 | #include "ofxAnimationPrimitives/Generator.h"
8 | #include "ofxAnimationPrimitives/Easing.h"
9 | #include "ofxAnimationPrimitives/InstanceManager.h"
10 | #include "ofxAnimationPrimitives/Lerp.h"
11 | #include "ofxAnimationPrimitives/Composition.h"
12 | #include "ofxAnimationPrimitives/Sequence.h"
13 | #include "ofxAnimationPrimitives/SceneManager.h"
14 | #include "ofxAnimationPrimitives/CommandQueue.h"
--------------------------------------------------------------------------------
/example-scene/src/testApp.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | class testApp : public ofBaseApp
6 | {
7 | public:
8 |
9 | void onOneshotTimerEvent(int&);
10 | void onRepeatTimerEvent(int&);
11 |
12 | void setup();
13 | void update();
14 | void draw();
15 |
16 | void keyPressed(int key);
17 | void keyReleased(int key);
18 | void mouseMoved(int x, int y);
19 | void mouseDragged(int x, int y, int button);
20 | void mousePressed(int x, int y, int button);
21 | void mouseReleased(int x, int y, int button);
22 | void windowResized(int w, int h);
23 | void dragEvent(ofDragInfo dragInfo);
24 | void gotMessage(ofMessage msg);
25 | };
--------------------------------------------------------------------------------
/example-sequence/src/testApp.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | class testApp : public ofBaseApp
6 | {
7 | public:
8 |
9 | void onOneshotTimerEvent(int&);
10 | void onRepeatTimerEvent(int&);
11 |
12 | void setup();
13 | void update();
14 | void draw();
15 |
16 | void keyPressed(int key);
17 | void keyReleased(int key);
18 | void mouseMoved(int x, int y);
19 | void mouseDragged(int x, int y, int button);
20 | void mousePressed(int x, int y, int button);
21 | void mouseReleased(int x, int y, int button);
22 | void windowResized(int w, int h);
23 | void dragEvent(ofDragInfo dragInfo);
24 | void gotMessage(ofMessage msg);
25 | };
--------------------------------------------------------------------------------
/example-timer/src/testApp.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | class testApp : public ofBaseApp
6 | {
7 | public:
8 |
9 | void onOneshotTimerEvent(int&);
10 | void onRepeatTimerEvent(int&);
11 |
12 | void setup();
13 | void update();
14 | void draw();
15 |
16 | void keyPressed(int key);
17 | void keyReleased(int key);
18 | void mouseMoved(int x, int y);
19 | void mouseDragged(int x, int y, int button);
20 | void mousePressed(int x, int y, int button);
21 | void mouseReleased(int x, int y, int button);
22 | void windowResized(int w, int h);
23 | void dragEvent(ofDragInfo dragInfo);
24 | void gotMessage(ofMessage msg);
25 | };
--------------------------------------------------------------------------------
/example-composition/src/testApp.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | class testApp : public ofBaseApp
6 | {
7 | public:
8 |
9 | void onOneshotTimerEvent(ofEventArgs&);
10 | void onRepeatTimerEvent(ofEventArgs&);
11 |
12 | void setup();
13 | void update();
14 | void draw();
15 |
16 | void keyPressed(int key);
17 | void keyReleased(int key);
18 | void mouseMoved(int x, int y);
19 | void mouseDragged(int x, int y, int button);
20 | void mousePressed(int x, int y, int button);
21 | void mouseReleased(int x, int y, int button);
22 | void windowResized(int w, int h);
23 | void dragEvent(ofDragInfo dragInfo);
24 | void gotMessage(ofMessage msg);
25 | };
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Generator.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #include "Constants.h"
6 |
7 | #include "detail/coroutine.hpp"
8 | // more info about boost::coroutine => http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/coroutine.html
9 |
10 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
11 |
12 | typedef coroutine Generator;
13 |
14 | #ifndef $seq
15 | # define $seq(c) CORO_REENTER(c)
16 | #endif
17 |
18 | #ifndef $inf
19 | # define $inf(c) CORO_REENTER(c) for(;;)
20 | #endif
21 |
22 | #ifndef yield
23 | # define yield CORO_YIELD
24 | #endif
25 |
26 | #ifndef fork
27 | # define fork CORO_FORK
28 | #endif
29 |
30 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
--------------------------------------------------------------------------------
/example-scene/openFrameworks-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleExecutable
8 | ${EXECUTABLE_NAME}
9 | CFBundleIdentifier
10 | com.yourcompany.openFrameworks
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundlePackageType
14 | APPL
15 | CFBundleSignature
16 | ????
17 | CFBundleVersion
18 | 1.0
19 |
20 |
21 |
--------------------------------------------------------------------------------
/example-timer/openFrameworks-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleExecutable
8 | ${EXECUTABLE_NAME}
9 | CFBundleIdentifier
10 | com.yourcompany.openFrameworks
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundlePackageType
14 | APPL
15 | CFBundleSignature
16 | ????
17 | CFBundleVersion
18 | 1.0
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Timer.cpp:
--------------------------------------------------------------------------------
1 | #include "Timer.h"
2 |
3 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
4 |
5 | Clock* Clock::system_clock = NULL;
6 |
7 | struct DefaultClock
8 | {
9 | Clock clock;
10 |
11 | void setup()
12 | {
13 | clock.makeSystemClock();
14 | ofAddListener(ofEvents().update, this, &DefaultClock::onUpdate);
15 | }
16 |
17 | void onUpdate(ofEventArgs &e)
18 | {
19 | Clock::getClock()->update();
20 | }
21 | };
22 |
23 | static DefaultClock default_clock;
24 |
25 | void Ticker::play()
26 | {
27 | Clock::regist(this);
28 | }
29 |
30 | void Ticker::stop()
31 | {
32 | Clock::unregist(this);
33 | }
34 |
35 | void initTimer()
36 | {
37 | default_clock.setup();
38 | }
39 |
40 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
--------------------------------------------------------------------------------
/example-composition/openFrameworks-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleExecutable
8 | ${EXECUTABLE_NAME}
9 | CFBundleIdentifier
10 | com.yourcompany.openFrameworks
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundlePackageType
14 | APPL
15 | CFBundleSignature
16 | ????
17 | CFBundleVersion
18 | 1.0
19 |
20 |
21 |
--------------------------------------------------------------------------------
/example-sequence/openFrameworks-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleExecutable
8 | ${EXECUTABLE_NAME}
9 | CFBundleIdentifier
10 | com.yourcompany.openFrameworks
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundlePackageType
14 | APPL
15 | CFBundleSignature
16 | ????
17 | CFBundleVersion
18 | 1.0
19 |
20 |
21 |
--------------------------------------------------------------------------------
/example-instance-manager/openFrameworks-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleExecutable
8 | ${EXECUTABLE_NAME}
9 | CFBundleIdentifier
10 | com.yourcompany.openFrameworks
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundlePackageType
14 | APPL
15 | CFBundleSignature
16 | ????
17 | CFBundleVersion
18 | 1.0
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Constants.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #define OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE namespace ofx { namespace AnimationPrimitives {
6 | #define OFX_ANIMATION_PRIMITIVES_END_NAMESPACE } }
7 |
8 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
9 |
10 | #pragma mark - Vector Helper
11 |
12 | template
13 | struct _$ : public vector
14 | {
15 | _$& operator,(const T& t)
16 | {
17 | this->push_back(t);
18 | return *this;
19 | }
20 | };
21 |
22 | #define $(type, ...) (_$(), __VA_ARGS__)
23 | #define $f(...) $(float, __VA_ARGS__)
24 | #define $i(...) $(int, __VA_ARGS__)
25 | #define $ui(...) $(unsigned int, __VA_ARGS__)
26 | #define $v2(...) $(ofVec2f, __VA_ARGS__)
27 | #define $v3(...) $(ofVec3f, __VA_ARGS__)
28 | #define $v4(...) $(ofVec4f, __VA_ARGS__)
29 |
30 | #pragma mark - misc
31 |
32 | namespace RTTI
33 | {
34 | typedef void* TypeID;
35 |
36 | template
37 | static TypeID getTypeID() {
38 | static size_t m = 0;
39 | return &m;
40 | }
41 | };
42 |
43 | template
44 | struct Ref_ : public ofPtr
45 | {
46 | Ref_() : ofPtr() {}
47 | Ref_(T *t) : ofPtr(t) {}
48 | Ref_(const ofPtr& o) : ofPtr(o) {}
49 | };
50 |
51 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
52 |
53 | namespace ofxAnimationPrimitives = ofx::AnimationPrimitives;
54 |
55 | #ifdef _WIN32
56 | #define isnormal(x) (_fpclass(x) == _FPCLASS_NN || _fpclass(x) == _FPCLASS_PN)
57 | #define isinf(x) (!_finite(x))
58 | #endif
--------------------------------------------------------------------------------
/example-timer/src/testApp.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 |
3 | #include "ofxAnimationPrimitives.h"
4 |
5 | ofxAnimationPrimitives::Timer oneshot;
6 | ofxAnimationPrimitives::Timer repeat;
7 |
8 | void testApp::onOneshotTimerEvent(int&)
9 | {
10 | ofBackground(255, 0, 0);
11 | cout << "oneshot: " << ofGetElapsedTimef() << endl;
12 | }
13 |
14 | void testApp::onRepeatTimerEvent(int&)
15 | {
16 | ofBackground(255);
17 | cout << "repeat: " << ofGetElapsedTimef() << endl;
18 | }
19 |
20 | //--------------------------------------------------------------
21 | void testApp::setup()
22 | {
23 | ofSetVerticalSync(true);
24 | ofSetFrameRate(60);
25 | ofBackground(0);
26 |
27 | ofAddListener(oneshot.timerEvent, this, &testApp::onOneshotTimerEvent);
28 | ofAddListener(repeat.timerEvent, this, &testApp::onRepeatTimerEvent);
29 |
30 | repeat.start(1, ofxAnimationPrimitives::Timer::FOREVER);
31 | oneshot.start(0.5, 1);
32 | }
33 |
34 | //--------------------------------------------------------------
35 | void testApp::update()
36 | {
37 | ofBackground(0);
38 | }
39 |
40 | //--------------------------------------------------------------
41 | void testApp::draw()
42 | {
43 | }
44 |
45 | //--------------------------------------------------------------
46 | void testApp::keyPressed(int key)
47 | {
48 | }
49 |
50 | //--------------------------------------------------------------
51 | void testApp::keyReleased(int key)
52 | {
53 |
54 | }
55 |
56 | //--------------------------------------------------------------
57 | void testApp::mouseMoved(int x, int y)
58 | {
59 | }
60 |
61 | //--------------------------------------------------------------
62 | void testApp::mouseDragged(int x, int y, int button)
63 | {
64 |
65 | }
66 |
67 | //--------------------------------------------------------------
68 | void testApp::mousePressed(int x, int y, int button)
69 | {
70 | oneshot.start(1, false);
71 | }
72 |
73 | //--------------------------------------------------------------
74 | void testApp::mouseReleased(int x, int y, int button)
75 | {
76 |
77 | }
78 |
79 | //--------------------------------------------------------------
80 | void testApp::windowResized(int w, int h)
81 | {
82 |
83 | }
84 |
85 | //--------------------------------------------------------------
86 | void testApp::gotMessage(ofMessage msg)
87 | {
88 |
89 | }
90 |
91 | //--------------------------------------------------------------
92 | void testApp::dragEvent(ofDragInfo dragInfo)
93 | {
94 |
95 | }
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/detail/coroutine.hpp:
--------------------------------------------------------------------------------
1 | //
2 | // coroutine.hpp
3 | // ~~~~~~~~~~~~~
4 | //
5 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 | //
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 | //
10 |
11 | #ifndef COROUTINE_HPP
12 | #define COROUTINE_HPP
13 |
14 | class coroutine
15 | {
16 | public:
17 | coroutine() : value_(0) {}
18 | bool is_child() const { return value_ < 0; }
19 | bool is_parent() const { return !is_child(); }
20 | bool is_complete() const { return value_ == -1; }
21 | private:
22 | friend class coroutine_ref;
23 | int value_;
24 | };
25 |
26 | class coroutine_ref
27 | {
28 | public:
29 | coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
30 | coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
31 | ~coroutine_ref() { if (!modified_) value_ = -1; }
32 | operator int() const { return value_; }
33 | int& operator=(int v) { modified_ = true; return value_ = v; }
34 | private:
35 | void operator=(const coroutine_ref&);
36 | int& value_;
37 | bool modified_;
38 | };
39 |
40 | #define CORO_REENTER(c) \
41 | switch (coroutine_ref _coro_value = c) \
42 | case -1: if (_coro_value) \
43 | { \
44 | goto terminate_coroutine; \
45 | terminate_coroutine: \
46 | _coro_value = -1; \
47 | goto bail_out_of_coroutine; \
48 | bail_out_of_coroutine: \
49 | break; \
50 | } \
51 | else case 0:
52 |
53 | #define CORO_YIELD_IMPL(n) \
54 | for (_coro_value = (n);;) \
55 | if (_coro_value == 0) \
56 | { \
57 | case (n): ; \
58 | break; \
59 | } \
60 | else \
61 | switch (_coro_value ? 0 : 1) \
62 | for (;;) \
63 | case -1: if (_coro_value) \
64 | goto terminate_coroutine; \
65 | else for (;;) \
66 | case 1: if (_coro_value) \
67 | goto bail_out_of_coroutine; \
68 | else case 0:
69 |
70 | #define CORO_FORK_IMPL(n) \
71 | for (_coro_value = -(n);; _coro_value = (n)) \
72 | if (_coro_value == (n)) \
73 | { \
74 | case -(n): ; \
75 | break; \
76 | } \
77 | else
78 |
79 | #if defined(_MSC_VER)
80 | # define CORO_YIELD CORO_YIELD_IMPL(__COUNTER__ + 1)
81 | # define CORO_FORK CORO_FORK_IMPL(__COUNTER__ + 1)
82 | #else // defined(_MSC_VER)
83 | # define CORO_YIELD CORO_YIELD_IMPL(__LINE__)
84 | # define CORO_FORK CORO_FORK_IMPL(__LINE__)
85 | #endif // defined(_MSC_VER)
86 |
87 | #endif // COROUTINE_HPP
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Timer.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #include "Constants.h"
6 |
7 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
8 |
9 | void initTimer();
10 |
11 | struct Ticker
12 | {
13 | virtual ~Ticker() { stop(); }
14 | virtual void tick(float delta) {}
15 |
16 | void play();
17 | void stop();
18 | };
19 |
20 | class Timer : protected Ticker
21 | {
22 | public:
23 |
24 | enum {
25 | FOREVER = -1
26 | };
27 |
28 | ofEvent timerEvent;
29 |
30 | Timer() : remain(0), duration(0), repeat(0) {}
31 |
32 | void start(float duration, int repeat = FOREVER)
33 | {
34 | this->duration = duration;
35 | this->remain = duration;
36 | this->repeat = repeat;
37 |
38 | count = 0;
39 |
40 | Ticker::play();
41 | }
42 |
43 | void stop()
44 | {
45 | Ticker::stop();
46 | }
47 |
48 | int getCount() const { return count; }
49 |
50 | protected:
51 |
52 | float remain, duration;
53 | int repeat, count;
54 |
55 | void tick(float delta)
56 | {
57 | remain -= delta;
58 |
59 | if (remain > 0) return;
60 |
61 | int N = count;
62 | ofNotifyEvent(timerEvent, N, this);
63 |
64 | count++;
65 |
66 | if (repeat < 0
67 | || count < repeat)
68 | {
69 | remain += duration;
70 | }
71 | else
72 | {
73 | stop();
74 | }
75 | }
76 | };
77 |
78 | #pragma mark -
79 |
80 | class Clock
81 | {
82 | public:
83 |
84 | virtual float getTime() const { return ofGetLastFrameTime(); }
85 |
86 | void update()
87 | {
88 | float dt = getTime();
89 | for (int i = 0; i < time_related().size(); i++)
90 | time_related()[i]->tick(dt);
91 | }
92 |
93 | void makeSystemClock() { system_clock = this; }
94 |
95 | public:
96 |
97 | static Clock* getClock() { return system_clock; }
98 |
99 | static void regist(Ticker *o)
100 | {
101 | if (find(time_related().begin(), time_related().end(), o) != time_related().end()) return;
102 | time_related().push_back(o);
103 | }
104 |
105 | static void unregist(Ticker *o)
106 | {
107 | vector::iterator it = remove(time_related().begin(), time_related().end(), o);
108 | time_related().erase(it, time_related().end());
109 | }
110 |
111 | protected:
112 |
113 | static Clock *system_clock;
114 | static vector& time_related()
115 | {
116 | static vector *p = new vector;
117 | return *p;
118 | }
119 | };
120 |
121 | class Tempo : public Clock
122 | {
123 | public:
124 |
125 | Tempo() : bpm(120) {}
126 |
127 | void setBpm(float v) { bpm = v; }
128 | float getBpm() const { return bpm; }
129 |
130 | float getTime() const
131 | {
132 | return ofGetLastFrameTime() * (bpm / 60);
133 | }
134 |
135 | protected:
136 |
137 | float bpm;
138 | };
139 |
140 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
141 |
--------------------------------------------------------------------------------
/example-composition/src/testApp.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 |
3 | #include "ofxAnimationPrimitives.h"
4 |
5 | class SubTopComposition : public ofxAnimationPrimitives::Composition
6 | {
7 | public:
8 |
9 | void draw()
10 | {
11 | ofSetColor(255, 0, 0, 255 * getAlpha());
12 | ofRect(0, 0, ofGetWidth(), ofGetHeight() / 2);
13 | }
14 | };
15 |
16 | class SubBottomComposition : public ofxAnimationPrimitives::Composition
17 | {
18 | public:
19 |
20 | void draw()
21 | {
22 | ofSetColor(0, 255, 0, 255 * getAlpha());
23 | ofRect(0, ofGetHeight() / 2, ofGetWidth(), ofGetHeight() / 2);
24 | }
25 | };
26 |
27 | class MainComposition : public ofxAnimationPrimitives::Composition
28 | {
29 | public:
30 |
31 | MainComposition()
32 | {
33 | // show SubTopComposition after MainComposition apper
34 | on(DID_APPEAR, New(), Duration(1, 3, 1));
35 |
36 | // show SubBottomComposition after 3 sec
37 | at(3, New(), Duration(1, 3, 1));
38 | }
39 |
40 | void draw()
41 | {
42 | ofSetColor(255 * getAlpha());
43 | ofRect(0, 0, ofGetWidth(), ofGetHeight());
44 | }
45 | };
46 |
47 | MainComposition::Ref comp;
48 |
49 | //--------------------------------------------------------------
50 | void testApp::setup()
51 | {
52 | ofSetVerticalSync(true);
53 | ofSetFrameRate(60);
54 | ofBackground(0);
55 |
56 | // create new composition
57 | comp = ofxAnimationPrimitives::Composition::New();
58 |
59 | // start MainComposition, fadein in 5sec, total duration in 10 sec, fadeout in 3 sec.
60 | comp->play(5, 10, 3);
61 | }
62 |
63 | //--------------------------------------------------------------
64 | void testApp::update()
65 | {
66 | ofxAnimationPrimitives::CompositionRunner::defaultRunner().update();
67 | }
68 |
69 | //--------------------------------------------------------------
70 | void testApp::draw()
71 | {
72 | ofxAnimationPrimitives::CompositionRunner::defaultRunner().draw();
73 | }
74 |
75 | //--------------------------------------------------------------
76 | void testApp::keyPressed(int key)
77 | {
78 | // replay MainComposition
79 | comp->reset();
80 | comp->play();
81 | }
82 |
83 | //--------------------------------------------------------------
84 | void testApp::keyReleased(int key)
85 | {
86 |
87 | }
88 |
89 | //--------------------------------------------------------------
90 | void testApp::mouseMoved(int x, int y)
91 | {
92 | }
93 |
94 | //--------------------------------------------------------------
95 | void testApp::mouseDragged(int x, int y, int button)
96 | {
97 |
98 | }
99 |
100 | //--------------------------------------------------------------
101 | void testApp::mousePressed(int x, int y, int button)
102 | {
103 | }
104 |
105 | //--------------------------------------------------------------
106 | void testApp::mouseReleased(int x, int y, int button)
107 | {
108 | }
109 |
110 | //--------------------------------------------------------------
111 | void testApp::windowResized(int w, int h)
112 | {
113 | }
114 |
115 | //--------------------------------------------------------------
116 | void testApp::gotMessage(ofMessage msg)
117 | {
118 | }
119 |
120 | //--------------------------------------------------------------
121 | void testApp::dragEvent(ofDragInfo dragInfo)
122 | {
123 | }
--------------------------------------------------------------------------------
/example-instance-manager/src/testApp.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 |
3 | #include "ofxAnimationPrimitives.h"
4 |
5 | int num_instance = 0;
6 |
7 | class MyInstance : public ofxAnimationPrimitives::Instance
8 | {
9 | public:
10 |
11 | ofVec2f pos;
12 | ofColor color;
13 | float phase;
14 |
15 | MyInstance(float x, float y)
16 | {
17 | pos.set(x, y);
18 | color = ofColor::fromHsb(ofRandom(255), 255, 255);
19 | phase = 0;
20 |
21 | cout << "created: " << ++num_instance << endl;
22 | }
23 |
24 | ~MyInstance()
25 | {
26 | cout << "deleted: " << --num_instance << endl;
27 | }
28 |
29 | void update()
30 | {
31 | phase += 0.05;
32 | }
33 |
34 | void draw()
35 | {
36 | ofFill();
37 | ofSetColor(color);
38 | ofCircle(pos, (30 + sin(phase * PI) * 10) * getLife());
39 | }
40 | };
41 |
42 | class MyInfinityInstance : public ofxAnimationPrimitives::Instance
43 | {
44 | public:
45 |
46 | ofVec2f pos;
47 | ofColor color;
48 | float phase;
49 | float mass;
50 |
51 | MyInfinityInstance(float x, float y)
52 | {
53 | pos.set(x, y);
54 | color = ofColor::fromHsb(ofRandom(255), 255, 255);
55 | phase = 0;
56 | mass = ofRandom(0.01, 0.1);
57 |
58 | cout << "created: " << ++num_instance << endl;
59 | }
60 |
61 | ~MyInfinityInstance()
62 | {
63 | cout << "deleted: " << --num_instance << endl;
64 | }
65 |
66 | void update()
67 | {
68 | phase += 0.01;
69 | pos += (ofVec2f(ofGetMouseX(), ofGetMouseY()) - pos) * mass;
70 | }
71 |
72 | void draw()
73 | {
74 | ofNoFill();
75 | ofSetColor(color);
76 | ofCircle(pos, (100 + sin(phase * PI) * 30) * getLife());
77 | }
78 | };
79 |
80 |
81 | ofxAnimationPrimitives::InstanceManager manager;
82 |
83 | //--------------------------------------------------------------
84 | void testApp::setup()
85 | {
86 | ofSetVerticalSync(true);
87 | ofSetFrameRate(60);
88 | ofBackground(0);
89 | }
90 |
91 | //--------------------------------------------------------------
92 | void testApp::update()
93 | {
94 | manager.update();
95 | }
96 |
97 | //--------------------------------------------------------------
98 | void testApp::draw()
99 | {
100 | manager.draw();
101 | }
102 |
103 | //--------------------------------------------------------------
104 | void testApp::keyPressed(int key)
105 | {
106 | manager.release(3);
107 | }
108 |
109 | //--------------------------------------------------------------
110 | void testApp::keyReleased(int key)
111 | {
112 |
113 | }
114 |
115 | //--------------------------------------------------------------
116 | void testApp::mouseMoved(int x, int y)
117 | {
118 | manager.createInstance(x, y)->play(4);
119 | }
120 |
121 | //--------------------------------------------------------------
122 | void testApp::mouseDragged(int x, int y, int button)
123 | {
124 |
125 | }
126 |
127 | //--------------------------------------------------------------
128 | void testApp::mousePressed(int x, int y, int button)
129 | {
130 | manager.createInstance(x, y)->playInfinity();
131 | }
132 |
133 | //--------------------------------------------------------------
134 | void testApp::mouseReleased(int x, int y, int button)
135 | {
136 |
137 | }
138 |
139 | //--------------------------------------------------------------
140 | void testApp::windowResized(int w, int h)
141 | {
142 |
143 | }
144 |
145 | //--------------------------------------------------------------
146 | void testApp::gotMessage(ofMessage msg)
147 | {
148 |
149 | }
150 |
151 | //--------------------------------------------------------------
152 | void testApp::dragEvent(ofDragInfo dragInfo)
153 | {
154 |
155 | }
--------------------------------------------------------------------------------
/example-sequence/src/testApp.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 |
3 | #include "ofxAnimationPrimitives.h"
4 |
5 | ofxAnimationPrimitives::Sequence seq;
6 |
7 | class MyClip : public ofxAnimationPrimitives::Sequence::Clip
8 | {
9 | public:
10 |
11 | string name;
12 | float start_time;
13 | float duration;
14 |
15 | ofVec2f pos;
16 |
17 | MyClip(string name, float start_time, float duration)
18 | : name(name), start_time(start_time), duration(duration) {}
19 |
20 | float getStartTime() const { return start_time; }
21 | float getDuration() const { return duration; }
22 |
23 |
24 |
25 | void onStart()
26 | {
27 | cout << "start: " << name << endl;
28 |
29 | pos.x = ofRandomWidth();
30 | pos.y = ofRandomHeight();
31 | }
32 |
33 | void onEnd()
34 | {
35 | cout << "end: " << name << endl;
36 | }
37 |
38 | void draw()
39 | {
40 | ofSetColor(255);
41 | ofDrawBitmapString(name, pos);
42 | ofRect(pos.x, pos.y, 100, 100);
43 | }
44 |
45 | };
46 |
47 | class MyRedClip : public MyClip
48 | {
49 | public:
50 |
51 | MyRedClip(string name, float start_time, float duration) : MyClip(name, start_time, duration) {}
52 |
53 | void onStart()
54 | {
55 | cout << "red start: " << name << endl;
56 |
57 | pos.x = ofRandomWidth();
58 | pos.y = ofRandomHeight();
59 | }
60 |
61 | void onEnd()
62 | {
63 | cout << "red end: " << name << endl;
64 | }
65 |
66 | void draw()
67 | {
68 | ofSetColor(255, 0, 0);
69 | ofDrawBitmapString(name, pos);
70 | ofRect(pos.x, pos.y, 100, 100);
71 | }
72 | };
73 |
74 | //--------------------------------------------------------------
75 | void testApp::setup()
76 | {
77 | ofSetVerticalSync(true);
78 | ofSetFrameRate(60);
79 | ofBackground(0);
80 |
81 | seq.addClip(new MyClip("clip0", 1, 1));
82 | seq.addClip(new MyClip("clip1", 1.5, 1));
83 | seq.addClip(new MyClip("clip2", 2, 1));
84 |
85 | seq.addClip(new MyRedClip("clip3", 3, 2));
86 | }
87 |
88 | //--------------------------------------------------------------
89 | void testApp::update()
90 | {
91 | seq.setTime(fmodf(ofGetElapsedTimef(), 4));
92 | }
93 |
94 | //--------------------------------------------------------------
95 | void testApp::draw()
96 | {
97 | ofSetColor(255);
98 | ofDrawBitmapString(ofToString(fmodf(ofGetElapsedTimef(), 4), 1), 10, 20);
99 |
100 | const vector& clips = seq.getActiveClips();
101 | for (int i = 0; i < clips.size(); i++)
102 | {
103 | clips[i]->draw();
104 | }
105 | }
106 |
107 | //--------------------------------------------------------------
108 | void testApp::keyPressed(int key)
109 | {
110 | seq.setTime(0);
111 | }
112 |
113 | //--------------------------------------------------------------
114 | void testApp::keyReleased(int key)
115 | {
116 |
117 | }
118 |
119 | //--------------------------------------------------------------
120 | void testApp::mouseMoved(int x, int y)
121 | {
122 | }
123 |
124 | //--------------------------------------------------------------
125 | void testApp::mouseDragged(int x, int y, int button)
126 | {
127 |
128 | }
129 |
130 | //--------------------------------------------------------------
131 | void testApp::mousePressed(int x, int y, int button)
132 | {
133 | }
134 |
135 | //--------------------------------------------------------------
136 | void testApp::mouseReleased(int x, int y, int button)
137 | {
138 |
139 | }
140 |
141 | //--------------------------------------------------------------
142 | void testApp::windowResized(int w, int h)
143 | {
144 |
145 | }
146 |
147 | //--------------------------------------------------------------
148 | void testApp::gotMessage(ofMessage msg)
149 | {
150 |
151 | }
152 |
153 | //--------------------------------------------------------------
154 | void testApp::dragEvent(ofDragInfo dragInfo)
155 | {
156 |
157 | }
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Lerp.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Timer.h"
4 |
5 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
6 |
7 | template
8 | class Lerp : public Ticker
9 | {
10 | public:
11 |
12 | Lerp() : value(0), target(0), speed(0.05) { play(); }
13 | Lerp(const T& v) : value(v), target(v), speed(0.05) { play(); }
14 | ~Lerp() { stop(); }
15 | Lerp(const Lerp& copy) { *this = copy; play(); }
16 |
17 | Lerp& operator=(const Lerp& copy)
18 | {
19 | value = copy.value;
20 | target = copy.target;
21 | speed = copy.speed;
22 | return *this;
23 | }
24 |
25 | inline Lerp& operator=(const T& v) { target = v; }
26 | inline operator const T&() const { return value; }
27 |
28 | inline void setValue(const T& v, bool override = false) { override ? value = v : target = v; }
29 | inline const T& getValue() const { return value; }
30 |
31 | inline void setSpeed(float v) { speed = ofClamp(v, 0, 1); }
32 | inline float getSpeed() const { return speed; }
33 |
34 | inline Lerp& operator+=(const T& v) { target += v; return *this; }
35 | inline Lerp& operator-=(const T& v) { target -= v; return *this; }
36 | inline Lerp& operator*=(const T& v) { target *= v; return *this; }
37 | inline Lerp& operator/=(const T& v) { target /= v; return *this; }
38 |
39 | protected:
40 |
41 | T value, target;
42 | float speed;
43 |
44 | void tick(float delta)
45 | {
46 | value += (target - value) * speed;
47 | }
48 | };
49 |
50 | class Line : public Ticker
51 | {
52 | public:
53 |
54 | Line()
55 | :value(0)
56 | ,value_delta(0)
57 | ,curtime(0)
58 | {}
59 |
60 | /*
61 | Max/MSP style lamp generator
62 |
63 | line.set("100 1 0 1"); // Value changes to 100 in 1 sec, then to 0 in 1 sec
64 | line.play();
65 |
66 | // or
67 |
68 | line.play("0, 1000 0.5 -1000 0.5"); // Initialize value with comma and play immediately
69 | */
70 | bool setup(const string& line_string)
71 | {
72 | string s = line_string;
73 |
74 | string::size_type n = s.find(",");
75 | if (n != string::npos)
76 | {
77 | value = ofToFloat(s.substr(0, n));
78 | s = s.substr(n + 1);
79 | }
80 |
81 | vector e = ofSplitString(s, " ", true, true);
82 | if ((e.size() % 2) != 0) return false;
83 |
84 | arr.clear();
85 |
86 | for (int i = 0; i < e.size(); i += 2)
87 | {
88 | TimeValue TV;
89 | TV.value = ofToFloat(e[i]);
90 | TV.time = ofToFloat(e[i + 1]);
91 | arr.push_back(TV);
92 | }
93 |
94 | return true;
95 | }
96 |
97 | void play()
98 | {
99 | if (arr.empty())
100 | {
101 | ofLogError("Line") << "setup first";
102 | return;
103 | }
104 |
105 | curtime = 0;
106 |
107 | iter = arr.begin();
108 | updateDelta();
109 |
110 | Ticker::play();
111 | }
112 |
113 | void play(const string& line_string)
114 | {
115 | if (setup(line_string))
116 | play();
117 | }
118 |
119 | inline operator float() const { return value; }
120 | inline float getValue() const { return value; }
121 |
122 | protected:
123 |
124 | float value;
125 | float value_delta;
126 |
127 | float curtime;
128 |
129 | struct TimeValue {
130 | float time;
131 | float value;
132 | };
133 |
134 | vector arr;
135 | vector::iterator iter;
136 |
137 | void updateDelta()
138 | {
139 | value_delta = (iter->value - value) / (iter->time - curtime);
140 | }
141 |
142 | void tick(float delta)
143 | {
144 | if (iter == arr.end()) return;
145 |
146 | value += value_delta * delta;
147 |
148 | if (curtime + delta < iter->time)
149 | {
150 | curtime += delta;
151 | }
152 | else
153 | {
154 | curtime = 0;
155 | value = iter->value;
156 |
157 | iter++;
158 | updateDelta();
159 | }
160 | }
161 | };
162 |
163 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
164 |
--------------------------------------------------------------------------------
/example-scene/src/testApp.cpp:
--------------------------------------------------------------------------------
1 | #include "testApp.h"
2 |
3 | #include "ofxAnimationPrimitives.h"
4 |
5 | ofxAnimationPrimitives::SceneManager SM;
6 |
7 | class Scene0 : public ofxAnimationPrimitives::Scene
8 | {
9 | public:
10 |
11 | OFX_ANIMATION_PRIMITIVES_DEFINE_SCENE(Scene0);
12 |
13 | Scene0(const int arg0)
14 | {
15 | cout << "Scene0: " << arg0 << endl;
16 | }
17 |
18 | void update()
19 | {
20 | cout << __PRETTY_FUNCTION__ << endl;
21 | }
22 |
23 | void draw()
24 | {
25 | ofSetColor(100, 0, 0, 255 * getAlpha());
26 | ofRect(0, 0, ofGetWidth() / 2, ofGetHeight());
27 |
28 | ofSetColor(255);
29 | ofDrawBitmapString("SCENE0 alpha:" + ofToString(getAlpha(), 3), 10, 20);
30 | }
31 |
32 | void viewWillAppear() { cout << __PRETTY_FUNCTION__ << endl; }
33 | void viewDidAppear() { cout << __PRETTY_FUNCTION__ << endl; }
34 | void viewWillDisappear() { cout << __PRETTY_FUNCTION__ << endl; }
35 | void viewDidDisappear() { cout << __PRETTY_FUNCTION__ << endl; }
36 | };
37 |
38 | class Scene1 : public ofxAnimationPrimitives::Scene
39 | {
40 | public:
41 |
42 | OFX_ANIMATION_PRIMITIVES_DEFINE_SCENE(Scene1);
43 |
44 | Scene1(const string& arg0, float arg1)
45 | {
46 | cout << "Scene1: " << arg0 << " " << arg1 << endl;
47 | }
48 |
49 | void update()
50 | {
51 | cout << __PRETTY_FUNCTION__ << endl;
52 | }
53 |
54 | void draw()
55 | {
56 | ofSetColor(0, 100, 0, 255 * getAlpha());
57 | ofRect(ofGetWidth() / 2, 0, ofGetWidth() / 2, ofGetHeight());
58 |
59 | ofSetColor(255);
60 | ofDrawBitmapString("SCENE1 alpha:" + ofToString(getAlpha(), 3), ofGetWidth() / 2 + 10, 20);
61 | }
62 |
63 | void viewWillAppear() { cout << __PRETTY_FUNCTION__ << endl; }
64 | void viewDidAppear() { cout << __PRETTY_FUNCTION__ << endl; }
65 | void viewWillDisappear() { cout << __PRETTY_FUNCTION__ << endl; }
66 | void viewDidDisappear() { cout << __PRETTY_FUNCTION__ << endl; }
67 | };
68 |
69 | //--------------------------------------------------------------
70 | void testApp::setup()
71 | {
72 | ofSetVerticalSync(true);
73 | ofSetFrameRate(60);
74 | ofBackground(0);
75 |
76 | SM.addScene(42);
77 | SM.addScene("'arg0 string'", PI);
78 |
79 | SM.changeScene("Scene0");
80 | }
81 |
82 | //--------------------------------------------------------------
83 | void testApp::update()
84 | {
85 | ofBackground(0);
86 |
87 | SM.update();
88 | }
89 |
90 | //--------------------------------------------------------------
91 | void testApp::draw()
92 | {
93 | SM.draw();
94 | }
95 |
96 | //--------------------------------------------------------------
97 | void testApp::keyPressed(int key)
98 | {
99 | if (key == '1') SM.changeScene(0.1);
100 | if (key == '2') SM.changeScene();
101 | }
102 |
103 | //--------------------------------------------------------------
104 | void testApp::keyReleased(int key)
105 | {
106 |
107 | }
108 |
109 | //--------------------------------------------------------------
110 | void testApp::mouseMoved(int x, int y)
111 | {
112 | }
113 |
114 | //--------------------------------------------------------------
115 | void testApp::mouseDragged(int x, int y, int button)
116 | {
117 |
118 | }
119 |
120 | //--------------------------------------------------------------
121 | void testApp::mousePressed(int x, int y, int button)
122 | {
123 | }
124 |
125 | //--------------------------------------------------------------
126 | void testApp::mouseReleased(int x, int y, int button)
127 | {
128 |
129 | }
130 |
131 | //--------------------------------------------------------------
132 | void testApp::windowResized(int w, int h)
133 | {
134 |
135 | }
136 |
137 | //--------------------------------------------------------------
138 | void testApp::gotMessage(ofMessage msg)
139 | {
140 |
141 | }
142 |
143 | //--------------------------------------------------------------
144 | void testApp::dragEvent(ofDragInfo dragInfo)
145 | {
146 |
147 | }
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Sequence.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Constants.h"
4 |
5 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
6 |
7 | template
8 | class Sequence_
9 | {
10 | public:
11 |
12 | class Clip
13 | {
14 | public:
15 |
16 | typedef Ref_ Ref;
17 |
18 | Clip() {}
19 | virtual ~Clip() {}
20 |
21 | virtual timestamp_t getStartTime() const { return 0; }
22 | virtual timestamp_t getDuration() const { return 0; }
23 |
24 | virtual void update() {}
25 | virtual void draw() {}
26 |
27 | public:
28 |
29 | virtual void onStart() {}
30 | virtual void onEnd() {}
31 | };
32 |
33 | public:
34 |
35 | Sequence_() : last_time(0) {}
36 | ~Sequence_() { clear(); }
37 |
38 | inline bool hasClip(typename Clip::Ref o)
39 | {
40 | return find(clips.begin(), clips.end(), o) != clips.end();
41 | }
42 |
43 | void addClip(typename Clip::Ref o)
44 | {
45 | if (hasClip(o)) return;
46 | clips.push_back(o);
47 |
48 | clip_map.insert(make_pair(o->getStartTime(), o));
49 | }
50 |
51 | void removeClip(typename Clip::Ref o)
52 | {
53 | if (!hasClip(o)) return;
54 | clips.erase(remove(clips.begin(), clips.end(), o), clips.end());
55 |
56 | // remove from clip_map
57 | int n = clip_map.count(o->getStartTime());
58 | if (n == 0) return;
59 |
60 | typename ClipMap::iterator it = clip_map.find(o->getStartTime());
61 | for (int i = 0; i < n; i++)
62 | {
63 | if (it->second == o)
64 | {
65 | clip_map.erase(it);
66 | break;
67 | }
68 | it++;
69 | }
70 | }
71 |
72 | void setTime(timestamp_t t)
73 | {
74 | timestamp_t begin = last_time;
75 | timestamp_t end = t;
76 |
77 | if (begin != end)
78 | updateInRange(begin, end);
79 |
80 | last_time = t;
81 | }
82 |
83 | const vector& getActiveClips() const { return active_clips_arr; }
84 | const vector& getAllClips() const { return clips; }
85 |
86 | void clear()
87 | {
88 | clips.clear();
89 | active_clips.clear();
90 | clip_map.clear();
91 | }
92 |
93 | protected:
94 |
95 | timestamp_t last_time;
96 |
97 | vector clips;
98 | set active_clips;
99 | vector active_clips_arr;
100 |
101 | typedef multimap ClipMap;
102 | ClipMap clip_map;
103 |
104 | void updateInRange(timestamp_t start, timestamp_t end)
105 | {
106 | if (start > end)
107 | {
108 | // remove future clip if play backwards
109 |
110 | typename set::iterator it = active_clips.begin();
111 | while (it != active_clips.end())
112 | {
113 | typename Clip::Ref o = *it;
114 |
115 | if (o->getStartTime() >= end)
116 | {
117 | o->onEnd();
118 | active_clips.erase(it++);
119 | }
120 | else it++;
121 | }
122 |
123 | return;
124 | }
125 |
126 | // Remove Pass 1: Find and remove out range clips
127 | {
128 | typename set::iterator it = active_clips.begin();
129 | while (it != active_clips.end())
130 | {
131 | typename Clip::Ref o = *it;
132 |
133 | if ((o->getStartTime() + o->getDuration()) <= end)
134 | {
135 | o->onEnd();
136 | active_clips.erase(it++);
137 | }
138 | else it++;
139 | }
140 | }
141 |
142 | // Find new in range clips
143 | {
144 | typename ClipMap::iterator it = clip_map.lower_bound(start);
145 | typename ClipMap::iterator end_it = clip_map.lower_bound(end);
146 |
147 | while (it != end_it)
148 | {
149 | typename Clip::Ref o = it->second;
150 | o->onStart();
151 |
152 | if (o->getDuration() > 0)
153 | {
154 | active_clips.insert(o);
155 | }
156 | else
157 | {
158 | o->onEnd();
159 | }
160 |
161 | it++;
162 | }
163 | }
164 |
165 | // Remove Pass 2: Find and remove in new clips
166 | {
167 | typename set::iterator it = active_clips.begin();
168 | while (it != active_clips.end())
169 | {
170 | typename Clip::Ref o = *it;
171 |
172 | if ((o->getStartTime() + o->getDuration()) <= end)
173 | {
174 | o->onEnd();
175 | active_clips.erase(it++);
176 | }
177 | else it++;
178 | }
179 | }
180 |
181 | active_clips_arr.assign(active_clips.begin(), active_clips.end());
182 | }
183 | };
184 |
185 | typedef Sequence_ Sequence;
186 |
187 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
188 |
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Replicator.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #include "Constants.h"
6 |
7 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
8 |
9 | template
10 | struct Replicator_
11 | {
12 | typedef Replicator_ Replicator;
13 |
14 | // base classes
15 | class Node;
16 | class Operator;
17 | class Layout;
18 |
19 | // layout
20 | class Linear;
21 | class Fill2D;
22 | class Grid2D;
23 | class Grid3D;
24 |
25 | inline size_t size() const { return children.size(); }
26 | inline Node& get(size_t i) { return children[i]; }
27 | inline Node& operator[](size_t i) { return children[i]; }
28 |
29 | void resize(size_t num)
30 | {
31 | children.resize(num);
32 | for (int i = 0; i < children.size(); i++)
33 | {
34 | children[i].id = i;
35 | }
36 | }
37 |
38 | protected:
39 |
40 | vector children;
41 | };
42 |
43 | #pragma mark - Base Classes
44 |
45 | template
46 | class Replicator_::Operator
47 | {
48 | public:
49 |
50 | virtual void operator()(Replicator &repl) {}
51 |
52 | Replicator& apply(Replicator &repl)
53 | {
54 | this->operator()(repl);
55 | return repl;
56 | }
57 | };
58 |
59 | template
60 | struct Replicator_::Node
61 | {
62 | friend struct Replicator_;
63 |
64 | public:
65 |
66 | struct Operator;
67 |
68 | T param;
69 |
70 | ofMatrix4x4 m;
71 |
72 | inline int getID() const { return id; }
73 | inline void beginTransform()
74 | {
75 | ofPushMatrix();
76 | ofMultMatrix(m);
77 | }
78 |
79 | inline void endTransform()
80 | {
81 | ofPopMatrix();
82 | }
83 |
84 | private:
85 |
86 | int id;
87 |
88 | };
89 |
90 | template
91 | class Replicator_::Node::Operator
92 | {
93 | public:
94 |
95 | virtual void operator()(Node &node) {}
96 |
97 | Replicator& apply(Replicator &repl)
98 | {
99 | for (int i = 0; i < repl.size(); i++)
100 | this->operator()(repl[i]);
101 |
102 | return repl;
103 | }
104 | };
105 |
106 | #pragma mark - Layout
107 |
108 | template
109 | struct Replicator_::Linear : public Operator
110 | {
111 | int count;
112 | ofVec3f offset;
113 |
114 | Linear(int count, ofVec3f offset) : count(count), offset(offset) {}
115 |
116 | void operator()(Replicator &repl)
117 | {
118 | repl.resize(count);
119 |
120 | float d = 0;
121 | for (int i = 0; i < repl.size(); i++)
122 | {
123 | repl[i].m.setTranslation(i * offset);
124 | }
125 | }
126 | };
127 |
128 | template
129 | struct Replicator_::Fill2D : public Operator
130 | {
131 | int offsetX, offsetY;
132 | float sizeX, sizeY;
133 |
134 | Fill2D(int offsetX, int offsetY, float sizeX, float sizeY) : offsetX(offsetX), offsetY(offsetY), sizeX(sizeX), sizeY(sizeY) {}
135 |
136 | void operator()(Replicator &repl)
137 | {
138 | const int nX = sizeX / offsetX + 1;
139 | const int nY = sizeY / offsetY + 1;
140 |
141 | const int num = nX * nY;
142 | repl.resize(num);
143 |
144 | int idx = 0;
145 | for (int y = 0; y < nY; y++)
146 | {
147 | for (int x = 0; x < nX; x++)
148 | {
149 | repl[idx].m.setTranslation(x * offsetX, y * offsetY, 0);
150 | repl[idx].param.x = x;
151 | repl[idx].param.y = y;
152 |
153 | idx++;
154 | }
155 | }
156 | }
157 | };
158 |
159 | template
160 | struct Replicator_::Grid2D : public Operator
161 | {
162 | int X, Y;
163 | float sizeX, sizeY;
164 |
165 | Grid2D(int X, int Y, float sizeX, float sizeY) : X(X), Y(Y), sizeX(sizeX), sizeY(sizeY) {}
166 |
167 | void operator()(Replicator &repl)
168 | {
169 | const int X_minus_one = X - 1;
170 | const int Y_minus_one = Y - 1;
171 |
172 | const int num = X * Y;
173 | repl.resize(num);
174 |
175 | int idx = 0;
176 | for (int y = 0; y < Y; y++)
177 | {
178 | for (int x = 0; x < X; x++)
179 | {
180 | float xx = ofMap(x, 0, X_minus_one, 0, 1) * sizeX;
181 | float yy = ofMap(y, 0, Y_minus_one, 0, 1) * sizeY;
182 |
183 | repl[idx].m.setTranslation(xx, yy, 0);
184 | repl[idx].param.x = x;
185 | repl[idx].param.y = y;
186 |
187 | idx++;
188 | }
189 | }
190 | }
191 | };
192 |
193 | template
194 | struct Replicator_::Grid3D : public Operator
195 | {
196 | int X, Y, Z;
197 | float sizeX, sizeY, sizeZ;
198 |
199 | Grid3D(int X, int Y, int Z, float sizeX, float sizeY, float sizeZ) : X(X), Y(Y), Z(Z), sizeX(sizeX), sizeY(sizeY), sizeZ(sizeZ) {}
200 |
201 | void operator()(Replicator &repl)
202 | {
203 | const int X_minus_one = X - 1;
204 | const int Y_minus_one = Y - 1;
205 | const int Z_minus_one = Z - 1;
206 |
207 | const int num = X * Y * Z;
208 | repl.resize(num);
209 |
210 | int idx = 0;
211 |
212 | for (int z = 0; z < Z; z++)
213 | {
214 | for (int y = 0; y < Y; y++)
215 | {
216 | for (int x = 0; x < X; x++)
217 | {
218 | float xx = ofMap(x, 0, X_minus_one, 0, 1) * sizeX;
219 | float yy = ofMap(y, 0, Y_minus_one, 0, 1) * sizeY;
220 | float zz = ofMap(z, 0, Z_minus_one, 0, 1) * sizeZ;
221 |
222 | repl[idx].m.setTranslation(xx, yy, zz);
223 | repl[idx].param.x = x;
224 | repl[idx].param.y = y;
225 | repl[idx].param.z = z;
226 |
227 | idx++;
228 | }
229 | }
230 | }
231 | }
232 | };
233 |
234 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Easing.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Constants.h"
4 |
5 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
6 |
7 | namespace Easing {
8 |
9 | struct Back
10 | {
11 | inline static float easeIn(const float t)
12 | {
13 | return 3 * t * t * t - 2 * t * t;
14 | }
15 |
16 | inline static float easeOut(const float t)
17 | {
18 | return 1. - easeIn(1. - t);
19 | }
20 |
21 | inline static float easeInOut(const float t)
22 | {
23 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
24 | }
25 | };
26 |
27 | struct Bounce
28 | {
29 |
30 | inline static float easeIn(const float t)
31 | {
32 | static const float DH = 1. / 22.;
33 | static const float D1 = 1. / 11.;
34 | static const float D2 = 2. / 11.;
35 | static const float D3 = 3. / 11.;
36 | static const float D4 = 4. / 11.;
37 | static const float D5 = 5. / 11.;
38 | static const float D7 = 7. / 11.;
39 | static const float IH = 1. / DH;
40 | static const float I1 = 1. / D1;
41 | static const float I2 = 1. / D2;
42 | static const float I4D = 1. / D4 / D4;
43 |
44 | float s;
45 | if (t < D1)
46 | {
47 | s = t - DH;
48 | s = DH - s * s * IH;
49 | }
50 | else if (t < D3)
51 | {
52 | s = t - D2;
53 | s = D1 - s * s * I1;
54 | }
55 | else if (t < D7)
56 | {
57 | s = t - D5;
58 | s = D2 - s * s * I2;
59 | }
60 | else
61 | {
62 | s = t - 1;
63 | s = 1 - s * s * I4D;
64 | }
65 | return s;
66 | }
67 |
68 | inline static float easeOut(const float t)
69 | {
70 | return 1. - easeIn(1. - t);
71 | }
72 |
73 | inline static float easeInOut(const float t)
74 | {
75 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
76 | }
77 |
78 | };
79 |
80 | struct Circ
81 | {
82 | inline static float easeIn(const float t)
83 | {
84 | return 1.0 - sqrtf(1.0 - t * t);
85 | }
86 |
87 | inline static float easeOut(const float t)
88 | {
89 | return 1. - easeIn(1. - t);
90 | }
91 |
92 | inline static float easeInOut(const float t)
93 | {
94 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
95 | }
96 | };
97 |
98 | struct Cubic
99 | {
100 | inline static float easeIn(const float t)
101 | {
102 | return t * t * t;
103 | }
104 |
105 | inline static float easeOut(const float t)
106 | {
107 | return 1. - easeIn(1. - t);
108 | }
109 |
110 | inline static float easeInOut(const float t)
111 | {
112 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
113 | }
114 | };
115 |
116 | struct Elastic
117 | {
118 | inline static float easeIn(const float t)
119 | {
120 | return 1.0 - easeOut(1.0 - t);
121 | }
122 |
123 | inline static float easeOut(const float t)
124 | {
125 | float s = 1 - t;
126 | return 1 - powf(s, 8) + sinf(t * t * 6 * PI) * s * s;
127 | }
128 |
129 | inline static float easeInOut(const float t)
130 | {
131 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
132 | }
133 | };
134 |
135 | struct Expo
136 | {
137 | inline static float easeIn(const float t)
138 | {
139 | return powf(2, 10 * (t - 1));
140 | }
141 |
142 | inline static float easeOut(const float t)
143 | {
144 | return 1.0 - powf(2, -10 * t);
145 | }
146 |
147 | inline static float easeInOut(const float t)
148 | {
149 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
150 | }
151 | };
152 |
153 | struct Linear
154 | {
155 | inline static float easeIn(const float t)
156 | {
157 | return t;
158 | }
159 |
160 | inline static float easeOut(const float t)
161 | {
162 | return t;
163 | }
164 |
165 | inline static float easeInOut(const float t)
166 | {
167 | return t;
168 | }
169 | };
170 |
171 | struct Quad
172 | {
173 | inline static float easeIn(const float t)
174 | {
175 | return t * t;
176 | }
177 |
178 | inline static float easeOut(const float t)
179 | {
180 | return 1. - easeIn(1. - t);
181 | }
182 |
183 | inline static float easeInOut(const float t)
184 | {
185 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
186 | }
187 | };
188 |
189 | struct Quart
190 | {
191 | inline static float easeIn(const float t)
192 | {
193 | return t * t * t * t;
194 | }
195 |
196 | inline static float easeOut(const float t)
197 | {
198 | return 1. - easeIn(1. - t);
199 | }
200 |
201 | inline static float easeInOut(const float t)
202 | {
203 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
204 | }
205 | };
206 |
207 | struct Quint
208 | {
209 | inline static float easeIn(const float t)
210 | {
211 | return t * t * t * t * t;
212 | }
213 |
214 | inline static float easeOut(const float t)
215 | {
216 | return 1. - easeIn(1. - t);
217 | }
218 |
219 | inline static float easeInOut(const float t)
220 | {
221 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
222 | }
223 | };
224 |
225 | struct Sine
226 | {
227 | inline static float easeIn(const float t)
228 | {
229 | return 1.0 - cosf(t * HALF_PI);
230 | }
231 |
232 | inline static float easeOut(const float t)
233 | {
234 | return 1. - easeIn(1. - t);
235 | }
236 |
237 | inline static float easeInOut(const float t)
238 | {
239 | return (t < 0.5) ? easeIn(t * 2.0) * 0.5 : 1 - easeIn(2.0 - t * 2.0) * 0.5;
240 | }
241 | };
242 |
243 | }
244 |
245 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/CommandQueue.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Constants.h"
4 |
5 | #include
6 |
7 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
8 |
9 | struct CommandQueueCommand;
10 | struct JoinCommand;
11 | struct UntilCommand;
12 |
13 | struct Command {
14 | using Callback = std::function;
15 | static void Empty() {}
16 | static void EmptyUpdate(float) {}
17 |
18 | std::function start = Empty;
19 | std::function update = EmptyUpdate;
20 | std::function finish = Empty;
21 |
22 | bool is_async = false;
23 |
24 | virtual void reset() {}
25 | virtual bool updateTick(float tick) { return false; }
26 | };
27 |
28 | struct LinearCommand : public Command {
29 | float current = 0, duration;
30 |
31 | LinearCommand()
32 | : current(0)
33 | , duration(0)
34 | {}
35 |
36 | LinearCommand(float duration)
37 | : duration(duration)
38 | {}
39 |
40 | void reset() override {
41 | current = 0;
42 | }
43 |
44 | bool updateTick(float tick) override {
45 | float pct = current / duration;
46 |
47 | update(pct);
48 |
49 | current += tick;
50 |
51 | if (current > duration) {
52 | current = duration;
53 | return false;
54 | }
55 |
56 | return true;
57 | }
58 | };
59 |
60 | struct CallbackCommand : public Command {
61 |
62 | CallbackCommand(const std::function& fn)
63 | : fn(fn)
64 | {
65 | this->start = fn;
66 | }
67 |
68 | bool updateTick(float tick) override {
69 | return false;
70 | }
71 |
72 | std::function fn;
73 | };
74 |
75 | class CommandQueue
76 | {
77 | public:
78 |
79 | CommandQueue& then(const std::function& fn) {
80 | auto o = std::make_shared(fn);
81 | queue.emplace_back(o);
82 | return *this;
83 | }
84 |
85 | CommandQueue& then(float duration, const std::function& update) {
86 | auto o = std::make_shared(duration);
87 | o->update = update;
88 | queue.emplace_back(o);
89 | return *this;
90 | }
91 |
92 | CommandQueue& then(CommandQueue& CQ) {
93 | auto o = std::make_shared(&CQ);
94 | CQ.parent = this;
95 | sub_commnad_queues.insert(&CQ);
96 | queue.emplace_back(o);
97 | return *this;
98 | }
99 |
100 | template
101 | CommandQueue& then(const std::function& fn) {
102 | auto o = std::make_shared();
103 | fn(o.get());
104 | queue.emplace_back(o);
105 | return *this;
106 | }
107 |
108 | CommandQueue& async(float duration, const std::function& update, const std::function& finish = Command::Empty) {
109 | auto o = std::make_shared(duration);
110 | o->is_async = true;
111 | o->update = update;
112 | o->finish = finish;
113 | queue.emplace_back(o);
114 | return *this;
115 | }
116 |
117 | CommandQueue& join()
118 | {
119 | auto o = std::make_shared(this);
120 | queue.emplace_back(o);
121 | return *this;
122 | }
123 |
124 | CommandQueue& wait(float duration = std::numeric_limits::infinity())
125 | {
126 | auto o = std::make_shared(duration);
127 | queue.emplace_back(o);
128 | return *this;
129 | }
130 |
131 | CommandQueue& until(const std::function& update)
132 | {
133 | auto o = std::make_shared(update);
134 | queue.emplace_back(o);
135 | return *this;
136 | }
137 |
138 | void start()
139 | {
140 | reset();
141 | startFrontQueue();
142 | }
143 |
144 | void reset()
145 | {
146 | running_queue = queue;
147 | }
148 |
149 | void clear()
150 | {
151 | running_queue.clear();
152 | queue.clear();
153 | }
154 |
155 | void update(float tick)
156 | {
157 | // only root commandd queue
158 | if (this->parent == nullptr)
159 | {
160 | updateAsync(tick);
161 | }
162 |
163 | if (running_queue.empty()) return;
164 |
165 | if (running_queue.front()->updateTick(tick) == false)
166 | {
167 | next();
168 | }
169 | }
170 |
171 | void next()
172 | {
173 | if (running_queue.empty()) return;
174 |
175 | auto front = running_queue.front();
176 | front->update(1);
177 | front->finish();
178 | running_queue.pop_front();
179 |
180 | if (running_queue.empty()) return;
181 |
182 | startFrontQueue();
183 | }
184 |
185 | bool isFinished() const { return running_queue.size() == 0 && !queue.empty(); }
186 | bool hasAsyncCommands() const { return !async_commands.empty(); }
187 |
188 | private:
189 |
190 | std::vector> async_commands;
191 | std::deque> running_queue;
192 |
193 | std::deque> queue;
194 |
195 | CommandQueue* parent = nullptr;
196 | std::unordered_set sub_commnad_queues;
197 |
198 | void startFrontQueue()
199 | {
200 | if (running_queue.empty()) return;
201 |
202 | while (!running_queue.empty())
203 | {
204 | auto front = running_queue.front();
205 |
206 | front->reset();
207 | front->start();
208 |
209 | // async command
210 | if (front->is_async)
211 | {
212 | async_commands.push_back(front);
213 | running_queue.pop_front();
214 | continue;
215 | }
216 |
217 | // callback only command
218 | if (front->updateTick(0) == false)
219 | {
220 | front->update(1);
221 | front->finish();
222 | running_queue.pop_front();
223 | continue;
224 | }
225 |
226 | break;
227 | }
228 | }
229 |
230 | void updateAsync(float tick)
231 | {
232 | auto it = async_commands.begin();
233 | while (it != async_commands.end())
234 | {
235 | auto ptr = it->get();
236 | if (ptr->updateTick(tick) == false)
237 | {
238 | ptr->update(1);
239 | ptr->finish();
240 |
241 | it = async_commands.erase(it);
242 | }
243 | else
244 | it++;
245 | }
246 |
247 | for (auto& it : sub_commnad_queues)
248 | {
249 | it->updateAsync(tick);
250 | }
251 | }
252 | };
253 |
254 | //
255 |
256 | struct CommandQueueCommand : public Command {
257 |
258 | CommandQueueCommand(CommandQueue* CQ)
259 | : CQ(CQ)
260 | {
261 | this->start = [this]() {
262 | this->CQ->start();
263 | };
264 | }
265 |
266 | bool updateTick(float tick) override {
267 | CQ->update(tick);
268 | return !CQ->isFinished();
269 | }
270 |
271 | CommandQueue* CQ;
272 | };
273 |
274 | struct JoinCommand : public Command {
275 |
276 | JoinCommand(CommandQueue* CQ)
277 | : CQ(CQ)
278 | {}
279 |
280 | bool updateTick(float tick) override {
281 | return CQ->hasAsyncCommands();
282 | }
283 |
284 | CommandQueue* CQ;
285 | };
286 |
287 | struct UntilCommand : public Command {
288 |
289 | std::function fn;
290 | float current = 0;
291 |
292 | UntilCommand(std::function fn)
293 | : fn(fn)
294 | {}
295 |
296 | void reset() override {
297 | current = 0;
298 | }
299 |
300 | bool updateTick(float tick) override {
301 | current += tick;
302 | return fn(current);
303 | }
304 | };
305 |
306 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
307 |
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Composition.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #include "Constants.h"
6 |
7 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
8 |
9 | class Composition
10 | {
11 | public:
12 |
13 | typedef ofPtr Ref;
14 |
15 | virtual void update() {}
16 | virtual void draw() {}
17 |
18 | virtual void viewWillAppear() {}
19 | virtual void viewDidAppear() {}
20 | virtual void viewWillDisappear() {}
21 | virtual void viewDidDisappear() {}
22 |
23 | public:
24 |
25 | // property
26 | int getElapsedFrame() const;
27 | float getElapsedTime() const;
28 |
29 | float getDuration() const;
30 | float getPosition() const;
31 |
32 | float getAlpha() const;
33 |
34 | public:
35 |
36 | void play();
37 | void play(float scene_total_duration);
38 | void play(float fadein_duration, float scene_total_duration, float fadeout_duration);
39 | void stop(float fadeout_duration = 0);
40 |
41 | bool isPlaying() const;
42 | bool isFinished() const;
43 |
44 | void reset();
45 |
46 | public:
47 |
48 | // events
49 |
50 | enum Trigger {
51 | WILL_APPEAR,
52 | DID_APPEAR,
53 | WILL_DISAPPEAR,
54 | DID_DISAPPEAR
55 | };
56 |
57 | ofEvent onTrigger;
58 |
59 | struct Duration {
60 | float fadein_duration;
61 | float total_duration;
62 | float fadeout_duration;
63 |
64 | Duration(float total_duration = std::numeric_limits::infinity());
65 | Duration(float fadein_duration, float total_duration, float fadeout_duration);
66 | };
67 |
68 | Composition::Ref on(Trigger event, Composition::Ref, Duration s = Duration());
69 | Composition::Ref at(float time, Composition::Ref, Duration s = Duration());
70 |
71 | public:
72 |
73 | // factory method
74 |
75 | template
76 | static Composition::Ref New();
77 |
78 | /*
79 | http://nedbatchelder.com/code/cog/
80 | $ cog.py -r SomoSourceCode.h
81 |
82 | [[[cog
83 | import cog
84 |
85 | tmpl = '''template
86 | static Composition::Ref New(%(B)s);
87 | '''
88 |
89 | cog.outl('')
90 | for i in xrange(1, 18):
91 | a = ', '.join(['typename A%i' % x for x in range(i)])
92 | b = ', '.join(['const A%i& a%i' % (x, x) for x in range(i)])
93 | c = ', '.join(['a%i' % x for x in range(i)])
94 | cog.outl(tmpl % {'A':a, 'B':b, 'C':c})
95 |
96 | ]]]*/
97 |
98 | template
99 | static Composition::Ref New(const A0& a0);
100 |
101 | template
102 | static Composition::Ref New(const A0& a0, const A1& a1);
103 |
104 | template
105 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2);
106 |
107 | template
108 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3);
109 |
110 | template
111 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4);
112 |
113 | template
114 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5);
115 |
116 | template
117 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6);
118 |
119 | template
120 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7);
121 |
122 | template
123 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8);
124 |
125 | template
126 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9);
127 |
128 | template
129 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10);
130 |
131 | template
132 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11);
133 |
134 | template
135 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12);
136 |
137 | template
138 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13);
139 |
140 | template
141 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14);
142 |
143 | template
144 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15);
145 |
146 | template
147 | static Composition::Ref New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15, const A16& a16);
148 |
149 | //[[[end]]]
150 |
151 | protected:
152 |
153 | Composition();
154 | virtual ~Composition();
155 |
156 | private:
157 |
158 | float alpha, alpha_delta;
159 | float elapsed_time;
160 |
161 | Duration duration;
162 |
163 | multimap trigger_event_map;
164 | multimap time_event_map;
165 |
166 | void procTimeEvent(float t0, float t1);
167 | void procTriggerEvent(Trigger trigger);
168 |
169 | enum CompositionState {
170 | PAUSED,
171 | STARTED,
172 | FADEIN,
173 | PLAYING,
174 | FADEOUT,
175 | FINISHED
176 | } state;
177 |
178 | friend class CompositionRunner;
179 |
180 | void updateState();
181 |
182 | void _update(float tick);
183 | void _draw();
184 |
185 | void _viewWillAppear();
186 | void _viewDidAppear();
187 | void _viewWillDisappear();
188 | void _viewDidDisappear();
189 |
190 | // noncopyable
191 | Composition(const Composition&) {}
192 | Composition& operator=(const Composition&) {}
193 | };
194 |
195 |
196 | class CompositionRunner
197 | {
198 | public:
199 |
200 | static CompositionRunner& defaultRunner()
201 | {
202 | static CompositionRunner o;
203 | return o;
204 | }
205 |
206 | void update(float tick = ofGetLastFrameTime())
207 | {
208 | for (int i = 0; i < comps.size(); i++)
209 | {
210 | comps[i]->_update(tick);
211 | }
212 | }
213 |
214 | void draw()
215 | {
216 | for (int i = 0; i < comps.size(); i++)
217 | {
218 | comps[i]->_draw();
219 | }
220 | }
221 |
222 | public: // internal use
223 |
224 | void registerComposition(Composition* o)
225 | {
226 | if (find(comps.begin(), comps.end(), o) != comps.end()) return;
227 | comps.push_back(o);
228 | }
229 |
230 | void unregisterComposition(Composition* o)
231 | {
232 | if (find(comps.begin(), comps.end(), o) == comps.end()) return;
233 | comps.erase(remove(comps.begin(), comps.end(), o), comps.end());
234 | }
235 |
236 | void clear()
237 | {
238 | comps.clear();
239 | }
240 |
241 | private:
242 |
243 | vector comps;
244 |
245 | // noncopyable
246 | CompositionRunner() {}
247 | ~CompositionRunner() {}
248 | CompositionRunner(const CompositionRunner&) {}
249 | CompositionRunner& operator=(const CompositionRunner&) {}
250 | };
251 |
252 | #include "detail/Composition.inc.h"
253 |
254 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/Pattern.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #include "Constants.h"
6 | #include "Timer.h"
7 |
8 | // based SuperCollider sequence mechanism
9 | // see. http://doc.sccode.org/Tutorials/Getting-Started/16-Sequencing-with-Patterns.html
10 |
11 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
12 |
13 | class Pattern
14 | {
15 | public:
16 |
17 | typedef float value_type;
18 | typedef ofPtr Ref;
19 |
20 | Pattern() {}
21 | Pattern(const float& value) : value(value) {}
22 |
23 | virtual bool valid() const { return true; }
24 | virtual void next() { if (valid()) update(); }
25 |
26 | const value_type& get() const { return value; }
27 |
28 | protected:
29 |
30 | value_type value;
31 | virtual void update() {}
32 | };
33 |
34 | class ListPattern : public Pattern
35 | {
36 | public:
37 |
38 | ListPattern() : num(0) {}
39 | ListPattern(int num) : num(num) {}
40 |
41 | bool valid() const { return num > 0 || num == -1; }
42 |
43 | protected:
44 |
45 | int num;
46 | void tick() { if (num > 0) num--; }
47 |
48 | };
49 |
50 | class Rand : public ListPattern
51 | {
52 | vector elem;
53 |
54 | public:
55 |
56 | Rand() {}
57 | Rand(const vector& elem, int num = -1) : elem(elem), ListPattern(num)
58 | {
59 | this->value = this->elem[rand() % this->elem.size()];
60 | }
61 |
62 | protected:
63 |
64 | void update()
65 | {
66 | ListPattern::tick();
67 | this->value = this->elem[rand() % this->elem.size()];
68 | }
69 | };
70 |
71 | class Seq : public ListPattern
72 | {
73 | int count;
74 | vector elem;
75 |
76 | public:
77 |
78 | Seq() {}
79 | Seq(const vector& elem, int num = -1) : elem(elem), ListPattern(num), count(0)
80 | {
81 | this->value = this->elem[0];
82 | }
83 |
84 | protected:
85 |
86 | void update()
87 | {
88 | ListPattern::tick();
89 | this->value = this->elem[++count % this->elem.size()];
90 | }
91 | };
92 |
93 | class While : public ListPattern
94 | {
95 | int count;
96 | value_type min, max;
97 |
98 | public:
99 |
100 | While() {}
101 | While(const value_type& min, const value_type& max, int num = -1) : min(min), max(max), ListPattern(num), count(0)
102 | {
103 | this->value = ofLerp(min, max, ofRandom(1));
104 | }
105 |
106 | protected:
107 |
108 | void update()
109 | {
110 | ListPattern::tick();
111 | this->value = ofLerp(min, max, ofRandom(1));
112 | }
113 | };
114 |
115 | // time
116 |
117 | class TimePattern : public Pattern, public Ticker
118 | {
119 | public:
120 |
121 | TimePattern() :remain(0), playing(false), value_pattern(NULL), dur_pattern(NULL) {}
122 |
123 | virtual ~TimePattern()
124 | {
125 | stop();
126 |
127 | if (value_pattern)
128 | delete value_pattern;
129 |
130 | if (dur_pattern)
131 | delete dur_pattern;
132 | }
133 |
134 | void play()
135 | {
136 | playing = true;
137 | Ticker::play();
138 | }
139 |
140 | void pause() { playing = false; }
141 |
142 | void stop()
143 | {
144 | Ticker::stop();
145 | remain = 0;
146 | playing = false;
147 | }
148 |
149 | void toggle() { isPlaying() ? stop() : play(); }
150 |
151 | bool isPlaying() { return playing; }
152 |
153 | bool valid() const
154 | {
155 | return value_pattern->valid()
156 | && dur_pattern->valid();
157 | }
158 |
159 | protected:
160 |
161 | virtual void event() {}
162 |
163 | protected:
164 |
165 | float remain;
166 | bool playing;
167 |
168 | void setPattern(Pattern *value, Pattern *dur)
169 | {
170 | setValuePattern(value);
171 | setDurationPattern(dur);
172 | }
173 |
174 | void setValuePattern(Pattern *value)
175 | {
176 | if (value_pattern)
177 | delete value_pattern;
178 | value_pattern = value;
179 | }
180 |
181 | void setDurationPattern(Pattern *dur)
182 | {
183 | if (dur_pattern)
184 | delete dur_pattern;
185 | dur_pattern = dur;
186 | }
187 |
188 | void tick(float delta)
189 | {
190 | // must set value_pattern & dur_pattern
191 | assert(value_pattern);
192 | assert(dur_pattern);
193 |
194 | if (!valid()) stop();
195 |
196 | if (!playing) return;
197 |
198 | bool fire = false;
199 | while (remain <= 0)
200 | {
201 | remain += dur_pattern->get();
202 | dur_pattern->next();
203 | fire = true;
204 | }
205 |
206 | if (fire)
207 | {
208 | this->value = value_pattern->get();
209 | value_pattern->next();
210 |
211 | this->event();
212 | }
213 |
214 | remain -= delta;
215 | }
216 |
217 | private:
218 |
219 | Pattern *value_pattern;
220 | Pattern *dur_pattern;
221 |
222 | };
223 |
224 | class Step : public TimePattern
225 | {
226 | public:
227 |
228 | Step() {}
229 |
230 | template
231 | Step(const T0& value, const T1& dur)
232 | {
233 | setValuePattern(new T0(value));
234 | setDurationPattern(new T1(dur));
235 | }
236 |
237 | };
238 |
239 | //
240 |
241 | class Bind : public Pattern
242 | {
243 | map patterns;
244 | map value;
245 |
246 | public:
247 |
248 | typedef map value_type;
249 |
250 | Bind() {}
251 |
252 | template
253 | Bind(const string& key0, const T0& t0)
254 | {
255 | patterns[key0] = Pattern::Ref(new T0(t0));
256 | }
257 |
258 | template
259 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1)
260 | {
261 | patterns[key0] = Pattern::Ref(new T0(t0));
262 | patterns[key1] = Pattern::Ref(new T1(t1));
263 | }
264 |
265 | template
266 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2)
267 | {
268 | patterns[key0] = Pattern::Ref(new T0(t0));
269 | patterns[key1] = Pattern::Ref(new T1(t1));
270 | patterns[key2] = Pattern::Ref(new T2(t2));
271 | }
272 |
273 | template
274 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2, const string& key3, const T3& t3)
275 | {
276 | patterns[key0] = Pattern::Ref(new T0(t0));
277 | patterns[key1] = Pattern::Ref(new T1(t1));
278 | patterns[key2] = Pattern::Ref(new T2(t2));
279 | patterns[key3] = Pattern::Ref(new T3(t3));
280 | }
281 |
282 | template
283 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2, const string& key3, const T3& t3, const string& key4, const T4& t4)
284 | {
285 | patterns[key0] = Pattern::Ref(new T0(t0));
286 | patterns[key1] = Pattern::Ref(new T1(t1));
287 | patterns[key2] = Pattern::Ref(new T2(t2));
288 | patterns[key3] = Pattern::Ref(new T3(t3));
289 | patterns[key4] = Pattern::Ref(new T4(t4));
290 | }
291 |
292 | template
293 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2, const string& key3, const T3& t3, const string& key4, const T4& t4, const string& key5, const T5& t5)
294 | {
295 | patterns[key0] = Pattern::Ref(new T0(t0));
296 | patterns[key1] = Pattern::Ref(new T1(t1));
297 | patterns[key2] = Pattern::Ref(new T2(t2));
298 | patterns[key3] = Pattern::Ref(new T3(t3));
299 | patterns[key4] = Pattern::Ref(new T4(t4));
300 | patterns[key5] = Pattern::Ref(new T5(t5));
301 | }
302 |
303 | template
304 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2, const string& key3, const T3& t3, const string& key4, const T4& t4, const string& key5, const T5& t5, const string& key6, const T6& t6)
305 | {
306 | patterns[key0] = Pattern::Ref(new T0(t0));
307 | patterns[key1] = Pattern::Ref(new T1(t1));
308 | patterns[key2] = Pattern::Ref(new T2(t2));
309 | patterns[key3] = Pattern::Ref(new T3(t3));
310 | patterns[key4] = Pattern::Ref(new T4(t4));
311 | patterns[key5] = Pattern::Ref(new T5(t5));
312 | patterns[key6] = Pattern::Ref(new T6(t6));
313 | }
314 |
315 | template
316 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2, const string& key3, const T3& t3, const string& key4, const T4& t4, const string& key5, const T5& t5, const string& key6, const T6& t6, const string& key7, const T7& t7)
317 | {
318 | patterns[key0] = Pattern::Ref(new T0(t0));
319 | patterns[key1] = Pattern::Ref(new T1(t1));
320 | patterns[key2] = Pattern::Ref(new T2(t2));
321 | patterns[key3] = Pattern::Ref(new T3(t3));
322 | patterns[key4] = Pattern::Ref(new T4(t4));
323 | patterns[key5] = Pattern::Ref(new T5(t5));
324 | patterns[key6] = Pattern::Ref(new T6(t6));
325 | patterns[key7] = Pattern::Ref(new T7(t7));
326 | }
327 |
328 | template
329 | Bind(const string& key0, const T0& t0, const string& key1, const T1& t1, const string& key2, const T2& t2, const string& key3, const T3& t3, const string& key4, const T4& t4, const string& key5, const T5& t5, const string& key6, const T6& t6, const string& key7, const T7& t7, const string& key8, const T8& t8)
330 | {
331 | patterns[key0] = Pattern::Ref(new T0(t0));
332 | patterns[key1] = Pattern::Ref(new T1(t1));
333 | patterns[key2] = Pattern::Ref(new T2(t2));
334 | patterns[key3] = Pattern::Ref(new T3(t3));
335 | patterns[key4] = Pattern::Ref(new T4(t4));
336 | patterns[key5] = Pattern::Ref(new T5(t5));
337 | patterns[key6] = Pattern::Ref(new T6(t6));
338 | patterns[key7] = Pattern::Ref(new T7(t7));
339 | patterns[key8] = Pattern::Ref(new T8(t8));
340 | }
341 |
342 | bool valid() const
343 | {
344 | map::const_iterator it = patterns.begin();
345 |
346 | while (it != patterns.end())
347 | {
348 | if (!it->second->valid()) return false;
349 | it++;
350 | }
351 |
352 | return true;
353 | }
354 |
355 | const Pattern::value_type& get(const string& key) { return value[key]; }
356 |
357 | void dump() const
358 | {
359 | map::const_iterator it = patterns.begin();
360 |
361 | cout << "[" << endl;
362 | while (it != patterns.end())
363 | {
364 | if (!it->second->valid());
365 | cout << "\t" << it->first << ": " << it->second->get() << endl;
366 | it++;
367 | }
368 | cout << "]" << endl;
369 | }
370 |
371 | protected:
372 |
373 | void update()
374 | {
375 | map::const_iterator it = patterns.begin();
376 |
377 | while (it != patterns.end())
378 | {
379 | it->second->next();
380 | value[it->first] = it->second->get();
381 | it++;
382 | }
383 | }
384 |
385 | };
386 |
387 | // SuperCollider like typename
388 |
389 | typedef Rand Prand;
390 | typedef Seq Pseq;
391 | typedef While Pwhile;
392 | typedef Step Pstep;
393 | typedef Bind Pbind;
394 |
395 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
396 |
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/InstanceManager.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "ofMain.h"
4 |
5 | #include "Constants.h"
6 |
7 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
8 |
9 | template
10 | class InstanceManager_;
11 |
12 | template
13 | class Instance_
14 | {
15 | friend class InstanceManager_;
16 |
17 | public:
18 |
19 | Instance_()
20 | : class_id(0)
21 | , remain(0)
22 | , duration(0)
23 | , life(0)
24 | , one_minus_life(0)
25 | , after(0)
26 | , elapsed(0)
27 | , uniform(NULL)
28 | {}
29 |
30 | virtual ~Instance_() {}
31 |
32 | virtual void update() {}
33 | virtual void draw() {}
34 |
35 | inline void play(float duration, float after = 0)
36 | {
37 | if (duration < 0) duration = 0;
38 | this->duration = remain = duration;
39 | this->after = after;
40 | }
41 |
42 | inline void playInfinity()
43 | {
44 | this->duration = remain = std::numeric_limits::infinity();
45 | }
46 |
47 | // use with playInfinity
48 | void release(float duration)
49 | {
50 | this->duration = remain = duration;
51 | }
52 |
53 | public:
54 |
55 | inline bool isAlive() const { return remain > 0; }
56 |
57 | inline float getLife() const { return life; }
58 | inline float getOneMinusLife() const { return one_minus_life; }
59 | inline float getInvLife() const { return getOneMinusLife(); }
60 |
61 | inline float getDuration() const { return duration; }
62 |
63 | inline bool isInfinity() const { return std::isinf(duration); }
64 |
65 | inline UniformData& getUniform() { return *uniform; }
66 | inline const UniformData& getUniform() const { return *uniform; }
67 |
68 | inline float getElapsed() const { return elapsed; }
69 |
70 | protected:
71 |
72 | virtual void willDelete() {}
73 |
74 | private:
75 |
76 | RTTI::TypeID class_id;
77 |
78 | float remain;
79 | float duration;
80 | float after;
81 | float elapsed;
82 |
83 | // cache
84 | float life;
85 | float one_minus_life;
86 |
87 | UniformData *uniform;
88 | };
89 |
90 | template
91 | class InstanceManager_
92 | {
93 | typedef Instance_ Instance;
94 |
95 | public:
96 |
97 | void update(float tick = ofGetLastFrameTime())
98 | {
99 | for (int i = 0; i < instances.size(); i++)
100 | {
101 | Instance *o = instances[i];
102 |
103 | if (o->after >= tick)
104 | {
105 | o->after -= tick;
106 | continue;
107 | }
108 | else o->after = 0;
109 |
110 | o->update();
111 | o->remain -= tick;
112 | o->elapsed += tick;
113 |
114 | if (isnormal(o->duration))
115 | o->life = o->remain / o->duration;
116 | else
117 | o->life = 1;
118 |
119 | o->one_minus_life = 1 - o->life;
120 | }
121 |
122 | typename vector::iterator it = instances.begin();
123 | while (it != instances.end())
124 | {
125 | Instance *o = *it;
126 | if (!o->isAlive())
127 | {
128 | it = instances.erase(it);
129 | o->willDelete();
130 | delete o;
131 | }
132 | else
133 | it++;
134 | }
135 | }
136 |
137 | void draw()
138 | {
139 | glPushAttrib(GL_ALL_ATTRIB_BITS);
140 | ofPushMatrix();
141 | ofPushStyle();
142 |
143 | typename vector::iterator it = instances.begin();
144 | while (it != instances.end())
145 | {
146 | Instance *o = *it;
147 | if (o->after == 0)
148 | o->draw();
149 | it++;
150 | }
151 |
152 | ofPopStyle();
153 | ofPopMatrix();
154 | glPopAttrib();
155 | }
156 |
157 | public:
158 |
159 | template
160 | T* createInstance()
161 | {
162 | return setupInstance(new T);
163 | }
164 |
165 | /*
166 | http://nedbatchelder.com/code/cog/
167 | $ cog.py -r SomoSourceCode.h
168 |
169 | [[[cog
170 | import cog
171 |
172 | tmpl = '''template
173 | T* createInstance(%(B)s)
174 | {
175 | return setupInstance(new T(%(C)s));
176 | }
177 | '''
178 |
179 | cog.outl('')
180 | for i in xrange(1, 18):
181 | a = ', '.join(['typename A%i' % x for x in range(i)])
182 | b = ', '.join(['const A%i& a%i' % (x, x) for x in range(i)])
183 | c = ', '.join(['a%i' % x for x in range(i)])
184 | cog.outl(tmpl % {'A':a, 'B':b, 'C':c})
185 |
186 | ]]]*/
187 |
188 | template
189 | T* createInstance(const A0& a0)
190 | {
191 | return setupInstance(new T(a0));
192 | }
193 |
194 | template
195 | T* createInstance(const A0& a0, const A1& a1)
196 | {
197 | return setupInstance(new T(a0, a1));
198 | }
199 |
200 | template
201 | T* createInstance(const A0& a0, const A1& a1, const A2& a2)
202 | {
203 | return setupInstance(new T(a0, a1, a2));
204 | }
205 |
206 | template
207 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3)
208 | {
209 | return setupInstance(new T(a0, a1, a2, a3));
210 | }
211 |
212 | template
213 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4)
214 | {
215 | return setupInstance(new T(a0, a1, a2, a3, a4));
216 | }
217 |
218 | template
219 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
220 | {
221 | return setupInstance(new T(a0, a1, a2, a3, a4, a5));
222 | }
223 |
224 | template
225 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6)
226 | {
227 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6));
228 | }
229 |
230 | template
231 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7)
232 | {
233 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7));
234 | }
235 |
236 | template
237 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8)
238 | {
239 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8));
240 | }
241 |
242 | template
243 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9)
244 | {
245 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9));
246 | }
247 |
248 | template
249 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10)
250 | {
251 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10));
252 | }
253 |
254 | template
255 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11)
256 | {
257 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11));
258 | }
259 |
260 | template
261 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12)
262 | {
263 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12));
264 | }
265 |
266 | template
267 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13)
268 | {
269 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13));
270 | }
271 |
272 | template
273 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14)
274 | {
275 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14));
276 | }
277 |
278 | template
279 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15)
280 | {
281 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15));
282 | }
283 |
284 | template
285 | T* createInstance(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15, const A16& a16)
286 | {
287 | return setupInstance(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16));
288 | }
289 |
290 | //[[[end]]]
291 |
292 | void release()
293 | {
294 | typename vector::iterator it = instances.begin();
295 | while (it != instances.end())
296 | {
297 | Instance *o = *it;
298 | o->willDelete();
299 | delete o;
300 | it++;
301 | }
302 | }
303 |
304 | void release(float duration)
305 | {
306 | typename vector::iterator it = instances.begin();
307 | while (it != instances.end())
308 | {
309 | Instance *o = *it;
310 | o->release(duration);
311 | it++;
312 | }
313 | }
314 |
315 | template
316 | void release()
317 | {
318 | typename vector::iterator it = instances.begin();
319 | while (it != instances.end())
320 | {
321 | Instance *o = *it;
322 | if (o->class_id == RTTI::getTypeID())
323 | {
324 | it = instances.erase(it);
325 | o->willDelete();
326 | delete o;
327 | }
328 | else
329 | it++;
330 | }
331 | }
332 |
333 | template
334 | void release(float duration)
335 | {
336 | typename vector::iterator it = instances.begin();
337 | while (it != instances.end())
338 | {
339 | Instance *o = *it;
340 | if (o->class_id == RTTI::getTypeID())
341 | {
342 | o->release(duration);
343 | }
344 | it++;
345 | }
346 | }
347 |
348 | void clear()
349 | {
350 | typename vector::iterator it = instances.begin();
351 | while (it != instances.end())
352 | {
353 | Instance *o = *it;
354 | delete o;
355 | it++;
356 | }
357 |
358 | instances.clear();
359 | }
360 |
361 | template
362 | vector getInstance()
363 | {
364 | vector result;
365 |
366 | typename vector::iterator it = instances.begin();
367 | while (it != instances.end())
368 | {
369 | Instance *o = *it;
370 | if (o->class_id == RTTI::getTypeID())
371 | {
372 | result.push_back((T*)o);
373 | }
374 | it++;
375 | }
376 |
377 | return result;
378 | }
379 |
380 | const vector& getInstance() const { return instances; }
381 |
382 | size_t getNumInstances() const { return instances.size(); }
383 |
384 | UniformData& getUniform() { return uniform; }
385 | const UniformData& getUniform() const { return uniform; }
386 |
387 | protected:
388 |
389 | UniformData uniform;
390 | vector instances;
391 |
392 | template
393 | T* setupInstance(T* o)
394 | {
395 | o->class_id = RTTI::getTypeID();
396 | o->uniform = &uniform;
397 | instances.push_back(o);
398 | return o;
399 | }
400 | };
401 |
402 | struct EmptyData {};
403 |
404 | typedef Instance_ Instance;
405 | typedef InstanceManager_ InstanceManager;
406 |
407 | OFX_ANIMATION_PRIMITIVES_END_NAMESPACE
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/detail/Composition.inc.h:
--------------------------------------------------------------------------------
1 |
2 | inline Composition::Duration::Duration(float total_duration)
3 | : fadein_duration(0)
4 | , total_duration(total_duration)
5 | , fadeout_duration(0)
6 | {
7 |
8 | }
9 |
10 | inline Composition::Duration::Duration(float fadein_duration, float total_duration, float fadeout_duration)
11 | : fadein_duration(fadein_duration)
12 | , total_duration(total_duration)
13 | , fadeout_duration(fadeout_duration)
14 | {
15 | }
16 |
17 |
18 | #pragma mark - Composition
19 |
20 | inline Composition::Composition()
21 | : state(PAUSED)
22 | , alpha(0)
23 | , alpha_delta(0)
24 | , elapsed_time(0)
25 | , duration(0, 0, 0)
26 | {
27 | }
28 |
29 | inline Composition::~Composition()
30 | {
31 | if (isPlaying())
32 | {
33 | _viewWillAppear();
34 | _viewDidAppear();
35 | }
36 | }
37 |
38 | template
39 | inline Composition::Ref Composition::New()
40 | {
41 | return Composition::Ref(new T);
42 | }
43 |
44 | /*
45 | http://nedbatchelder.com/code/cog/
46 | $ cog.py -r SomoSourceCode.h
47 |
48 | [[[cog
49 | import cog
50 |
51 | tmpl = '''template
52 | inline Composition::Ref Composition::New(%(B)s)
53 | {
54 | return Composition::Ref(new T(%(C)s));
55 | }
56 | '''
57 |
58 | cog.outl('')
59 | for i in xrange(1, 18):
60 | a = ', '.join(['typename A%i' % x for x in range(i)])
61 | b = ', '.join(['const A%i& a%i' % (x, x) for x in range(i)])
62 | c = ', '.join(['a%i' % x for x in range(i)])
63 | cog.outl(tmpl % {'A':a, 'B':b, 'C':c})
64 |
65 | ]]]*/
66 |
67 | template
68 | inline Composition::Ref Composition::New(const A0& a0)
69 | {
70 | return Composition::Ref(new T(a0));
71 | }
72 |
73 | template
74 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1)
75 | {
76 | return Composition::Ref(new T(a0, a1));
77 | }
78 |
79 | template
80 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2)
81 | {
82 | return Composition::Ref(new T(a0, a1, a2));
83 | }
84 |
85 | template
86 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3)
87 | {
88 | return Composition::Ref(new T(a0, a1, a2, a3));
89 | }
90 |
91 | template
92 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4)
93 | {
94 | return Composition::Ref(new T(a0, a1, a2, a3, a4));
95 | }
96 |
97 | template
98 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
99 | {
100 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5));
101 | }
102 |
103 | template
104 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6)
105 | {
106 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6));
107 | }
108 |
109 | template
110 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7)
111 | {
112 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7));
113 | }
114 |
115 | template
116 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8)
117 | {
118 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8));
119 | }
120 |
121 | template
122 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9)
123 | {
124 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9));
125 | }
126 |
127 | template
128 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10)
129 | {
130 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10));
131 | }
132 |
133 | template
134 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11)
135 | {
136 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11));
137 | }
138 |
139 | template
140 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12)
141 | {
142 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12));
143 | }
144 |
145 | template
146 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13)
147 | {
148 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13));
149 | }
150 |
151 | template
152 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14)
153 | {
154 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14));
155 | }
156 |
157 | template
158 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15)
159 | {
160 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15));
161 | }
162 |
163 | template
164 | inline Composition::Ref Composition::New(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15, const A16& a16)
165 | {
166 | return Composition::Ref(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16));
167 | }
168 |
169 | //[[[end]]]
170 |
171 | inline float Composition::getElapsedTime() const { return elapsed_time; }
172 | inline float Composition::getDuration() const { return duration.total_duration; }
173 | inline float Composition::getPosition() const { return elapsed_time / (duration.total_duration + std::numeric_limits::denorm_min()); }
174 | inline float Composition::getAlpha() const { return alpha; }
175 | inline bool Composition::isPlaying() const { return state != PAUSED && state != FINISHED; }
176 | inline bool Composition::isFinished() const { return state == FINISHED; }
177 |
178 | inline void Composition::reset()
179 | {
180 | CompositionRunner::defaultRunner().unregisterComposition(this);
181 |
182 | state = PAUSED;
183 | elapsed_time = 0;
184 | alpha = 0;
185 | alpha_delta = 0;
186 |
187 | {
188 | multimap::iterator it = trigger_event_map.begin();
189 | while (it != trigger_event_map.end())
190 | {
191 | Composition::Ref &o = it->second;
192 | o->reset();
193 | it++;
194 | }
195 |
196 | trigger_event_map.clear();
197 | }
198 |
199 | {
200 | multimap::iterator it = time_event_map.begin();
201 | while (it != time_event_map.end())
202 | {
203 | Composition::Ref &o = it->second;
204 | o->reset();
205 | it++;
206 | }
207 |
208 | time_event_map.clear();
209 | }
210 | }
211 |
212 | inline void Composition::play()
213 | {
214 | if (isPlaying())
215 | {
216 | ofLogWarning("Composition") << "play method called multiple times.";
217 | return;
218 | }
219 |
220 | if (duration.fadein_duration <= 0)
221 | {
222 | alpha_delta = 1.;
223 | alpha = 1;
224 |
225 | updateState();
226 | }
227 | else
228 | {
229 | alpha_delta = 1. / duration.fadein_duration;
230 | }
231 |
232 | state = STARTED;
233 |
234 | CompositionRunner::defaultRunner().registerComposition(this);
235 | }
236 |
237 | inline void Composition::play(float scene_total_duration)
238 | {
239 | Composition::play(0, scene_total_duration, 0);
240 | }
241 |
242 | inline void Composition::play(float fadein_duration, float scene_total_duration, float fadeout_duration)
243 | {
244 | duration = Duration(fadein_duration, scene_total_duration, fadeout_duration);
245 | Composition::play();
246 | }
247 |
248 | inline void Composition::stop(float fadeout_duration)
249 | {
250 | if (state == FADEOUT)
251 | {
252 | ofLogWarning("Composition") << "stop method called multiple times.";
253 | return;
254 | }
255 |
256 | if (fadeout_duration <= 0)
257 | {
258 | alpha_delta = -1;
259 | alpha = 0;
260 |
261 | updateState();
262 | }
263 | else
264 | {
265 | alpha_delta = -alpha / fadeout_duration;
266 | }
267 | }
268 |
269 | inline void Composition::_update(float tick)
270 | {
271 | if (!isPlaying()) return;
272 |
273 | float last_elapsed_time = elapsed_time;
274 | elapsed_time += tick;
275 | alpha += alpha_delta * tick;
276 |
277 | if (state == PLAYING
278 | && !isinf(duration.total_duration))
279 | {
280 | const float fadeout_time = duration.total_duration - duration.fadeout_duration;
281 | if (elapsed_time > fadeout_time)
282 | {
283 | stop(duration.fadeout_duration);
284 | }
285 | }
286 |
287 | updateState();
288 |
289 | procTimeEvent(last_elapsed_time, elapsed_time);
290 |
291 | if (isPlaying())
292 | update();
293 | }
294 |
295 | inline void Composition::_draw()
296 | {
297 | if (!isPlaying()) return;
298 |
299 | glPushAttrib(GL_ALL_ATTRIB_BITS);
300 | ofPushStyle();
301 | ofPushView();
302 | draw();
303 | ofPopView();
304 | ofPopStyle();
305 | glPopAttrib();
306 | }
307 |
308 | inline void Composition::_viewWillAppear()
309 | {
310 | viewWillAppear();
311 |
312 | procTriggerEvent(WILL_APPEAR);
313 | }
314 |
315 | inline void Composition::_viewDidAppear()
316 | {
317 | viewDidAppear();
318 |
319 | procTriggerEvent(DID_APPEAR);
320 | }
321 |
322 | inline void Composition::_viewWillDisappear()
323 | {
324 | viewWillDisappear();
325 |
326 | procTriggerEvent(WILL_DISAPPEAR);
327 | }
328 |
329 | inline void Composition::_viewDidDisappear()
330 | {
331 | viewDidDisappear();
332 |
333 | procTriggerEvent(DID_DISAPPEAR);
334 | CompositionRunner::defaultRunner().unregisterComposition(this);
335 | }
336 |
337 | inline void Composition::updateState()
338 | {
339 | if (state == STARTED && alpha_delta > 0.)
340 | {
341 | state = FADEIN;
342 |
343 | _viewWillAppear();
344 | }
345 | if (state == FADEIN && alpha >= 1.)
346 | {
347 | alpha = 1;
348 | alpha_delta = 0;
349 | state = PLAYING;
350 |
351 | _viewDidAppear();
352 | }
353 | if (state == PLAYING && alpha_delta < 0.)
354 | {
355 | state = FADEOUT;
356 |
357 | _viewWillDisappear();
358 | }
359 | if (state == FADEOUT && alpha <= 0.)
360 | {
361 | alpha = 0;
362 | alpha_delta = 0;
363 | state = FINISHED;
364 |
365 | _viewDidDisappear();
366 | }
367 | }
368 |
369 | inline void Composition::procTimeEvent(float t0, float t1)
370 | {
371 | multimap::iterator begin = time_event_map.lower_bound(t0);
372 | multimap::iterator end = time_event_map.upper_bound(t1);
373 |
374 | if (begin == end) return;
375 |
376 | multimap::iterator it = begin;
377 |
378 | while (it != end)
379 | {
380 | Composition::Ref &o = it->second;
381 | o->play();
382 | it++;
383 | }
384 | }
385 |
386 | inline void Composition::procTriggerEvent(Trigger trigger)
387 | {
388 | if (trigger_event_map.count(trigger))
389 | {
390 | std::pair::iterator,
391 | multimap::iterator> range = trigger_event_map.equal_range(trigger);
392 | multimap::iterator it = range.first;
393 | while (it != range.second)
394 | {
395 | Composition::Ref &o = it->second;
396 | o->play();
397 | it++;
398 | }
399 |
400 | ofNotifyEvent(onTrigger, trigger, this);
401 | }
402 | }
403 |
404 | inline Composition::Ref Composition::on(Trigger event, Composition::Ref o, Duration s)
405 | {
406 | if (o.get() == this)
407 | {
408 | throw;
409 | }
410 |
411 | trigger_event_map.insert(make_pair(event, o));
412 | o->duration = s;
413 |
414 | return o;
415 | }
416 |
417 | inline Composition::Ref Composition::at(float time, Composition::Ref o, Duration s)
418 | {
419 | if (o.get() == this)
420 | {
421 | throw;
422 | }
423 |
424 | time_event_map.insert(make_pair(time, o));
425 | o->duration = s;
426 |
427 | return o;
428 | }
429 |
430 | #pragma mark - CompositionRunner
431 |
432 |
--------------------------------------------------------------------------------
/src/ofxAnimationPrimitives/SceneManager.h:
--------------------------------------------------------------------------------
1 | OFX_ANIMATION_PRIMITIVES_BEGIN_NAMESPACE
2 |
3 | class SceneManager;
4 |
5 | #define OFX_ANIMATION_PRIMITIVES_DEFINE_SCENE(CLASS) \
6 | static const char* getSceneName() { return #CLASS; }
7 |
8 | class Scene
9 | {
10 | friend class SceneManager;
11 |
12 | public:
13 |
14 | Scene()
15 | : _alpha(0)
16 | {}
17 |
18 | virtual ~Scene() {}
19 |
20 | virtual void update() {}
21 | virtual void draw() {}
22 |
23 | float getAlpha() const { return _alpha; }
24 |
25 | virtual void viewWillAppear() {}
26 | virtual void viewDidAppear() {}
27 | virtual void viewWillDisappear() {}
28 | virtual void viewDidDisappear() {}
29 |
30 | private:
31 |
32 | float _alpha;
33 | };
34 |
35 | class SceneManager
36 | {
37 | public:
38 |
39 | SceneManager()
40 | : default_fade_duration(1)
41 | {}
42 |
43 | void update(float tick = ofGetLastFrameTime())
44 | {
45 | {
46 | map::iterator it = faders.begin();
47 | while (it != faders.end())
48 | {
49 | Fader& fader = it->second;
50 | fader.update(tick);
51 | it++;
52 | }
53 | }
54 |
55 | {
56 | map::iterator it = scenes.begin();
57 | while (it != scenes.end())
58 | {
59 | Scene* scene = it->second;
60 | if (scene->getAlpha() > 0)
61 | {
62 | scene->update();
63 | }
64 |
65 | it++;
66 | }
67 | }
68 | }
69 |
70 | void draw()
71 | {
72 | map::iterator it = scenes.begin();
73 | while (it != scenes.end())
74 | {
75 | Scene* scene = it->second;
76 | if (scene->getAlpha() > 0)
77 | {
78 | ofPushMatrix();
79 | ofPushStyle();
80 |
81 | scene->draw();
82 |
83 | ofPopStyle();
84 | ofPopMatrix();
85 | }
86 |
87 | it++;
88 | }
89 | }
90 |
91 | template
92 | void addScene();
93 |
94 | /*
95 | http://nedbatchelder.com/code/cog/
96 | $ cog.py -r SomoSourceCode.h
97 |
98 | [[[cog
99 | import cog
100 |
101 | tmpl = '''template
102 | void addScene(%(B)s);
103 | '''
104 |
105 | cog.outl('')
106 | for i in xrange(1, 18):
107 | a = ', '.join(['typename A%i' % x for x in range(i)])
108 | b = ', '.join(['const A%i& a%i' % (x, x) for x in range(i)])
109 | cog.outl(tmpl % {'A':a, 'B':b})
110 |
111 | ]]]*/
112 |
113 | template
114 | void addScene(const A0& a0);
115 |
116 | template
117 | void addScene(const A0& a0, const A1& a1);
118 |
119 | template
120 | void addScene(const A0& a0, const A1& a1, const A2& a2);
121 |
122 | template
123 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3);
124 |
125 | template
126 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4);
127 |
128 | template
129 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5);
130 |
131 | template
132 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6);
133 |
134 | template
135 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7);
136 |
137 | template
138 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8);
139 |
140 | template
141 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9);
142 |
143 | template
144 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10);
145 |
146 | template
147 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11);
148 |
149 | template
150 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12);
151 |
152 | template
153 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13);
154 |
155 | template
156 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14);
157 |
158 | template
159 | void addScene(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10, const A11& a11, const A12& a12, const A13& a13, const A14& a14, const A15& a15);
160 |
161 | template