examples.geometry.AbstractExample.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.AbstractExample.java

Source

/*******************************************************************************
 * Copyright (c) 2012 itemis AG and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Matthias Wienand (itemis AG) - initial API and implementation
 *     
 *******************************************************************************/
package examples.geometry;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.FillStrokeStyle;
import com.google.gwt.dom.client.Touch;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
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.TouchMoveEvent;
import com.google.gwt.event.dom.client.TouchMoveHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.event.shared.EventBus;

import examples.geometry.DrawEvent.DrawEventHandler;

abstract public class AbstractExample implements GWTExample {

    protected Canvas canvas;
    protected ControllableShape[] shapes;
    protected ControllableShape targetShape;
    protected EventBus eventBus;

    public AbstractExample(EventBus eventBus, Canvas canvas) {
        this.canvas = canvas;
        this.eventBus = eventBus;
        setEventHandlers();
    }

    protected ControllableShape findShape(int x, int y) {
        for (ControllableShape shape : shapes) {
            if (shape.isActive()) {
                for (int i = 0; i < shape.controlPoints.size(); i++) {
                    ControlPoint cp = shape.controlPoints.get(i);
                    double dx = cp.getX() - x;
                    double dy = cp.getY() - y;
                    if (dx * dx + dy * dy < shape.controlRadius * shape.controlRadius) {
                        shape.isDragging = true;
                        shape.dragPointIndex = i;
                        return shape;
                    }
                }
            }
        }
        return null;
    }

    protected void setEventHandlers() {
        eventBus.addHandler(DrawEvent.TYPE, new DrawEventHandler() {
            @Override
            public void onDraw() {
                draw();
            }
        });
        canvas.addMouseMoveHandler(new MouseMoveHandler() {
            @Override
            public void onMouseMove(MouseMoveEvent event) {
                event.preventDefault();
                if (targetShape != null) {
                    targetShape.onMove(event.getX(), event.getY());
                }
            }
        });
        canvas.addMouseDownHandler(new MouseDownHandler() {
            @Override
            public void onMouseDown(MouseDownEvent event) {
                event.preventDefault();
                targetShape = findShape(event.getX(), event.getY());
            }
        });
        canvas.addMouseUpHandler(new MouseUpHandler() {
            @Override
            public void onMouseUp(MouseUpEvent event) {
                event.preventDefault();
                if (targetShape != null) {
                    targetShape.isDragging = false;
                    targetShape = null;
                }
            }
        });
        canvas.addTouchStartHandler(new TouchStartHandler() {
            @Override
            public void onTouchStart(TouchStartEvent event) {
                event.preventDefault();
                if (event.getTouches().length() > 0) {
                    Touch touch = event.getTouches().get(0);
                    int x = touch.getRelativeX(canvas.getElement());
                    int y = touch.getRelativeX(canvas.getElement());
                    targetShape = findShape(x, y);
                }
                event.preventDefault();
            }
        });
        canvas.addTouchMoveHandler(new TouchMoveHandler() {
            @Override
            public void onTouchMove(TouchMoveEvent event) {
                event.preventDefault();
                if (targetShape != null && event.getTouches().length() > 0) {
                    Touch touch = event.getTouches().get(0);
                    int x = touch.getRelativeX(canvas.getElement());
                    int y = touch.getRelativeX(canvas.getElement());
                    targetShape.onMove(x, y);
                }
                event.preventDefault();
            }
        });
        canvas.addTouchEndHandler(new TouchEndHandler() {
            @Override
            public void onTouchEnd(TouchEndEvent event) {
                event.preventDefault();
                if (targetShape != null) {
                    targetShape.isDragging = false;
                    targetShape = null;
                }
            }
        });
    }

    protected abstract ControllableShape[] createShapes(Canvas canvas, EventBus eventBus);

    protected ControllableShape[] getControllableShapes() {
        if (shapes == null) {
            shapes = createShapes(canvas, eventBus);
        }
        return shapes;
    }

    public void remove() {
        Context2d context = canvas.getContext2d();
        context.clearRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
    }

    @Override
    public void draw() {
        Context2d context = canvas.getContext2d();
        // reset
        context.clearRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
        context.setFillStyle("black");
        context.setStrokeStyle("black");
        context.setLineWidth(1);

        for (ControllableShape shape : getControllableShapes()) {
            //         e.gc.setForeground(canvas.getDisplay().getSystemColor(shape.shapeColor));
            //         e.gc.setBackground(canvas.getDisplay().getSystemColor(shape.shapeColor));

            shape.onDraw(canvas);
        }

        for (ControllableShape shape : getControllableShapes()) {
            if (shape.isActive()) {
                FillStrokeStyle fillStyle = context.getFillStyle();

                for (ControlPoint cp : shape.controlPoints) {
                    context.beginPath();
                    context.arc(cp.getX(), cp.getY(), shape.controlRadius, 0, 180);
                    context.setFillStyle(shape.controlColor);
                    context.fill();
                    context.closePath();
                }
                context.setFillStyle(fillStyle);
            }
        }
    }

}