Skip to content

Commit

Permalink
Implement proper autofill preview on developer console sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Orange-Panda committed May 16, 2023
1 parent f3794ed commit ec3f0c2
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 72 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this package are documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.0.0] - PENDING
## [2.0.0] - Unreleased

### Breaking Changes

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed Samples~/Dev Console/Default Console Assets/Square.png
Binary file not shown.
24 changes: 17 additions & 7 deletions Samples~/Dev Console/DevConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public class DevConsoleRunner : MonoBehaviour
[SerializeField]
private TMP_InputField inputText;
[SerializeField]
private TMP_Text autofillPreview;
private TMP_Text autofillText;
[SerializeField]
private RectTransform autofillContainer;
[SerializeField]
private RectTransform autofillParent;

[Header("Properties")]
[Tooltip("When true will require a key to be held to open/close the console.")]
Expand Down Expand Up @@ -133,7 +137,7 @@ private void Update()
UpdateAutofillPreview();

// Auto fill
if (DevConsole.ConsoleActive && Input.GetKeyDown(KeyCode.Tab) && DevConsole.console.ApplyNextAutofill(out string newInputText))
if (DevConsole.ConsoleActive && Input.GetKeyDown(KeyCode.Tab) && DevConsole.console.TryGetNextAutofillApplied(out string newInputText))
{
inputText.SetTextWithoutNotify(newInputText);
inputText.caretPosition = inputText.text.Length;
Expand Down Expand Up @@ -232,15 +236,21 @@ void UpdateAutofillPreview()
}

autofillPreviewValue = DevConsole.console.NextAutofill;
if (autofillPreviewValue != null)
if (autofillPreviewValue != null && DevConsole.console.VirtualText.Length > 0)
{
autofillPreview.text = autofillPreviewValue.markupNewWord;
autofillPreview.enabled = true;
TMP_CharacterInfo startIndexInfo = inputText.textComponent.textInfo.characterInfo[autofillPreviewValue.globalStartIndex];
autofillText.text = autofillPreviewValue.markupNewWord;
autofillContainer.gameObject.SetActive(true);
autofillContainer.sizeDelta = autofillText.GetPreferredValues(1024, autofillContainer.sizeDelta.y);
float parentHalfWidth = autofillParent.rect.width / 2;
float max = parentHalfWidth - autofillContainer.sizeDelta.x;
float autofillHorizontal = Mathf.Clamp(startIndexInfo.topLeft.x, -parentHalfWidth, max);
autofillContainer.anchoredPosition = new Vector2(autofillHorizontal, 32);
}
else
{
autofillPreview.enabled = false;
autofillPreview.text = string.Empty;
autofillContainer.gameObject.SetActive(false);
autofillText.text = string.Empty;
}
}
}
Expand Down
Loading

0 comments on commit ec3f0c2

Please sign in to comment.