Mercurial > projects > dwt2
comparison org.eclipse.draw2d/src/org/eclipse/draw2d/LayoutAnimator.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | dbfb303e8fb0 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.draw2d.LayoutAnimator; | |
15 | |
16 import java.lang.all; | |
17 | |
18 import org.eclipse.draw2d.geometry.Rectangle; | |
19 import org.eclipse.draw2d.Animator; | |
20 import org.eclipse.draw2d.LayoutListener; | |
21 import org.eclipse.draw2d.IFigure; | |
22 import org.eclipse.draw2d.Animation; | |
23 | |
24 /** | |
25 * Animates the layout of a figure's children. The animator will capture the effects of a | |
26 * layout manager, and then play back the placement of children using linear interpolation | |
27 * for each child's start and end locations. | |
28 * <P> | |
29 * To use an animator, hook it as a layout listener for the figure whose layout is to | |
30 * be animated, by calling {@link IFigure#addLayoutListener(LayoutListener)}. It is not | |
31 * necessary to have an animator for every figure in a composition that is undergoing | |
32 * animation. For example, if a figure without an animator moves during the animation, it | |
33 * will continue to move and layout its children normally during each step of the | |
34 * animation. | |
35 * <P> | |
36 * Animator must be used in conjunction with layouts. If figures are placed manually using | |
37 * <code>setBounds()</code>, the animator may not be able to track and playback the | |
38 * changes that occur. | |
39 * | |
40 * @since 3.2 | |
41 */ | |
42 public class LayoutAnimator : Animator , LayoutListener { | |
43 | |
44 private static LayoutAnimator INSTANCE_; | |
45 static LayoutAnimator INSTANCE(){ | |
46 if( INSTANCE_ is null ){ | |
47 synchronized( LayoutAnimator.classinfo ){ | |
48 if( INSTANCE_ is null ){ | |
49 INSTANCE_ = new LayoutAnimator(); | |
50 } | |
51 } | |
52 } | |
53 assert(INSTANCE_); | |
54 return INSTANCE_; | |
55 } | |
56 | |
57 /** | |
58 * Constructs a new Animator. The default instance ({@link #getDefault()}) can be used on | |
59 * all figures being animated. | |
60 * | |
61 * @since 3.2 | |
62 */ | |
63 protected this() { } | |
64 | |
65 /** | |
66 * Returns an object encapsulating the placement of children in a container. This method | |
67 * is called to capture both the initial and final states. | |
68 * @param container the container figure | |
69 * @return the current state | |
70 * @since 3.2 | |
71 */ | |
72 protected Object getCurrentState(IFigure container) { | |
73 Map locations = new HashMap(); | |
74 List children = container.getChildren(); | |
75 IFigure child; | |
76 for (int i = 0; i < children.size(); i++) { | |
77 child = cast(IFigure)children.get(i); | |
78 locations.put(cast(Object)child, child.getBounds().getCopy()); | |
79 } | |
80 return cast(Object)locations; | |
81 } | |
82 | |
83 /** | |
84 * Returns the default instance. | |
85 * @return the default instance | |
86 * @since 3.2 | |
87 */ | |
88 public static LayoutAnimator getDefault() { | |
89 return INSTANCE; | |
90 } | |
91 | |
92 /** | |
93 * Hooks invalidation in case animation is in progress. | |
94 * @see LayoutListener#invalidate(IFigure) | |
95 */ | |
96 public final void invalidate(IFigure container) { | |
97 if (Animation.isInitialRecording()) | |
98 Animation.hookAnimator(container, this); | |
99 } | |
100 | |
101 /** | |
102 * Hooks layout in case animation is in progress. | |
103 * @see org.eclipse.draw2d.LayoutListener#layout(org.eclipse.draw2d.IFigure) | |
104 */ | |
105 public final bool layout(IFigure container) { | |
106 if (Animation.isAnimating()) | |
107 return Animation.hookPlayback(container, this); | |
108 return false; | |
109 } | |
110 | |
111 /** | |
112 * Plays back the animated layout. | |
113 * @see Animator#playback(IFigure) | |
114 */ | |
115 protected bool playback(IFigure container) { | |
116 Map initial = cast(Map) Animation.getInitialState(this, container); | |
117 Map ending = cast(Map) Animation.getFinalState(this, container); | |
118 if (initial is null) | |
119 return false; | |
120 List children = container.getChildren(); | |
121 | |
122 float progress = Animation.getProgress(); | |
123 float ssergorp = 1 - progress; | |
124 | |
125 Rectangle rect1, rect2; | |
126 | |
127 for (int i = 0; i < children.size(); i++) { | |
128 IFigure child = cast(IFigure) children.get(i); | |
129 rect1 = cast(Rectangle)initial.get(cast(Object)child); | |
130 rect2 = cast(Rectangle)ending.get(cast(Object)child); | |
131 | |
132 //TODO need to change this to hide the figure until the end. | |
133 if (rect1 is null) | |
134 continue; | |
135 child.setBounds(new Rectangle( | |
136 cast(int)Math.round(progress * rect2.x + ssergorp * rect1.x), | |
137 cast(int)Math.round(progress * rect2.y + ssergorp * rect1.y), | |
138 cast(int)Math.round(progress * rect2.width + ssergorp * rect1.width), | |
139 cast(int)Math.round(progress * rect2.height + ssergorp * rect1.height) | |
140 )); | |
141 } | |
142 return true; | |
143 } | |
144 | |
145 /** | |
146 * Hooks post layout in case animation is in progress. | |
147 * @see LayoutListener#postLayout(IFigure) | |
148 */ | |
149 public final void postLayout(IFigure container) { | |
150 if (Animation.isFinalRecording()) | |
151 Animation.hookNeedsCapture(container, this); | |
152 } | |
153 | |
154 /** | |
155 * This callback is unused. Reserved for possible future use. | |
156 * @see LayoutListener#remove(IFigure) | |
157 */ | |
158 public final void remove(IFigure child) { } | |
159 | |
160 /** | |
161 * This callback is unused. Reserved for possible future use. | |
162 * @see LayoutListener#setConstraint(IFigure, Object) | |
163 */ | |
164 public final void setConstraint(IFigure child, Object constraint) { } | |
165 | |
166 } |