X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fanimation.cpp;h=744262d84781ae40e0b2c42b604b1b2badb516cf;hp=35d2ae7aca1565f79b6934b76a5b675a89afe1e9;hb=19555043d70f972feedb89786fc24a2b83b4f7e9;hpb=4b4d2a48048268d2ad48bafbce8647af8088573f diff --git a/source/animation.cpp b/source/animation.cpp index 35d2ae7a..744262d8 100644 --- a/source/animation.cpp +++ b/source/animation.cpp @@ -3,6 +3,7 @@ #include #include #include "animation.h" +#include "animationeventobserver.h" #include "armature.h" #include "error.h" #include "pose.h" @@ -42,16 +43,42 @@ const string &Animation::get_uniform_name(unsigned i) const } void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf) +{ + RefPtr kfr(&kf); + kfr.keep(); + add_keyframe(t, kfr); +} + +void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr &kf) { if(!keyframes.empty() && t=keyframes.capacity()); + + keyframes.push_back(TimedKeyFrame()); + TimedKeyFrame &tkf = keyframes.back(); tkf.time = t; - tkf.keyframe = &kf; - tkf.keyframe.keep(); + tkf.keyframe = kf; + + if(realloc) + { + for(unsigned i=1; i1) + tkf.prev = &tkf-1; + prepare_keyframe(tkf); - keyframes.push_back(tkf); +} + +void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v) +{ + Event event; + event.time = t; + event.name = n; + event.value = v; + events.push_back(event); } void Animation::set_looping(bool l) @@ -61,8 +88,6 @@ void Animation::set_looping(bool l) void Animation::prepare_keyframe(TimedKeyFrame &tkf) { - tkf.prev = (keyframes.empty() ? 0 : &keyframes.back()); - const KeyFrame::UniformMap &kf_uniforms = tkf.keyframe->get_uniforms(); for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i) { @@ -79,7 +104,7 @@ void Animation::prepare_keyframe(TimedKeyFrame &tkf) uniforms.push_back(UniformInfo(i->first, i->second.size)); } - tkf.prepare(); + tkf.prepare(*this); } @@ -170,12 +195,11 @@ Matrix Animation::MatrixInterpolation::get(float t) const } -Animation::TimedKeyFrame::TimedKeyFrame(const Animation &a): - animation(a), +Animation::TimedKeyFrame::TimedKeyFrame(): prev(0) { } -void Animation::TimedKeyFrame::prepare() +void Animation::TimedKeyFrame::prepare(const Animation &animation) { const KeyFrame::UniformMap &kf_uniforms = keyframe->get_uniforms(); for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i) @@ -220,6 +244,7 @@ Animation::UniformInfo::UniformInfo(const string &n, unsigned s): Animation::Iterator::Iterator(const Animation &a): animation(&a), iter(animation->keyframes.begin()), + event_iter(animation->events.begin()), end(false) { } @@ -228,7 +253,7 @@ Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t) time_since_keyframe += t; while(time_since_keyframe>iter->delta_t) { - KeyFrameList::const_iterator next = iter; + vector::const_iterator next = iter; ++next; if(next==animation->keyframes.end()) { @@ -249,6 +274,24 @@ Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t) return *this; } +void Animation::Iterator::dispatch_events(AnimationEventObserver &observer) +{ + vector::const_iterator events_end = animation->events.end(); + if(end) + { + for(; event_iter!=events_end; ++event_iter) + observer.animation_event(0, event_iter->name, event_iter->value); + } + else if(event_iter!=events_end) + { + Time::TimeDelta t = time_since_keyframe; + if(iter->prev) + t += iter->prev->time; + for(; (event_iter!=events_end && event_iter->time<=t); ++event_iter) + observer.animation_event(0, event_iter->name, event_iter->value); + } +} + Matrix Animation::Iterator::get_matrix() const { if(!iter->prev) @@ -315,12 +358,48 @@ Animation::Loader::Loader(Animation &a, Collection &c): void Animation::Loader::init() { add("armature", &Animation::armature); + add("event", &Loader::event); + add("event", &Loader::event1i); + add("event", &Loader::event1f); + add("event", &Loader::event2f); + add("event", &Loader::event3f); + add("event", &Loader::event4f); add("interval", &Loader::interval); add("keyframe", &Loader::keyframe); add("keyframe", &Loader::keyframe_inline); add("looping", &Animation::looping); } +void Animation::Loader::event(const string &n) +{ + obj.add_event(current_time, n); +} + +void Animation::Loader::event1i(const string &n, int v) +{ + obj.add_event(current_time, n, v); +} + +void Animation::Loader::event1f(const string &n, float v) +{ + obj.add_event(current_time, n, v); +} + +void Animation::Loader::event2f(const string &n, float v0, float v1) +{ + obj.add_event(current_time, n, LinAl::Vector(v0, v1)); +} + +void Animation::Loader::event3f(const string &n, float v0, float v1, float v2) +{ + obj.add_event(current_time, n, Vector3(v0, v1, v2)); +} + +void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3) +{ + obj.add_event(current_time, n, Vector4(v0, v1, v2, v3)); +} + void Animation::Loader::keyframe(const string &n) { obj.add_keyframe(current_time, get_collection().get(n)); @@ -334,11 +413,7 @@ void Animation::Loader::keyframe_inline() else load_sub(*kf); - TimedKeyFrame tkf(obj); - tkf.time = current_time; - tkf.keyframe = kf; - obj.prepare_keyframe(tkf); - obj.keyframes.push_back(tkf); + obj.add_keyframe(current_time, kf); } void Animation::Loader::interval(float t)