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

feature-better-tweakables
Andrew Cassidy 4 years ago
parent 86548a1037
commit 7ec4c85e8c

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

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

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

@ -27,11 +27,16 @@ namespace ConformalDecals.Text {
}
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) {
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() {
@ -44,11 +49,11 @@ namespace ConformalDecals.Text {
}
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) {
return left != null && !left.Equals(right);
return !Equals(left, right);
}
}
}

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

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

@ -61,8 +61,6 @@ namespace ConformalDecals.Text {
}
}
private void Start() {
if (_instance != null) {
Debug.Log("[ConformalDecals] Duplicate TextRenderer created???");
@ -74,12 +72,13 @@ namespace ConformalDecals.Text {
}
private void Update() {
bool renderNeeded;
do {
if (RenderJobs.Count == 0) return;
var nextJob = RenderJobs.Dequeue();
RunJob(nextJob, out renderNeeded);
} while (!renderNeeded);
// TODO: ASYNC RENDERING
// bool renderNeeded;
// do {
// if (RenderJobs.Count == 0) return;
// var nextJob = RenderJobs.Dequeue();
// RunJob(nextJob, out renderNeeded);
// } while (!renderNeeded);
}
private void Setup() {
@ -160,6 +159,13 @@ namespace ConformalDecals.Text {
_tmp.overflowMode = TextOverflowModes.Overflow;
_tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline;
_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
_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
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
var window = new Rect {
size = size * sizeRatio,
center = (Vector2) textureSize / 2
};
Debug.Log($"Window size: {window.size}");
Debug.Log($"Texture size: {textureSize}");
// SETUP TEXTURE
if (texture == null) {
@ -220,6 +229,7 @@ namespace ConformalDecals.Text {
Graphics.SetRenderTarget(renderTex);
GL.PushMatrix();
GL.LoadProjectionMatrix(matrix);
GL.Clear(false, true, Color.black);
_blitMaterial.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
GL.PopMatrix();
@ -232,7 +242,7 @@ namespace ConformalDecals.Text {
// RELEASE RENDERTEX
RenderTexture.ReleaseTemporary(renderTex);
return new TextRenderOutput(texture, window);
return new TextRenderOutput(texture, window, weight);
}
}
}
Loading…
Cancel
Save