OSDN Git Service

add Text Layer
[eliscolors/main.git] / ElisKeyframe.m
1 //  Copyright (c) 2009 Yanagi Asakura
2 //
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.
6 //
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:
10 //
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.
15 //
16 //  2. Altered source versions must be plainly marked as such, and must not be
17 //  misrepresented as being the original software.
18 //
19 //  3. This notice may not be removed or altered from any source
20 //  distribution.
21
22 //
23 //  ElisKeyframe.m
24 //  Elis Colors
25 //
26 //  Created by 柳 on 09/09/15.
27 //  Copyright 2009 __MyCompanyName__. All rights reserved.
28 // 
29
30 #import "ElisKeyframe.h"
31
32 static float convertQTTimeToSecond(QTTime t)
33 {
34     return (float)t.timeValue/t.timeScale;
35 }
36
37 @implementation ElisKeyframe
38
39 - (id)init
40 {
41     [super init];
42     timesAndValues = [[NSMutableDictionary alloc] init];
43     cacheTime = NAN;
44     
45 #ifdef __SNOW_LEOPARD_GCD__
46     diq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
47 #endif
48     return self;
49 }
50
51 - (void)setValueForTime:(float)value time:(QTTime)time
52 {
53     int i, j;
54     long long tmp1, tmp2;
55     
56     [timesAndValues setObject:[NSNumber numberWithFloat:value] forKey:[NSNumber numberWithFloat:convertQTTimeToSecond(time)]];
57     sortedTimes = [[timesAndValues allKeys] sortedArrayUsingSelector:@selector(compare:)];
58     // 常識的に考えて、NSArrayである必要なくね? Cの配列でよくね?
59     
60     cacheTime = NAN;
61     
62 // Cの配列時代の実装。挿入ソート?
63 //    for(i = 0; i < index-1; i++){
64 //        if(times[i].timeValue == time.timeValue){
65 //            values[i] = value;
66 //            return;
67 //        }
68 //        if(times[i].timeValue <= time.timeValue && time.timeValue <= times[i+1].timeValue){
69 //            for(j = index; j > i+1; j--){
70 //                times[j] = times[j-1];
71 //                values[j] = values[j-1];
72 //            }
73 //            times[i+1] = time;
74 //            values[i+1] = value;
75 //            return;
76 //        }
77 //    }
78 //    if(times[index-1].timeValue == time.timeValue) values[index-1] = value;
79 //    else if(times[index-1].timeValue <= time.timeValue){
80 //        times[index] = time;
81 //        values[index++] = value; 
82 //    }else{
83 //        times[index] = times[index-1];
84 //        values[index] = values[index-1];
85 //        times[index-1] = time;
86 //        values[index-1] = value;
87 //        index++;
88 //    }
89 //    return;
90 }
91
92 // いろいろすさまじい。ボトルネックになりそうだなあここ。
93 // 描画を実行する度に2回呼ばれる。キャッシュするか。
94 - (float)getValueForTime:(QTTime)time
95 {
96 //    long long now = time.timeValue;
97 //    int i;
98 //    for(i = 0; i < index-1; i++){
99 //        if(times[i].timeValue <= now && now <= times[i+1].timeValue){
100 //            return ((float)(times[i+1].timeValue - now) * values[i] +
101 //                    (float)(now - times[i].timeValue) * values[i+1])/(times[i+1].timeValue - times[i].timeValue);
102 //        }
103 //    }
104 //    if(now <= times[0].timeValue) return values[0];
105 //    if(times[index-1].timeValue <= now) return values[index-1];
106     float now = convertQTTimeToSecond(time);
107     
108     if(cacheTime == now) return cacheValue;
109     cacheTime = now;
110     
111     int i, size = [sortedTimes count];
112     
113     if(size == 1){
114         cacheValue = [[timesAndValues objectForKey:[sortedTimes objectAtIndex:0]] floatValue];
115         return cacheValue;
116     }
117     
118     if([[sortedTimes lastObject] floatValue] <= now){
119         cacheValue = [[timesAndValues objectForKey:[sortedTimes lastObject]] floatValue];
120         return cacheValue;
121     }
122     
123 //#ifdef __SNOW_LEOPARD_GCD__
124 //    dispatch_apply(size-1, diq, ^(size_t i) {
125 //        if([[sortedTimes objectAtIndex:i] floatValue] <= now &&
126 //           now <= [[sortedTimes objectAtIndex:i+1] floatValue]){
127 //            cacheValue = (([[sortedTimes objectAtIndex:i+1] floatValue] - now) * 
128 //                          [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i]] floatValue] +
129 //                          (now - [[sortedTimes objectAtIndex:i] floatValue]) * 
130 //                          [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i+1]] floatValue]) /
131 //            ([[sortedTimes objectAtIndex:i+1] floatValue] - [[sortedTimes objectAtIndex:i] floatValue]);
132 //        }
133 //    });
134 //    return cacheValue;
135 //#else
136     for(i = 0; i < size-1; i++){
137         if([[sortedTimes objectAtIndex:i] floatValue] <= now &&
138            now <= [[sortedTimes objectAtIndex:i+1] floatValue]){
139             cacheValue = (([[sortedTimes objectAtIndex:i+1] floatValue] - now) * 
140                 [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i]] floatValue] +
141                 (now - [[sortedTimes objectAtIndex:i] floatValue]) * 
142                 [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i+1]] floatValue]) /
143             ([[sortedTimes objectAtIndex:i+1] floatValue] - [[sortedTimes objectAtIndex:i] floatValue]);
144             return cacheValue;
145         }
146     }
147 //#endif
148
149     return cacheValue;
150 }
151
152 - (void)encodeWithCoder:(NSCoder*)encoder
153 {
154     [encoder encodeObject:timesAndValues forKey:@"timesAndValues"];
155     [encoder encodeObject:sortedTimes forKey:@"sortedTimes"];
156 }
157
158 - (id)initWithCoder:(NSCoder*)coder
159 {
160     timesAndValues = [coder decodeObjectForKey:@"timesAndValues"];
161     sortedTimes = [coder decodeObjectForKey:@"sortedTimes"];
162     cacheTime = NAN;
163     
164     return self;
165 }
166
167 @end