aboutsummaryrefslogtreecommitdiff
path: root/src/org/happysanta/gd/Menu/NameInputMenuScreen.java
blob: e742c7c8e4946defc93e9c15de5fbbff0df1061c (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
package org.happysanta.gd.Menu;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.happysanta.gd.Global;
import org.happysanta.gd.Menu.Views.MenuLinearLayout;
import org.happysanta.gd.Menu.Views.MenuTextView;

import static org.happysanta.gd.Helpers.getDp;
import static org.happysanta.gd.Helpers.getGDActivity;
import static org.happysanta.gd.Helpers.getGameMenu;

public class NameInputMenuScreen extends MenuScreen {

	protected static final String CURSOR = "^";
	protected static final int WORD_SPACE = 3;

	protected static int wordWidth = 0;

	protected int cursorPosition = 0;
	protected byte chars[];

	protected MenuTextView nameTextViews[];
	protected MenuTextView cursorTextViews[];
	protected MenuLinearLayout nameLayout;
	protected MenuLinearLayout cursorLayout;

	static {
		wordWidth = getWordWidth();
	}

	public NameInputMenuScreen(String title, MenuScreen navTarget, byte nameChars[]) {
		super(title, navTarget);

		chars = nameChars;

		Context context = getGDActivity();

		nameTextViews = new MenuTextView[3];
		cursorTextViews = new MenuTextView[3];

		nameLayout = new MenuLinearLayout(context);
		nameLayout.setOrientation(LinearLayout.HORIZONTAL);

		cursorLayout = new MenuLinearLayout(context);
		cursorLayout.setOrientation(LinearLayout.HORIZONTAL);

		for (int i = 0; i < 3; i++) {
			nameTextViews[i] = createTextView();
			nameLayout.addView(nameTextViews[i]);

			cursorTextViews[i] = createTextView();
			cursorLayout.addView(cursorTextViews[i]);
		}

		layout.addView(nameLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		layout.addView(cursorLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

		updateText();
		updateCursorPosition();
	}

	protected MenuTextView createTextView() {
		Context context = getGDActivity();
		MenuTextView textView = new MenuTextView(context);
		textView.setTextColor(0xff000000);
		textView.setTypeface(Global.robotoCondensedTypeface);
		textView.setTextSize(ClickableMenuElement.TEXT_SIZE);
		textView.setLayoutParams(new LinearLayout.LayoutParams(
				wordWidth,
				ViewGroup.LayoutParams.WRAP_CONTENT
		));
		return textView;
	}

	protected static int getWordWidth() {
		Context context = getGDActivity();

		String text = "W";
		TextView textView = new TextView(context);
		textView.setTextSize(ClickableMenuElement.TEXT_SIZE);
		textView.setTypeface(Global.robotoCondensedTypeface);

		Rect bounds = new Rect();

		Paint textPaint = textView.getPaint();
		textPaint.getTextBounds(text, 0, text.length(), bounds);

		return bounds.width() + getDp(WORD_SPACE);
	}

	@Override
	public void performAction(int k) {
		switch (k) {
			default:
				break;

			case MenuScreen.KEY_FIRE: // select
				if (cursorPosition == 2) {
					getGameMenu().setCurrentMenu(navTarget, false);
				} else {
					cursorPosition++;
					updateCursorPosition();
				}
				break;

			case MenuScreen.KEY_RIGHT: // right
				cursorPosition++;
				if (cursorPosition > 2) {
					cursorPosition = 2;
				}
				updateCursorPosition();
				break;

			case MenuScreen.KEY_LEFT: // left
				cursorPosition--;
				if (cursorPosition < 0)
					cursorPosition = 0;
				updateCursorPosition();
				break;

			case MenuScreen.KEY_UP: // up
				if (chars[cursorPosition] == 32) {
					chars[cursorPosition] = 65;
					updateText();
					break;
				}
				chars[cursorPosition]++;
				if (chars[cursorPosition] > 90) {
					chars[cursorPosition] = 32;
				}
				updateText();
				break;

			case MenuScreen.KEY_DOWN: // down
				if (chars[cursorPosition] == 32) {
					chars[cursorPosition] = 90;
					updateText();
					break;
				}
				chars[cursorPosition]--;
				if (chars[cursorPosition] < 65) {
					chars[cursorPosition] = 32;
				}
				updateText();
				break;
		}
	}

	protected void updateText() {
		for (int i = 0; i < nameTextViews.length; i++) {
			nameTextViews[i].setTextOnUiThread(String.valueOf((char) chars[i]));
		}
	}

	protected void updateCursorPosition() {
		for (int i = 0; i < cursorTextViews.length; i++) {
			cursorTextViews[i].setTextOnUiThread(i == cursorPosition ? CURSOR : "");
		}
	}

	public byte[] getChars() {
		return chars;
	}

	public void resetCursorPosition() {
		cursorPosition = 0;
		updateCursorPosition();
	}

}