Skip to content

Commit

Permalink
add: ui elements and adjust ui
Browse files Browse the repository at this point in the history
Signed-off-by: Alptuğ Cırıt <[email protected]>
  • Loading branch information
mozhoku committed May 2, 2024
1 parent aaa403e commit bd11746
Show file tree
Hide file tree
Showing 48 changed files with 5,774 additions and 2,310 deletions.
Empty file removed Assets/AWSIM/Fonts/.gitkeep
Empty file.
6,079 changes: 3,800 additions & 2,279 deletions Assets/AWSIM/Scenes/Main/AutowareSimulation.unity

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Assets/AWSIM/Scripts/UI/DemoUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private void Start()
timeScaleText.text = "x " + timeScaleSlider.value.ToString("F2");
var version = Application.version;
print(version);
versionText.text = "AWSIM Labs v \n" + version;
versionText.text = "AWSIM Labs\nv" + version;
}

public void SetTimeScale(float timeScale)
Expand Down
20 changes: 16 additions & 4 deletions Assets/AWSIM/Scripts/UI/HotkeyHandler.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
using UnityEngine;
using UnityEngine.Serialization;

namespace AWSIM.Scripts.UI
{
public class HotkeyHandler : MonoBehaviour
{
[SerializeField] private Canvas mainMenu;
[SerializeField] private UISideBarHandler uiSideBarHandler;

private EgoVehiclePositionManager egoVehiclePositionManager;
private TrafficControlManager trafficControlManager;

private void Awake()
{
egoVehiclePositionManager = GetComponent<EgoVehiclePositionManager>();
trafficControlManager = GetComponent<TrafficControlManager>();
}

void Update()
{
// Toggle the main menu
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!mainMenu)
if (!uiSideBarHandler)
{
Debug.LogWarning(
"The mainMenu reference is null. Please ensure the mainMenu Canvas object is assigned to mainMenu in the Inspector. Without it, the GUI cannot be toggled on/off.");
}
else
{
mainMenu.enabled = !mainMenu.enabled;
uiSideBarHandler.ToggleSideBar();
}
}

Expand All @@ -37,6 +40,15 @@ void Update()
egoVehiclePositionManager.ResetEgoToSpawnPoint();
}
}

// Toggle traffic visibility
if (Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetKeyDown(KeyCode.T))
{
trafficControlManager.TrafficManagerVisibilityToggle();
}
}
}
}
}
}

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

33 changes: 33 additions & 0 deletions Assets/AWSIM/Scripts/UI/Toggle/UITrafficControlPlayToggle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;
using UnityEngine.UI;

namespace AWSIM.Scripts.UI.Toggle
{
public class UITrafficControlPlayToggle : MonoBehaviour
{
[SerializeField] public Sprite sprite1;
[SerializeField] public Sprite sprite2;

private TrafficControlManager trafficControlManager;
private Image image;

private void Start()
{
image = GetComponent<Image>();
trafficControlManager = FindObjectOfType<TrafficControlManager>();
trafficControlManager.trafficPlayToggleEvent.AddListener(OnStatusChangeUpdateImage);
}

private void OnStatusChangeUpdateImage(bool isToggled)
{
Debug.Log("OnStatusChangeUpdateImage called. isToggled: " + isToggled);

image.sprite = isToggled ? sprite1 : sprite2;
}

private void OnDestroy()
{
trafficControlManager.trafficPlayToggleEvent.RemoveListener(OnStatusChangeUpdateImage);
}
}
}
11 changes: 11 additions & 0 deletions Assets/AWSIM/Scripts/UI/Toggle/UITrafficControlPlayToggle.cs.meta

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

33 changes: 33 additions & 0 deletions Assets/AWSIM/Scripts/UI/Toggle/UITrafficControlVisibilityToggle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;
using UnityEngine.UI;

namespace AWSIM.Scripts.UI.Toggle
{
public class UITrafficControlVisibilityToggle : MonoBehaviour
{
[SerializeField] private Sprite sprite1;
[SerializeField] private Sprite sprite2;

private TrafficControlManager trafficControlManager;
private Image image;

private void Start()
{
image = GetComponent<Image>();
trafficControlManager = FindObjectOfType<TrafficControlManager>();
trafficControlManager.trafficVisibilityToggleEvent.AddListener(OnStatusChangeUpdateImage);
}

private void OnStatusChangeUpdateImage(bool isToggled)
{
Debug.Log("OnStatusChangeUpdateImage called. isToggled: " + isToggled);

image.sprite = isToggled ? sprite1 : sprite2;
}

private void OnDestroy()
{
trafficControlManager.trafficVisibilityToggleEvent.RemoveListener(OnStatusChangeUpdateImage);
}
}
}

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

52 changes: 42 additions & 10 deletions Assets/AWSIM/Scripts/UI/TrafficControlManager.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
using System;
using AWSIM.TrafficSimulation;
using UnityEngine;
using UnityEngine.Events;

namespace AWSIM.Scripts.UI
{
public class TrafficPlayToggleEvent : UnityEvent<bool>
{
}

public class TrafficVisibilityToggleEvent : UnityEvent<bool>
{
}

public class TrafficControlManager : MonoBehaviour
{
public TrafficManager TrafficManager { private get; set; }

public void TrafficManagerPauseResumeToggle()
public TrafficPlayToggleEvent trafficPlayToggleEvent;
public TrafficVisibilityToggleEvent trafficVisibilityToggleEvent;

private void Awake()
{
trafficPlayToggleEvent ??= new TrafficPlayToggleEvent();
trafficVisibilityToggleEvent ??= new TrafficVisibilityToggleEvent();
}

public void TrafficManagerPlayToggle()
{
if (TrafficManager.gameObject.activeSelf)
{
TrafficManager.enabled = !TrafficManager.enabled;
trafficPlayToggleEvent.Invoke(TrafficManager.enabled);
}
else
Debug.Log("TrafficManager is not active. Can't pause/resume!");
}

public void TrafficManagerEnable()
public void TrafficManagerVisibilityToggle()
{
TrafficManager.gameObject.SetActive(true);
TrafficManager.enabled = true;
}
if (!TrafficManager.gameObject.activeSelf)
{
TrafficManager.gameObject.SetActive(true);
}
else
{
TrafficManager.enabled = false;
TrafficManager.gameObject.SetActive(false);

public void TrafficManagerDisable()
{
TrafficManager.gameObject.SetActive(false);
// Invoke event
trafficPlayToggleEvent.Invoke(TrafficManager.enabled);
}

//Invoke event
trafficVisibilityToggleEvent.Invoke(TrafficManager.gameObject.activeSelf);
}

public void TrafficManagerReset()
Expand All @@ -41,7 +67,13 @@ public void TrafficManagerReset()
TrafficManager.gameObject.SetActive(true);
TrafficManager.enabled = true;
TrafficManager.npcVehicleSimulator.ClearAll();

// Invoke event
trafficVisibilityToggleEvent.Invoke(TrafficManager.gameObject.activeSelf);
}

// Invoke event
trafficPlayToggleEvent.Invoke(TrafficManager.enabled);
}

public void TrafficMangerSetSeed(int seed)
Expand All @@ -54,4 +86,4 @@ public void TrafficMangerSetSeed(int seed)
TrafficManager.seed = seed;
}
}
}
}
18 changes: 7 additions & 11 deletions Assets/AWSIM/Scripts/UI/UIBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class UIBridge : MonoBehaviour
{
private EgoVehiclePositionManager egoVehiclePositionManager;
private TrafficControlManager trafficControlManager;
[SerializeField] InputField trafficSeedInputField;

[SerializeField] private InputField trafficSeedInputField;

private void Awake()
{
Expand All @@ -21,19 +22,14 @@ public void ResetEgoToSpawnPoint()
egoVehiclePositionManager.ResetEgoToSpawnPoint();
}

public void TrafficManagerPauseResumeToggle()
{
trafficControlManager.TrafficManagerPauseResumeToggle();
}

public void TrafficManagerEnable()
public void TrafficManagerPlayToggle(bool isOn)
{
trafficControlManager.TrafficManagerEnable();
trafficControlManager.TrafficManagerPlayToggle();
}

public void TrafficManagerDisable()
public void TrafficManagerVisibilityToggle(bool isOn)
{
trafficControlManager.TrafficManagerDisable();
trafficControlManager.TrafficManagerVisibilityToggle();
}

public void TrafficManagerReset()
Expand All @@ -53,4 +49,4 @@ public void TrafficMangerSetSeed()
trafficControlManager.TrafficMangerSetSeed(seed);
}
}
}
}
56 changes: 56 additions & 0 deletions Assets/AWSIM/Scripts/UI/UIFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections;
using UnityEngine;

namespace AWSIM.Scripts.UI
{
// Functions used by the UI elements
public static class UIFunctions
{
// Lerp UI objects position
public static IEnumerator LerpUIRectPosition(RectTransform uiRect, Vector2 targetPosition, float lerpValue, bool willDisableAtEnd)
{
float elapsedTime = 0f;
Vector2 currentPosition = uiRect.anchoredPosition;

while (elapsedTime < 1f)
{
elapsedTime += Time.deltaTime * lerpValue;
uiRect.anchoredPosition = Vector2.Lerp(currentPosition, targetPosition, elapsedTime);
yield return null;
}

// Ensure the UI element is exactly at the target position
uiRect.anchoredPosition = targetPosition;

// Disable at end if wanted
if (willDisableAtEnd)
{
uiRect.gameObject.SetActive(false);
}
}

// Lerp UI objects height
public static IEnumerator LerpUIRectHeight(RectTransform uiRect, float targetHeight, float lerpValue, bool willDisableAtEnd)
{
float elapsedTime = 0f;
float currentHeight = uiRect.sizeDelta.y;

while (elapsedTime < 1f)
{
elapsedTime += Time.deltaTime * lerpValue;
float lerpedHeight = Mathf.Lerp(currentHeight, targetHeight, elapsedTime);
uiRect.sizeDelta = new Vector2(uiRect.sizeDelta.x, lerpedHeight);
yield return null;
}

// Ensure the UI element has the target height
uiRect.sizeDelta = new Vector2(uiRect.sizeDelta.x, targetHeight);

// Disable at end if wanted
if (willDisableAtEnd)
{
uiRect.gameObject.SetActive(false);
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/AWSIM/Scripts/UI/UIFunctions.cs.meta

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

Loading

0 comments on commit bd11746

Please sign in to comment.