-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrabbableController.pde
57 lines (43 loc) · 1.34 KB
/
GrabbableController.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class GrabbableController extends Collidable implements Controller {
private GrabbableView _view;
private boolean _pressed;
private Vector2 _dragOffset;
GrabbableController(GrabbableView view) {
this._view = view;
this._pressed = false;
this._dragOffset = new Vector2(0, 0);
}
boolean isCursorInside() {
return dist(mouseX, mouseY, _view.getPosition().x, _view.getPosition().y) <= _view.getRadius();
}
boolean canDrag() { return true; };
boolean canHover() { return true; };
void willPress() {};
void didPress() {};
void mouseHover() {
_view.setHovered(canHover() && isCursorInside());
}
void mouseMove() {
_view.setPosition(mouseX, mouseY);
}
void mousePress() {
_pressed = isCursorInside();
if (_pressed) willPress();
_view.setPressed(_pressed);
_dragOffset.x = _view.getPosition().x - mouseX;
_dragOffset.y = _view.getPosition().y - mouseY;
}
void mouseDrag() {
if (canDrag() && _pressed) {
_view.setPosition(mouseX + _dragOffset.x, mouseY + _dragOffset.y);
}
}
void mouseRelease() {
if (_pressed) didPress();
_view.setPressed(false);
_pressed = false;
}
GrabbableView getView() { return _view; }
Vector2 getCenter() { return _view.getPosition().clone(); }
float getRadius() { return _view.getRadius(); }
}