Skip to content

Commit

Permalink
Add UI buttons and modal dialogues.
Browse files Browse the repository at this point in the history
  • Loading branch information
0mdc committed May 15, 2024
1 parent f0d636c commit e6bb099
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Assets/Scripts/AppMouseKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class AppMouseKeyboard : MonoBehaviour
InputTrackerMouse _inputTrackerMouse;
InputTrackerKeyboard _inputTrackerKeyboard;

// Producer/Consumers
UIElementDrawer _uiElementDrawer;

// Application state
ConfigLoader _configLoader;
GfxReplayPlayer _gfxReplayPlayer;
Expand All @@ -47,6 +50,9 @@ public class AppMouseKeyboard : MonoBehaviour

protected void Awake()
{
// Initialize Producer/Consumers.
_uiElementDrawer = new UIElementDrawer(_camera);

// Initialize IKeyframeMessageConsumers.
_serverKeyframeIdHandler = new ServerKeyframeIdHandler();
_guiDrawer = new GuiDrawer(_appConfig, _camera);
Expand All @@ -63,6 +69,7 @@ protected void Awake()
_cameraTransformHandler,
_textRenderer,
_loadingScreenOverlay,
_uiElementDrawer,
};

// Initialize IClientStateProducers.
Expand All @@ -72,6 +79,7 @@ protected void Awake()
{
_inputTrackerMouse,
_inputTrackerKeyboard,
_uiElementDrawer,
};

// Initialize application state.
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scripts/ClientState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ClientState
public MouseInputData mouse;
public Dictionary<string, string> connection_params_dict;
public int? recentServerKeyframeId = null;
public UIElements ui;
}

[Serializable]
Expand Down Expand Up @@ -59,3 +60,10 @@ public class MouseInputData
public float[] rayOrigin = new float[3];
public float[] rayDirection = new float[3];
}

[Serializable]
public class UIElements
{
// Collection of UI buttons that were pressed since the last client message.
public List<string> buttonsPressed = new List<string>();
}
6 changes: 6 additions & 0 deletions Assets/Scripts/InputTrackerKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public class InputTrackerKeyboard : IClientStateProducer

public InputTrackerKeyboard()
{
// Don't update if paused.
if (LoadProgressTracker.Instance.IsApplicationPaused)
{
return;
}

// Bake the dict into an array for faster lookups.
_keyMap = new int[KEY_COUNT];
foreach (var kv in KEY_MAP)
Expand Down
6 changes: 6 additions & 0 deletions Assets/Scripts/InputTrackerMouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public InputTrackerMouse(Camera camera)

public void Update()
{
// Don't update if paused.
if (LoadProgressTracker.Instance.IsApplicationPaused)
{
return;
}

// Record all mouse actions that occurred OnEndFrame() call.
// Note that multiple Unity frames may occur during that time.
int mouseIndex = 0;
Expand Down
17 changes: 17 additions & 0 deletions Assets/Scripts/Keyframe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class Message
public List<TextMessage> texts;
public AbsTransform camera;
public int serverKeyframeId;
public Dialog dialog;
}

[Serializable]
Expand Down Expand Up @@ -141,3 +142,19 @@ public class TextMessage
public string text;
public List<float> position;
}

[Serializable]
public class Dialog
{
public string title;
public string text;
public Button[] buttons;
}

[Serializable]
public class Button
{
public string id;
public string text;
public bool enabled;
}
12 changes: 12 additions & 0 deletions Assets/Scripts/LoadProgressTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class LoadProgressTracker
uint _successCount = 0;
uint _failureCount = 0;

// TODO: Refactor.
public bool _modalDialogueShown = false;

/// <summary>
/// Get the LoadProgressTracker singleton.
/// </summary>
Expand Down Expand Up @@ -62,6 +65,15 @@ public float EstimateProgress()
return _progress;
}

/// <summary>
/// Whether the client is paused. True if loading or modal dialogue shown.
/// TODO: Either move out or rename this class.
/// </summary>
public bool IsApplicationPaused { get
{
return IsLoading || _modalDialogueShown;
}}

/// <summary>
/// Whether at least one asset is being loaded.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/TextRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TextRendererGUI : MonoBehaviour

public void OnGUI()
{
if (texts == null || LoadProgressTracker.Instance.IsLoading)
if (texts == null || LoadProgressTracker.Instance.IsApplicationPaused)
{
return;
}
Expand Down
116 changes: 116 additions & 0 deletions Assets/Scripts/UIElementDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class UIElementDrawer : IKeyframeMessageConsumer, IClientStateProducer
{
/// <summary>
/// Modal dialogue with buttons.
/// </summary>
class ModalDialogue : MonoBehaviour
{
Camera _camera = null;
Dialog _dialog = null;
HashSet<string> _clickedButtons = new HashSet<string>();

public List<string> ClickedButtons { get { return _clickedButtons.ToList(); } }

public void Reset()
{
_clickedButtons.Clear();
}

public void Initialize(Camera camera)
{
_camera = camera;
}

public void UpdateState(Dialog dialog)
{
_dialog = dialog;

// TODO: Cleanup
LoadProgressTracker.Instance._modalDialogueShown = dialog != null;
}

void OnGUI()
{
if (_dialog == null || _camera == null || LoadProgressTracker.Instance.IsLoading)
{
return;
}

// TODO: Size
int width = _camera.pixelWidth;
int height = _camera.pixelHeight;
int windowWidth = width / 3;
int windowHeight = width / 5;
int windowXOffset = windowWidth - (windowWidth / 2);
int windowYOffset = windowHeight - (windowHeight / 2);

GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUI.color = Color.white;
GUILayout.Window(
9, // TODO
new Rect(16, 16, 0, 0),
WindowUpdate,
_dialog.title,
GUILayout.Width(256)
);
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}

void WindowUpdate(int windowId)
{
GUILayout.BeginVertical();
GUI.color = Color.white;
GUILayout.Label(_dialog.text);

GUILayout.BeginHorizontal();
foreach (var button in _dialog.buttons)
{
GUI.enabled = button.enabled;
bool clicked = GUILayout.Button(button.text);
if (clicked)
{
_clickedButtons.Add(button.id);
}
GUI.enabled = true;
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
}

UIElements _uiElements = new UIElements();

ModalDialogue _modalDialogue;

public UIElementDrawer(Camera camera)
{
_modalDialogue = new GameObject("LoadingScreenOverlay").AddComponent<ModalDialogue>();
_modalDialogue.Initialize(camera);
}

public void ProcessMessage(Message message)
{
Dialog dialog = message.dialog;
_modalDialogue.UpdateState(dialog);
}

void IUpdatable.Update() {}

public void UpdateClientState(ref ClientState state)
{
_uiElements.buttonsPressed = _modalDialogue.ClickedButtons;
state.ui = _uiElements;
}

public void OnEndFrame()
{
_modalDialogue.Reset();
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/UIElementDrawer.cs.meta

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

0 comments on commit e6bb099

Please sign in to comment.