Skip to content

Commit 28246cc

Browse files
committed
Sandbox refactoring.
1 parent 9132e90 commit 28246cc

File tree

5 files changed

+129
-100
lines changed

5 files changed

+129
-100
lines changed

LowLevel/Sandbox/Assets/Sandbox/Scripts/SandboxManager.cs

Lines changed: 107 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public float CameraZoom
2626
public UIDocument SceneOptionsUI { get; set; }
2727

2828
private bool ColorShapeState { get; set; }
29+
private ControlsMenu.CustomButton m_PausePlayButton;
30+
private ControlsMenu.CustomButton m_StepButton;
31+
private ControlsMenu.CustomButton m_DebugButton;
32+
private ControlsMenu.CustomButton m_UIButton;
2933
private ControlsMenu.CustomButton m_QuitButton;
3034

3135
public string StartScene = string.Empty;
@@ -181,69 +185,6 @@ private struct MenuDefaults
181185
public Color32 ShapeColorState => ColorShapeState ? new Color32() : new Color32((byte)m_Random.NextUInt(0, 256), (byte)m_Random.NextUInt(0, 256), (byte)m_Random.NextUInt(0, 256), 255);
182186
bool IShapeColorProvider.IsShapeColorActive => ColorShapeState;
183187

184-
public void ResetSceneState()
185-
{
186-
// Disable any "SceneBody".
187-
foreach (var sceneBody in FindObjectsByType<SceneBody>(FindObjectsInactive.Include, FindObjectsSortMode.None))
188-
sceneBody.enabled = false;
189-
190-
// Disable any "SceneWorlds".
191-
foreach (var sceneWorld in FindObjectsByType<SceneWorld>(FindObjectsSortMode.None))
192-
sceneWorld.enabled = false;
193-
194-
{
195-
var destroyBodies = new NativeList<PhysicsBody>(1000, Allocator.Temp);
196-
197-
// Iterate all worlds.
198-
using var allWorlds = PhysicsWorld.GetWorlds();
199-
foreach (var world in allWorlds)
200-
{
201-
// Iterate all non-owned bodies.
202-
using var bodies = world.GetBodies();
203-
foreach (var body in bodies)
204-
{
205-
if (!body.isOwned)
206-
destroyBodies.Add(body);
207-
}
208-
}
209-
210-
if (destroyBodies.Length > 0)
211-
PhysicsWorld.DestroyBodyBatch(destroyBodies.AsArray());
212-
213-
// Dispose.
214-
destroyBodies.Dispose();
215-
}
216-
217-
// Clear the debug draw.
218-
ClearDebugDraw();
219-
220-
// Reset random generator.
221-
m_Random.InitState(0x42424242);
222-
223-
// Reset the default world.
224-
PhysicsWorld.defaultWorld.Reset();
225-
226-
// Enable any "SceneWorlds".
227-
foreach (var sceneWorld in FindObjectsByType<SceneWorld>(FindObjectsSortMode.None))
228-
sceneWorld.enabled = true;
229-
230-
// Enable all bodies again.
231-
foreach (var sceneBody in FindObjectsByType<SceneBody>(FindObjectsInactive.Include, FindObjectsSortMode.None))
232-
sceneBody.enabled = true;
233-
}
234-
235-
private void OnEnable()
236-
{
237-
// We don't want this appearing all the time.
238-
UnityEngine.Rendering.DebugManager.instance.enableRuntimeUI = false;
239-
240-
m_DrawFlagElements = new Dictionary<PhysicsWorld.DrawOptions, Toggle>(capacity: 8);
241-
242-
// Overrides.
243-
m_OverrideDrawOptions = PhysicsWorld.DrawOptions.Off;
244-
m_OverridePreviousDrawOptions = PhysicsWorld.DrawOptions.Off;
245-
}
246-
247188
private void Start()
248189
{
249190
m_CameraManipulator = FindFirstObjectByType<CameraManipulator>();
@@ -258,8 +199,17 @@ private void Start()
258199

259200
// Reset the controls.
260201
ControlsMenu.ResetControls();
202+
m_PausePlayButton = ControlsMenu.pausePlayButton;
203+
m_StepButton = ControlsMenu.stepButton;
204+
m_DebugButton = ControlsMenu.debugButton;
205+
m_UIButton = ControlsMenu.uiButton;
261206
m_QuitButton = ControlsMenu.quitButton;
262-
207+
208+
m_PausePlayButton.button.clickable.clicked += TogglePauseContinue;
209+
m_StepButton.button.clickable.clicked += SingleStep;
210+
m_DebugButton.button.clickable.clicked += ToggleDebugging;
211+
m_UIButton.button.clickable.clicked += ToggleUI;
212+
263213
var defaultWorld = PhysicsWorld.defaultWorld;
264214
m_MenuDefaults = new MenuDefaults
265215
{
@@ -290,9 +240,21 @@ private void Start()
290240
m_UpdateTimeFPS = UpdatePeriodFPS;
291241
}
292242

243+
private void OnEnable()
244+
{
245+
// We don't want this appearing all the time.
246+
UnityEngine.Rendering.DebugManager.instance.enableRuntimeUI = false;
247+
248+
m_DrawFlagElements = new Dictionary<PhysicsWorld.DrawOptions, Toggle>(capacity: 8);
249+
250+
// Overrides.
251+
m_OverrideDrawOptions = PhysicsWorld.DrawOptions.Off;
252+
m_OverridePreviousDrawOptions = PhysicsWorld.DrawOptions.Off;
253+
}
254+
293255
private void Update()
294256
{
295-
// Keyboard Controls.
257+
// Controls.
296258
{
297259
var currentKeyboard = Keyboard.current;
298260

@@ -310,32 +272,13 @@ private void Update()
310272
// Toggle UI.
311273
if (currentKeyboard.tabKey.wasPressedThisFrame)
312274
{
313-
m_ShowUI = !m_ShowUI;
314-
315-
// Debugging Menu.
316-
if (m_ShowDebuggingElement.value)
317-
DebuggingMenu.gameObject.SetActive(m_ShowUI);
318-
319-
// Toggle Shortcut Menu.
320-
ShortcutMenu.gameObject.SetActive(!ShortcutMenu.gameObject.activeInHierarchy);
321-
322-
// Main Menu.
323-
m_MainMenuDocument.rootVisualElement.style.display = m_ShowUI ? DisplayStyle.Flex : DisplayStyle.None;
324-
325-
// If we have an assigned scene options UI then toggle it.
326-
if (SceneOptionsUI != null)
327-
SceneOptionsUI.enabled = m_ShowUI;
328-
275+
ToggleUI();
329276
return;
330277
}
331278

332279
// Single-Step.
333280
if (currentKeyboard.sKey.wasPressedThisFrame)
334281
{
335-
// Pause the world if we're not already paused.
336-
if (!WorldPaused)
337-
TogglePauseContinue();
338-
339282
// Single-step.
340283
SingleStep();
341284
}
@@ -349,7 +292,7 @@ private void Update()
349292
// Debugging.
350293
if (currentKeyboard.dKey.wasPressedThisFrame)
351294
{
352-
m_ShowDebuggingElement.value = !m_ShowDebuggingElement.value;
295+
ToggleDebugging();
353296
}
354297

355298
// Toggle Color PhysicsShape State.
@@ -389,6 +332,81 @@ private void Update()
389332
}
390333
}
391334

335+
private void ToggleUI()
336+
{
337+
m_ShowUI = !m_ShowUI;
338+
339+
// Debugging Menu.
340+
if (m_ShowDebuggingElement.value)
341+
DebuggingMenu.gameObject.SetActive(m_ShowUI);
342+
343+
// Toggle Shortcut Menu.
344+
ShortcutMenu.gameObject.SetActive(!ShortcutMenu.gameObject.activeInHierarchy);
345+
346+
// Main Menu.
347+
m_MainMenuDocument.rootVisualElement.style.display = m_ShowUI ? DisplayStyle.Flex : DisplayStyle.None;
348+
349+
// If we have an assigned scene options UI then toggle it.
350+
if (SceneOptionsUI != null)
351+
SceneOptionsUI.enabled = m_ShowUI;
352+
}
353+
354+
private void ToggleDebugging()
355+
{
356+
m_ShowDebuggingElement.value = !m_ShowDebuggingElement.value;
357+
}
358+
359+
public void ResetSceneState()
360+
{
361+
// Disable any "SceneBody".
362+
foreach (var sceneBody in FindObjectsByType<SceneBody>(FindObjectsInactive.Include, FindObjectsSortMode.None))
363+
sceneBody.enabled = false;
364+
365+
// Disable any "SceneWorlds".
366+
foreach (var sceneWorld in FindObjectsByType<SceneWorld>(FindObjectsSortMode.None))
367+
sceneWorld.enabled = false;
368+
369+
{
370+
var destroyBodies = new NativeList<PhysicsBody>(1000, Allocator.Temp);
371+
372+
// Iterate all worlds.
373+
using var allWorlds = PhysicsWorld.GetWorlds();
374+
foreach (var world in allWorlds)
375+
{
376+
// Iterate all non-owned bodies.
377+
using var bodies = world.GetBodies();
378+
foreach (var body in bodies)
379+
{
380+
if (!body.isOwned)
381+
destroyBodies.Add(body);
382+
}
383+
}
384+
385+
if (destroyBodies.Length > 0)
386+
PhysicsWorld.DestroyBodyBatch(destroyBodies.AsArray());
387+
388+
// Dispose.
389+
destroyBodies.Dispose();
390+
}
391+
392+
// Clear the debug draw.
393+
ClearDebugDraw();
394+
395+
// Reset random generator.
396+
m_Random.InitState(0x42424242);
397+
398+
// Reset the default world.
399+
PhysicsWorld.defaultWorld.Reset();
400+
401+
// Enable any "SceneWorlds".
402+
foreach (var sceneWorld in FindObjectsByType<SceneWorld>(FindObjectsSortMode.None))
403+
sceneWorld.enabled = true;
404+
405+
// Enable all bodies again.
406+
foreach (var sceneBody in FindObjectsByType<SceneBody>(FindObjectsInactive.Include, FindObjectsSortMode.None))
407+
sceneBody.enabled = true;
408+
}
409+
392410
private void SetupOptions()
393411
{
394412
var root = m_MainMenuDocument.rootVisualElement;
@@ -777,15 +795,14 @@ private void TogglePauseContinue()
777795

778796
private void SingleStep()
779797
{
798+
if (!WorldPaused)
799+
return;
800+
780801
var defaultWorld = PhysicsWorld.defaultWorld;
781802

782803
var oldPaused = defaultWorld.paused;
783804
var oldSimulationMode = defaultWorld.simulationMode;
784805

785-
// Pause thw world if we're not already paused.
786-
if (!WorldPaused)
787-
TogglePauseContinue();
788-
789806
// Update the worlds.
790807
using var worlds = PhysicsWorld.GetWorlds();
791808
foreach (var world in worlds)

LowLevel/Sandbox/Assets/Sandbox/UI/ControlsMenu/ControlsMenu.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void Reset()
3434
public bool isPressed { get; set; }
3535
}
3636

37+
public CustomButton pausePlayButton { get; private set; }
38+
public CustomButton stepButton { get; private set; }
39+
public CustomButton debugButton { get; private set; }
40+
public CustomButton uiButton { get; private set; }
3741
public CustomButton quitButton { get; private set; }
3842

3943
public CustomButton this[int index]
@@ -77,6 +81,10 @@ private void OnEnable()
7781
this[1] = new CustomButton(root.Q<Button>("button1"));
7882
this[2] = new CustomButton(root.Q<Button>("button2"));
7983

84+
pausePlayButton = new CustomButton(root.Q<Button>("pause-play"));
85+
stepButton = new CustomButton(root.Q<Button>("step"));
86+
debugButton = new CustomButton(root.Q<Button>("debug"));
87+
uiButton = new CustomButton(root.Q<Button>("ui"));
8088
quitButton = new CustomButton(root.Q<Button>("quit"));
8189

8290
ResetControls();

LowLevel/Sandbox/Assets/Sandbox/UI/ControlsMenu/ControlsMenu.uxml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
22
<Style src="project://database/Assets/Sandbox/UI/Styling/SandboxStyleOverrides.uss?fileID=7433441132597879392&amp;guid=e140b35909eadcf48959626f553b2145&amp;type=3#SandboxStyleOverrides"/>
33
<ui:VisualElement name="VisualElement" picking-mode="Ignore" style="width: 100%; height: 100%; align-items: flex-start; justify-content: flex-end; align-content: auto; padding-right: 8px; padding-bottom: 8px;">
4-
<ui:VisualElement name="menu-region" style="margin-top: 0; margin-right: 0; margin-bottom: 2px; margin-left: 0; align-items: auto; align-self: center; align-content: flex-end; width: auto; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; height: 6%; justify-content: center;">
5-
<ui:VisualElement name="controls" style="flex-grow: 1; flex-shrink: 0; color: rgba(210, 210, 210, 0.5); -unity-text-align: middle-left; align-self: stretch; padding-top: 3px; padding-left: 2px; padding-right: 2px; padding-bottom: 3px; flex-direction: row;">
6-
<ui:Button text="..." name="button0" enabled="true" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" enable-rich-text="false" emoji-fallback-support="false" focusable="false"/>
7-
<ui:Button text="..." name="button1" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" enable-rich-text="false" focusable="false"/>
8-
<ui:Button text="..." name="button2" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false"/>
9-
<ui:Button text="Quit" name="quit" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false"/>
4+
<ui:VisualElement name="menu-region" style="margin-top: 0; margin-right: 0; margin-bottom: 2px; margin-left: 0; align-items: auto; align-self: center; align-content: center; width: auto; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; height: 5%; justify-content: center;">
5+
<ui:VisualElement name="controls" style="flex-grow: 1; flex-shrink: 0; color: rgba(210, 210, 210, 0.5); -unity-text-align: middle-left; align-self: stretch; padding-top: 3px; padding-left: 2px; padding-right: 2px; padding-bottom: 3px; flex-direction: row; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; border-left-color: rgba(80, 80, 80, 0.9); border-right-color: rgba(80, 80, 80, 0.9); border-top-color: rgba(80, 80, 80, 0.9); border-bottom-color: rgba(80, 80, 80, 0.9);">
6+
<ui:Button text="..." name="button0" enabled="true" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" enable-rich-text="false" emoji-fallback-support="false" focusable="false" style="margin-left: 1px; margin-right: 1px;"/>
7+
<ui:Button text="..." name="button1" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" enable-rich-text="false" focusable="false" style="margin-left: 1px; margin-right: 1px;"/>
8+
<ui:Button text="..." name="button2" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false" style="margin-left: 1px; margin-right: 16px;"/>
9+
<ui:Button text="Pause/Play" name="pause-play" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false" style="margin-left: 1px; margin-right: 1px;"/>
10+
<ui:Button text="Step" name="step" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false" style="margin-left: 1px; margin-right: 1px;"/>
11+
<ui:Button text="Debug" name="debug" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false" style="margin-left: 1px; margin-right: 1px;"/>
12+
<ui:Button text="UI" name="ui" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false" style="margin-left: 1px; margin-right: 1px;"/>
13+
<ui:Button text="Quit" name="quit" enable-rich-text="false" emoji-fallback-support="false" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" focusable="false" style="margin-left: 16px;"/>
1014
</ui:VisualElement>
1115
</ui:VisualElement>
1216
</ui:VisualElement>

LowLevel/Sandbox/Assets/Sandbox/UI/MainMenu/MainMenu.uxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<ui:DropdownField label="Frequency" choices="15,30,60,120,Variable" index="2" name="frequency" focusable="false"/>
1717
<ui:Button text="Restart" name="restart" focusable="false" enabled="true"/>
1818
<ui:Button text="Pause/Continue (P)" name="pause-continue" focusable="false"/>
19-
<ui:Button text="Single-Step (S)" name="single-step" focusable="false" enabled="false"/>
19+
<ui:Button text="Single-Step (S)" name="single-step" focusable="false" enabled="true"/>
2020
<ui:Button text="Quit " name="quit-application" focusable="false"/>
2121
</ui:VisualElement>
2222
</ui:Tab>

LowLevel/Sandbox/Assets/Sandbox/UI/ShortcutsMenu/ShortcutMenu.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ private void OnEnable()
3131
$"[{color}C{endColor}]\t\tToggle Color State\n" +
3232
$"[{color}1{endColor}]\t\tLeft Mouse Drag\n" +
3333
$"[{color}2{endColor}]\t\tLeft Mouse Explode\n" +
34-
$"[{color}D{endColor}]\t\tToggle Debugging\n" +
35-
$"[{color}TAB{endColor}]\t\tToggle Overlays\n" +
34+
$"[{color}D{endColor}]\t\tToggle Debug\n" +
35+
$"[{color}TAB{endColor}]\t\tToggle UI\n" +
3636
$"[{color}LMB+LCtrl{endColor}]\tPan\n" +
3737
$"[{color}LMB+Move{endColor}]\t\tDrag/Explode\n" +
3838
$"[{color}Mouse Wheel{endColor}]\tZoom\n" +
3939
$"[{color}Arrows+Space{endColor}]\tMisc Interaction\n" +
40-
$"[{color}ESC{endColor}]\t\tQuit (Player)\n";
40+
$"[{color}ESC{endColor}]\t\tQuit\n";
4141
}
4242
}
4343
}

0 commit comments

Comments
 (0)