MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / Scrolling

Class Scrolling

src/main/java/fieldbox/boxes/plugins/Scrolling.java:17–109  ·  view source on GitHub ↗

Adds mouse wheel (and, thus two-finger drag on OS X) pan support to the canvas

Source from the content-addressed store, hash-verified

15public class Scrolling extends Box {
16
17 public Scrolling(Box root_unused) {
18 this.properties.put(Planes.plane, "__always__");
19 this.properties.putToMap(Mouse.onMouseScroll, "__scrolling__", e -> {
20
21 if (e.properties.isTrue(FieldBoxWindow.consumed, false)) return;
22
23 Log.log("scrolling", () -> "not consumed");
24 if (e.after.dwheel != 0 || e.after.dwheely != 0) {
25 this.find(Drawing.drawing, this.both())
26 .findFirst()
27 .ifPresent(x ->
28 scrollForMouseEvent(Scrolling.this, e, x));
29 }
30 });
31 }
32
33 static public void scrollForMouseEvent(Box from, Window.Event<Window.MouseState> e, Drawing x) {
34
35 if (e.after.keyboardState.isShiftDown()) {
36 Vec2 t = new Vec2(x.getScale());
37
38 Vec2 dm = new Vec2(e.after.mx, e.after.my);
39
40 Vec2 d = new Vec2(e.after.x, e.after.y);
41
42
43 double sc = x.getScale().x;
44
45 t.x = t.y = t.y * Math.pow(2, e.after.dwheely / 50f);
46
47 t.x = Math.max(0.02f, Math.min(50, t.x));
48 t.y = Math.max(0.02f, Math.min(50, t.y));
49
50 x.setScale(from, t);
51
52 double r = (t.x / sc);
53
54 Vec2 trans;
55 {
56 Vec2 dm2 = windowSystemToDrawingSystem(d, t, x.getTranslation());
57 x.setTranslation(from, trans = x.getTranslation().add(new Vec2(dm).sub(dm2).mul(-t.x)));
58 }
59 } else {
60 Vec2 t = x.getTranslation();
61
62 double sc = x.getScale().x;
63
64 t.x += e.after.dwheel * 8;
65 t.y += e.after.dwheely * 8;
66
67 x.setTranslation(from, t);
68 }
69
70 FrameManipulation.continueTranslationFeedback(from, false);
71
72 }
73
74 /**

Callers

nothing calls this directly

Calls 2

dictMethod · 0.95
getFloatMethod · 0.80

Tested by

no test coverage detected