Source code: com/arranger/jarl/trait/base/RotationPainter.java
1 package com.arranger.jarl.trait.base;
2
3 import com.arranger.jarl.trait.BaseSegmentTrait;
4 import com.arranger.jarl.widget.IWidget;
5 import com.arranger.jarl.base.IContext;
6 import com.arranger.jarl.base.IGradientManager;
7 import com.arranger.jarl.base.IJarlObjectInfo;
8 import com.arranger.jarl.util.WidgetConfigSegment;
9 import com.arranger.jarl.util.ObjectUtil;
10 import com.arranger.jarl.util.InterpolateUtil;
11 import com.arranger.jarl.util.JarlInfoUtil;
12 import com.jhlabs.image.Gradient;
13
14 import java.awt.*;
15
16 import org.w3c.dom.Element;
17
18 /**
19 * RotationPainter will cycle through the colors in a gradient
20 * one color per frame, or at the rate passed it
21 *
22 *
23 * @traitAttribute gradientIndex ## xs:integer ## the gradient to cycle through
24 * @traitAttribute gradientCycle ## xs:string ## how many times to cycle through the gradient
25 *
26 * @traitAttribute startGradientIndex ## xs:integer ## the starting gradient to cycle through
27 * @traitAttribute startGradientCycle ## xs:string ## starting how many times to cycle through the gradient
28 * @traitAttribute endGradientIndex ## xs:integer ## the ending gradient to cycle through
29 * @traitAttribute endGradientCycle ## xs:string ## ending how many times to cycle through the gradient
30 */
31 public class RotationPainter extends BaseSegmentTrait {
32
33 protected int m_gradientIndex = Integer.MAX_VALUE;
34 protected double m_gradientCycle = Double.NEGATIVE_INFINITY;
35
36 protected int m_startGradientIndex = Integer.MAX_VALUE;
37 protected double m_startGradientCycle = Double.NEGATIVE_INFINITY;
38 protected int m_endGradientIndex = Integer.MAX_VALUE;
39 protected double m_endGradientCycle = Double.NEGATIVE_INFINITY;
40
41 /**
42 * Prior to the {@link IWidget#paint} the trait can perform some work
43 * @param widget the widget that is being painted
44 * @param context the current context
45 * @param graphics2D the current graphics object
46 * @return the new graphics, or the same
47 */
48 public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
49 store(widget.getColor());
50
51 IGradientManager gradientManager = context.getGradientManager();
52 Color color;
53 double gradientCycle;
54 int[] map;
55 double currentPct = InterpolateUtil.interpolate(widget, context.getTime());
56 if (m_gradientIndex != Integer.MAX_VALUE) {
57 Gradient gradient = gradientManager.getGradient(m_gradientIndex);
58 map = gradient.getMap();
59 gradientCycle = m_gradientCycle;
60 } else if (m_startGradientIndex != Integer.MAX_VALUE && m_endGradientIndex != Integer.MAX_VALUE) {
61 //interpolate
62 Gradient gradient1 = gradientManager.getGradient(m_startGradientIndex);
63 Gradient gradient2 = gradientManager.getGradient(m_endGradientIndex);
64 Gradient gradient = gradientManager.interpolate(gradient1, gradient2, currentPct);
65 map = gradient.getMap();
66
67 gradientCycle = InterpolateUtil.interpolate(widget.getStartTime(),
68 widget.getEndTime(),
69 m_startGradientCycle,
70 m_endGradientCycle,
71 context.getTime());
72 } else {
73 RotationPainterConfigSegment rotationPainterConfigSegment = (RotationPainterConfigSegment) getCurrentSegment(widget, context);
74 currentPct = getCurrentSegmentTimePct(widget, context, rotationPainterConfigSegment);
75
76 //interpolate
77 Gradient gradient1 = gradientManager.getGradient(rotationPainterConfigSegment.getStartGradientIndex());
78 Gradient gradient2 = gradientManager.getGradient(rotationPainterConfigSegment.getEndGradientIndex());
79 Gradient gradient = gradientManager.interpolate(gradient1, gradient2, currentPct);
80 map = gradient.getMap();
81
82 gradientCycle = InterpolateUtil.interpolate(0,
83 1,
84 rotationPainterConfigSegment.getStartGradientCycle(),
85 rotationPainterConfigSegment.getEndGradientCycle(),
86 currentPct);
87 }
88
89 //now get the right color from the gradient
90 int index = (int) InterpolateUtil.interpolate(0, 1, 0, map.length * gradientCycle, currentPct);
91 index %= map.length;
92 color = new Color(map[index]);
93
94 widget.setColor(color);
95 return graphics2D;
96 }
97
98 /**
99 * After the call to {@link IWidget#paint} is called
100 * the trait can restore some state
101 * @param widget the widget that is being painted
102 * @param context the current context
103 * @param graphics2D the current graphics object
104 * @return the new graphics, or the same
105 */
106 public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
107 restorePaint(widget);
108 return graphics2D;
109 }
110
111 /**
112 * Always remember some attrs might not be there
113 * @param context
114 */
115 protected void initAttributes(IContext context) {
116 super.initAttributes(context);
117
118 ObjectUtil.initializeField("gradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
119 ObjectUtil.initializeField("gradientCycle", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
120
121 ObjectUtil.initializeField("startGradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
122 ObjectUtil.initializeField("startGradientCycle", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
123 ObjectUtil.initializeField("endGradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
124 ObjectUtil.initializeField("endGradientCycle", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
125 }
126
127 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
128 super.addJarlObjectInfo(jarlObjectInfo);
129 populateInfo(jarlObjectInfo, "gradientIndex", "Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
130 populateInfo(jarlObjectInfo, "gradientCycle", "Gradient Cycle", JarlInfoUtil.PCT_DISPLAY);
131
132 populateInfo(jarlObjectInfo, "startGradientIndex", "Start Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
133 populateInfo(jarlObjectInfo, "startGradientCycle", "Start Gradient Cycle", JarlInfoUtil.PCT_DISPLAY);
134 populateInfo(jarlObjectInfo, "endGradientIndex", "End Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
135 populateInfo(jarlObjectInfo, "endGradientCycle", "End Gradient Cycle", JarlInfoUtil.PCT_DISPLAY);
136 }
137
138 /**
139 * Create a concrete {@link WidgetConfigSegment} based on this element
140 * @param element
141 * @return a specific {@link WidgetConfigSegment}
142 */
143 public WidgetConfigSegment createSegment(Element element) {
144 return new RotationPainterConfigSegment(element);
145 }
146
147 public static class RotationPainterConfigSegment extends WidgetConfigSegment {
148
149 protected int m_startGradientIndex = Integer.MAX_VALUE;
150 protected double m_startGradientCycle = Double.NEGATIVE_INFINITY;
151 protected int m_endGradientIndex = Integer.MAX_VALUE;
152 protected double m_endGradientCycle = Double.NEGATIVE_INFINITY;
153
154 public RotationPainterConfigSegment(Element element) {
155 super(element);
156 ObjectUtil.initializeFieldStrict("startGradientIndex", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
157 ObjectUtil.initializeFieldStrict("startGradientCycle", element, this, ObjectUtil.NORMALIZING_CONVERSION);
158 ObjectUtil.initializeFieldStrict("endGradientIndex", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
159 ObjectUtil.initializeFieldStrict("endGradientCycle", element, this, ObjectUtil.NORMALIZING_CONVERSION);
160 }
161
162 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
163 super.addJarlObjectInfo(jarlObjectInfo);
164 populateInfo(jarlObjectInfo, "startGradientIndex", "Start Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
165 populateInfo(jarlObjectInfo, "startGradientCycle", "Start Gradient Cycle", JarlInfoUtil.PCT_DISPLAY);
166 populateInfo(jarlObjectInfo, "endGradientIndex", "End Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
167 populateInfo(jarlObjectInfo, "endGradientCycle", "End Gradient Cycle", JarlInfoUtil.PCT_DISPLAY);
168 }
169
170 public int getStartGradientIndex() {
171 return m_startGradientIndex;
172 }
173
174 public double getStartGradientCycle() {
175 return m_startGradientCycle;
176 }
177
178 public int getEndGradientIndex() {
179 return m_endGradientIndex;
180 }
181
182 public double getEndGradientCycle() {
183 return m_endGradientCycle;
184 }
185 }
186 }