-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_output.py
150 lines (121 loc) · 4.48 KB
/
input_output.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import flet as ft
from CurveNode import CurveNode
class PointConnectOutput(ft.Draggable):
def __init__(self,id=0):
super().__init__(content = ft.Container(
width=20,
height=20,
shape=ft.BoxShape.CIRCLE,
bgcolor='white',
))
self.id_ = id
self.group = 'start'
self._offset_ = 0.0
def calculate_self_position(self):
top=self.node.top
height=self.node.height
circle_size=20
bar_height=30
circle_count=len(self.node.outputs)
if circle_count == 0:
return []
available_height = height - 2 * bar_height - circle_count * circle_size
if available_height < 0:
raise ValueError("Not enough space to fit the circles")
spacing = available_height / (circle_count + 1)
pos = top + bar_height + spacing * (self.id_ + 1) + circle_size * self.id_ + circle_size / 2
self._offset_ = pos - top
def update_offsets(self):
self.calculate_self_position()
self.node.offsets_out.append(self._offset_)
def did_mount(self):
self.node = self.parent.parent.parent.parent
self.update_offsets()
return super().did_mount()
class PointConnectInput(ft.Row):
def __init__(self,text='None',id=0):
super().__init__()
self.text = text
self.id_ = id
self.node = None
self._offset_ = 0.0
self.controls = self.gen_controls()
def gen_controls(self):
self.builder = ft.Container(
width=20,
height=20,
shape=ft.BoxShape.CIRCLE,
bgcolor=ft.colors.GREY,
shadow=ft.BoxShadow(spread_radius=2,blur_radius=0,color=ft.colors.GREY_800)
)
self.drag=ft.DragTarget(
on_accept=self.accept,
group='start',
content=ft.Draggable(self.builder,on_drag_start=self.remove_link)
)
text = ft.Text(self.text)
return [self.drag,text]
def remove_link(self,e):
for i in self.stack.controls:
try:
if i.id_input == self.control_id_:
if i.id_output == self.id_:
self.stack.controls.remove(i)
self.node.curves.remove([i,'in'])
self.control_.node.curves.remove([i,'out'])
except:
pass
self.stack.update()
self.node.update()
self.control_.update()
def calculate_self_position(self):
top=self.node.top
height=self.node.height
circle_size=20
bar_height=30
circle_count=len(self.node.inputs)
if circle_count == 0:
return []
available_height = height - 2 * bar_height - circle_count * circle_size
if available_height < 0:
raise ValueError("Not enough space to fit the circles")
spacing = available_height / (circle_count + 1)
pos = top + bar_height + spacing * (self.id_ + 1) + circle_size * self.id_ + circle_size / 2
self._offset_ = pos - top
print(self._offset_)
def accept(self, e: ft.DragTargetAcceptEvent):
control = self.page.get_control(e.src_id)
control: PointConnectOutput
self.control_id_ = control.id_
self.control_ = control
self.stack: ft.Stack
left = self.node.left
curve = CurveNode(
start=[
control.node.left+control.node.width,
control._offset_+control.node.top
],
end=[
left,
self._offset_+self.node.top
],
node_in=control.node,
node_out=self.node,
id_out=self.id_,
id_in=control.id_
)
self.stack.controls.append(
curve
)
self.node.curves.append([curve,'in'])
control.node.curves.append([curve,'out'])
self.stack.update()
def update_offsets(self):
self.calculate_self_position()
self.node.offsets_in.append(self._offset_)
def did_mount(self):
self.node = self.parent.parent.parent
self.stack = self.node.stack
print(F'ID INPUT: {self.id_}')
self.update_offsets()
return super().did_mount()