Small fixes, add border color for in/out on machines

This commit is contained in:
Ben 2024-01-30 21:16:00 +01:00
parent 35cadf318a
commit 3c409fdbff
Signed by: ben
GPG key ID: 1353F41CF1CFF2D3
2 changed files with 47 additions and 14 deletions

View file

@ -11,7 +11,7 @@ from ..helper import prompt
@click.command() @click.command()
@click.option("--result", is_flag=True) @click.option("--result", is_flag=True, default=True)
@click.option("--debug", is_flag=True) @click.option("--debug", is_flag=True)
@click.option("--refetch", is_flag=True) @click.option("--refetch", is_flag=True)
@click.argument("search") @click.argument("search")

View file

@ -1,13 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import re
from datetime import timedelta from datetime import timedelta
import click import click
from NodeGraphQt import BaseNode, NodeBaseWidget from NodeGraphQt import BaseNode, NodeBaseWidget
from NodeGraphQt import NodeGraph, Port from NodeGraphQt import NodeGraph, Port
from NodeGraphQt.constants import PortTypeEnum from NodeGraphQt.constants import PortTypeEnum
from NodeGraphQt.widgets.node_widgets import _NodeGroupBox
from PySide2.QtCore import Qt from PySide2.QtCore import Qt
from PySide2.QtWidgets import QSlider from PySide2.QtWidgets import QSlider, QLineEdit
from Qt import QtWidgets from Qt import QtWidgets
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@ -16,6 +18,10 @@ from factorygame.data.common import chose_resource, resource_needs_update, chose
from .models import Recipe, Resource from .models import Recipe, Resource
from ..helper import prompt from ..helper import prompt
INPUT_COLOR = (249, 169, 0)
OUTPUT_COLOR = (204, 44, 36)
OTHER_COLOR = (0, 83, 135)
class NodeSlider(NodeBaseWidget): class NodeSlider(NodeBaseWidget):
def __init__(self, name, label="", parent=None): def __init__(self, name, label="", parent=None):
@ -52,13 +58,21 @@ class GlobalInput(BaseNode):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.add_output("Create Machine") self.add_output("Create Machine", color=OTHER_COLOR)
class Machine(BaseNode): class Machine(BaseNode):
__identifier__ = "factorygame" __identifier__ = "factorygame"
NODE_NAME = "FactoryGame Machine" NODE_NAME = "FactoryGame Machine"
STYLESHEET_BORDER_COLOR_REGEX = re.compile(
r"QLineEdit\s*{[^{}]*\s*border:[^;]*(rgb\([^)]+\))", re.MULTILINE | re.DOTALL
)
def __init__(self):
super().__init__()
self.model.width = 240
def assign_recipe(self, recipe: Recipe): def assign_recipe(self, recipe: Recipe):
for port_idx in range(len(self._inputs)): for port_idx in range(len(self._inputs)):
self.delete_input(port_idx) self.delete_input(port_idx)
@ -83,10 +97,16 @@ class Machine(BaseNode):
f"{input_resources[resource_label]:.2f} / min" f"{input_resources[resource_label]:.2f} / min"
) )
else: else:
port = self.add_input(resource_label, color=(180, 80, 0)) port = self.add_input(resource_label, color=INPUT_COLOR)
port.add_accept_port_type(resource_label, PortTypeEnum.OUT.value, "factorygame.Machine") port.add_accept_port_type(resource_label, PortTypeEnum.OUT.value, "factorygame.Machine")
port.add_accept_port_type("Create Machine", PortTypeEnum.OUT.value, "factorygame.GlobalInput") port.add_accept_port_type("Create Machine", PortTypeEnum.OUT.value, "factorygame.GlobalInput")
self._add_text_label(in_amount_name(resource_label), ingredient, input_resources, resource_label) self._add_text_label(
in_amount_name(resource_label),
ingredient,
input_resources,
resource_label,
border_color=INPUT_COLOR,
)
output_resources: dict[str, float] = {} output_resources: dict[str, float] = {}
for result in recipe.results: for result in recipe.results:
@ -97,9 +117,11 @@ class Machine(BaseNode):
f"{output_resources[resource_label]:.2f} / min" f"{output_resources[resource_label]:.2f} / min"
) )
else: else:
port = self.add_output(resource_label, color=(200, 20, 0)) port = self.add_output(resource_label, color=OUTPUT_COLOR)
port.add_accept_port_type(resource_label, PortTypeEnum.IN.value, "factorygame.Machine") port.add_accept_port_type(resource_label, PortTypeEnum.IN.value, "factorygame.Machine")
self._add_text_label(out_amount_name(resource_label), result, output_resources, resource_label) self._add_text_label(
out_amount_name(resource_label), result, output_resources, resource_label, border_color=OUTPUT_COLOR
)
performance_slider_name = "machine performance" performance_slider_name = "machine performance"
@ -115,12 +137,23 @@ class Machine(BaseNode):
self.add_custom_widget(slider) self.add_custom_widget(slider)
slider.value_changed.connect(lambda name, value: set_performance(max(1, min(250, value)))) slider.value_changed.connect(lambda name, value: set_performance(max(1, min(250, value))))
def _add_text_label(self, name, flow, resource_amounts, resource_label): def _add_text_label(self, name, flow, resource_amounts, resource_label, border_color):
resource_amounts[resource_label] = flow.amount_per_minute() resource_amounts[resource_label] = flow.amount_per_minute()
self.add_text_input(name=name, label=name, text=f"{resource_amounts[resource_label]} / min") self.add_text_input(name=name, label=name, text=f"{resource_amounts[resource_label]} / min")
widget = self.get_widget(name) widget = self.get_widget(name)
widget.get_custom_widget().setReadOnly(True) line_edit_widget: QLineEdit = widget.get_custom_widget()
widget.widget().setMaximumWidth(220) line_edit_widget.setReadOnly(True)
group: _NodeGroupBox = widget.widget()
group.setMaximumWidth(220)
group.adjustSize()
if border_color:
stylesheet = line_edit_widget.styleSheet()
match = Machine.STYLESHEET_BORDER_COLOR_REGEX.match(stylesheet)
stylesheet = (
stylesheet[: match.start(1)] + f"rgb({','.join(map(str, border_color))})" + stylesheet[match.end(1) :]
)
line_edit_widget.setStyleSheet(stylesheet)
line_edit_widget.update()
def on_port_connected(input_port: Port, output_port: Port): def on_port_connected(input_port: Port, output_port: Port):
@ -144,10 +177,10 @@ def on_port_connected(input_port: Port, output_port: Port):
recipe_machine = graph.create_node("factorygame.Machine", push_undo=True) recipe_machine = graph.create_node("factorygame.Machine", push_undo=True)
recipe_machine.assign_recipe(recipe) recipe_machine.assign_recipe(recipe)
recipe_machine.update() recipe_machine.update()
recipe_machine.set_x_pos(input_port.node().x_pos() - recipe_machine.view.width - 200) recipe_machine.set_x_pos(input_port.node().x_pos() - recipe_machine.view.width - 100)
recipe_machine.get_output(input_port.name()).connect_to(input_port) recipe_machine.get_output(input_port.name()).connect_to(input_port)
if recipe_machine.x_pos() - (global_input.x_pos() - global_input.view.width) < 200: if recipe_machine.x_pos() - (global_input.x_pos() - global_input.view.width) < 150:
global_input.set_x_pos(recipe_machine.x_pos() - global_input.view.width - 200) global_input.set_x_pos(recipe_machine.x_pos() - global_input.view.width - 150)
@click.command @click.command
@ -196,7 +229,7 @@ def main(debug: bool, search: str):
recipe_machine.assign_recipe(recipe) recipe_machine.assign_recipe(recipe)
graph.auto_layout_nodes([global_input, recipe_machine]) graph.auto_layout_nodes([global_input, recipe_machine])
global_input.set_y_pos(recipe_machine.y_pos()) global_input.set_y_pos(recipe_machine.y_pos())
global_input.set_x_pos(global_input.x_pos() - global_input.model.width - 200) global_input.set_x_pos(global_input.x_pos() - global_input.model.width - 150)
graph.center_on([global_input, recipe_machine]) graph.center_on([global_input, recipe_machine])
graph.port_connected.connect(on_port_connected) graph.port_connected.connect(on_port_connected)
app.exec_() app.exec_()