-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.py
More file actions
51 lines (44 loc) · 1.6 KB
/
selection.py
File metadata and controls
51 lines (44 loc) · 1.6 KB
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
class NodeSelection:
def __init__(self):
self.rect_start = None
self.rect_end = None
self.nodes = []
self.selected_nodes = []
def begin(self, start_pos):
self.rect_start = start_pos
self.rect_end = start_pos
self.nodes = []
def update(self, end_pos):
self.rect_end = end_pos
def finish(self, all_nodes, canvas_offset_x, canvas_offset_y, zoom):
if self.rect_start and self.rect_end:
x0, y0 = self.rect_start
x1, y1 = self.rect_end
rx0, ry0 = min(x0, x1), min(y0, y1)
rx1, ry1 = max(x0, x1), max(y0, y1)
self.nodes = []
for node in all_nodes:
nx = (node.x - canvas_offset_x) * zoom
ny = (node.y - canvas_offset_y) * zoom
nw = node.width * zoom
nh = node.height * zoom
if rx0 <= nx and nx + nw <= rx1 and ry0 <= ny and ny + nh <= ry1:
self.nodes.append(node)
self.rect_start = None
self.rect_end = None
def clear(self):
self.rect_start = None
self.rect_end = None
self.nodes = []
def is_active(self):
return self.rect_start is not None and self.rect_end is not None
def select_node(self, node, all_nodes):
# Deselect all others, select only this node
for n in all_nodes:
n.selected = False
node.selected = True
self.selected_nodes = [node]
def clear_selection(self, all_nodes):
for n in all_nodes:
n.selected = False
self.selected_nodes = []