Update SkinANIM and changed to struct and add string parsing

This commit is contained in:
miku-666
2022-08-03 02:41:20 +02:00
parent 3cb60810d9
commit fd88eeffc6
2 changed files with 21 additions and 40 deletions

View File

@@ -2,9 +2,7 @@
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PckStudio.Classes.Utils
{
@@ -52,35 +50,14 @@ namespace PckStudio.Classes.Utils
DINNERBONE = 1 << 31,
}
internal class SkinANIM
internal struct SkinANIM
{
eANIM_EFFECTS _ANIM;
public bool isValid;
public SkinANIM()
{
_ANIM = 0;
}
eANIM_EFFECTS _ANIM = 0;
static readonly Regex animRegex = new Regex(@"^0x[0-9a-f]{1,8}\b", RegexOptions.IgnoreCase);
public SkinANIM(string anim)
{
// Port of my ANIM Generator found at https://mattnl.com/lce/anim-generator - MattNL
if (anim.StartsWith("0x")) anim = anim.Substring(2);
isValid = anim.Length <= 8 && Regex.IsMatch(anim, @"\A\b[0-9a-fA-F]+\b\Z");
if (isValid)
{
anim = anim.PadLeft(8, '0');
string bits = String.Join("", anim.Select(
b => Convert.ToString(Convert.ToInt32(b.ToString(), 16), 2).PadLeft(4, '0')
)
);
int current_bit = 31;
foreach (char bit in bits)
{
SetANIMFlag((eANIM_EFFECTS)(1 << current_bit), bit == '1');
current_bit--;
}
}
_ANIM = Parse(anim);
}
public SkinANIM(eANIM_EFFECTS anim)
@@ -88,17 +65,23 @@ namespace PckStudio.Classes.Utils
_ANIM = anim;
}
public override string ToString()
{
// Thanks miku :D - MattNL
return "0x" + ((int)_ANIM).ToString("x08");
}
public override string ToString() => "0x" + _ANIM.ToString("x");
public static bool IsValidANIM(string anim) => animRegex.IsMatch(anim);
public static eANIM_EFFECTS Parse(string anim)
=> IsValidANIM(anim)
? (eANIM_EFFECTS)Convert.ToInt32(anim, 16)
: 0;
public void SetANIM(int anim) => SetANIM((eANIM_EFFECTS)anim);
public void SetANIM(eANIM_EFFECTS anim) => _ANIM = anim;
/// <summary>
/// Sets the desired flag in the bitfield
/// </summary>
/// <param name="flag">ANIM Flag to be set</param>
/// <param name="state">wether to enable the flag</param>
/// <param name="flag">ANIM Flag to set</param>
/// <param name="state">Wether to enable the flag</param>
public void SetANIMFlag(eANIM_EFFECTS flag, bool state)
{
if (state) _ANIM |= flag;
@@ -112,7 +95,7 @@ namespace PckStudio.Classes.Utils
/// <returns>Bool wether its set or not</returns>
public bool GetANIMFlag(eANIM_EFFECTS flag)
{
return ((int)_ANIM & (int)flag) != 0;
return (_ANIM & flag) != 0;
}
}
}

View File

@@ -64,8 +64,8 @@ namespace PckStudio.Forms.Utilities.Skins
public ANIMEditor(string ANIM)
{
InitializeComponent();
if (!SkinANIM.IsValidANIM(ANIM)) Close();
anim = new SkinANIM(ANIM);
if (!anim.isValid) Close();
bobbingCheckBox.CheckedChanged += (sender, EventArgs) => { flagChanged(sender, EventArgs, eANIM_EFFECTS.HEAD_BOBBING_DISABLED); };
bodyCheckBox.CheckedChanged += (sender, EventArgs) => { flagChanged(sender, EventArgs, eANIM_EFFECTS.BODY_DISABLED); };
@@ -147,11 +147,10 @@ namespace PckStudio.Forms.Utilities.Skins
private void importButton_Click(object sender, EventArgs e)
{
SkinANIM check = new SkinANIM(":3");
string new_value = "";
bool first = true;
while (!check.isValid)
while (!SkinANIM.IsValidANIM(new_value))
{
if (!first) MessageBox.Show("The following value \"" + new_value + "\" is not valid. Please try again.");
RenamePrompt diag = new RenamePrompt(new_value);
@@ -160,12 +159,11 @@ namespace PckStudio.Forms.Utilities.Skins
if (diag.ShowDialog() == DialogResult.OK)
{
new_value = diag.NewText;
check = new SkinANIM(new_value);
}
else return;
first = false;
}
anim = check;
anim = new SkinANIM(new_value);
processCheckBoxes();
}