package jetbrains.mps.lang.editor.diagram.runtime.jetpad.views;
/*Generated by MPS */
import jetbrains.jetpad.projectional.view.PolyLineView;
import jetbrains.jetpad.model.property.Property;
import jetbrains.jetpad.values.Color;
import jetbrains.jetpad.model.property.ValueProperty;
import jetbrains.jetpad.geometry.Vector;
import jetbrains.jetpad.mapper.Mapper;
import jetbrains.jetpad.mapper.Synchronizers;
import jetbrains.jetpad.model.property.WritableProperty;
import jetbrains.jetpad.base.Registration;
import jetbrains.jetpad.projectional.view.ViewTrait;
import jetbrains.jetpad.projectional.view.ViewTraitBuilder;
import jetbrains.jetpad.projectional.view.ViewEvents;
import jetbrains.jetpad.projectional.view.ViewEventHandler;
import jetbrains.jetpad.event.MouseEvent;
import jetbrains.jetpad.projectional.view.View;
public class ResizeHandleView extends PolyLineView {
public static final int DEFAULT_HALF_WIDTH = 3;
public Property<Color> color = new ValueProperty<Color>(Color.GRAY);
public Property<Color> backgroundColor = new ValueProperty<Color>(Color.LIGHT_GRAY);
public Property<Integer> halfWidth = new ValueProperty<Integer>(DEFAULT_HALF_WIDTH);
public Property<Vector> centerLocation = new ValueProperty<Vector>(new Vector(0, 0));
public Property<DragHandler> dragHandler = new ValueProperty<DragHandler>();
private ResizeHandleView() {
new Mapper<ResizeHandleView, ResizeHandleView>(this, this) {
@Override
protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
super.registerSynchronizers(configuration);
configuration.add(Synchronizers.forProperty(color, color()));
configuration.add(Synchronizers.forProperty(backgroundColor, background()));
configuration.add(Synchronizers.forProperty(halfWidth, new Runnable() {
public void run() {
updateLocation(centerLocation.get(), halfWidth.get());
}
}));
configuration.add(Synchronizers.forProperty(centerLocation, new Runnable() {
public void run() {
updateLocation(centerLocation.get(), halfWidth.get());
}
}));
configuration.add(Synchronizers.forProperty(dragHandler, new WritableProperty<DragHandler>() {
private Registration myRegistration;
public void set(DragHandler handler) {
if (myRegistration != null) {
myRegistration.remove();
}
if (handler != null) {
myRegistration = addTrait(getResizeHandlingTrait());
}
}
}));
}
}.attachRoot();
}
public ResizeHandleView(Vector location) {
this();
centerLocation.set(location);
}
@Override
protected boolean contains(Vector vector) {
return dragHandler.get() != null;
}
private ViewTrait getResizeHandlingTrait() {
return new ViewTraitBuilder().on(ViewEvents.MOUSE_PRESSED, new ViewEventHandler<MouseEvent>() {
public void handle(View view, MouseEvent event) {
dragHandler.get().dragStarted(event.location());
event.consume();
}
}).on(ViewEvents.MOUSE_DRAGGED, new ViewEventHandler<MouseEvent>() {
public void handle(View view, MouseEvent event) {
dragHandler.get().updatePosition(event.location());
}
}).on(ViewEvents.MOUSE_RELEASED, new ViewEventHandler<MouseEvent>() {
public void handle(View view, MouseEvent event) {
dragHandler.get().dragStopped(event.location());
}
}).build();
}
private void updateLocation(Vector location, int halfWidth) {
points.clear();
points.add(new Vector(location.x - halfWidth, location.y - halfWidth));
points.add(new Vector(location.x + halfWidth, location.y - halfWidth));
points.add(new Vector(location.x + halfWidth, location.y + halfWidth));
points.add(new Vector(location.x - halfWidth, location.y + halfWidth));
points.add(new Vector(location.x - halfWidth, location.y - halfWidth));
}
}