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.clickable.targetable.player.Player;
29 import net.sf.magicproject.event.MEventListener;
30 import net.sf.magicproject.stack.StackManager;
31 import net.sf.magicproject.test.Test;
32 import net.sf.magicproject.test.TestOn;
33
34 /***
35 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36 */
37 public class ControllerModifier extends Modifier {
38
39 /***
40 * Creates a new instance of ControllerModifier <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 * the modifier layer.
60 * @param newController
61 * the new controller to set
62 */
63 ControllerModifier(String name, MCard to, Ability ability,
64 Test whileCondition, List<MEventListener> linkedEvents,
65 List<MEventListener> until, boolean linked, int layer,
66 TestOn newController) {
67 super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
68 this.newController = newController;
69 }
70
71 /***
72 * Return the modified controller
73 *
74 * @param oldPlayer
75 * The old player.
76 * @return the modified controller.
77 */
78 public Player getPlayer(Player oldPlayer) {
79 if (activated) {
80 if (next != null) {
81 return ((ControllerModifier) next).getPlayer(newController.getPlayer(
82 ability, null));
83 }
84 return newController.getPlayer(ability, null);
85 }
86 if (next != null) {
87 return ((ControllerModifier) next).getPlayer(oldPlayer);
88 }
89 return oldPlayer;
90 }
91
92 @Override
93 public Modifier removeModifier(Modifier modifier) {
94 if (this == modifier) {
95 if (activated) {
96 StackManager.postRefreshController(to);
97 }
98 return next;
99 }
100 return super.removeModifier(modifier);
101 }
102
103 @Override
104 public void removeFromManager() {
105 super.removeFromManager();
106 if (!unregisteredModifier) {
107 to.removeModifier(this);
108 }
109 unregisteredModifier = true;
110 }
111
112 @Override
113 public void refresh() {
114 boolean oldActivated = activated;
115 activated = whileCondition.test(ability, to);
116
117
118 if (oldActivated != activated) {
119 StackManager.postRefreshController(to);
120 }
121 }
122
123 /***
124 * The new controller to set
125 */
126 private TestOn newController;
127
128 }