-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
TileSelectorLogic.cs
131 lines (110 loc) · 3.89 KB
/
TileSelectorLogic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* 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.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class TileSelectorLogic : CommonSelectorLogic
{
class TileSelectorTemplate
{
public readonly TerrainTemplateInfo Template;
public readonly string[] Categories;
public readonly string[] SearchTerms;
public readonly string Tooltip;
public TileSelectorTemplate(TerrainTemplateInfo template)
{
Template = template;
Categories = template.Categories;
Tooltip = template.Id.ToString();
SearchTerms = new[] { Tooltip };
}
}
readonly TileSet tileset;
readonly TileSelectorTemplate[] allTemplates;
readonly EditorCursorLayer editorCursor;
[ObjectCreator.UseCtor]
public TileSelectorLogic(Widget widget, World world, WorldRenderer worldRenderer)
: base(widget, world, worldRenderer, "TILETEMPLATE_LIST", "TILEPREVIEW_TEMPLATE")
{
tileset = world.Map.Rules.TileSet;
allTemplates = tileset.Templates.Values.Select(t => new TileSelectorTemplate(t)).ToArray();
editorCursor = world.WorldActor.Trait<EditorCursorLayer>();
allCategories = allTemplates.SelectMany(t => t.Categories)
.Distinct()
.OrderBy(CategoryOrder)
.ToArray();
foreach (var c in allCategories)
{
SelectedCategories.Add(c);
FilteredCategories.Add(c);
}
SearchTextField.OnTextEdited = () =>
{
searchFilter = SearchTextField.Text.Trim();
FilteredCategories.Clear();
if (!string.IsNullOrEmpty(searchFilter))
FilteredCategories.AddRange(
allTemplates.Where(t => t.SearchTerms.Any(
s => s.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0))
.SelectMany(t => t.Categories)
.Distinct()
.OrderBy(CategoryOrder));
else
FilteredCategories.AddRange(allCategories);
InitializePreviews();
};
InitializePreviews();
}
int CategoryOrder(string category)
{
var i = tileset.EditorTemplateOrder.IndexOf(category);
return i >= 0 ? i : int.MaxValue;
}
protected override void InitializePreviews()
{
Panel.RemoveChildren();
if (!SelectedCategories.Any())
return;
foreach (var t in allTemplates)
{
if (!SelectedCategories.Overlaps(t.Categories))
continue;
if (!string.IsNullOrEmpty(searchFilter) && !t.SearchTerms.Any(s => s.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0))
continue;
var tileId = t.Template.Id;
var item = ScrollItemWidget.Setup(ItemTemplate,
() => editorCursor.Type == EditorCursorType.TerrainTemplate && editorCursor.TerrainTemplate.Id == tileId,
() => Editor.SetBrush(new EditorTileBrush(Editor, tileId, WorldRenderer)));
var preview = item.Get<TerrainTemplatePreviewWidget>("TILE_PREVIEW");
var template = tileset.Templates[tileId];
var grid = WorldRenderer.World.Map.Grid;
var bounds = WorldRenderer.Theater.TemplateBounds(template, grid.TileSize, grid.Type);
// Scale templates to fit within the panel
var scale = 1f;
while (scale * bounds.Width > ItemTemplate.Bounds.Width)
scale /= 2;
preview.Template = template;
preview.GetScale = () => scale;
preview.Bounds.Width = (int)(scale * bounds.Width);
preview.Bounds.Height = (int)(scale * bounds.Height);
item.Bounds.Width = preview.Bounds.Width + 2 * preview.Bounds.X;
item.Bounds.Height = preview.Bounds.Height + 2 * preview.Bounds.Y;
item.IsVisible = () => true;
item.GetTooltipText = () => t.Tooltip;
Panel.AddChild(item);
}
}
}
}