1
14 package demo.view.viewmode;
15
16 import demo.view.DemoBase;
17
18 import y.anim.AnimationFactory;
19 import y.anim.AnimationObject;
20 import y.anim.AnimationPlayer;
21 import y.base.Node;
22 import y.base.NodeMap;
23 import y.view.EditMode;
24 import y.view.Graph2DViewRepaintManager;
25 import y.view.HitInfo;
26 import y.view.NodeRealizer;
27 import y.view.ViewAnimationFactory;
28 import y.view.ViewMode;
29 import y.view.AutoDragViewMode;
30 import y.view.DefaultGraph2DRenderer;
31 import y.util.DefaultMutableValue2D;
32 import y.util.Value2D;
33
34 import java.awt.Dimension;
35
36
37
43 public class RollOverEffectDemo extends DemoBase {
44
45 public RollOverEffectDemo() {
46 final DefaultGraph2DRenderer g2dr = new DefaultGraph2DRenderer();
47 g2dr.setDrawEdgesFirst(true);
48 view.setGraph2DRenderer(g2dr);
49 view.setPreferredSize(new Dimension(800, 600));
50 loadInitialGraph();
51 }
52
53
56 protected void registerViewModes() {
57 final EditMode editMode = createEditMode();
58 if (editMode != null) {
59 view.addViewMode(editMode);
60 }
61 view.addViewMode(new AutoDragViewMode());
62 view.addViewMode(new RollOverViewMode());
63 }
64
65
68 protected void loadInitialGraph() {
69 loadGraph("resource/sample.gml");
70 }
71
72
73 public static void main( String[] args ) {
74 initLnF();
75 (new RollOverEffectDemo()).start();
76 }
77
78
82 private static final class RollOverViewMode extends ViewMode {
83
84 private static final int NONE = 0;
85
86 private static final int MARKED = 1;
87
88 private static final int UNMARK = 2;
89
90
91
92 private static final int PREFERRED_DURATION = 350;
93
94
95 private static final Value2D SCALE_FACTOR =
96 DefaultMutableValue2D.create(3, 3);
97
98
99
100 private Node lastHitNode;
101
102 private NodeMap size;
103
104 private NodeMap state;
105
106 private ViewAnimationFactory factory;
107 private AnimationPlayer player;
108
109
112 public void mouseMoved( final double x, final double y ) {
113 final HitInfo hi = getHitInfo(x, y);
114 if (hi.hasHitNodes()) {
115 final Node node = (Node) hi.hitNodes().current();
116 if (node != lastHitNode) {
117 unmark(lastHitNode);
118 }
119 if (state.getInt(node) == NONE) {
120 mark(node);
121 lastHitNode = node;
122 }
123 } else {
124 unmark(lastHitNode);
125 lastHitNode = null;
126 }
127 }
128
129
133 public void activate( final boolean b ) {
134 if (b) {
135 factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
136 player = factory.createConfiguredPlayer();
137 size = view.getGraph2D().createNodeMap();
138 state = view.getGraph2D().createNodeMap();
139 } else {
140 view.getGraph2D().disposeNodeMap(state);
141 view.getGraph2D().disposeNodeMap(size);
142 state = null;
143 size = null;
144 player = null;
145 factory = null;
146 }
147 super.activate(b);
148 }
149
150
153 protected HitInfo getHitInfo( final double x, final double y ) {
154 final HitInfo hi = new HitInfo(view, x, y, true, HitInfo.NODE);
155 setLastHitInfo(hi);
156 return hi;
157 }
158
159
163 protected void mark( final Node node ) {
164 if (state.getInt(node) == NONE) {
167 state.setInt(node, MARKED);
168
169 final NodeRealizer nr = getGraph2D().getRealizer(node);
170 size.set(node, DefaultMutableValue2D.create(nr.getWidth(), nr.getHeight()));
171 final AnimationObject ao = factory.scale(
172 nr,
173 SCALE_FACTOR,
174 ViewAnimationFactory.APPLY_EFFECT,
175 PREFERRED_DURATION);
176 player.animate(AnimationFactory.createEasedAnimation(ao));
177 }
178 }
179
180
184 protected void unmark( final Node node ) {
185 if (node == null) {
186 return;
187 }
188
189 if (state.getInt(node) == MARKED) {
192 state.setInt(node, UNMARK);
193
194 final Value2D oldSize = (Value2D) size.get(node);
195 final NodeRealizer nr = getGraph2D().getRealizer(node);
196 final AnimationObject ao = factory.resize(
197 nr,
198 oldSize,
199 ViewAnimationFactory.APPLY_EFFECT,
200 PREFERRED_DURATION);
201 final AnimationObject eao = AnimationFactory.createEasedAnimation(ao);
202 player.animate(new Reset(eao, node, nr, oldSize));
203 }
204 }
205
206
209 private final class Reset implements AnimationObject {
210 private AnimationObject ao;
211 private final Node node;
212 private final NodeRealizer nr;
213 private final Value2D oldSize;
214
215 Reset(
216 final AnimationObject ao,
217 final Node node,
218 final NodeRealizer nr,
219 final Value2D size
220 ) {
221 this.ao = ao;
222 this.node = node;
223 this.nr = nr;
224 this.oldSize = size;
225 }
226
227 public void initAnimation() {
228 ao.initAnimation();
229 }
230
231 public void calcFrame( final double time ) {
232 ao.calcFrame(time);
233 }
234
235
239 public void disposeAnimation() {
240 ao.disposeAnimation();
241 nr.setSize(oldSize.getX(), oldSize.getY());
242 size.set(node, null);
243 state.setInt(node, NONE);
244 }
245
246 public long preferredDuration() {
247 return ao.preferredDuration();
248 }
249 }
250 }
251 }
252