package com.gardeneaters.game_v0.client;
import com.gardeneaters.game_v0.rules.Play;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.dom.client.Touch;
import com.google.gwt.event.dom.client.GestureStartEvent;
import com.google.gwt.event.dom.client.GestureStartHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.TouchEndEvent;
import com.google.gwt.event.dom.client.TouchEndHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
public class GardenEaters implements EntryPoint {
static final String holderId = "canvasholder";
static final String upgradeMessage = "Your browser does not support the HTML5 Canvas. Please upgrade your browser to view this demo.";
Canvas canvas;
Context2d context;
static int canvasHeight;
static int canvasWidth;
// mouse positions relative to canvas
int mouseStartX, mouseStartY, mouseEndX, mouseEndY;
//timer refresh rate, in milliseconds
static final int refreshRate = 25;
Play play;
HTMLPanel panel;
public void onModuleLoad() {
canvas = Canvas.createIfSupported();
if (canvas == null) {
RootPanel.get(holderId).add(new Label("Sorry, your browser doesn't support the HTML5 Canvas element"));
return;
}
canvasHeight = (int) (Window.getClientHeight()*0.95);
canvasWidth = (int) (canvasHeight*1.6666);
canvas.setStyleName("mainCanvas");
canvas.setWidth(canvasWidth + "px");
canvas.setCoordinateSpaceWidth(canvasWidth);
canvas.setHeight(canvasHeight + "px");
canvas.setCoordinateSpaceHeight(canvasHeight);
RootPanel.get(holderId).add(canvas);
context = canvas.getContext2d();
play = new Play(context);
Window.addResizeHandler(new ResizeHandler() {
@Override
public void onResize(ResizeEvent event) {
// TODO Auto-generated method stub
RootPanel.get(holderId).clear();
canvasHeight = (int) (Window.getClientHeight()*0.95);
canvasWidth = (int) (canvasHeight*1.6666);
canvas.setWidth(canvasWidth + "px");
canvas.setCoordinateSpaceWidth(canvasWidth);
canvas.setHeight(canvasHeight + "px");
canvas.setCoordinateSpaceHeight(canvasHeight);
RootPanel.get(holderId).add(canvas);
context = canvas.getContext2d();
play.resize();
}
});
//Initializing objects
//initializing handlers
initHandlers();
final Timer timer = new Timer() {
@Override
public void run() {
doUpdate();
}
};
timer.scheduleRepeating(refreshRate);
}
void doUpdate() {
play.update();
//logoGroup.update(mouseX, mouseY);
//logoGroup.draw(backBufferContext);
}
void initHandlers() {
canvas.addMouseDownHandler(new MouseDownHandler(){
@Override
public void onMouseDown(MouseDownEvent event) {
mouseStartX = event.getRelativeX(canvas.getElement());
mouseStartY = event.getRelativeY(canvas.getElement());
// garden.draw(mouseX, mouseY);
}
});
canvas.addMouseUpHandler(new MouseUpHandler(){
@Override
public void onMouseUp(MouseUpEvent event) {
mouseEndX = event.getRelativeX(canvas.getElement());
mouseEndY = event.getRelativeY(canvas.getElement());
play.click(mouseStartX, mouseStartY, mouseEndX, mouseEndY);
//garden.draw(mouseX, mouseY);
}
});
// canvas.addMouseOutHandler(new MouseOutHandler() {
// public void onMouseOut(MouseOutEvent event) {
// //mouseX = -200;
// // mouseY = -200;
// }
// });
canvas.addTouchStartHandler(new TouchStartHandler() {
@Override
public void onTouchStart(TouchStartEvent event) {
event.preventDefault();
if (event.getTouches().length() > 0) {
Touch touch = event.getTouches().get(0);
mouseStartX = touch.getRelativeX(canvas.getElement());
mouseStartY = touch.getRelativeY(canvas.getElement());
//garden.draw(mouseX,mouseY);
}
event.preventDefault();
}
});
canvas.addTouchEndHandler(new TouchEndHandler() {
public void onTouchEnd(TouchEndEvent event) {
event.preventDefault();
mouseEndX = -200;
mouseEndY = -200;
play.click(mouseStartX, mouseStartY, mouseEndX, mouseEndY);
}
});
canvas.addGestureStartHandler(new GestureStartHandler() {
public void onGestureStart(GestureStartEvent event) {
event.preventDefault();
}
});
}
}