summaryrefslogtreecommitdiffstats
path: root/src/demos/xtrans/XTBasicTransitionManager.java
blob: 85a94fb2827b201f9c1bbb010b4059e83f7ed036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package demos.xtrans;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import gleem.linalg.*;

/** A basic transition manager supporting animated scrolling, rotating
    and fading of components. */

public class XTBasicTransitionManager implements XTTransitionManager {
  /** Indicates the style of the transition (either no motion,
      scrolling, or rotating). */
  public static class Style {
    private Style() {}
  }

  /** Indicates the component has no motion (scrolling or rotation) in
      its animation. */
  public static Style STYLE_NO_MOTION = new Style();

  /** Indicates the component is to be scrolled in to or out of
      place. */
  public static Style STYLE_SCROLL    = new Style();

  /** Indicates the component is to be rotated in to or out of
      place. */
  public static Style STYLE_ROTATE    = new Style();

  /** Indicates the direction of the transition if it contains any
      motion (either up, down, left, or right). */
  public static class Direction {
    private Direction() {}
  }

  /** Indicates the component's animation is from or toward the left,
      depending on whether the transition is an "in" or "out"
      transition. */
  public static Direction DIR_LEFT  = new Direction();

  /** Indicates the component's animation is from or toward the right,
      depending on whether the transition is an "in" or "out"
      transition. */
  public static Direction DIR_RIGHT = new Direction();

  /** Indicates the component's animation is in the upward
      direction. */
  public static Direction DIR_UP    = new Direction();

  /** Indicates the component's animation is in the downward
      direction. */
  public static Direction DIR_DOWN  = new Direction();

  private Style     nextTransitionStyle;
  private Direction nextTransitionDirection;
  private boolean   nextTransitionFade;

  private Random random;

  /** Sets the next transition to be used by this transition manager
      for either an "in" or an "out" transition. By default the
      transition manager selects random transitions from those
      available. */
  public void setNextTransition(Style style,
                                Direction direction,
                                boolean fade) {
    if (style == null) {
      throw new IllegalArgumentException("Must supply a style");
    }
    nextTransitionStyle     = style;
    nextTransitionDirection = direction;
    nextTransitionFade      = fade;
  }

  /** Creates an XTBasicTransition for the given component. By default
      this transition manager chooses a random transition from those
      available if one is not specified via {@link #setNextTransition
      setNextTransition}. */
  public XTTransition createTransitionForComponent(Component c,
                                                   boolean isAddition,
                                                   Rectangle   oglViewportOfDesktop,
                                                   Point       viewportOffsetFromOrigin,
                                                   Rectangle2D oglTexCoordsOnBackBuffer) {
    if (nextTransitionStyle == null) {
      chooseRandomTransition();
    }

    // Figure out the final positions of everything
    // Keep in mind that the Java2D origin is at the upper left and
    // the OpenGL origin is at the lower left
    Rectangle bounds = c.getBounds();
    int x = bounds.x;
    int y = bounds.y;
    int w = bounds.width;
    int h = bounds.height;
    float tx = (float) oglTexCoordsOnBackBuffer.getX();
    float ty = (float) oglTexCoordsOnBackBuffer.getY();
    float tw = (float) oglTexCoordsOnBackBuffer.getWidth();
    float th = (float) oglTexCoordsOnBackBuffer.getHeight();
    float vx = oglViewportOfDesktop.x;
    float vy = oglViewportOfDesktop.y;
    float vw = oglViewportOfDesktop.width;
    float vh = oglViewportOfDesktop.height;
    Quad3f verts = new Quad3f(new Vec3f(0,  0, 0),
                              new Vec3f(0, -h, 0),
                              new Vec3f(w, -h, 0),
                              new Vec3f(w,  0, 0));
    Quad2f texcoords = new Quad2f(new Vec2f(tx,      ty + th),
                                  new Vec2f(tx,      ty),
                                  new Vec2f(tx + tw, ty),
                                  new Vec2f(tx + tw, ty + th));

    XTBasicTransition trans = new XTBasicTransition();

    Vec3f translation = new Vec3f(x - viewportOffsetFromOrigin.x,
                                  vh - y - viewportOffsetFromOrigin.y,
                                  0);
    InterpolatedVec3f transInterp = new InterpolatedVec3f();
    transInterp.setStart(translation);
    transInterp.setEnd(translation);

    InterpolatedQuad3f quadInterp = new InterpolatedQuad3f();
    quadInterp.setStart(verts);
    quadInterp.setEnd(verts);

    InterpolatedQuad2f texInterp = new InterpolatedQuad2f();
    texInterp.setStart(texcoords);
    texInterp.setEnd(texcoords);

    trans.setTranslation(transInterp);
    trans.setVertices(quadInterp);
    trans.setTexCoords(texInterp);

    // Now decide how we are going to handle this transition
    Style     transitionStyle     = nextTransitionStyle;
    Direction transitionDirection = nextTransitionDirection;
    boolean   fade                = nextTransitionFade;
    nextTransitionStyle = null;
    nextTransitionDirection = null;
    nextTransitionFade = false;

    int[] vtIdxs = null;
    int[] ttIdxs = null;
    Vec3f rotAxis = null;
    Vec3f pivot = null;
    float startAngle = 0;
    float endAngle = 0;

    if (fade) {
      InterpolatedFloat alpha = new InterpolatedFloat();
      float start = (isAddition ? 0.0f : 1.0f);
      float end   = (isAddition ? 1.0f : 0.0f);
      alpha.setStart(start);
      alpha.setEnd(end);
      trans.setAlpha(alpha);
    }

    if (transitionDirection != null) {
      if (transitionStyle == STYLE_SCROLL) {
        if (transitionDirection == DIR_LEFT) {
          vtIdxs = new int[] { 3, 2, 2, 3 };
          ttIdxs = new int[] { 0, 1, 1, 0 };
        } else if (transitionDirection == DIR_RIGHT) {
          vtIdxs = new int[] { 0, 1, 1, 0 };
          ttIdxs = new int[] { 3, 2, 2, 3 };
        } else if (transitionDirection == DIR_UP) {
          vtIdxs = new int[] { 1, 1, 2, 2 };
          ttIdxs = new int[] { 0, 0, 3, 3 };
        } else {
          // DIR_DOWN
          vtIdxs = new int[] { 0, 0, 3, 3 };
          ttIdxs = new int[] { 1, 1, 2, 2 };
        }
      } else if (transitionStyle == STYLE_ROTATE) {
        if (transitionDirection == DIR_LEFT) {
          rotAxis = new Vec3f(0, 1, 0);
          pivot = new Vec3f();
          startAngle = -90;
          endAngle = 0;
        } else if (transitionDirection == DIR_RIGHT) {
          rotAxis = new Vec3f(0, 1, 0);
          pivot = new Vec3f(w, 0, 0);
          startAngle = 90;
          endAngle = 0;
        } else if (transitionDirection == DIR_UP) {
          rotAxis = new Vec3f(1, 0, 0);
          pivot = new Vec3f(0, -h, 0);
          startAngle = 90;
          endAngle = 0;
        } else {
          // DIR_DOWN
          rotAxis = new Vec3f(1, 0, 0);
          pivot = new Vec3f();
          startAngle = -90;
          endAngle = 0;
        }
      }
    }


    /*
    switch (transitionType) {
      case FADE:
      {
        InterpolatedFloat alpha = new InterpolatedFloat();
        float start = (isAddition ? 0.0f : 1.0f);
        float end   = (isAddition ? 1.0f : 0.0f);
        alpha.setStart(start);
        alpha.setEnd(end);
        trans.setAlpha(alpha);
        break;
      }
      case SCROLL_LEFT:
      {
        vtIdxs = new int[] { 3, 2, 2, 3 };
        ttIdxs = new int[] { 0, 1, 1, 0 };
        break;
      }
      case SCROLL_RIGHT:
      {
        vtIdxs = new int[] { 0, 1, 1, 0 };
        ttIdxs = new int[] { 3, 2, 2, 3 };
        break;
      }
      case SCROLL_UP:
      {
        vtIdxs = new int[] { 1, 1, 2, 2 };
        ttIdxs = new int[] { 0, 0, 3, 3 };
        break;
      }
      case SCROLL_DOWN:
      {
        vtIdxs = new int[] { 0, 0, 3, 3 };
        ttIdxs = new int[] { 1, 1, 2, 2 };
        break;
      }
      case ROTATE_LEFT:
      {
        rotAxis = new Vec3f(0, 1, 0);
        pivot = new Vec3f();
        startAngle = -90;
        endAngle = 0;
        break;
      }
      case ROTATE_RIGHT:
      {
        rotAxis = new Vec3f(0, 1, 0);
        //        pivot = translation.plus(new Vec3f(w, 0, 0));
        pivot = new Vec3f(w, 0, 0);
        startAngle = 90;
        endAngle = 0;
        break;
      }
      case ROTATE_UP:
      {
        rotAxis = new Vec3f(1, 0, 0);
        //        pivot = translation.plus(new Vec3f(0, -h, 0));
        pivot = new Vec3f(0, -h, 0);
        startAngle = 90;
        endAngle = 0;
        break;
      }
      case ROTATE_DOWN:
      {
        rotAxis = new Vec3f(1, 0, 0);
        pivot = new Vec3f();
        startAngle = -90;
        endAngle = 0;
        break;
      }
    }

    */

    if (vtIdxs != null) {
      if (isAddition) {
        quadInterp.setStart(new Quad3f(verts.getVec(vtIdxs[0]),
                                       verts.getVec(vtIdxs[1]),
                                       verts.getVec(vtIdxs[2]),
                                       verts.getVec(vtIdxs[3])));
        texInterp.setStart(new Quad2f(texcoords.getVec(ttIdxs[0]),
                                      texcoords.getVec(ttIdxs[1]),
                                      texcoords.getVec(ttIdxs[2]),
                                      texcoords.getVec(ttIdxs[3])));
      } else {
        // Note: swapping the vertex and texture indices happens to
        // have the correct effect
        int[] tmp = vtIdxs;
        vtIdxs = ttIdxs;
        ttIdxs = tmp;

        quadInterp.setEnd(new Quad3f(verts.getVec(vtIdxs[0]),
                                     verts.getVec(vtIdxs[1]),
                                     verts.getVec(vtIdxs[2]),
                                     verts.getVec(vtIdxs[3])));
        texInterp.setEnd(new Quad2f(texcoords.getVec(ttIdxs[0]),
                                    texcoords.getVec(ttIdxs[1]),
                                    texcoords.getVec(ttIdxs[2]),
                                    texcoords.getVec(ttIdxs[3])));
      }
    } else if (rotAxis != null) {
      if (!isAddition) {
        float tmp = endAngle;
        endAngle = -startAngle;
        startAngle = tmp;
      }

      trans.setPivotPoint(pivot);
      trans.setRotationAxis(rotAxis);
      InterpolatedFloat rotInterp = new InterpolatedFloat();
      rotInterp.setStart(startAngle);
      rotInterp.setEnd(endAngle);
      trans.setRotationAngle(rotInterp);
    }

    return trans;
  }

  /** Chooses a random transition from those available. */
  protected void chooseRandomTransition() {
    if (random == null) {
      random = new Random();
    }
    nextTransitionFade = random.nextBoolean();
    nextTransitionStyle = null;
    do {
      int style = random.nextInt(3);
      switch (style) {
        // Make no-motion transitions always use fades for effect
        // without biasing transitions toward no-motion transitions
        case 0:   if (nextTransitionFade) nextTransitionStyle = STYLE_NO_MOTION; break;
        case 1:   nextTransitionStyle = STYLE_SCROLL;                            break;
        default:  nextTransitionStyle = STYLE_ROTATE;                            break;
      }
    } while (nextTransitionStyle == null);
    int dir = random.nextInt(4);
    switch (dir) {
      case 0:   nextTransitionDirection = DIR_LEFT;  break;
      case 1:   nextTransitionDirection = DIR_RIGHT; break;
      case 2:   nextTransitionDirection = DIR_UP;    break;
      default:  nextTransitionDirection = DIR_DOWN;  break;
    }
  }
}