Mouse handling of the escalator.

This commit is contained in:
Thomas Guimbretiere committed 2013-10-24 01:42:54 +02:00
1 parent 098c88676e
commit 4ad3e92fae
1 file changed
+67 -5
@@ -16,9 +16,12 @@ public class ViewportScrollable extends WidgetBase {
int yOffset = 0;
int sizeCursor = 8;
int maxValue = 0;
int step = 0;
boolean drag = false;
public Escalator(IWidget parent){
public Escalator(IWidget parent, int step){
this.parent = parent;
this.step = step;
}
public void setOffset(int yoffset){
@@ -29,12 +32,58 @@ public class ViewportScrollable extends WidgetBase {
this.maxValue = value;
}
public void setStep(int step){
this.step = step;
}
@Override
public void draw(Point pos) {
UIHelper.drawGradientRect(this.getLeft(), this.getTop(), this.getRight(), this.getBottom(), 1, 0xff999999, 0xff999999);
int offsetScaled = (int)(((double)this.getSize().getY() - (double)sizeCursor + 1) / (double)this.maxValue * (yOffset));
UIHelper.drawGradientRect(this.getLeft(), this.getTop() + offsetScaled, this.getRight(), this.getTop() + offsetScaled + sizeCursor, 1, 0xffffffff, 0xffffffff);
}
@Override
public void onMouseClick(MouseEvent event){
if (event.button == 0){
int offsetScaled = this.getTop() + (int)(((double)this.getSize().getY() - (double)sizeCursor + 1) / (double)this.maxValue * (yOffset));
if (event.y < offsetScaled)
this.yOffset += this.step;
else if (event.y > offsetScaled + sizeCursor)
this.yOffset -= this.step;
else
this.drag = true;
this.emit(Signal.VALUE_CHANGED, this.yOffset);
} else {
super.onMouseClick(event);
}
}
@Override
public void onMouseRelease(MouseEvent event){
if (event.button == 0)
this.drag = false;
super.onMouseRelease(event);
}
@Override
public void onMouseDrag(MouseEvent event){
if (this.drag){
int relativeY = (int)event.y - this.getTop();
double factor = ((double)this.getSize().getY() - (double)sizeCursor + 1) / (double)this.maxValue;
this.yOffset = (int)(relativeY / factor);
this.yOffset = Math.max(this.yOffset, this.maxValue);
this.emit(Signal.VALUE_CHANGED, this.yOffset);
}
else
super.onMouseDrag(event);
}
}
IWidget attachedWidget = null;
@@ -43,8 +92,8 @@ public class ViewportScrollable extends WidgetBase {
public ViewportScrollable( IWidget parent){
super(parent);
this.addWidget("Cropping", new LayoutCropping(null)).setGeometry(new WidgetGeometry(50.0, 50.0, 100.0, 100.0, CType.RELXY, CType.RELXY, WAlign.CENTER, WAlign.CENTER));
this.addWidget("Escalator", new Escalator(null)).setGeometry(new WidgetGeometry(100.0, 0, 8, 100.0, CType.RELXY, CType.REL_Y, WAlign.RIGHT, WAlign.TOP)).hide();
this.addWidget("Cropping", new LayoutCropping(null)).setGeometry(new WidgetGeometry(50.0, 50.0, 80.0, 100.0, CType.RELXY, CType.RELXY, WAlign.CENTER, WAlign.CENTER));
this.addWidget("Escalator", new Escalator(null, this.step * 5)).setGeometry(new WidgetGeometry(100.0, 0, 8, 100.0, CType.RELXY, CType.REL_Y, WAlign.RIGHT, WAlign.TOP)).hide();
}
public IWidget attachWidget(IWidget widget){
@@ -58,6 +107,7 @@ public class ViewportScrollable extends WidgetBase {
public IWidget setStep(int step){
this.step = step;
((Escalator)this.getWidget("Escalator")).setStep(this.step * 5);
return this;
}
@@ -90,10 +140,22 @@ public class ViewportScrollable extends WidgetBase {
@Override
public void onWidgetEvent(IWidget srcwidget, Signal signal, Object... params){
System.out.println(srcwidget.getSize());
if (srcwidget.equals(this.attachedWidget) && signal == Signal.GEOM_CHANGED){
((Escalator)this.getWidget("Escalator")).setMaxValue(this.getSize().getY() - srcwidget.getSize().getY());
}
if (srcwidget.equals(this.getWidget("Escalator")) && signal == Signal.VALUE_CHANGED){
this.yOffset = (Integer)params[0];
((LayoutCropping)this.getWidget("Cropping")).setOffsets(0, this.yOffset);
}
}
@Override
public void onMouseDrag(MouseEvent event){
if (this.getWidget("Escalator").shouldRender())
this.getWidget("Escalator").onMouseDrag(event);
else
super.onMouseDrag(event);
}
}