From 86dcb439ee65b96a2e8b6697da32d65c21f348c0 Mon Sep 17 00:00:00 2001
From: Li
Date: Tue, 25 Apr 2023 14:22:34 +1200
Subject: [PATCH] Fix textboxes
---
.gitignore | 1 +
ChovySign-GUI/ChovySign-GUI.csproj | 8 +-
ChovySign-GUI/Global/FilteredTextBox.axaml | 8 +
ChovySign-GUI/Global/FilteredTextBox.axaml.cs | 149 ++++++++++++++++++
ChovySign-GUI/Global/KeySelector.axaml | 7 +-
ChovySign-GUI/Global/KeySelector.axaml.cs | 15 +-
ChovySign-GUI/Global/LabeledTextBox.axaml | 5 +-
ChovySign-GUI/Global/LabeledTextBox.axaml.cs | 100 +++---------
.../Global/KeySelector/ActRifMethodGUI.axaml | 4 +-
ChovySign-GUI/Ps1/Ps1Tab.axaml | 2 +-
LibChovy/VersionKey/KeysTxtMethod.cs | 2 +-
11 files changed, 203 insertions(+), 98 deletions(-)
create mode 100644 ChovySign-GUI/Global/FilteredTextBox.axaml
create mode 100644 ChovySign-GUI/Global/FilteredTextBox.axaml.cs
diff --git a/.gitignore b/.gitignore
index 17e21f7..633f314 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
*.7z
*.pdn
*.user
+*launchSettings.json
*Thumbs.db
PbpResign/bin/*
diff --git a/ChovySign-GUI/ChovySign-GUI.csproj b/ChovySign-GUI/ChovySign-GUI.csproj
index 52df410..3454d34 100644
--- a/ChovySign-GUI/ChovySign-GUI.csproj
+++ b/ChovySign-GUI/ChovySign-GUI.csproj
@@ -42,11 +42,11 @@
-
-
+
+
-
-
+
+
diff --git a/ChovySign-GUI/Global/FilteredTextBox.axaml b/ChovySign-GUI/Global/FilteredTextBox.axaml
new file mode 100644
index 0000000..9219508
--- /dev/null
+++ b/ChovySign-GUI/Global/FilteredTextBox.axaml
@@ -0,0 +1,8 @@
+
+
+
diff --git a/ChovySign-GUI/Global/FilteredTextBox.axaml.cs b/ChovySign-GUI/Global/FilteredTextBox.axaml.cs
new file mode 100644
index 0000000..ebe7183
--- /dev/null
+++ b/ChovySign-GUI/Global/FilteredTextBox.axaml.cs
@@ -0,0 +1,149 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Input;
+using Avalonia.Interactivity;
+using System;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ChovySign_GUI.Global
+{
+ public partial class FilteredTextBox : UserControl
+ {
+
+ private string? allowedChars = null;
+
+ public event EventHandler? TextChanged;
+ protected virtual void OnTextChanged(EventArgs e)
+ {
+ if (TextChanged is not null)
+ TextChanged(this, e);
+ }
+
+ public int MaxLength
+ {
+ get
+ {
+ return this.txtBox.MaxLength;
+ }
+ set
+ {
+ this.txtBox.MaxLength = value;
+ }
+ }
+
+ public bool Password
+ {
+ get
+ {
+ return this.txtBox.PasswordChar == default(char);
+ }
+ set
+ {
+ if (value) this.txtBox.PasswordChar = 'X';
+ else this.txtBox.PasswordChar = default(char);
+ }
+ }
+
+ public string Watermark
+ {
+ get
+ {
+ return this.txtBox.Watermark;
+ }
+ set
+ {
+ this.txtBox.Watermark = value;
+ }
+ }
+ public string? AllowedChars
+ {
+ get
+ {
+ if (allowedChars is null) return "";
+ else return allowedChars;
+ }
+ set
+ {
+ allowedChars = value;
+ }
+ }
+ public string Text
+ {
+ get
+ {
+ if (this.txtBox.Text is null) return "";
+ return this.txtBox.Text;
+ }
+ set
+ {
+ this.txtBox.Text = value;
+ }
+ }
+ public FilteredTextBox()
+ {
+ InitializeComponent();
+ this.txtBox.PastingFromClipboard += onPaste;
+ this.txtBox.AddHandler(TextInputEvent, onTxtInput, RoutingStrategies.Tunnel);
+ }
+
+
+ private string filter(string original)
+ {
+ if (allowedChars is not null)
+ {
+ StringBuilder str = new StringBuilder();
+
+ for (int i = 0; i < original.Length; i++)
+ {
+ if (allowedChars.Contains(original[i]))
+ str.Append(original[i]);
+ }
+
+ return str.ToString();
+ }
+ return original;
+ }
+ private async Task setClipboardText(string text)
+ {
+ if (Application.Current is null) return false;
+ if (Application.Current.Clipboard is null) return false;
+ await Application.Current.Clipboard.SetTextAsync(text);
+ return true;
+ }
+ private async Task getClipboardText()
+ {
+ if (Application.Current is null) return "";
+ if (Application.Current.Clipboard is null) return "";
+ string? clipboard = await Application.Current.Clipboard.GetTextAsync();
+ if (clipboard is null) return "";
+ return clipboard;
+ }
+
+ private async void onPaste(object? sender, RoutedEventArgs e)
+ {
+ TextBox? txt = sender as TextBox;
+ if (txt is null) return;
+
+ string clipboard = getClipboardText().Result;
+ clipboard = filter(clipboard);
+ _ = setClipboardText(clipboard).Result;
+
+ // annoyingly, the text being pasted isnt actually in the textbox yet
+ // and it wont trigger a textInput event when pasting; t-this really is the best can do
+ await Task.Delay(100);
+ OnTextChanged(new EventArgs());
+ }
+
+ private void onTxtInput(object? sender, TextInputEventArgs e)
+ {
+ string? newTxt = e.Text;
+ if (newTxt is null) newTxt = "";
+
+ newTxt = filter(newTxt);
+ e.Text = newTxt;
+
+ OnTextChanged(new EventArgs());
+ }
+ }
+}
diff --git a/ChovySign-GUI/Global/KeySelector.axaml b/ChovySign-GUI/Global/KeySelector.axaml
index e695543..5308589 100644
--- a/ChovySign-GUI/Global/KeySelector.axaml
+++ b/ChovySign-GUI/Global/KeySelector.axaml
@@ -2,7 +2,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="40"
+ xmlns:Global="clr-namespace:ChovySign_GUI.Global"
+ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="40"
x:Class="ChovySign_GUI.Global.KeySelector">
@@ -18,7 +19,7 @@
-
+
@@ -26,7 +27,7 @@
-
+
diff --git a/ChovySign-GUI/Global/KeySelector.axaml.cs b/ChovySign-GUI/Global/KeySelector.axaml.cs
index 7047ae3..1931c1b 100644
--- a/ChovySign-GUI/Global/KeySelector.axaml.cs
+++ b/ChovySign-GUI/Global/KeySelector.axaml.cs
@@ -60,7 +60,6 @@ namespace ChovySign_GUI.Global
if (zRif.Text is null) return false;
if (zRif.Text.Length <= 0) return false;
- byte[] key = MathUtil.StringToByteArray(vKey.Text);
byte[] rif = new NpDrmRif(zRif.Text).Rif;
if (rif.Length <= 0) return false;
return (VersionKey is not null && Rif is not null);
@@ -245,15 +244,13 @@ namespace ChovySign_GUI.Global
reloadCfg();
lastValid = IsValid;
- zRif.KeyDown += onZrifKeyDown;
- zRif.KeyUp += onZrifKeyDown;
- vKey.KeyDown += onVkeyKeyDown;
- vKey.KeyUp += onVkeyKeyDown;
+ zRif.TextChanged += onZrifChanged;
+ vKey.TextChanged += onVkeyChanged;
}
- private void onVkeyKeyDown(object? sender, Avalonia.Input.KeyEventArgs e)
+ private void onVkeyChanged(object? sender, EventArgs e)
{
- TextBox? txt = sender as TextBox;
+ FilteredTextBox? txt = sender as FilteredTextBox;
if (txt is null) return;
if (lastValid != IsValid)
@@ -269,9 +266,9 @@ namespace ChovySign_GUI.Global
catch { };
}
- private void onZrifKeyDown(object? sender, KeyEventArgs e)
+ private void onZrifChanged(object? sender, EventArgs e)
{
- TextBox? txt = sender as TextBox;
+ FilteredTextBox? txt = sender as FilteredTextBox;
if (txt is null) return;
if (lastValid != IsValid)
diff --git a/ChovySign-GUI/Global/LabeledTextBox.axaml b/ChovySign-GUI/Global/LabeledTextBox.axaml
index 36b8274..2ae6abc 100644
--- a/ChovySign-GUI/Global/LabeledTextBox.axaml
+++ b/ChovySign-GUI/Global/LabeledTextBox.axaml
@@ -2,7 +2,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="80"
+ xmlns:Global="clr-namespace:ChovySign_GUI.Global"
+ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="80"
x:Class="ChovySign_GUI.Global.LabeledTextBox"
Height="60">
@@ -11,6 +12,6 @@
-
+
diff --git a/ChovySign-GUI/Global/LabeledTextBox.axaml.cs b/ChovySign-GUI/Global/LabeledTextBox.axaml.cs
index 5c46abd..e0c19b4 100644
--- a/ChovySign-GUI/Global/LabeledTextBox.axaml.cs
+++ b/ChovySign-GUI/Global/LabeledTextBox.axaml.cs
@@ -2,17 +2,16 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
-using JetBrains.Annotations;
-using Org.BouncyCastle.Asn1.X509;
using System;
using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
namespace ChovySign_GUI.Global
{
+
public partial class LabeledTextBox : UserControl
{
- private string lastTxt;
- private string? allowedChars = null;
public event EventHandler? TextChanged;
protected virtual void OnTextChanged(EventArgs e)
@@ -20,7 +19,6 @@ namespace ChovySign_GUI.Global
if (TextChanged is not null)
TextChanged(this, e);
}
-
public int MaxLength
{
get
@@ -37,25 +35,11 @@ namespace ChovySign_GUI.Global
{
get
{
- return this.txtBox.PasswordChar == default(char);
+ return this.txtBox.Password;
}
set
{
- if (value) this.txtBox.PasswordChar = 'X';
- else this.txtBox.PasswordChar = default(char);
- }
- }
- public string Label
- {
- get
- {
- string? lbl = this.lblTxt.Content as string;
- if (lbl is null) return "";
- else return lbl;
- }
- set
- {
- this.lblTxt.Content = value;
+ this.txtBox.Password = value;
}
}
@@ -74,19 +58,17 @@ namespace ChovySign_GUI.Global
{
get
{
- if (allowedChars is null) return "";
- else return allowedChars;
+ return this.txtBox.AllowedChars;
}
set
{
- allowedChars = value;
+ this.txtBox.AllowedChars = value;
}
}
public string Text
{
get
{
- if (this.txtBox.Text is null) return "";
return this.txtBox.Text;
}
set
@@ -94,63 +76,29 @@ namespace ChovySign_GUI.Global
this.txtBox.Text = value;
}
}
+ public string Label
+ {
+ get
+ {
+ string? lbl = this.lblTxt.Content as string;
+ if (lbl is null) return "";
+ else return lbl;
+ }
+ set
+ {
+ this.lblTxt.Content = value;
+ }
+ }
+
public LabeledTextBox()
{
InitializeComponent();
- lastTxt = this.txtBox.Text;
-
- this.txtBox.PastingFromClipboard += onPaste;
- this.txtBox.AddHandler(TextInputEvent, onTxtInput, RoutingStrategies.Tunnel);
+ this.txtBox.TextChanged += onTxtBoxTextChange;
}
- private string filter(string original)
+ private void onTxtBoxTextChange(object? sender, EventArgs e)
{
- if (allowedChars is not null)
- {
- string newTxt = original.ToUpperInvariant();
- for (int i = 0; i < newTxt.Length; i++)
- {
- if (!allowedChars.Contains(newTxt[i]))
- {
- newTxt = newTxt.Replace(newTxt[i].ToString(), "");
- }
- }
- return newTxt;
- }
- else
- {
- return original;
- }
- }
-
- private async void onPaste(object? sender, RoutedEventArgs e)
- {
- TextBox? txt = sender as TextBox;
- if (txt is null) return;
- if (Application.Current is null) return;
- if (Application.Current.Clipboard is null) return;
-
- e.Handled = true;
-
- string? newTxt = await Application.Current.Clipboard.GetTextAsync();
- if (newTxt is null) newTxt = "";
-
- /*TextInputEventArgs txtInput = new TextInputEventArgs();
- txtInput.Text = filter(newTxt);*/
-
- //txt.RaiseEvent(new RoutedEventArgs(txtInput));
- }
-
- private void onTxtInput(object? sender, TextInputEventArgs e)
- {
- string? newTxt = e.Text;
- if (newTxt is null) newTxt = "";
-
- newTxt = filter(newTxt);
- e.Text = newTxt;
-
- if (newTxt != lastTxt) OnTextChanged(new EventArgs());
- lastTxt = newTxt;
+ OnTextChanged(e);
}
}
}
diff --git a/ChovySign-GUI/Popup/Global/KeySelector/ActRifMethodGUI.axaml b/ChovySign-GUI/Popup/Global/KeySelector/ActRifMethodGUI.axaml
index a8edd4f..f3b3b27 100644
--- a/ChovySign-GUI/Popup/Global/KeySelector/ActRifMethodGUI.axaml
+++ b/ChovySign-GUI/Popup/Global/KeySelector/ActRifMethodGUI.axaml
@@ -17,8 +17,8 @@
-
-
+
+
diff --git a/ChovySign-GUI/Ps1/Ps1Tab.axaml b/ChovySign-GUI/Ps1/Ps1Tab.axaml
index fc88321..2011ee7 100644
--- a/ChovySign-GUI/Ps1/Ps1Tab.axaml
+++ b/ChovySign-GUI/Ps1/Ps1Tab.axaml
@@ -9,7 +9,7 @@
-
+
diff --git a/LibChovy/VersionKey/KeysTxtMethod.cs b/LibChovy/VersionKey/KeysTxtMethod.cs
index d8434bc..942c4f3 100644
--- a/LibChovy/VersionKey/KeysTxtMethod.cs
+++ b/LibChovy/VersionKey/KeysTxtMethod.cs
@@ -57,7 +57,7 @@ namespace LibChovy.VersionKey
{
line = line.ReplaceLineEndings("");
string[] data = line.Split(' ');
- if (data.Length != 4) continue;
+ if (data.Length != 5) continue;
if (data[0].Equals(contentId, StringComparison.InvariantCultureIgnoreCase))
return new NpDrmInfo(MathUtil.StringToByteArray(data[1 + keyIndex]), contentId, keyIndex);