~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
package ui;

import flash.events.MouseEvent;
import flash.display.Sprite;

/** 
 * Implements a common base for button like items. This allows the creation of custom
 * draw classes far easier than working with Button and simpler than mimic'ing all behaviour.
 *
 * You must override _resize
 *	It will be called whenever the image might change (status change of some kind). You 
 * may use the down and over variables to determine how to draw the button.
 */
class ButtonBase extends Sprite, implements Widget
{
	var down : Bool;	//the button is in the down position
	var over : Bool; //the mouse is over the button
	
	var event : Action; //the action to perform on clicking
	
	function new( event : Action )
	{
		super();
		MixinWidget();
		
		this.event = event;
		down = false;
		over = false;
		enabled = true;
		
		//setup flash
		buttonMode = true;
		useHandCursor = true;	//look clickable
		mouseChildren = false;	//so added images don't change cursor
		
		addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true /*weak*/ );
		addEventListener( MouseEvent.MOUSE_UP, onMouseUp,false, 0, true /*weak*/ );
		addEventListener( MouseEvent.MOUSE_OVER, onMouseOver,false, 0, true /*weak*/ );
		addEventListener( MouseEvent.MOUSE_OUT, onMouseOut,false, 0, true /*weak*/ );
	}
	
	function onMouseDown( evt : MouseEvent )
	{
		evt.stopPropagation();	//we hide this from our visual ancestors
		if( !enabled )
			return;
			
		down = true;
		update();
	}
	
	function onMouseUp( evt : MouseEvent )
	{
		evt.stopPropagation();	//we hide this from our visual ancestors
		if( !enabled )
			return;
			
		var wasDown = down;
		down = false;
		update();
		
		//only handle if we were actually down, to prevent spurious half-events from activating
		if( wasDown )
			Action.handle( this, event );
	}
	
	function onMouseOver( evt : MouseEvent )
	{
		over = true;
		down = evt.buttonDown;
		
		if( !enabled )
			return;
			
		update();
	}
	
	function onMouseOut( evt : MouseEvent )
	{
		over = false;
		down = false;
		
		if( !enabled )
			return;
			
		update();
	}
	
	function _resize( w : Float, h : Float )
	{
		Assert.pureVirtual();
	}
	
	define(`AbstractGFX')
	include(`MixinWidget.ihx')
}