forked from decentraland/unity-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add new toggle UI component (decentraland#2087)
* Added toggle component and model * Additional changes for new toggle component * Added tests for new toggle component * minor changes * minor changes * renamed test file
- Loading branch information
1 parent
5f67632
commit d264574
Showing
8 changed files
with
448 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
unity-renderer/Assets/UIComponents/Scripts/Components/Toggle.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
unity-renderer/Assets/UIComponents/Scripts/Components/Toggle/ToggleComponentModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
[Serializable] | ||
public class ToggleComponentModel : BaseComponentModel | ||
{ | ||
public string text; | ||
public bool isTextActive; | ||
} |
11 changes: 11 additions & 0 deletions
11
unity-renderer/Assets/UIComponents/Scripts/Components/Toggle/ToggleComponentModel.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
unity-renderer/Assets/UIComponents/Scripts/Components/Toggle/ToggleComponentView.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using TMPro; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public interface IToggleComponentView | ||
{ | ||
/// <summary> | ||
/// Event that will be triggered when the toggle is clicked. | ||
/// </summary> | ||
Toggle.ToggleEvent onToggleChange { get; } | ||
|
||
/// <summary> | ||
/// Set the toggle text. | ||
/// </summary> | ||
/// <param name="newText">New text.</param> | ||
void SetText(string newText); | ||
|
||
/// <summary> | ||
/// Set toggle text active | ||
/// </summary> | ||
/// <param name="isActive">Is active</param> | ||
void SetTextActive(bool isActive); | ||
|
||
/// <summary> | ||
/// Set the toggle clickable or not. | ||
/// </summary> | ||
/// <param name="isInteractable">Clickable or not</param> | ||
void SetInteractable(bool isInteractable); | ||
|
||
/// <summary> | ||
/// Return if the toggle is Interactable or not | ||
/// </summary> | ||
bool IsInteractable(); | ||
} | ||
|
||
public class ToggleComponentView : BaseComponentView, IToggleComponentView, IComponentModelConfig | ||
{ | ||
|
||
[Header("Prefab References")] | ||
[SerializeField] internal Toggle toggle; | ||
[SerializeField] internal TMP_Text text; | ||
|
||
[Header("Configuration")] | ||
[SerializeField] internal ToggleComponentModel model; | ||
|
||
public Toggle.ToggleEvent onToggleChange => toggle?.onValueChanged; | ||
|
||
public void Configure(BaseComponentModel newModel) | ||
{ | ||
model = (ToggleComponentModel)newModel; | ||
RefreshControl(); | ||
} | ||
|
||
public override void RefreshControl() | ||
{ | ||
if (model == null) | ||
return; | ||
|
||
SetTextActive(model.isTextActive); | ||
SetText(model.text); | ||
} | ||
|
||
public bool IsInteractable() { return toggle.interactable; } | ||
|
||
public void SetInteractable(bool isActive) { toggle.interactable = isActive; } | ||
|
||
public void SetTextActive(bool isActive) | ||
{ | ||
model.isTextActive = isActive; | ||
text.gameObject.SetActive(isActive); | ||
} | ||
|
||
public void SetText(string newText) | ||
{ | ||
model.text = newText; | ||
|
||
if (text == null) | ||
return; | ||
|
||
text.text = newText; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
unity-renderer/Assets/UIComponents/Scripts/Components/Toggle/ToggleComponentView.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.