Project

General

Profile

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

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

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
  static void onDestroy()
49
    {
50
    mNextID =  0;
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// PUBLIC API
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
/**
57
 * Create empty effect queue.
58
 */
59
  public DistortedEffects()
60
    {
61
    mID = ++mNextID;
62
    mQueues = new EffectQueue[EffectType.LENGTH];
63
    EffectQueue.allocateQueues(mQueues,null,0,mID);
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Copy constructor.
69
 * <p>
70
 * Whatever we do not clone gets created just like in the default constructor.
71
 *
72
 * @param dc    Source object to create our object from
73
 * @param flags A bitmask of values specifying what to copy.
74
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
75
 */
76
  public DistortedEffects(DistortedEffects dc, int flags)
77
    {
78
    mID = ++mNextID;
79
    mQueues = new EffectQueue[EffectType.LENGTH];
80
    EffectQueue.allocateQueues(mQueues,dc.getQueues(),flags,mID);
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
/**
85
 * Returns unique ID of this instance.
86
 *
87
 * @return ID of the object.
88
 */
89
  public long getID()
90
      {
91
      return mID;
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
/**
96
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
97
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
98
 * 
99
 * @param el A class implementing the EffectListener interface that wants to get notifications.
100
 */
101
  @SuppressWarnings("unused")
102
  public void registerForMessages(EffectListener el)
103
    {
104
    for( int i=0; i<EffectType.LENGTH; i++)
105
      {
106
      mQueues[i].registerForMessages(el);
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
/**
112
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
113
 * 
114
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
115
 */
116
  @SuppressWarnings("unused")
117
  public void deregisterForMessages(EffectListener el)
118
    {
119
    for( int i=0; i<EffectType.LENGTH; i++)
120
      {
121
      mQueues[i].deregisterForMessages(el);
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
/**
127
 * Aborts all Effects.
128
 * @return Number of effects aborted.
129
 */
130
  public int abortAllEffects()
131
    {
132
    int aborted = 0;
133

    
134
    for( int i=0; i<EffectType.LENGTH; i++)
135
      {
136
      aborted += mQueues[i].abortAll(true);
137
      }
138

    
139
    return aborted;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
/**
144
 * Aborts all Effects of a given type, for example all MATRIX Effects.
145
 * 
146
 * @param type one of the constants defined in {@link EffectType}
147
 * @return Number of effects aborted.
148
 */
149
  public int abortByType(EffectType type)
150
    {
151
    int num = type.ordinal();
152
    return mQueues[num].abortAll(true);
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
/**
157
 * Aborts an Effect by its ID.
158
 *
159
 * @param id the Id of the Effect to be removed, as returned by getID().
160
 * @return Number of effects aborted.
161
 */
162
  public int abortById(long id)
163
    {
164
    int num = (int)(id&EffectType.MASK);
165
    return mQueues[num].removeById(id);
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
/**
170
 * Aborts a single Effect.
171
 * 
172
 * @param effect the Effect we want to abort.
173
 * @return number of Effects aborted. Always either 0 or 1.
174
 */
175
  public int abortEffect(Effect effect)
176
    {
177
    int num = effect.getType().ordinal();
178
    return mQueues[num].removeEffect(effect);
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
/**
183
 * Abort all Effects of a given name, for example all rotations.
184
 * 
185
 * @param name one of the constants defined in {@link EffectName}
186
 * @return number of Effects aborted.
187
 */
188
  public int abortByName(EffectName name)
189
    {
190
    int num = name.getType().ordinal();
191
    return mQueues[num].removeByName(name);
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
/**
196
 * Add a new Effect to our queue.
197
 *
198
 * @param effect The Effect to add.
199
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
200
 */
201
  public boolean apply(Effect effect)
202
    {
203
    int num = effect.getType().ordinal();
204
    return mQueues[num].add(effect);
205
    }
206
  }
(4-4/19)