-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxLib_GenericDataList.pde
More file actions
193 lines (155 loc) · 3.97 KB
/
Copy pathxLib_GenericDataList.pde
File metadata and controls
193 lines (155 loc) · 3.97 KB
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
// a set of tools used to manages list (planets, particles and so on)
class DataList extends GenericData
{
int current_index = 0;
ArrayList<GenericData> items = new ArrayList<GenericData>();
String sub_chapter_name = "";
DataList(String chapter_name, String sub_chapter_name)
{
super(chapter_name);
this.sub_chapter_name = sub_chapter_name;
}
int count()
{
return items.size();
}
void reset()
{
items.clear();
current_index = 0;
}
// must be overriden by real implementation
GenericData newItem()
{
return null;
}
public void LoadJson(JSONObject json) {
if (json == null) return;
int nb_items = json.getInt("nb_items", 0);
current_index = json.getInt("current_index", 0);
for (int i = 0; i < nb_items; i++)
{
GenericData item = newItem();
item.LoadJson(json.getJSONObject(sub_chapter_name + "_" + i));
items.add(item);
}
}
public JSONObject SaveJson() {
JSONObject json = new JSONObject();
int nb_items = items.size();
json.setInt("nb_items", nb_items);
for (int i = 0; i < nb_items; i++)
{
GenericData item = items.get(i);
json.setJSONObject(sub_chapter_name + "_"+i, item.SaveJson());
}
json.setInt("current_index", current_index);
return json;
}
}
class GUIListPanel extends GUIPanel
{
DataList data_list;
GUIListPanel(String pageName, DataList data)
{
super(pageName, data);
this.data_list = data;
}
int last_index = -1;
void updateCurrentItem()
{
// update content of the current item
}
void addListBar()
{
space();
addButton("Prev").plugTo(this, "prev");
xPos+= 20;
addButton("Move Prev").plugTo(this, "move_prev");
addButton("Remove").plugTo(this, "remove");
addButton("Add").plugTo(this, "add");
addButton("Move Next").plugTo(this, "move_next");
xPos+= 20;
addButton("Next").plugTo(this, "next");
nextLine();
space();
}
void prev()
{
if (data_list.count() == 0)
{
data_list.current_index = 0;
} else
{
data_list.current_index = data_list.current_index -1;
if (data_list.current_index < 0)
data_list.current_index = data_list.count() -1;
}
updateCurrentItem();
}
void next()
{
if (data_list.count() == 0)
{
data_list.current_index = 0;
} else
{
data_list.current_index = data_list.current_index + 1;
if (data_list.current_index >= data_list.count())
data_list.current_index = 0;
}
updateCurrentItem();
}
void fix_index()
{
if (data_list.current_index >= data_list.count())
data_list.current_index = data_list.count() -1;
else if (data_list.current_index < 0)
data_list.current_index = 0;
}
void add()
{
data_list.items.add(data_list.newItem());
data_list.current_index = data_list.items.size() -1;
last_index = -1;
updateCurrentItem();
}
void remove()
{
if (data_list.count() == 0)
return;
fix_index();
data_list.items.remove(data_list.current_index);
// print(data_list.current_index);
data_list.current_index--;
last_index = -1;
fix_index();
updateCurrentItem();
}
void move_prev()
{
if (data_list.count() == 0)
return;
if (data_list.current_index <= 0)
return;
var current = data_list.items.get(data_list.current_index);
data_list.items.remove(data_list.current_index);
data_list.current_index = data_list.current_index - 1;
data_list.items.add(data_list.current_index, current);
fix_index();
updateCurrentItem();
}
void move_next()
{
if (data_list.count() == 0)
return;
if (data_list.current_index >= data_list.count()-1)
return;
var current = data_list.items.get(data_list.current_index);
data_list.items.remove(data_list.current_index);
data_list.current_index = data_list.current_index + 1;
data_list.items.add(data_list.current_index, current);
fix_index();
updateCurrentItem();
}
}