half-functional DragToSelectPlugin for KissExtendedSprites
This commit is contained in:
77
src/kiss_flixel/DragToSelectPlugin.hx
Normal file
77
src/kiss_flixel/DragToSelectPlugin.hx
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package kiss_flixel;
|
||||||
|
|
||||||
|
import flixel.FlxG;
|
||||||
|
import flixel.addons.plugin.FlxMouseControl;
|
||||||
|
import flixel.FlxState;
|
||||||
|
import flixel.FlxCamera;
|
||||||
|
import flixel.FlxBasic;
|
||||||
|
import flixel.math.FlxPoint;
|
||||||
|
import flixel.math.FlxRect;
|
||||||
|
|
||||||
|
typedef DragState = {
|
||||||
|
camera:FlxCamera,
|
||||||
|
debugLayer:DebugLayer,
|
||||||
|
enabledSprites:Array<KissExtendedSprite>,
|
||||||
|
selectedSprites:Array<KissExtendedSprite>,
|
||||||
|
firstCorner:Null<FlxPoint>,
|
||||||
|
secondCorner:Null<FlxPoint>
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Added automatically when you call enableDragToSelect() on a KissExtendedSprite.
|
||||||
|
*/
|
||||||
|
class DragToSelectPlugin extends FlxBasic {
|
||||||
|
var dragStates:Map<FlxState,DragState> = [];
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enableSprite(s:KissExtendedSprite, ?state:FlxState, ?camera:FlxCamera) {
|
||||||
|
if (!dragStates.exists(state)) {
|
||||||
|
if (state == null) state = FlxG.state;
|
||||||
|
if (camera == null) camera = FlxG.camera;
|
||||||
|
dragStates[state] = {
|
||||||
|
camera: camera,
|
||||||
|
debugLayer: new DebugLayer(),
|
||||||
|
enabledSprites: [s],
|
||||||
|
selectedSprites: [],
|
||||||
|
firstCorner: null,
|
||||||
|
secondCorner: null
|
||||||
|
};
|
||||||
|
dragStates[state].debugLayer.cameras = [camera];
|
||||||
|
state.add(dragStates[state].debugLayer);
|
||||||
|
} else {
|
||||||
|
dragStates[state].enabledSprites.push(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override function update(elapsed:Float) {
|
||||||
|
if (dragStates.exists(FlxG.state)) {
|
||||||
|
var dragState = dragStates[FlxG.state];
|
||||||
|
dragState.debugLayer.clear();
|
||||||
|
|
||||||
|
// Might have to skip a frame after justPressed, so KissExtendedSprites
|
||||||
|
// can get first access to the mouse input
|
||||||
|
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) {
|
||||||
|
var rounded1 = dragState.firstCorner.copyTo();
|
||||||
|
var rounded2 = dragState.secondCorner.copyTo();
|
||||||
|
for (r in [rounded1, rounded2]) {
|
||||||
|
r.x = Std.int(r.x);
|
||||||
|
r.y = Std.int(r.y);
|
||||||
|
}
|
||||||
|
var rect = new FlxRect().fromTwoPoints(rounded1, rounded2);
|
||||||
|
if (!rect.isEmpty)
|
||||||
|
dragState.debugLayer.drawFlxRect(rect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -3,12 +3,14 @@ package kiss_flixel;
|
|||||||
import flixel.FlxG;
|
import flixel.FlxG;
|
||||||
import flixel.FlxCamera;
|
import flixel.FlxCamera;
|
||||||
import flixel.FlxSprite;
|
import flixel.FlxSprite;
|
||||||
|
import flixel.FlxState;
|
||||||
import flixel.math.FlxPoint;
|
import flixel.math.FlxPoint;
|
||||||
import flixel.system.FlxAssets;
|
import flixel.system.FlxAssets;
|
||||||
import flixel.util.FlxColor;
|
import flixel.util.FlxColor;
|
||||||
import flixel.addons.plugin.FlxMouseControl;
|
import flixel.addons.plugin.FlxMouseControl;
|
||||||
import flixel.util.FlxCollision;
|
import flixel.util.FlxCollision;
|
||||||
import flash.display.BitmapData;
|
import flash.display.BitmapData;
|
||||||
|
import kiss_flixel.DragToSelectPlugin;
|
||||||
|
|
||||||
class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
||||||
public function new(X:Float = 0, Y:Float = 0, ?SimpleGraphic:FlxGraphicAsset)
|
public function new(X:Float = 0, Y:Float = 0, ?SimpleGraphic:FlxGraphicAsset)
|
||||||
@@ -77,6 +79,15 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
resetStartPos();
|
resetStartPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function enableDragToSelect(?state:FlxState) {
|
||||||
|
var plugin = FlxG.plugins.get(DragToSelectPlugin);
|
||||||
|
if (plugin == null) {
|
||||||
|
plugin = new DragToSelectPlugin();
|
||||||
|
FlxG.plugins.add(plugin);
|
||||||
|
}
|
||||||
|
plugin.enableSprite(this, state, thisCamera());
|
||||||
|
}
|
||||||
|
|
||||||
override function update(elapsed:Float) {
|
override function update(elapsed:Float) {
|
||||||
#if debug
|
#if debug
|
||||||
// color = (mouseOver && pixelPerfect(_dragPixelPerfectAlpha)) ? FlxColor.LIME : FlxColor.WHITE;
|
// color = (mouseOver && pixelPerfect(_dragPixelPerfectAlpha)) ? FlxColor.LIME : FlxColor.WHITE;
|
||||||
@@ -98,9 +109,17 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function thisCamera() {
|
||||||
|
if (cameras != null && cameras.length > 0)
|
||||||
|
return cameras[0];
|
||||||
|
return FlxG.camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if FLX_MOUSE
|
#if FLX_MOUSE
|
||||||
override function get_mouseOver() {
|
override function get_mouseOver() {
|
||||||
var mouseOver = getScreenBounds(cameras[0]).containsPoint(FlxG.mouse.getScreenPosition(cameras[0]));
|
var mouseOver = getScreenBounds(thisCamera()).containsPoint(FlxG.mouse.getScreenPosition(thisCamera()));
|
||||||
|
|
||||||
return mouseOver;
|
return mouseOver;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user