From d8bd567fde79a3becd61cb0fbe42fec610d42366 Mon Sep 17 00:00:00 2001 From: Yoshman29 Date: Thu, 20 Oct 2022 22:53:11 +0200 Subject: [PATCH] some window stuff --- buildnumber.txt | 2 +- source/funkin/desktop/DesktopMain.hx | 30 +++- source/funkin/desktop/Window.hx | 57 -------- source/funkin/desktop/WindowContentSprite.hx | 28 ---- source/funkin/desktop/WindowGroup.hx | 7 - source/funkin/desktop/editors/HelloWorld.hx | 20 +++ source/funkin/desktop/editors/import.hx | 3 + source/funkin/desktop/windows/Window.hx | 132 ++++++++++++++++++ .../{ => windows}/WindowCaptionButton.hx | 2 +- .../funkin/desktop/windows/WindowContent.hx | 46 ++++++ .../desktop/windows/WindowDragHitbox.hx | 42 ++++++ source/funkin/desktop/windows/WindowGroup.hx | 20 +++ source/funkin/mods/ModsAssetLibrary.hx | 5 +- source/funkin/mods/ModsFolder.hx | 23 ++- source/funkin/system/CoolUtil.hx | 14 ++ 15 files changed, 332 insertions(+), 99 deletions(-) delete mode 100644 source/funkin/desktop/Window.hx delete mode 100644 source/funkin/desktop/WindowContentSprite.hx delete mode 100644 source/funkin/desktop/WindowGroup.hx create mode 100644 source/funkin/desktop/editors/HelloWorld.hx create mode 100644 source/funkin/desktop/editors/import.hx create mode 100644 source/funkin/desktop/windows/Window.hx rename source/funkin/desktop/{ => windows}/WindowCaptionButton.hx (96%) create mode 100644 source/funkin/desktop/windows/WindowContent.hx create mode 100644 source/funkin/desktop/windows/WindowDragHitbox.hx create mode 100644 source/funkin/desktop/windows/WindowGroup.hx diff --git a/buildnumber.txt b/buildnumber.txt index 38017008..8a260223 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -668 \ No newline at end of file +686 \ No newline at end of file diff --git a/source/funkin/desktop/DesktopMain.hx b/source/funkin/desktop/DesktopMain.hx index b43cac29..95cea120 100644 --- a/source/funkin/desktop/DesktopMain.hx +++ b/source/funkin/desktop/DesktopMain.hx @@ -7,6 +7,8 @@ import flixel.system.scaleModes.FixedScaleAdjustSizeScaleMode; import flixel.FlxG; import flixel.FlxSprite; import flixel.math.FlxPoint; +import funkin.desktop.editors.HelloWorld; +import funkin.desktop.windows.*; class DesktopMain extends MusicBeatState { public var oldScaleMode:BaseScaleMode; @@ -17,6 +19,8 @@ class DesktopMain extends MusicBeatState { public static var instance:DesktopMain; + public var mouseInput:MouseInput = new MouseInput(); + public override function create() { super.create(); instance = this; @@ -31,22 +35,28 @@ class DesktopMain extends MusicBeatState { wallpaper.loadGraphic(BitmapData.fromFile('${Sys.getEnv("AppData")}\\Microsoft\\Windows\\Themes\\TranscodedWallpaper')); } catch(e) #end wallpaper.loadGraphic(Paths.image("menuBG")); wallpaper.scrollFactor.set(0, 0); // anchor at top left + wallpaper.antialiasing = true; add(wallpaper); screenSize = new FlxPoint(-1, -1); windows = new WindowGroup(); add(windows); - windows.add(new Window("Test Window", 640, 480, 200, 200)); } public override function update(elapsed:Float) { + mouseInput.set(FlxG.mouse.pressed, FlxG.mouse.justPressed, FlxG.mouse.justReleased); + super.update(elapsed); if (screenSize.x != FlxG.width || screenSize.y != FlxG.height) { screenSize.set(FlxG.width, FlxG.height); updateScreenRes(); } + + // debug window + if (FlxG.keys.justPressed.SPACE) + windows.add(new Window(new HelloWorld())); } public function updateScreenRes() { @@ -62,4 +72,22 @@ class DesktopMain extends MusicBeatState { // TODO: switch back while using config FlxG.resizeGame(1280, 720); } +} + +class MouseInput { + public var pressed:Bool = false; + public var justPressed:Bool = false; + public var justReleased:Bool = false; + + public function cancel() { + pressed = justPressed = justReleased = false; + } + + public function new() {} + + public function set(pressed:Bool, justPressed:Bool, justReleased:Bool) { + this.pressed = pressed; + this.justPressed = justPressed; + this.justReleased = justReleased; + } } \ No newline at end of file diff --git a/source/funkin/desktop/Window.hx b/source/funkin/desktop/Window.hx deleted file mode 100644 index 52ca5d31..00000000 --- a/source/funkin/desktop/Window.hx +++ /dev/null @@ -1,57 +0,0 @@ -package funkin.desktop; - -import flixel.group.FlxSpriteGroup; -import openfl.geom.Rectangle; -import flixel.addons.ui.FlxUI9SliceSprite; -import flixel.FlxSprite; -import flixel.FlxCamera; -import flixel.FlxG; -import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup; - -class Window extends FlxTypedSpriteGroup { - public var windowFrame:FlxUI9SliceSprite; - public var captionButtons:FlxTypedSpriteGroup; - - public var windowWidth:Int = 0; - public var windowHeight:Int = 0; - - public var windowCamera:FlxCamera; - public var windowContentSprite:WindowContentSprite; - - public function new(caption:String, width:Int = 640, height:Int = 480, x:Int = 20, y:Int = 20) { - super(); - windowFrame = new FlxUI9SliceSprite(0, 0, Paths.image('desktop/windowFrame'), new Rectangle(10, 10), [3, 3, 6, 6]); - add(windowFrame); - - captionButtons = new FlxTypedSpriteGroup(); - for(i in 0...3) { - var btn = new WindowCaptionButton(this, i); - btn.x = (i+1) * -18; - captionButtons.add(btn); - } - add(captionButtons); - - windowWidth = width; - windowHeight = height; - - scrollFactor.set(0.5, 0.5); - updateWindowFrame(); - - windowContentSprite = new WindowContentSprite(4, 23, FlxG.camera); - add(windowContentSprite); - } - - public function resize(width:Int, height:Int) { - windowFrame.resize(width + 4 + 4, height + 23 + 4); - - } - - public function close() { - DesktopMain.instance.windows.remove(this, true); - } - - public function updateWindowFrame() { - captionButtons.setPosition(windowWidth - 4, 4); - windowFrame.resize(windowWidth, windowHeight); - } -} \ No newline at end of file diff --git a/source/funkin/desktop/WindowContentSprite.hx b/source/funkin/desktop/WindowContentSprite.hx deleted file mode 100644 index 20b9d202..00000000 --- a/source/funkin/desktop/WindowContentSprite.hx +++ /dev/null @@ -1,28 +0,0 @@ -package funkin.desktop; - -import flixel.FlxSprite; -import flixel.FlxCamera; - -class WindowContentSprite extends FlxSprite { - var cam:FlxCamera; - public override function get_width() { - return cam.width; - } - public override function get_height() { - return cam.height; - } - - public function new(x:Float, y:Float, cam:FlxCamera) { - super(x, y); - this.cam = cam; - makeGraphic(cam.width, cam.height, 0xFF000000); - } - - public override function update(elapsed:Float) { - super.update(elapsed); - // cam.render() - } - public override function draw() { - super.draw(); - } -} \ No newline at end of file diff --git a/source/funkin/desktop/WindowGroup.hx b/source/funkin/desktop/WindowGroup.hx deleted file mode 100644 index e2808d3f..00000000 --- a/source/funkin/desktop/WindowGroup.hx +++ /dev/null @@ -1,7 +0,0 @@ -package funkin.desktop; - -import flixel.group.FlxGroup.FlxTypedGroup; - -class WindowGroup extends FlxTypedGroup { - // not removing, may be interesting -} \ No newline at end of file diff --git a/source/funkin/desktop/editors/HelloWorld.hx b/source/funkin/desktop/editors/HelloWorld.hx new file mode 100644 index 00000000..211353fa --- /dev/null +++ b/source/funkin/desktop/editors/HelloWorld.hx @@ -0,0 +1,20 @@ +package funkin.desktop.editors; + +import flixel.util.FlxDestroyUtil.IFlxDestroyable; +import flixel.addons.ui.FlxUIText; + +class HelloWorld extends WindowContent { + public override function new() { + super("Test Window", 0, 0, 640, 480); + } + + public override function create() { + super.create(); + + var helloWorldText = new FlxUIText(0, 0, 0, "Hello, World!"); + helloWorldText.color = 0xFF000000; + helloWorldText.scrollFactor.set(0.5, 0.5); + helloWorldText.cameraCenter(camera); + add(helloWorldText); + } +} \ No newline at end of file diff --git a/source/funkin/desktop/editors/import.hx b/source/funkin/desktop/editors/import.hx new file mode 100644 index 00000000..23162a24 --- /dev/null +++ b/source/funkin/desktop/editors/import.hx @@ -0,0 +1,3 @@ +package funkin.desktop.editors; + +import funkin.desktop.windows.*; \ No newline at end of file diff --git a/source/funkin/desktop/windows/Window.hx b/source/funkin/desktop/windows/Window.hx new file mode 100644 index 00000000..882883dd --- /dev/null +++ b/source/funkin/desktop/windows/Window.hx @@ -0,0 +1,132 @@ +package funkin.desktop.windows; + +import flixel.addons.ui.FlxUIText; +import flixel.group.FlxGroup.FlxTypedGroup; +import flixel.FlxBasic; +import openfl.geom.Point; +import flixel.group.FlxSpriteGroup; +import openfl.geom.Rectangle; +import flixel.addons.ui.FlxUI9SliceSprite; +import flixel.FlxSprite; +import flixel.FlxCamera; +import flixel.FlxG; +import flixel.tweens.FlxTween; +import flixel.tweens.FlxEase; +import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup; + +class Window extends FlxTypedGroup { + public var windowFrame:FlxUI9SliceSprite; + public var captionButtons:FlxTypedSpriteGroup; + + public var windowWidth:Int = 0; + public var windowHeight:Int = 0; + + public var windowCaptionCamera:FlxCamera; + public var windowCameras:Array = []; + + private var __captionTweens:Array = []; + + public var content:WindowContent; + + public var caption:FlxUIText; + + public function new(content:WindowContent) { + super(); + windowFrame = new FlxUI9SliceSprite(0, 0, Paths.image('desktop/windowFrame'), new Rectangle(10, 10), [3, 3, 6, 6]); + add(windowFrame); + + caption = new FlxUIText(4, 4, 0, content.title); + captionButtons = new FlxTypedSpriteGroup(); + for(i in 0...3) { + var btn = new WindowCaptionButton(this, i); + btn.x = (i+1) * -18; + captionButtons.add(btn); + } + add(caption); + add(captionButtons); + + this.content = content; + + // scrollFactor.set(0.5, 0.5); + + windowCaptionCamera = new FlxCamera(0, 0, content.width, content.height, 1); + windowCaptionCamera.pixelPerfectRender = true; + windowCaptionCamera.bgColor = 0; + FlxG.cameras.add(windowCaptionCamera, false); + cameras = [windowCaptionCamera]; + + for(btn in captionButtons.members) + btn.cameras = cameras; // so that buttons detects on the right camera + + if (content.windowCamera == null) { + content.windowCamera = new FlxCamera(Std.int(content.winX + 4), Std.int(content.winY + 23), Std.int(content.width), Std.int(content.height), 1); + content.windowCamera.pixelPerfectRender = true; + content.windowCamera.bgColor = -1; // walter white + } + content.cameras = [content.windowCamera]; + content.parent = this; + content.create(); + add(content); + + windowWidth = content.width; + windowHeight = content.height; + + FlxG.cameras.add(content.windowCamera, false); + windowCameras.push(content.windowCamera); + + + popupCamera(windowCaptionCamera); + for(e in windowCameras) popupCamera(e); + + resize(content.width, content.height); + move(content.winX, content.winY); + } + + public function changeCaption(text:String) { + caption.text = text; + } + + public function popupCamera(cam:FlxCamera) { + cam.flashSprite.scaleX = 0.85; + cam.flashSprite.scaleY = 0.85; + cam.alpha = 0; + + __captionTweens.push(FlxTween.tween(cam.flashSprite, {scaleX: 1, scaleY: 1}, 1/3, {ease: FlxEase.cubeOut})); + __captionTweens.push(FlxTween.tween(cam, {alpha: 1}, 1/3, {ease: FlxEase.cubeOut})); + } + public override function destroy() { + super.destroy(); + for(e in __captionTweens) e.cancel(); + FlxG.cameras.remove(windowCaptionCamera, true); + for(cam in windowCameras) FlxG.cameras.remove(cam, true); + } + + public function resize(width:Int, height:Int) { + windowWidth = width; + windowHeight = height; + updateWindowFrame(); + } + + public function move(x:Float, y:Float) { + windowCaptionCamera.x = x; + windowCaptionCamera.y = y; + for(e in windowCameras) { + e.x = x + 4; + e.y = y + 23; + } + } + + public function close() { + DesktopMain.instance.windows.remove(this, true); + destroy(); + } + + public function updateWindowFrame() { + windowFrame.resize(windowWidth + 4 + 4, windowHeight + 23 + 4); + windowCaptionCamera.setSize(windowWidth + 4 + 4, windowHeight + 23 + 4); + captionButtons.setPosition(windowWidth + 4, 4); + for(e in windowCameras) { + e.setSize(windowWidth, windowHeight); + } + } +} \ No newline at end of file diff --git a/source/funkin/desktop/WindowCaptionButton.hx b/source/funkin/desktop/windows/WindowCaptionButton.hx similarity index 96% rename from source/funkin/desktop/WindowCaptionButton.hx rename to source/funkin/desktop/windows/WindowCaptionButton.hx index ae46c3b5..af78f15f 100644 --- a/source/funkin/desktop/WindowCaptionButton.hx +++ b/source/funkin/desktop/windows/WindowCaptionButton.hx @@ -1,4 +1,4 @@ -package funkin.desktop; +package funkin.desktop.windows; import flixel.addons.ui.FlxUIButton; import flixel.FlxSprite; diff --git a/source/funkin/desktop/windows/WindowContent.hx b/source/funkin/desktop/windows/WindowContent.hx new file mode 100644 index 00000000..3f562c00 --- /dev/null +++ b/source/funkin/desktop/windows/WindowContent.hx @@ -0,0 +1,46 @@ +package funkin.desktop.windows; + +import flixel.FlxSprite; +import flixel.group.FlxGroup.FlxTypedGroup; +import flixel.FlxCamera; + +class WindowContent extends FlxTypedGroup { + public var winX(default, set):Float = 0; + public var winY(default, set):Float = 0; + public var title(default, set):String = "Window"; + + public var windowCamera:FlxCamera = null; + public var parent:Window = null; + + public var width:Int = 0; + public var height:Int = 0; + + private function set_winX(v:Float):Float { + winX = v; + if (parent != null) parent.move(winX, winY); + return winX; + } + + private function set_title(text:String):String { + title = text; + if (parent != null) parent.changeCaption(title); + return title; + } + private function set_winY(v:Float):Float { + winY = v; + if (parent != null) parent.move(winX, winY); + return winY; + } + + public function new(caption:String, x:Float, y:Float, width:Int, height:Int) { + super(); + this.width = width; + this.height = height; + this.winX = x; + this.winY = y; + } + + public function create() { + + } +} \ No newline at end of file diff --git a/source/funkin/desktop/windows/WindowDragHitbox.hx b/source/funkin/desktop/windows/WindowDragHitbox.hx new file mode 100644 index 00000000..3abc1963 --- /dev/null +++ b/source/funkin/desktop/windows/WindowDragHitbox.hx @@ -0,0 +1,42 @@ +package funkin.desktop.windows; + +import flixel.FlxObject; + +class WindowDragHitbox extends FlxObject { + public var parent:Window; + + public var left:Float; + public var top:Float; + public var right:Float; + + public var dragging:Bool = false; + + public function new(left:Float, top:Float, right:Float, height:Float) { + x = left; + y = top; + this.left = left; + this.top = top; + this.right = right; + this.height = height; + } + + public override function get_width() { + return parent.windowWidth - left - right; + } + + public override function update(elapsed:Float) { + super.update(elapsed); + if (dragging) { + if (DesktopMain.instance.mouseInput.justReleased) + dragging = false; + DesktopMain.instance.mouseInput.cancel(); + } else { + if (FlxG.mouse.overlaps(this)) { + if (DesktopMain.instance.mouseInput.justPressed) { + dragging = true; + } + DesktopMain.instance.mouseInput.cancel(); + } + } + } +} \ No newline at end of file diff --git a/source/funkin/desktop/windows/WindowGroup.hx b/source/funkin/desktop/windows/WindowGroup.hx new file mode 100644 index 00000000..ea7968c6 --- /dev/null +++ b/source/funkin/desktop/windows/WindowGroup.hx @@ -0,0 +1,20 @@ +package funkin.desktop.windows; + +import flixel.group.FlxGroup.FlxTypedGroup; + +class WindowGroup extends FlxTypedGroup { + public override function update(elapsed:Float) { + // update them backwards, so that the top most window gets the priority + var i = length-1; + while(i >= 0) { + var window = members[i]; + if (window == null || !window.exists) { + i--; + continue; + } + window.update(elapsed); + + i--; + } + } +} \ No newline at end of file diff --git a/source/funkin/mods/ModsAssetLibrary.hx b/source/funkin/mods/ModsAssetLibrary.hx index 11e7108c..60c15439 100644 --- a/source/funkin/mods/ModsAssetLibrary.hx +++ b/source/funkin/mods/ModsAssetLibrary.hx @@ -25,6 +25,7 @@ using StringTools; class ModsAssetLibrary extends AssetLibrary { public var folderPath:String; public var libName:String; + public var useImageCache:Bool = false; public function new(folderPath:String, libName:String) { this.folderPath = folderPath; @@ -89,7 +90,7 @@ class ModsAssetLibrary extends AssetLibrary { } public override function getImage(id:String):Image { - if (__isCacheValid(cachedImages, id)) + if (useImageCache && __isCacheValid(cachedImages, id)) return cachedImages.get(id); else { if (!exists(id, "IMAGE")) { @@ -141,7 +142,7 @@ class ModsAssetLibrary extends AssetLibrary { return false; if (editedTimes[asset] == null) return false; if (editedTimes[asset] < FileSystem.stat(getPath(asset)).mtime.getTime()) return false; - return true; + return cache.exists(asset) && cache[asset] != null; } private function __parseAsset(asset:String):Bool { diff --git a/source/funkin/mods/ModsFolder.hx b/source/funkin/mods/ModsFolder.hx index 4a235fd6..8a1ec061 100644 --- a/source/funkin/mods/ModsFolder.hx +++ b/source/funkin/mods/ModsFolder.hx @@ -125,8 +125,8 @@ class ModsFolder { Assets.cache.clear(); lime.utils.Assets.cache.clear(); - if (currentModFolder == null) return; #if MOD_SUPPORT + if (currentModFolder == null) return; var bmapsToRemove:Array = []; @:privateAccess for(bmap in FlxG.bitmap._cache) { @@ -146,9 +146,28 @@ class ModsFolder { } } } + + // TODO: add setting for cache clearing + @:privateAccess + for(libName=>lib in lime.utils.Assets.libraries) { + var library = lib; + if (library is openfl.utils.AssetLibrary) { + var flLib = cast(library, openfl.utils.AssetLibrary); + @:privateAccess + if (flLib.__proxy != null) library = flLib.__proxy; + } + if (library is ModsAssetLibrary) { + var modLib = cast(library, ModsAssetLibrary); + @:privateAccess + modLib.cachedBytes = []; + @:privateAccess + for(sound in modLib.cachedAudioBuffers) + sound.dispose(); + + } + } for(e in bmapsToRemove) FlxG.bitmap.remove(e); - #end } } \ No newline at end of file diff --git a/source/funkin/system/CoolUtil.hx b/source/funkin/system/CoolUtil.hx index 249d822d..e1d3bbb1 100644 --- a/source/funkin/system/CoolUtil.hx +++ b/source/funkin/system/CoolUtil.hx @@ -7,6 +7,9 @@ import flixel.util.FlxColor; import flixel.math.FlxMath; import flixel.FlxG; import flixel.FlxSprite; +import flixel.FlxObject; +import flixel.FlxCamera; +import flixel.util.FlxAxes; using StringTools; @@ -174,4 +177,15 @@ class CoolUtil default: key.toString(); } } + + public static function cameraCenter(obj:FlxObject, cam:FlxCamera, axes:FlxAxes = XY) { + switch(axes) { + case XY: + obj.setPosition((cam.width - obj.width) / 2, (cam.height - obj.height) / 2); + case X: + obj.x = (cam.width - obj.width) / 2; + case Y: + obj.y = (cam.height - obj.height) / 2; + } + } } \ No newline at end of file