Files
OpenRA/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorTabsLogic.cs
let5sne.win10 9cf6ebb986
Some checks failed
Continuous Integration / Linux (.NET 8.0) (push) Has been cancelled
Continuous Integration / Windows (.NET 8.0) (push) Has been cancelled
Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:46:54 +08:00

101 lines
3.0 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class MapEditorTabsLogic : ChromeLogic
{
enum MenuType { Select, Tiles, Layers, Actors, Tools, History }
readonly World world;
readonly Widget panelContainer;
readonly Widget tabContainer;
readonly EditorViewportControllerWidget editor;
MenuType menuType = MenuType.Tiles;
MenuType lastSelectedTab = MenuType.Tiles;
public static event Action OnTabChanged;
[ObjectCreator.UseCtor]
public MapEditorTabsLogic(Widget widget, World world)
{
this.world = world;
panelContainer = widget.Parent;
tabContainer = widget.Get("MAP_EDITOR_TAB_CONTAINER");
editor = widget.Parent.Parent.Get<EditorViewportControllerWidget>("MAP_EDITOR");
editor.DefaultBrush.UpdateSelectedTab += HandleUpdateSelectedTab;
SetupTab("SELECT_TAB", "SELECT_WIDGETS", MenuType.Select);
SetupTab("TILES_TAB", "TILE_WIDGETS", MenuType.Tiles);
SetupTab("OVERLAYS_TAB", "LAYER_WIDGETS", MenuType.Layers);
SetupTab("ACTORS_TAB", "ACTOR_WIDGETS", MenuType.Actors);
SetupTab("TOOLS_TAB", "TOOLS_WIDGETS", MenuType.Tools);
SetupTab("HISTORY_TAB", "HISTORY_WIDGETS", MenuType.History);
}
protected override void Dispose(bool disposing)
{
editor.DefaultBrush.UpdateSelectedTab -= HandleUpdateSelectedTab;
base.Dispose(disposing);
}
void SetupTab(string buttonId, string tabId, MenuType tabType)
{
var tab = tabContainer.Get<ButtonWidget>(buttonId);
tab.IsHighlighted = () => menuType == tabType;
tab.OnClick = () =>
{
if (tabType != MenuType.Select)
lastSelectedTab = tabType;
menuType = tabType;
OnTabChanged?.Invoke();
// Clear keyboard focus when switching tabs.
Ui.KeyboardFocusWidget = null;
};
// Selection tab is special, it can only be selected if a selection exists.
if (tabType == MenuType.Select)
tab.IsDisabled = () => !editor.DefaultBrush.Selection.HasSelection;
if (tabType == MenuType.Tools)
{
var toolsAvailable = world.WorldActor.TraitsImplementing<IEditorTool>().Any();
tab.IsDisabled = () => !toolsAvailable;
}
var container = panelContainer.Get<ContainerWidget>(tabId);
container.IsVisible = () => menuType == tabType;
}
void HandleUpdateSelectedTab()
{
var hasSelection = editor.DefaultBrush.Selection.HasSelection;
if (menuType != MenuType.Select && hasSelection)
menuType = MenuType.Select;
else if (menuType == MenuType.Select && !hasSelection)
menuType = lastSelectedTab;
OnTabChanged?.Invoke();
}
}
}