Game.UI.Editor.WaterPanelSystem
Assembly: Assembly-CSharp (typical for Cities: Skylines 2 mods)
Namespace: Game.UI.Editor
Type: class WaterPanelSystem
Base: EditorPanelSystemBase
Summary:
WaterPanelSystem implements the in-editor UI for configuring water sources and water simulation settings in the editor mode. It builds the editor widgets for constant-rate sources, constant-level sources and border (river/sea) sources, reads existing WaterSource entities from the ECS world into an editable config, and writes/configures ECS water source entities when the user changes values. The system also exposes water simulation speed toggles and manages tool activation (switching to the WaterToolSystem when the player uses keyboard & mouse). It uses an EndFrameBarrier to issue EntityCommandBuffer operations when applying changes.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Used to create an EntityCommandBuffer for deferred entity operations (adding/updating/removing water source entities) at end-of-frame. -
private ToolSystem m_ToolSystem
Reference to the global ToolSystem; used to query/change the active tool (activate/deactivate the water tool and prefab tools). -
private WaterToolSystem m_WaterToolSystem
Reference to the water editing tool system used for direct water editing in the editor. -
private WaterSystem m_WaterSystem
Reference to the simulation WaterSystem which holds settings such as WaterSimSpeed and calculation helpers (e.g., CalculateSourceMultiplier). -
private TerrainSystem m_TerrainSystem
Reference to terrain services (sampling heights, bounds, and position offsets) used when initializing/placing water sources. -
private EntityQuery m_WaterSourceQuery
Query for existing water source entities (reads Game.Simulation.WaterSourceData and Game.Objects.Transform, excludes prefab/Deleted/Temp). Used to fetch existing water sources into the editor UI. -
private EntityQuery m_UpdatedSourceQuery
Query that detects updated/deleted WaterSourceData components (All=WaterSourceData, Any=Updated/Deleted, None=PrefabRef/Temp). Used to auto-refresh the UI when world water sources change. -
private EntityArchetype m_WaterSourceArchetype
Archetype used when creating new water source entities (contains WaterSourceData and Transform components). -
private WaterConfig m_Config = new WaterConfig()
Local editable configuration backing the UI. Contains lists of ConstantRateWaterSource, ConstantLevelWaterSource and BorderWaterSource instances that are edited by the UI. -
private static readonly int[] kWaterSpeedValues = new int[7] { 0, 1, 8, 16, 32, 64, 128 }
Available water simulation speeds shown as toggles in the UI. Each toggle maps to WaterSystem.WaterSimSpeed.
Properties
- (No public properties defined on this type; the system relies on fields and inherited behavior.)
Constructors
public WaterPanelSystem()
Default constructor. The system is marked with [Preserve] in lifecycle methods to ensure calls are kept by code stripping. Initialization of fields and full setup is performed in OnCreate.
Methods
-
protected override void OnCreate()
: System.Void
Lifecycle method. Resolves required systems (EndFrameBarrier, ToolSystem, WaterToolSystem, WaterSystem, TerrainSystem), creates entity queries and archetype, builds the editor UI widgets (an EditorGenerator is used to build the WaterConfig UI) and sets up the water simulation speed toggles. This method constructs the children widget tree for the panel. -
private IWidget[] BuildWaterSpeedToggles()
: IWidget[]
Creates a ToggleField widget for each value in kWaterSpeedValues. Each toggle shows a "Nx" label (e.g., "8x") and uses a DelegateAccessor to get/set WaterSystem.WaterSimSpeed. Selecting a toggle sets the WaterSimSpeed to the corresponding value. -
protected override void OnGameLoaded(Context serializationContext)
: System.Void
Called when a game is loaded in the editor. Invokes FetchWaterSources() to populate the editor UI with current world water sources. -
protected override void OnStartRunning()
: System.Void
When the editor panel becomes active, if the current input scheme is KeyboardAndMouse, sets the active tool to the WaterToolSystem so the user can edit water with mouse input. -
protected override void OnUpdate()
: System.Void
Checks m_UpdatedSourceQuery and, if not empty, calls FetchWaterSources() to refresh the UI when water sources change in the world. -
protected override void OnStopRunning()
: System.Void
When the panel is closed or the system stops running, deactivates the water tool (if it is the active tool) and resets WaterSystem.WaterSimSpeed back to 1. -
protected override bool OnCancel()
: System.Boolean
Handles cancel/back action. If the water tool is active, deactivates it and returns false to consume the cancel; otherwise falls back to base.OnCancel(). -
protected override void OnValueChanged(IWidget widget)
: System.Void
Called when any bound UI value changes; triggers ApplyWaterSources() to write the modified config back into ECS entities. -
public void FetchWaterSources()
: System.Void
Reads existing water source entities from the world and populates m_Config lists. Uses m_WaterSourceQuery to obtain arrays of Game.Simulation.WaterSourceData and Game.Objects.Transform, then converts entities into editable WaterConfig.* entries. The mapping from WaterSourceData.m_ConstantDepth is: - 0 => ConstantRateWaterSource (m_Amount => m_Rate)
- 1 => ConstantLevelWaterSource (m_Amount => m_Height)
- 2 => Border river (BorderWaterSource; m_Multiplier => m_FloodHeight)
-
3 => Border sea (BorderWaterSource) NativeArrays are allocated with Allocator.TempJob and disposed in a finally block.
-
private void ApplyWaterSources()
: System.Void
Applies the edited m_Config lists back to the world. Creates an EntityCommandBuffer from m_EndFrameBarrier and enumerates existing source entities (m_WaterSourceQuery); for each configured source it either updates an existing entity or creates a new one (GetSource). After processing configured items, any remaining entities are marked Deleted. Uses m_TerrainSystem.GetHeightData() for height calculations. -
private Entity GetSource(EntityCommandBuffer buffer, NativeArray<Entity> sources, ref int sourceCount)
: Entity
Helper that returns an existing source entity from the sources array if available (incrementing sourceCount) or creates a new entity from m_WaterSourceArchetype using the EntityCommandBuffer. -
private void AddSource(EntityCommandBuffer buffer, Entity entity, WaterConfig.WaterSource source, int constantDepth, ref float amount, ref TerrainHeightData terrainHeightData)
: System.Void
Generic routine for adding/updating a non-border water source (constant-rate or constant-level). If the source is not initialized, it will: - Use the camera pivot as initial position
- Clamp the position to the terrain bounds (so the source is on the map)
-
Set default radius and amount depending on constantDepth (0: rate, 1: level) It then computes Game.Simulation.WaterSourceData including calling WaterSystem.CalculateSourceMultiplier, and sets the entity's WaterSourceData and Transform components using the buffer.
-
private void AddBorderSource(EntityCommandBuffer buffer, Entity entity, WaterConfig.BorderWaterSource source, int constantDepth, ref TerrainHeightData terrainHeightData)
: System.Void
Similar to AddSource but for border sources (river/sea). Special initialization logic ensures the source is placed near or outside the map edge depending on radius and then sets m_Height, m_Radius and m_Multiplier properly. Writes WaterSourceData and Transform to the entity. -
(Nested types) WaterConfig and contained classes:
- WaterConfig holds editable lists for ConstantRateWaterSource, ConstantLevelWaterSource, BorderWaterSource.
- WaterSource (base) contains m_Initialized, m_Position (float2), m_Radius and m_Pollution.
- ConstantRateWaterSource has m_Rate.
- ConstantLevelWaterSource has m_Height.
- BorderWaterSource (inherits ConstantLevelWaterSource) adds m_FloodHeight (NonSerialized inspector field).
- WaterSourcePositionFactory implements IFieldBuilderFactory and creates a small widget column with a Float2InputField and a "Locate" Button which centers the editor camera on the chosen float2 position. The Locate method sets CameraUpdateSystem.activeCameraController.pivot = new Vector3(x,0,y).
Usage Example
// Typical patterns used in this system are already implemented in the class.
// Example: override OnCreate to set up references and build UI (simplified form).
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Resolve systems
m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_WaterSystem = World.GetOrCreateSystemManaged<WaterSystem>();
m_TerrainSystem = World.GetOrCreateSystemManaged<TerrainSystem>();
// Build UI (EditorGenerator usage is inside the actual implementation)
title = "Editor.WATER";
children = new IWidget[]
{
// ... create scrollable editor sections and toggles ...
};
}
Notes and tips: - The system maps m_ConstantDepth values of existing WaterSourceData to editor types (0: rate, 1: level, 2: border river, 3: border sea). - When ApplyWaterSources uses an EndFrameBarrier-created EntityCommandBuffer, changes are committed at the end of the frame to avoid structural changes during system update. - FetchWaterSources uses Allocator.TempJob for component array copies—these arrays are disposed in a finally block to avoid leaks. - The UI includes toggles for water simulation speeds: 0x (probably paused), 1x (real-time), 8x, 16x, 32x, 64x, 128x. Selecting one sets WaterSystem.WaterSimSpeed.