1
+ import pygame
2
+ import time
3
+ from icons import *
4
+
5
+ def format_duration (duration ):
6
+ return str (duration ) + "s"
7
+
8
+ def semicircle (radius , color , inverted = False ):
9
+ output = pygame .Surface ((radius * 2 , radius * 2 ), pygame .SRCALPHA )
10
+ output .fill ((0 ,0 ,0 ,0 ))
11
+ pygame .draw .circle (output , color , (radius , radius ), radius )
12
+ pygame .draw .circle (output , (0 ,0 ,0 ), (radius , radius ), radius , 1 )
13
+ if inverted :
14
+ pygame .draw .rect (output , (0 ,0 ,0 ,0 ), (radius , 0 , radius * 2 , radius * 2 ))
15
+ else :
16
+ pygame .draw .rect (output , (0 ,0 ,0 ,0 ), (0 , 0 , radius , radius * 2 ))
17
+ return output
18
+
19
+ class GraphicNode :
20
+ def __init__ (self , screen , type , pos , connection = None , bar = False ):
21
+ self .bar = bar
22
+ self .screen = screen
23
+ self .type = type
24
+ self .value = 0
25
+ self .connection = None
26
+ self .connection_2 = None
27
+ self .width = 0
28
+ self .height = 0
29
+ self .duration = None
30
+ self .timer_type = None
31
+ self .timer = None
32
+ if self .type == 1 : # Digital Input
33
+ self .width = 40
34
+ self .height = 40
35
+ elif self .type == 2 : # Digital Output
36
+ self .width = 40
37
+ self .height = 40
38
+ self .connection = connection
39
+ elif self .type == 3 : # Timer
40
+ self .width = 80
41
+ self .height = 40
42
+ self .duration = 1.0
43
+ self .timer_type = 0 # 0=Delay, 1=On-Delay, 2=Off-Delay
44
+ self .timer = [0 ,0 ] # (Timestamp of new value, new value)
45
+ elif self .type == 4 : # Digital Not Gate
46
+ self .width = 40
47
+ self .height = 40
48
+ elif self .type == 5 : # Digital Logic Gate
49
+ self .width = 40
50
+ self .height = 40
51
+ self .gate_type = 0 # 0 = AND, 1 = OR
52
+ self .pos = pygame .Rect (pos [0 ], pos [1 ], self .width , self .height )
53
+ self .old_pos = pygame .Rect (self .pos )
54
+ if not self .bar :
55
+ self .rect = pygame .Rect (self .pos [0 ], self .pos [1 ]+ 50 , self .width , self .height )
56
+ else :
57
+ self .rect = pygame .Rect (self .pos [0 ], self .pos [1 ], self .width , self .height )
58
+ if self .type == 5 :
59
+ self .in_1_rect = pygame .Rect (self .pos [0 ], self .pos [1 ], self .width , self .height / 2 )
60
+ self .in_2_rect = pygame .Rect (self .pos [0 ], self .pos [1 ]+ self .height / 2 , self .width , self .height / 2 )
61
+ self .font = pygame .font .Font ("font.ttf" , 35 )
62
+ self .font .set_bold (True )
63
+ self .smallfont = pygame .font .Font ("font.ttf" , 12 )
64
+ self .smallfont .set_bold (True )
65
+ self .deleted = False
66
+ self .label = ""
67
+
68
+ def update (self , pos = None ): # pos is the xy position of the movable page
69
+ if not self .bar :
70
+ self .rect = self .pos .move (pos [0 ], pos [1 ]+ 50 )
71
+ if self .type == 3 :
72
+ self .connect_in_point = (self .rect .left + 2 , self .rect .centery )
73
+ self .connect_out_point = (self .rect .right - 2 , self .rect .centery )
74
+ if self .timer_type == 3 : # Clock
75
+ self .connection = None
76
+ self .value = int (time .time ()/ self .duration % 2 )
77
+ elif self .type == 4 :
78
+ self .connect_in_point = (self .rect .left + 2 , self .rect .centery )
79
+ self .connect_out_point = (self .rect .right - 2 , self .rect .centery )
80
+ elif self .type == 5 :
81
+ self .connect_in_point = (self .rect .left + 2 , self .rect .centery - 10 )
82
+ self .connect_in_point_2 = (self .rect .left + 2 , self .rect .centery + 10 )
83
+ self .connect_out_point = (self .rect .right - 2 , self .rect .centery )
84
+ self .in_1_rect = pygame .Rect (self .rect .left , self .rect .top , self .rect .width , self .rect .height / 2 )
85
+ self .in_2_rect = pygame .Rect (self .rect .left , self .rect .top + self .rect .height / 2 , self .rect .width , self .rect .height / 2 )
86
+ else :
87
+ self .connect_in_point = self .rect .center
88
+ self .connect_out_point = self .rect .center
89
+ if self .connection :
90
+ if self .type == 3 :
91
+ if self .timer_type == 0 : # Delay
92
+ if self .timer [1 ] != self .connection .value :
93
+ self .timer [0 ] = time .time ()
94
+ self .timer [1 ] = self .connection .value
95
+ if self .timer [0 ]+ self .duration < time .time ():
96
+ self .value = self .timer [1 ]
97
+ if self .timer_type == 1 : # On-Delay
98
+ if self .timer [1 ] != self .connection .value :
99
+ self .timer [0 ] = time .time ()
100
+ self .timer [1 ] = self .connection .value
101
+ if self .connection .value :
102
+ if self .timer [0 ]+ self .duration < time .time ():
103
+ self .value = self .timer [1 ]
104
+ else :
105
+ self .value = 0
106
+ if self .timer_type == 2 : # Off-Delay
107
+ if self .timer [1 ] != self .connection .value :
108
+ self .timer [0 ] = time .time ()
109
+ self .timer [1 ] = self .connection .value
110
+ if not self .connection .value :
111
+ if self .timer [0 ]+ self .duration < time .time ():
112
+ self .value = self .timer [1 ]
113
+ else :
114
+ self .value = 1
115
+ elif self .type == 4 :
116
+ self .value = not self .connection .value
117
+ else :
118
+ self .value = self .connection .value
119
+ if self .type == 5 :
120
+ if self .gate_type == 0 :
121
+ if self .connection and self .connection_2 :
122
+ self .value = self .connection .value and self .connection_2 .value
123
+ else :
124
+ self .value = 0
125
+ """
126
+ if self.connection:
127
+ self.value = self.connection
128
+ elif self.connection_2:
129
+ self.value = self.connection_2
130
+ """
131
+ else :
132
+ self .rect = self .pos
133
+
134
+ def draw (self ):
135
+ if self .type == 1 : # 1 = Digital Input
136
+ if self .value :
137
+ pygame .draw .rect (self .screen , (0 , 255 , 0 ), self .rect )
138
+ text_surface = self .font .render ("1" , True , (0 ,0 ,0 ))
139
+ else :
140
+ pygame .draw .rect (self .screen , (0 , 60 , 0 ), self .rect )
141
+ text_surface = self .font .render ("0" , True , (0 ,0 ,0 ))
142
+ self .screen .blit (text_surface , (self .rect .left + self .rect .width / 2 - text_surface .get_rect ().width / 2 , self .rect .top + self .rect .height / 2 - text_surface .get_rect ().height / 2 ))
143
+ pygame .draw .rect (self .screen , (0 ,0 ,0 ), self .rect , 1 )
144
+ elif self .type == 2 : # 2 = Digital Output
145
+ if self .value :
146
+ pygame .draw .circle (self .screen , (0 , 255 , 0 ), self .rect .center , self .rect .width / 2 )
147
+ text_surface = self .font .render ("1" , True , (0 ,0 ,0 ))
148
+ else :
149
+ pygame .draw .circle (self .screen , (0 , 60 , 0 ), self .rect .center , self .rect .width / 2 )
150
+ text_surface = self .font .render ("0" , True , (0 ,0 ,0 ))
151
+ self .screen .blit (text_surface , (self .rect .left + self .rect .width / 2 - text_surface .get_rect ().width / 2 , self .rect .top + self .rect .height / 2 - text_surface .get_rect ().height / 2 ))
152
+ pygame .draw .circle (self .screen , (0 , 0 , 0 ), self .rect .center , self .rect .width / 2 , 1 )
153
+ elif self .type == 3 : # 3 = Timer
154
+ pygame .draw .rect (self .screen , (200 ,200 ,200 ), self .rect )
155
+ timer_icon .set_alpha (128 )
156
+ self .screen .blit (timer_icon , self .rect .move (2 ,2 ).topleft )
157
+ text_surface = self .smallfont .render (format_duration (self .duration ), True , (0 ,0 ,0 ))
158
+ self .screen .blit (text_surface , (self .rect .right - text_surface .get_rect ().width - 2 , self .rect .bottom - text_surface .get_rect ().height ))
159
+ if self .timer_type == 0 :
160
+ text_surface = self .smallfont .render ("Delay" , True , (0 ,0 ,0 ))
161
+ elif self .timer_type == 1 :
162
+ text_surface = self .smallfont .render ("On-D" , True , (0 ,0 ,0 ))
163
+ elif self .timer_type == 2 :
164
+ text_surface = self .smallfont .render ("Off-D" , True , (0 ,0 ,0 ))
165
+ elif self .timer_type == 3 :
166
+ text_surface = self .smallfont .render ("Clock" , True , (0 ,0 ,0 ))
167
+ self .screen .blit (text_surface , (self .rect .right - text_surface .get_rect ().width - 2 , self .rect .top ))
168
+ pygame .draw .rect (self .screen , (0 ,0 ,0 ), self .rect , 1 )
169
+ pygame .draw .polygon (self .screen , (128 ,80 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .left + 5 , self .rect .top + self .rect .height / 2 ), (self .rect .left , self .rect .top + self .rect .height / 2 + 5 )])
170
+ pygame .draw .polygon (self .screen , (0 ,0 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .left + 5 , self .rect .top + self .rect .height / 2 ), (self .rect .left , self .rect .top + self .rect .height / 2 + 5 )], 1 )
171
+ pygame .draw .polygon (self .screen , (128 ,80 ,0 ), [(self .rect .right - 6 , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .right , self .rect .top + self .rect .height / 2 ), (self .rect .right - 6 , self .rect .top + self .rect .height / 2 + 5 )])
172
+ pygame .draw .polygon (self .screen , (0 ,0 ,0 ), [(self .rect .right - 6 , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .right - 1 , self .rect .top + self .rect .height / 2 ), (self .rect .right - 6 , self .rect .top + self .rect .height / 2 + 5 )], 1 )
173
+ elif self .type == 4 : # 4 = Not Gate
174
+ pygame .draw .polygon (self .screen , (128 ,128 ,128 ), [(self .rect .left , self .rect .top + self .rect .height / 2 - 10 ), (self .rect .left + 30 , self .rect .top + self .rect .height / 2 ), (self .rect .left , self .rect .top + self .rect .height / 2 + 10 )])
175
+ pygame .draw .polygon (self .screen , (0 ,0 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 - 10 ), (self .rect .left + 30 , self .rect .top + self .rect .height / 2 ), (self .rect .left , self .rect .top + self .rect .height / 2 + 10 )],1 )
176
+ pygame .draw .circle (self .screen , (128 ,128 ,128 ), (self .rect .left + 35 , self .rect .centery ), 5 )
177
+ pygame .draw .circle (self .screen , (0 ,0 ,0 ), (self .rect .left + 35 , self .rect .centery ), 5 , 1 )
178
+ elif self .type == 5 : # 5 = Logic Gate
179
+ if self .gate_type == 0 : # And Gate
180
+ #pygame.draw.arc(self.screen, (128,128,128), self.rect, -math.pi/2, math.pi/2, 10)
181
+ #pygame.gfxdraw.pie(self.screen, self.rect.centerx, self.rect.centery, 20, -90, 90, (128, 128, 128))
182
+ pygame .draw .rect (self .screen , (128 ,128 ,128 ), pygame .Rect (self .rect .left , self .rect .top , self .rect .width / 2 , self .rect .height ))
183
+ pygame .draw .rect (self .screen , (0 ,0 ,0 ), pygame .Rect (self .rect .left , self .rect .top , self .rect .width / 2 , self .rect .height ), 1 )
184
+ self .screen .blit (semicircle (20 , (128 ,128 ,128 ), False ), self .rect .move (- 1 ,0 ).topleft )
185
+ pygame .draw .polygon (self .screen , (128 ,80 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 + 5 ), (self .rect .left + 5 , self .rect .top + self .rect .height / 2 + 10 ), (self .rect .left , self .rect .top + self .rect .height / 2 + 15 )])
186
+ pygame .draw .polygon (self .screen , (0 ,0 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 + 5 ), (self .rect .left + 5 , self .rect .top + self .rect .height / 2 + 10 ), (self .rect .left , self .rect .top + self .rect .height / 2 + 15 )], 1 )
187
+ pygame .draw .polygon (self .screen , (128 ,80 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .left + 5 , self .rect .top + self .rect .height / 2 - 10 ), (self .rect .left , self .rect .top + self .rect .height / 2 - 15 )])
188
+ pygame .draw .polygon (self .screen , (0 ,0 ,0 ), [(self .rect .left , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .left + 5 , self .rect .top + self .rect .height / 2 - 10 ), (self .rect .left , self .rect .top + self .rect .height / 2 - 15 )], 1 )
189
+
190
+ pygame .draw .polygon (self .screen , (128 ,80 ,0 ), [(self .rect .right - 6 , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .right - 1 , self .rect .top + self .rect .height / 2 ), (self .rect .right - 6 , self .rect .top + self .rect .height / 2 + 5 )])
191
+ pygame .draw .polygon (self .screen , (0 ,0 ,0 ), [(self .rect .right - 6 , self .rect .top + self .rect .height / 2 - 5 ), (self .rect .right - 1 , self .rect .top + self .rect .height / 2 ), (self .rect .right - 6 , self .rect .top + self .rect .height / 2 + 5 )], 1 )
192
+
193
+
194
+ def draw_connection (self ):
195
+ if not self .bar :
196
+ if self .connection :
197
+ if self .connection .value :
198
+ pygame .draw .line (self .screen , (0 , 255 , 0 ), self .connection .connect_out_point , self .connect_in_point , 5 )
199
+ else :
200
+ pygame .draw .line (self .screen , (0 , 60 , 0 ), self .connection .connect_out_point , self .connect_in_point , 5 )
201
+ if self .connection_2 :
202
+ if self .connection_2 .value :
203
+ pygame .draw .line (self .screen , (0 , 255 , 0 ), self .connection_2 .connect_out_point , self .connect_in_point_2 , 5 )
204
+ else :
205
+ pygame .draw .line (self .screen , (0 , 60 , 0 ), self .connection_2 .connect_out_point , self .connect_in_point_2 , 5 )
206
+
207
+ def collidepoint (self , point ):
208
+ return self .rect .collidepoint (point )
209
+
210
+ def delete (self ):
211
+ self .deleted = True
0 commit comments