Created
June 20, 2013 14:27
-
-
Save porcelli/5823190 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.uberfire.client.screens.hellolienzo; | |
| import javax.annotation.PostConstruct; | |
| import javax.enterprise.context.Dependent; | |
| import javax.enterprise.event.Event; | |
| import javax.inject.Inject; | |
| import com.emitrom.lienzo.client.core.event.NodeMouseDownEvent; | |
| import com.emitrom.lienzo.client.core.event.NodeMouseDownHandler; | |
| import com.emitrom.lienzo.client.core.shape.Circle; | |
| import com.emitrom.lienzo.client.core.shape.Layer; | |
| import com.emitrom.lienzo.client.core.shape.Rectangle; | |
| import com.emitrom.lienzo.client.core.shape.Shape; | |
| import com.emitrom.lienzo.client.widget.LienzoPanel; | |
| import com.emitrom.lienzo.shared.core.types.Color; | |
| import com.google.gwt.dom.client.Style; | |
| import com.google.gwt.dom.client.Style.Position; | |
| import com.google.gwt.dom.client.Style.Unit; | |
| 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.shared.HandlerRegistration; | |
| import com.google.gwt.user.client.ui.Composite; | |
| import com.google.gwt.user.client.ui.RootPanel; | |
| @Dependent | |
| public class ShapesGroup extends Composite { | |
| @Inject | |
| private Event<ShapeAddEvent> shapeAddEvent; | |
| private Layer layer; | |
| private LienzoPanel panel; | |
| @PostConstruct | |
| public void init() { | |
| panel = new LienzoPanel( 200, 300 ); | |
| initWidget( panel ); | |
| layer = new Layer(); | |
| panel.add( layer ); | |
| layer.add( new PaletteShape() { | |
| @Override | |
| public Shape<?> getShape() { | |
| return new Rectangle( 30, 30 ) {{ | |
| setY( 5 ); | |
| setX( 5 ); | |
| setStrokeColor( Color.rgbToBrowserHexColor( 255, 0, 0 ) ); | |
| setStrokeWidth( 3 ); | |
| setFillColor( Color.rgbToBrowserHexColor( 0, 255, 255 ) ); | |
| setDraggable( false ); | |
| }}; | |
| } | |
| @Override | |
| public void setPos( Shape<?> shape ) { | |
| shape.setX( 2 ).setY( 2 ); | |
| } | |
| }.build() ); | |
| layer.add( new PaletteShape() { | |
| @Override | |
| public Shape<?> getShape() { | |
| return new Circle( 15 ) {{ | |
| setY( 20 ); | |
| setX( 65 ); | |
| setStrokeColor( Color.rgbToBrowserHexColor( 255, 0, 0 ) ); | |
| setStrokeWidth( 3 ); | |
| setFillColor( Color.rgbToBrowserHexColor( 0, 255, 255 ) ); | |
| setDraggable( false ); | |
| }}; | |
| } | |
| @Override | |
| public void setPos( Shape<?> shape ) { | |
| shape.setX( 17 ).setY( 17 ); | |
| } | |
| }.build() ); | |
| layer.draw(); | |
| } | |
| abstract class PaletteShape { | |
| public abstract Shape<?> getShape(); | |
| public abstract void setPos( final Shape<?> shape ); | |
| public final Shape<?> build() { | |
| final Shape<?> shape = getShape(); | |
| shape.addNodeMouseDownHandler( new NodeMouseDownHandler() { | |
| public void onNodeMouseDown( NodeMouseDownEvent event ) { | |
| final Layer floatingLayer = new Layer(); | |
| final LienzoPanel floatingPanel = new LienzoPanel( 35, 35 ); | |
| final Style style = floatingPanel.getElement().getStyle(); | |
| style.setPosition( Position.ABSOLUTE ); | |
| style.setLeft( panel.getAbsoluteLeft() + event.getX(), Unit.PX ); | |
| style.setTop( panel.getAbsoluteTop() + event.getY(), Unit.PX ); | |
| style.setZIndex( 100 ); | |
| final Shape floatingShape = getShape(); | |
| setPos( floatingShape ); | |
| floatingShape.setDraggable( false ); | |
| floatingLayer.add( floatingShape ); | |
| floatingPanel.add( floatingLayer ); | |
| floatingLayer.draw(); | |
| RootPanel.get().add( floatingPanel ); | |
| final HandlerRegistration[] handlerRegs = new HandlerRegistration[ 2 ]; | |
| handlerRegs[ 0 ] = RootPanel.get().addDomHandler( new MouseMoveHandler() { | |
| public void onMouseMove( MouseMoveEvent mouseMoveEvent ) { | |
| style.setLeft( mouseMoveEvent.getX(), Unit.PX ); | |
| style.setTop( mouseMoveEvent.getY(), Unit.PX ); | |
| } | |
| }, MouseMoveEvent.getType() ); | |
| handlerRegs[ 1 ] = RootPanel.get().addDomHandler( new MouseUpHandler() { | |
| public void onMouseUp( MouseUpEvent mouseUpEvent ) { | |
| handlerRegs[ 0 ].removeHandler(); | |
| handlerRegs[ 1 ].removeHandler(); | |
| RootPanel.get().remove( floatingPanel ); | |
| shapeAddEvent.fire( new ShapeAddEvent( floatingShape, mouseUpEvent.getX(), mouseUpEvent.getY() ) ); | |
| } | |
| }, MouseUpEvent.getType() ); | |
| } | |
| } ); | |
| return shape; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment