CameraTools/GroupTools bugfixing and sprite scale support
This commit is contained in:
@@ -5,9 +5,13 @@
|
||||
(prop &mut :FlxCamera uiCamera)
|
||||
|
||||
(method &override :Void create []
|
||||
(#when debug
|
||||
(add cameraBounds))
|
||||
|
||||
(add logTexts)
|
||||
(set Prelude.printStr log)
|
||||
(set pieceCamera FlxG.camera)
|
||||
(set cameraBounds.cameras [pieceCamera])
|
||||
(set uiCamera (new FlxCamera))
|
||||
(set uiCamera.bgColor FlxColor.TRANSPARENT)
|
||||
(pieceCamera.copyFrom FlxG.camera)
|
||||
@@ -23,10 +27,19 @@
|
||||
(prop &mut :EntryType typeAdding Todo)
|
||||
(prop &mut :FlxInputText entryNameText)
|
||||
|
||||
(prop :FlxSprite cameraBounds (new FlxSprite))
|
||||
|
||||
(method &override :Void update [:Float elapsed]
|
||||
(super.update elapsed)
|
||||
|
||||
(pieceCamera.updateScrollWheelZoom elapsed 1)
|
||||
(#when debug
|
||||
(let [b (pieceCamera.getScrollBounds)]
|
||||
(set cameraBounds.x b.x)
|
||||
(set cameraBounds.y b.y)
|
||||
(cameraBounds.makeGraphic (Std.int b.width) (Std.int b.height) FlxColor.TRANSPARENT true)
|
||||
(cameraBounds.drawRect 0 0 b.width b.height FlxColor.TRANSPARENT (object color FlxColor.LIME thickness 5))))
|
||||
|
||||
(pieceCamera.updateScrollWheelZoom elapsed 5)
|
||||
(pieceCamera.updateMouseBorderControl elapsed KEYBOARD_SCROLL_SPEED 0.002 uiCamera)
|
||||
|
||||
// Hold left-click to hide the habit text and see the image clearly:
|
||||
@@ -42,7 +55,7 @@
|
||||
(set save.data.backgroundIndex #{(save.data.backgroundIndex + 1) % backgroundOptions.length;}#)
|
||||
(save.flush)
|
||||
// setModel so the entry text gets remade in inverted/lightened colors when necessary
|
||||
(setModel model (nth model.rewardFiles rewardFileIndex))))
|
||||
(refreshModel)))
|
||||
|
||||
(method startAdding [:EntryType type]
|
||||
(set typeAdding type)
|
||||
@@ -54,8 +67,9 @@
|
||||
(when FlxG.keys.justPressed.ENTER
|
||||
(cond
|
||||
(entryNameText
|
||||
// addEntry() calls save()
|
||||
(model.addEntry typeAdding [entryNameText.text])
|
||||
(setModel model (nth model.rewardFiles rewardFileIndex))
|
||||
(refreshModel)
|
||||
(entryNameText.kill)
|
||||
(set entryNameText null))
|
||||
(true
|
||||
@@ -79,11 +93,11 @@
|
||||
(when FlxG.keys.justPressed.LEFT
|
||||
(unless (= rewardFileIndex 0)
|
||||
(-= rewardFileIndex 1)
|
||||
(setModel model (nth model.rewardFiles rewardFileIndex))))
|
||||
(refreshModel)))
|
||||
(when FlxG.keys.justPressed.RIGHT
|
||||
(unless (= rewardFileIndex maxRewardFile)
|
||||
(+= rewardFileIndex 1)
|
||||
(setModel model (nth model.rewardFiles rewardFileIndex)))))
|
||||
(refreshModel))))
|
||||
// Handle keyboard input:
|
||||
(when (and shortcutHandler !entryNameText)
|
||||
(shortcutHandler.update)))
|
||||
@@ -110,6 +124,7 @@
|
||||
(prop &mut :Map<FlxExtendedSprite,Int> indexMap (new Map))
|
||||
(prop &mut :Map<Int,FlxExtendedSprite> spriteMap (new Map)) // Because rewardSprites will be re-ordered in depth handling, this is required
|
||||
|
||||
(prop &mut lastRewardFileIndex -1)
|
||||
(prop &mut rewardFileIndex 0)
|
||||
(prop &mut maxRewardFile 0)
|
||||
|
||||
@@ -144,6 +159,7 @@
|
||||
(set save.data.backgroundIndex 0))
|
||||
(set pieceCamera.bgColor (nth backgroundOptions save.data.backgroundIndex))
|
||||
|
||||
(unless (= lastRewardFileIndex rewardFileIndex)
|
||||
(let [rewardSprite
|
||||
(new FlxSprite 0 0
|
||||
(BitmapData.fromFile
|
||||
@@ -226,10 +242,9 @@
|
||||
(dictSet (the Map<Int,FlxPoint> save.data.storedPositions) i (new FlxPoint s.x s.y))
|
||||
(doFor connected (recursivelyConnectedPieces s)
|
||||
(checkMatches (dictGet indexMap connected))
|
||||
(log "uh $(dictGet indexMap connected)")
|
||||
(dictSet (the Map<Int,FlxPoint> save.data.storedPositions) (dictGet indexMap connected) (new FlxPoint connected.x connected.y)))
|
||||
|
||||
(pieceCamera.calculateScrollBounds rewardSprites SCROLL_BOUND_MARGIN)
|
||||
(pieceCamera.calculateScrollBounds rewardSprites uiCamera SCROLL_BOUND_MARGIN)
|
||||
(save.flush)
|
||||
})
|
||||
|
||||
@@ -264,9 +279,10 @@
|
||||
(dictSet matchingPiecesDown id toDown)) (catch [e] null)))))
|
||||
(add rewardSprites)
|
||||
(doFor i (range TOTAL_PIECES)
|
||||
(checkMatches i))))
|
||||
(checkMatches i)))))
|
||||
(set lastRewardFileIndex rewardFileIndex)
|
||||
|
||||
(pieceCamera.calculateScrollBounds rewardSprites SCROLL_BOUND_MARGIN)
|
||||
(pieceCamera.calculateScrollBounds rewardSprites uiCamera SCROLL_BOUND_MARGIN)
|
||||
|
||||
(when entryTexts (remove entryTexts))
|
||||
(set entryTexts (new FlxTypedGroup))
|
||||
@@ -307,6 +323,10 @@
|
||||
(shortcutHandler.start)))
|
||||
(shortcutHandler.start))
|
||||
|
||||
(method refreshModel [&opt m]
|
||||
(let [m (or m model)]
|
||||
(setModel m (nth m.rewardFiles rewardFileIndex))))
|
||||
|
||||
(prop &mut textY 0)
|
||||
// Color currently used for making text, may be inverted or lightened to contrast with background:
|
||||
(prop &mut :FlxColor _color FlxColor.BLACK)
|
||||
@@ -361,8 +381,8 @@
|
||||
|
||||
(prop &mut c 0)
|
||||
(method :Void connectPiece [id self toSprite]
|
||||
(let [thisConnectedPieces (dictGet connectedPieces ~id)
|
||||
toConnectedPieces (dictGet connectedPieces ~(dictGet indexMap toSprite))]
|
||||
(let [thisConnectedPieces (dictGet connectedPieces id)
|
||||
toConnectedPieces (dictGet connectedPieces (dictGet indexMap toSprite))]
|
||||
(+= c 1)
|
||||
// Don't add duplicates
|
||||
(thisConnectedPieces.remove toSprite)
|
||||
@@ -388,13 +408,13 @@
|
||||
(unless .isEmpty (mzl.intersection mzr)
|
||||
(connectPiece id s toRight)))
|
||||
(whenLet [toUp (dictGet matchingPiecesUp id)
|
||||
mzu ~(matchZoneUp s)
|
||||
mzd ~(matchZoneDown toUp)]
|
||||
mzu (matchZoneUp s)
|
||||
mzd (matchZoneDown toUp)]
|
||||
(unless .isEmpty (mzu.intersection mzd)
|
||||
(connectPiece id s toUp)))
|
||||
(whenLet [toDown (dictGet matchingPiecesDown id)
|
||||
mzd ~(matchZoneDown s)
|
||||
mzu ~(matchZoneUp toDown)]
|
||||
mzd (matchZoneDown s)
|
||||
mzu (matchZoneUp toDown)]
|
||||
(unless .isEmpty (mzu.intersection mzd)
|
||||
(connectPiece id s toDown)))))
|
||||
|
||||
|
@@ -19,12 +19,11 @@ class Main extends Sprite
|
||||
};
|
||||
function reloadModel(_) {
|
||||
if (t.draggingSprite == null) {
|
||||
// TODO don't change camera position and zoom when this happens:
|
||||
t.setModel(new HabitModel(habitFile));
|
||||
t.refreshModel(new HabitModel(habitFile));
|
||||
t.model.save();
|
||||
}
|
||||
}
|
||||
reloadModel(null);
|
||||
t.setModel(new HabitModel(habitFile));
|
||||
new FlxTimer().start(30, reloadModel, 0);
|
||||
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@ import flash.display.BitmapData;
|
||||
import flixel.FlxCamera;
|
||||
import flixel.math.FlxVector;
|
||||
import flixel.math.FlxPoint;
|
||||
import flixel.math.FlxRect;
|
||||
import flixel.FlxSprite;
|
||||
import flixel.FlxG;
|
||||
import flixel.util.FlxColor;
|
||||
|
@@ -76,20 +76,36 @@
|
||||
(<= (- bottom margin) mPos.y)
|
||||
(<= (- bottom margin) mPos.y bottom)))))
|
||||
|
||||
(function updateScrollWheelZoom [:FlxCamera camera :Float elapsed :Float speed]
|
||||
#{
|
||||
if (FlxG.mouse.wheel != 0) {
|
||||
camera.zoom += (FlxG.mouse.wheel * elapsed * speed);
|
||||
}
|
||||
}#)
|
||||
// GOTCHA: if you change FlxG.camera to a moving camera, you MUST provide a default camera for FlxG.mouse.getScreenPosition()
|
||||
(function updateScrollWheelZoom [:FlxCamera camera :Float elapsed :Float speed &opt :FlxCamera screenCamera]
|
||||
(case FlxG.mouse.wheel
|
||||
(0 null)
|
||||
(v
|
||||
(let [deltaZoom (* camera.zoom v elapsed speed)
|
||||
scrollPosition (camera.scroll.copyTo)
|
||||
mouseWorldPosition (FlxG.mouse.getWorldPosition camera)]
|
||||
(+= camera.zoom deltaZoom)
|
||||
(let [newMouseWorldPosition (FlxG.mouse.getWorldPosition camera)
|
||||
deltaMousePosition (newMouseWorldPosition.subtractPoint mouseWorldPosition)]
|
||||
(camera.scroll.subtractPoint deltaMousePosition))
|
||||
// Undo any scrolling that expands the viewport past its bounds
|
||||
**(unless (.containsPoint (getScrollBounds camera) camera.scroll)
|
||||
(-= camera.zoom deltaZoom)
|
||||
(set camera.scroll scrollPosition))))
|
||||
(otherwise null)))
|
||||
|
||||
(function calculateScrollBounds <>[:FlxObject T] [:FlxCamera camera :FlxTypedGroup<T> group &opt :Float margin]
|
||||
(let [r (GroupTools.calculateBounds group margin)]
|
||||
(camera.setScrollBoundsRect r.x r.y r.width r.height)))
|
||||
(function getScrollBounds [:FlxCamera camera]
|
||||
(.fromTwoPoints (new FlxRect) (new FlxPoint camera.minScrollX camera.minScrollY) (new FlxPoint camera.maxScrollX camera.maxScrollY)))
|
||||
|
||||
(function extendScrollBounds [:FlxCamera camera :FlxObject object &opt :Float margin]
|
||||
// GOTCHA: if you change FlxG.camera to a moving camera, you MUST provide a default camera for FlxG.mouse.getScreenPosition()
|
||||
(function calculateScrollBounds <>[:FlxSprite T] [:FlxCamera camera :FlxTypedGroup<T> group &opt :FlxCamera screenCamera :Float margin]
|
||||
(let [r (GroupTools.calculateScreenBounds group screenCamera margin)]
|
||||
(camera.setScrollBoundsRect r.x r.y ~r.width ~r.height)))
|
||||
|
||||
// GOTCHA: if you change FlxG.camera to a moving camera, you MUST provide a default camera for FlxG.mouse.getScreenPosition()
|
||||
(function extendScrollBounds [:FlxCamera camera :FlxSprite sprite &opt :FlxCamera screenCamera :Float margin]
|
||||
// if the given object is out of bounds, extend the bounds
|
||||
(let [r (object.getRotatedBounds)]
|
||||
(let [r (sprite.getScreenBounds camera)]
|
||||
(setMin camera.minScrollX (- r.left margin))
|
||||
(setMin camera.minScrollY (- r.top margin))
|
||||
(setMax camera.maxScrollX (+ r.right margin))
|
||||
|
@@ -6,7 +6,9 @@ import flixel.FlxObject;
|
||||
import flixel.FlxState;
|
||||
import flixel.math.FlxRect;
|
||||
import flixel.math.FlxPoint;
|
||||
import flixel.FlxCamera;
|
||||
import flixel.group.FlxGroup;
|
||||
import flixel.FlxSprite;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class GroupTools {}
|
||||
|
@@ -8,20 +8,22 @@
|
||||
(group.remove obj)
|
||||
(group.insert 0 obj)))
|
||||
|
||||
(function :FlxRect calculateBounds <>[:FlxObject T] [:FlxTypedGroup<T> group &opt :Float margin]
|
||||
(function :FlxRect calculateScreenBounds <>[:FlxSprite T] [:FlxTypedGroup<T> group &opt :FlxCamera camera :Float margin]
|
||||
(unless margin (set margin 0))
|
||||
(let [&mut minX 0
|
||||
&mut maxX 0
|
||||
&mut minY 0
|
||||
&mut maxY 0
|
||||
r (new FlxRect)]
|
||||
(let [s (group.getFirstAlive)
|
||||
r (new FlxRect)
|
||||
bounds (s.getScreenBounds r camera)
|
||||
&mut minX bounds.left
|
||||
&mut maxX bounds.right
|
||||
&mut minY bounds.top
|
||||
&mut maxY bounds.bottom]
|
||||
|
||||
(group.forEach ->object
|
||||
(let [bounds (object.getRotatedBounds r)]
|
||||
(set minX (min minX bounds.left))
|
||||
(set minY (min minY bounds.top))
|
||||
(set maxX (max maxX bounds.right))
|
||||
(set maxY (max maxY bounds.bottom))))
|
||||
(group.forEach ->sprite
|
||||
(let [bounds (sprite.getScreenBounds r camera)]
|
||||
(setMin minX bounds.left)
|
||||
(setMin minY bounds.top)
|
||||
(setMax maxX bounds.right)
|
||||
(setMax maxY bounds.bottom)))
|
||||
|
||||
(r.fromTwoPoints
|
||||
(new FlxPoint (- minX margin) (- minY margin))
|
||||
|
Reference in New Issue
Block a user