1
0
mirror of synced 2024-12-11 23:36:01 +01:00
Switch-Toolbox/File_Format_Library/GUI/BFLYT/UserDataEditor.cs

190 lines
6.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Syroot.NintenTools.Bfres;
namespace LayoutBXLYT
{
public partial class UserDataEditor : UserControl
{
public UserDataEditor()
{
InitializeComponent();
listViewCustom1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
}
2019-10-13 23:02:25 +02:00
BasePane activePane;
UserData ActiveUserData;
UserDataEntry SelectedEntry;
public void Reset()
{
2019-10-13 23:02:25 +02:00
ActiveUserData = new UserData();
SelectedEntry = null;
listViewCustom1.Items.Clear();
}
2019-10-13 23:02:25 +02:00
public void LoadUserData(BasePane pane, UserData UserData)
{
2019-10-13 23:02:25 +02:00
activePane = pane;
if (UserData != null)
{
listViewCustom1.Items.Clear();
2019-10-13 23:02:25 +02:00
ActiveUserData = UserData;
foreach (var item in ActiveUserData.Entries)
LoadUserData(item);
}
}
private void LoadUserData(UserDataEntry item)
{
ListViewItem listItem = new ListViewItem();
listItem.Text = item.Name;
listItem.SubItems.Add(item.Type.ToString());
string value = "";
switch (item.Type)
{
case UserDataType.String:
if (item.GetString() != null)
{
value += $" {item.GetString()}";
}
break;
case UserDataType.Float:
if (item.GetFloats() != null)
{
foreach (var val in item.GetFloats())
value += $" {val}";
}
break;
case UserDataType.Int:
if (item.GetInts() != null)
{
foreach (var val in item.GetInts())
value += $" {val}";
}
break;
}
listItem.SubItems.Add(value);
listViewCustom1.Items.Add(listItem);
}
private void listViewCustom1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listViewCustom1.SelectedItems.Count > 0)
{
btnScrolDown.Enabled = true;
btnScrollUp.Enabled = true;
btnEdit.Enabled = true;
btnRemove.Enabled = true;
2019-10-13 23:02:25 +02:00
SelectedEntry = ActiveUserData.Entries[listViewCustom1.SelectedIndices[0]];
}
else
{
SelectedEntry = null;
btnScrolDown.Enabled = false;
btnScrollUp.Enabled = false;
btnEdit.Enabled = false;
btnRemove.Enabled = false;
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
EditData();
}
private void btnAdd_Click(object sender, EventArgs e)
{
2019-10-13 23:02:25 +02:00
if (activePane == null) return;
UserDataEntry userDataNew = ActiveUserData.CreateUserData();
userDataNew.SetValue(new int[0]);
SelectedEntry = userDataNew;
bool IsEdited = EditData();
if (IsEdited)
{
2020-09-12 22:50:05 +02:00
ActiveUserData.Edited = true;
2019-10-13 23:02:25 +02:00
ActiveUserData.Entries.Add(userDataNew);
LoadUserData(userDataNew);
}
}
private bool EditData()
{
if (SelectedEntry != null)
{
UserDataParser parser = new UserDataParser();
parser.UserDataName = SelectedEntry.Name;
2019-09-04 01:23:04 +02:00
parser.Type = SelectedEntry.Type;
switch (SelectedEntry.Type)
{
case UserDataType.String:
if (SelectedEntry.GetString() != null)
parser.LoadValues(SelectedEntry.GetString());
break;
case UserDataType.Float:
if (SelectedEntry.GetString() != null)
parser.LoadValues(SelectedEntry.GetFloats());
break;
case UserDataType.Int:
if (SelectedEntry.GetInts() != null)
parser.LoadValues(SelectedEntry.GetInts());
break;
}
if (parser.ShowDialog() == DialogResult.OK)
{
SelectedEntry.Name = parser.UserDataName;
2019-09-04 01:23:04 +02:00
if (parser.Type == UserDataType.Float)
SelectedEntry.SetValue(parser.GetFloats());
2019-09-04 01:23:04 +02:00
if (parser.Type == UserDataType.Int)
SelectedEntry.SetValue(parser.GetInts());
2019-09-04 01:23:04 +02:00
if (parser.Type == UserDataType.String)
SelectedEntry.SetValue(parser.GetStringASCII());
2020-09-12 22:50:05 +02:00
if (ActiveUserData == null) {
2019-10-13 23:02:25 +02:00
ActiveUserData = activePane.CreateUserData();
2020-09-12 22:50:05 +02:00
}
2019-10-13 23:02:25 +02:00
2020-09-12 22:50:05 +02:00
ActiveUserData.Edited = true;
2019-10-13 23:02:25 +02:00
LoadUserData(activePane, ActiveUserData);
return true;
}
}
return false;
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (listViewCustom1.SelectedIndices.Count > 0)
{
int index = listViewCustom1.SelectedIndices[0];
listViewCustom1.Items.RemoveAt(index);
2020-09-12 22:50:05 +02:00
if (ActiveUserData != null) {
2019-10-13 23:02:25 +02:00
ActiveUserData.Entries.RemoveAt(index);
2020-09-12 22:50:05 +02:00
ActiveUserData.Edited = true;
}
}
}
private void listViewCustom1_DoubleClick(object sender, EventArgs e)
{
EditData();
}
}
}