~eda-qa/dhlib/main

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
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package ui.anim;

import ui.Widget;
import ui.Action;

/*hidden*/ class __FXDescriptor implements FXDescriptor
{
	public var cl : Class<Dynamic>;
	public var seq : String;
	public var args : Dynamic;
	
	public function new() { }
}

/**
 * An assistance class for using effects/transformations on widgets on
 * the screen.
 */
class FXBuilder
{
	/**
	 * Used to create an FX description which can be used by other functions
	 * in this class.  This provides future compatibility to other methods of
	 * providing effects.
	 */
	static public function createActorFX( cl : Class<Dynamic>, seq : String, ?args : Dynamic ) : FXDescriptor
	{
		var fx = new __FXDescriptor();
		fx.cl = cl;
		fx.seq = seq;
		fx.args = args;
		return fx;
	}
	
	/**
	 * Ends the lifetime of a Widget with the provided FX.  At the end of the
	 * FX the widget will have been removed from the parent.
	 *
	 * @param widget [in] the widget to apply the fx to
	 * @param fx [in] an FXDescriptor as created by FXBuilder
	 * @param completion [in] what to do when the FX is complete
	 */
	static public function endWidget( widget : Widget, fx : FXDescriptor, ?completion : ui.Action )
	{
		ASSERT_IS( fx, __FXDescriptor );
		var _fx = cast( fx, __FXDescriptor );
		ASSERT_NOTNULL( widget.getNative().parent );
		var p = widget.getNative().parent;
		p.removeChild( widget.getNative() );
		
		//translate position (TODO: Handle scaling and non-0 origins, etc...)
		
		var x = widget.getNative().x;
		var y = widget.getNative().y;
		widget.getNative().x = 0;
		widget.getNative().y = 0;
		
		var c : ui.anim.Actor = Type.createInstance( 
			_fx.cl, 
			[ DynamicUtil.merge( { item: widget }, _fx.args ) ] 
			);
		//resize and move container
		c.resize( widget.getNative().width, widget.getNative().height );
		c.move( x, y );
		p.addChild( c );
		c.actorSetSequence( _fx.seq, Action.bind2( __endFX, c, completion ) );
	}
	
	/**
	 * Begins the lifetime of a Widget with the provided FX.  The parent of the
	 * widget should already be set, and the initial sizing complete.
	 */
	static public function beginWidget( widget : Widget, fx : FXDescriptor, ?completion : Action )
	{
		ASSERT_IS( fx, __FXDescriptor );
		var _fx = cast( fx, __FXDescriptor );
		ASSERT_NOTNULL( widget.getNative().parent );
		var p = widget.getNative().parent;
		p.removeChild( widget.getNative() );
		
		//translate position (TODO: Handle scaling and non-0 origins, etc...)
		
		var x = widget.getNative().x;
		var y = widget.getNative().y;
		widget.getNative().x = 0;
		widget.getNative().y = 0;
		
		var c : ui.anim.Actor = Type.createInstance( 
			_fx.cl, 
			[ DynamicUtil.merge( { item: widget }, _fx.args ) ] 
			);
		//resize and move container
		c.resize( widget.getNative().width, widget.getNative().height );
		c.move( x, y );
		p.addChild( c );
		c.actorSetSequence( _fx.seq, Action.bind1( __beginFX, {
			parent: p,
			actor: c,
			completion: completion,
			widget: widget,
			x: x,
			y: y
			} ) );
	}
	
	/**
	 * onWidget does an FX on an existing widget.  Though at the moment this does
	 * the same thing as beginWidget, there are some exception cases whcih will be
	 * implemented later which will diverge the two, thus, use this call if the widget
	 * is not a new one.
	 */
	static public function onWidget( widget : Widget, fx : FXDescriptor, ?completion : Action )
	{
		beginWidget( widget, fx, completion );
	}
	
	/*hidden*/ static function __beginFX( data : Dynamic )
	{
		data.actor.destroy();
		data.parent.addChild( data.widget.getNative() );
		data.widget.move( data.x, data.y );
		if( data.completion != null )
			Action.handle( data.widget.getNative(), data.completion );
	}
	
	/*hidden*/ static function __endFX( act : ui.anim.Actor, action : ui.Action )
	{
		act.parent.removeChild( act );
		if( action != null )
			ui.Action.handle( act.getNative(), action );
	}
	
}