-
Notifications
You must be signed in to change notification settings - Fork 0
/
Draggable.h
52 lines (43 loc) · 939 Bytes
/
Draggable.h
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
/*
* Copyright 2023, Andrea Anzani <andrea.anzani@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#pragma once
#include <Point.h>
#include <cmath>
class Draggable {
public:
Draggable() {};
void OnMouseDown(BPoint where )
{
fDragging = false;
fTryDrag = true;
fDragPoint = where;
}
void OnMouseUp(BPoint where)
{
fTryDrag = false;
fDragging = false;
fDragPoint = BPoint(0, 0);
}
void OnMouseMoved(BPoint where)
{
if (fTryDrag) {
// initiate a drag if the mouse was moved far enough
BPoint offset = where - fDragPoint;
float dragDistance = sqrtf(offset.x * offset.x + offset.y * offset.y);
if (dragDistance >= 5.0f) {
fTryDrag = false;
fDragging = InitiateDrag(fDragPoint);
}
}
}
virtual bool InitiateDrag(BPoint where)
{
return false;
}
private:
bool fTryDrag = false;
bool fDragging = false;
BPoint fDragPoint = BPoint(0, 0);
};