Skip to content

Commit

Permalink
Move code attribute to base element class
Browse files Browse the repository at this point in the history
  • Loading branch information
bgribble committed Feb 26, 2025
1 parent 11dfb4d commit 879db10
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 19 deletions.
9 changes: 7 additions & 2 deletions mfp/gui/base_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from mfp import log
from .colordb import ColorDB, RGBAColor
from .backend_interfaces import BackendInterface
from .param_info import ParamInfo, ListOfInt
from .param_info import ParamInfo, ListOfInt, CodeBlock
from .layer import Layer


Expand Down Expand Up @@ -81,6 +81,7 @@ def move_to_top(self):
'export_offset_x': ParamInfo(label="Export offset X", param_type=float),
'export_offset_y': ParamInfo(label="Export offset Y", param_type=float),
'debug': ParamInfo(label="Enable debugging", param_type=bool),
'code': ParamInfo(label="Custom code", param_type=CodeBlock, show=True),
}


Expand Down Expand Up @@ -131,6 +132,7 @@ def __init__(self, window, x, y):
self.connections_in = []
self.is_export = False
self.param_list = [a for a in self.store_attrs]
self.code = None

self.app_window = window

Expand Down Expand Up @@ -338,6 +340,10 @@ async def move(self, x, y, **kwargs):
self.position_x = x
self.position_y = y

@saga('code')
async def code_changed(self, action, state_diff, previous):
self.send_params()

@mutates('obj_state')
async def delete(self, delete_obj=True):
# FIXME this is because self.app_window is the backend, not the app window
Expand Down Expand Up @@ -396,7 +402,6 @@ async def create(self, obj_type, init_args):
self.tags = {}
self.update_badge()
objinfo = await MFPGUI().mfp.create(obj_type, init_args, patchname, scopename, name, self.synced_params())

if not objinfo or "obj_id" not in objinfo:
self.app_window.hud_write("ERROR: Could not create, see log for details")

Expand Down
1 change: 1 addition & 0 deletions mfp/gui/imgui/app_window/info_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def render_code_editors(app_window):
new_val["lang"] = "python"
new_val["errorinfo"] = None

log.debug(f"Dispatching setter for {target} {param_name} {new_val}")
MFPGUI().async_task(target.dispatch_setter(param_name, new_val))
selected, _ = imgui.menu_item("Close", "", False)
if selected:
Expand Down
3 changes: 3 additions & 0 deletions mfp/gui/imgui/base_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def __init__(self, window, x, y):
self.hovered_state = False
self.child_elements = []

# data for code editor
self.imgui_code_editor = None

super().__init__(window, x, y)

def select(self):
Expand Down
3 changes: 0 additions & 3 deletions mfp/gui/imgui/processor_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ def __init__(self, window, x, y):
self.min_height = self.height = 12
self.position_set = False

# data for code editor
self.imgui_code_editor = None

@mutates('position_x', 'position_y', 'width', 'height')
def render(self):
"""
Expand Down
14 changes: 1 addition & 13 deletions mfp/gui/processor_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..gui_main import MFPGUI
from .backend_interfaces import BackendInterface
from .base_element import BaseElement
from .param_info import ParamInfo, CodeBlock
from .param_info import ParamInfo


class ProcessorElementImpl(BackendInterface, metaclass=ABCMeta):
Expand All @@ -29,13 +29,6 @@ class ProcessorElement (BaseElement):
display_type = "processor"
proc_type = None

extra_params = {
'code': ParamInfo(label="Custom code", param_type=CodeBlock, show=True),
}
store_attrs = {
**BaseElement.store_attrs, **extra_params
}

# constants
label_off_x = 3
label_off_y = 0
Expand Down Expand Up @@ -68,7 +61,6 @@ def __init__(self, window, x, y, params=None):
if not self.show_label:
self.label.hide()

self.code = None
self.export_x = None
self.export_y = None
self.export_w = None
Expand Down Expand Up @@ -105,10 +97,6 @@ async def recreate_element(self, action, state_diff, previous):
None, f"{self.obj_type}{args}"
)

@saga('code')
async def params_changed(self, action, state_diff, previous):
self.send_params()

async def label_edit_finish(self, widget, text=None, aborted=False):
if text is not None and not aborted:
parts = text.split(' ', 1)
Expand Down
2 changes: 2 additions & 0 deletions mfp/mfp_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ async def create(self, init_type, init_args, patch, scope, name, params=None):
if errinfo:
create_params["code"] = dict(body=codestr, lang="python", errorinfo=errinfo)
rval = create_params
else:
create_params["code"] = dict(body=codestr, lang="python")

# second try: is there a .mfp patch file in the search path?
if ctor is None:
Expand Down
2 changes: 1 addition & 1 deletion mfp/mfp_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def create(self, objtype, initargs, patch_name, scope_name, obj_name, para

obj = await MFPApp().create(objtype, initargs, patch, scope, obj_name, params)
if obj is None:
log.warning(f"Failed to create object {objtype} {initargs} {patch} {scope} {obj_name}")
log.warning(f"Failed to create object {objtype} {initargs} {patch} {scope} {obj_name} {params}")
return None
elif isinstance(obj, dict):
log.warning(f"Error in creating {objtype} {obj}")
Expand Down

0 comments on commit 879db10

Please sign in to comment.