/
FlxFilterFrames.hx
226 lines (185 loc) · 5.33 KB
/
FlxFilterFrames.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package flixel.graphics.frames;
import openfl.display.BitmapData;
import openfl.geom.Point;
import openfl.geom.Rectangle;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxFramesCollection.FlxFrameCollectionType;
import flixel.util.FlxColor;
import openfl.filters.BitmapFilter;
/**
* Frames collection which you can apply bitmap filters to.
* WARNING: this frame collection doesn't use caching, so be careful or you will "leak" out memory very fast.
* You should destroy frames collections of this type manually.
*/
class FlxFilterFrames extends FlxFramesCollection
{
static var point:Point = new Point();
static var rect:Rectangle = new Rectangle();
/**
* Generates new frames collection from specified frames.
*
* @param frames Frames collection to generate filters for.
* @param widthInc How much frames should expand horizontally.
* @param heightInc How much frames should expend vertically.
* @param filters Optional filters array to apply.
* @return New frames collection which you can apply filters to.
*/
public static inline function fromFrames(frames:FlxFramesCollection, widthInc:Int = 0, heightInc:Int = 0, ?filters:Array<BitmapFilter>):FlxFilterFrames
{
return new FlxFilterFrames(frames, widthInc, heightInc, filters);
}
/**
* Original frames collection
*/
public var sourceFrames(default, null):FlxFramesCollection;
/**
* How much frames should expand horizontally
*/
public var widthInc(default, null):Int = 0;
/**
* How much frames should expand vertically
*/
public var heightInc(default, null):Int = 0;
/**
* Filters applied to these frames
*/
public var filters(default, set):Array<BitmapFilter>;
function new(sourceFrames:FlxFramesCollection, widthInc:Int = 0, heightInc:Int = 0, ?filters:Array<BitmapFilter>)
{
super(null, FlxFrameCollectionType.FILTER);
this.sourceFrames = sourceFrames;
widthInc = (widthInc >= 0) ? widthInc : 0;
heightInc = (heightInc >= 0) ? heightInc : 0;
widthInc = 2 * Math.ceil(0.5 * widthInc);
heightInc = 2 * Math.ceil(0.5 * heightInc);
this.widthInc = widthInc;
this.heightInc = heightInc;
this.filters = (filters == null) ? [] : filters;
genFrames();
applyFilters();
}
/**
* Just helper method which "centers" sprite offsets
*
* @param spr Sprite to apply this frame collection.
* @param saveAnimations Whether to save sprite's animations or not.
* @param updateFrames Whether to regenerate frame `BitmapData`s or not.
*/
public function applyToSprite(spr:FlxSprite, saveAnimations:Bool = false, updateFrames:Bool = false):Void
{
if (updateFrames)
set_filters(filters);
var w:Float = spr.width;
var h:Float = spr.height;
spr.setFrames(this, saveAnimations);
spr.offset.add(0.5 * widthInc, 0.5 * heightInc);
spr.setSize(w, h);
}
function genFrames():Void
{
var canvas:BitmapData;
var graph:FlxGraphic;
var filterFrame:FlxFrame;
for (frame in sourceFrames.frames)
{
canvas = new BitmapData(Std.int(frame.sourceSize.x + widthInc), Std.int(frame.sourceSize.y + heightInc), true, FlxColor.TRANSPARENT);
graph = FlxGraphic.fromBitmapData(canvas, false, null, false);
filterFrame = graph.imageFrame.frame;
frames.push(filterFrame);
if (frame.name != null)
{
filterFrame.name = frame.name;
framesByName.set(frame.name, filterFrame);
}
}
regenBitmaps(false);
}
/**
* Adds a filter to this frames collection.
*
* @param filter The filter to be added.
*/
public inline function addFilter(filter:BitmapFilter):Void
{
if (filter != null)
{
filters.push(filter);
applyFilter(filter);
}
}
/**
* Removes a filter from this frames collection.
*
* @param filter The filter to be removed.
*/
public function removeFilter(filter:BitmapFilter):Void
{
if (filters.length == 0 || filter == null)
return;
if (filters.remove(filter))
regenAndApplyFilters();
}
/**
* Removes all filters from the frames.
*/
public function clearFilters():Void
{
if (filters.length == 0)
return;
filters.splice(0, filters.length);
regenBitmaps();
}
function regenAndApplyFilters():Void
{
regenBitmaps();
applyFilters();
}
function regenBitmaps(fill:Bool = true):Void
{
var numFrames:Int = frames.length;
var frame:FlxFrame;
var sourceFrame:FlxFrame;
var frameOffset:Point = point;
for (i in 0...numFrames)
{
sourceFrame = sourceFrames.frames[i];
frame = frames[i];
if (fill)
frame.parent.bitmap.fillRect(frame.parent.bitmap.rect, FlxColor.TRANSPARENT);
frameOffset.setTo(widthInc * 0.5, heightInc * 0.5);
sourceFrame.paint(frame.parent.bitmap, frameOffset, true);
}
}
function applyFilter(filter:BitmapFilter)
{
var bitmap:BitmapData;
for (frame in frames)
{
point.setTo(0, 0);
rect.setTo(0, 0, frame.sourceSize.x, frame.sourceSize.y);
bitmap = frame.parent.bitmap;
bitmap.applyFilter(bitmap, rect, point, filter);
}
}
function applyFilters():Void
{
for (filter in filters)
applyFilter(filter);
}
override public function destroy():Void
{
sourceFrames = null;
filters = null;
for (frame in frames)
frame.parent.destroy();
super.destroy();
}
function set_filters(value:Array<BitmapFilter>):Array<BitmapFilter>
{
filters = value;
if (value != null)
regenAndApplyFilters();
return filters;
}
}