-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreEditorView.java
224 lines (195 loc) · 7.98 KB
/
ScoreEditorView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Magic Annotator - The only thing you need to write down whatever you want.
* Copyright (C) 2013 Nahuel Barrios <barrios.nahuel@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nbempire.android.magicannotator.util.android.view;
import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.nbempire.android.magicannotator.R;
/**
* GUI entity type to use with the {@code scoreeditor_horizontal.xml} layout because it does its functionality. User can use it from layout resources
* with or without setting the {@code increment} XML attribute. If you don't pass that attribute, its default value will be {@code 1}.
* <p/>
* Use example in XML layout file:
* <p/>
* <com.nbempire.android.magicannotator.util.android.view.ScoreEditorView android:id="@+id/anId"/>
*
* @author Nahuel Barrios.
* @since 1
*/
public class ScoreEditorView extends RelativeLayout {
/**
* {@link String} El nombre del jugador que se va a mostrar vinculado con el puntaje.
*/
private final String playerNickname;
/**
* The default increment used to add/substract scores.
*/
private static final int DEFAULT_INCREMENT = 1;
private final boolean editable;
/**
* A constructor method for the type.
*
* @param context
* The view's context.
* @param playerNickname
* The name of the player linked with this ScoreEditorView.
* @param currentScore
* The initial score for the view.
* @param emsForPlayerScore
* Number of EMS for player score EditText.
*/
public ScoreEditorView(Context context, String playerNickname, int currentScore, int emsForPlayerScore, boolean editable) {
super(context);
this.playerNickname = playerNickname;
this.editable = editable;
initializeView(context, String.valueOf(currentScore), emsForPlayerScore);
}
/**
* A constructor method for the type.
*
* @param context
* The view's context.
* @param currentScore
* The initial score for the view.
* @param emsForPlayerScore
* Number of EMS for player score EditText.
*/
private void initializeView(final Context context, final String currentScore, final int emsForPlayerScore) {
LayoutInflater.from(context).inflate(R.layout.scoreeditor_horizontal, this, true);
TextView playerNameTextView = (TextView) findViewById(R.id.playerNickname);
playerNameTextView.setText(playerNickname);
TextSwitcher score = (TextSwitcher) findViewById(R.id.score);
score.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
View scoreEditText;
if (editable) {
scoreEditText = new EditText(context);
((EditText) scoreEditText).setInputType(InputType.TYPE_CLASS_NUMBER);
((EditText) scoreEditText).setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
} else {
scoreEditText = new TextView(context);
}
((TextView) scoreEditText).setEms(emsForPlayerScore);
((TextView) scoreEditText).setTextSize(26);
((TextView) scoreEditText).setText(currentScore);
scoreEditText.setId(ViewsUtil.generateId(playerNickname));
return scoreEditText;
}
});
addOnTouchActions(DEFAULT_INCREMENT);
}
/**
* Add corresponding functionality to add/substract buttons when users touch the screen.
*
* @param increment
* The increment used to add/substract scores.
*/
public void addOnTouchActions(int increment) {
Button plus = (Button) findViewById(R.id.plusButton);
plus.setOnTouchListener(new ScoreEditorOnTouchListener(increment));
Button substract = (Button) findViewById(R.id.substractButton);
substract.setOnTouchListener(new ScoreEditorOnTouchListener(increment, true));
}
/**
* Listener that implements {@link OnTouchListener} to do the functionality to add/substract score.
* <p/>
* TODO : Refactor : Move this type outside to re-use it.
*
* @author Nahuel Barrios.
* @since 1
*/
class ScoreEditorOnTouchListener implements OnTouchListener {
private boolean forSubstract = false;
private int increment = 1;
/**
* A constructor method for the type.
*
* @param increment
* The increment used to add scores.
*/
public ScoreEditorOnTouchListener(int increment) {
super();
this.increment = increment;
}
/**
* A constructor method for the type.
*
* @param increment
* The increment used to add/substract scores depending on the value of {@code forSubstract} parameter.
* @param forSubstract
* Boolean value indicating with {@code true} when the listener has to substract, or {@code false} for add.
*/
public ScoreEditorOnTouchListener(int increment, boolean forSubstract) {
super();
this.increment = increment;
this.forSubstract = forSubstract;
}
public boolean onTouch(View callerView, MotionEvent event) {
hideSoftKeyboard(callerView);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int newValue = increment;
if (forSubstract) {
newValue = increment * -1;
}
updateScore((TextView) findViewById(ViewsUtil.generateId(playerNickname)), newValue);
break;
default:
break;
}
return false;
}
/**
* Hide digital keyboard.
*
* @param callerView
* The caller view that called this listener.
*/
private void hideSoftKeyboard(View callerView) {
InputMethodManager inputMethodManager = (InputMethodManager) callerView.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(callerView.getWindowToken(), 0);
}
}
/**
* Add value of {@code increment} to the current value of the specified {@code score}.
* <p/>
* Please note that this method always add the {@code increment} value to the current value, so if you want to substract you will transform {@code
* increment} to the negative form.
*
* @param score
* The score to update.
* @param increment
* The increment used to add scores.
*/
private void updateScore(TextView score, int increment) {
String currentValue = score.getText().toString();
score.setText(Integer.toString(Integer.parseInt(currentValue) + increment));
}
}