Automatic switch between Player <-> Editor Tabs

+ Allow Editor to inject into Settings
This commit is contained in:
Simon Lübeß
2023-08-14 22:10:27 +02:00
parent f71e851ab5
commit a112fcc64a
4 changed files with 203 additions and 92 deletions
+29
View File
@@ -712,6 +712,20 @@ namespace GlitchyEditor
#region Scene Management #region Scene Management
/// Focuses the Play window, so that its tab will be shown.
private void SwitchToPlayWindow()
{
let window = ImGui.FindWindowByName(GameViewportWindow.s_WindowTitle);
ImGui.FocusWindow(window);
}
/// Focuses the Editor window, so that its tab will be shown.
private void SwitchToEditorWindow()
{
let window = ImGui.FindWindowByName(EditorViewportWindow.s_WindowTitle);
ImGui.FocusWindow(window);
}
/// Starts the play mode for the current scene /// Starts the play mode for the current scene
private void OnScenePlay() private void OnScenePlay()
{ {
@@ -728,6 +742,9 @@ namespace GlitchyEditor
} }
_editor.CurrentScene = _activeScene; _editor.CurrentScene = _activeScene;
if (Application.Instance.Settings.EditorSettings.SwitchToPlayerOnPlay)
SwitchToPlayWindow();
} }
/// Starts the physics simulation mode for the current scene /// Starts the physics simulation mode for the current scene
@@ -746,18 +763,27 @@ namespace GlitchyEditor
} }
_editor.CurrentScene = _activeScene; _editor.CurrentScene = _activeScene;
if (Application.Instance.Settings.EditorSettings.SwitchToPlayerOnSimulate)
SwitchToPlayWindow();
} }
/// Pauses the scene /// Pauses the scene
private void OnScenePause() private void OnScenePause()
{ {
_isPaused = true; _isPaused = true;
if (Application.Instance.Settings.EditorSettings.SwitchToEditorOnPause)
SwitchToEditorWindow();
} }
/// Resumes the simulation / game /// Resumes the simulation / game
private void OnSceneResume() private void OnSceneResume()
{ {
_isPaused = false; _isPaused = false;
if (Application.Instance.Settings.EditorSettings.SwitchToPlayerOnResume)
SwitchToPlayWindow();
} }
/// Requests execution of a single simulation / game tick /// Requests execution of a single simulation / game tick
@@ -792,6 +818,9 @@ namespace GlitchyEditor
*/ */
GameViewportSizeChanged(null, _editor.GameViewportWindow.ViewportSize); GameViewportSizeChanged(null, _editor.GameViewportWindow.ViewportSize);
} }
if (Application.Instance.Settings.EditorSettings.SwitchToEditorOnStop)
SwitchToEditorWindow();
} }
/// Stops the scene and cleans up the subsystems to allow loading another scene. /// Stops the scene and cleans up the subsystems to allow loading another scene.
+47
View File
@@ -0,0 +1,47 @@
using System;
using GlitchyEngine;
using Bon;
using GlitchyEditor;
using System.Collections;
namespace GlitchyEngine
{
extension Settings
{
[SettingContainer, BonInclude]
public readonly EditorSettings EditorSettings = new .() ~ delete _;
public this()
{
OnApplySettings.Add(new (s, e) => EditorSettings.Apply());
}
}
}
namespace GlitchyEditor;
[Reflect]
class EditorSettings
{
[Setting("Editor", "Switch to Player on play", "If checked the editor will automatically switch to the \"Play\" window after starting the game."), BonInclude]
public bool SwitchToPlayerOnPlay = true;
[Setting("Editor", "Switch to Player on simulate", "If checked the editor will automatically switch to the \"Play\" window after starting the simulation."), BonInclude]
public bool SwitchToPlayerOnSimulate = true;
[Setting("Editor", "Switch to Player on continue", "If checked the editor will automatically switch to the \"Play\" window when the game is continued after pausing."), BonInclude]
public bool SwitchToPlayerOnResume = false;
[Setting("Editor", "Switch to Editor on stop", "If checked the editor will automatically switch to the \"Editor\" window after stopping the game."), BonInclude]
public bool SwitchToEditorOnStop = true;
[Setting("Editor", "Switch to Editor on pause", "If checked the editor will automatically switch to the \"Editor\" window when the game is being paused."), BonInclude]
public bool SwitchToEditorOnPause = false;
//
//public readonly List<String> RecentProjects = new .() ~ delete _;
public void Apply()
{
}
}
+73 -44
View File
@@ -14,12 +14,14 @@ namespace GlitchyEditor
public Object SettingsObject; public Object SettingsObject;
public String Name ~ delete _; public String Name ~ delete _;
public String FieldName ~ delete _; public String FieldName ~ delete _;
public StringView Tooltip;
public this(StringView name, StringView fieldName, Object settingsObject) public this(StringView name, StringView fieldName, Object settingsObject, StringView tooltip)
{ {
Name = new String(name); Name = new String(name);
FieldName = new String(fieldName); FieldName = new String(fieldName);
SettingsObject = settingsObject; SettingsObject = settingsObject;
Tooltip = tooltip;
} }
} }
@@ -36,9 +38,9 @@ namespace GlitchyEditor
Header = new String(header); Header = new String(header);
} }
public void AddSetting(StringView name, StringView fieldName, Object settingsObject = null) public void AddSetting(StringView name, StringView fieldName, Object settingsObject = null, StringView tooltip = "")
{ {
Binding binding = new .(name, fieldName, settingsObject ?? SettingsObject); Binding binding = new .(name, fieldName, settingsObject ?? SettingsObject, tooltip);
_bindings.Add(binding); _bindings.Add(binding);
} }
} }
@@ -89,7 +91,7 @@ namespace GlitchyEditor
if (settingResult case .Ok(let settingInfo)) if (settingResult case .Ok(let settingInfo))
{ {
AddSetting(settingInfo.Category, settingInfo.Name, field.Name, container); AddSetting(settingInfo.Category, settingInfo.Name, field.Name, container, settingInfo.Tooltip);
} }
Result<SettingContainerAttribute> containerResult = field.GetCustomAttribute<SettingContainerAttribute>(); Result<SettingContainerAttribute> containerResult = field.GetCustomAttribute<SettingContainerAttribute>();
@@ -110,11 +112,11 @@ namespace GlitchyEditor
} }
} }
void AddSetting(String categoryName, String name, StringView fieldName, Object container) void AddSetting(String categoryName, String name, StringView fieldName, Object container, StringView tooltip)
{ {
Category category = AddCategory(categoryName); Category category = AddCategory(categoryName);
category.AddSetting(name, fieldName, container); category.AddSetting(name, fieldName, container, tooltip);
} }
protected override void InternalShow() protected override void InternalShow()
@@ -131,15 +133,66 @@ namespace GlitchyEditor
{ {
if (ImGui.BeginTabItem(category.Header)) if (ImGui.BeginTabItem(category.Header))
{ {
ImGui.Columns(2); ShowCategory(category);
defer ImGui.Columns(1); ImGui.EndTabItem();
ImGui.SetColumnWidth(0, 100); }
}
ImGui.EndTabBar();
ImGui.EndChild();
ImGui.BeginDisabled(!_settingsChanged);
if (ImGui.Button("Save"))
{
_settings.Save();
_settings.Apply();
_open = false;
}
ImGui.SameLine();
if (ImGui.Button("Apply"))
{
_settings.Apply();
}
ImGui.EndDisabled();
ImGui.SameLine();
if (ImGui.Button("Cancel"))
{
Settings.Load();
_open = false;
}
}
private void ShowCategory(Category category)
{
if (ImGui.BeginTable("SettingsTable", 2, .SizingFixedFit | .RowBg))
{
ImGui.TableSetupColumn("Settings");
ImGui.TableSetupColumn("Values", .WidthStretch);
ImGui.TableNextRow();
ImGui.TableSetColumnIndex(1);
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().x);
for (Binding setting in category._bindings) for (Binding setting in category._bindings)
{ {
ImGui.TableNextRow();
ImGui.TableNextColumn();
// Name of setting
ImGui.TextUnformatted(setting.Name); ImGui.TextUnformatted(setting.Name);
ImGui.NextColumn(); if (!setting.Tooltip.IsWhiteSpace)
ImGui.AttachTooltip(setting.Tooltip);
ImGui.TableNextColumn();
Type settingsObjectType = setting.SettingsObject.GetType(); Type settingsObjectType = setting.SettingsObject.GetType();
@@ -188,6 +241,15 @@ namespace GlitchyEditor
switch (fieldType) switch (fieldType)
{ {
case typeof(bool):
let value = GetSettingValue!<bool>();
if (!ImGui.Checkbox(scope $"##{setting.Name}", &value))
break;
SetSettingValue!(value);
_settingsChanged = true;
case typeof(int32): case typeof(int32):
int32 value = GetSettingValue!<int32>(); int32 value = GetSettingValue!<int32>();
@@ -211,42 +273,9 @@ namespace GlitchyEditor
_settingsChanged = true; _settingsChanged = true;
} }
} }
ImGui.NextColumn();
} }
ImGui.EndTabItem(); ImGui.EndTable();
}
}
ImGui.EndTabBar();
ImGui.EndChild();
ImGui.BeginDisabled(!_settingsChanged);
if (ImGui.Button("Save"))
{
_settings.Save();
_settings.Apply();
_open = false;
}
ImGui.SameLine();
if (ImGui.Button("Apply"))
{
_settings.Apply();
}
ImGui.EndDisabled();
ImGui.SameLine();
if (ImGui.Button("Cancel"))
{
Settings.Load();
_open = false;
} }
} }
} }
+8 -2
View File
@@ -23,11 +23,13 @@ namespace GlitchyEngine
{ {
public String Category; public String Category;
public String Name; public String Name;
public String Tooltip;
public this(String category, String name) public this(String category, String name, String tooltip = "")
{ {
Category = category; Category = category;
Name = name; Name = name;
Tooltip = tooltip;
} }
} }
@@ -38,6 +40,8 @@ namespace GlitchyEngine
[SettingContainer, BonInclude] [SettingContainer, BonInclude]
public readonly ImGuiSettings ImGuiSettings = new .() ~ delete _; public readonly ImGuiSettings ImGuiSettings = new .() ~ delete _;
#endif #endif
[BonIgnore]
public Event<EventHandler> OnApplySettings = .() ~ _.Dispose();
/* /*
[BonInclude] [BonInclude]
@@ -57,6 +61,8 @@ namespace GlitchyEngine
ImGuiSettings.Apply(); ImGuiSettings.Apply();
#endif #endif
OnApplySettings.Invoke(this, .Empty);
/*for (let settings in _userSettings) /*for (let settings in _userSettings)
{ {
settings.Apply(); settings.Apply();
@@ -119,7 +125,7 @@ namespace GlitchyEngine
public void Apply() public void Apply()
{ {
Application.Get().[Friend]_imGuiLayer.SettingsInvalid = true; Application.Instance.[Friend]_imGuiLayer.SettingsInvalid = true;
} }
} }
#endif #endif