Files
Studio/Assets/Scripts/Studio/UI/Panel/Panel_AppSetting.cs
2025-07-01 15:53:55 +09:00

218 lines
7.7 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XRLib.UI;
using System.IO;
using TMPro;
using TriLibCore.SFB;
using Studio.Util;
namespace Studio.UI
{
public enum AnimationStyle
{
Static, FadeIn, FadeOut
}
public enum ProgressBarStyle
{
None, Bar, Spinner, Skeleton
}
public enum DefultLanauge
{
Korean, English, Chinese, Japanese
}
public class Panel_AppSetting : PanelBase
{
private Button Button_ImageSelected;
private Image Image_Selected;
private TMP_Dropdown Dropdown_AnimationStyle;
private TMP_Dropdown Dropdown_ProgressBarStyle;
private Toggle Toggle_AutoStart;
private TMP_Dropdown Dropdown_DefaultLangauge;
private TMP_InputField InputField_LanguagePack;
private Button Button_LanguagePack;
private float ratio;
public Action onClose;
public Button Button_Close;
public void Active(bool isActive)
{
gameObject.SetActive(isActive);
if (!isActive)
{
onClose?.Invoke();
}
}
public override void AfterAwake()
{
Button_ImageSelected.onClick.AddListener(OnClickImageSelectedButton);
Toggle_AutoStart.onValueChanged.AddListener(OnAutoStartValuedChanged);
Button_LanguagePack.onClick.AddListener(OnClickLanguagePackButton);
Button_Close.onClick.AddListener(() => Active(false));
SetAnimationStyleDropdown();
SetProgressBarStyleDropdown();
SetDefaultLangaugeDropdown();
}
private void SetAnimationStyleDropdown()
{
Dropdown_AnimationStyle.ClearOptions();
var types = Enum.GetNames(typeof(AnimationStyle));
List<string> options = new List<string>();
for (int i = 0; i < types.Length; i++)
{
options.Add(types[i]);
}
Dropdown_AnimationStyle.AddOptions(options);
}
private void SetProgressBarStyleDropdown()
{
Dropdown_ProgressBarStyle.ClearOptions();
var types = Enum.GetNames(typeof(ProgressBarStyle));
List<string> options = new List<string>();
for (int i = 0; i < types.Length; i++)
{
options.Add(types[i]);
}
Dropdown_ProgressBarStyle.AddOptions(options);
}
private void SetDefaultLangaugeDropdown()
{
Dropdown_DefaultLangauge.ClearOptions();
var types = Enum.GetNames(typeof(DefultLanauge));
List<string> options = new List<string>();
for (int i = 0; i < types.Length; i++)
{
options.Add(types[i]);
}
Dropdown_DefaultLangauge.AddOptions(options);
}
private void OnClickImageSelectedButton()
{
var extensions = new[] { new ExtensionFilter("Image Files", "png", "jpg", "jpeg") };
StandaloneFileBrowser.OpenFilePanelAsync("이미지 열기", "", extensions, false, LoadImage);
}
private void OnAutoStartValuedChanged(bool isOn)
{
}
private void OnClickLanguagePackButton()
{
var packItem = StandaloneFileBrowser.OpenFilePanel("언어 팩 열기", "", "", false);
if (packItem != null && packItem.Count > 0)
{
var item = packItem[0];
if (string.IsNullOrEmpty(item.Name))
{
return;
}
InputField_LanguagePack.text = packItem[0].Name;
}
}
void LoadImage(IList<ItemWithStream> items)
{
if (items != null && items.Count > 0)
{
var item = items[0];
if (string.IsNullOrEmpty(item.Name))
{
return;
}
using (var memoryStream = new MemoryStream())
{
using (var inputStream = item.OpenStream())
{
inputStream.CopyTo(memoryStream);
}
byte[] imageData = memoryStream.ToArray();
Texture2D texture = new Texture2D(2, 2);
if (texture.LoadImage(imageData))
{
Image_Selected.enabled = true;
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
Image_Selected.sprite = sprite;
var text = Button_ImageSelected.GetComponentInChildren<TMP_Text>();
text.enabled = false;
ratio = texture.height / (float)texture.width;
}
else
{
Image_Selected.enabled = false;
var text = Button_ImageSelected.GetComponentInChildren<TMP_Text>();
text.enabled = true;
Debug.LogError("이미지 로드 실패");
}
}
}
else
{
Debug.LogWarning("이미지를 선택하지 않았습니다.");
}
}
public AppSetting GetAppSetting()
{
var appSetting = new AppSetting();
if (Image_Selected.sprite != null)
{
var bytes = Image_Selected.sprite.texture.EncodeToPNG();
appSetting.splashImageByte = Convert.ToBase64String(bytes);
appSetting.ratio = ratio;
}
appSetting.animationStyle = Dropdown_AnimationStyle.options[Dropdown_AnimationStyle.value].text;
appSetting.progressBarStyle = Dropdown_ProgressBarStyle.options[Dropdown_ProgressBarStyle.value].text;
appSetting.autoStart = Toggle_AutoStart.isOn;
appSetting.defaultLanauge = Dropdown_DefaultLangauge.options[Dropdown_DefaultLangauge.value].text;
appSetting.languagePackPath = InputField_LanguagePack.text;
return appSetting;
}
public void SetAppSetting(AppSetting appSetting)
{
if (appSetting == null)
{
Image_Selected.sprite = null;
Dropdown_AnimationStyle.value = 0;
Dropdown_ProgressBarStyle.value = 0;
Toggle_AutoStart.isOn = true;
Dropdown_DefaultLangauge.value = 0;
InputField_LanguagePack.text = string.Empty;
return;
}
if (!string.IsNullOrEmpty(appSetting.splashImageByte))
{
var bytes = Convert.FromBase64String(appSetting.splashImageByte);
Texture2D texture = new Texture2D(2, 2);
if (texture.LoadImage(bytes))
{
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
Image_Selected.sprite = sprite;
}
}
Dropdown_AnimationStyle.value = Dropdown_AnimationStyle.options.FindIndex(option => option.text == appSetting.animationStyle);
Dropdown_ProgressBarStyle.value = Dropdown_ProgressBarStyle.options.FindIndex(option => option.text == appSetting.progressBarStyle);
Toggle_AutoStart.isOn = appSetting.autoStart;
Dropdown_DefaultLangauge.value = Dropdown_DefaultLangauge.options.FindIndex(option => option.text == appSetting.defaultLanauge);
InputField_LanguagePack.text = appSetting.languagePackPath;
}
}
}