Mirror for Friday Night Funkin
1package funkin.ui.debug.charting.dialogs;
2
3import funkin.input.Cursor;
4import funkin.ui.debug.charting.dialogs.ChartEditorBaseDialog.DialogDropTarget;
5import funkin.ui.debug.charting.dialogs.ChartEditorBaseDialog.DialogParams;
6import funkin.util.FileUtil;
7import haxe.io.Path;
8import haxe.ui.containers.dialogs.Dialog.DialogButton;
9import haxe.ui.containers.dialogs.Dialog.DialogEvent;
10import haxe.ui.containers.dialogs.Dialogs.SelectedFileInfo;
11
12// @:nullSafety // TODO: Fix null safety when used with HaxeUI build macros.
13@:build(haxe.ui.ComponentBuilder.build("assets/exclude/data/ui/chart-editor/dialogs/upload-chart.xml"))
14@:access(funkin.ui.debug.charting.ChartEditorState)
15class ChartEditorUploadChartDialog extends ChartEditorBaseDialog
16{
17 var dropHandlers:Array<DialogDropTarget> = [];
18
19 public function new(state2:ChartEditorState, params2:DialogParams)
20 {
21 super(state2, params2);
22
23 this.dialogCancel.onClick = (_) -> this.hideDialog(DialogButton.CANCEL);
24
25 this.chartBox.onClick = (_) -> this.onClickChartBox();
26
27 this.chartBox.onMouseOver = function(_event) {
28 if (this.locked) return;
29 this.chartBox.swapClass('upload-bg', 'upload-bg-hover');
30 Cursor.cursorMode = Pointer;
31 }
32
33 this.chartBox.onMouseOut = function(_event) {
34 this.chartBox.swapClass('upload-bg-hover', 'upload-bg');
35 Cursor.cursorMode = Default;
36 }
37
38 dropHandlers.push({component: this.chartBox, handler: this.onDropFileChartBox});
39 }
40
41 public static function build(state:ChartEditorState, ?closable:Bool, ?modal:Bool):ChartEditorUploadChartDialog
42 {
43 var dialog = new ChartEditorUploadChartDialog(state,
44 {
45 closable: closable ?? false,
46 modal: modal ?? true
47 });
48
49 for (dropTarget in dialog.dropHandlers)
50 {
51 state.addDropHandler(dropTarget);
52 }
53
54 dialog.showDialog(modal ?? true);
55
56 return dialog;
57 }
58
59 public override function onClose(event:DialogEvent):Void
60 {
61 super.onClose(event);
62
63 if (event.button != DialogButton.APPLY && !this.closable)
64 {
65 // User cancelled the wizard! Back to the welcome dialog.
66 chartEditorState.openWelcomeDialog(this.closable);
67 }
68
69 for (dropTarget in dropHandlers)
70 {
71 chartEditorState.removeDropHandler(dropTarget);
72 }
73 }
74
75 public override function lock():Void
76 {
77 super.lock();
78 this.dialogCancel.disabled = true;
79 }
80
81 public override function unlock():Void
82 {
83 super.unlock();
84 this.dialogCancel.disabled = false;
85 }
86
87 /**
88 * Called when clicking the Upload Chart box.
89 */
90 public function onClickChartBox():Void
91 {
92 if (this.locked) return;
93
94 this.lock();
95 // TODO / BUG: File filtering not working on mac finder dialog, so we don't use it for now
96 #if !mac
97 FileUtil.browseForBinaryFile('Open Chart', [FileUtil.FILE_EXTENSION_INFO_FNFC], onSelectFile, onCancelBrowse);
98 #else
99 FileUtil.browseForBinaryFile('Open Chart', null, onSelectFile, onCancelBrowse);
100 #end
101 }
102
103 /**
104 * Called when a file is selected by dropping a file onto the Upload Chart box.
105 */
106 function onDropFileChartBox(pathStr:String):Void
107 {
108 var path:Path = new Path(pathStr);
109 trace('Dropped file (${path})');
110
111 try
112 {
113 var result:Null<Array<String>> = ChartEditorImportExportHandler.loadFromFNFCPath(chartEditorState, path.toString());
114 if (result != null)
115 {
116 chartEditorState.success('Loaded Chart',
117 result.length == 0 ? 'Loaded chart (${path.toString()})' : 'Loaded chart (${path.toString()})\n${result.join("\n")}');
118 this.hideDialog(DialogButton.APPLY);
119 }
120 else
121 {
122 chartEditorState.failure('Failed to Load Chart', 'Failed to load chart (${path.toString()})');
123 }
124 }
125 catch (err)
126 {
127 chartEditorState.failure('Failed to Load Chart', 'Failed to load chart (${path.toString()}): ${err}');
128 }
129 }
130
131 /**
132 * Called when a file is selected by the dialog displayed when clicking the Upload Chart box.
133 */
134 function onSelectFile(selectedFile:SelectedFileInfo):Void
135 {
136 this.unlock();
137
138 if (selectedFile != null && selectedFile.bytes != null)
139 {
140 try
141 {
142 var result:Null<Array<String>> = ChartEditorImportExportHandler.loadFromFNFC(chartEditorState, selectedFile.bytes);
143 if (result != null)
144 {
145 chartEditorState.success('Loaded Chart',
146 result.length == 0 ? 'Loaded chart (${selectedFile.name})' : 'Loaded chart (${selectedFile.name})\n${result.join("\n")}');
147
148 if (selectedFile.fullPath != null) chartEditorState.currentWorkingFilePath = selectedFile.fullPath;
149 this.hideDialog(DialogButton.APPLY);
150 }
151 }
152 catch (err)
153 {
154 chartEditorState.failure('Failed to Load Chart', 'Failed to load chart (${selectedFile.name}): ${err}');
155 }
156 }
157 }
158
159 function onCancelBrowse():Void
160 {
161 this.unlock();
162 }
163}