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

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import org.happysanta.gd.Global;
import org.happysanta.gd.Menu.Views.MenuHelmetView;
import org.happysanta.gd.Menu.Views.MenuTextView;
import org.happysanta.gd.R;

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

public class ClickableMenuElement
		implements MenuElement {

	public static final int TEXT_SIZE = 20;
	public static final int PADDING_TOP = 5;
	protected View textView;
	protected String text;
	protected View.OnTouchListener onTouchListener;
	protected LinearLayout layout;
	protected MenuHelmetView helmet;
	protected OnMenuElementHighlightListener onMenuElementHighlightListener = null;
	protected boolean isHighlighted = false;
	protected Thread originalThread = null;
	protected boolean disabled = false;

	public ClickableMenuElement() {
	}

	public ClickableMenuElement(String text) {
		this.text = text;
		originalThread = Thread.currentThread();

		createAllViews();
	}

	protected boolean inViewBounds(View view, int x, int y) {
		Rect rect = new Rect();
		int location[] = new int[2];

		view.getDrawingRect(rect);
		view.getLocationOnScreen(location);
		rect.offset(location[0], location[1]);

		return rect.contains(x, y);
	}

	protected void createAllViews() {
		final ClickableMenuElement self = this;
		Context context = getGDActivity();

		layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

		helmet = new MenuHelmetView(context);
		helmet.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));

		/*if (!isSDK11OrHigher()) {
			helmet.setMeasuredHeight(true);
		}*/

		onTouchListener = new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				if (disabled) return false;

				switch (motionEvent.getAction()) {
					case MotionEvent.ACTION_DOWN:
						view.setSelected(true);
						helmet.setShow(true);

						if (onMenuElementHighlightListener != null)
							onMenuElementHighlightListener.onElementHighlight(self);

						setHighlighted(true);
						break;

					case MotionEvent.ACTION_CANCEL:
					case MotionEvent.ACTION_UP:
						view.setSelected(false);

						if (motionEvent.getAction() == MotionEvent.ACTION_UP && inViewBounds(view, (int) motionEvent.getRawX(), (int) motionEvent.getRawY())) {
							performAction(MenuScreen.KEY_FIRE);
						}

						setHighlighted(false);
						break;

					case MotionEvent.ACTION_MOVE:
						if (!inViewBounds(view, (int) motionEvent.getRawX(), (int) motionEvent.getRawY())) {
							view.setSelected(false);
							setHighlighted(false);
						} else {
							view.setSelected(true);
							setHighlighted(true);
						}
						break;
				}
				return true;
			}
		};

		textView = createMainView();

		layout.addView(helmet);
		layout.addView(textView);
		layout.setOnTouchListener(onTouchListener);
	}

	protected View createMainView() {
		Context context = getGDActivity();
		MenuTextView mtv = new MenuTextView(context);
		mtv.setText(getTextForView());
		mtv.setTextColor(defaultColorStateList());
		mtv.setTypeface(Global.robotoCondensedTypeface);
		mtv.setTextSize(TEXT_SIZE);
		mtv.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT
		));
		mtv.setPadding(0, getDp(PADDING_TOP), 0, getDp(PADDING_TOP));

		return mtv;
	}

	protected ColorStateList defaultColorStateList() {
		return getGDActivity().getResources().getColorStateList(R.drawable.menu_item_color);
	}

	@Override
	public boolean isSelectable() {
		return true;
	}

	@Override
	public View getView() {
		return layout;
	}

	protected MenuTextView getMenuTextView() {
		return (MenuTextView) textView;
	}

	@Override
	public void setText(String text) {
		this.text = text;
		updateViewText();
	}

	public String getText() {
		return text;
	}

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

	protected String getTextForView() {
		return text;
	}

	@Override
	public void performAction(int k) {

	}

	public void setOnHighlightListener(OnMenuElementHighlightListener listener) {
		onMenuElementHighlightListener = listener;
	}

	public void showHelmet() {
		helmet.setShow(true);
	}

	private void setHighlighted(boolean highlighted) {
		isHighlighted = highlighted;
		onHighlightChanged();
	}

	protected void onHighlightChanged() {
	}

	public boolean isDisabled() {
		return disabled;
	}

}