I forgot to commit, but i have added all the basics: nodes, and connections, and evaluation. Took a long time due to bugs

This commit is contained in:
csd4ni3l
2025-10-13 18:17:55 +02:00
parent df83bdd01a
commit e2fa83f83b
4 changed files with 175 additions and 8 deletions

View File

@@ -1,11 +1,146 @@
import arcade, arcade.gui
import arcade, arcade.gui, random
from utils.constants import menu_background_color, button_style
from utils.preload import button_texture, button_hovered_texture
from utils.utils import cubic_bezier_points, get_gate_port_position
from utils.constants import dropdown_style, LOGICAL_GATES
class LogicalGate(arcade.gui.UIBoxLayout):
def __init__(self, id, x, y, gate_type):
super().__init__(x=x, y=y, space_between=2, vertical=False)
self.id = id
self.gate_type = gate_type
if gate_type == "INPUT":
self.value = 1
else:
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=100))
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.output: LogicalGate | None = None
def calculate_value(self):
if self.gate_type == "OUTPUT":
self.value = self.input[0].calculate_value()
elif len(self.input) == 2:
self.value = int(LOGICAL_GATES[self.gate_type](self.input[0].calculate_value(), self.input[1].calculate_value())) # have to convert to int cause it might return boolean
self.gate_button.text = f"{self.gate_type} ({self.value})"
return self.value
def __repr__(self):
return f"{self.gate_type}: {self.value}"
class Game(arcade.gui.UIView):
def __init__(self, pypresence_client):
super().__init__()
def on_show_view(self):
super().on_show_view()
self.pypresence_client = pypresence_client
self.pypresence_client.update(state="In game")
self.gates: list[LogicalGate] = []
self.connections = []
self.default_gate_type = "AND"
self.dragged_gate = None
self.selected_input = None
self.selected_output = None
self.ui.on_event = self.on_event
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.tools_box = self.anchor.add(arcade.gui.UIBoxLayout(vertical=False, space_between=5), anchor_x="center", anchor_y="bottom", align_y=5)
for gate in LOGICAL_GATES.keys():
button = self.tools_box.add(arcade.gui.UIFlatButton(width=self.window.width * 0.1, height=self.window.height * 0.1, text=f"Create {gate} gate", style=dropdown_style))
button.on_click = lambda event, gate=gate: self.add_gate(random.randint(0, self.window.width - 100), random.randint(0, self.window.height - 100), gate)
evaluate_button = self.tools_box.add(arcade.gui.UIFlatButton(width=self.window.width * 0.1, height=self.window.height * 0.1, text="Evaluate", style=dropdown_style))
evaluate_button.on_click = lambda event: self.run_logic()
def run_logic(self):
for gate in self.gates:
if not gate.output:
gate.calculate_value()
def select_output(self, gate_id):
if self.gates[gate_id].output:
return
self.selected_output = gate_id
self.selected_input = None
def select_input(self, gate_id):
if len(self.gates[gate_id].input) == 2:
return
if self.selected_output is not None:
self.selected_input = gate_id
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
def add_gate(self, x, y, gate_type):
if len(self.gates) < 2:
gate_type = "INPUT"
self.gates.append(self.add_widget(LogicalGate(len(self.gates), x, y, gate_type)))
self.gates[-1].input_add_button.on_click = lambda e, gate_id=len(self.gates) - 1: self.select_input(gate_id)
self.gates[-1].output_add_button.on_click = lambda e, gate_id=len(self.gates) - 1: self.select_output(gate_id)
def on_event(self, event):
arcade.gui.UIManager.on_event(self.ui, event)
if isinstance(event, arcade.gui.UIMousePressEvent):
if not self.dragged_gate:
for gate in self.gates:
if gate.gate_button.rect.point_in_rect((event.x, event.y)):
self.dragged_gate = gate
break
def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers):
if self.dragged_gate is not None:
self.dragged_gate.rect = self.dragged_gate.rect.move(dx, dy)
def on_mouse_release(self, x, y, button, modifiers):
self.dragged_gate = None
def on_key_press(self, symbol, modifiers):
if symbol == arcade.key.ESCAPE:
from menus.main import Main
self.window.show_view(Main(self.pypresence_client))
def on_draw(self):
super().on_draw()
for conn in self.connections:
start_id, end_id = conn
start_gate = self.gates[start_id]
end_gate = self.gates[end_id]
p0 = get_gate_port_position(start_gate, "output")
p3 = get_gate_port_position(end_gate, "input")
dx = p3[0] - p0[0]
offset = max(60, abs(dx) * 0.45)
c1 = (p0[0] + offset, p0[1])
c2 = (p3[0] - offset, p3[1])
points = cubic_bezier_points(p0, c1, c2, p3, segments=40)
arcade.draw_line_strip(points, arcade.color.WHITE, 3)

View File

@@ -52,10 +52,10 @@ class Main(arcade.gui.UIView):
self.title_label = self.box.add(arcade.gui.UILabel(text="Logical Signals", font_name="Roboto", font_size=48))
self.play_button = self.box.add(arcade.gui.UITextureButton(text="Play", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=150, style=big_button_style))
self.play_button = self.box.add(arcade.gui.UITextureButton(text="Play", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 8, style=big_button_style))
self.play_button.on_click = lambda event: self.play()
self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=150, style=big_button_style))
self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 8, style=big_button_style))
self.settings_button.on_click = lambda event: self.settings()
def play(self):

View File

@@ -7,6 +7,17 @@ menu_background_color = (30, 30, 47)
log_dir = 'logs'
discord_presence_id = 1427213145667276840
LOGICAL_GATES = {
"INPUT": lambda a: a,
"AND": lambda a, b: a and b,
"OR": lambda a, b: a or b,
"NAND": lambda a, b: not (a and b),
"NOR": lambda a, b: not (a or b),
"XOR": lambda a, b: a != b,
"XNOR": lambda a, b: a == b,
"OUTPUT": lambda a: a
}
button_style = {'normal': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK), 'hover': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK),
'press': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK), 'disabled': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK)}
big_button_style = {'normal': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK, font_size=26), 'hover': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK, font_size=26),

View File

@@ -2,7 +2,28 @@ import logging, arcade, arcade.gui, sys, traceback
from utils.constants import menu_background_color
import pyglet.info, pyglet.event
import pyglet.display
def lerp(a, b, t):
return a + (b - a) * t
def cubic_bezier_point(p0, p1, p2, p3, t):
u = 1 - t
x = (u ** 3) * p0[0] + 3 * (u ** 2) * t * p1[0] + 3 * u * (t ** 2) * p2[0] + (t ** 3) * p3[0]
y = (u ** 3) * p0[1] + 3 * (u ** 2) * t * p1[1] + 3 * u * (t ** 2) * p2[1] + (t ** 3) * p3[1]
return x, y
def cubic_bezier_points(p0, p1, p2, p3, segments=40):
return [cubic_bezier_point(p0, p1, p2, p3, i / segments) for i in range(segments + 1)]
def get_gate_port_position(gate, port: str):
rect = gate.rect
center_y = rect.center_y
if port == "output":
return (rect.right, center_y)
else:
return (rect.left, center_y)
def dump_platform():
import platform