-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.py
104 lines (92 loc) · 3.79 KB
/
connector.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Logic Circuits - connector.py
#
# Copyleft 2015 Chris Meyers <[email protected]> 2015.12.13
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
__author__ = "Chris Meyers, Jeff Elkner and Kevin Cole"
__copyright__ = "Copyleft 2015, Chris Meyers (2015.12.13)"
__credits__ = ["Chris Meyers", "Jeff Elkner",
"Kevin Cole"] # Authors and bug reporters
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Chris Meyers, Jeff Elkner, Kevin Cole"
__email__ = "[email protected]"
__status__ = "Development" # "Prototype", "Development" or "Production"
__appname__ = "Logic Circuits"
import pygame as pg
from component import posAdd
from settings import HOT, COLD, CmdEncapsuleLayer
from text import textDraw
class Connector:
def __init__(s, parent, name, pos, value=0, feeds=()):
s.name, s.pos, s.value, s.feeds = name, pos, value, feeds
s.parent = parent
s.layer = parent.layer
s.prevValue = None
s.labelQuad = 0
parent.kids += (s,)
def x(s, val=0):
return s.pos[0] + val * s.parent.scale
def y(s, val=0):
return s.pos[1] + val * s.parent.scale
def scootOver(self, xmov, ymov, nest=0):
self.pos = posAdd(self.pos, (xmov, ymov))
def addWire(s, *others): # to a series of connectors
this = s
anons = []
maxLayer = 0
for other in others:
if type(other) == type(()): # only pos? make new one
other = Connector(s.parent, "Anon", other, s.value)
anons.append(other) # save to assign layer later
other.layer = None # hold for now
else:
maxLayer = max(maxLayer, other.layer)
this.feeds += (other,)
this = other # chain 'em
for anon in anons:
anon.layer = maxLayer
return this # lets us chain calls left to right
def drawWires(s, screen, color=None):
if not color:
color = [COLD, HOT][s.value]
slq = s.labelQuad
for d in s.feeds:
dlq = d.labelQuad
txt = "Draw wire {0}.{1}={2} @ {3} - {4}.{5}={6} @ {7}" \
.format(s.parent.name, s.name, s.layer, s.pos[0], s.pos[1],
d.parent.name, d.name, d.layer, d.pos[0], d.pos[1])
if max(s.layer, d.layer) <= CmdEncapsuleLayer:
pg.draw.lines(screen, color, False, (s.pos, d.pos))
if slq:
textDraw(screen, s.pos, color, s.name, quad=slq)
if dlq:
textDraw(screen, d.pos, color, d.name, quad=dlq)
d.drawWires(screen, color) # fan out
def sendOutput(self): # from connector to all targets
if self.prevValue == self.value:
return # short cut
self.prevValue = self.value
for dest in self.feeds:
dest.value = self.value
dest.sendOutput()
def __repr__(s):
par = s.parent.name
return "<Conn: {0}.{1} {2} {3}>".format(par, s.name, s.pos, s.layer)