highlight selected jigsaw pieces

This commit is contained in:
2022-08-20 17:15:09 +00:00
parent d77ac2594d
commit ab9e47e271
4 changed files with 40 additions and 11 deletions

View File

@@ -46,13 +46,18 @@ typedef StartPuzzleFunc = (Int, Int) -> Void;
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class HabitState extends FlxState { class HabitState extends FlxState {
public function drawPieceShape( surface: FlxSprite, jig: JigsawPiece, scale:Float, c: FlxColor ) public function drawPieceShape( surface: FlxSprite, jig: JigsawPiece, scale:Float, fillColor: FlxColor, ?outlineColor: FlxColor)
{ {
if (outlineColor == null) outlineColor = fillColor;
var points = [for (point in jig.getPoints()) new FlxPoint(point.x / scale + ROT_PADDING, point.y / scale + ROT_PADDING)]; var points = [for (point in jig.getPoints()) new FlxPoint(point.x / scale + ROT_PADDING, point.y / scale + ROT_PADDING)];
points.push(points[0]); points.push(points[0]);
FlxSpriteUtil.drawPolygon( FlxSpriteUtil.drawPolygon(
surface, surface,
points, points,
c); fillColor,
{
thickness: 1,
color: outlineColor
});
} }
} }

View File

@@ -299,6 +299,7 @@
(unless (= lastRewardFileIndex rewardFileIndex) (unless (= lastRewardFileIndex rewardFileIndex)
// Make a new camera so scroll from the last puzzle doesn't start the camera out of boundS // Make a new camera so scroll from the last puzzle doesn't start the camera out of boundS
(newPieceCamera) (newPieceCamera)
(set pieceCamera.bgColor (nth backgroundOptions save.data.backgroundIndex))
(set rewardSprite (set rewardSprite
(new FlxSprite 0 0 (new FlxSprite 0 0
(BitmapData.fromFile (BitmapData.fromFile
@@ -388,7 +389,8 @@
(dictSet indexMap s i) (dictSet indexMap s i)
(dictSet spriteMap i s) (dictSet spriteMap i s)
(set s.draggable true) (set s.draggable true)
(s.enableDragToSelect)
(s.enableMouseDrag false true) (s.enableMouseDrag false true)
(set s.mouseStartDragCallback (set s.mouseStartDragCallback
->:Void [s x y] ->:Void [s x y]
@@ -413,7 +415,7 @@
(pieceCamera.calculateScrollBounds rewardSprites uiCamera SCROLL_BOUND_MARGIN) (pieceCamera.calculateScrollBounds rewardSprites uiCamera SCROLL_BOUND_MARGIN)
(save.flush))) (save.flush)))
(var ROT_PADDING 2) (var ROT_PADDING 4)
(localVar fWidth (+ (Std.int sourceRect.width) (* 2 ROT_PADDING))) (localVar fWidth (+ (Std.int sourceRect.width) (* 2 ROT_PADDING)))
(localVar fHeight (+ (Std.int sourceRect.height) (* 2 ROT_PADDING))) (localVar fHeight (+ (Std.int sourceRect.height) (* 2 ROT_PADDING)))
(source.makeGraphic fWidth fHeight FlxColor.TRANSPARENT true) (source.makeGraphic fWidth fHeight FlxColor.TRANSPARENT true)
@@ -421,15 +423,29 @@
(mask.makeGraphic fWidth fHeight FlxColor.TRANSPARENT true) (mask.makeGraphic fWidth fHeight FlxColor.TRANSPARENT true)
(drawPieceShape mask jig ros FlxColor.BLACK) (drawPieceShape mask jig ros FlxColor.BLACK)
(FlxSpriteUtil.alphaMask s source.pixels mask.pixels) (localVar unhighlightedS (new FlxSprite))
(FlxSpriteUtil.alphaMask unhighlightedS source.pixels mask.pixels)
// Uncomment to debug piece ids and row/columns (localVar highlightedS (new FlxSprite))
(s.loadGraphic unhighlightedS.pixels)
(highlightedS.loadGraphic unhighlightedS.pixels false 0 0 true)
(drawPieceShape highlightedS jig ros FlxColor.TRANSPARENT FlxColor.LIME)
(localFunction loadRotatedGraphic [:FlxSprite _s]
(s.loadRotatedGraphic _s.pixels 4 -1))
(loadRotatedGraphic unhighlightedS)
(s.enableDragToSelect
->:Void {
(loadRotatedGraphic highlightedS)
}
->:Void {
(loadRotatedGraphic unhighlightedS)
})
**(#when debug **(#when debug
(kiss_flixel.SpriteTools.writeOnSprite "$i" 32 s (object x (Percent 0.5) y (Percent 0.5)) FlxColor.RED) (kiss_flixel.SpriteTools.writeOnSprite "$i" 32 s (object x (Percent 0.5) y (Percent 0.5)) FlxColor.RED)
(kiss_flixel.SpriteTools.writeOnSprite "(${jig.col},${jig.row})" 32 s (object x (Percent 0.5) y (Percent 0.7)) FlxColor.RED)) (kiss_flixel.SpriteTools.writeOnSprite "(${jig.col},${jig.row})" 32 s (object x (Percent 0.5) y (Percent 0.7)) FlxColor.RED))
(s.loadRotatedGraphic s.pixels 4 -1 /*false true*/)
(set s.cameras [pieceCamera]) (set s.cameras [pieceCamera])

View File

@@ -66,6 +66,11 @@ class DragToSelectPlugin extends FlxBasic {
} }
public function deselectSprites() { public function deselectSprites() {
for (sprite in dragStates[FlxG.state].selectedSprites) {
if (sprite.onDeselected != null) {
sprite.onDeselected();
}
}
dragStates[FlxG.state].selectedSprites = []; dragStates[FlxG.state].selectedSprites = [];
} }

View File

@@ -106,7 +106,11 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
} }
var _dragToSelectEnabled = false; var _dragToSelectEnabled = false;
public function enableDragToSelect(?state:FlxState, ?camera:FlxCamera) { public var onSelected:Void->Void = null;
public var onDeselected:Void->Void;
public function enableDragToSelect(?onSelected:Void->Void, ?onDeselected:Void->Void, ?state:FlxState, ?camera:FlxCamera) {
this.onSelected = onSelected;
this.onDeselected = onDeselected;
var plugin = FlxG.plugins.get(DragToSelectPlugin); var plugin = FlxG.plugins.get(DragToSelectPlugin);
if (plugin == null) { if (plugin == null) {
plugin = new DragToSelectPlugin(); plugin = new DragToSelectPlugin();
@@ -149,7 +153,6 @@ class KissExtendedSprite extends flixel.addons.display.FlxExtendedSprite {
} }
} }
public var onSelected:Void->Void = null;
public function pixelPerfectDrag() { public function pixelPerfectDrag() {
return _dragPixelPerfect; return _dragPixelPerfect;