Skip to content

Commit

Permalink
Feat: add new toggle UI component (decentraland#2087)
Browse files Browse the repository at this point in the history
* 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
davidejensen authored Mar 30, 2022
1 parent 5f67632 commit d264574
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 7 deletions.
203 changes: 196 additions & 7 deletions unity-renderer/Assets/UIComponents/Resources/Toggle_Capsule.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,141 @@ MonoBehaviour:
targetRectTransform: {fileID: 8835051055064169428}
hoverScale: 1.05
normalScale: 1
--- !u!1 &5490641609121925526
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3659324120495912970}
- component: {fileID: 661218021015313219}
- component: {fileID: 1996526906130022640}
m_Layer: 5
m_Name: Text (TMP)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &3659324120495912970
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5490641609121925526}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6521834947971461802}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: -97.3, y: -8.8}
m_SizeDelta: {x: 180, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &661218021015313219
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5490641609121925526}
m_CullTransparentMesh: 1
--- !u!114 &1996526906130022640
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5490641609121925526}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text:
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 0da28b1264c7140eda8582804e5d094b, type: 2}
m_sharedMaterial: {fileID: 4551780013374805225, guid: 0da28b1264c7140eda8582804e5d094b,
type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 14
m_fontSizeBase: 14
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 0
m_fontSizeMax: 16
m_fontStyle: 0
m_HorizontalAlignment: 2
m_VerticalAlignment: 512
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 0
m_wordWrappingRatios: 0.4
m_overflowMode: 1
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1 &7225474628005434785
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -308,8 +443,11 @@ GameObject:
m_Component:
- component: {fileID: 6521834947971461802}
- component: {fileID: 7625753975028379600}
- component: {fileID: 1291761582272755365}
- component: {fileID: 7833640354987619407}
- component: {fileID: -659517891130458604}
- component: {fileID: 5668197389447213992}
- component: {fileID: 2322060113541534132}
- component: {fileID: 8217971648214782342}
m_Layer: 5
m_Name: Toggle_Capsule
m_TagString: Untagged
Expand All @@ -330,6 +468,7 @@ RectTransform:
m_Children:
- {fileID: 8038834335027227982}
- {fileID: 2224212902183062667}
- {fileID: 3659324120495912970}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -386,7 +525,7 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls: []
m_IsOn: 1
--- !u!114 &1291761582272755365
--- !u!114 &7833640354987619407
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand All @@ -395,12 +534,10 @@ MonoBehaviour:
m_GameObject: {fileID: 7225474628005434785}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a1fae83919e09485289d831f8083fbf5, type: 3}
m_Script: {fileID: 11500000, guid: 1b102d1bc6e18474980da7b796ce9b08, type: 3}
m_Name:
m_EditorClassIdentifier:
activeOn: {fileID: 8806659310530846726}
activeOff: {fileID: 9008524825783389075}
--- !u!114 &7833640354987619407
--- !u!114 &-659517891130458604
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand All @@ -409,9 +546,61 @@ MonoBehaviour:
m_GameObject: {fileID: 7225474628005434785}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1b102d1bc6e18474980da7b796ce9b08, type: 3}
m_Script: {fileID: 11500000, guid: 49868df02d62a428c8aa5b2dd1a1ce20, type: 3}
m_Name:
m_EditorClassIdentifier:
toggle: {fileID: 7625753975028379600}
text: {fileID: 1996526906130022640}
model:
text:
isTextActive: 0
--- !u!225 &5668197389447213992
CanvasGroup:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7225474628005434785}
m_Enabled: 1
m_Alpha: 1
m_Interactable: 1
m_BlocksRaycasts: 1
m_IgnoreParentGroups: 0
--- !u!95 &2322060113541534132
Animator:
serializedVersion: 3
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7225474628005434785}
m_Enabled: 1
m_Avatar: {fileID: 0}
m_Controller: {fileID: 22100000, guid: d5f6cd638da0f1b4388c7a3679abe0b0, type: 2}
m_CullingMode: 0
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0
m_WarningMessage:
m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1
m_KeepAnimatorControllerStateOnDisable: 0
--- !u!114 &8217971648214782342
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7225474628005434785}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d8b939b5bedb9494b806609faeda7d8d, type: 3}
m_Name:
m_EditorClassIdentifier:
hideOnEnable: 0
animSpeedFactor: 1
disableAfterFadeOut: 0
visibleParam: visible
--- !u!1 &8806659310530846726
GameObject:
m_ObjectHideFlags: 0
Expand Down

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

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;
}

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

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;
}
}

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

Loading

0 comments on commit d264574

Please sign in to comment.