mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Fix color parsing and serialization
This commit is contained in:
parent
bf7d5dd933
commit
d1029ca0c1
Binary file not shown.
@ -104,6 +104,7 @@
|
|||||||
<Compile Include="UI/BoxSlider.cs" />
|
<Compile Include="UI/BoxSlider.cs" />
|
||||||
<Compile Include="Util\ColorHSL.cs" />
|
<Compile Include="Util\ColorHSL.cs" />
|
||||||
<Compile Include="Util\ColorHSV.cs" />
|
<Compile Include="Util\ColorHSV.cs" />
|
||||||
|
<Compile Include="Util\ColorUtil.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)/Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)/Microsoft.CSharp.targets" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
using ConformalDecals.MaterialProperties;
|
using ConformalDecals.MaterialProperties;
|
||||||
using ConformalDecals.Text;
|
using ConformalDecals.Text;
|
||||||
using ConformalDecals.UI;
|
using ConformalDecals.UI;
|
||||||
|
using ConformalDecals.Util;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace ConformalDecals {
|
namespace ConformalDecals {
|
||||||
public class ModuleConformalText : ModuleConformalDecal, ISerializationCallbackReceiver {
|
public class ModuleConformalText : ModuleConformalDecal, ISerializationCallbackReceiver {
|
||||||
[KSPField(isPersistant = true)] public string text = "Text";
|
[KSPField(isPersistant = true)] public string text = "Text";
|
||||||
[KSPField(isPersistant = true)] public Color fillColor = Color.black;
|
|
||||||
[KSPField(isPersistant = true)] public Color outlineColor = Color.white;
|
|
||||||
|
|
||||||
[KSPField] public Vector2 lineSpacingRange = new Vector2(-50, 50);
|
[KSPField] public Vector2 lineSpacingRange = new Vector2(-50, 50);
|
||||||
[KSPField] public Vector2 charSpacingRange = new Vector2(-50, 50);
|
[KSPField] public Vector2 charSpacingRange = new Vector2(-50, 50);
|
||||||
@ -19,6 +18,8 @@ namespace ConformalDecals {
|
|||||||
[KSPField(isPersistant = true)] public bool vertical;
|
[KSPField(isPersistant = true)] public bool vertical;
|
||||||
[KSPField(isPersistant = true)] public float lineSpacing;
|
[KSPField(isPersistant = true)] public float lineSpacing;
|
||||||
[KSPField(isPersistant = true)] public float charSpacing;
|
[KSPField(isPersistant = true)] public float charSpacing;
|
||||||
|
[KSPField(isPersistant = true)] public string fillColor = "000000FF";
|
||||||
|
[KSPField(isPersistant = true)] public string outlineColor = "FFFFFFFF";
|
||||||
|
|
||||||
// KSP TWEAKABLES
|
// KSP TWEAKABLES
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ namespace ConformalDecals {
|
|||||||
guiActive = false, guiActiveEditor = true)]
|
guiActive = false, guiActiveEditor = true)]
|
||||||
public void SetFillColor() {
|
public void SetFillColor() {
|
||||||
if (_fillColorPickerController == null) {
|
if (_fillColorPickerController == null) {
|
||||||
_fillColorPickerController = ColorPickerController.Create(fillColor, OnFillColorUpdate);
|
_fillColorPickerController = ColorPickerController.Create(_fillColor, OnFillColorUpdate);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_fillColorPickerController.Close();
|
_fillColorPickerController.Close();
|
||||||
@ -66,7 +67,7 @@ namespace ConformalDecals {
|
|||||||
guiActive = false, guiActiveEditor = true)]
|
guiActive = false, guiActiveEditor = true)]
|
||||||
public void SetOutlineColor() {
|
public void SetOutlineColor() {
|
||||||
if (_outlineColorPickerController == null) {
|
if (_outlineColorPickerController == null) {
|
||||||
_outlineColorPickerController = ColorPickerController.Create(outlineColor, OnOutlineColorUpdate);
|
_outlineColorPickerController = ColorPickerController.Create(_outlineColor, OnOutlineColorUpdate);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_outlineColorPickerController.Close();
|
_outlineColorPickerController.Close();
|
||||||
@ -75,6 +76,8 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
private DecalTextStyle _style;
|
private DecalTextStyle _style;
|
||||||
private DecalFont _font;
|
private DecalFont _font;
|
||||||
|
private Color32 _fillColor;
|
||||||
|
private Color32 _outlineColor;
|
||||||
|
|
||||||
private TextEntryController _textEntryController;
|
private TextEntryController _textEntryController;
|
||||||
private ColorPickerController _fillColorPickerController;
|
private ColorPickerController _fillColorPickerController;
|
||||||
@ -131,23 +134,23 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) {
|
public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) {
|
||||||
fillColor = rgb;
|
_fillColor = rgb;
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
|
|
||||||
foreach (var counterpart in part.symmetryCounterparts) {
|
foreach (var counterpart in part.symmetryCounterparts) {
|
||||||
var decal = counterpart.GetComponent<ModuleConformalText>();
|
var decal = counterpart.GetComponent<ModuleConformalText>();
|
||||||
decal.fillColor = fillColor;
|
decal._fillColor = _fillColor;
|
||||||
decal.UpdateMaterials();
|
decal.UpdateMaterials();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnOutlineColorUpdate(Color rgb, Util.ColorHSV hsv) {
|
public void OnOutlineColorUpdate(Color rgb, Util.ColorHSV hsv) {
|
||||||
outlineColor = rgb;
|
_outlineColor = rgb;
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
|
|
||||||
foreach (var counterpart in part.symmetryCounterparts) {
|
foreach (var counterpart in part.symmetryCounterparts) {
|
||||||
var decal = counterpart.GetComponent<ModuleConformalText>();
|
var decal = counterpart.GetComponent<ModuleConformalText>();
|
||||||
decal.outlineColor = outlineColor;
|
decal._outlineColor = _outlineColor;
|
||||||
decal.UpdateMaterials();
|
decal.UpdateMaterials();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,11 +203,23 @@ namespace ConformalDecals {
|
|||||||
vertical = _style.Vertical;
|
vertical = _style.Vertical;
|
||||||
lineSpacing = _style.LineSpacing;
|
lineSpacing = _style.LineSpacing;
|
||||||
charSpacing = _style.CharSpacing;
|
charSpacing = _style.CharSpacing;
|
||||||
|
fillColor = _fillColor.ToHexString();
|
||||||
|
outlineColor = _outlineColor.ToHexString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAfterDeserialize() {
|
public void OnAfterDeserialize() {
|
||||||
_font = DecalConfig.GetFont(fontName);
|
_font = DecalConfig.GetFont(fontName);
|
||||||
_style = new DecalTextStyle((FontStyles) style, vertical, lineSpacing, charSpacing);
|
_style = new DecalTextStyle((FontStyles) style, vertical, lineSpacing, charSpacing);
|
||||||
|
|
||||||
|
if (!ParseUtil.TryParseColor32(fillColor, out _fillColor)) {
|
||||||
|
Logging.LogWarning($"Improperly formatted color value for fill: '{fillColor}'");
|
||||||
|
_fillColor = Color.magenta;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ParseUtil.TryParseColor32(outlineColor, out _outlineColor)) {
|
||||||
|
Logging.LogWarning($"Improperly formatted color value for outline: '{outlineColor}'");
|
||||||
|
_outlineColor = Color.magenta;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnDestroy() {
|
public override void OnDestroy() {
|
||||||
@ -261,10 +276,10 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
protected override void UpdateMaterials() {
|
protected override void UpdateMaterials() {
|
||||||
_fillEnabledProperty.value = fillEnabled;
|
_fillEnabledProperty.value = fillEnabled;
|
||||||
_fillColorProperty.color = fillColor;
|
_fillColorProperty.color = _fillColor;
|
||||||
|
|
||||||
_outlineEnabledProperty.value = outlineEnabled;
|
_outlineEnabledProperty.value = outlineEnabled;
|
||||||
_outlineColorProperty.color = outlineColor;
|
_outlineColorProperty.color = _outlineColor;
|
||||||
_outlineWidthProperty.value = outlineWidth;
|
_outlineWidthProperty.value = outlineWidth;
|
||||||
|
|
||||||
base.UpdateMaterials();
|
base.UpdateMaterials();
|
||||||
|
22
Source/ConformalDecals/Util/ColorUtil.cs
Normal file
22
Source/ConformalDecals/Util/ColorUtil.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace ConformalDecals.Util {
|
||||||
|
public static class ColorUtil {
|
||||||
|
/// Returns an RGBA 32-bit hex string
|
||||||
|
public static string ToHexString(this Color32 color) {
|
||||||
|
return $"#{color.r:x2}{color.g:x2}{color.b:x2}{color.a:x2}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns an RGBA 32-bit unsigned integer representation of the color
|
||||||
|
public static uint ToUint(this Color32 color) {
|
||||||
|
uint rgba = color.r;
|
||||||
|
rgba <<= 8;
|
||||||
|
rgba |= color.g;
|
||||||
|
rgba <<= 8;
|
||||||
|
rgba |= color.b;
|
||||||
|
rgba <<= 8;
|
||||||
|
rgba |= color.a;
|
||||||
|
return rgba;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -142,7 +142,7 @@ namespace ConformalDecals.Util {
|
|||||||
public static bool TryParseHexColor(string valueString, out Color32 value) {
|
public static bool TryParseHexColor(string valueString, out Color32 value) {
|
||||||
value = new Color32(0, 0, 0, byte.MaxValue);
|
value = new Color32(0, 0, 0, byte.MaxValue);
|
||||||
|
|
||||||
if (!int.TryParse(valueString, System.Globalization.NumberStyles.HexNumber, null, out var hexColor)) return false;
|
if (!uint.TryParse(valueString, System.Globalization.NumberStyles.HexNumber, null, out var hexColor)) return false;
|
||||||
|
|
||||||
switch (valueString.Length) {
|
switch (valueString.Length) {
|
||||||
case 8: // RRGGBBAA
|
case 8: // RRGGBBAA
|
||||||
@ -216,7 +216,14 @@ namespace ConformalDecals.Util {
|
|||||||
value.g = (byte) (green * 0xFF);
|
value.g = (byte) (green * 0xFF);
|
||||||
value.b = (byte) (blue * 0xFF);
|
value.b = (byte) (blue * 0xFF);
|
||||||
return true;
|
return true;
|
||||||
|
case 1: // try again for hex color
|
||||||
|
if (TryParseHexColor(split[0], out var hexcolor)) {
|
||||||
|
value = hexcolor;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user