context menu done

This commit is contained in:
Yoshman29
2022-12-23 15:32:18 +01:00
parent 9ffea799c7
commit 2dbefc46d5
7 changed files with 99 additions and 8 deletions
+1 -1
View File
@@ -23,5 +23,5 @@
<!-- CONTEXT MENU -->
<contextBackground path="context" textColor="0,0,0" left="4" top="4" right="4" bottom="4" />
<contextOption path="contextOption" textColor="0,0,0" height="20" />
<contextOption path="contextOption" textColor="255,255,255" height="20" />
</theme>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

After

Width:  |  Height:  |  Size: 257 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 B

After

Width:  |  Height:  |  Size: 308 B

+1 -1
View File
@@ -1 +1 @@
1713
1719
+30 -4
View File
@@ -12,6 +12,7 @@ import flixel.FlxSprite;
import flixel.FlxBasic;
import flixel.FlxCamera;
import flixel.math.FlxPoint;
import flixel.util.FlxDestroyUtil;
import funkin.desktop.editors.*;
import funkin.desktop.windows.*;
import funkin.desktop.theme.Theme;
@@ -29,12 +30,14 @@ class DesktopMain extends MusicBeatState {
public static var theme:Theme = null;
public static var contextMenu:ContextMenu = null;
public static function init() {
theme = Theme.loadFromAssets(Paths.getPath('images/desktop/ui.xml', TEXT, null));
mouseInput = new MouseInput();
FlxG.signals.preUpdate.add(function() {
mouseInput.set(FlxG.mouse.pressed, FlxG.mouse.justPressed, FlxG.mouse.justReleased);
updateMouseInput();
});
}
@@ -62,8 +65,15 @@ class DesktopMain extends MusicBeatState {
add(windows);
}
private static function updateMouseInput() {
mouseInput.set(FlxG.mouse.pressed, FlxG.mouse.justPressed, FlxG.mouse.justReleased, FlxG.mouse.pressedRight, FlxG.mouse.justPressedRight, FlxG.mouse.justReleasedRight);
}
public override function update(elapsed:Float) {
mouseInput.set(FlxG.mouse.pressed, FlxG.mouse.justPressed, FlxG.mouse.justReleased);
updateMouseInput();
if (contextMenu != null)
contextMenu.update(elapsed);
super.update(elapsed);
@@ -85,6 +95,12 @@ class DesktopMain extends MusicBeatState {
return windows.add(new Window(content));
}
public override function draw() {
super.draw();
if (contextMenu != null)
contextMenu.draw();
}
public function updateScreenRes() {
// update wallpaper
wallpaper.setUnstretchedGraphicSize(FlxG.width, FlxG.height);
@@ -95,6 +111,8 @@ class DesktopMain extends MusicBeatState {
public override function destroy() {
super.destroy();
contextMenu = FlxDestroyUtil.destroy(contextMenu);
FlxG.scaleMode = oldScaleMode;
FlxG.mouse.useSystemCursor = FlxG.mouse.visible = false;
@@ -124,19 +142,27 @@ class MouseInput {
public var pressed:Bool = false;
public var justPressed:Bool = false;
public var justReleased:Bool = false;
public var pressedRight:Bool = false;
public var justPressedRight:Bool = false;
public var justReleasedRight:Bool = false;
public var screenPos:FlxPoint = null;
private var __cancelled:Bool = false;
public function cancel() {
pressed = justPressed = justReleased = false;
pressed = justPressed = justReleased = pressedRight = justPressedRight = justReleasedRight = false;
__cancelled = true;
}
public function new() {}
public function set(pressed:Bool, justPressed:Bool, justReleased:Bool) {
public function set(pressed:Bool, justPressed:Bool, justReleased:Bool, pressedRight:Bool, justPressedRight:Bool, justReleasedRight:Bool) {
this.pressed = pressed;
this.justPressed = justPressed;
this.justReleased = justReleased;
this.pressedRight = pressedRight;
this.justPressedRight = justPressedRight;
this.justReleasedRight = justReleasedRight;
this.screenPos = FlxG.mouse.getScreenPosition(FlxG.camera);
this.__cancelled = false;
}
+55 -2
View File
@@ -1,5 +1,7 @@
package funkin.desktop.windows;
import flixel.FlxCamera;
import flixel.util.FlxDestroyUtil;
import flixel.FlxSprite;
import funkin.desktop.sprites.*;
import flixel.FlxObject;
@@ -9,9 +11,16 @@ class ContextMenu extends WindowGroup<FlxObject> {
public var bg:SpliceSprite;
public var texts:Array<WindowText> = [];
public var selectionBGs:Array<FlxSprite> = [];
public var callback:Int->Void;
public var contextOptions:Array<ContextOption> = [];
public var contextCamera:FlxCamera;
public override function new(x:Float, y:Float, options:Array<ContextOption>, callback:Int->Void) {
super();
this.callback = callback;
this.contextOptions = options;
DesktopMain.contextMenu = FlxDestroyUtil.destroy(DesktopMain.contextMenu);
var bgTheme = DesktopMain.theme.contextBackground;
var optionTheme = DesktopMain.theme.contextOption;
@@ -23,23 +32,67 @@ class ContextMenu extends WindowGroup<FlxObject> {
var w:Float = 80;
for(k=>o in options) {
var selectSpr = new FlxSprite(bgTheme.left, bgTheme.top + (k * optionTheme.height));
selectSpr.loadGraphic(Paths.image(bgTheme.sprite));
selectSpr.loadGraphic(Paths.image(optionTheme.sprite));
selectSpr.setGraphicSize(10, Std.int(optionTheme.height));
selectSpr.visible = false;
selectionBGs.push(selectSpr);
add(selectSpr);
var text = new WindowText(4, 4, 0, o.name);
text.y = selectSpr.y + ((selectSpr.height - text.height) / 2);
text.applyFontSettings(bgTheme);
texts.push(text);
add(text);
if (w < text.width)
w = text.width;
lastOption = selectSpr;
}
var ow = Std.int(w) + 20;
for(o in selectionBGs) {
o.setGraphicSize(ow, Std.int(optionTheme.height));
o.updateHitbox();
}
bg.resize(bgTheme.left + ow + bgTheme.right, lastOption.y + lastOption.height + bgTheme.bottom);
contextCamera = camera = new FlxCamera(Std.int(x), Std.int(y), Std.int(bg.width), Std.int(bg.height));
contextCamera.pixelPerfectRender = true;
contextCamera.bgColor = 0;
FlxG.cameras.add(contextCamera, false);
DesktopMain.contextMenu = this;
}
public override function update(elapsed:Float) {
super.update(elapsed);
if (DesktopMain.mouseInput.overlaps(bg, camera)) {
for(i=>o in selectionBGs) {
var wasVisible = o.visible;
if (o.visible = DesktopMain.mouseInput.overlaps(o, camera)) {
if (DesktopMain.mouseInput.justReleased) {
// select option
if (contextOptions[i].callback != null)
contextOptions[i].callback();
if (callback != null)
callback(i);
destroy();
}
}
if (o.visible != wasVisible) {
texts[i].applyFontSettings(o.visible ? DesktopMain.theme.contextOption : DesktopMain.theme.contextBackground);
}
}
DesktopMain.mouseInput.cancel();
} else {
if (DesktopMain.mouseInput.justPressed) {
destroy();
}
}
}
public override function destroy() {
super.destroy();
FlxG.cameras.remove(camera, true);
DesktopMain.contextMenu = null;
FlxG.cameras.remove(contextCamera, true);
}
}
@@ -54,6 +54,18 @@ class WindowDragHitbox extends FlxObject {
var screenPos = FlxG.mouse.getScreenPosition(FlxG.camera);
offset = FlxPoint.get(parent.content.winX - screenPos.x, parent.content.winY - screenPos.y);
dragging = true;
} else if (DesktopMain.mouseInput.justPressedRight) {
var input = DesktopMain.mouseInput;
new ContextMenu(input.screenPos.x, input.screenPos.y, [
{
name: "Close",
callback: function() {
parent.close();
}
}
], function(i) {
trace(i);
});
}
DesktopMain.mouseInput.cancel();
}