-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBannedContentDialog.java
210 lines (182 loc) · 7.54 KB
/
BannedContentDialog.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package mindustry.editor;
import arc.*;
import arc.func.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.scene.style.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*;
import arc.struct.*;
import arc.util.*;
import mindustry.ctype.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.type.*;
import mindustry.ui.*;
import mindustry.ui.dialogs.*;
import mindustry.world.*;
import static mindustry.Vars.*;
public class BannedContentDialog<T extends UnlockableContent> extends BaseDialog{
private final ContentType type;
private Table selectedTable;
private Table deselectedTable;
private ObjectSet<T> contentSet;
private final Boolf<T> pred;
private String contentSearch;
private Category selectedCategory;
private Seq<T> filteredContent;
public BannedContentDialog(String title, ContentType type, Boolf<T> pred){
super(title);
this.type = type;
this.pred = pred;
contentSearch = "";
selectedTable = new Table();
deselectedTable = new Table();
addCloseButton();
shown(this::build);
resized(this::build);
}
public void show(ObjectSet<T> contentSet){
this.contentSet = contentSet;
show();
}
public void build(){
cont.clear();
var cell = cont.table(t -> {
t.table(s -> {
s.label(() -> "@search").padRight(10);
var field = s.field(contentSearch, value -> {
contentSearch = value.trim().replaceAll(" +", " ").toLowerCase();
rebuildTables();
}).get();
s.button(Icon.cancel, Styles.emptyi, () -> {
contentSearch = "";
field.setText("");
rebuildTables();
}).padLeft(10f).size(35f);
});
if(type == ContentType.block){
t.row();
t.table(c -> {
c.marginTop(8f);
c.defaults().marginRight(4f);
for(Category category : Category.values()){
c.button(ui.getIcon(category.name()), Styles.squareTogglei, () -> {
if(selectedCategory == category){
selectedCategory = null;
}else{
selectedCategory = category;
}
rebuildTables();
}).size(45f).update(i -> i.setChecked(selectedCategory == category)).padLeft(4f);
}
c.add("").padRight(4f);
}).center();
}
});
cont.row();
if(!Core.graphics.isPortrait()) cell.colspan(2);
filteredContent = content.<T>getBy(type).select(pred);
if(!contentSearch.isEmpty()) filteredContent.removeAll(content -> !content.localizedName.toLowerCase().contains(contentSearch.toLowerCase()));
cont.table(table -> {
if(type == ContentType.block){
table.add("@bannedblocks").color(Color.valueOf("f25555")).padBottom(-1).top().row();
}else{
table.add("@bannedunits").color(Color.valueOf("f25555")).padBottom(-1).top().row();
}
table.image().color(Color.valueOf("f25555")).height(3f).padBottom(5f).fillX().expandX().top().row();
table.pane(table2 -> selectedTable = table2).fill().expand().row();
table.button("@addall", Icon.add, () -> {
contentSet.addAll(filteredContent);
rebuildTables();
}).disabled(button -> contentSet.toSeq().containsAll(filteredContent)).padTop(10f).bottom().fillX();
}).fill().expandY().uniform();
if(Core.graphics.isPortrait()) cont.row();
var cell2 = cont.table(table -> {
if(type == ContentType.block){
table.add("@unbannedblocks").color(Pal.accent).padBottom(-1).top().row();
}else{
table.add("@unbannedunits").color(Pal.accent).padBottom(-1).top().row();
}
table.image().color(Pal.accent).height(3f).padBottom(5f).fillX().top().row();
table.pane(table2 -> deselectedTable = table2).fill().expand().row();
table.button("@addall", Icon.add, () -> {
contentSet.removeAll(filteredContent);
rebuildTables();
}).disabled(button -> {
Seq<T> array = content.getBy(type);
array = array.copy();
array.removeAll(contentSet.toSeq());
return array.containsAll(filteredContent);
}).padTop(10f).bottom().fillX();
}).fill().expandY().uniform();
if(Core.graphics.isPortrait()){
cell2.padTop(10f);
}else{
cell2.padLeft(10f);
}
rebuildTables();
}
private void rebuildTables(){
filteredContent.clear();
filteredContent = content.getBy(type);
filteredContent = filteredContent.select(pred);
if(!contentSearch.isEmpty()) filteredContent.removeAll(content -> !content.localizedName.toLowerCase().contains(contentSearch.toLowerCase()));
if(type == ContentType.block){
filteredContent.removeAll(content -> selectedCategory != null && ((Block)content).category != selectedCategory);
}
rebuildTable(selectedTable, true);
rebuildTable(deselectedTable, false);
}
private void rebuildTable(Table table, boolean isSelected){
table.clear();
int cols;
if(Core.graphics.isPortrait()){
cols = Math.max(4, (int)((Core.graphics.getWidth() / Scl.scl() - 100f) / 50f));
}else{
cols = Math.max(4, (int)((Core.graphics.getWidth() / Scl.scl() - 300f) / 50f / 2));
}
if((isSelected && contentSet.isEmpty()) || (!isSelected && contentSet.size == content.<T>getBy(type).count(pred))){
table.add("@empty").width(50f * cols).padBottom(5f).get().setAlignment(Align.center);
}else{
Seq<T> array;
if(!isSelected){
array = content.getBy(type);
array = array.copy();
array.removeAll(contentSet.toSeq());
}else{
array = contentSet.toSeq();
}
array.sort();
array.removeAll(content -> !filteredContent.contains(content));
if(array.isEmpty()){
table.add("@empty").width(50f * cols).padBottom(5f).get().setAlignment(Align.center);
return;
}
int i = 0;
boolean requiresPad = true;
for(T content : array){
TextureRegion region = content.uiIcon;
ImageButton button = new ImageButton(Tex.whiteui, Styles.clearNonei);
button.getStyle().imageUp = new TextureRegionDrawable(region);
button.resizeImage(8 * 4f);
if(isSelected) button.clicked(() -> {
contentSet.remove(content);
rebuildTables();
});
else button.clicked(() -> {
contentSet.add(content);
rebuildTables();
});
table.add(button).size(50f).tooltip(content.localizedName);
if(++i % cols == 0){
table.row();
requiresPad = false;
}
}
if(requiresPad){
table.add("").padRight(50f * (cols - i));
}
}
}
}