drag to select works in many cases
This commit is contained in:
@@ -347,8 +347,7 @@
|
|||||||
(set s.priorityID (+ 1 .priorityID (last (the kiss.List<KissExtendedSprite> rewardSprites.members))))
|
(set s.priorityID (+ 1 .priorityID (last (the kiss.List<KissExtendedSprite> rewardSprites.members))))
|
||||||
(let [connectedPieces (recursivelyConnectedPieces s)]
|
(let [connectedPieces (recursivelyConnectedPieces s)]
|
||||||
// Bring currently held pieces to the front:
|
// Bring currently held pieces to the front:
|
||||||
(rewardSprites.bringAllToFront connectedPieces)
|
(rewardSprites.bringAllToFront connectedPieces))
|
||||||
(set s.connectedSprites connectedPieces))
|
|
||||||
(set draggingSprite s)
|
(set draggingSprite s)
|
||||||
(set draggingLastPos (new FlxPoint s.x s.y))))
|
(set draggingLastPos (new FlxPoint s.x s.y))))
|
||||||
(set s.mouseStopDragCallback
|
(set s.mouseStopDragCallback
|
||||||
@@ -505,6 +504,9 @@
|
|||||||
|
|
||||||
(thisConnectedPieces.push toSprite)
|
(thisConnectedPieces.push toSprite)
|
||||||
(toConnectedPieces.push self)
|
(toConnectedPieces.push self)
|
||||||
|
(let [selfAndAttached (recursivelyConnectedPieces self)]
|
||||||
|
(doFor s selfAndAttached
|
||||||
|
(set s.connectedSprites selfAndAttached)))
|
||||||
true))
|
true))
|
||||||
|
|
||||||
(defMacro _checkMatch [side otherSide]
|
(defMacro _checkMatch [side otherSide]
|
||||||
|
@@ -28,9 +28,9 @@ class DragToSelectPlugin extends FlxBasic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function enableSprite(s:KissExtendedSprite, ?state:FlxState, ?camera:FlxCamera) {
|
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 (!dragStates.exists(state)) {
|
||||||
if (state == null) state = FlxG.state;
|
|
||||||
if (camera == null) camera = FlxG.camera;
|
|
||||||
dragStates[state] = {
|
dragStates[state] = {
|
||||||
camera: camera,
|
camera: camera,
|
||||||
debugLayer: new DebugLayer(),
|
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) {
|
public override function update(elapsed:Float) {
|
||||||
if (dragStates.exists(FlxG.state)) {
|
if (dragStates.exists(FlxG.state)) {
|
||||||
var dragState = dragStates[FlxG.state];
|
var dragState = dragStates[FlxG.state];
|
||||||
@@ -56,11 +64,9 @@ class DragToSelectPlugin extends FlxBasic {
|
|||||||
if (FlxMouseControl.dragTarget == null) {
|
if (FlxMouseControl.dragTarget == null) {
|
||||||
if (FlxG.mouse.justPressed) {
|
if (FlxG.mouse.justPressed) {
|
||||||
dragState.firstCorner = FlxG.mouse.getWorldPosition(dragState.camera);
|
dragState.firstCorner = FlxG.mouse.getWorldPosition(dragState.camera);
|
||||||
} else if (FlxG.mouse.justReleased) {
|
}
|
||||||
dragState.firstCorner = null;
|
|
||||||
}
|
|
||||||
dragState.secondCorner = FlxG.mouse.getWorldPosition(dragState.camera);
|
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 rounded1 = dragState.firstCorner.copyTo();
|
||||||
var rounded2 = dragState.secondCorner.copyTo();
|
var rounded2 = dragState.secondCorner.copyTo();
|
||||||
for (r in [rounded1, rounded2]) {
|
for (r in [rounded1, rounded2]) {
|
||||||
@@ -68,8 +74,20 @@ class DragToSelectPlugin extends FlxBasic {
|
|||||||
r.y = Std.int(r.y);
|
r.y = Std.int(r.y);
|
||||||
}
|
}
|
||||||
var rect = new FlxRect().fromTwoPoints(rounded1, rounded2);
|
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);
|
dragState.debugLayer.drawFlxRect(rect);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,20 +22,46 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
var mouseStartPos:FlxPoint = null;
|
var mouseStartPos:FlxPoint = null;
|
||||||
|
|
||||||
public var connectedSprites:Array<KissExtendedSprite> = [];
|
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> = [];
|
var connectedSpritesStartPos:Array<FlxPoint> = [];
|
||||||
|
|
||||||
function resetStartPos() {
|
function resetStartPos() {
|
||||||
dragStartPos = new FlxPoint(x, y);
|
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();
|
mouseStartPos = FlxG.mouse.getWorldPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override function startDrag() {
|
public override function startDrag() {
|
||||||
super.startDrag();
|
super.startDrag();
|
||||||
|
|
||||||
|
if (_dragToSelectEnabled) {
|
||||||
|
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
||||||
|
if (plugin.selectedSprites().indexOf(this) == -1)
|
||||||
|
plugin.deselectSprites();
|
||||||
|
}
|
||||||
|
|
||||||
resetStartPos();
|
resetStartPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override function stopDrag() {
|
||||||
|
super.stopDrag();
|
||||||
|
}
|
||||||
|
|
||||||
private var rotationPadding = new FlxPoint();
|
private var rotationPadding = new FlxPoint();
|
||||||
public function getRotationPadding() {
|
public function getRotationPadding() {
|
||||||
return rotationPadding.copyTo();
|
return rotationPadding.copyTo();
|
||||||
@@ -71,7 +97,7 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_rot(this, deg);
|
_rot(this, deg);
|
||||||
for (c in connectedSprites) {
|
for (c in connectedAndSelectedSprites()) {
|
||||||
if (c != this) {
|
if (c != this) {
|
||||||
_rot(c, deg);
|
_rot(c, deg);
|
||||||
}
|
}
|
||||||
@@ -79,6 +105,7 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
resetStartPos();
|
resetStartPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _dragToSelectEnabled = false;
|
||||||
public function enableDragToSelect(?state:FlxState) {
|
public function enableDragToSelect(?state:FlxState) {
|
||||||
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
||||||
if (plugin == null) {
|
if (plugin == null) {
|
||||||
@@ -86,6 +113,7 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
FlxG.plugins.add(plugin);
|
FlxG.plugins.add(plugin);
|
||||||
}
|
}
|
||||||
plugin.enableSprite(this, state, thisCamera());
|
plugin.enableSprite(this, state, thisCamera());
|
||||||
|
_dragToSelectEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
override function update(elapsed:Float) {
|
override function update(elapsed:Float) {
|
||||||
@@ -100,8 +128,9 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
var nextPos = dragStartPos.copyTo().addPoint(mouseTotalMovement);
|
var nextPos = dragStartPos.copyTo().addPoint(mouseTotalMovement);
|
||||||
x = nextPos.x;
|
x = nextPos.x;
|
||||||
y = nextPos.y;
|
y = nextPos.y;
|
||||||
for (i in 0...connectedSprites.length) {
|
var l = connectedAndSelectedSprites();
|
||||||
var sprite = connectedSprites[i];
|
for (i in 0...l.length) {
|
||||||
|
var sprite = l[i];
|
||||||
var startPos = connectedSpritesStartPos[i];
|
var startPos = connectedSpritesStartPos[i];
|
||||||
var nextPos = startPos.copyTo().addPoint(mouseTotalMovement);
|
var nextPos = startPos.copyTo().addPoint(mouseTotalMovement);
|
||||||
sprite.x = nextPos.x;
|
sprite.x = nextPos.x;
|
||||||
|
Reference in New Issue
Block a user