JD2022-TU1/main/tools/legacy/UA_AtlasEditor/Options.cs

396 lines
18 KiB
C#

using System;
using System.Xml;
using System.IO;
using System.Drawing;
using JD.Core.ProjectManaging;
using JD.Perforce;
using Plume.Core.Logging;
namespace UA_AtlasEditor
{
public class Options
{
private string _file;
private ILogger _logger;
public Rectangle _windowRect;
public Rectangle _optionsRect;
public bool _optionsVisible;
public Rectangle _shortcutsRect;
public int _shortcutsColumn1Size = 120;
public int _shortcutsColumn2Size = 300;
public string OpenFileInitialDirectory { get; set; }
public int _openFileFilterIndex = 1;
public int _leftPanelWidth = 100;
public bool _leftPanelVisible = true;
public string _openedAtlas = "";
public History _openedHistory = new History();
public bool _showUnselected = true;
public bool _showPointIndex = false;
public bool _showPointWeight = false;
public bool _showPointDepth = false;
public bool _showPointOffsetX = false;
public bool _showPointOffsetY = false;
public bool _showPointOffsetSinus = false;
public Color _backColor = Color.DarkGray;
public Color _unselColor = Color.Gray;
public Color _selColor = Color.Yellow;
public Color _createColor = Color.YellowGreen;
public bool _bilinearFilter = true;
public AtlasShape.Info _drawMethod = AtlasShape.Info.Index;
public float _editGridSnap = 0.001f;
public bool _editGridSnapEnable = true;
public bool _editSnapEnable = true;
public bool _editCreateListPoint = false;
public float _editWeightMin = -1;
public float _editWeightMax = 1;
public float _editDepthMin = -10;
public float _editDepthMax = 10;
public float _editOffsetSinusMin = -2;
public float _editOffsetSinusMax = 2;
public bool _breakableShow = false;
public int _breakableCellPixelSize = 16;
public bool _saveRectangle = true;
public bool _saveListPoint = true;
public bool _saveMesh2D = true;
public bool _saveRectangleAsListOfPoints = false;
public bool _saveWeight = true;
public bool _saveDepth = true;
public bool _saveColor = true;
public bool _saveOffsetX = true;
public bool _saveOffsetY = true;
public bool _saveOffsetSinus = true;
public bool _load2PointsAsRectangle = true;
public string _texturePackerExe = "";
public string _texturePackerRoot = "x:/data/";
public int _texturePackerSampling = 16;
public int _texturePackerDownscale = 50;
public bool _paintMode = false;
public int _paintRadius = 50;
public float _paintValueSetMin = -10;
public float _paintValueSetMax = 10;
public float _paintValueAdd = 1;
public float _paintOpacity = 0.05f;
public bool _paintFlat = false;
public bool _paintAdd = true;
public bool _showViewAlpha = false;
public void Init(ILogger logger, string file)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_file = file;
Load();
}
public void check()
{
_texturePackerExe = EnginePath.FindExecutable("TexturePacker.exe");
if (_paintOpacity > 1) _paintOpacity = 1;
if (_paintOpacity < 0) _paintOpacity = 0;
if ( (_editGridSnap <= 0.0001f) || (_editGridSnap >= 1)) _editGridSnap = 0.001f;
}
private void Load()
{
if (!File.Exists(_file))
{
_logger.Info("config file " + _file + " not found !");
return;
}
// open xml
XmlDocument xml;
xml = new XmlDocument();
try
{
xml.Load(_file);
_windowRect = LoadRectangle(xml, "/Window", "Rect", new Rectangle(0, 0, 0, 0));
_optionsRect = LoadRectangle(xml, "/Window/Options", "Rect", new Rectangle(0, 0, 0, 0));
_optionsVisible = LoadBool(xml, "/Window/Options", "Visible", false);
_shortcutsRect = LoadRectangle(xml, "/Window/Shortcut", "Rect", new Rectangle(0, 0, 0, 0));
_shortcutsColumn1Size = LoadInt(xml, "/Window/Shortcut", "SizeColumn1", _shortcutsColumn1Size);
_shortcutsColumn2Size = LoadInt(xml, "/Window/Shortcut", "SizeColumn2", _shortcutsColumn2Size);
OpenFileInitialDirectory = LoadString(xml, "/OpenFile", "InitialDirectory", P4Manager.Instance.PerforceService.Connection?.PerforceClient);
_openFileFilterIndex = LoadInt(xml, "/OpenFile", "FilterIndex", 1);
_leftPanelWidth = LoadInt(xml, "/Window/LeftPanel", "Width", 200);
_leftPanelVisible = LoadBool(xml, "/Window/LeftPanel", "Visible", true);
_showUnselected = LoadBool(xml, "/Display", "ShowUnselected", _showUnselected);
_showPointIndex = LoadBool(xml, "/Display", "ShowPointIndex", _showPointIndex);
_showPointWeight = LoadBool(xml, "/Display", "ShowPointWeight", _showPointWeight);
_showPointDepth = LoadBool(xml, "/Display", "ShowPointDepth", _showPointDepth);
_backColor = LoadColor(xml, "/Display", "BackColor", Color.DarkGray);
_unselColor = LoadColor(xml, "/Display", "UnselColor", Color.Gray);
_selColor = LoadColor(xml, "/Display", "SelColor", Color.Yellow);
_createColor = LoadColor(xml, "/Display", "CreateColor", Color.YellowGreen);
_bilinearFilter = LoadBool(xml, "/Display", "BilinearFilter", true);
_editSnapEnable = LoadBool(xml, "/Edition/Snap", "Enable", true);
_editGridSnapEnable = LoadBool(xml, "/Edition/SnapGrid", "Enable", true);
_editGridSnap = LoadFloat(xml, "/Edition/SnapGrid", "Value", 0.001f);
_editCreateListPoint = LoadBool(xml, "/Edition/Create", "ListPoint", false);
_editWeightMin = LoadFloat(xml, "/Edition/Weight", "Min", _editWeightMin);
_editWeightMax = LoadFloat(xml, "/Edition/Weight", "Max", _editWeightMax);
_editDepthMin = LoadFloat(xml, "/Edition/Depth", "Min", _editDepthMin);
_editDepthMax = LoadFloat(xml, "/Edition/Depth", "Max", _editDepthMax);
_breakableShow = LoadBool(xml, "/Edition/Breakable", "Show", _breakableShow);
_breakableCellPixelSize = LoadInt(xml, "/Edition/Breakable", "RefSize", _breakableCellPixelSize);
_saveRectangle = LoadBool(xml, "/Save/Atl", "Rectangle", true);
_saveListPoint = LoadBool(xml, "/Save/Atl", "ListPoint", true);
_saveMesh2D = LoadBool(xml, "/Save/Atl", "Mesh2D", true);
_saveRectangleAsListOfPoints = LoadBool(xml, "/Save", "RectangleAsListPoint", false);
_saveWeight = LoadBool(xml, "/Save", "Weight", true);
_saveDepth = LoadBool(xml, "/Save", "Depth", true);
_saveColor = LoadBool(xml, "/Save", "Color", true);
_load2PointsAsRectangle = LoadBool(xml, "/Load", "TwoPointAsRectangle", _load2PointsAsRectangle);
_openedAtlas = LoadString(xml, "/Opened", "Atlas", "");
int historyNum = LoadInt(xml, "/Opened", "History", 0);
for (int i = 0; i < historyNum; i++)
{
string historyKey = "/Opened/History" + i.ToString();
string historyFile = LoadString(xml, historyKey, "File", "");
string historyDate = LoadString(xml, historyKey, "Date", "");
_openedHistory.add(historyFile, Convert.ToDateTime(historyDate));
}
_texturePackerExe = LoadString(xml, "/TexturePacker", "Exe", "");
_texturePackerRoot = LoadString(xml, "/TexturePacker", "Root", P4Manager.Instance.PerforceService.Connection?.PerforceClient);
_texturePackerSampling = LoadInt(xml, "/TexturePacker", "Sampling", _texturePackerSampling);
_texturePackerDownscale = LoadInt(xml, "/TexturePacker", "Downscale", _texturePackerDownscale);
_paintMode = LoadBool(xml, "/PaintMode", "Enable", _paintMode);
_paintRadius = LoadInt(xml, "/PaintMode", "Radius", _paintRadius);
_paintValueSetMin = LoadFloat(xml, "/PaintMode", "SetMinValue", _paintValueSetMin);
_paintValueSetMax = LoadFloat(xml, "/PaintMode", "SetMaxValue", _paintValueSetMax);
_paintValueAdd = LoadFloat(xml, "/PaintMode", "AddValue", _paintValueAdd);
_paintOpacity = LoadFloat(xml, "/PaintMode", "Opacity", _paintOpacity);
_paintFlat = LoadBool(xml, "/PaintMode", "Flat", _paintFlat);
_paintAdd = LoadBool(xml, "/PaintMode", "Add", _paintAdd);
check();
}
catch(Exception exception)
{
_logger.Error("error reading config file " + _file, exception);
}
}
public void save()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using (XmlWriter xml = XmlWriter.Create(_file, settings))
{
xml.WriteStartDocument();
xml.WriteStartElement("Options");
// windows parameters
xml.WriteStartElement("Window");
xml.WriteAttributeString("Rect", _windowRect.ToString());
xml.WriteStartElement("Options");
xml.WriteAttributeString("Rect", _optionsRect.ToString());
xml.WriteAttributeString("Visible", _optionsVisible.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Shortcut");
xml.WriteAttributeString("Rect", _shortcutsRect.ToString());
xml.WriteAttributeString("SizeColumn1", _shortcutsColumn1Size.ToString());
xml.WriteAttributeString("SizeColumn2", _shortcutsColumn2Size.ToString());
xml.WriteEndElement();
xml.WriteStartElement("LeftPanel");
xml.WriteAttributeString("Width", _leftPanelWidth.ToString());
xml.WriteAttributeString("Visible", _leftPanelVisible.ToString());
xml.WriteEndElement();
xml.WriteEndElement();
xml.WriteStartElement("OpenFile");
xml.WriteAttributeString("InitialDirectory", OpenFileInitialDirectory);
xml.WriteAttributeString("FilterIndex", _openFileFilterIndex.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Opened");
xml.WriteAttributeString("Atlas", _openedAtlas);
xml.WriteAttributeString("History", _openedHistory.getCount().ToString());
for (int i = 0; i < _openedHistory.getCount(); i++)
{
xml.WriteStartElement("History"+i.ToString());
xml.WriteAttributeString("File", _openedHistory.getPath(i));
xml.WriteAttributeString("Date", _openedHistory.getDate(i).ToString());
xml.WriteEndElement();
}
xml.WriteEndElement();
xml.WriteStartElement("Display");
xml.WriteAttributeString("ShowUnselected", _showUnselected.ToString());
xml.WriteAttributeString("ShowPointIndex", _showPointIndex.ToString());
xml.WriteAttributeString("ShowPointWeight", _showPointWeight.ToString());
xml.WriteAttributeString("ShowPointDepth", _showPointDepth.ToString());
xml.WriteAttributeString("BackColor", _backColor.ToArgb().ToString());
xml.WriteAttributeString("UnselColor", _unselColor.ToArgb().ToString());
xml.WriteAttributeString("SelColor", _selColor.ToArgb().ToString());
xml.WriteAttributeString("CreateColor", _createColor.ToArgb().ToString());
xml.WriteAttributeString("BilinearFilter", _bilinearFilter.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Edition");
xml.WriteStartElement("Snap");
xml.WriteAttributeString("Enable", _editSnapEnable.ToString());
xml.WriteEndElement();
xml.WriteStartElement("SnapGrid");
xml.WriteAttributeString("Enable", _editGridSnapEnable.ToString());
xml.WriteAttributeString("Value", _editGridSnap.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Create");
xml.WriteAttributeString("ListPoint", _editCreateListPoint.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Weight");
xml.WriteAttributeString("Min", _editWeightMin.ToString());
xml.WriteAttributeString("Max", _editWeightMax.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Depth");
xml.WriteAttributeString("Min", _editDepthMin.ToString());
xml.WriteAttributeString("Max", _editDepthMax.ToString());
xml.WriteEndElement();
xml.WriteStartElement("Breakable");
xml.WriteAttributeString("Show", _breakableShow.ToString());
xml.WriteAttributeString("RefSize", _breakableCellPixelSize.ToString());
xml.WriteEndElement();
xml.WriteEndElement();
xml.WriteStartElement("Save");
xml.WriteAttributeString("RectangleAsListPoint", _saveRectangleAsListOfPoints.ToString());
xml.WriteAttributeString("Weight", _saveWeight.ToString());
xml.WriteAttributeString("Depth", _saveDepth.ToString());
xml.WriteStartElement("Atl");
xml.WriteAttributeString("Rectangle", _saveRectangle.ToString());
xml.WriteAttributeString("ListPoint", _saveListPoint.ToString());
xml.WriteAttributeString("Mesh2D", _saveMesh2D.ToString());
xml.WriteEndElement();
xml.WriteEndElement();
xml.WriteStartElement("Load");
xml.WriteAttributeString("TwoPointAsRectangle", _load2PointsAsRectangle.ToString());
xml.WriteEndElement();
xml.WriteStartElement("TexturePacker");
xml.WriteAttributeString("Exe", _texturePackerExe.ToString());
xml.WriteAttributeString("Root", _texturePackerRoot.ToString());
xml.WriteAttributeString("Sampling", _texturePackerSampling.ToString());
xml.WriteAttributeString("Downscale", _texturePackerDownscale.ToString());
xml.WriteEndElement();
xml.WriteStartElement("PaintMode");
xml.WriteAttributeString("Enable", _paintMode.ToString());
xml.WriteAttributeString("Radius", _paintRadius.ToString());
xml.WriteAttributeString("SetMinValue", _paintValueSetMin.ToString());
xml.WriteAttributeString("SetMaxValue", _paintValueSetMax.ToString());
xml.WriteAttributeString("AddValue", _paintValueAdd.ToString());
xml.WriteAttributeString("Opacity", _paintOpacity.ToString());
xml.WriteAttributeString("Flat", _paintFlat.ToString());
xml.WriteAttributeString("Add", _paintAdd.ToString());
xml.WriteEndElement();
xml.WriteEndElement();
}
}
public string LoadString(XmlDocument xml, string xmlPath, string attribute, string defaultValue)
{
XmlNode node;
XmlAttribute atr;
node = xml.SelectSingleNode("/Options" + xmlPath);
if (node == null) return defaultValue;
atr = node.Attributes[attribute];
if (atr == null) return defaultValue;
return atr.Value;
}
public bool LoadBool(XmlDocument xml, string xmlPath, string attribute, bool defaultValue)
{
return Convert.ToBoolean(LoadString(xml, xmlPath, attribute, defaultValue.ToString()));
}
public int LoadInt(XmlDocument xml, string xmlPath, string attribute, int defaultValue)
{
return Convert.ToInt32(LoadString(xml, xmlPath, attribute, defaultValue.ToString()));
}
public float LoadFloat(XmlDocument xml, string xmlPath, string attribute, float defaultValue)
{
return Convert.ToSingle(LoadString(xml, xmlPath, attribute, defaultValue.ToString()));
}
public Color LoadColor(XmlDocument xml, string xmlPath, string attribute, Color defaultValue)
{
int intColor = LoadInt(xml, xmlPath, attribute, defaultValue.ToArgb());
return Color.FromArgb(intColor);
}
public Rectangle LoadRectangle(XmlDocument xml, string xmlPath, string attribute, Rectangle defaultValue)
{
string val = LoadString(xml, xmlPath, attribute, null);
if (val == null)
return defaultValue;
int[] iVal = new int[4] { 0, 0, 0, 0 };
int i = 0, c = 0, sign = 1;
while (c < val.Length)
{
while (val[c] == ' ') c++;
if (val[c] == ',')
{
iVal[i] *= sign;
sign = 1;
i++;
if (i == 4) break;
}
else if (val[c] == '-' && iVal[i] == 0)
sign = -1;
else if (Char.IsDigit(val[c]))
iVal[i] = iVal[i] * 10 + (val[c] - '0');
c++;
}
return Rectangle.FromLTRB(iVal[0], iVal[1], iVal[0] + iVal[2], iVal[1] + iVal[3]);
}
}
}