mirror of
https://github.com/csd4ni3l/logical-signals.git
synced 2026-01-01 04:23:46 +01:00
Allow connecting from both directions, use textured logic gates instead of UI to get a huge speedup, make output single output(should have been), add connection removal with right click
This commit is contained in:
BIN
assets/graphics/logic_gate_false.png
Normal file
BIN
assets/graphics/logic_gate_false.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 246 B |
BIN
assets/graphics/logic_gate_input_false.png
Normal file
BIN
assets/graphics/logic_gate_input_false.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 199 B |
BIN
assets/graphics/logic_gate_input_true.png
Normal file
BIN
assets/graphics/logic_gate_input_true.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 410 B |
BIN
assets/graphics/logic_gate_output_false.png
Normal file
BIN
assets/graphics/logic_gate_output_false.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 200 B |
BIN
assets/graphics/logic_gate_output_true.png
Normal file
BIN
assets/graphics/logic_gate_output_true.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 409 B |
BIN
assets/graphics/logic_gate_true.png
Normal file
BIN
assets/graphics/logic_gate_true.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 252 B |
125
game/play.py
125
game/play.py
@@ -4,11 +4,11 @@ from datetime import datetime
|
|||||||
|
|
||||||
from utils.utils import cubic_bezier_points, get_gate_port_position, generate_task_text
|
from utils.utils import cubic_bezier_points, get_gate_port_position, generate_task_text
|
||||||
from utils.constants import button_style, dropdown_style, LOGICAL_GATES, LEVELS, SINGLE_INPUT_LOGICAL_GATES
|
from utils.constants import button_style, dropdown_style, LOGICAL_GATES, LEVELS, SINGLE_INPUT_LOGICAL_GATES
|
||||||
from utils.preload import button_texture, button_hovered_texture
|
from utils.preload import button_texture, button_hovered_texture, logic_gate_textures
|
||||||
|
|
||||||
class LogicalGate(arcade.gui.UIBoxLayout):
|
class LogicalGate(arcade.Sprite):
|
||||||
def __init__(self, id, x, y, gate_type, value):
|
def __init__(self, id, x, y, gate_type, value):
|
||||||
super().__init__(x=x, y=y, space_between=2, vertical=False)
|
super().__init__(center_x=x, center_y=y, img=logic_gate_textures[gate_type][value if value is not None else 0])
|
||||||
|
|
||||||
self.id = id
|
self.id = id
|
||||||
self.gate_type = gate_type
|
self.gate_type = gate_type
|
||||||
@@ -17,14 +17,6 @@ class LogicalGate(arcade.gui.UIBoxLayout):
|
|||||||
self.value = value
|
self.value = value
|
||||||
else:
|
else:
|
||||||
self.value = 0
|
self.value = 0
|
||||||
|
|
||||||
self.input_add_button = self.add(arcade.gui.UIFlatButton(text="+", style=dropdown_style, height=30, width=30))
|
|
||||||
self.input_add_button.visible = not self.gate_type == "INPUT"
|
|
||||||
|
|
||||||
self.gate_button = self.add(arcade.gui.UIFlatButton(text=f"{gate_type} ({self.value})", style=dropdown_style, height=30, width=120))
|
|
||||||
|
|
||||||
self.output_add_button = self.add(arcade.gui.UIFlatButton(text="+", style=dropdown_style, height=30, width=30))
|
|
||||||
self.output_add_button.visible = not self.gate_type == "OUTPUT"
|
|
||||||
|
|
||||||
self.input: list[LogicalGate] = []
|
self.input: list[LogicalGate] = []
|
||||||
self.output: LogicalGate | None = None
|
self.output: LogicalGate | None = None
|
||||||
@@ -32,6 +24,7 @@ class LogicalGate(arcade.gui.UIBoxLayout):
|
|||||||
def calculate_value(self):
|
def calculate_value(self):
|
||||||
if self.gate_type == "OUTPUT" and self.input:
|
if self.gate_type == "OUTPUT" and self.input:
|
||||||
self.value = self.input[0].calculate_value()
|
self.value = self.input[0].calculate_value()
|
||||||
|
|
||||||
elif self.gate_type == "INPUT": # dont set INPUT to None
|
elif self.gate_type == "INPUT": # dont set INPUT to None
|
||||||
pass
|
pass
|
||||||
elif self.gate_type in SINGLE_INPUT_LOGICAL_GATES and len(self.input) == 1:
|
elif self.gate_type in SINGLE_INPUT_LOGICAL_GATES and len(self.input) == 1:
|
||||||
@@ -41,7 +34,7 @@ class LogicalGate(arcade.gui.UIBoxLayout):
|
|||||||
else:
|
else:
|
||||||
self.value = None
|
self.value = None
|
||||||
|
|
||||||
self.gate_button.text = f"{self.gate_type} ({self.value})"
|
self.texture = logic_gate_textures[self.gate_type][self.value if self.value is not None else 0]
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -51,6 +44,11 @@ class Game(arcade.gui.UIView):
|
|||||||
def __init__(self, pypresence_client, level_num):
|
def __init__(self, pypresence_client, level_num):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self.camera = arcade.Camera2D()
|
||||||
|
self.camera.match_window()
|
||||||
|
|
||||||
|
self.spritelist = arcade.SpriteList()
|
||||||
|
|
||||||
self.pypresence_client = pypresence_client
|
self.pypresence_client = pypresence_client
|
||||||
self.pypresence_client.update(state="In game")
|
self.pypresence_client.update(state="In game")
|
||||||
|
|
||||||
@@ -58,6 +56,8 @@ class Game(arcade.gui.UIView):
|
|||||||
|
|
||||||
self.gates: list[LogicalGate] = []
|
self.gates: list[LogicalGate] = []
|
||||||
self.connections = []
|
self.connections = []
|
||||||
|
self.bezier_points = []
|
||||||
|
|
||||||
self.default_gate_type = "AND"
|
self.default_gate_type = "AND"
|
||||||
self.dragged_gate = None
|
self.dragged_gate = None
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ class Game(arcade.gui.UIView):
|
|||||||
self.add_gate(random.randint(0, 200), random.randint(200, self.window.height - 100), "INPUT", requirement[2])
|
self.add_gate(random.randint(0, 200), random.randint(200, self.window.height - 100), "INPUT", requirement[2])
|
||||||
elif requirement[1] == "OUTPUT":
|
elif requirement[1] == "OUTPUT":
|
||||||
for _ in range(requirement[0]):
|
for _ in range(requirement[0]):
|
||||||
self.add_gate(random.randint(self.window.width - 500, self.window.width - 200), random.randint(200, self.window.height - 100), "OUTPUT", requirement[2])
|
self.add_gate(random.randint(self.window.width - 500, self.window.width - 350), random.randint(200, self.window.height - 100), "OUTPUT", requirement[2])
|
||||||
else:
|
else:
|
||||||
for _ in range(requirement[0]):
|
for _ in range(requirement[0]):
|
||||||
self.add_gate(random.randint(300, self.window.width - 600), random.randint(200, self.window.height - 100), requirement[1])
|
self.add_gate(random.randint(300, self.window.width - 600), random.randint(200, self.window.height - 100), requirement[1])
|
||||||
@@ -90,7 +90,7 @@ class Game(arcade.gui.UIView):
|
|||||||
if "INPUT" in gate:
|
if "INPUT" in gate:
|
||||||
func = lambda: (random.randint(0, 200), random.randint(200, self.window.height - 100))
|
func = lambda: (random.randint(0, 200), random.randint(200, self.window.height - 100))
|
||||||
elif gate == "OUTPUT":
|
elif gate == "OUTPUT":
|
||||||
func = lambda: (random.randint(self.window.width - 500, self.window.width - 200), random.randint(200, self.window.height - 100))
|
func = lambda: (random.randint(self.window.width - 500, self.window.width - 350), random.randint(200, self.window.height - 100))
|
||||||
else:
|
else:
|
||||||
func = lambda: (random.randint(300, self.window.width - 600), random.randint(200, self.window.height - 100))
|
func = lambda: (random.randint(300, self.window.width - 600), random.randint(200, self.window.height - 100))
|
||||||
|
|
||||||
@@ -189,34 +189,45 @@ class Game(arcade.gui.UIView):
|
|||||||
with open("data.json", "w") as file:
|
with open("data.json", "w") as file:
|
||||||
file.write(json.dumps(self.data, indent=4))
|
file.write(json.dumps(self.data, indent=4))
|
||||||
|
|
||||||
def select_output(self, gate_id):
|
def add_connection(self):
|
||||||
if self.gates[gate_id].output:
|
output_gate = self.gates[self.selected_output]
|
||||||
return
|
input_gate = self.gates[self.selected_input]
|
||||||
|
|
||||||
self.selected_output = gate_id
|
output_gate.output = input_gate
|
||||||
|
input_gate.input.append(output_gate)
|
||||||
|
|
||||||
|
self.connections.append([self.selected_output, self.selected_input])
|
||||||
|
|
||||||
|
self.selected_output = None
|
||||||
self.selected_input = None
|
self.selected_input = None
|
||||||
|
|
||||||
|
self.evaluate()
|
||||||
|
|
||||||
|
def select_output(self, gate_id):
|
||||||
|
if gate_id == self.selected_input:
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.gates[gate_id].output:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.selected_output = gate_id
|
||||||
|
|
||||||
|
if self.selected_input is not None:
|
||||||
|
self.add_connection()
|
||||||
|
|
||||||
def select_input(self, gate_id):
|
def select_input(self, gate_id):
|
||||||
|
if gate_id == self.selected_output:
|
||||||
|
return
|
||||||
|
|
||||||
if self.gates[gate_id].gate_type not in SINGLE_INPUT_LOGICAL_GATES and len(self.gates[gate_id].input) == 2:
|
if self.gates[gate_id].gate_type not in SINGLE_INPUT_LOGICAL_GATES and len(self.gates[gate_id].input) == 2:
|
||||||
return
|
return
|
||||||
elif self.gates[gate_id].gate_type in SINGLE_INPUT_LOGICAL_GATES and len(self.gates[gate_id].input) == 1:
|
elif self.gates[gate_id].gate_type in SINGLE_INPUT_LOGICAL_GATES and len(self.gates[gate_id].input) == 1:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.selected_input = gate_id
|
||||||
|
|
||||||
if self.selected_output is not None:
|
if self.selected_output is not None:
|
||||||
self.selected_input = gate_id
|
self.add_connection()
|
||||||
|
|
||||||
output_gate = self.gates[self.selected_output]
|
|
||||||
input_gate = self.gates[self.selected_input]
|
|
||||||
|
|
||||||
output_gate.output = input_gate
|
|
||||||
input_gate.input.append(output_gate)
|
|
||||||
|
|
||||||
self.connections.append([self.selected_output, self.selected_input])
|
|
||||||
|
|
||||||
self.selected_output = None
|
|
||||||
self.selected_input = None
|
|
||||||
|
|
||||||
self.evaluate()
|
|
||||||
|
|
||||||
def add_gate(self, x, y, gate_type, value=None):
|
def add_gate(self, x, y, gate_type, value=None):
|
||||||
if gate_type == "INPUT 0":
|
if gate_type == "INPUT 0":
|
||||||
@@ -226,10 +237,9 @@ class Game(arcade.gui.UIView):
|
|||||||
gate_type = "INPUT"
|
gate_type = "INPUT"
|
||||||
value = 1
|
value = 1
|
||||||
|
|
||||||
self.gates.append(self.add_widget(LogicalGate(len(self.gates), x, y, gate_type, value)))
|
sprite = LogicalGate(len(self.gates), x, y, gate_type, value)
|
||||||
|
self.gates.append(sprite)
|
||||||
self.gates[-1].input_add_button.on_click = lambda e, gate_id=len(self.gates) - 1: self.select_input(gate_id)
|
self.spritelist.append(sprite)
|
||||||
self.gates[-1].output_add_button.on_click = lambda e, gate_id=len(self.gates) - 1: self.select_output(gate_id)
|
|
||||||
|
|
||||||
self.evaluate()
|
self.evaluate()
|
||||||
|
|
||||||
@@ -237,15 +247,35 @@ class Game(arcade.gui.UIView):
|
|||||||
arcade.gui.UIManager.on_event(self.ui, event)
|
arcade.gui.UIManager.on_event(self.ui, event)
|
||||||
|
|
||||||
if isinstance(event, arcade.gui.UIMousePressEvent):
|
if isinstance(event, arcade.gui.UIMousePressEvent):
|
||||||
if not self.dragged_gate:
|
if event.button == arcade.MOUSE_BUTTON_RIGHT:
|
||||||
|
for i in range(len(self.bezier_points) - 1, -1, -1):
|
||||||
|
for point in self.bezier_points[i]:
|
||||||
|
if event.pos.distance(point) < 5:
|
||||||
|
self.gates[self.connections[i][0]].output = None
|
||||||
|
self.gates[self.connections[i][1]].input.remove(self.gates[self.connections[i][0]])
|
||||||
|
self.gates[self.connections[i][1]].calculate_value()
|
||||||
|
|
||||||
|
self.connections.pop(i)
|
||||||
|
self.bezier_points.pop(i)
|
||||||
|
break
|
||||||
|
|
||||||
|
elif event.button == arcade.MOUSE_BUTTON_LEFT:
|
||||||
for gate in self.gates:
|
for gate in self.gates:
|
||||||
if gate.gate_button.rect.point_in_rect((event.x, event.y)):
|
if gate.rect.point_in_rect((event.x, event.y)):
|
||||||
self.dragged_gate = gate
|
x = gate.center_x - event.x
|
||||||
break
|
if abs(x) < 58:
|
||||||
|
self.dragged_gate = gate
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
if x > 0:
|
||||||
|
self.select_input(gate.id)
|
||||||
|
else:
|
||||||
|
self.select_output(gate.id)
|
||||||
|
|
||||||
def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers):
|
def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers):
|
||||||
if self.dragged_gate is not None:
|
if self.dragged_gate is not None:
|
||||||
self.dragged_gate.rect = self.dragged_gate.rect.move(dx, dy)
|
self.dragged_gate.center_x += dx
|
||||||
|
self.dragged_gate.center_y += dy
|
||||||
|
|
||||||
def on_mouse_release(self, x, y, button, modifiers):
|
def on_mouse_release(self, x, y, button, modifiers):
|
||||||
self.dragged_gate = None
|
self.dragged_gate = None
|
||||||
@@ -257,10 +287,15 @@ class Game(arcade.gui.UIView):
|
|||||||
def on_key_press(self, symbol, modifiers):
|
def on_key_press(self, symbol, modifiers):
|
||||||
if symbol == arcade.key.ESCAPE:
|
if symbol == arcade.key.ESCAPE:
|
||||||
self.main_exit()
|
self.main_exit()
|
||||||
|
|
||||||
def on_draw(self):
|
def on_draw(self):
|
||||||
super().on_draw()
|
super().on_draw()
|
||||||
|
|
||||||
|
self.camera.use()
|
||||||
|
self.spritelist.draw()
|
||||||
|
|
||||||
|
self.bezier_points = []
|
||||||
|
|
||||||
for conn in self.connections:
|
for conn in self.connections:
|
||||||
start_id, end_id = conn
|
start_id, end_id = conn
|
||||||
start_gate = self.gates[start_id]
|
start_gate = self.gates[start_id]
|
||||||
@@ -274,5 +309,7 @@ class Game(arcade.gui.UIView):
|
|||||||
c1 = (p0[0] + offset, p0[1])
|
c1 = (p0[0] + offset, p0[1])
|
||||||
c2 = (p3[0] - offset, p3[1])
|
c2 = (p3[0] - offset, p3[1])
|
||||||
|
|
||||||
points = cubic_bezier_points(p0, c1, c2, p3, segments=40)
|
points = cubic_bezier_points(p0, c1, c2, p3, segments=100)
|
||||||
arcade.draw_line_strip(points, arcade.color.WHITE, 3)
|
self.bezier_points.append(points)
|
||||||
|
|
||||||
|
arcade.draw_line_strip(points, arcade.color.WHITE, 6)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import arcade, arcade.gui, asyncio, pypresence, time, copy, json
|
import arcade, arcade.gui, asyncio, pypresence, time, copy, json
|
||||||
|
|
||||||
from utils.preload import button_texture, button_hovered_texture
|
|
||||||
from utils.constants import big_button_style, discord_presence_id
|
from utils.constants import big_button_style, discord_presence_id
|
||||||
|
from utils.preload import button_texture, button_hovered_texture
|
||||||
from utils.utils import FakePyPresence
|
from utils.utils import FakePyPresence
|
||||||
|
|
||||||
class Main(arcade.gui.UIView):
|
class Main(arcade.gui.UIView):
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ menu_background_color = (30, 30, 47)
|
|||||||
log_dir = 'logs'
|
log_dir = 'logs'
|
||||||
discord_presence_id = 1427213145667276840
|
discord_presence_id = 1427213145667276840
|
||||||
|
|
||||||
SINGLE_INPUT_LOGICAL_GATES = ["NOT"]
|
SINGLE_INPUT_LOGICAL_GATES = ["NOT", "OUTPUT"]
|
||||||
|
|
||||||
LOGICAL_GATES = {
|
LOGICAL_GATES = {
|
||||||
"AND": lambda a, b: a and b,
|
"AND": lambda a, b: a and b,
|
||||||
@@ -148,7 +148,7 @@ LEVELS = [
|
|||||||
[1, "NOR"],
|
[1, "NOR"],
|
||||||
[1, "AND"],
|
[1, "AND"],
|
||||||
[1, "XOR"],
|
[1, "XOR"],
|
||||||
[1, "OUTPUT", 1]
|
[1, "OUTPUT", 0]
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
[1, "INPUT", 1],
|
[1, "INPUT", 1],
|
||||||
|
|||||||
@@ -1,4 +1,54 @@
|
|||||||
import arcade.gui, arcade
|
import arcade.gui, arcade
|
||||||
|
|
||||||
|
from utils.constants import LOGICAL_GATES
|
||||||
|
|
||||||
|
from PIL import ImageDraw, ImageFont
|
||||||
|
|
||||||
button_texture = arcade.gui.NinePatchTexture(64 // 4, 64 // 4, 64 // 4, 64 // 4, arcade.load_texture("assets/graphics/button.png"))
|
button_texture = arcade.gui.NinePatchTexture(64 // 4, 64 // 4, 64 // 4, 64 // 4, arcade.load_texture("assets/graphics/button.png"))
|
||||||
button_hovered_texture = arcade.gui.NinePatchTexture(64 // 4, 64 // 4, 64 // 4, 64 // 4, arcade.load_texture("assets/graphics/button_hovered.png"))
|
button_hovered_texture = arcade.gui.NinePatchTexture(64 // 4, 64 // 4, 64 // 4, 64 // 4, arcade.load_texture("assets/graphics/button_hovered.png"))
|
||||||
|
|
||||||
|
true_logic_gate = arcade.load_image("assets/graphics/logic_gate_true.png")
|
||||||
|
false_logic_gate = arcade.load_image("assets/graphics/logic_gate_false.png")
|
||||||
|
|
||||||
|
true_input_logic_gate = arcade.load_image("assets/graphics/logic_gate_input_true.png")
|
||||||
|
false_input_logic_gate = arcade.load_image("assets/graphics/logic_gate_input_false.png")
|
||||||
|
|
||||||
|
true_output_logic_gate = arcade.load_image("assets/graphics/logic_gate_output_true.png")
|
||||||
|
false_output_logic_gate = arcade.load_image("assets/graphics/logic_gate_output_false.png")
|
||||||
|
|
||||||
|
logic_gate_textures = {}
|
||||||
|
|
||||||
|
font = ImageFont.truetype("assets/fonts/Roboto-Black.ttf", 14)
|
||||||
|
|
||||||
|
for gate_name in list(LOGICAL_GATES.keys()) + ["INPUT", "OUTPUT"]:
|
||||||
|
logic_gate_textures[gate_name] = []
|
||||||
|
|
||||||
|
for i in range(2):
|
||||||
|
if not i:
|
||||||
|
if gate_name == "INPUT":
|
||||||
|
img = false_input_logic_gate.copy()
|
||||||
|
elif gate_name == "OUTPUT":
|
||||||
|
img = false_output_logic_gate.copy()
|
||||||
|
else:
|
||||||
|
img = false_logic_gate.copy()
|
||||||
|
else:
|
||||||
|
if gate_name == "INPUT":
|
||||||
|
img = true_input_logic_gate.copy()
|
||||||
|
elif gate_name == "OUTPUT":
|
||||||
|
img = true_output_logic_gate.copy()
|
||||||
|
else:
|
||||||
|
img = true_logic_gate.copy()
|
||||||
|
|
||||||
|
draw = ImageDraw.Draw(img)
|
||||||
|
|
||||||
|
bbox = draw.textbbox((0, 0), gate_name, font=font)
|
||||||
|
text_w = (bbox[2] - bbox[0]) * 1.25
|
||||||
|
text_h = (bbox[3] - bbox[1]) * 1.25
|
||||||
|
|
||||||
|
width, height = img.size
|
||||||
|
text_x = (width - text_w) // 2
|
||||||
|
text_y = (height - text_h) // 2
|
||||||
|
|
||||||
|
draw.text((text_x, text_y), gate_name, font=font, fill=(0, 0, 0, 255))
|
||||||
|
|
||||||
|
logic_gate_textures[gate_name].append(arcade.Texture(name=gate_name, image=img))
|
||||||
Reference in New Issue
Block a user