/
TableBuilder.java
284 lines (249 loc) · 10.5 KB
/
TableBuilder.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2014-2017 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kotcrab.vis.ui.building;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntArray;
import com.kotcrab.vis.ui.building.utilities.CellWidget;
import com.kotcrab.vis.ui.building.utilities.CellWidget.CellWidgetBuilder;
import com.kotcrab.vis.ui.building.utilities.Padding;
import com.kotcrab.vis.ui.building.utilities.layouts.ActorLayout;
import com.kotcrab.vis.ui.building.utilities.layouts.TableLayout;
/**
* Allows to easily build Scene2D tables, without having to worry about different colspans of table's rows.
* Table built using this helper class will have the same amount of cells in each row. CellWidget class allows
* to store cell's settings, and thanks to that - even the most complex tables can be built using one of the
* TableBuilders.
* @author MJ
*/
public abstract class TableBuilder {
private final static int DEFAULT_WIDGETS_AMOUNT = 10, DEFAULT_ROWS_AMOUNT = 3;
private final Array<CellWidget<? extends Actor>> widgets;
private final IntArray rowSizes;
// Control variables.
private int currentRowSize;
// Settings.
private final Padding widgetPadding;
private Padding tablePadding;
public TableBuilder () {
this(DEFAULT_WIDGETS_AMOUNT, DEFAULT_ROWS_AMOUNT, Padding.PAD_0);
}
/** @param defaultWidgetPadding will be applied to all added widgets if no specific padding is given. */
public TableBuilder (final Padding defaultWidgetPadding) {
this(DEFAULT_WIDGETS_AMOUNT, DEFAULT_ROWS_AMOUNT, defaultWidgetPadding);
}
public TableBuilder (final int estimatedWidgetsAmount, final int estimatedRowsAmount) {
this(estimatedWidgetsAmount, estimatedRowsAmount, Padding.PAD_0);
}
/** @param defaultWidgetPadding will be applied to all added widgets if no specific padding is given. */
public TableBuilder (final int estimatedWidgetsAmount, final int estimatedRowsAmount,
final Padding defaultWidgetPadding) {
widgets = new Array<CellWidget<? extends Actor>>(estimatedWidgetsAmount);
rowSizes = new IntArray(estimatedRowsAmount);
widgetPadding = defaultWidgetPadding;
}
/** @return the greatest common denominator of two values. */
public static int getGreatestCommonDenominator (final int valueA, final int valueB) {
return valueB == 0 ? valueA : getGreatestCommonDenominator(valueB, valueA % valueB);
}
/** @return lowest common multiple for the given two values. */
public static int getLowestCommonMultiple (final int valueA, final int valueB) {
return valueA * (valueB / getGreatestCommonDenominator(valueA, valueB));
}
/**
* @param values cannot be empty or null.
* @return lowest common multiple for the given values.
*/
public static int getLowestCommonMultiple (final IntArray values) {
int lowestCommonMultiple = values.first();
for (int index = 1; index < values.size; index++) {
lowestCommonMultiple = getLowestCommonMultiple(lowestCommonMultiple, values.get(index));
}
return lowestCommonMultiple;
}
/**
* @param tablePadding will define the amount of pixels separating widgets from the table's borders. Can be
* null - nulled padding will be ignored.
*/
public TableBuilder setTablePadding (final Padding tablePadding) {
this.tablePadding = tablePadding;
return this;
}
/**
* @return default widgets' padding. Should be applied to cells that have not specified custom padding
* setting.
*/
protected Padding getDefaultWidgetPadding () {
return widgetPadding;
}
/** @param widget will be added to the table with current default table's padding. */
public TableBuilder append (final Actor widget) {
return append(CellWidget.of(widget).padding(widgetPadding).wrap());
}
/** @param widget will be added to the table with custom provided data. */
public TableBuilder append (final CellWidget<? extends Actor> widget) {
widgets.add(widget);
currentRowSize++;
return this;
}
/** @param widgets will be converted into one cell, with widgets appended into one row. */
public TableBuilder append (final Actor... widgets) {
return append(TableLayout.HORIZONTAL, widgets);
}
/**
* @param widgets will be converted into one cell, with widgets appended into one row. Note that these
* CellWidgets' settings are local to the merging widget and additional data might have to be
* passed. See methods that consume CellWidgetBuilder.
*/
public TableBuilder append (final CellWidget<?>... widgets) {
return append(TableLayout.HORIZONTAL, widgets);
}
/**
* @param layout will determine how widgets are converted into one cell. See TableLayout for default
* implementations.
* @param widgets will be converted into one cell using passed layout.
*/
public TableBuilder append (final ActorLayout layout, final Actor... widgets) {
return append(layout.convertToActor(widgets));
}
/**
* @param layout will determine how widgets are converted into one cell. See TableLayout for default
* implementations.
* @param widgets will be converted into one cell using passed layout. Note that some (or all) CellWidget
* settings might be ignored, depending on the implementation of ActorLayout. Default layouts
* use TableBuilders, so they do not ignore (most of) passed data. Also, these CellWidgets'
* settings are local to the merging widget and additional data might have to be passed. See
* methods that consume CellWidgetBuilder.
*/
public TableBuilder append (final ActorLayout layout, final CellWidget<?>... widgets) {
return append(layout.convertToActor(widgets));
}
/**
* @param mergedCellSettings its data will be applied to the cell that will contain passed widgets merged
* into one actor.
* @param widgets will be converted into one cell, with widgets appended into one row.
*/
public TableBuilder append (final CellWidgetBuilder<Actor> mergedCellSettings, final Actor... widgets) {
return append(TableLayout.HORIZONTAL, mergedCellSettings, widgets);
}
/**
* @param mergedCellSettings its data will be applied to the cell that will contain passed widgets merged
* into one actor.
* @param widgets will be converted into one cell, with widgets appended into one row.
*/
public TableBuilder append (final CellWidgetBuilder<Actor> mergedCellSettings,
final CellWidget<?>... widgets) {
return append(TableLayout.HORIZONTAL, mergedCellSettings, widgets);
}
/**
* @param layout will determine how widgets are converted into one cell. See TableLayout for default
* implementations.
* @param mergedCellSettings its data will be applied to the cell that will contain passed widgets merged
* into one actor.
* @param widgets will be converted into one cell using passed layout.
*/
public TableBuilder append (final ActorLayout layout, final CellWidgetBuilder<Actor> mergedCellSettings,
final Actor... widgets) {
return append(mergedCellSettings.widget(layout.convertToActor(widgets)).wrap());
}
/**
* @param layout will determine how widgets are converted into one cell. See TableLayout for default
* implementations.
* @param mergedCellSettings its data will be applied to the cell that will contain passed widgets merged
* into one actor.
* @param widgets will be converted into one cell using passed layout. Note that some (or all) CellWidget
* settings might be ignored, depending on the implementation of ActorLayout. Default layouts
* use TableBuilders, so they do not ignore (most of) passed data.
*/
public TableBuilder append (final ActorLayout layout, final CellWidgetBuilder<Actor> mergedCellSettings,
final CellWidget<?>... widgets) {
return append(mergedCellSettings.widget(layout.convertToActor(widgets)).wrap());
}
/**
* Appends an empty cell to the table. Equivalent to passing null to append methods or appending
* CellWidget.EMPTY/CellWidget.empty().
*/
public TableBuilder append () {
return append(CellWidget.EMPTY);
}
/**
* Changes the current row, starts another. If no widgets were appended since the last call, row() will be
* ignored.
*/
public TableBuilder row () {
if (currentRowSize != 0) {
rowSizes.add(currentRowSize);
currentRowSize = 0;
}
return this;
}
/** @return a new table with the appended widgets, with widgets added depending on the chosen builder type. */
public Table build () {
return build(new Table());
}
/**
* @return passed table with the appended widgets, with widgets added depending on the chosen builder type.
* Note that if the passed table is not empty, builder implementations do not have to ensure that
* the widgets are actually correctly appended.
*/
public <T extends Table> T build (final T table) {
prepareNewTable(table);
if (widgets.size == 0) {
// Table is empty; avoiding unnecessary operations.
return table;
} else {
fillTable(table);
return prepareBuiltTable(table);
}
}
private Table prepareNewTable (final Table table) {
validateRowSize();
if (tablePadding != null) {
return tablePadding.applyPadding(table);
}
return table;
}
/**
* Should fill the given table with the widgets appended to the builder. Widgets can be accessed with
* getWidget(index) and getWidgets() methods. Row sizes are already validated. There is at least one
* widget.
* @param table is a properly created Scene2D table. Will be packed and return after filling.
*/
protected abstract void fillTable (Table table);
private <T extends Table> T prepareBuiltTable (final T table) {
table.pack();
return table;
}
/** Will append a new row if any new widgets were passed to make sure that all widgets are honored. */
private void validateRowSize () {
if (currentRowSize != 0) {
row();
}
}
/** @return array with sizes of each row. */
protected IntArray getRowSizes () {
return rowSizes;
}
/** @return CellWidget with the given index in the widgets array. */
protected CellWidget<? extends Actor> getWidget (final int index) {
return widgets.get(index);
}
/** @return all CellWidgets appended to the builder. */
protected Array<CellWidget<? extends Actor>> getWidgets () {
return widgets;
}
}