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

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import org.happysanta.gd.Game.Bitmap;
import org.happysanta.gd.Global;

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

public class MenuHelmetView extends View {

	protected static final int WIDTH = 8;
	protected static final int HEIGHT = 8;
	/*protected static final int PADDING_LEFT = 0;
	protected static final int PADDING_TOP = 5;
	protected static final int PADDING_RIGHT = 5;
	protected static final int PADDING_BOTTOM = 0;*/

	protected static int angle = 0;
	protected static long angleLastMs = 0;
	protected static final int angleInterval = 50;
	protected static final int angleDelta = 10;

	protected boolean show = false;
	protected boolean _setMeasuredHeight = false;
	protected Bitmap helmet = Bitmap.get(Bitmap.HELMET);
	protected static MenuHelmetView lastActive = null;

	public static void clearStaticFields() {
		lastActive = null;
		angle = 0;
		angleLastMs = 0;
	}

	public MenuHelmetView(Context context) {
		super(context);
	}

	public MenuHelmetView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.save();
		canvas.scale(Global.density, Global.density);

		drawHelmet(canvas);

		canvas.restore();
		invalidate();
	}

	protected void drawHelmet(Canvas canvas) {
		if (show) {
			long ms = System.currentTimeMillis();
			if (angleLastMs == 0 || ms - angleLastMs >= angleInterval) {
				angle += angleDelta;
				if (angle >= 360) angle -= 360;
				angleLastMs = ms;
			}

			int y = getScaledHeight() / 2 - helmet.getHeightDp() / 2;

			canvas.save();
			canvas.rotate(angle, helmet.getWidthDp() / 2, y + helmet.getHeightDp() / 2);
			canvas.drawBitmap(helmet.bitmap, new Rect(0, 0, helmet.getWidth(), helmet.getHeight()), new RectF(0, y, helmet.getWidthDp(), y + helmet.getHeightDp()), null);
			canvas.restore();
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(getDp(WIDTH * 2.2f));
		int height = heightMeasureSpec;
		if (_setMeasuredHeight)
			height = MeasureSpec.getSize(getDp(HEIGHT * 2.2f));
		else if (!isSDK11OrHigher()) {
			height = MeasureSpec.getSize(getDp(HEIGHT * 4.5f));
		}
		setMeasuredDimension(width, height);
	}

	public void setShow(boolean show) {
		setShow(show, true);
	}

	public void setShow(boolean show, boolean checkLast) {
		if (checkLast && lastActive != null) {
			lastActive.setShow(false, false);
		}
		this.show = show;
		lastActive = this;
	}

	protected int getScaledHeight() {
		return Math.round(getHeight() / Global.density);
	}

	public void setMeasuredHeight(boolean setMeasuredHeight) {
		this._setMeasuredHeight = setMeasuredHeight;
	}

}