1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.action;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import javax.swing.JOptionPane;
27
28 import net.sf.magicproject.action.context.ActionContextWrapper;
29 import net.sf.magicproject.action.context.Int;
30 import net.sf.magicproject.clickable.ability.Ability;
31 import net.sf.magicproject.clickable.targetable.player.Player;
32 import net.sf.magicproject.event.context.ContextEventListener;
33 import net.sf.magicproject.expression.IntValue;
34 import net.sf.magicproject.expression.ListExpression;
35 import net.sf.magicproject.network.IdMessages;
36 import net.sf.magicproject.stack.StackManager;
37 import net.sf.magicproject.tools.Log;
38 import net.sf.magicproject.tools.MToolKit;
39
40 /***
41 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42 * @since 0.82
43 */
44 class InputColor extends MessagingAction {
45
46 /***
47 * Create an instance of InputInteger by reading a file Offset's file must
48 * pointing on the first byte of this action. <br>
49 * <ul>
50 * Structure of InputStream : Data[size]
51 * <li>super [Input]</li>
52 * <li>allowed colors [Expression[]]</li>
53 * <li>is colorless mana available [boolean]</li>
54 * <li>multi select [boolean]</li>
55 * </ul>
56 *
57 * @param inputFile
58 * file containing this action
59 * @throws IOException
60 * if error occurred during the reading process from the specified
61 * input stream
62 */
63 InputColor(InputStream inputFile) throws IOException {
64 super(inputFile);
65 registerModifier = (ModifyRegister) ActionFactory.readAction(inputFile,
66 null);
67 allowedColors = new ListExpression(inputFile);
68 allowColorless = inputFile.read() == 1;
69 multiselect = inputFile.read() == 1;
70 }
71
72 @Override
73 public final Actiontype getIdAction() {
74 return Actiontype.INPUT_COLOR;
75 }
76
77 @Override
78 public boolean play(ContextEventListener context, Ability ability) {
79 final Player controller = (Player) this.controller.getTargetable(ability,
80 context, null);
81 controller.setHandedPlayer();
82 if (controller.isYou()) {
83 Log.debug("Opponent is waiting for our answer");
84 int codeColors = 0;
85 if (allowedColors.isEmpty())
86 codeColors = 0xFF;
87 else {
88 for (int color : allowedColors.getList(ability, ability.getCard(),
89 context))
90 codeColors |= color;
91 }
92 replayAction(context, ability,
93 new net.sf.magicproject.ui.wizard.InputColor(context, ability, this,
94 text, codeColors, allowColorless, multiselect));
95 } else {
96 Log.debug("Waiting for opponent's answer");
97 }
98 return false;
99 }
100
101 @Override
102 public final int getMessagingActionId() {
103 return IdMessages.COLOR_ANSWER;
104 }
105
106 @Override
107 protected void setAnswer(int optionAnswer, int indexAnswer,
108 ContextEventListener context, Ability ability,
109 ActionContextWrapper actionContext) {
110 if (optionAnswer == JOptionPane.NO_OPTION) {
111 ((IntValue) registerModifier.valueExpr).value = 0;
112 } else {
113 ((IntValue) registerModifier.valueExpr).value = indexAnswer;
114 }
115 if (actionContext != null) {
116 actionContext.actionContext = new Int(
117 ((IntValue) registerModifier.valueExpr).value);
118 }
119 if (registerModifier.play(context, ability)) {
120 StackManager.resolveStack();
121 }
122 }
123
124 @Override
125 public String toString(Ability ability) {
126 return "choose color";
127 }
128
129 @Override
130 public String toHtmlString(Ability ability, ContextEventListener context) {
131 return "<img src='file:///"
132 + MToolKit.getTbsHtmlPicture("actions/input-color.gif") + "'> ";
133 }
134
135 @Override
136 public final boolean replay(ActionContextWrapper actionContext,
137 ContextEventListener context, Ability ability) {
138 ((IntValue) registerModifier.valueExpr).value = ((Int) actionContext.actionContext)
139 .getInt();
140 return registerModifier.play(context, ability);
141 }
142
143 /***
144 * The register modifiaction action using the answer as input.
145 */
146 private final ModifyRegister registerModifier;
147
148 /***
149 * The allowed colors
150 */
151 private final ListExpression allowedColors;
152
153 /***
154 * Is colorless mana available?
155 */
156 private final boolean allowColorless;
157
158 /***
159 * Multi-select.
160 */
161 private boolean multiselect;
162
163 }