1 // Copyright (c) 2009 Yanagi Asakura
3 // This software is provided 'as-is', without any express or implied
4 // warranty. In no event will the authors be held liable for any damages
5 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
11 // 1. The origin of this software must not be misrepresented; you must not
12 // claim that you wrote the original software. If you use this software
13 // in a product, an acknowledgment in the product documentation would be
14 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
19 // 3. This notice may not be removed or altered from any source
26 // Created by 柳 on 09/09/15.
27 // Copyright 2009 __MyCompanyName__. All rights reserved.
30 #import "ElisKeyframe.h"
32 static float convertQTTimeToSecond(QTTime t)
34 return (float)t.timeValue/t.timeScale;
37 @implementation ElisKeyframe
42 timesAndValues = [[NSMutableDictionary alloc] init];
47 - (void)setValueForTime:(float)value time:(QTTime)time
52 [timesAndValues setObject:[NSNumber numberWithFloat:value] forKey:[NSNumber numberWithFloat:convertQTTimeToSecond(time)]];
53 sortedTimes = [[timesAndValues allKeys] sortedArrayUsingSelector:@selector(compare:)];
54 // 常識的に考えて、NSArrayである必要なくね? Cの配列でよくね?
59 // for(i = 0; i < index-1; i++){
60 // if(times[i].timeValue == time.timeValue){
64 // if(times[i].timeValue <= time.timeValue && time.timeValue <= times[i+1].timeValue){
65 // for(j = index; j > i+1; j--){
66 // times[j] = times[j-1];
67 // values[j] = values[j-1];
70 // values[i+1] = value;
74 // if(times[index-1].timeValue == time.timeValue) values[index-1] = value;
75 // else if(times[index-1].timeValue <= time.timeValue){
76 // times[index] = time;
77 // values[index++] = value;
79 // times[index] = times[index-1];
80 // values[index] = values[index-1];
81 // times[index-1] = time;
82 // values[index-1] = value;
88 // いろいろすさまじい。ボトルネックになりそうだなあここ。
89 // 描画を実行する度に2回呼ばれる。キャッシュするか。
90 - (float)getValueForTime:(QTTime)time
92 // long long now = time.timeValue;
94 // for(i = 0; i < index-1; i++){
95 // if(times[i].timeValue <= now && now <= times[i+1].timeValue){
96 // return ((float)(times[i+1].timeValue - now) * values[i] +
97 // (float)(now - times[i].timeValue) * values[i+1])/(times[i+1].timeValue - times[i].timeValue);
100 // if(now <= times[0].timeValue) return values[0];
101 // if(times[index-1].timeValue <= now) return values[index-1];
102 float now = convertQTTimeToSecond(time);
104 if(cacheTime == now) return cacheValue;
107 int i, size = [sortedTimes count];
110 cacheValue = [[timesAndValues objectForKey:[sortedTimes objectAtIndex:0]] floatValue];
114 if([[sortedTimes lastObject] floatValue] <= now){
115 cacheValue = [[timesAndValues objectForKey:[sortedTimes lastObject]] floatValue];
119 for(i = 0; i < size-1; i++){
120 if([[sortedTimes objectAtIndex:i] floatValue] <= now &&
121 now <= [[sortedTimes objectAtIndex:i+1] floatValue]){
122 cacheValue = (([[sortedTimes objectAtIndex:i+1] floatValue] - now) *
123 [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i]] floatValue] +
124 (now - [[sortedTimes objectAtIndex:i] floatValue]) *
125 [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i+1]] floatValue]) /
126 ([[sortedTimes objectAtIndex:i+1] floatValue] - [[sortedTimes objectAtIndex:i] floatValue]);
135 - (void)encodeWithCoder:(NSCoder*)encoder
137 [encoder encodeObject:timesAndValues forKey:@"timesAndValues"];
138 [encoder encodeObject:sortedTimes forKey:@"sortedTimes"];
141 - (id)initWithCoder:(NSCoder*)coder
143 timesAndValues = [coder decodeObjectForKey:@"timesAndValues"];
144 sortedTimes = [coder decodeObjectForKey:@"sortedTimes"];