source: trunk/src_apollo/org/apollo/items/FramePlaybackLauncher.java@ 315

Last change on this file since 315 was 315, checked in by bjn8, 16 years ago

Apollo spin-off added

File size: 4.2 KB
Line 
1package org.apollo.items;
2
3import java.awt.BasicStroke;
4import java.awt.Color;
5import java.awt.Graphics2D;
6import java.awt.Polygon;
7import java.awt.Stroke;
8
9import org.apollo.audio.SampledAudioManager;
10import org.apollo.audio.util.FrameLayoutDaemon;
11import org.apollo.audio.util.MultiTrackPlaybackController;
12import org.apollo.audio.util.SoundDesk;
13import org.apollo.audio.util.Timeline;
14import org.apollo.audio.util.MultiTrackPlaybackController.MultitrackLoadListener;
15import org.apollo.io.IconRepository;
16import org.apollo.util.AudioMath;
17import org.apollo.widgets.FramePlayer;
18import org.expeditee.gui.Browser;
19import org.expeditee.gui.DisplayIO;
20import org.expeditee.gui.Frame;
21import org.expeditee.items.Item;
22import org.expeditee.items.ItemParentStateChangedEvent;
23
24/**
25 * A transient widget that destroys self once anchored...
26 * once anchored it commences playback of a frame if there is something to play.
27 *
28 * @author Brook Novak
29 *
30 */
31public class FramePlaybackLauncher extends Item {
32
33 private static int WIDTH = 80;
34 private static int HEIGHT = 80;
35 private static Stroke BORDER_STOKE = new BasicStroke(1.0f);
36
37 public FramePlaybackLauncher() {
38 this.setID(getParentOrCurrentFrame().getNextItemID());
39 }
40
41 @Override
42 public Item copy() {
43 FramePlaybackLauncher copy = new FramePlaybackLauncher();
44
45 DuplicateItem(this, copy);
46
47 return copy;
48 }
49
50 @Override
51 public Item merge(Item merger, int mouseX, int mouseY) {
52 return null;
53 }
54
55 @Override
56 public void paint(Graphics2D g) {
57 if (Browser._theBrowser == null) return;
58
59 g.setColor(Color.LIGHT_GRAY);
60 g.fillRect((int)_x, (int)_y, WIDTH, HEIGHT);
61
62 g.setColor(Color.BLACK);
63 g.setStroke(BORDER_STOKE);
64 g.drawRect((int)_x, (int)_y, WIDTH, HEIGHT);
65
66 IconRepository.getIcon("frameplay.png").paintIcon(
67 Browser._theBrowser.getContentPane(), g, getX() + 25, getY() + 25);
68
69 }
70
71 @Override
72 public void setAnnotation(boolean val) {
73 }
74
75
76 @Override
77 public int getHeight() {
78 return HEIGHT;
79 }
80
81 @Override
82 public int getWidth() {
83 return WIDTH;
84 }
85
86 @Override
87 public void updatePolygon() {
88
89 _poly = new Polygon();
90
91 int x = (int)_x;
92 int y = (int)_y;
93
94 _poly.addPoint(x, y);
95 _poly.addPoint(x + WIDTH, y);
96 _poly.addPoint(x + WIDTH, y + HEIGHT);
97 _poly.addPoint(x, y + HEIGHT);
98
99 }
100
101 @Override
102 public void onParentStateChanged(ItemParentStateChangedEvent e) {
103 super.onParentStateChanged(e);
104
105 switch (e.getEventType()) {
106
107 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
108 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
109 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
110 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
111 // todo: Invoke later for concurrent modification possibility?
112 selfDestruct();
113 break;
114
115 }
116 }
117
118 private void selfDestruct() {
119
120 Frame parent = getParent();
121 parent.removeItem(this);
122
123 Frame currentFrame = DisplayIO.getCurrentFrame();
124
125 if (currentFrame != null) {
126
127 Timeline tl = FrameLayoutDaemon.getInstance().getLastComputedTimeline();
128
129 if (tl == null || FrameLayoutDaemon.getInstance().getTimelineOwner() == null ||
130 FrameLayoutDaemon.getInstance().getTimelineOwner() != currentFrame) {
131 tl = FrameLayoutDaemon.inferTimeline(currentFrame);
132 }
133
134 if (tl != null) {
135
136 long initiateMS = tl.getMSTimeAtX((int)_x);
137
138 // Clamp
139 if (initiateMS < tl.getFirstInitiationTime())
140 initiateMS = tl.getFirstInitiationTime();
141
142 else if (initiateMS > (tl.getFirstInitiationTime() + tl.getRunningTime()))
143 initiateMS = tl.getFirstInitiationTime() + tl.getRunningTime();
144
145 int startFrame = AudioMath.millisecondsToFrames(
146 initiateMS - tl.getFirstInitiationTime(),
147 SampledAudioManager.getInstance().getDefaultPlaybackFormat());
148
149 // To ensure that the frame channels are not in use
150 MultiTrackPlaybackController.getInstance().stopPlayback();
151 SoundDesk.getInstance().freeChannels(FramePlayer.FRAME_PLAYERMASTER_CHANNEL_ID);
152
153 FramePlayer.playFrame(
154 new MultitrackLoadListener() {
155
156 public void multiplaybackLoadStatusUpdate(int id, Object state) {
157 // Ignore
158 }
159
160 },
161 currentFrame.getName(),
162 false,
163 0,
164 startFrame,
165 Integer.MAX_VALUE
166 );
167
168 }
169
170 }
171 }
172
173
174
175}
Note: See TracBrowser for help on using the repository browser.