Source code: com/arranger/jarl/trait/base/Motion.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.util.*;
6 import com.arranger.jarl.widget.IWidget;
7 import org.w3c.dom.Element;
8
9 import java.awt.*;
10 import java.awt.geom.GeneralPath;
11 import java.awt.geom.Point2D;
12
13 /**
14 * Motion allows you to create quadratic motion paths for widets.
15 *
16 * @traitAttribute startPos ## xs:string ## The start position (relative to the screen) of the motion (10%, 10%)
17 * @traitAttribute endPos ## xs:string ## The end position (relative to the screen) of the motion (90%, 10%)
18 * @traitAttribute controlPos ## xs:string ## The control position (relative to the screen) of the motion (50%, 50%)
19 */
20 public class Motion extends Position {
21
22 protected Point2D m_controlPos;
23
24 /**
25 * Prior to the {@link IWidget#paint} the trait can perform some work
26 * @param widget the widget that is being painted
27 * @param context the current context
28 * @param graphics2D the current graphics object
29 *
30 */
31 public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
32 //get and store transform
33 graphics2D = initGraphicsTransform(graphics2D);
34
35 float startX, startY, endX, endY, controlX, controlY;
36 double currentTimePct;
37 if (m_startPos != null && m_endPos != null && m_controlPos != null) {
38 startX = (float) m_startPos .getX();
39 startY = (float) m_startPos .getY();
40 endX = (float) m_endPos.getX();
41 endY = (float) m_endPos.getY();
42 controlX = (float) m_controlPos.getX();
43 controlY = (float) m_controlPos.getY();
44 currentTimePct = InterpolateUtil.interpolate(widget, context.getTime());
45 } else {
46 //get the current position segment and the pct relative to that segment
47 MotionConfigSegment motionConfigSegment = (MotionConfigSegment) getCurrentSegment(widget, context);
48 currentTimePct = getCurrentSegmentTimePct(widget, context, motionConfigSegment);
49 startX = (float) motionConfigSegment.getStartPos().getX();
50 startY = (float) motionConfigSegment.getStartPos().getY();
51 endX = (float) motionConfigSegment.getEndPos().getX();
52 endY = (float) motionConfigSegment.getEndPos().getY();
53 controlX = (float) motionConfigSegment.getControlPos().getX();
54 controlY = (float) motionConfigSegment.getControlPos().getY();
55 }
56
57 GeneralPath generalPath = new GeneralPath();
58 generalPath.moveTo(startX, startY);
59 generalPath.quadTo(controlX, controlY, endX, endY);
60
61 Point2D[] points = WidgetUtil.getPoints(generalPath);
62 int index = (int) InterpolateUtil.interpolate(0,
63 1,
64 0,
65 points.length - 1,
66 currentTimePct);
67
68 translate(graphics2D, context, points[index].getX(), points[index].getY());
69 return graphics2D;
70 }
71
72 protected void initAttributes(IContext context) {
73 super.initAttributes(context);
74
75 ObjectUtil.initializeField("controlPos", m_configElement, this, ObjectUtil.POINT2D_CONVERSION);
76 }
77
78 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
79 super.addJarlObjectInfo(jarlObjectInfo);
80 populateInfo(jarlObjectInfo, "controlPos", "Control Pos", JarlInfoUtil.POINT2D_DISPLAY);
81 }
82
83 /**
84 * Create a concrete {@link WidgetConfigSegment} based on this element
85 * @param element
86 * @return a specific {@link WidgetConfigSegment}
87 */
88 public WidgetConfigSegment createSegment(Element element) {
89 return new MotionConfigSegment(element);
90 }
91
92 protected static class MotionConfigSegment extends PositionConfigSegment {
93
94 protected Point2D m_controlPos;
95
96 public MotionConfigSegment(Element element) {
97 super(element);
98 ObjectUtil.initializeFieldStrict("controlPos", element, this, ObjectUtil.POINT2D_CONVERSION);
99 }
100
101 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
102 super.addJarlObjectInfo(jarlObjectInfo);
103 populateInfo(jarlObjectInfo, "controlPos", "Control Pos", JarlInfoUtil.POINT2D_DISPLAY);
104 }
105
106 public Point2D getControlPos() {
107 return m_controlPos;
108 }
109 }
110 }