1require 'mingc'
2require 'final'        # for Ruby 1.4
3include Mingc
4include ObjectSpace
5
6class SWFBase
7	attr_accessor :this
8
9	def initialize(this)
10		@this = this
11	end
12end
13
14class SWFRect <SWFBase
15	def initialize(minX, maxX, minY, maxY)
16		super newSWFRect(minX, maxX, minY, maxY)
17		define_finalizer(self) {destroySWFRect(@this)}
18	end
19
20	def getWidth
21		return SWFRect_getWidth(@this)
22	end
23
24	def getHeight
25		return SWFRect_getHeight(@this)
26	end
27end
28
29class SWFShape <SWFBase
30	def initialize(o = nil)
31		@fills = []
32		if o.nil?
33			super newSWFShape()
34		else
35			super o
36		end
37		define_finalizer(self) {destroySWFShape(@this)}
38	end
39
40	def setLine(width, r, g, b, a=0xff)
41		return SWFShape_setLine(@this, width, r, g, b, a)
42	end
43
44	# I know there's probably a better way to do this..
45	def addFill(arg1, arg2=0, arg3=nil, arg4=0xff)
46		if arg3 != nil
47			return SWFFill.new(SWFShape_addSolidFill(@this, arg1, arg2, arg3, arg4))
48		end
49		if arg1.is_a?(SWFGradient)
50			# XXX - have to keep reference to gradient so it's not disposed
51			@fills.push(arg1)
52			return SWFFill.new(SWFShape_addGradientFill(@this, arg1.this, arg2))
53		end
54
55		if arg1.is_a?(SWFBitmap)
56			# XXX - have to keep reference to bitmap so it's not disposed
57			@fills.push(arg1)
58			return SWFFill.new(SWFShape_addBitmapFill(@this, arg1.this, arg2))
59		else
60			raise "bad argument to SWFShape::addFill"
61		end
62	end
63
64	def setLeftFill(fill)
65		SWFShape_setLeftFill(@this, fill.this)
66	end
67
68	def setRightFill(fill)
69		SWFShape_setRightFill(@this, fill.this)
70	end
71
72	def movePenTo(x, y)
73		SWFShape_movePenTo(@this, x, y)
74	end
75
76	def drawLineTo(x, y)
77		SWFShape_drawLineTo(@this, x, y)
78	end
79
80	def drawLine(dx, dy)
81		SWFShape_drawLine(@this, dx, dy)
82	end
83
84	def drawRect(rect)
85		SWFShape_drawRect(@this, rect.this)
86	end
87
88	def drawCurveTo(bx, by, cx, cy, dx=nil, dy=nul)
89		if dx != nil
90			SWFShape_drawCubicTo(@this, bx, by, cx, cy, dx, dy)
91		else
92			SWFShape_drawCurveTo(@this, bx, by, cx, cy)
93		end
94	end
95
96	def drawCurve(bx, by, cx, cy, dx=nil, dy=nil)
97		if dx != nil
98			SWFShape_drawCubic(@this, bx, by, cx, cy, dx, dy)
99		else
100			SWFShape_drawCurve(@this, bx, by, cx, cy)
101		end
102	end
103
104	def drawCubicTo(bx, by, cx, cy, dx, dy)
105		SWFShape_drawCubicTo(@this, bx, by, cx, cy, dx, dy)
106	end
107
108	def drawCurve(bx, by, cx, cy, dx, dy)
109		SWFShape_drawCubic(@this, bx, by, cx, cy, dx, dy)
110	end
111
112	def drawGlyph(font, char, size=0)
113		SWFShape_drawSizedGlyph(@this, font.this, char[0], size) #ord(char[0]))
114	end
115
116	def addSolidFill(r, g, b, a=0xff)
117		return SWFFill.new(SWFShape_addSolidFill(@this, r, g, b, a))
118	end
119
120	def addGradientFill(gradient, flags)
121		return SWFShape_addGradientFill(@this, gradient.this, flags)
122	end
123
124	def addBitmapFill(bitmap, flags)
125		return SWFShape_addBitmapFill(@this, bitmap.this, flags)
126	end
127end
128
129class SWFFill <SWFBase
130	def initialize(o)
131		define_finalizer(self) {destroySWFFill(@this)}
132		super o
133	end
134
135	def rotateTo(degrees)
136		SWFFill_rotateTo(@this, degrees)
137	end
138
139	def moveTo(x, y)
140		SWFFill_moveTo(@this, x, y)
141	end
142
143	def scaleTo(xScale, yScale=nil)
144		if yScale.nil?
145			SWFFill_scaleXYTo(@this, xScale, xScale)
146		else
147			SWFFill_scaleXYTo(@this, xScale, yScale)
148		end
149	end
150
151	def scaleXTo(xScale)
152		SWFFill_scaleXTo(@this, xScale)
153	end
154
155	def scaleYTo(yScale)
156		SWFFill_scaleYTo(@this, yScale)
157	end
158
159	def skewXTo(x)
160		SWFFill_skewXTo(@this, x);
161	end
162
163	def skewYTo(y)
164		SWFFill_skewYTo(@this, y)
165	end
166
167	def setMatrix(a, b, c, d, x, y)
168		SWFFill_setMatrix(@this, a, b, c, d, x, y)
169	end
170end
171
172class SWFDisplayItem <SWFBase
173	def rotate(degrees)
174		SWFDisplayItem_rotate(@this, degrees)
175	end
176
177	def rotateTo(degrees)
178		SWFDisplayItem_rotateTo(@this, degrees)
179	end
180
181	def move(x, y)
182		SWFDisplayItem_move(@this, x, y)
183	end
184
185	def moveTo(x, y)
186		SWFDisplayItem_moveTo(@this, x, y)
187	end
188
189	def scale(xScale, yScale)
190		SWFDisplayItem_scale(@this, xScale, yScale)
191	end
192
193	def scaleTo(xScale, yScale)
194		SWFDisplayItem_scaleTo(@this, xScale, yScale)
195	end
196
197	def skewX(x)
198		SWFDisplayItem_skewX(@this, x)
199	end
200
201	def skewXTo(x)
202		SWFDisplayItem_skewXTo(@this, x);
203	end
204
205	def skewY(y)
206		SWFDisplayItem_skewY(@this, y)
207	end
208
209	def skewYTo(y)
210		SWFDisplayItem_skewYTo(@this, y)
211	end
212
213	def setMatrix(a, b, c, d, x, y)
214		SWFDisplayItem_setMatrix(@this, a, b, c, d, x, y)
215	end
216
217	def setName(name)
218		SWFDisplayItem_setName(@this, name)
219	end
220
221	def setRatio(ratio)
222		SWFDisplayItem_setRatio(@this, ratio)
223	end
224
225	def getDepth
226		return SWFDisplayItem_getDepth(@this)
227	end
228
229	def setDepth(depth)
230		SWFDisplayItem_setDepth(@this, depth)
231	end
232
233	def addColor(r, g, b, a=0)
234		SWFDisplayItem_addColor(@this, r, g, b, a)
235	end
236
237	def multColor(r, g, b, a=1.0)
238		SWFDisplayItem_multColor(@this, r, g, b, a)
239	end
240
241	def remove
242		SWFDisplayItem_remove(@this)
243	end
244end
245
246class SWFMovie <SWFBase
247	def initialize
248		super newSWFMovie()
249		@blocks = []
250		define_finalizer(self) {destroySWFMovie(@this)}
251	end
252
253	def setRate(rate)
254		SWFMovie_setRate(@this, rate)
255	end
256
257	def setDimension(x, y)
258		SWFMovie_setDimension(@this, x, y)
259	end
260
261	def setBackground(r, g, b)
262		SWFMovie_setBackground(@this, r, g, b)
263	end
264
265	def setFrames(totalFrames)
266		SWFMovie_setNumberOfFrames(@this, totalFrames)
267	# or:
268	end
269
270	def setNumberOfFrames(totalFrames)
271		SWFMovie_setNumberOfFrames(@this, totalFrames)
272	end
273
274	def nextFrame
275		SWFMovie_nextFrame(@this)
276	end
277
278	def add(block)
279		@blocks.push(block)
280		return SWFDisplayItem.new(SWFMovie_add(@this, block.this))
281	end
282
283	def remove(item)
284		SWFMovie_remove(@this, item.this)
285	end
286
287	def streamMp3(sound)
288		@blocks.push(sound)
289		SWFMovie_setSoundStream(@this, sound.this);
290	end
291
292	# deprecated:
293	def setSoundStream(sound)
294		@blocks.push(sound)
295		SWFMovie_setSoundStream(@this, sound.this);
296	end
297
298	def output
299		return SWFMovie_simpleOutput(@this)
300	end
301
302	# deprecated: (?)
303	def simpleOutput
304		return SWFMovie_simpleOutput(@this)
305	end
306
307	def save(filename)
308		SWFMovie_save(@this, filename)
309	end
310
311	def labelFrame(label)
312		SWFMovie_labelFrame(@this, label)
313	end
314end
315
316class SWFSprite <SWFBase
317	def initialize
318		super newSWFMovieClip()
319		@blocks = []
320		define_finalizer(self) {destroySWFMovieClip(@this)}
321	end
322
323	def setNumberOfFrames(frames)
324		SWFMovieClip_setNumberOfFrames(@this, frames)
325	end
326
327	def add(block)
328		@blocks.push(block)
329		return SWFDisplayItem.new(SWFMovieClip_add(@this, block.this))
330	end
331
332	def remove(item)
333		SWFMovieClip_remove(@this, item.this)
334	end
335
336	def nextFrame
337		SWFMovieClip_nextFrame(@this)
338	end
339
340	def labelFrame(label)
341		SWFMovieClip_labelFrame(@this, label)
342	end
343end
344
345# deprecated
346class SWFMovieClip <SWFSprite
347
348class SWFGradient <SWFBase
349	def initialize
350		super newSWFGradient()
351	end
352
353	def addEntry(ratio, r, g, b, a)
354		return SWFGradient_addEntry(@this, ratio, r, g, b, a)
355	end
356end
357
358class SWFMorph <SWFBase
359	def initialize
360		super newSWFMorphShape()
361		define_finalizer(self) {destroySWFMorph(@this)}
362	end
363
364	def getShape1
365		# have to keep a reference so it doesn't scope out
366		@shape1 = SWFShape.new(SWFMorph_getShape1(@this))
367		return @shape1
368	end
369
370	def getShape2
371		@shape2 = SWFShape.new(SWFMorph_getShape2(@this))
372		return @shape2
373	end
374end
375
376# deprecated
377class SWFMorphShape <SWFMorph
378
379class SWFFont <SWFBase
380
381	def initialize(name)
382		if name =~ /\.fdb$/
383			@browserfont = 0
384			super loadSWFFontFromFile(open(name))
385		else
386			@browserfont = 1
387			super newSWFBrowserFont(name)
388		end
389
390		define_finalizer(self) {
391			if @browserfont == 1
392				destroySWFBrowserFont(@this)
393			else
394				destroySWFFont(@this)
395			end
396		}
397	end
398
399	def getAscent
400		return SWFFont_getAscent(@this)
401	end
402
403	def getDescent
404		return SWFFont_getDescent(@this)
405	end
406
407	def getLeading
408		return SWFFont_getLeading(@this)
409	end
410
411	def getWidth(string)
412		return SWFFont_getWidth(@this, string)
413	end
414end
415
416class SWFBitmap <SWFBase
417	def initialize(fname, alpha=nil)
418		if fname =~ /\.dbl$/i
419			@file = open(fname)
420			super newSWFDBLBitmap(@file)
421		elsif fname =~ /\.jpg$/i
422			@file = open(fname)
423			if alpha.nil?
424				super newSWFJpegBitmap(@file)
425			else
426				@alpha = open(alpha)
427				super newSWFJpegWithAlpha(@file, @alpha)
428			end
429		end
430		define_finalizer(self) {destroySWFBitmap(@this)}
431	end
432
433	def getWidth
434		return SWFBitmap_getWidth(@this)
435	end
436
437	def getHeight
438		return SWFBitmap_getHeight(@this)
439	end
440end
441
442# deprecated
443class SWFDBLBitmap <SWFBitmap
444	def initialize(fname)
445		super newSWFDBLBitmap(open(fname))
446	end
447end
448
449# deprecated
450class JpegBitmap <SWFBitmap
451	def initialize(fname, alpha=nil)
452		if alpha.nil?
453			super newSWFJpegBitmap(open(fname))
454		else
455			super newSWFJpegWithAlpha(open(fname), open(alpha))
456 		end
457 	end
458end
459
460class SWFText <SWFBase
461	def initialize
462		super newSWFText2()
463		define_finalizer(self) {destroySWFText(@this)}
464	end
465
466	def setFont(font)
467		SWFText_setFont(@this, font.this)
468	end
469
470	def setHeight(height)
471		SWFText_setHeight(@this, height)
472	end
473
474	def moveTo(x, y)
475		SWFText_moveTo(@this, x, y)
476	end
477
478	def setColor(r, g, b, a=0xff)
479		SWFText_setColor(@this, r, g, b, a)
480	end
481
482	def addString(s, advance=nil)
483		SWFText_addString(@this, s, advance)
484	end
485
486	def setSpacing(spacing)
487		SWFText_setSpacing(@this, spacing)
488	end
489
490	def getAscent
491		return SWFText_getAscent(@this)
492	end
493
494	def getDescent
495		return SWFText_getDescent(@this)
496	end
497
498	def getLeading
499		return SWFText_getLeading(@this)
500	end
501
502	def getWidth(string)
503		return SWFText_getWidth(@this, string)
504	end
505end
506
507class SWFTextField <SWFBase
508 	def initialize
509		super newSWFTextField()
510		define_finalizer(self) {destroySWFTextField(@this)}
511	end
512
513	def setFont(font)
514		SWFTextField_setFont(@this, font.this)
515	end
516
517	def setBounds(width, height)
518		SWFTextField_setBounds(@this, width, height)
519 	end
520
521	def setFlags(flags)
522		SWFTextField_setFlags(@this, flags)
523 	end
524
525	def setColor(r, g, b, a=0xff)
526		SWFTextField_setColor(@this, r, g, b, a)
527	end
528
529	def setVariableName(name)
530		SWFTextField_setVariableName(@this, name)
531	end
532
533	def addString(string)
534		SWFTextField_addString(@this, string)
535	end
536
537	def setHeight( height)
538		SWFTextField_setHeight(@this,  height)
539	end
540
541	def setLeftMargin(leftMargin)
542		SWFTextField_setLeftMargin(@this, leftMargin)
543	end
544
545	def setRightMargin(rightMargin)
546		SWFTextField_setRightMargin(@this, rightMargin)
547	end
548
549	def setIndentation(indentation)
550		SWFTextField_setIndentation(@this, indentation)
551	end
552
553	def setLineSpacing(lineSpacing)
554		SWFTextField_setLineSpacing(@this, lineSpacing)
555	end
556
557	def setAlignment(alignment)
558		SWFTextField_setAlignment(@this,  alignment)
559	# or just
560	end
561
562	def align(alignment)
563		SWFTextField_setAlignment(@this,  alignment)
564	end
565
566	def setLength(length)
567		SWFTextField_setLength(@this, length)
568	end
569end
570
571class SWFSound <SWFBase
572	def initialize(fname)
573		@file = open(fname)
574		super newSWFSound(@file)
575	end
576	# display list destroys this..
577end
578
579class SWFAction <SWFBase
580	def initialize(script)
581		super compileSWFActionCode(script)
582	end
583	# assigned object will destroy this..
584end
585
586class SWFButton <SWFBase
587 	def initialize
588		super newSWFButton()
589		@shapes = []
590		define_finalizer(self) {destroySWFButton(@this)}
591	end
592
593	def setUp(character)
594		addShape(character, SWFBUTTON_UP)
595	end
596
597	def setDown(character)
598		addShape(character, SWFBUTTON_DOWN)
599	end
600
601	def setOver(character)
602		addShape(character, SWFBUTTON_OVER)
603	end
604
605	def setHit(character)
606		addShape(character, SWFBUTTON_HIT)
607	end
608
609	def addShape(character, flags)
610		@shapes.push(character)
611		SWFButton_addShape(@this, character.this, flags)
612	end
613
614	def addAction(action, flags)
615		SWFButton_addAction(@this, action.this, flags)
616	end
617
618	def SWFButton.SWFBUTTON_KEYPRESS(c)
619		return swfButton_keypress(c)
620	end
621end
622