2011-01-29 01:39:36 +00:00
|
|
|
package
|
|
|
|
{
|
|
|
|
import flash.display.BitmapData;
|
2011-02-01 16:31:41 +00:00
|
|
|
import flash.filesystem.File;
|
2011-01-29 01:39:36 +00:00
|
|
|
import flash.geom.Point;
|
|
|
|
import flash.geom.Rectangle;
|
|
|
|
import net.flashpunk.Entity;
|
|
|
|
import net.flashpunk.graphics.Image;
|
2011-02-01 16:31:41 +00:00
|
|
|
import net.flashpunk.Tween;
|
|
|
|
import net.flashpunk.tweens.misc.Alarm;
|
2011-01-29 01:39:36 +00:00
|
|
|
import net.flashpunk.utils.Draw;
|
|
|
|
import net.flashpunk.utils.Input;
|
|
|
|
import net.flashpunk.utils.Key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
* @author jjp
|
|
|
|
*/
|
|
|
|
public class Token extends Entity
|
|
|
|
{
|
|
|
|
private var drag: Drag;
|
|
|
|
public var posReal: Point;
|
2011-02-01 16:31:41 +00:00
|
|
|
private var urff: String;
|
2011-01-29 01:39:36 +00:00
|
|
|
private var xml: XML;
|
2011-02-01 16:31:41 +00:00
|
|
|
private var filewatcher: FileWatcher;
|
|
|
|
|
|
|
|
public function Token(source:BitmapData, urff: String, x: int, y: int, xml:XML = null)
|
2011-01-29 01:39:36 +00:00
|
|
|
{
|
|
|
|
this.posReal = new Point(x, y);
|
|
|
|
super(x, y, new Image(source));
|
|
|
|
this.type = "Token";
|
|
|
|
this.layer = WorldStage.LAYER_TOKENS;
|
|
|
|
this.drag = null;
|
2011-02-01 16:31:41 +00:00
|
|
|
this.urff = urff;
|
2011-01-29 01:39:36 +00:00
|
|
|
if (xml === null)
|
|
|
|
this.xml = XML("<token/>");
|
|
|
|
else
|
|
|
|
this.xml = xml;
|
|
|
|
}
|
2011-02-04 17:14:15 +00:00
|
|
|
public function FDirty():Boolean
|
|
|
|
{
|
|
|
|
return int(xml.@x) !== posReal.x || int(xml.@y) !== posReal.y;
|
|
|
|
}
|
2011-01-29 01:39:36 +00:00
|
|
|
public function FSelected(): Boolean
|
|
|
|
{
|
|
|
|
return WorldStage(this.world).tokSelected === this;
|
|
|
|
}
|
2011-02-01 16:31:41 +00:00
|
|
|
override public function added():void
|
|
|
|
{
|
|
|
|
super.added();
|
|
|
|
FixupZoom();
|
|
|
|
filewatcher = FileWatcher.Get(WorldStage(world).UabFromUrf(urff));
|
|
|
|
filewatcher.Register(OnFileChanged, this);
|
|
|
|
}
|
2011-01-29 01:39:36 +00:00
|
|
|
override public function removed():void
|
|
|
|
{
|
|
|
|
super.removed();
|
|
|
|
if (drag !== null)
|
|
|
|
{
|
|
|
|
drag.Done();
|
|
|
|
drag = null;
|
|
|
|
}
|
2011-02-01 16:31:41 +00:00
|
|
|
filewatcher.Unregister(this);
|
2011-01-29 01:39:36 +00:00
|
|
|
}
|
2011-02-01 16:31:41 +00:00
|
|
|
public function OnFileChanged(bmp: BitmapData, file: File): void
|
2011-01-29 01:39:36 +00:00
|
|
|
{
|
2011-02-01 16:31:41 +00:00
|
|
|
graphic = new Image(bmp);
|
2011-01-29 01:39:36 +00:00
|
|
|
}
|
|
|
|
private function FixupZoom(): Number
|
|
|
|
{
|
|
|
|
var worldStage: WorldStage = WorldStage(world);
|
|
|
|
var zoom: Number = worldStage.zoom;
|
|
|
|
var img: Image = Image(this.graphic);
|
|
|
|
Image(this.graphic).scale = zoom;
|
|
|
|
var posScreen : Point = worldStage.PointScreenFromReal(posReal);
|
|
|
|
x = int(posScreen.x);
|
|
|
|
y = int(posScreen.y);
|
|
|
|
this.setHitbox(img.scaledWidth, img.scaledHeight, -img.x, -img.y);
|
|
|
|
|
|
|
|
return zoom;
|
|
|
|
}
|
|
|
|
override public function update():void
|
|
|
|
{
|
|
|
|
super.update();
|
|
|
|
|
|
|
|
var zoom: Number = FixupZoom();
|
|
|
|
|
|
|
|
if (Input.mouseUp && drag !== null)
|
|
|
|
{
|
|
|
|
trace("drag done");
|
|
|
|
drag.Done();
|
|
|
|
drag = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FSelected())
|
|
|
|
{
|
|
|
|
var deltaMove: Point = null;
|
|
|
|
if (Input.pressed(Key.UP))
|
|
|
|
deltaMove = new Point(0, -1);
|
|
|
|
else if (Input.pressed(Key.DOWN))
|
|
|
|
deltaMove = new Point(0, 1);
|
|
|
|
else if (Input.pressed(Key.LEFT))
|
|
|
|
deltaMove = new Point(-1, 0);
|
|
|
|
else if (Input.pressed(Key.RIGHT))
|
|
|
|
deltaMove = new Point(1, 0);
|
|
|
|
|
|
|
|
if (Input.pressed(Key.PAGE_UP))
|
|
|
|
world.bringForward(this);
|
|
|
|
else if (Input.pressed(Key.PAGE_DOWN))
|
|
|
|
world.sendBackward(this);
|
|
|
|
|
|
|
|
if (Input.mouseDown)
|
|
|
|
{
|
|
|
|
if (drag === null && collidePoint(x, y, Input.mouseX, Input.mouseY))
|
|
|
|
{
|
|
|
|
drag = Drag.Claim();
|
|
|
|
if (drag !== null)
|
2011-02-01 16:31:41 +00:00
|
|
|
trace("drag claimed for " + urff);
|
2011-01-29 01:39:36 +00:00
|
|
|
else
|
2011-02-01 16:31:41 +00:00
|
|
|
trace("drag failed for " + urff);
|
2011-01-29 01:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (drag !== null)
|
|
|
|
{
|
|
|
|
deltaMove = drag.Delta(zoom);
|
|
|
|
drag.Update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (deltaMove !== null)
|
|
|
|
posReal = posReal.add(deltaMove);
|
|
|
|
|
|
|
|
if (Input.pressed(Key.DELETE))
|
|
|
|
WorldStage(world).KillToken(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override public function render():void
|
|
|
|
{
|
|
|
|
super.render();
|
|
|
|
if (FSelected())
|
|
|
|
Draw.hitbox(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function GenXML(): XML
|
|
|
|
{
|
|
|
|
var xml: XML = this.xml.copy();
|
|
|
|
xml.@x = int(posReal.x);
|
|
|
|
xml.@y = int(posReal.y);
|
2011-02-01 16:31:41 +00:00
|
|
|
xml.@path = urff;
|
2011-02-04 17:14:15 +00:00
|
|
|
this.xml = xml;
|
2011-01-29 01:39:36 +00:00
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|