Funkin/source/funkin/ui/debug/charting/dialogs/ChartEditorUploadChartDialog.hx

166 lines
4.9 KiB
Haxe
Raw Normal View History

2023-11-24 00:41:38 -05:00
package funkin.ui.debug.charting.dialogs;
import funkin.input.Cursor;
import funkin.ui.debug.charting.dialogs.ChartEditorBaseDialog.DialogDropTarget;
import funkin.ui.debug.charting.dialogs.ChartEditorBaseDialog.DialogParams;
import funkin.util.FileUtil;
import haxe.io.Path;
import haxe.ui.containers.dialogs.Dialog.DialogButton;
import haxe.ui.containers.dialogs.Dialog.DialogEvent;
import haxe.ui.containers.dialogs.Dialogs;
import haxe.ui.notifications.NotificationManager;
import haxe.ui.notifications.NotificationType;
// @:nullSafety // TODO: Fix null safety when used with HaxeUI build macros.
2023-11-24 00:41:38 -05:00
@:build(haxe.ui.ComponentBuilder.build("assets/exclude/data/ui/chart-editor/dialogs/upload-chart.xml"))
2024-01-17 23:53:23 -05:00
@:access(funkin.ui.debug.charting.ChartEditorState)
2023-11-24 00:41:38 -05:00
class ChartEditorUploadChartDialog extends ChartEditorBaseDialog
{
var dropHandlers:Array<DialogDropTarget> = [];
public function new(state2:ChartEditorState, params2:DialogParams)
{
super(state2, params2);
this.dialogCancel.onClick = (_) -> this.hideDialog(DialogButton.CANCEL);
this.chartBox.onClick = (_) -> this.onClickChartBox();
this.chartBox.onMouseOver = function(_event) {
if (this.locked) return;
this.chartBox.swapClass('upload-bg', 'upload-bg-hover');
Cursor.cursorMode = Pointer;
}
this.chartBox.onMouseOut = function(_event) {
this.chartBox.swapClass('upload-bg-hover', 'upload-bg');
Cursor.cursorMode = Default;
}
dropHandlers.push({component: this.chartBox, handler: this.onDropFileChartBox});
}
public static function build(state:ChartEditorState, ?closable:Bool, ?modal:Bool):ChartEditorUploadChartDialog
{
var dialog = new ChartEditorUploadChartDialog(state,
{
closable: closable ?? false,
modal: modal ?? true
});
for (dropTarget in dialog.dropHandlers)
{
state.addDropHandler(dropTarget);
}
dialog.showDialog(modal ?? true);
return dialog;
}
public override function onClose(event:DialogEvent):Void
{
super.onClose(event);
if (event.button != DialogButton.APPLY && !this.closable)
{
// User cancelled the wizard! Back to the welcome dialog.
2023-12-15 21:09:01 -05:00
chartEditorState.openWelcomeDialog(this.closable);
2023-11-24 00:41:38 -05:00
}
for (dropTarget in dropHandlers)
{
2023-12-15 21:09:01 -05:00
chartEditorState.removeDropHandler(dropTarget);
2023-11-24 00:41:38 -05:00
}
}
public override function lock():Void
{
super.lock();
this.dialogCancel.disabled = true;
}
public override function unlock():Void
{
super.unlock();
this.dialogCancel.disabled = false;
}
/**
* Called when clicking the Upload Chart box.
*/
public function onClickChartBox():Void
{
if (this.locked) return;
this.lock();
2023-12-08 02:14:26 -05:00
// TODO / BUG: File filtering not working on mac finder dialog, so we don't use it for now
#if !mac
2023-11-24 00:41:38 -05:00
FileUtil.browseForBinaryFile('Open Chart', [FileUtil.FILE_EXTENSION_INFO_FNFC], onSelectFile, onCancelBrowse);
2023-12-08 02:14:26 -05:00
#else
FileUtil.browseForBinaryFile('Open Chart', null, onSelectFile, onCancelBrowse);
#end
2023-11-24 00:41:38 -05:00
}
/**
* Called when a file is selected by dropping a file onto the Upload Chart box.
*/
function onDropFileChartBox(pathStr:String):Void
{
var path:Path = new Path(pathStr);
trace('Dropped file (${path})');
try
{
2023-12-15 21:09:01 -05:00
var result:Null<Array<String>> = ChartEditorImportExportHandler.loadFromFNFCPath(chartEditorState, path.toString());
2023-11-24 00:41:38 -05:00
if (result != null)
{
2023-12-15 21:09:01 -05:00
chartEditorState.success('Loaded Chart',
result.length == 0 ? 'Loaded chart (${path.toString()})' : 'Loaded chart (${path.toString()})\n${result.join("\n")}');
2023-11-24 00:41:38 -05:00
this.hideDialog(DialogButton.APPLY);
}
else
{
2023-12-15 21:09:01 -05:00
chartEditorState.failure('Failed to Load Chart', 'Failed to load chart (${path.toString()})');
2023-11-24 00:41:38 -05:00
}
}
catch (err)
{
2023-12-15 21:09:01 -05:00
chartEditorState.failure('Failed to Load Chart', 'Failed to load chart (${path.toString()}): ${err}');
2023-11-24 00:41:38 -05:00
}
}
/**
* Called when a file is selected by the dialog displayed when clicking the Upload Chart box.
*/
function onSelectFile(selectedFile:SelectedFileInfo):Void
{
this.unlock();
if (selectedFile != null && selectedFile.bytes != null)
{
try
{
2023-12-15 21:09:01 -05:00
var result:Null<Array<String>> = ChartEditorImportExportHandler.loadFromFNFC(chartEditorState, selectedFile.bytes);
2023-11-24 00:41:38 -05:00
if (result != null)
{
2023-12-15 21:09:01 -05:00
chartEditorState.success('Loaded Chart',
2023-12-10 23:33:08 -05:00
result.length == 0 ? 'Loaded chart (${selectedFile.name})' : 'Loaded chart (${selectedFile.name})\n${result.join("\n")}');
2023-11-24 00:41:38 -05:00
2023-12-15 21:09:01 -05:00
if (selectedFile.fullPath != null) chartEditorState.currentWorkingFilePath = selectedFile.fullPath;
2023-11-24 00:41:38 -05:00
this.hideDialog(DialogButton.APPLY);
}
}
catch (err)
{
2023-12-15 21:09:01 -05:00
chartEditorState.failure('Failed to Load Chart', 'Failed to load chart (${selectedFile.name}): ${err}');
2023-11-24 00:41:38 -05:00
}
}
}
function onCancelBrowse():Void
{
this.unlock();
}
}