Source code: com/arranger/jarl/trait/base/Painter.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.*;
7 import com.arranger.jarl.widget.IWidget;
8 import org.w3c.dom.Element;
9
10 import java.awt.*;
11
12 /**
13 * Painter gradually changes colored paints from the start paint to the end paint.
14 * It would be nice if you could chain these together maybe...
15 *
16 * Required attributes:
17 * startColor or startColorData
18 * endColor or endColorData
19 *
20 * @traitAttribute color ## xs:string ## possible values are: white, lightGray, gray, darkGray, black, red, pink, orange, yellow, green, magenta, cyan, blue
21 * @traitAttribute colorData ## xs:string ## the rgb values of a color. For example: (255, 0, 0) = red
22 * @traitAttribute startColor ## xs:string ## the color to start with
23 * @traitAttribute startColorData ## xs:string ## the starting colorData
24 * @traitAttribute endColor ## xs:string ## the color to end with
25 * @traitAttribute endColorData ## xs:string ## the ending colorData
26 *
27 * @see IWidget#setColor
28 */
29 public class Painter extends BaseSegmentTrait {
30
31 protected Color m_color;
32 protected Color m_startColor;
33 protected Color m_endColor;
34
35 /**
36 * Prior to the {@link IWidget#paint} the trait can perform some work
37 * @param widget the widget that is being painted
38 * @param context the current context
39 * @param graphics2D the current graphics object
40 */
41 public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
42 store(widget.getColor());
43
44 Color startColor, endColor;
45 double pct;
46
47 if (m_color != null) {
48 startColor = endColor = m_color;
49 pct = 0;
50 } else if (m_startColor != null && m_endColor != null) {
51 startColor = m_startColor;
52 endColor = m_endColor;
53 pct = InterpolateUtil.interpolate(widget, context.getTime());
54 } else {
55 ColorConfigSegment colorConfigSegment = (ColorConfigSegment) getCurrentSegment(widget, context);
56 startColor = colorConfigSegment.getStartColor();
57 endColor = colorConfigSegment.getEndColor();
58 pct = getCurrentSegmentTimePct(widget, context, colorConfigSegment);
59 }
60
61 Color newColor = PaintUtil.interpolatePaint(startColor, endColor, pct);
62 widget.setColor(newColor);
63 return graphics2D;
64 }
65
66 /**
67 * After the call to {@link IWidget#paint} is called
68 * the trait can restore some state
69 * @param widget the widget that is being painted
70 * @param context the current context
71 * @param graphics2D the current graphics object
72 */
73 public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
74 restorePaint(widget);
75 return graphics2D;
76 }
77
78 /**
79 * Always remember some attrs might not be there
80 * @param context
81 */
82 protected void initAttributes(IContext context) {
83 super.initAttributes(context);
84
85 ObjectUtil.initializeField("color", m_configElement, this, ObjectUtil.COLOR_CONVERSION);
86 ObjectUtil.initializeField("startColor", m_configElement, this, ObjectUtil.COLOR_CONVERSION);
87 ObjectUtil.initializeField("endColor", m_configElement, this, ObjectUtil.COLOR_CONVERSION);
88 }
89
90 /**
91 * Override this, and for every field that you're using, call {@link #populateInfo}
92 * for example:
93 * <code>
94 * populateInfo(jarlObjectInfo, "zOrder", "Z-Order", JarlInfoUtil.PRIMITIVE_DISPLAY);
95 * </code>
96 *
97 * @param jarlObjectInfo
98 *
99 * @see JarlInfoUtil#PRIMITIVE_DISPLAY
100 * @see #populateInfo
101 * @see ObjectUtil#initializeField
102 */
103 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
104 super.addJarlObjectInfo(jarlObjectInfo);
105 populateInfo(jarlObjectInfo, "color", "Color", JarlInfoUtil.COLOR_DISPLAY);
106 populateInfo(jarlObjectInfo, "startColor", "Start Color", JarlInfoUtil.COLOR_DISPLAY);
107 populateInfo(jarlObjectInfo, "endColor", "End Color", JarlInfoUtil.COLOR_DISPLAY);
108 }
109
110 /**
111 * Create a concrete {@link WidgetConfigSegment} based on this element
112 * @param element
113 * @return a specific {@link WidgetConfigSegment}
114 */
115 public WidgetConfigSegment createSegment(Element element) {
116 return new ColorConfigSegment(element);
117 }
118
119 protected static class ColorConfigSegment extends WidgetConfigSegment {
120
121 protected Color m_startColor;
122 protected Color m_endColor;
123
124 public ColorConfigSegment(Element element) {
125 super(element);
126
127 ObjectUtil.initializeFieldStrict("startColor", element, this, ObjectUtil.COLOR_CONVERSION);
128 ObjectUtil.initializeFieldStrict("endColor", element, this, ObjectUtil.COLOR_CONVERSION);
129 }
130
131 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
132 super.addJarlObjectInfo(jarlObjectInfo);
133 populateInfo(jarlObjectInfo, "startColor", "Start Color", JarlInfoUtil.COLOR_DISPLAY);
134 populateInfo(jarlObjectInfo, "endColor", "End Color", JarlInfoUtil.COLOR_DISPLAY);
135 }
136
137 public Color getStartColor() {
138 return m_startColor;
139 }
140
141 public Color getEndColor() {
142 return m_endColor;
143 }
144 }
145 }