1 |
c204c69d
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 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 |
|
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import java.util.ArrayList;
|
23 |
|
|
|
24 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
/**
|
26 |
|
|
* This static class handles changing topology of a Tree in a safe way. I.e. if one wants to
|
27 |
|
|
* attach() a new Node somewhere in the Tree, we cannot simply attach it mid-render (detach() is
|
28 |
|
|
* even worse!). What we do instead is mark that this job will have to be done and actually do it
|
29 |
|
|
* just before the next render. That's what this Daemon does.
|
30 |
|
|
*/
|
31 |
|
|
class DistortedAttachDaemon
|
32 |
|
|
{
|
33 |
|
|
private static final int ATTACH = 0; //
|
34 |
|
|
private static final int DETACH = 1; // types of jobs that the Daemon can do
|
35 |
|
|
private static final int DETACHALL = 2; //
|
36 |
|
|
|
37 |
|
|
private static class Job
|
38 |
|
|
{
|
39 |
|
|
int type;
|
40 |
|
|
DistortedAttacheable attacheable;
|
41 |
|
|
DistortedNode object;
|
42 |
|
|
|
43 |
|
|
Job(int t, DistortedAttacheable a, DistortedNode o)
|
44 |
|
|
{
|
45 |
|
|
type = t;
|
46 |
|
|
attacheable = a;
|
47 |
|
|
object = o;
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
private static ArrayList<Job> mJobs = new ArrayList<>();
|
52 |
|
|
|
53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
|
|
private DistortedAttachDaemon()
|
56 |
|
|
{
|
57 |
|
|
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61 |
|
|
|
62 |
|
|
static boolean toDo()
|
63 |
|
|
{
|
64 |
|
|
int num = mJobs.size();
|
65 |
|
|
Job job;
|
66 |
|
|
|
67 |
|
|
for(int i=0; i<num; i++)
|
68 |
|
|
{
|
69 |
|
|
job = mJobs.remove(0);
|
70 |
|
|
|
71 |
|
|
switch(job.type)
|
72 |
|
|
{
|
73 |
|
|
case ATTACH : job.attacheable.attachNow(job.object);
|
74 |
|
|
break;
|
75 |
|
|
case DETACH : job.attacheable.detachNow(job.object);
|
76 |
|
|
break;
|
77 |
|
|
case DETACHALL: job.attacheable.detachAllNow() ;
|
78 |
|
|
break;
|
79 |
|
|
default : android.util.Log.e("AttachDaemon", "what?");
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
return ( num>0 );
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87 |
|
|
|
88 |
|
|
static void attach(DistortedAttacheable a, DistortedNode n)
|
89 |
|
|
{
|
90 |
|
|
mJobs.add(new Job(ATTACH,a,n));
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
|
95 |
|
|
static void detach(DistortedAttacheable a, DistortedNode n)
|
96 |
|
|
{
|
97 |
|
|
mJobs.add(new Job(DETACH,a,n));
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
|
|
|
102 |
|
|
static void detachAll(DistortedAttacheable a)
|
103 |
|
|
{
|
104 |
|
|
mJobs.add(new Job(DETACHALL,a,null));
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108 |
|
|
|
109 |
|
|
static void onDestroy()
|
110 |
|
|
{
|
111 |
|
|
mJobs.clear();
|
112 |
|
|
}
|
113 |
|
|
}
|