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 net.sf.magicproject.action.context.ActionContextWrapper;
27 import net.sf.magicproject.action.context.Int;
28 import net.sf.magicproject.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.targetable.player.Player;
30 import net.sf.magicproject.database.propertyconfig.PropertyProxyConfig;
31 import net.sf.magicproject.event.context.ContextEventListener;
32 import net.sf.magicproject.expression.Expression;
33 import net.sf.magicproject.expression.ExpressionFactory;
34 import net.sf.magicproject.expression.IntValue;
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 * Display an input box waiting for an integer answer. It's possible to use the
42 * multi-language ability for text if the string start with the '$' character.
43 * The entered integer is used as 'value' for 'modifyregister'.
44 *
45 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46 * @since 0.82
47 * @since 0.91 use 'default' value and 'stric-max' constraint.
48 */
49 class InputNumber extends MessagingAction {
50
51 /***
52 * Create an instance of InputInteger by reading a file Offset's file must
53 * pointing on the first byte of this action. <br>
54 * <ul>
55 * Structure of InputStream : Data[size]
56 * <li>[super]</li>
57 * <li>modifier [ModifyRegister]</li>
58 * <li>min expression [...]</li>
59 * <li>max expression [...]</li>
60 * <li>default expression [...]</li>
61 * <li>strict-max boolean 1/0 [1]</li>
62 * </ul>
63 *
64 * @param inputFile
65 * file containing this action
66 * @throws IOException
67 * if error occurred during the reading process from the specified
68 * input stream
69 */
70 InputNumber(InputStream inputFile) throws IOException {
71 super(inputFile);
72 registerModifier = (ModifyRegister) ActionFactory.readAction(inputFile,
73 null);
74 minExpr = ExpressionFactory.readNextExpression(inputFile);
75 maxExpr = ExpressionFactory.readNextExpression(inputFile);
76 defaultExpr = ExpressionFactory.readNextExpression(inputFile);
77 strictMax = inputFile.read() != 0;
78 }
79
80 @Override
81 public final Actiontype getIdAction() {
82 return Actiontype.INPUT_NUMBER;
83 }
84
85 @Override
86 public boolean play(ContextEventListener context, Ability ability) {
87 final Player controller = (Player) this.controller.getTargetable(ability,
88 context, null);
89 controller.setHandedPlayer();
90 if (controller.isYou()) {
91 Log.debug("Opponent is waiting for our answer");
92 PropertyProxyConfig.values.put("%max", maxExpr);
93 PropertyProxyConfig.values.put("%min", minExpr);
94 replayAction(context, ability,
95 new net.sf.magicproject.ui.wizard.InputNumber(context, ability, this,
96 text, minExpr.getValue(ability, ability.getCard(), context),
97 maxExpr.getValue(ability, ability.getCard(), context), false,
98 defaultExpr.getValue(ability, ability.getCard(), context),
99 strictMax));
100 } else {
101 Log.debug("Waiting for opponent's answer");
102 }
103 return false;
104 }
105
106 @Override
107 public String toString(Ability ability) {
108
109 return "integer inputbox";
110 }
111
112 @Override
113 public String toHtmlString(Ability ability, ContextEventListener context) {
114 return "<img src='file:///"
115 + MToolKit.getTbsHtmlPicture("actions/input-number.gif") + "'> ";
116 }
117
118 @Override
119 protected final int getMessagingActionId() {
120 return IdMessages.INTEGER_ANSWER;
121 }
122
123 @Override
124 protected void setAnswer(int optionAnswer, int indexAnswer,
125 ContextEventListener context, Ability ability,
126 ActionContextWrapper actionContext) {
127 ((IntValue) registerModifier.valueExpr).value = indexAnswer;
128 if (actionContext != null) {
129 actionContext.actionContext = new Int(
130 ((IntValue) registerModifier.valueExpr).value);
131 }
132 if (registerModifier.play(context, ability)) {
133 StackManager.resolveStack();
134 }
135 }
136
137 @Override
138 public final boolean replay(ActionContextWrapper actionContext,
139 ContextEventListener context, Ability ability) {
140 ((IntValue) registerModifier.valueExpr).value = ((Int) actionContext.actionContext)
141 .getInt();
142 return registerModifier.play(context, ability);
143 }
144
145 /***
146 * The register modifiaction action using the answer as input.
147 */
148 private final ModifyRegister registerModifier;
149
150 /***
151 * The maximal value accepted for the displayed wizard.
152 */
153 private final Expression maxExpr;
154
155 /***
156 * The minimal value accepted for the displayed wizard.
157 */
158 private final Expression minExpr;
159
160 /***
161 * The default value displayed in the diaLog. By default, the maximal allowed
162 * value is used (%max).
163 */
164 private final Expression defaultExpr;
165
166 /***
167 * Allow or not to enter a value exceeding the maximal value. If false
168 * (default), the user can enter a higher value with a warning displayed in
169 * the diaLog.
170 */
171 private final boolean strictMax;
172 }