1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.magicproject.modifier;
23
24 import java.util.List;
25
26 import net.sf.magicproject.clickable.ability.Ability;
27 import net.sf.magicproject.clickable.targetable.card.MCard;
28 import net.sf.magicproject.event.MEventListener;
29 import net.sf.magicproject.expression.Expression;
30 import net.sf.magicproject.operation.Operation;
31 import net.sf.magicproject.stack.StackManager;
32 import net.sf.magicproject.test.Test;
33
34 /***
35 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36 * @since 0.86 Operation is used instead of add/remove boolean
37 * <code>hasNot</code>
38 */
39 public class ColorModifier extends Modifier {
40
41 /***
42 * Creates a new instance of ColorModifier <br>
43 *
44 * @param name
45 * the modifier name
46 * @param to
47 * the component to be attached to
48 * @param ability
49 * is the ability owning this test. The card component of this
50 * ability should correspond to the card owning this test too.
51 * @param whileCondition
52 * the test conditionning the activation of this modifier.
53 * @param linkedEvents
54 * the events to be registered. This events would cause this modifier
55 * to be updated.
56 * @param until
57 * this modifier exists while this test is true.
58 * @param linked
59 * is the creator is linked to this modifier.
60 * @param layer
61 * is the strategy used to add this modifier within the existing
62 * chain.
63 * @param idColor
64 * the color to add/remove/...
65 * @param op
66 * the operation applied to previous value with the value of this
67 * modifier.
68 */
69 ColorModifier(String name, MCard to, Ability ability, Test whileCondition,
70 List<MEventListener> linkedEvents, List<MEventListener> until,
71 boolean linked, int layer, Expression idColor, Operation op) {
72 super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
73 this.idColor = idColor;
74 this.op = op;
75 }
76
77 /***
78 * Return the modified idColor
79 *
80 * @param oldIdColor
81 * The previous idColor.
82 * @return the modified idColor.
83 */
84 public int getIdColor(int oldIdColor) {
85 if (activated) {
86 if (next != null) {
87 return ((ColorModifier) next).getIdColor(op.process(oldIdColor, idColor
88 .getValue(ability, to, null)));
89 }
90 return op.process(oldIdColor, idColor.getValue(ability, to, null));
91 }
92 if (next != null) {
93 return ((ColorModifier) next).getIdColor(oldIdColor);
94 }
95 return oldIdColor;
96 }
97
98 @Override
99 public Modifier removeModifier(Modifier modifier) {
100 if (this == modifier) {
101 if (activated) {
102 StackManager.postRefreshColor(to);
103 }
104 return next;
105 }
106 return super.removeModifier(modifier);
107 }
108
109 @Override
110 public final void removeFromManager() {
111 super.removeFromManager();
112 if (!unregisteredModifier) {
113 to.removeModifier(this);
114 }
115 unregisteredModifier = true;
116 }
117
118 @Override
119 public void refresh() {
120 boolean oldActivated = activated;
121 activated = whileCondition.test(ability, to);
122
123
124 if (oldActivated != activated) {
125 StackManager.postRefreshColor(to);
126 }
127 }
128
129 /***
130 * The modified idColor
131 */
132 protected Expression idColor;
133
134 /***
135 * Indicates the operation applied to previous value with the value of this
136 * modifier.
137 */
138 protected Operation op;
139
140 }