Project

General

Profile

Download (4.89 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedAttachDaemon.java @ 8baa1fe6

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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 cancelAttachJobs(DistortedAttacheable a, DistortedEffects e)
89
    {
90
    int num = mJobs.size();
91
    Job job;
92

    
93
    for(int i=0; i<num; i++)
94
      {
95
      job = mJobs.get(i);
96

    
97
      if( job.type == ATTACH && job.attacheable==a )
98
        {
99
        DistortedEffects effects = job.object.getEffects();
100

    
101
        if( effects.getID() == e.getID() )
102
          {
103
          mJobs.remove(i);
104
          break;
105
          }
106
        }
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  static void attach(DistortedAttacheable a, DistortedNode n)
113
    {
114
    mJobs.add(new Job(ATTACH,a,n));
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  static void detach(DistortedAttacheable a, DistortedNode n)
120
    {
121
    mJobs.add(new Job(DETACH,a,n));
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  static void detachAll(DistortedAttacheable a)
127
    {
128
    mJobs.add(new Job(DETACHALL,a,null));
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  static void onDestroy()
134
    {
135
    mJobs.clear();
136
    }
137
  }
(2-2/23)