Skip to content

Commit

Permalink
Selection info into Area selection tab
Browse files Browse the repository at this point in the history
Selection info into Area selection tab

add Resource counter and measure info into Area selection tab
  • Loading branch information
Porenutak authored and PunkPun committed Feb 3, 2024
1 parent 311d55f commit 4e031a6
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 5 deletions.
18 changes: 18 additions & 0 deletions OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ void UpdateNetWorth(string oldResourceType, int oldDensity, string newResourceTy
NetWorth += (newDensity + 1) * newResourceValue;
}

public int CalculateRegionValue(CellRegion sourceRegion)
{
var resourceValueInRegion = 0;
foreach (var cell in sourceRegion)
{
var mcell = cell.ToMPos(Map);
if (Map.Resources.Contains(mcell) && Map.Resources[mcell].Type != 0)
{
resourceValueInRegion++;
var rcell = Map.Resources[mcell];
if (ResourceTypesByIndex.TryGetValue(rcell.Type, out var resourceType) && resourceValues.TryGetValue(resourceType, out var resourceValuePerUnit))
resourceValueInRegion += Tiles[mcell].Density * resourceValuePerUnit;
}
}

return resourceValueInRegion;
}

protected virtual int CalculateCellDensity(ResourceLayerContents contents, CPos c)
{
var resources = Map.Resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
Expand All @@ -31,21 +32,29 @@ public class MapEditorSelectionLogic : ChromeLogic
readonly CheckboxWidget copyActorsCheckbox;
readonly EditorActorLayer editorActorLayer;

public LabelWidget RegionLabel;
public LabelWidget DimensionsLabel;
public LabelWidget DiagonalLabel;
public LabelWidget ResourceCounterLabel;

MapCopyFilters copyFilters = MapCopyFilters.All;
EditorClipboard? clipboard;

readonly IResourceLayer resourceLayer;

readonly EditorResourceLayer editorResourceLayer;

[ObjectCreator.UseCtor]
public MapEditorSelectionLogic(Widget widget, World world, WorldRenderer worldRenderer)
{
this.worldRenderer = worldRenderer;

editorActorLayer = world.WorldActor.Trait<EditorActorLayer>();
resourceLayer = world.WorldActor.Trait<IResourceLayer>();
editorResourceLayer = world.WorldActor.Trait<EditorResourceLayer>();

editor = widget.Get<EditorViewportControllerWidget>("MAP_EDITOR");

editor.DefaultBrush.SelectionChanged += HandleSelectionChanged;
var selectTabContainer = widget.Get("SELECT_WIDGETS");
actorEditPanel = selectTabContainer.Get<ContainerWidget>("ACTOR_EDIT_PANEL");
areaEditPanel = selectTabContainer.Get<ContainerWidget>("AREA_EDIT_PANEL");
Expand All @@ -65,6 +74,11 @@ public MapEditorSelectionLogic(Widget widget, World world, WorldRenderer worldRe
copyButton.OnClick = () => clipboard = CopySelectionContents();
copyButton.IsDisabled = () => editor.DefaultBrush.Selection.Area == null;

RegionLabel = areaEditPanel.Get<LabelWidget>("REGION_COUNTER_LABEL");
DimensionsLabel = areaEditPanel.Get<LabelWidget>("DIMENSION_COUNTER_LABEL");
DiagonalLabel = areaEditPanel.Get<LabelWidget>("DIAGONAL_COUNTER_LABEL");
ResourceCounterLabel = areaEditPanel.Get<LabelWidget>("RESOURCES_COUNTER_LABEL");

var pasteButton = widget.Get<ButtonWidget>("PASTE_BUTTON");
pasteButton.OnClick = () =>
{
Expand Down Expand Up @@ -124,5 +138,30 @@ void CreateCategoryPanel(MapCopyFilters copyFilter, CheckboxWidget checkbox)
checkbox.IsVisible = () => true;
checkbox.OnClick = () => copyFilters ^= copyFilter;
}

protected override void Dispose(bool disposing)
{
editor.DefaultBrush.SelectionChanged -= HandleSelectionChanged;
base.Dispose(disposing);
}

void HandleSelectionChanged()
{
var selectedRegion = editor.DefaultBrush.Selection.Area;
if (selectedRegion == null)
return;
var selectionSize = selectedRegion.BottomRight - selectedRegion.TopLeft + new CPos(1, 1);
var diagonalLength = Math.Round(Math.Sqrt(Math.Pow(selectionSize.X, 2) + Math.Pow(selectionSize.Y, 2)), 3);
var resourceValueInRegion = editorResourceLayer.CalculateRegionValue(selectedRegion);
RegionLabel.GetText = () => $"{PositionAsString(selectedRegion.TopLeft)} : {PositionAsString(selectedRegion.BottomRight)}";
DimensionsLabel.GetText = () => PositionAsString(selectionSize);
DiagonalLabel.GetText = () => $"{diagonalLength}";
ResourceCounterLabel.GetText = () => $"{resourceValueInRegion}$";
}

static string PositionAsString(CPos cell)
{
return $"{cell.X},{cell.Y}";
}
}
}
68 changes: 66 additions & 2 deletions mods/cnc/chrome/editor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,73 @@ Container@EDITOR_WORLD_ROOT:
Width: PARENT_RIGHT - 29
Height: 20
Text: label-filter-actors
Label@AREA_INFO_TITLE:
X: 15
Y: 150
Width: 281
Height: 24
Align: Center
Font: Bold
Text: label-area-info
Label@REGION_LABEL:
X: 15
Y: 175
Width: 55
Height: 20
Font: Bold
Align: Left
Text: label-selected-area-selected-region
Label@DIMENSION_LABEL:
X: 15
Y: 200
Width: 55
Height: 25
Font: Bold
Align: Left
Text: label-selected-area-dimension
Label@DIAGONAL_LABEL:
X: 15
Y: 225
Width: 55
Height: 25
Font: Bold
Align: Left
Text: label-selected-area-diagonal
Label@RESOURCE_LABEL:
X: 15
Y: 250
Width: 55
Height: 25
Font: Bold
Align: Left
Text: label-selected-area-resources
Label@REGION_COUNTER_LABEL:
X: 150
Y: 175
Width: 55
Height: 22
Align: Left
Label@DIMENSION_COUNTER_LABEL:
X: 150
Y: 200
Width: 55
Height: 22
Align: Left
Label@DIAGONAL_COUNTER_LABEL:
X: 150
Y: 225
Width: 55
Height: 22
Align: Left
Label@RESOURCES_COUNTER_LABEL:
X: 150
Y: 250
Width: 55
Height: 22
Align: Left
Button@SELECTION_CANCEL_BUTTON:
X: 209
Y: 136
X: 107
Y: 275
Width: 75
Height: 25
Text: button-selection-cancel
Expand Down
5 changes: 5 additions & 0 deletions mods/cnc/languages/chrome/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ label-actors-bg-search = Search:
label-actors-bg-categories = Filter:
label-actors-bg-owners = Owner:
label-area-selection = Area Selection
label-area-info = Area Info
label-selected-area-selected-region = Region:
label-selected-area-dimension = Dimensions:
label-selected-area-diagonal = Diagonal:
label-selected-area-resources= Resources:
label-copy-filters = Copy Filters
label-filter-terrain = Terrain
label-filter-resources = Resources
Expand Down
68 changes: 66 additions & 2 deletions mods/common/chrome/editor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,73 @@ Container@EDITOR_WORLD_ROOT:
Width: PARENT_RIGHT - 29
Height: 20
Text: label-filter-actors
Label@AREA_INFO_TITLE:
X: 15
Y: 150
Width: 281
Height: 24
Align: Center
Font: Bold
Text: label-area-info
Label@REGION_LABEL:
X: 15
Y: 175
Width: 55
Height: 20
Font: Bold
Align: Left
Text: label-selected-area-selected-region
Label@DIMENSION_LABEL:
X: 15
Y: 200
Width: 55
Height: 25
Font: Bold
Align: Left
Text: label-selected-area-dimension
Label@DIAGONAL_LABEL:
X: 15
Y: 225
Width: 55
Height: 25
Font: Bold
Align: Left
Text: label-selected-area-diagonal
Label@RESOURCE_LABEL:
X: 15
Y: 250
Width: 55
Height: 25
Font: Bold
Align: Left
Text: label-selected-area-resources
Label@REGION_COUNTER_LABEL:
X: 150
Y: 175
Width: 55
Height: 22
Align: Left
Label@DIMENSION_COUNTER_LABEL:
X: 150
Y: 200
Width: 55
Height: 22
Align: Left
Label@DIAGONAL_COUNTER_LABEL:
X: 150
Y: 225
Width: 55
Height: 22
Align: Left
Label@RESOURCES_COUNTER_LABEL:
X: 150
Y: 250
Width: 55
Height: 22
Align: Left
Button@SELECTION_CANCEL_BUTTON:
X: 222
Y: 145
X: 117
Y: 275
Width: 75
Height: 25
Text: button-selection-cancel
Expand Down
5 changes: 5 additions & 0 deletions mods/common/languages/chrome/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ label-actors-bg-search = Search:
label-actors-bg-categories = Filter:
label-actors-bg-owners = Owner:
label-area-selection = Area Selection
label-area-info = Area Info
label-selected-area-selected-region = Region:
label-selected-area-dimension = Dimensions:
label-selected-area-diagonal = Diagonal:
label-selected-area-resources= Resources:
label-copy-filters = Copy Filters
label-filter-terrain = Terrain
label-filter-resources = Resources
Expand Down

0 comments on commit 4e031a6

Please sign in to comment.