HeavenStudioPlus/Assets/Scripts/LevelEditor/Editor.cs

412 lines
15 KiB
C#
Raw Normal View History

2022-01-30 12:03:37 +00:00
using System;
using System.IO;
2022-01-03 22:42:43 +00:00
using System.Collections;
using System.Collections.Generic;
2022-01-30 12:03:37 +00:00
using System.Threading.Tasks;
2022-01-03 22:42:43 +00:00
2022-01-30 12:03:37 +00:00
using UnityEngine;
2022-01-03 22:42:43 +00:00
using UnityEngine.UI;
2022-01-30 12:03:37 +00:00
using UnityEngine.Networking;
2022-01-03 22:42:43 +00:00
using Newtonsoft.Json;
using TMPro;
using Starpelly;
2022-01-30 12:03:37 +00:00
using SFB;
using RhythmHeavenMania.Editor.Track;
2022-01-30 12:03:37 +00:00
using RhythmHeavenMania.Util;
using System.IO.Compression;
using System.Text;
2022-01-03 22:42:43 +00:00
namespace RhythmHeavenMania.Editor
{
public class Editor : MonoBehaviour
{
private Initializer Initializer;
2022-01-06 00:11:33 +00:00
[SerializeField] private Canvas MainCanvas;
[SerializeField] public Camera EditorCamera;
2022-01-06 00:11:33 +00:00
2022-01-03 22:42:43 +00:00
[Header("Rect")]
[SerializeField] private RenderTexture ScreenRenderTexture;
[SerializeField] private RawImage Screen;
2022-01-09 23:35:55 +00:00
[SerializeField] private RectTransform GridGameSelector;
public RectTransform eventSelectorBG;
2022-01-03 22:42:43 +00:00
2022-01-06 00:11:33 +00:00
[Header("Components")]
[SerializeField] private Timeline Timeline;
[SerializeField] private TMP_Text GameEventSelectorTitle;
2022-01-06 00:11:33 +00:00
2022-01-15 07:08:23 +00:00
[Header("Toolbar")]
[SerializeField] private Button NewBTN;
[SerializeField] private Button OpenBTN;
[SerializeField] private Button SaveBTN;
[SerializeField] private Button UndoBTN;
[SerializeField] private Button RedoBTN;
2022-01-29 21:59:20 +00:00
[SerializeField] private Button MusicSelectBTN;
[SerializeField] private Button EditorSettingsBTN;
[SerializeField] private Button EditorThemeBTN;
[SerializeField] private Button FullScreenBTN;
2022-01-15 07:08:23 +00:00
2022-01-30 12:03:37 +00:00
[Header("Properties")]
private bool changedMusic = false;
2022-02-03 22:20:26 +00:00
private bool loadedMusic = false;
2022-01-30 12:03:37 +00:00
private string currentRemixPath = "";
2022-01-30 23:40:12 +00:00
private int lastEditorObjectsCount = 0;
private bool fullscreen;
2022-02-03 02:09:50 +00:00
public bool discordDuringTesting = false;
2022-01-12 03:29:27 +00:00
public static Editor instance { get; private set; }
2022-01-03 22:42:43 +00:00
private void Start()
{
instance = this;
2022-01-03 22:42:43 +00:00
Initializer = GetComponent<Initializer>();
2022-01-06 00:11:33 +00:00
}
public void Init()
{
2022-02-03 02:09:50 +00:00
GameCamera.instance.camera.targetTexture = ScreenRenderTexture;
2022-01-03 22:42:43 +00:00
GameManager.instance.CursorCam.targetTexture = ScreenRenderTexture;
Screen.texture = ScreenRenderTexture;
2022-01-06 00:11:33 +00:00
2022-01-08 16:42:48 +00:00
GameManager.instance.Init();
2022-01-06 00:11:33 +00:00
Timeline.Init();
2022-01-09 23:35:55 +00:00
for (int i = 0; i < EventCaller.instance.minigames.Count; i++)
{
GameObject GameIcon_ = Instantiate(GridGameSelector.GetChild(0).gameObject, GridGameSelector);
GameIcon_.GetComponent<Image>().sprite = GameIcon(EventCaller.instance.minigames[i].name);
GameIcon_.gameObject.SetActive(true);
2022-01-11 00:17:29 +00:00
GameIcon_.name = EventCaller.instance.minigames[i].displayName;
2022-01-09 23:35:55 +00:00
}
2022-01-11 02:06:13 +00:00
2022-01-23 03:40:53 +00:00
Tooltip.AddTooltip(NewBTN.gameObject, "New <color=#adadad>[Ctrl+N]</color>");
Tooltip.AddTooltip(OpenBTN.gameObject, "Open <color=#adadad>[Ctrl+O]</color>");
Tooltip.AddTooltip(SaveBTN.gameObject, "Save Project <color=#adadad>[Ctrl+S]</color>\nSave Project As <color=#adadad>[Ctrl+Alt+S]</color>");
Tooltip.AddTooltip(UndoBTN.gameObject, "Undo <color=#adadad>[Ctrl+Z]</color>");
Tooltip.AddTooltip(RedoBTN.gameObject, "Redo <color=#adadad>[Ctrl+Y or Ctrl+Shift+Z]</color>");
2022-01-29 21:59:20 +00:00
Tooltip.AddTooltip(MusicSelectBTN.gameObject, "Music Select");
2022-01-29 23:30:29 +00:00
Tooltip.AddTooltip(EditorSettingsBTN.gameObject, "Editor Settings <color=#adadad>[Ctrl+Shift+O]</color>");
Tooltip.AddTooltip(EditorThemeBTN.gameObject, "Editor Theme");
Tooltip.AddTooltip(FullScreenBTN.gameObject, "Preview <color=#adadad>[Tab]</color>");
2022-01-30 23:40:12 +00:00
UpdateEditorStatus(true);
2022-01-09 23:35:55 +00:00
}
2022-02-03 07:28:14 +00:00
public void LateUpdate()
2022-01-14 22:46:14 +00:00
{
if (Input.GetKeyDown(KeyCode.Tab))
{
Fullscreen();
}
if (Input.GetKeyDown(KeyCode.Delete))
{
List<TimelineEventObj> ev = new List<TimelineEventObj>();
for (int i = 0; i < Selections.instance.eventsSelected.Count; i++) ev.Add(Selections.instance.eventsSelected[i]);
CommandManager.instance.Execute(new Commands.Deletion(ev));
}
if (CommandManager.instance.canUndo())
UndoBTN.transform.GetChild(0).GetComponent<Image>().color = "BD8CFF".Hex2RGB();
else
UndoBTN.transform.GetChild(0).GetComponent<Image>().color = Color.gray;
if (CommandManager.instance.canRedo())
RedoBTN.transform.GetChild(0).GetComponent<Image>().color = "FFD800".Hex2RGB();
else
RedoBTN.transform.GetChild(0).GetComponent<Image>().color = Color.gray;
2022-01-23 03:40:53 +00:00
if (Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetKeyDown(KeyCode.Z))
{
if (Input.GetKey(KeyCode.LeftShift))
CommandManager.instance.Redo();
else
CommandManager.instance.Undo();
}
else if (Input.GetKeyDown(KeyCode.Y))
{
CommandManager.instance.Redo();
}
}
if (Timeline.instance.timelineState.selected)
{
2022-02-03 22:20:26 +00:00
if (Input.GetMouseButtonUp(0))
{
2022-02-03 22:20:26 +00:00
List<TimelineEventObj> selectedEvents = Timeline.instance.eventObjs.FindAll(c => c.selected == true && c.eligibleToMove == true);
2022-02-03 22:20:26 +00:00
if (selectedEvents.Count > 0)
{
2022-02-03 22:20:26 +00:00
List<TimelineEventObj> result = new List<TimelineEventObj>();
for (int i = 0; i < selectedEvents.Count; i++)
{
2022-02-03 22:20:26 +00:00
if (selectedEvents[i].isCreating == false)
{
result.Add(selectedEvents[i]);
}
selectedEvents[i].OnUp();
}
2022-02-03 22:20:26 +00:00
CommandManager.instance.Execute(new Commands.Move(result));
}
}
}
2022-01-30 12:03:37 +00:00
if (Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetKeyDown(KeyCode.O))
{
OpenRemix();
}
else if (Input.GetKey(KeyCode.LeftAlt))
{
if (Input.GetKeyDown(KeyCode.S))
{
SaveRemix(true);
}
}
else if (Input.GetKeyDown(KeyCode.S))
{
SaveRemix(false);
}
}
2022-01-30 23:40:12 +00:00
if (lastEditorObjectsCount != GameManager.instance.BeatmapEntities())
{
UpdateEditorStatus(false);
}
lastEditorObjectsCount = GameManager.instance.BeatmapEntities();
2022-02-03 22:20:26 +00:00
if (Application.isEditor)
{
if (Input.GetKeyDown(KeyCode.S))
{
SaveRemix(false);
}
}
2022-01-14 22:46:14 +00:00
}
2022-01-09 23:35:55 +00:00
public static Sprite GameIcon(string name)
{
return Resources.Load<Sprite>($"Sprites/Editor/GameIcons/{name}");
2022-01-03 22:42:43 +00:00
}
2022-01-15 22:52:53 +00:00
2022-01-30 12:03:37 +00:00
#region Dialogs
public void SelectMusic()
{
var extensions = new[]
{
new ExtensionFilter("Music Files", "mp3", "ogg", "wav")
};
StandaloneFileBrowser.OpenFilePanelAsync("Open File", "", extensions, false, async (string[] paths) =>
{
if (paths.Length > 0)
{
Conductor.instance.musicSource.clip = await LoadClip(Path.Combine(paths));
changedMusic = true;
}
}
);
}
private async Task<AudioClip> LoadClip(string path)
{
AudioClip clip = null;
AudioType audioType = AudioType.OGGVORBIS;
// this is a bad solution but i'm lazy
if (path.Substring(path.Length - 3) == "ogg")
audioType = AudioType.OGGVORBIS;
else if (path.Substring(path.Length - 3) == "mp3")
audioType = AudioType.MPEG;
else if (path.Substring(path.Length - 3) == "wav")
audioType = AudioType.WAV;
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(path, audioType))
{
uwr.SendWebRequest();
try
{
while (!uwr.isDone) await Task.Delay(5);
if (uwr.result == UnityWebRequest.Result.ProtocolError) Debug.Log($"{uwr.error}");
else
{
clip = DownloadHandlerAudioClip.GetContent(uwr);
}
}
catch (Exception err)
{
Debug.Log($"{err.Message}, {err.StackTrace}");
}
}
return clip;
}
public void SaveRemix(bool saveAs = true)
{
if (saveAs == true)
{
SaveRemixFilePanel();
}
else
{
if (currentRemixPath == string.Empty)
{
SaveRemixFilePanel();
}
else
{
SaveRemixFile(currentRemixPath);
}
}
}
private void SaveRemixFilePanel()
{
var extensions = new[]
{
new ExtensionFilter("Rhythm Heaven Mania Remix File", "rhmania")
};
StandaloneFileBrowser.SaveFilePanelAsync("Save Remix As", "", "remix_level", extensions, (string path) =>
{
if (path != String.Empty)
{
SaveRemixFile(path);
}
});
}
private void SaveRemixFile(string path)
{
using (FileStream zipFile = File.Open(path, FileMode.Create))
{
2022-02-03 22:20:26 +00:00
using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
2022-01-30 12:03:37 +00:00
{
var levelFile = archive.CreateEntry("remix.json", System.IO.Compression.CompressionLevel.NoCompression);
using (var zipStream = levelFile.Open())
zipStream.Write(Encoding.ASCII.GetBytes(GetJson()), 0, Encoding.ASCII.GetBytes(GetJson()).Length);
if (changedMusic || currentRemixPath != path)
{
byte[] bytes = OggVorbis.VorbisPlugin.GetOggVorbis(Conductor.instance.musicSource.clip, 1);
var musicFile = archive.CreateEntry("song.ogg", System.IO.Compression.CompressionLevel.NoCompression);
using (var zipStream = musicFile.Open())
zipStream.Write(bytes, 0, bytes.Length);
}
}
}
}
public void OpenRemix()
{
var extensions = new[]
{
new ExtensionFilter("Rhythm Heaven Mania Remix File", "rhmania")
};
StandaloneFileBrowser.OpenFilePanelAsync("Open Remix", "", extensions, false, (string[] paths) =>
{
var path = Path.Combine(paths);
if (path != String.Empty)
{
using (FileStream zipFile = File.Open(path, FileMode.Open))
{
using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.Name == "remix.json")
{
using (var stream = entry.Open())
{
byte[] bytes;
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
bytes = ms.ToArray();
string json = Encoding.Default.GetString(bytes);
GameManager.instance.LoadRemix(json);
Timeline.instance.LoadRemix();
}
}
}
else if (entry.Name == "song.ogg")
{
using (var stream = entry.Open())
{
byte[] bytes;
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
bytes = ms.ToArray();
Conductor.instance.musicSource.clip = OggVorbis.VorbisPlugin.ToAudioClip(bytes, "music");
2022-02-03 22:20:26 +00:00
loadedMusic = true;
2022-01-30 12:03:37 +00:00
}
}
}
}
}
}
currentRemixPath = path;
CommandManager.instance.Clear();
}
});
}
#endregion
public void Fullscreen()
{
if (fullscreen == false)
{
MainCanvas.enabled = false;
EditorCamera.enabled = false;
2022-02-03 02:09:50 +00:00
GameCamera.instance.camera.targetTexture = null;
GameCamera.instance.camera.transform.parent.GetChild(1).GetComponent<Camera>().enabled = false;
fullscreen = true;
}
else
{
MainCanvas.enabled = true;
EditorCamera.enabled = true;
2022-02-03 02:09:50 +00:00
GameCamera.instance.camera.targetTexture = ScreenRenderTexture;
GameCamera.instance.camera.transform.parent.GetChild(1).GetComponent<Camera>().enabled = true;
fullscreen = false;
}
}
2022-01-30 23:40:12 +00:00
private void UpdateEditorStatus(bool updateTime)
{
2022-02-03 02:09:50 +00:00
if (discordDuringTesting || !Application.isEditor)
2022-01-30 23:40:12 +00:00
DiscordRPC.DiscordRPC.UpdateActivity("In Editor", $"Objects: {GameManager.instance.Beatmap.entities.Count + GameManager.instance.Beatmap.tempoChanges.Count}", updateTime);
}
2022-01-30 12:03:37 +00:00
public string GetJson()
2022-01-15 22:52:53 +00:00
{
string json = string.Empty;
json = JsonConvert.SerializeObject(GameManager.instance.Beatmap);
2022-01-30 12:03:37 +00:00
return json;
2022-01-15 22:52:53 +00:00
}
public void SetGameEventTitle(string txt)
{
GameEventSelectorTitle.text = txt;
}
2022-01-03 22:42:43 +00:00
}
}