Source code: com/arranger/jarl/trait/base/Shear.java
1 package com.arranger.jarl.trait.base;
2
3 import com.arranger.jarl.base.IContext;
4 import com.arranger.jarl.base.IJarlObjectInfo;
5 import com.arranger.jarl.trait.BaseSegmentTrait;
6 import com.arranger.jarl.util.InterpolateUtil;
7 import com.arranger.jarl.util.JarlInfoUtil;
8 import com.arranger.jarl.util.ObjectUtil;
9 import com.arranger.jarl.util.WidgetConfigSegment;
10 import com.arranger.jarl.widget.IWidget;
11 import org.w3c.dom.Element;
12
13 import java.awt.*;
14
15 /**
16 * Shear implements Shear
17 *
18 * @traitAttribute shearX ## xs:float ## the amount to shear in the x direction (between -1 & 1)
19 * @traitAttribute shearY ## xs:float ## the amount to shear in the y direction (between -1 & 1)
20 *
21 * @traitAttribute startShearX ## xs:float ## the starting amount to shear in the x direction (between -1 & 1)
22 * @traitAttribute startShearY ## xs:float ## the starting amount to shear in the y direction (between -1 & 1)
23 * @traitAttribute endShearX ## xs:float ## the ending amount to shear in the x direction (between -1 & 1)
24 * @traitAttribute endShearY ## xs:float ## the ending amount to shear in the y direction (between -1 & 1)
25 */
26 public class Shear extends BaseSegmentTrait {
27
28 protected double m_shearX = Double.NEGATIVE_INFINITY;
29 protected double m_shearY = Double.NEGATIVE_INFINITY;
30
31 protected double m_startShearX = Double.NEGATIVE_INFINITY;
32 protected double m_startShearY = Double.NEGATIVE_INFINITY;
33 protected double m_endShearX = Double.NEGATIVE_INFINITY;
34 protected double m_endShearY = Double.NEGATIVE_INFINITY;
35
36 /**
37 * Prior to the {@link IWidget#paint} the trait can perform some work
38 * @param widget the widget that is being painted
39 * @param context the current context
40 * @param graphics2D the current graphics object
41 * @return the new graphics, or the same
42 */
43 public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
44 //get and store transform
45 graphics2D = initGraphicsTransform(graphics2D);
46
47 double shearX, shearY;
48 if (m_shearX != Double.NEGATIVE_INFINITY && m_shearY != Double.NEGATIVE_INFINITY) {
49 shearX = m_shearX;
50 shearY = m_shearY;
51 } else if (m_startShearX != Double.NEGATIVE_INFINITY &&
52 m_startShearY != Double.NEGATIVE_INFINITY &&
53 m_endShearX != Double.NEGATIVE_INFINITY &&
54 m_endShearY != Double.NEGATIVE_INFINITY) {
55
56 shearX = InterpolateUtil.interpolate(widget.getStartTime(),
57 widget.getEndTime(),
58 m_startShearX,
59 m_endShearX,
60 context.getTime());
61
62 shearY = InterpolateUtil.interpolate(widget.getStartTime(),
63 widget.getEndTime(),
64 m_startShearY,
65 m_endShearY,
66 context.getTime());
67 } else {
68 ShearConfigSegment shearConfigSegment = (ShearConfigSegment) getCurrentSegment(widget, context);
69 double currentTimePct = getCurrentSegmentTimePct(widget, context, shearConfigSegment);
70
71 shearX = InterpolateUtil.interpolate(0,
72 1,
73 shearConfigSegment.getStartShearX(),
74 shearConfigSegment.getEndShearX(),
75 currentTimePct);
76
77 shearY = InterpolateUtil.interpolate(0,
78 1,
79 shearConfigSegment.getStartShearY(),
80 shearConfigSegment.getEndShearY(),
81 currentTimePct);
82 }
83
84 graphics2D.shear(shearX, shearY);
85 return graphics2D;
86 }
87
88 /**
89 * After the call to {@link IWidget#paint} is called
90 * the trait can restore some state
91 * @param widget the widget that is being painted
92 * @param context the current context
93 * @param graphics2D the current graphics object
94 * @return the new graphics, or the same
95 */
96 public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
97 restoreTransform(graphics2D);
98 return graphics2D;
99 }
100
101 /**
102 * Always remember some attrs might not be there
103 * @param context
104 */
105 protected void initAttributes(IContext context) {
106 super.initAttributes(context);
107
108 ObjectUtil.initializeField("shearX", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
109 ObjectUtil.initializeField("shearY", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
110 ObjectUtil.initializeField("startShearX", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
111 ObjectUtil.initializeField("startShearY", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
112 ObjectUtil.initializeField("endShearX", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
113 ObjectUtil.initializeField("endShearY", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
114 }
115
116 /**
117 * Override this, and for every field that you're using, call {@link #populateInfo}
118 * for example:
119 * <code>
120 * populateInfo(jarlObjectInfo, "zOrder", "Z-Order", JarlInfoUtil.PRIMITIVE_DISPLAY);
121 * </code>
122 *
123 * @param jarlObjectInfo
124 *
125 * @see JarlInfoUtil#PRIMITIVE_DISPLAY
126 * @see #populateInfo
127 * @see ObjectUtil#initializeField
128 */
129 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
130 super.addJarlObjectInfo(jarlObjectInfo);
131 populateInfo(jarlObjectInfo, "shearX", "X-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
132 populateInfo(jarlObjectInfo, "shearY", "Y-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
133
134 populateInfo(jarlObjectInfo, "startShearX", "Start X-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
135 populateInfo(jarlObjectInfo, "endShearX", "End X-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
136 populateInfo(jarlObjectInfo, "startShearY", "Start Y-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
137 populateInfo(jarlObjectInfo, "endShearY", "End Y-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
138 }
139
140 /**
141 * Create a concrete {@link WidgetConfigSegment} based on this element
142 * @param element
143 * @return a specific {@link WidgetConfigSegment}
144 */
145 public WidgetConfigSegment createSegment(Element element) {
146 return new ShearConfigSegment(element);
147 }
148
149 public static class ShearConfigSegment extends WidgetConfigSegment {
150
151 protected double m_startShearX = Double.NEGATIVE_INFINITY;
152 protected double m_startShearY = Double.NEGATIVE_INFINITY;
153 protected double m_endShearX = Double.NEGATIVE_INFINITY;
154 protected double m_endShearY = Double.NEGATIVE_INFINITY;
155
156 public ShearConfigSegment(Element element) {
157 super(element);
158
159 ObjectUtil.initializeFieldStrict("startShearX", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
160 ObjectUtil.initializeFieldStrict("startShearY", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
161 ObjectUtil.initializeFieldStrict("endShearX", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
162 ObjectUtil.initializeFieldStrict("endShearY", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
163 }
164
165 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
166 super.addJarlObjectInfo(jarlObjectInfo);
167 populateInfo(jarlObjectInfo, "startShearX", "Start X-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
168 populateInfo(jarlObjectInfo, "endShearX", "End X-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
169 populateInfo(jarlObjectInfo, "startShearY", "Start Y-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
170 populateInfo(jarlObjectInfo, "endShearY", "End Y-Shear", JarlInfoUtil.PRIMITIVE_DISPLAY);
171 }
172
173 public double getStartShearX() {
174 return m_startShearX;
175 }
176
177 public double getStartShearY() {
178 return m_startShearY;
179 }
180
181 public double getEndShearX() {
182 return m_endShearX;
183 }
184
185 public double getEndShearY() {
186 return m_endShearY;
187 }
188 }
189 }