package com.icystar.findnumber;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class NumbersView extends View {
private int gameIndex = -1;
public NumbersView(Context context) {
this(context, null);
initialize();
}
public NumbersView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
initialize();
}
public NumbersView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
public void initialize() {
setKeepScreenOn(true);
}
public int getGameIndex(int value)
{ return gameIndex; }
public void setGameIndex(int value) {
if (getGame() != null) getGame().onLayout(0, 0);
gameIndex = value;
if (getGame() != null) getGame().onLayout(getMeasuredWidth(), getMeasuredHeight());
}
public Game getGame()
{ return gameIndex >= 0 ? Game.games[gameIndex] : null; }
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Game game = getGame();
if (game != null) game.draw(canvas);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Game game = getGame();
if (game != null) game.onLayout(getMeasuredWidth(), getMeasuredHeight());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Game game = getGame();
if (game != null) game.onTouch(Math.round(event.getX()), Math.round(event.getY()));
return true;
}
return false;
}
}