Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ bfe45b4a

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.main;
21

    
22
import org.distorted.library.effect.Effect;
23
import org.distorted.library.effect.EffectName;
24
import org.distorted.library.effect.EffectType;
25
import org.distorted.library.message.EffectListener;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
30
 * <p>
31
 * The queues hold actual effects to be applied to a given (InputSurface,MeshBase) combo.
32
 */
33
public class DistortedEffects
34
  {
35
  private static long mNextID =0;
36
  private long mID;
37
  private EffectQueue[] mQueues;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
  EffectQueue[] getQueues()
42
    {
43
    return mQueues;
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  void newNode(DistortedNode node)
49
    {
50
    EffectQueue.newNode(mQueues,node);
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  void removeNode(DistortedNode node)
56
    {
57
    EffectQueue.removeNode(mQueues,node);
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  static void onDestroy()
63
    {
64
    mNextID =  0;
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
// PUBLIC API
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
/**
71
 * Create empty effect queue.
72
 */
73
  public DistortedEffects()
74
    {
75
    mID = ++mNextID;
76
    mQueues = new EffectQueue[EffectType.LENGTH];
77
    EffectQueue.allocateQueues(mQueues,null,0,mID);
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
/**
82
 * Copy constructor.
83
 * <p>
84
 * Whatever we do not clone gets created just like in the default constructor.
85
 *
86
 * @param dc    Source object to create our object from
87
 * @param flags A bitmask of values specifying what to copy.
88
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
89
 */
90
  public DistortedEffects(DistortedEffects dc, int flags)
91
    {
92
    mID = ++mNextID;
93
    mQueues = new EffectQueue[EffectType.LENGTH];
94
    EffectQueue.allocateQueues(mQueues,dc.getQueues(),flags,mID);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
/**
99
 * Returns unique ID of this instance.
100
 *
101
 * @return ID of the object.
102
 */
103
  public long getID()
104
      {
105
      return mID;
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
/**
110
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
111
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
112
 * 
113
 * @param el A class implementing the EffectListener interface that wants to get notifications.
114
 */
115
  @SuppressWarnings("unused")
116
  public void registerForMessages(EffectListener el)
117
    {
118
    for( int i=0; i<EffectType.LENGTH; i++)
119
      {
120
      mQueues[i].registerForMessages(el);
121
      }
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
/**
126
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
127
 * 
128
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
129
 */
130
  @SuppressWarnings("unused")
131
  public void deregisterForMessages(EffectListener el)
132
    {
133
    for( int i=0; i<EffectType.LENGTH; i++)
134
      {
135
      mQueues[i].deregisterForMessages(el);
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
/**
141
 * Aborts all Effects.
142
 * @return Number of effects aborted.
143
 */
144
  public int abortAllEffects()
145
    {
146
    int aborted = 0;
147

    
148
    for( int i=0; i<EffectType.LENGTH; i++)
149
      {
150
      aborted += mQueues[i].abortAll(true);
151
      }
152

    
153
    return aborted;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
/**
158
 * Aborts all Effects of a given type, for example all MATRIX Effects.
159
 * 
160
 * @param type one of the constants defined in {@link EffectType}
161
 * @return Number of effects aborted.
162
 */
163
  public int abortByType(EffectType type)
164
    {
165
    int num = type.ordinal();
166
    return mQueues[num].abortAll(true);
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
/**
171
 * Aborts an Effect by its ID.
172
 *
173
 * @param id the Id of the Effect to be removed, as returned by getID().
174
 * @return Number of effects aborted.
175
 */
176
  public int abortById(long id)
177
    {
178
    int num = (int)(id&EffectType.MASK);
179
    return mQueues[num].removeById(id);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
/**
184
 * Aborts a single Effect.
185
 * 
186
 * @param effect the Effect we want to abort.
187
 * @return number of Effects aborted. Always either 0 or 1.
188
 */
189
  public int abortEffect(Effect effect)
190
    {
191
    int num = effect.getType().ordinal();
192
    return mQueues[num].removeEffect(effect);
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
/**
197
 * Abort all Effects of a given name, for example all rotations.
198
 * 
199
 * @param name one of the constants defined in {@link EffectName}
200
 * @return number of Effects aborted.
201
 */
202
  public int abortByName(EffectName name)
203
    {
204
    int num = name.getType().ordinal();
205
    return mQueues[num].removeByName(name);
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
/**
210
 * Add a new Effect to our queue.
211
 *
212
 * @param effect The Effect to add.
213
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
214
 */
215
  public boolean apply(Effect effect)
216
    {
217
    int num = effect.getType().ordinal();
218
    return mQueues[num].add(effect);
219
    }
220
  }
(4-4/19)