Handle bold text and several events, wondering whats up with Actions[]

This commit is contained in:
Andrew Cassidy 2020-08-06 22:54:51 -07:00
parent 8d73541a42
commit bb965580db
No known key found for this signature in database
GPG Key ID: 963017B38FD477A1
8 changed files with 176 additions and 59 deletions

View File

@ -49,9 +49,11 @@ PART
{ {
name = ModuleConformalText name = ModuleConformalText
shader = ConformalDecals/Decal/Text
useBaseNormal = true useBaseNormal = true
scaleMode = MINIMUM
defaultDepth = 0.2 defaultDepth = 0.2
defaultCutoff = 0 defaultCutoff = 0.5
} }
} }

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:1b6601f24b2a422a2cc7eb8cb1b862362e019bf622ded69af1ee695f84563a53 oid sha256:a579fa0a84a21f57c9ea27de6f76689d12ad8370aef7c39af1e24ceceb395c82
size 82944 size 85504

View File

@ -246,8 +246,6 @@ namespace ConformalDecals {
_boundsRenderer = decalProjectorTransform.GetComponent<MeshRenderer>(); _boundsRenderer = decalProjectorTransform.GetComponent<MeshRenderer>();
//UpdateMaterials();
// handle tweakables // handle tweakables
if (HighLogic.LoadedSceneIsEditor) { if (HighLogic.LoadedSceneIsEditor) {
GameEvents.onEditorPartEvent.Add(OnEditorEvent); GameEvents.onEditorPartEvent.Add(OnEditorEvent);
@ -360,7 +358,7 @@ namespace ConformalDecals {
} }
} }
protected void OnAttach() { protected virtual void OnAttach() {
if (part.parent == null) { if (part.parent == null) {
this.LogError("Attach function called but part has no parent!"); this.LogError("Attach function called but part has no parent!");
_isAttached = false; _isAttached = false;
@ -383,7 +381,7 @@ namespace ConformalDecals {
UpdateScale(); UpdateScale();
} }
protected void OnDetach() { protected virtual void OnDetach() {
_isAttached = false; _isAttached = false;
// unhide model // unhide model
@ -500,7 +498,7 @@ namespace ConformalDecals {
} }
} }
protected void UpdateTweakables() { protected virtual void UpdateTweakables() {
// setup tweakable fields // setup tweakable fields
var scaleField = Fields[nameof(scale)]; var scaleField = Fields[nameof(scale)];
var depthField = Fields[nameof(depth)]; var depthField = Fields[nameof(depth)];

View File

@ -24,6 +24,9 @@ namespace ConformalDecals {
if (_textEntryController == null) { if (_textEntryController == null) {
_textEntryController = TextEntryController.Create(text, _font, _style, OnTextUpdate); _textEntryController = TextEntryController.Create(text, _font, _style, OnTextUpdate);
} }
else {
_textEntryController.OnClose();
}
} }
// FILL // FILL
@ -39,6 +42,9 @@ namespace ConformalDecals {
if (_fillColorPickerController == null) { if (_fillColorPickerController == null) {
_fillColorPickerController = ColorPickerController.Create(fillColor, OnFillColorUpdate); _fillColorPickerController = ColorPickerController.Create(fillColor, OnFillColorUpdate);
} }
else {
_fillColorPickerController.OnClose();
}
} }
// OUTLINE // OUTLINE
@ -56,8 +62,11 @@ namespace ConformalDecals {
[KSPEvent(guiName = "#LOC_ConformalDecals_gui-set-outline-color", groupName = "decal-outline", groupDisplayName = "#LOC_ConformalDecals_gui-group-outline", [KSPEvent(guiName = "#LOC_ConformalDecals_gui-set-outline-color", groupName = "decal-outline", groupDisplayName = "#LOC_ConformalDecals_gui-group-outline",
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 {
_outlineColorPickerController.OnClose();
} }
} }
@ -66,7 +75,17 @@ namespace ConformalDecals {
private TextEntryController _textEntryController; private TextEntryController _textEntryController;
private ColorPickerController _fillColorPickerController; private ColorPickerController _fillColorPickerController;
private ColorPickerController _outlineColorPickerCOntroller; private ColorPickerController _outlineColorPickerController;
private MaterialTextureProperty _decalTextureProperty;
private MaterialFloatProperty _decalTextWeightProperty;
private MaterialKeywordProperty _fillEnabledProperty;
private MaterialColorProperty _fillColorProperty;
private MaterialKeywordProperty _outlineEnabledProperty;
private MaterialColorProperty _outlineColorProperty;
private MaterialFloatProperty _outlineWidthProperty;
private TextRenderJob _currentJob; private TextRenderJob _currentJob;
private DecalText _currentText; private DecalText _currentText;
@ -74,6 +93,8 @@ namespace ConformalDecals {
public override void OnLoad(ConfigNode node) { public override void OnLoad(ConfigNode node) {
base.OnLoad(node); base.OnLoad(node);
OnAfterDeserialize(); OnAfterDeserialize();
UpdateTextRecursive();
} }
public override void OnSave(ConfigNode node) { public override void OnSave(ConfigNode node) {
@ -83,10 +104,24 @@ namespace ConformalDecals {
public override void OnStart(StartState state) { public override void OnStart(StartState state) {
base.OnStart(state); base.OnStart(state);
UpdateTextRecursive(); UpdateTextRecursive();
} }
public override void OnAwake() {
base.OnAwake();
_decalTextureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true);
_decalTextWeightProperty = materialProperties.AddOrGetProperty<MaterialFloatProperty>("_Weight");
_fillEnabledProperty = materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_FILL");
_fillColorProperty = materialProperties.AddOrGetProperty<MaterialColorProperty>("_DecalColor");
_outlineEnabledProperty = materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_OUTLINE");
_outlineColorProperty = materialProperties.AddOrGetProperty<MaterialColorProperty>("_OutlineColor");
_outlineWidthProperty = materialProperties.AddOrGetProperty<MaterialFloatProperty>("_OutlineWidth");
}
public void OnTextUpdate(string newText, DecalFont newFont, DecalTextStyle newStyle) { public void OnTextUpdate(string newText, DecalFont newFont, DecalTextStyle newStyle) {
text = newText; text = newText;
_font = newFont; _font = newFont;
@ -96,25 +131,63 @@ namespace ConformalDecals {
public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) { public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) {
fillColor = rgb; fillColor = rgb;
Debug.Log($"new fill color: {rgb}, {hsv}"); UpdateMaterials();
foreach (var counterpart in part.symmetryCounterparts) {
var decal = counterpart.GetComponent<ModuleConformalText>();
decal.fillColor = fillColor;
decal.UpdateMaterials();
}
} }
public void OnOutlineColorUpdate(Color rgb, Util.ColorHSV hsv) { public void OnOutlineColorUpdate(Color rgb, Util.ColorHSV hsv) {
outlineColor = rgb; outlineColor = rgb;
Debug.Log($"new outline color: {rgb}, {hsv}"); UpdateMaterials();
}
public void OnFillToggle() { foreach (var counterpart in part.symmetryCounterparts) {
if (!fillEnabled && !outlineEnabled) { var decal = counterpart.GetComponent<ModuleConformalText>();
outlineEnabled = true; decal.outlineColor = outlineColor;
OnOutlineToggle(); decal.UpdateMaterials();
} }
} }
public void OnOutlineToggle() { public void OnFillToggle(BaseField field, object obj) {
if (!fillEnabled && !outlineEnabled) {
outlineEnabled = true;
OnOutlineToggle(field, obj);
}
UpdateTweakables();
foreach (var counterpart in part.symmetryCounterparts) {
var decal = counterpart.GetComponent<ModuleConformalText>();
decal.fillEnabled = fillEnabled;
decal.UpdateTweakables();
}
}
public void OnOutlineToggle(BaseField field, object obj) {
if (!fillEnabled && !outlineEnabled) { if (!fillEnabled && !outlineEnabled) {
fillEnabled = true; fillEnabled = true;
OnFillToggle(); OnFillToggle(field, obj);
}
UpdateTweakables();
foreach (var counterpart in part.symmetryCounterparts) {
var decal = counterpart.GetComponent<ModuleConformalText>();
decal.outlineEnabled = outlineEnabled;
decal.UpdateTweakables();
}
}
public void OnOutlineWidthUpdate(BaseField field, object obj) {
UpdateMaterials();
foreach (var counterpart in part.symmetryCounterparts) {
var decal = counterpart.GetComponent<ModuleConformalText>();
decal.outlineWidth = outlineWidth;
decal.UpdateMaterials();
} }
} }
@ -132,10 +205,20 @@ namespace ConformalDecals {
} }
public override void OnDestroy() { public override void OnDestroy() {
TextRenderer.UnregisterText(_currentText); if (_currentText != null) TextRenderer.UnregisterText(_currentText);
base.OnDestroy(); base.OnDestroy();
} }
protected override void OnDetach() {
// close all UIs
if (_textEntryController != null) _textEntryController.OnClose();
if (_fillColorPickerController != null) _fillColorPickerController.OnClose();
if (_outlineColorPickerController != null) _outlineColorPickerController.OnClose();
base.OnDetach();
}
private void UpdateTextRecursive() { private void UpdateTextRecursive() {
UpdateText(); UpdateText();
@ -152,14 +235,13 @@ namespace ConformalDecals {
} }
private void UpdateText() { private void UpdateText() {
// Render text // Render text
var newText = new DecalText(text, _font, _style); var newText = new DecalText(text, _font, _style);
var output = TextRenderer.UpdateTextNow(_currentText, newText); var output = TextRenderer.UpdateTextNow(_currentText, newText);
_currentText = newText; _currentText = newText;
UpdateTexture(output); UpdateTexture(output);
// TODO: ASYNC RENDERING // TODO: ASYNC RENDERING
// var newText = new DecalText(text, _font, _style); // var newText = new DecalText(text, _font, _style);
// _currentJob = TextRenderer.UpdateText(_currentText, newText, UpdateTexture); // _currentJob = TextRenderer.UpdateText(_currentText, newText, UpdateTexture);
@ -167,25 +249,49 @@ namespace ConformalDecals {
} }
public void UpdateTexture(TextRenderOutput output) { public void UpdateTexture(TextRenderOutput output) {
var textureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true); _decalTextureProperty.Texture = output.Texture;
textureProperty.Texture = output.Texture; _decalTextureProperty.SetTile(output.Window);
textureProperty.SetTile(output.Window); _decalTextWeightProperty.value = output.Weight;
UpdateMaterials(); UpdateMaterials();
UpdateScale();
} }
protected override void UpdateMaterials() { protected override void UpdateMaterials() {
materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_FILL").value = fillEnabled; _fillEnabledProperty.value = fillEnabled;
materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_OUTLINE").value = outlineEnabled; _fillColorProperty.color = fillColor;
if (fillEnabled) {
materialProperties.AddOrGetProperty<MaterialColorProperty>("_DecalColor").color = fillColor;
}
if (outlineEnabled) { _outlineEnabledProperty.value = outlineEnabled;
materialProperties.AddOrGetProperty<MaterialColorProperty>("_OutlineColor").color = outlineColor; _outlineColorProperty.color = outlineColor;
materialProperties.AddOrGetProperty<MaterialFloatProperty>("_OutlineWidth").value = outlineWidth; _outlineWidthProperty.value = outlineWidth;
}
base.UpdateMaterials(); base.UpdateMaterials();
} }
protected override void UpdateTweakables() {
Debug.Log($"Fields is null: {Fields == null}");
Debug.Log($"Actions is null: {Actions == null}");
var fillEnabledField = Fields[nameof(fillEnabled)];
var fillColorAction = Actions["SetFillColor"];
var outlineEnabledField = Fields[nameof(outlineEnabled)];
var outlineWidthField = Fields[nameof(outlineWidth)];
var outlineColorAction = Actions["SetOutlineColor"];
Debug.Log($"outlineColorAction is null: {outlineColorAction == null}");
// fillColorAction.activeEditor = fillEnabled;
// outlineWidthField.guiActiveEditor = outlineEnabled;
// outlineColorAction.activeEditor = outlineEnabled;
Debug.Log("Fart");
((UI_Toggle) fillEnabledField.uiControlEditor).onFieldChanged = OnFillToggle;
((UI_Toggle) outlineEnabledField.uiControlEditor).onFieldChanged = OnOutlineToggle;
((UI_FloatRange) outlineWidthField.uiControlEditor).onFieldChanged = OnOutlineWidthUpdate;
base.UpdateTweakables();
}
protected void UpdateCachedProperties() { }
} }
} }

View File

@ -27,11 +27,16 @@ namespace ConformalDecals.Text {
} }
public bool Equals(DecalText other) { public bool Equals(DecalText other) {
return other != null && (Text == other.Text && Equals(Font, other.Font) && Style.Equals(other.Style)); if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Text == other.Text && Equals(Font, other.Font) && Style.Equals(other.Style);
} }
public override bool Equals(object obj) { public override bool Equals(object obj) {
return obj is DecalText other && Equals(other); if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((DecalText) obj);
} }
public override int GetHashCode() { public override int GetHashCode() {
@ -44,11 +49,11 @@ namespace ConformalDecals.Text {
} }
public static bool operator ==(DecalText left, DecalText right) { public static bool operator ==(DecalText left, DecalText right) {
return left != null && left.Equals(right); return Equals(left, right);
} }
public static bool operator !=(DecalText left, DecalText right) { public static bool operator !=(DecalText left, DecalText right) {
return left != null && !left.Equals(right); return !Equals(left, right);
} }
} }
} }

View File

@ -12,14 +12,7 @@ namespace ConformalDecals.Text {
public readonly TextRenderer.TextRenderEvent onRenderFinished = new TextRenderer.TextRenderEvent(); public readonly TextRenderer.TextRenderEvent onRenderFinished = new TextRenderer.TextRenderEvent();
public TextRenderJob(DecalText oldText, DecalText newText, UnityAction<TextRenderOutput> renderFinishedCallback) { public TextRenderJob(DecalText oldText, DecalText newText, UnityAction<TextRenderOutput> renderFinishedCallback) {
OldText = oldText ?? throw new ArgumentNullException(nameof(oldText)); OldText = oldText;
NewText = newText ?? throw new ArgumentNullException(nameof(newText));
Needed = true;
if (renderFinishedCallback != null) onRenderFinished.AddListener(renderFinishedCallback);
}
public TextRenderJob(DecalText newText, UnityAction<TextRenderOutput> renderFinishedCallback) {
NewText = newText ?? throw new ArgumentNullException(nameof(newText)); NewText = newText ?? throw new ArgumentNullException(nameof(newText));
Needed = true; Needed = true;

View File

@ -5,12 +5,15 @@ namespace ConformalDecals.Text {
public Texture2D Texture { get; private set; } public Texture2D Texture { get; private set; }
public Rect Window { get; private set; } public Rect Window { get; private set; }
public float Weight { get; private set; }
public int UserCount { get; set; } public int UserCount { get; set; }
public TextRenderOutput(Texture2D texture, Rect window) { public TextRenderOutput(Texture2D texture, Rect window, float weight) {
Texture = texture; Texture = texture;
Window = window; Window = window;
Weight = weight;
} }
} }
} }

View File

@ -61,8 +61,6 @@ namespace ConformalDecals.Text {
} }
} }
private void Start() { private void Start() {
if (_instance != null) { if (_instance != null) {
Debug.Log("[ConformalDecals] Duplicate TextRenderer created???"); Debug.Log("[ConformalDecals] Duplicate TextRenderer created???");
@ -74,12 +72,13 @@ namespace ConformalDecals.Text {
} }
private void Update() { private void Update() {
bool renderNeeded; // TODO: ASYNC RENDERING
do { // bool renderNeeded;
if (RenderJobs.Count == 0) return; // do {
var nextJob = RenderJobs.Dequeue(); // if (RenderJobs.Count == 0) return;
RunJob(nextJob, out renderNeeded); // var nextJob = RenderJobs.Dequeue();
} while (!renderNeeded); // RunJob(nextJob, out renderNeeded);
// } while (!renderNeeded);
} }
private void Setup() { private void Setup() {
@ -160,6 +159,13 @@ namespace ConformalDecals.Text {
_tmp.overflowMode = TextOverflowModes.Overflow; _tmp.overflowMode = TextOverflowModes.Overflow;
_tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline; _tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline;
_tmp.fontSize = FontSize; _tmp.fontSize = FontSize;
// CALCULATE FONT WEIGHT
float weight = 0;
if (text.Style.Bold && text.Font.FontAsset.fontWeights[7].regularTypeface == null) {
weight = text.Font.FontAsset.boldStyle;
}
// SETUP BLIT MATERIAL // SETUP BLIT MATERIAL
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.FontAsset.atlas); _blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.FontAsset.atlas);
@ -191,13 +197,16 @@ namespace ConformalDecals.Text {
} }
// scale up everything to fit the texture for maximum usage // scale up everything to fit the texture for maximum usage
float sizeRatio = Mathf.Min(textureSize.x / size.x, textureSize.y, size.y); float sizeRatio = Mathf.Min(textureSize.x / size.x, textureSize.y / size.y);
// calculate where in the texture the used area actually is // calculate where in the texture the used area actually is
var window = new Rect { var window = new Rect {
size = size * sizeRatio, size = size * sizeRatio,
center = (Vector2) textureSize / 2 center = (Vector2) textureSize / 2
}; };
Debug.Log($"Window size: {window.size}");
Debug.Log($"Texture size: {textureSize}");
// SETUP TEXTURE // SETUP TEXTURE
if (texture == null) { if (texture == null) {
@ -220,6 +229,7 @@ namespace ConformalDecals.Text {
Graphics.SetRenderTarget(renderTex); Graphics.SetRenderTarget(renderTex);
GL.PushMatrix(); GL.PushMatrix();
GL.LoadProjectionMatrix(matrix); GL.LoadProjectionMatrix(matrix);
GL.Clear(false, true, Color.black);
_blitMaterial.SetPass(0); _blitMaterial.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity); Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
GL.PopMatrix(); GL.PopMatrix();
@ -232,7 +242,7 @@ namespace ConformalDecals.Text {
// RELEASE RENDERTEX // RELEASE RENDERTEX
RenderTexture.ReleaseTemporary(renderTex); RenderTexture.ReleaseTemporary(renderTex);
return new TextRenderOutput(texture, window); return new TextRenderOutput(texture, window, weight);
} }
} }
} }