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 */
37 public class RegisterModifier extends Modifier {
38
39 /***
40 * Creates a new instance of RegisterModifier <br>
41 *
42 * @param name
43 * the modifier name
44 * @param to
45 * the component to be attached to
46 * @param ability
47 * is the ability owning this test. The card component of this
48 * ability should correspond to the card owning this test too.
49 * @param whileCondition
50 * the test conditionning the activation of this modifier.
51 * @param linkedEvents
52 * the events to be registered. This events would cause this modifier
53 * to be updated.
54 * @param until
55 * this modifier exists while this test is true.
56 * @param linked
57 * is the creator is linked to this modifier.
58 * @param layer
59 * is the strategy used to add this modifier within the existing
60 * chain.
61 * @param index
62 * the modified index
63 * @param rightExpression
64 * the expression used to modify the register
65 * @param op
66 * the operation applied to previous value with the value of this
67 * modifier.
68 */
69 RegisterModifier(String name, MCard to, Ability ability, Test whileCondition,
70 List<MEventListener> linkedEvents, List<MEventListener> until,
71 boolean linked, int layer, int index, Expression rightExpression,
72 Operation op) {
73 super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
74 this.op = op;
75 this.rightExpression = rightExpression;
76 this.index = index;
77 }
78
79 @Override
80 public void removeFromManager() {
81 super.removeFromManager();
82 if (!unregisteredModifier) {
83 to.removeModifier(this, index);
84 }
85 unregisteredModifier = true;
86 }
87
88 @Override
89 public Modifier removeModifier(Modifier modifier) {
90 if (this == modifier) {
91 if (activated) {
92 StackManager.postRefreshRegisters(to, index);
93 }
94 return next;
95 }
96 return super.removeModifier(modifier);
97 }
98
99 /***
100 * Return the modified value of the specified one. This value is only modified
101 * if the while condition test succeed.
102 *
103 * @param oldValue
104 * the old value
105 * @return the modified value.
106 */
107 public int getValue(int oldValue) {
108 if (activated) {
109 if (next != null) {
110 return ((RegisterModifier) next).getValue(op.process(oldValue,
111 rightValue));
112 }
113 return op.process(oldValue, rightValue);
114 }
115 if (next != null) {
116 return ((RegisterModifier) next).getValue(oldValue);
117 }
118 return oldValue;
119 }
120
121 @Override
122 public void refresh() {
123 boolean oldActivated = activated;
124 if (whileCondition.test(ability, to)) {
125 activated = true;
126 int oldValue = rightValue;
127
128 rightValue = rightExpression.getValue(ability, to, null);
129 if (oldValue != rightValue || !oldActivated) {
130 StackManager.postRefreshRegisters(to, index);
131 }
132 } else {
133 activated = false;
134
135 if (oldActivated) {
136
137 StackManager.postRefreshRegisters(to, index);
138 }
139 }
140 }
141
142 /***
143 * The looked for register index
144 */
145 protected int index;
146
147 /***
148 * The operation applied to the left and right values
149 */
150 private Operation op;
151
152 /***
153 * right integer value The complex expression to use for the right value. Is
154 * null if the IdToken number is not a complex expression.
155 */
156 private Expression rightExpression = null;
157
158 /***
159 * right integer value
160 */
161 private int rightValue;
162
163 }