1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
|
21
|
package org.distorted.library.main;
|
22
|
|
23
|
import org.distorted.library.mesh.MeshBase;
|
24
|
|
25
|
import java.util.ArrayList;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
|
29
|
class DistortedChildrenList implements DistortedMaster.Slave
|
30
|
{
|
31
|
private static final int ATTACH = 0;
|
32
|
private static final int DETACH = 1;
|
33
|
private static final int DETALL = 2;
|
34
|
private static final int SORT = 3;
|
35
|
|
36
|
private class Job
|
37
|
{
|
38
|
int type;
|
39
|
DistortedNode node;
|
40
|
|
41
|
Job(int t, DistortedNode n)
|
42
|
{
|
43
|
type = t;
|
44
|
node = n;
|
45
|
}
|
46
|
}
|
47
|
|
48
|
private ArrayList<Job> mJobs;
|
49
|
private ArrayList<DistortedNode> mChildren;
|
50
|
private DistortedChildrenList.Parent mParent;
|
51
|
private int mNumChildren;
|
52
|
|
53
|
public interface Parent
|
54
|
{
|
55
|
void adjustIsomorphism();
|
56
|
DistortedChildrenList getChildren();
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
DistortedChildrenList(DistortedChildrenList.Parent parent)
|
62
|
{
|
63
|
mParent = parent;
|
64
|
mJobs = new ArrayList<>();
|
65
|
mChildren = null;
|
66
|
mNumChildren = 0;
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
int getNumChildren()
|
72
|
{
|
73
|
return mNumChildren;
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
DistortedNode getChild(int index)
|
79
|
{
|
80
|
return mChildren.get(index);
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
void removeChild(DistortedNode node)
|
86
|
{
|
87
|
if( mChildren.remove(node) )
|
88
|
{
|
89
|
mNumChildren--;
|
90
|
}
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
// Can make this logarithmic but the typical number of children is very small anyway.
|
95
|
//
|
96
|
// We want to keep same buckets next to each other, while avoiding changes in order of the children
|
97
|
// (if possible!) We want to keep bucket=0 (i.e. the non-postprocessed children) at the beginning.
|
98
|
|
99
|
void addSortingByBuckets(DistortedNode newChild)
|
100
|
{
|
101
|
int i;
|
102
|
EffectQueue queue = newChild.getEffects().getQueues()[3];
|
103
|
long bucket = queue.getID();
|
104
|
boolean sameBucket = false;
|
105
|
|
106
|
for(i=0; i<mNumChildren; i++)
|
107
|
{
|
108
|
queue = mChildren.get(i).getEffects().getQueues()[3];
|
109
|
|
110
|
if( queue.getID() == bucket )
|
111
|
{
|
112
|
sameBucket=true;
|
113
|
}
|
114
|
else if( sameBucket || bucket==0 )
|
115
|
{
|
116
|
break;
|
117
|
}
|
118
|
}
|
119
|
|
120
|
mChildren.add(i,newChild);
|
121
|
mNumChildren++;
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
void attach(DistortedNode node)
|
127
|
{
|
128
|
mJobs.add(new Job(ATTACH,node));
|
129
|
DistortedMaster.newSlave(this);
|
130
|
}
|
131
|
|
132
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133
|
|
134
|
DistortedNode attach(DistortedSurface surface, DistortedEffects effects, MeshBase mesh)
|
135
|
{
|
136
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
137
|
mJobs.add(new Job(ATTACH,node));
|
138
|
DistortedMaster.newSlave(this);
|
139
|
return node;
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
void detach(DistortedNode node)
|
145
|
{
|
146
|
mJobs.add(new Job(DETACH,node));
|
147
|
DistortedMaster.newSlave(this);
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
void detach(DistortedEffects effects)
|
153
|
{
|
154
|
long id = effects.getID();
|
155
|
DistortedNode node;
|
156
|
boolean detached = false;
|
157
|
|
158
|
for(int i=0; i<mNumChildren; i++)
|
159
|
{
|
160
|
node = mChildren.get(i);
|
161
|
|
162
|
if( node.getEffects().getID()==id )
|
163
|
{
|
164
|
detached = true;
|
165
|
mJobs.add(new Job(DETACH,node));
|
166
|
DistortedMaster.newSlave(this);
|
167
|
break;
|
168
|
}
|
169
|
}
|
170
|
|
171
|
if( !detached )
|
172
|
{
|
173
|
// if we failed to detach any, it still might be the case that
|
174
|
// there's an ATTACH job that we need to cancel.
|
175
|
int num = mJobs.size();
|
176
|
Job job;
|
177
|
|
178
|
for(int i=0; i<num; i++)
|
179
|
{
|
180
|
job = mJobs.get(i);
|
181
|
|
182
|
if( job.type==ATTACH && job.node.getEffects()==effects )
|
183
|
{
|
184
|
mJobs.remove(i);
|
185
|
break;
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
void detachAll()
|
194
|
{
|
195
|
mJobs.add(new Job(DETALL,null));
|
196
|
DistortedMaster.newSlave(this);
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
/**
|
201
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
202
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
203
|
* Java has no multiple inheritance.
|
204
|
*
|
205
|
* @y.exclude
|
206
|
*/
|
207
|
public void doWork()
|
208
|
{
|
209
|
int num = mJobs.size();
|
210
|
|
211
|
if( num>0 )
|
212
|
{
|
213
|
Job job;
|
214
|
int numChanges=0;
|
215
|
|
216
|
for(int i=0; i<num; i++)
|
217
|
{
|
218
|
job = mJobs.remove(0);
|
219
|
|
220
|
switch(job.type)
|
221
|
{
|
222
|
case ATTACH: numChanges++;
|
223
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
224
|
job.node.setParent(mParent);
|
225
|
addSortingByBuckets(job.node);
|
226
|
break;
|
227
|
case DETACH: numChanges++;
|
228
|
if( mNumChildren>0 && mChildren.remove(job.node) )
|
229
|
{
|
230
|
job.node.setParent(null);
|
231
|
mNumChildren--;
|
232
|
}
|
233
|
break;
|
234
|
case DETALL: numChanges++;
|
235
|
if( mNumChildren>0 )
|
236
|
{
|
237
|
DistortedNode tmp;
|
238
|
|
239
|
for(int j=mNumChildren-1; j>=0; j--)
|
240
|
{
|
241
|
tmp = mChildren.remove(j);
|
242
|
tmp.setParent(null);
|
243
|
}
|
244
|
|
245
|
mNumChildren = 0;
|
246
|
}
|
247
|
break;
|
248
|
case SORT : mChildren.remove(job.node);
|
249
|
addSortingByBuckets(job.node);
|
250
|
break;
|
251
|
}
|
252
|
}
|
253
|
if( numChanges>0 ) mParent.adjustIsomorphism();
|
254
|
}
|
255
|
}
|
256
|
}
|
257
|
|