Skip to content

Commit

Permalink
Added Speed Controls
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-Morgan committed Aug 31, 2021
1 parent 1cdc063 commit 25ec944
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Evacuation Simulation/Assets/Resources/UI/Styles/UI_root.uss
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ Label {
margin-bottom: 0;
margin-top: 0;
}

#Speed > Button {
background-color: rgba(53, 59, 72, 255);
}
4 changes: 2 additions & 2 deletions Evacuation Simulation/Assets/Resources/UI/UI.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<PedestrianSimulation.UI.Elements.SpeckleWindowElement />
</ui:VisualElement>
<ui:VisualElement name="bottom" style="flex-direction: column-reverse;">
<PedestrianSimulation.UI.Elements.TimelineElement />
<ui:VisualElement name="resultsParent" style="width: 150px; margin-bottom: 20px; margin-left: 15px;" />
<PedestrianSimulation.UI.Elements.TimelineElement style="margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px;" />
<ui:VisualElement name="speed" style="flex-direction: row; margin-left: 10px; margin-right: 10px; margin-top: 5px; margin-bottom: 5px;" />
</ui:VisualElement>
<ui:VisualElement name="top">
<PedestrianSimulation.UI.Elements.CameraViewsElement />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using UnityEngine.UIElements;

namespace JMTools
{
#nullable enable
public static class UIToolkitExtensions
{
public static void AddRange(this VisualElement parent, IEnumerable<VisualElement?> children)
{
foreach (var child in children)
{
parent.Add(child);
}
}

public static void AddRange(this VisualElement parent, params VisualElement?[] childrenParams) => AddRange(parent, children: childrenParams);
}
}

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,51 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using JMTools;
using UnityEngine;
using UnityEngine.UIElements;

namespace PedestrianSimulation.UI
{
[RequireComponent(typeof(UIDocument))]
public class TimeScaleController : MonoBehaviour
{

private VisualElement element;
private void Start()
{
var uiDocument = GetComponent<UIDocument>();
var rootVisualElement = uiDocument.rootVisualElement;

element = rootVisualElement.Q("speed");

element.AddRange(GenerateSpeedControls(1,2,4));
}


private static List<VisualElement> GenerateSpeedControls(params float[] speedParams) => GenerateSpeedControls(speeds: speedParams);
private static List<VisualElement> GenerateSpeedControls(IEnumerable<float> speeds)
{
var elements = new List<VisualElement>();

foreach (float speed in speeds)
{
var button = new Button(() =>
{
SetTimeScale(speed);
})
{
text = $"{speed}",
};

elements.Add(button);
}

return elements;
}

private static void SetTimeScale(float value) => Time.timeScale = value;
}
}

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

0 comments on commit 25ec944

Please sign in to comment.