From 2ad07feba0cf07d39ac8e1f0cea146db5066760f Mon Sep 17 00:00:00 2001 From: Yoshman29 Date: Fri, 21 Oct 2022 23:37:28 +0200 Subject: [PATCH] windows can be moved!! --- buildnumber.txt | 2 +- source/funkin/desktop/DesktopMain.hx | 20 ++++++++++++++ source/funkin/desktop/editors/HelloWorld.hx | 11 ++++++-- source/funkin/desktop/windows/Window.hx | 27 +++++++++++++++++-- .../funkin/desktop/windows/WindowContent.hx | 12 ++++----- .../desktop/windows/WindowDragHitbox.hx | 18 ++++++++++--- 6 files changed, 76 insertions(+), 14 deletions(-) diff --git a/buildnumber.txt b/buildnumber.txt index 3bcb0b10..2764f7b0 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -696 \ No newline at end of file +708 \ No newline at end of file diff --git a/source/funkin/desktop/DesktopMain.hx b/source/funkin/desktop/DesktopMain.hx index 95cea120..a056f76c 100644 --- a/source/funkin/desktop/DesktopMain.hx +++ b/source/funkin/desktop/DesktopMain.hx @@ -1,11 +1,15 @@ package funkin.desktop; +import openfl.geom.Rectangle; +import flixel.FlxObject; import lime.app.Application; import openfl.display.BitmapData; import flixel.system.scaleModes.BaseScaleMode; import flixel.system.scaleModes.FixedScaleAdjustSizeScaleMode; import flixel.FlxG; import flixel.FlxSprite; +import flixel.FlxBasic; +import flixel.FlxCamera; import flixel.math.FlxPoint; import funkin.desktop.editors.HelloWorld; import funkin.desktop.windows.*; @@ -78,9 +82,11 @@ class MouseInput { public var pressed:Bool = false; public var justPressed:Bool = false; public var justReleased:Bool = false; + private var __cancelled:Bool = false; public function cancel() { pressed = justPressed = justReleased = false; + __cancelled = true; } public function new() {} @@ -89,5 +95,19 @@ class MouseInput { this.pressed = pressed; this.justPressed = justPressed; this.justReleased = justReleased; + this.__cancelled = false; + } + + public function overlaps(spr:FlxObject, ?camera:FlxCamera) { + return overlapsRect(spr, new Rectangle(spr.x, spr.y, spr.width, spr.height), camera); + // return FlxG.mouse.overlaps(spr, camera); + } + + public function overlapsRect(spr:FlxBasic, rect:Rectangle, ?camera:FlxCamera) { + if (__cancelled) return false; + if (camera == null) camera = FlxG.camera; + var pos = FlxG.mouse.getScreenPosition(camera); + + return ((pos.x > rect.x) && (pos.x < rect.x + rect.width)) && ((pos.y > rect.y) && (pos.y < rect.y + rect.height)); } } \ No newline at end of file diff --git a/source/funkin/desktop/editors/HelloWorld.hx b/source/funkin/desktop/editors/HelloWorld.hx index e65ee466..a7cd7505 100644 --- a/source/funkin/desktop/editors/HelloWorld.hx +++ b/source/funkin/desktop/editors/HelloWorld.hx @@ -2,6 +2,7 @@ package funkin.desktop.editors; import flixel.util.FlxDestroyUtil.IFlxDestroyable; import flixel.addons.ui.FlxUIText; +import flixel.FlxG; class HelloWorld extends WindowContent { public override function new() { @@ -25,11 +26,17 @@ class HelloWorld extends WindowContent { } public var time:Float = 0; + public var coolAssMode:Bool = false; public override function update(elapsed:Float) { super.update(elapsed); time += elapsed; - width = 500 + Std.int(Math.cos(time * Math.PI) * 250); - height = 500 + Std.int(Math.sin(time * Math.PI) * 250); + + if (FlxG.keys.justPressed.M) coolAssMode = !coolAssMode; + + if (coolAssMode) { + width = 500 + Std.int(Math.cos(time * Math.PI) * 250); + height = 500 + Std.int(Math.sin(time * Math.PI) * 250); + } parent.updateWindowFrame(); } } \ No newline at end of file diff --git a/source/funkin/desktop/windows/Window.hx b/source/funkin/desktop/windows/Window.hx index 60e03264..f60be5d8 100644 --- a/source/funkin/desktop/windows/Window.hx +++ b/source/funkin/desktop/windows/Window.hx @@ -37,6 +37,7 @@ class Window extends FlxTypedGroup { public var caption:FlxUIText; public var icon:FlxSprite; + public var dragHitbox:WindowDragHitbox; public var draggable:Bool = true; public var resizeable:Bool = true; @@ -59,6 +60,10 @@ class Window extends FlxTypedGroup { } add(caption); + dragHitbox = new WindowDragHitbox(4, 4, 4, 18); + dragHitbox.parent = this; + add(dragHitbox); + icon = new FlxSprite(4, 4); add(icon); loadIcon(content.icon); @@ -74,6 +79,7 @@ class Window extends FlxTypedGroup { windowCaptionCamera.bgColor = 0; FlxG.cameras.add(windowCaptionCamera, false); cameras = [windowCaptionCamera]; + dragHitbox.cameras = [windowCaptionCamera]; for(btn in captionButtons.members) btn.cameras = cameras; // so that buttons detects on the right camera @@ -140,14 +146,31 @@ class Window extends FlxTypedGroup { } public function move(x:Float, y:Float) { - windowCaptionCamera.x = x; - windowCaptionCamera.y = y; + @:privateAccess content.__noParentUpdate = true; + content.winX = windowCaptionCamera.x = x; + content.winY = windowCaptionCamera.y = y; + @:privateAccess content.__noParentUpdate = false; for(e in windowCameras) { e.camera.x = x + 4; e.camera.y = y + 23; } } + public override function update(elapsed:Float) { + var i = members.length; + + var shouldCancel = DesktopMain.instance.mouseInput.overlapsRect(this, new Rectangle(0, 0, windowCaptionCamera.width, windowCaptionCamera.height), windowCaptionCamera); + // updates them backwards!! + while(i > 0) { + i--; + var spr = members[i]; + if (spr == null || !spr.exists) continue; + spr.update(elapsed); + } + + if (shouldCancel) DesktopMain.instance.mouseInput.cancel(); + } + public function close() { DesktopMain.instance.windows.remove(this, true); destroy(); diff --git a/source/funkin/desktop/windows/WindowContent.hx b/source/funkin/desktop/windows/WindowContent.hx index 3d2696b9..58d18de9 100644 --- a/source/funkin/desktop/windows/WindowContent.hx +++ b/source/funkin/desktop/windows/WindowContent.hx @@ -19,7 +19,7 @@ class WindowContent extends FlxTypedGroup { private var __noParentUpdate:Bool = false; private function set_width(w:Int):Int { width = w; - if (parent != null) { + if (parent != null && !__noParentUpdate) { parent.windowWidth = w; if (!__noParentUpdate) parent.updateWindowFrame(); @@ -28,7 +28,7 @@ class WindowContent extends FlxTypedGroup { } private function set_height(h:Int):Int { height = h; - if (parent != null) { + if (parent != null && !__noParentUpdate) { parent.windowHeight = h; if (!__noParentUpdate) parent.updateWindowFrame(); @@ -37,24 +37,24 @@ class WindowContent extends FlxTypedGroup { } private function set_winX(v:Float):Float { winX = v; - if (parent != null) parent.move(winX, winY); + if (parent != null && !__noParentUpdate) parent.move(winX, winY); return winX; } private function set_title(text:String):String { title = text; - if (parent != null) parent.changeCaption(title); + if (parent != null && !__noParentUpdate) parent.changeCaption(title); return title; } private function set_icon(text:String):String { icon = text; - if (parent != null) parent.loadIcon(icon); + if (parent != null && !__noParentUpdate) parent.loadIcon(icon); return icon; } private function set_winY(v:Float):Float { winY = v; - if (parent != null) parent.move(winX, winY); + if (parent != null && !__noParentUpdate) parent.move(winX, winY); return winY; } diff --git a/source/funkin/desktop/windows/WindowDragHitbox.hx b/source/funkin/desktop/windows/WindowDragHitbox.hx index 3abc1963..1f95758b 100644 --- a/source/funkin/desktop/windows/WindowDragHitbox.hx +++ b/source/funkin/desktop/windows/WindowDragHitbox.hx @@ -1,6 +1,8 @@ package funkin.desktop.windows; +import flixel.FlxG; import flixel.FlxObject; +import flixel.math.FlxPoint; class WindowDragHitbox extends FlxObject { public var parent:Window; @@ -11,7 +13,10 @@ class WindowDragHitbox extends FlxObject { public var dragging:Bool = false; + public var offset:FlxPoint; + public function new(left:Float, top:Float, right:Float, height:Float) { + super(); x = left; y = top; this.left = left; @@ -21,18 +26,25 @@ class WindowDragHitbox extends FlxObject { } public override function get_width() { - return parent.windowWidth - left - right; + return parent.windowCaptionCamera.width - left - right; } public override function update(elapsed:Float) { super.update(elapsed); if (dragging) { - if (DesktopMain.instance.mouseInput.justReleased) + var screenPos = FlxG.mouse.getScreenPosition(FlxG.camera); + if (DesktopMain.instance.mouseInput.justReleased) { dragging = false; + offset.put(); + return; + } + parent.move(screenPos.x + offset.x, screenPos.y + offset.y); DesktopMain.instance.mouseInput.cancel(); } else { - if (FlxG.mouse.overlaps(this)) { + if (DesktopMain.instance.mouseInput.overlaps(this, camera)) { if (DesktopMain.instance.mouseInput.justPressed) { + var screenPos = FlxG.mouse.getScreenPosition(FlxG.camera); + offset = FlxPoint.get(parent.content.winX - screenPos.x, parent.content.winY - screenPos.y); dragging = true; } DesktopMain.instance.mouseInput.cancel();