Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/backend/DeckManagement/DeckController.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ def get_use_page_label_properties(self, position: str) -> dict:
"font-size": self.page_labels[position].font_size is not None
}

def get_composed_label(self, position: str) -> str:
def get_composed_label(self, position: str) -> KeyLabel:
use_page_label_properties = self.get_use_page_label_properties(position)

label = copy(self.action_labels.get(position)) or KeyLabel(self.controller_key)
Expand Down Expand Up @@ -1355,11 +1355,9 @@ def update_state_switcher(self):
def get_active_state(self) -> "ControllerKeyState":
return self.states.get(self.state, ControllerKeyState(self, -1))


def get_current_deck_image(self) -> Image.Image:
state = self.get_active_state()


background: Image.Image = None
# Only load the background image if it's not gonna be hidden by the background color
if self.get_active_state().background_color[-1] < 255:
Expand All @@ -1374,7 +1372,6 @@ def get_current_deck_image(self) -> Image.Image:
else:
background.paste(background_color_img, (0, 0), background_color_img)


if background is None:
background = self.deck_controller.generate_alpha_key().copy()

Expand Down Expand Up @@ -1514,7 +1511,7 @@ def add_labels_to_image(self, _image: Image.Image) -> Image.Image:
position = (image.width / 2, h/2 + 3)

if label == "center":
position = ((image.width - 0) / 2, (image.height - 0) / 2)
position = (image.width / 2, image.height / 2)

if label == "bottom":
position = (image.width / 2, image.height - h/2 - 3)
Expand Down Expand Up @@ -1670,15 +1667,15 @@ def load_from_page_dict(self, page_dict, update: bool = True, load_labels: bool
)
state.layout_manager.set_page_layout(layout, update=False)
elif len(state.get_own_actions()) > 1 and False: # Disabled for now - we might reuse it later
if state_dict.get("image-control-action") is None:
if not state_dict.get("image-control-action", False): # this image-control logic is deprecated
with Image.open(os.path.join("Assets", "images", "multi_action.png")) as image:
self.set_key_image(KeyImage(
controller_key=self,
image=image.copy(),
), update=False)

elif len(state.get_own_actions()) == 1:
if state_dict.get("image-control-action") is None:
if not state.get_own_actions()[0].has_image_control:
self.set_key_image(None, update=False)
# action = self.get_own_actions()[0]
# if action.has_image_control()
Expand Down
16 changes: 14 additions & 2 deletions src/backend/PageManagement/Page.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from src.backend.PluginManager.ActionHolder import ActionHolder
from src.backend.DeckManagement.DeckController import ControllerKeyState, ControllerKey


class Page:
def __init__(self, json_path, deck_controller, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -48,16 +49,18 @@ def __init__(self, json_path, deck_controller, *args, **kwargs):

self.load(load_from_file=True)

self.file_access_semaphore = threading.Semaphore()

def get_name(self) -> str:
return os.path.splitext(os.path.basename(self.json_path))[0]

def update_dict(self) -> None:
"""
Updates the dict without any updates on the action objects.
Do NOT use if you made changes to the action objects
"""
self.dict = gl.page_manager.get_page_json(self.json_path)

def load(self, load_from_file: bool = False):
start = time.time()
if load_from_file:
Expand All @@ -70,13 +73,15 @@ def load(self, load_from_file: bool = False):

def save(self):
# Make backup in case something goes wrong
self.file_access_semaphore.acquire()
self.make_backup()

without_objects = self.get_without_action_objects()
# Make keys last element
self.move_key_to_end(without_objects, "keys")
with open(self.json_path, "w") as f:
json.dump(without_objects, f, indent=4)
self.file_access_semaphore.release()

def make_backup(self):
os.makedirs(os.path.join(gl.DATA_PATH, "pages","backups"), exist_ok=True)
Expand Down Expand Up @@ -125,14 +130,21 @@ def load_action_objects(self):
self.action_objects[key].setdefault(state, {})

action_holder = gl.plugin_manager.get_action_holder_from_id(action["id"])

if action_holder is None:
plugin_id = gl.plugin_manager.get_plugin_id_from_action_id(action["id"])
if gl.plugin_manager.get_is_plugin_out_of_date(plugin_id):
self.action_objects[key][state][i] = ActionOutdated(id=action["id"])
else:
self.action_objects[key][state][i] = NoActionHolderFound(id=action["id"])
continue

action_class = action_holder.action_base

action_holder.has_image_control = action.get("has_image_control", False)
action_holder.has_top_label_control = action.get("has_top_label_control", False)
action_holder.has_center_label_control = action.get("has_center_label_control", False)
action_holder.has_bottom_label_control = action.get("has_bottom_label_control", False)

if action_class is None:
self.action_objects[key][state][i] = NoActionHolderFound(id=action["id"])
Expand Down
41 changes: 15 additions & 26 deletions src/backend/PluginManager/ActionBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class ActionBase(rpyc.Service):
# Change to match your action
def __init__(self, action_id: str, action_name: str,
deck_controller: "DeckController", page: "Page", coords: str, plugin_base: "PluginBase", state: int):
deck_controller: "DeckController", page: "Page", coords: str, plugin_base: "PluginBase", state: int, has_image_control: bool = False, has_top_label_control: bool = False, has_center_label_control: bool = False, has_bottom_label_control: bool = False):
#TODO: Add state arg to all init methods
self.backend_connection: Connection = None
self.backend: netref = None
Expand All @@ -58,6 +58,10 @@ def __init__(self, action_id: str, action_name: str,
self.KEY_IMAGE_CAN_BE_OVERWRITTEN: bool = True
self.LABELS_CAN_BE_OVERWRITTEN: list[bool] = [True, True, True]
self.has_configuration = False
self.has_image_control = has_image_control
self.has_top_label_control = has_top_label_control
self.has_center_label_control = has_center_label_control
self.has_bottom_label_control = has_bottom_label_control

self.labels = {}
self.current_key = {
Expand Down Expand Up @@ -158,7 +162,7 @@ def set_media(self, image = None, media_path=None, size: float = None, valign: f
if self.has_custom_user_asset():
return

if not self.has_image_control():
if not self.has_image_control:
return

if self.get_key_state().state != self.state:
Expand Down Expand Up @@ -221,7 +225,6 @@ def hide_error(self) -> None:
if not self.get_is_present(): return
if self.get_is_multi_action(): return
self.get_key_state().hide_error()


def set_label(self, text: str, position: str = "bottom", color: list[int] = None,
font_family: str = None, font_size = None, update: bool = True):
Expand All @@ -232,11 +235,9 @@ def set_label(self, text: str, position: str = "bottom", color: list[int] = None
if not self.on_ready_called:
update = False

label_index = 0 if position == "top" else 1 if position == "center" else 2

if not self.has_label_control()[label_index]:
if not self.has_label_control(position):
return

if text is None:
text = ""

Expand Down Expand Up @@ -306,27 +307,15 @@ def get_is_multi_action(self) -> bool:
actions = self.page.action_objects.get(self.page_coords, {}).get(self.state, [])
return len(actions) > 1

def has_label_control(self) -> list[bool]:
def has_label_control(self, position: str) -> bool:
self.raise_error_if_not_ready()

key_dict = self.page.dict.get("keys", {}).get(self.page_coords, {}).get("states", {}).get(str(self.state), {})

ind = self.get_own_action_index()

return [i == self.get_own_action_index() for i in key_dict.get("label-control-actions", [None, None, None])]

def has_image_control(self):
self.raise_error_if_not_ready()

key_dict = self.page.dict.get("keys", {}).get(self.page_coords, {}).get("states", {}).get(str(self.state), {})

if key_dict.get("image-control-action") is None:
return False

if not self.get_is_multi_action():
return True

return self.get_own_action_index() == key_dict.get("image-control-action")
if position == "top":
return self.has_top_label_control
elif position == "center":
return self.has_center_label_control
else:
return self.has_bottom_label_control

def get_is_present(self):
if self.page is None: return False
Expand Down
10 changes: 9 additions & 1 deletion src/backend/PluginManager/ActionHolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def __init__(self, plugin_base: "PluginBase", action_base: ActionBase, action_id
self.action_name = action_name
self.icon = icon
self.min_app_version = min_app_version
self.has_image_control = False
self.has_top_label_control = False
self.has_center_label_control = False
self.has_bottom_label_control = False

def get_is_compatible(self) -> bool:
if self.min_app_version is not None:
Expand All @@ -72,5 +76,9 @@ def init_and_get_action(self, deck_controller: DeckController, page: Page, coord
page = page,
coords = coords,
plugin_base = self.plugin_base,
state = state
state = state,
has_image_control = self.has_image_control,
has_top_label_control = self.has_top_label_control,
has_center_label_control = self.has_center_label_control,
has_bottom_label_control = self.has_bottom_label_control,
)
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,6 @@ def on_remove_button_click(self, button):

# Remove from page json
page.dict["keys"][self.action.page_coords]["states"][str(self.action.state)]["actions"].pop(self.index)

if page.dict["keys"][self.action.page_coords]["states"][str(self.action.state)]["image-control-action"] == self.index:
if len(page.dict["keys"][self.action.page_coords]["states"][str(self.action.state)]["actions"]) > 0:
page.dict["keys"][self.action.page_coords]["states"][str(self.action.state)]["image-control-action"] = 0
else:
page.dict["keys"][self.action.page_coords]["states"][str(self.action.state)]["image-control-action"] = None

page.save()

# Reload configurator
Expand Down
Loading