mirror of
https://mirror.ghproxy.com/https://github.com/dexyfex/CodeWalker
synced 2026-05-15 04:50:46 +08:00
Added QuaternionBox to allow editing of quaternions as Euler angles as well as quaternions
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DockPanelSuite.ThemeVS2015" Version="3.0.6" />
|
||||
<PackageReference Include="SharpDX" Version="4.2.0" />
|
||||
<PackageReference Include="SharpDX.Mathematics" Version="4.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
|
||||
namespace CodeWalker.WinForms
|
||||
{
|
||||
partial class QuaternionBox
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Component Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.QuaternionTextBox = new System.Windows.Forms.TextBox();
|
||||
this.EulerXUpDown = new System.Windows.Forms.NumericUpDown();
|
||||
this.EulerYUpDown = new System.Windows.Forms.NumericUpDown();
|
||||
this.EulerZUpDown = new System.Windows.Forms.NumericUpDown();
|
||||
this.NormalizeButton = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.EulerXUpDown)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.EulerYUpDown)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.EulerZUpDown)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// QuaternionTextBox
|
||||
//
|
||||
this.QuaternionTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.QuaternionTextBox.Location = new System.Drawing.Point(0, 2);
|
||||
this.QuaternionTextBox.Name = "QuaternionTextBox";
|
||||
this.QuaternionTextBox.Size = new System.Drawing.Size(190, 20);
|
||||
this.QuaternionTextBox.TabIndex = 0;
|
||||
this.QuaternionTextBox.Text = "0, 0, 0, 1";
|
||||
this.QuaternionTextBox.TextChanged += new System.EventHandler(this.QuaternionTextBox_TextChanged);
|
||||
//
|
||||
// EulerXUpDown
|
||||
//
|
||||
this.EulerXUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.EulerXUpDown.DecimalPlaces = 2;
|
||||
this.EulerXUpDown.Location = new System.Drawing.Point(196, 2);
|
||||
this.EulerXUpDown.Maximum = new decimal(new int[] {
|
||||
360,
|
||||
0,
|
||||
0,
|
||||
0});
|
||||
this.EulerXUpDown.Minimum = new decimal(new int[] {
|
||||
360,
|
||||
0,
|
||||
0,
|
||||
-2147483648});
|
||||
this.EulerXUpDown.Name = "EulerXUpDown";
|
||||
this.EulerXUpDown.Size = new System.Drawing.Size(56, 20);
|
||||
this.EulerXUpDown.TabIndex = 1;
|
||||
this.EulerXUpDown.ValueChanged += new System.EventHandler(this.EulerXUpDown_ValueChanged);
|
||||
//
|
||||
// EulerYUpDown
|
||||
//
|
||||
this.EulerYUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.EulerYUpDown.DecimalPlaces = 2;
|
||||
this.EulerYUpDown.Location = new System.Drawing.Point(258, 2);
|
||||
this.EulerYUpDown.Maximum = new decimal(new int[] {
|
||||
360,
|
||||
0,
|
||||
0,
|
||||
0});
|
||||
this.EulerYUpDown.Minimum = new decimal(new int[] {
|
||||
360,
|
||||
0,
|
||||
0,
|
||||
-2147483648});
|
||||
this.EulerYUpDown.Name = "EulerYUpDown";
|
||||
this.EulerYUpDown.Size = new System.Drawing.Size(56, 20);
|
||||
this.EulerYUpDown.TabIndex = 2;
|
||||
this.EulerYUpDown.ValueChanged += new System.EventHandler(this.EulerYUpDown_ValueChanged);
|
||||
//
|
||||
// EulerZUpDown
|
||||
//
|
||||
this.EulerZUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.EulerZUpDown.DecimalPlaces = 2;
|
||||
this.EulerZUpDown.Location = new System.Drawing.Point(320, 2);
|
||||
this.EulerZUpDown.Maximum = new decimal(new int[] {
|
||||
360,
|
||||
0,
|
||||
0,
|
||||
0});
|
||||
this.EulerZUpDown.Minimum = new decimal(new int[] {
|
||||
360,
|
||||
0,
|
||||
0,
|
||||
-2147483648});
|
||||
this.EulerZUpDown.Name = "EulerZUpDown";
|
||||
this.EulerZUpDown.Size = new System.Drawing.Size(56, 20);
|
||||
this.EulerZUpDown.TabIndex = 3;
|
||||
this.EulerZUpDown.ValueChanged += new System.EventHandler(this.EulerZUpDown_ValueChanged);
|
||||
//
|
||||
// NormalizeButton
|
||||
//
|
||||
this.NormalizeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.NormalizeButton.Location = new System.Drawing.Point(382, 0);
|
||||
this.NormalizeButton.Name = "NormalizeButton";
|
||||
this.NormalizeButton.Size = new System.Drawing.Size(68, 23);
|
||||
this.NormalizeButton.TabIndex = 4;
|
||||
this.NormalizeButton.Text = "Normalize";
|
||||
this.NormalizeButton.UseVisualStyleBackColor = true;
|
||||
this.NormalizeButton.Click += new System.EventHandler(this.NormalizeButton_Click);
|
||||
//
|
||||
// QuaternionBox
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.Controls.Add(this.NormalizeButton);
|
||||
this.Controls.Add(this.EulerZUpDown);
|
||||
this.Controls.Add(this.EulerYUpDown);
|
||||
this.Controls.Add(this.EulerXUpDown);
|
||||
this.Controls.Add(this.QuaternionTextBox);
|
||||
this.Margin = new System.Windows.Forms.Padding(0);
|
||||
this.Name = "QuaternionBox";
|
||||
this.Size = new System.Drawing.Size(450, 24);
|
||||
((System.ComponentModel.ISupportInitialize)(this.EulerXUpDown)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.EulerYUpDown)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.EulerZUpDown)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.TextBox QuaternionTextBox;
|
||||
private System.Windows.Forms.NumericUpDown EulerXUpDown;
|
||||
private System.Windows.Forms.NumericUpDown EulerYUpDown;
|
||||
private System.Windows.Forms.NumericUpDown EulerZUpDown;
|
||||
private System.Windows.Forms.Button NormalizeButton;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using SharpDX;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace CodeWalker.WinForms
|
||||
{
|
||||
public partial class QuaternionBox : UserControl
|
||||
{
|
||||
public QuaternionBox()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
|
||||
public Quaternion Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Value;
|
||||
}
|
||||
set
|
||||
{
|
||||
_Value = value;
|
||||
UpdateFromValue();
|
||||
}
|
||||
}
|
||||
|
||||
private Quaternion _Value = Quaternion.Identity;
|
||||
private bool suppressEvents = false;
|
||||
|
||||
public event EventHandler ValueChanged;
|
||||
private void RaiseValueChanged()
|
||||
{
|
||||
if (suppressEvents) return;
|
||||
if (ValueChanged == null) return;
|
||||
ValueChanged(this, null);
|
||||
}
|
||||
|
||||
|
||||
private void UpdateFromValue()
|
||||
{
|
||||
suppressEvents = true;
|
||||
UpdateTextBox();
|
||||
UpdateEulerUpDowns();
|
||||
suppressEvents = false;
|
||||
}
|
||||
|
||||
|
||||
private void UpdateFromTextBox()
|
||||
{
|
||||
if (suppressEvents) return;
|
||||
suppressEvents = true;
|
||||
_Value = ParseQuaternionString(QuaternionTextBox.Text);
|
||||
UpdateEulerUpDowns();
|
||||
suppressEvents = false;
|
||||
RaiseValueChanged();
|
||||
}
|
||||
|
||||
private void UpdateFromEuler()
|
||||
{
|
||||
if (suppressEvents) return;
|
||||
suppressEvents = true;
|
||||
_Value = GetQuaternion(EulerXUpDown.Value, EulerYUpDown.Value, EulerZUpDown.Value);
|
||||
UpdateTextBox();
|
||||
suppressEvents = false;
|
||||
RaiseValueChanged();
|
||||
}
|
||||
|
||||
private void UpdateTextBox()
|
||||
{
|
||||
QuaternionTextBox.Text = GetQuaternionString(_Value);
|
||||
}
|
||||
|
||||
private void UpdateEulerUpDowns()
|
||||
{
|
||||
var e = GetEulerAngles(_Value);
|
||||
EulerXUpDown.Value = (decimal)e.X;
|
||||
EulerYUpDown.Value = (decimal)e.Y;
|
||||
EulerZUpDown.Value = (decimal)e.Z;
|
||||
}
|
||||
|
||||
private void Normalize()
|
||||
{
|
||||
_Value.Normalize();
|
||||
UpdateFromValue();
|
||||
RaiseValueChanged();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static Quaternion ParseQuaternionString(string s)
|
||||
{
|
||||
bool tryParseFloat(string str, out float f)
|
||||
{
|
||||
if (float.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out f))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Quaternion q = Quaternion.Identity;
|
||||
string[] ss = s.Split(',');
|
||||
if (ss.Length > 0)
|
||||
{
|
||||
tryParseFloat(ss[0].Trim(), out q.X);
|
||||
}
|
||||
if (ss.Length > 1)
|
||||
{
|
||||
tryParseFloat(ss[1].Trim(), out q.Y);
|
||||
}
|
||||
if (ss.Length > 2)
|
||||
{
|
||||
tryParseFloat(ss[2].Trim(), out q.Z);
|
||||
}
|
||||
if (ss.Length > 3)
|
||||
{
|
||||
tryParseFloat(ss[3].Trim(), out q.W);
|
||||
}
|
||||
return q;
|
||||
}
|
||||
private static string GetQuaternionString(Quaternion q, string d = ", ")
|
||||
{
|
||||
var c = CultureInfo.InvariantCulture;
|
||||
return q.X.ToString(c) + d + q.Y.ToString(c) + d + q.Z.ToString(c) + d + q.W.ToString(c);
|
||||
}
|
||||
private static Vector3 GetEulerAngles(Quaternion q)
|
||||
{
|
||||
var x = q.X;
|
||||
var y = q.Y;
|
||||
var z = q.Z;
|
||||
var w = q.W;
|
||||
var xx = x * x;
|
||||
var yy = y * y;
|
||||
var zz = z * z;
|
||||
var ww = w * w;
|
||||
var ls = xx + yy + zz + ww;
|
||||
var st = x * w - y * z;
|
||||
var sv = ls * 0.499f;
|
||||
var rd = 180.0f / (float)Math.PI;
|
||||
if (st > sv)
|
||||
{
|
||||
return new Vector3(90, (float)Math.Atan2(y, x) * 2.0f * rd, 0);
|
||||
}
|
||||
else if (st < -sv)
|
||||
{
|
||||
return new Vector3(-90, (float)Math.Atan2(y, x) * -2.0f * rd, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector3(
|
||||
(float)Math.Asin(2.0f * st) * rd,
|
||||
(float)Math.Atan2(2.0f * (y * w + x * z), 1.0f - 2.0f * (xx + yy)) * rd,
|
||||
(float)Math.Atan2(2.0f * (x * y + z * w), 1.0f - 2.0f * (xx + zz)) * rd
|
||||
);
|
||||
}
|
||||
}
|
||||
private static Quaternion GetQuaternion(decimal x, decimal y, decimal z)
|
||||
{
|
||||
var deg = new Vector3((float)x, (float)y, (float)z);
|
||||
var rads = deg * (float)(Math.PI / 180.0);
|
||||
return Quaternion.RotationYawPitchRoll(rads.Y, rads.X, rads.Z);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void QuaternionTextBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateFromTextBox();
|
||||
}
|
||||
|
||||
private void EulerXUpDown_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateFromEuler();
|
||||
}
|
||||
|
||||
private void EulerYUpDown_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateFromEuler();
|
||||
}
|
||||
|
||||
private void EulerZUpDown_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateFromEuler();
|
||||
}
|
||||
|
||||
private void NormalizeButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Normalize();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
Reference in New Issue
Block a user