Skip to content

Commit 23a9a73

Browse files
author
as
committed
added HitButton.as and DragHitButton.as
1 parent 0aa3f3c commit 23a9a73

File tree

5 files changed

+318
-1
lines changed

5 files changed

+318
-1
lines changed

bin/Falcon.swc

1.57 MB
Binary file not shown.
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package com.hendrix.feathers.controls.flex
2+
{
3+
import flash.geom.Point;
4+
5+
import starling.display.DisplayObject;
6+
import starling.events.Event;
7+
import starling.events.Touch;
8+
import starling.events.TouchEvent;
9+
import starling.events.TouchPhase;
10+
11+
/**
12+
* a draggable <code>HitButton</code> with the following features
13+
* <lu>
14+
* <li/>this component is <code>HitButton</code> and draggable.
15+
* <li/>register <code>DisplayObject</code>s to detect when this object was dropped on them.
16+
* <li/>listen to <code>DROP, DROPPED_ON, DRAG</code> events.
17+
*
18+
*
19+
* @author Tomer Shalev
20+
*
21+
* @see HitButton
22+
*/
23+
public class DragHitButton extends HitButton
24+
{
25+
/**
26+
* Event for Dropping the this button
27+
*/
28+
public static const DROP: String = "drop";
29+
[Event(name="drop", type="starling.events.Event")]
30+
31+
/**
32+
* Event for Dropping this button on one of the registered DisplayObject.
33+
*/
34+
public static const DROPPED_ON: String = "droppedOn";
35+
[Event(name="droppedOn", type="starling.events.Event")]
36+
37+
/**
38+
* Event for Dragging this button.
39+
*/
40+
public static const DRAG: String = "drag";
41+
[Event(name="drag", type="starling.events.Event")]
42+
43+
/**
44+
* a helper <code>Point</code>
45+
*/
46+
private var _point_helper: Point = null;
47+
private var _isDragged: Boolean = false;
48+
49+
private var _expected_flag: Boolean = false;
50+
51+
private var callback_onComplete: Function = null;
52+
private var point_aux: Point = new Point();
53+
54+
private var _pOriginal_x: Number = NaN;
55+
private var _pOriginal_y: Number = NaN;
56+
/**
57+
* registered vector of <code>DisplayObject</code> to detect dropping on.
58+
*/
59+
private var _dop_dropped: Vector.<DisplayObject> = null;
60+
61+
public function DragHitButton()
62+
{
63+
super();
64+
65+
_dop_dropped = new Vector.<DisplayObject>();
66+
}
67+
68+
override public function set x(value:Number):void
69+
{
70+
if(isNaN(_pOriginal_x))
71+
_pOriginal_x = value;
72+
73+
super.x = value;
74+
}
75+
76+
override public function set y(value:Number):void
77+
{
78+
if(isNaN(_pOriginal_y))
79+
_pOriginal_y = value;
80+
81+
super.y = value;
82+
}
83+
84+
public function reset():void
85+
{
86+
if(!isNaN(_pOriginal_x))
87+
x = _pOriginal_x;
88+
if(!isNaN(_pOriginal_y))
89+
y = _pOriginal_y;
90+
}
91+
92+
/**
93+
* is dragged
94+
*
95+
* @return <code>true/false</code>
96+
*
97+
*/
98+
public function get isDragged():Boolean
99+
{
100+
return _isDragged;
101+
}
102+
103+
/**
104+
* add <code>DisplayObject</code> to detect this object was dropped on.
105+
*
106+
* @param dop a <code>DisplayObject</code>
107+
*
108+
*/
109+
public function registerDisplayObject(dop: DisplayObject):void
110+
{
111+
_dop_dropped.push(dop);
112+
}
113+
114+
override protected function feathersControl_addedToStageHandler(event:Event):void
115+
{
116+
super.feathersControl_addedToStageHandler(event);
117+
118+
_point_helper = _point_helper ? _point_helper : new Point();
119+
120+
addEventListener(TouchEvent.TOUCH, touchHandler);
121+
}
122+
123+
override protected function feathersControl_removedFromStageHandler(event:Event):void
124+
{
125+
super.feathersControl_removedFromStageHandler(event);
126+
127+
removeEventListener(TouchEvent.TOUCH, touchHandler);
128+
}
129+
130+
private function touchHandler(event: TouchEvent = null):void
131+
{
132+
if(event == null)
133+
return;
134+
135+
var touch: Touch = event.getTouch(stage);
136+
137+
touch.getLocation(stage, _point_helper);
138+
139+
var target: HitButton = event.target as HitButton;
140+
141+
if(touch.phase == TouchPhase.MOVED) {
142+
target.x = _point_helper.x - target.width/2;
143+
target.y = _point_helper.y - target.height/2;
144+
145+
if(hasEventListener(DRAG))
146+
dispatchEventWith(DRAG, false, _point_helper);
147+
148+
_isDragged = true;
149+
}
150+
else if(touch.phase == TouchPhase.ENDED) {
151+
if(_isDragged) {
152+
if(hasEventListener(DROP))
153+
dispatchEventWith(DROP, false, _point_helper);
154+
155+
if(hasEventListener(DROPPED_ON)) {
156+
for (var ix: int = 0; ix < _dop_dropped.length; ix++)
157+
{
158+
if(_dop_dropped[ix].getBounds(stage).containsPoint(_point_helper))
159+
dispatchEventWith(DROPPED_ON, false, {dropped: target, dropped_on: _dop_dropped[ix], pos:_point_helper});
160+
}
161+
}
162+
163+
_isDragged = false;
164+
}
165+
}
166+
else {
167+
}
168+
169+
}
170+
171+
}
172+
173+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.hendrix.feathers.controls.flex
2+
{
3+
import com.hendrix.feathers.controls.flex.FlexButton;
4+
import com.hendrix.feathers.controls.utils.geom.Polygon;
5+
6+
import flash.geom.Point;
7+
8+
import starling.display.DisplayObject;
9+
import starling.events.TouchEvent;
10+
11+
/**
12+
* a flex button with a definable Polygon hit area.
13+
* if the polygon is not setup than the default algorithm for
14+
* hitArea will be used.
15+
*
16+
* @author Tomer Shalev
17+
*
18+
*/
19+
public class HitButton extends FlexButton
20+
{
21+
private var _polygon_test: Polygon = null;
22+
23+
public function HitButton()
24+
{
25+
super();
26+
27+
_polygon_test = new Polygon();
28+
}
29+
30+
/**
31+
* add a <code>Point</code> to the <code>Polygon</code>.
32+
*
33+
* @param p a <code>Point</code>
34+
*
35+
* @see Polygon
36+
*
37+
*/
38+
public function addPoint(p:Point):void {
39+
_polygon_test.addPoint(p);
40+
}
41+
42+
override protected function button_touchHandler(event:TouchEvent):void
43+
{
44+
super.button_touchHandler(event);
45+
}
46+
47+
override public function hitTest(localPoint:Point, forTouch:Boolean=false):DisplayObject
48+
{
49+
if(_polygon_test.size() < 3)
50+
return super.hitTest(localPoint, forTouch);
51+
52+
if(_polygon_test.pointInPolygon(localPoint))
53+
return super.hitTest(localPoint, forTouch);
54+
55+
return null;
56+
}
57+
58+
}
59+
60+
}

src/com/hendrix/feathers/controls/utils/SCalendarInfo.as

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ package com.hendrix.feathers.controls.utils
6060
}
6161

6262
/**
63-
* the delta in seconds between now and a given date
63+
* the delta in years between now and a given date
6464
* @param date - a future Date object
6565
*/
6666
static public function dateDeltaYears(date:Date):Number
@@ -128,6 +128,7 @@ package com.hendrix.feathers.controls.utils
128128

129129
/**
130130
* my own implementation for date arithmatic
131+
*
131132
* @param _dateAnchor the base Date
132133
* @param days the days to add/subtract
133134
*
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.hendrix.feathers.controls.utils.geom
2+
{
3+
import flash.geom.Point;
4+
/**
5+
* ...
6+
* @author Andrew McGrath - muongames.com - 2013
7+
*/
8+
public class Polygon
9+
{
10+
private var vertices:Vector.<Point> = null;
11+
12+
public function Polygon()
13+
{
14+
vertices = new Vector.<Point>();
15+
}
16+
17+
public function addPoint(p:Point):void {
18+
vertices.push(p);
19+
}
20+
21+
public function pointInPolygon(p:Point):Boolean
22+
{
23+
//Loop through vertices, check if point is left of each line.
24+
//If it is, check if it line intersects with horizontal ray from point p
25+
var n:int = vertices.length;
26+
var j:int;
27+
var v1:Point, v2:Point;
28+
var count:int;
29+
for (var i:int = 0; i < n; i++)
30+
{
31+
j = i + 1 == n ? 0: i + 1;
32+
v1 = vertices[i];
33+
v2 = vertices[j];
34+
//does point lay to the left of the line?
35+
if (isLeft(p,v1,v2))
36+
{
37+
if ((p.y > v1.y && p.y <= v2.y) || (p.y > v2.y && p.y <= v1.y))
38+
{
39+
count++;
40+
}
41+
}
42+
}
43+
if (count % 2 == 0)
44+
{
45+
return false;
46+
}else
47+
{
48+
return true;
49+
}
50+
}
51+
52+
public function isLeft(p:Point, v1:Point, v2:Point):Boolean
53+
{
54+
if (v1.x == v2.x)
55+
{
56+
if (p.x <= v1.x)
57+
{
58+
return true;
59+
}else
60+
{
61+
return false;
62+
}
63+
}else
64+
{
65+
var m:Number = (v2.y - v1.y) / (v2.x - v1.x);
66+
var x2:Number = (p.y - v1.y) / m + v1.x;
67+
if (p.x <= x2)
68+
{
69+
return true;
70+
}else
71+
{
72+
return false;
73+
}
74+
}
75+
}
76+
77+
public function size():Boolean
78+
{
79+
return vertices.length;
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)