aboutsummaryrefslogtreecommitdiff
path: root/src/org/happysanta/gd/Menu/OptionsMenuElement.java
blob: 2119b3222ce275b89ba5347b405741e33975589f (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package org.happysanta.gd.Menu;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.happysanta.gd.Global;
import org.happysanta.gd.Menu.Views.MenuImageView;
import org.happysanta.gd.Menu.Views.MenuTextView;
import org.happysanta.gd.R;
// import com.grishka.agdtr.R;

import static org.happysanta.gd.Helpers.getDp;
import static org.happysanta.gd.Helpers.getGDActivity;
import static org.happysanta.gd.Helpers.getString;
import static org.happysanta.gd.Helpers.logDebug;

public class OptionsMenuElement
		extends ClickableMenuElement
		implements MenuElement, MenuHandler {

	protected int selectedIndex;
	protected String options[];
	protected int unlockedCount;
	protected MenuHandler handler;
	protected MenuScreen optionsScreen = null;
	protected MenuScreen screen = null;
	protected boolean isOnOffToggle;
	protected boolean m_oZ = false;
	protected String selectedOption;
	protected ActionMenuElement optionsScreenItems[] = null;
	protected MenuImageView lockImage = null;
	protected MenuTextView optionTextView = null;

	public OptionsMenuElement(String text, int selectedIndex, MenuHandler handler, String options[], boolean isOnOffToggle, MenuScreen screen) {
		this.text = text;
		this.selectedIndex = selectedIndex;
		this.handler = handler;
		this.options = options;
		if (this.options == null) this.options = new String[]{""};
		unlockedCount = this.options.length - 1;
		this.isOnOffToggle = isOnOffToggle;

		createAllViews();
		setSelectedOption(selectedIndex);

		if (isOnOffToggle) {
			if (selectedIndex == 1) {
				selectedOption = getString(R.string.off);
			} else {
				selectedOption = getString(R.string.on);
			}
		} else {
			this.screen = screen;
			updateSelectedOption();
			update();
		}
	}

	@Override
	protected void createAllViews() {
		Context context = getGDActivity();

		super.createAllViews();

		textView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT
		));

		optionTextView = new MenuTextView(context);
		optionTextView.setText(selectedOption);
		optionTextView.setTextColor(getMenuTextView().getTextColors());
		optionTextView.setTextSize(TEXT_SIZE);
		optionTextView.setTypeface(Global.robotoCondensedTypeface);
		optionTextView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT
		));
		optionTextView.setPadding(
				textView.getPaddingLeft(),
				textView.getPaddingTop(),
				textView.getPaddingRight(),
				textView.getPaddingBottom()
		);

		lockImage = new MenuImageView(context);
		lockImage.setImageResource(ActionMenuElement.locks[0]);
		lockImage.setScaleType(ImageView.ScaleType.CENTER);
		lockImage.setVisibility(View.GONE);

		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
		lp.setMargins(0, 0, getDp(ActionMenuElement.LOCK_IMAGE_MARGIN_RIGHT), 0);
		lockImage.setLayoutParams(lp);
		lockImage.setVisibility(View.GONE);

		layout.addView(lockImage);
		layout.addView(optionTextView);
	}

	private void updateSelectedOption() {
		selectedOption = options[selectedIndex];
		updateViewText();

		if (selectedIndex > unlockedCount && !isOnOffToggle) {
			lockImage.setVisibility(View.VISIBLE);
		} else {
			lockImage.setVisibility(View.GONE);
		}
	}

	public int getUnlockedCount() {
		return unlockedCount;
	}

	public void setUnlockedCount(int k) {
		unlockedCount = k;
		if (unlockedCount > options.length - 1)
			unlockedCount = options.length - 1;
		if (optionsScreen != null) {
			for (int l = 0; l < optionsScreenItems.length; l++)
				if (l > k)
					optionsScreenItems[l].setLock(true, true);
				else
					optionsScreenItems[l].setLock(false, false);
		}
		updateSelectedOption();
	}

	public int getOptionCount() {
		return options.length - 1;
	}

	public String[] getOptions() {
		return options;
	}

	public void setOptions(String as[]) {
		setOptions(as, true);
	}

	public void setOptions(String as[], boolean update) {
		options = as;
		if (selectedIndex > options.length - 1)
			selectedIndex = options.length - 1;
		if (unlockedCount > options.length - 1)
			unlockedCount = options.length - 1;
		updateSelectedOption();
		if (update) update();
	}

	public int getSelectedOption() {
		return selectedIndex;
	}

	public void setSelectedOption(int k) {
		selectedIndex = k;
		if (selectedIndex > options.length - 1)
			selectedIndex = 0;
		if (selectedIndex < 0)
			selectedIndex = options.length - 1;
		updateSelectedOption();
	}

	public void update() {
		optionsScreen = new MenuScreen(text, screen);
		optionsScreenItems = new ActionMenuElement[options.length];
		for (int k = 0; k < optionsScreenItems.length; k++) {
			if (k > unlockedCount) {
				optionsScreenItems[k] = new ActionMenuElement(options[k], this);
				optionsScreenItems[k].setLock(true, true);
			} else {
				optionsScreenItems[k] = new ActionMenuElement(options[k], this);
			}
			optionsScreen.addItem(optionsScreenItems[k]);
		}
		optionsScreen.setSelected(selectedIndex);

		// System.gc();
	}

	public boolean _charvZ() {
		if (m_oZ) {
			m_oZ = false;
			return true;
		} else {
			return m_oZ;
		}
	}

	@Override
	public void handleAction(MenuElement item) {
		int k = 0;
		do {
			if (k >= optionsScreenItems.length)
				break;
			if (item == optionsScreenItems[k]) {
				selectedIndex = k;
				updateSelectedOption();
				break;
			}
			k++;
		} while (true);

		handler.setCurrentMenu(screen, true);
		handler.handleAction(this);
	}

	@Override
	public MenuScreen getCurrentMenu() {
		return optionsScreen;
	}

	@Override
	public void setCurrentMenu(MenuScreen e1, boolean flag) {
	}

	@Override
	protected void updateViewText() {
		if (textView != null && textView instanceof MenuTextView)
			((MenuTextView) textView).setTextOnUiThread(getTextForView());
		if (optionTextView != null) optionTextView.setTextOnUiThread(selectedOption);
	}

	@Override
	public void performAction(int k) {
		// logDebug("OptionMenuElement performAction: k = " + k);
		switch (k) {
			case MenuScreen.KEY_FIRE:
				if (isOnOffToggle) {
					selectedIndex++;
					if (selectedIndex > 1)
						selectedIndex = 0;
					if (selectedIndex == 1)
						selectedOption = getString(R.string.off);
					else
						selectedOption = getString(R.string.on);
					updateViewText();
					handler.handleAction(this);
					return;
				} else {
					m_oZ = true;
					handler.handleAction(this);
					return;
				}

			case MenuScreen.KEY_RIGHT:
				if (isOnOffToggle) {
					if (selectedIndex == 1) {
						selectedIndex = 0;
						selectedOption = getString(R.string.on);
						handler.handleAction(this);
						updateViewText();
					}
					return;
				}
				selectedIndex++;
				if (selectedIndex > options.length - 1)
					selectedIndex = options.length - 1;
				else
					handler.handleAction(this);
				updateSelectedOption();
				return;

			case MenuScreen.KEY_LEFT: // '\003'
				if (isOnOffToggle) {
					if (selectedIndex == 0) {
						selectedIndex = 1;
						selectedOption = getString(R.string.off);
						handler.handleAction(this);
						updateViewText();
					}
					return;
				}
				selectedIndex--;
				if (selectedIndex < 0) {
					selectedIndex = 0;
				} else {
					updateSelectedOption();
					handler.handleAction(this);
				}
				updateSelectedOption();
				break;
		}
	}

	public void setScreen(MenuScreen screen) {
		this.screen = screen;
	}

	@Override
	protected String getTextForView() {
		return text + ": ";
	}

	@Override
	protected void onHighlightChanged() {
		lockImage.setImageResource(ActionMenuElement.locks[isHighlighted ? 2 : 0]);
	}

}