drag to select works in many cases
This commit is contained in:
@@ -28,9 +28,9 @@ class DragToSelectPlugin extends FlxBasic {
|
||||
}
|
||||
|
||||
public function enableSprite(s:KissExtendedSprite, ?state:FlxState, ?camera:FlxCamera) {
|
||||
if (state == null) state = FlxG.state;
|
||||
if (camera == null) camera = FlxG.camera;
|
||||
if (!dragStates.exists(state)) {
|
||||
if (state == null) state = FlxG.state;
|
||||
if (camera == null) camera = FlxG.camera;
|
||||
dragStates[state] = {
|
||||
camera: camera,
|
||||
debugLayer: new DebugLayer(),
|
||||
@@ -46,6 +46,14 @@ class DragToSelectPlugin extends FlxBasic {
|
||||
}
|
||||
}
|
||||
|
||||
public function selectedSprites() {
|
||||
return dragStates[FlxG.state].selectedSprites;
|
||||
}
|
||||
|
||||
public function deselectSprites() {
|
||||
dragStates[FlxG.state].selectedSprites = [];
|
||||
}
|
||||
|
||||
public override function update(elapsed:Float) {
|
||||
if (dragStates.exists(FlxG.state)) {
|
||||
var dragState = dragStates[FlxG.state];
|
||||
@@ -56,11 +64,9 @@ class DragToSelectPlugin extends FlxBasic {
|
||||
if (FlxMouseControl.dragTarget == null) {
|
||||
if (FlxG.mouse.justPressed) {
|
||||
dragState.firstCorner = FlxG.mouse.getWorldPosition(dragState.camera);
|
||||
} else if (FlxG.mouse.justReleased) {
|
||||
dragState.firstCorner = null;
|
||||
}
|
||||
}
|
||||
dragState.secondCorner = FlxG.mouse.getWorldPosition(dragState.camera);
|
||||
if (dragState.firstCorner != null) {
|
||||
if (dragState.firstCorner != null && dragState.selectedSprites.length == 0) {
|
||||
var rounded1 = dragState.firstCorner.copyTo();
|
||||
var rounded2 = dragState.secondCorner.copyTo();
|
||||
for (r in [rounded1, rounded2]) {
|
||||
@@ -68,8 +74,20 @@ class DragToSelectPlugin extends FlxBasic {
|
||||
r.y = Std.int(r.y);
|
||||
}
|
||||
var rect = new FlxRect().fromTwoPoints(rounded1, rounded2);
|
||||
if (!rect.isEmpty)
|
||||
if (FlxG.mouse.justReleased && dragState.selectedSprites.length == 0) {
|
||||
dragState.firstCorner = null;
|
||||
for (s in dragState.enabledSprites) {
|
||||
if (s.scale.x != 1 || s.scale.y != 1) {
|
||||
throw "DragToSelectPlugin can't handle scaled sprites yet!";
|
||||
}
|
||||
if (!s.getRotatedBounds().intersection(rect).isEmpty) {
|
||||
// TODO if pixel perfect is true, get the pixels in the intersection and hit test them for transparency
|
||||
dragState.selectedSprites.push(s);
|
||||
}
|
||||
}
|
||||
} else if (!rect.isEmpty) {
|
||||
dragState.debugLayer.drawFlxRect(rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -22,20 +22,46 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
||||
var mouseStartPos:FlxPoint = null;
|
||||
|
||||
public var connectedSprites:Array<KissExtendedSprite> = [];
|
||||
function connectedAndSelectedSprites() {
|
||||
var l = connectedSprites;
|
||||
var map = [for (s in l) s => true];
|
||||
if (_dragToSelectEnabled) {
|
||||
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
||||
for (s in plugin.selectedSprites()) {
|
||||
map[s] = true;
|
||||
for (c in s.connectedSprites) {
|
||||
map[c] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Remove duplicates and return
|
||||
l = [for (s => bool in map) s];
|
||||
return l;
|
||||
}
|
||||
var connectedSpritesStartPos:Array<FlxPoint> = [];
|
||||
|
||||
function resetStartPos() {
|
||||
dragStartPos = new FlxPoint(x, y);
|
||||
connectedSpritesStartPos = [for (s in connectedSprites) new FlxPoint(s.x, s.y)];
|
||||
connectedSpritesStartPos = [for (s in connectedAndSelectedSprites()) new FlxPoint(s.x, s.y)];
|
||||
mouseStartPos = FlxG.mouse.getWorldPosition();
|
||||
}
|
||||
|
||||
public override function startDrag() {
|
||||
super.startDrag();
|
||||
|
||||
if (_dragToSelectEnabled) {
|
||||
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
||||
if (plugin.selectedSprites().indexOf(this) == -1)
|
||||
plugin.deselectSprites();
|
||||
}
|
||||
|
||||
resetStartPos();
|
||||
}
|
||||
|
||||
public override function stopDrag() {
|
||||
super.stopDrag();
|
||||
}
|
||||
|
||||
private var rotationPadding = new FlxPoint();
|
||||
public function getRotationPadding() {
|
||||
return rotationPadding.copyTo();
|
||||
@@ -71,7 +97,7 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
||||
}
|
||||
}
|
||||
_rot(this, deg);
|
||||
for (c in connectedSprites) {
|
||||
for (c in connectedAndSelectedSprites()) {
|
||||
if (c != this) {
|
||||
_rot(c, deg);
|
||||
}
|
||||
@@ -79,6 +105,7 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
||||
resetStartPos();
|
||||
}
|
||||
|
||||
var _dragToSelectEnabled = false;
|
||||
public function enableDragToSelect(?state:FlxState) {
|
||||
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
||||
if (plugin == null) {
|
||||
@@ -86,6 +113,7 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
||||
FlxG.plugins.add(plugin);
|
||||
}
|
||||
plugin.enableSprite(this, state, thisCamera());
|
||||
_dragToSelectEnabled = true;
|
||||
}
|
||||
|
||||
override function update(elapsed:Float) {
|
||||
@@ -100,8 +128,9 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
||||
var nextPos = dragStartPos.copyTo().addPoint(mouseTotalMovement);
|
||||
x = nextPos.x;
|
||||
y = nextPos.y;
|
||||
for (i in 0...connectedSprites.length) {
|
||||
var sprite = connectedSprites[i];
|
||||
var l = connectedAndSelectedSprites();
|
||||
for (i in 0...l.length) {
|
||||
var sprite = l[i];
|
||||
var startPos = connectedSpritesStartPos[i];
|
||||
var nextPos = startPos.copyTo().addPoint(mouseTotalMovement);
|
||||
sprite.x = nextPos.x;
|
||||
|
Reference in New Issue
Block a user