From c00542ef575ec717d79f1bce03d6e06d2e0e0517 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Wed, 6 Nov 2024 04:16:09 +0900 Subject: [PATCH 01/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20overlay=E3=81=AEsmall=20log=E3=81=AE=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 変数名をsmall_log用に修正 - OVERLAY_SETTINGSを削除しOVERLAY_SMALL_LOG_SETTINGSに統合 - OVERLAY_UI_TYPEを廃止 --- src-python/config.py | 34 +----- src-python/model.py | 55 ++++----- src-python/models/overlay/overlay.py | 12 +- src-python/models/overlay/overlay_image.py | 127 ++------------------- src-python/webui_controller.py | 39 +++---- 5 files changed, 59 insertions(+), 208 deletions(-) diff --git a/src-python/config.py b/src-python/config.py index fd21ca00..7231981e 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -738,19 +738,6 @@ class Config: self._SEND_MESSAGE_BUTTON_TYPE = value self.saveConfig(inspect.currentframe().f_code.co_name, value) - @property - @json_serializable('OVERLAY_SETTINGS') - def OVERLAY_SETTINGS(self): - return self._OVERLAY_SETTINGS - - @OVERLAY_SETTINGS.setter - def OVERLAY_SETTINGS(self, value): - if isinstance(value, dict) and set(value.keys()) == set(self.OVERLAY_SETTINGS.keys()): - for key, value in value.items(): - if isinstance(value, (int, float)): - self._OVERLAY_SETTINGS[key] = float(value) - self.saveConfig(inspect.currentframe().f_code.co_name, self.OVERLAY_SETTINGS) - @property @json_serializable('OVERLAY_SMALL_LOG') def OVERLAY_SMALL_LOG(self): @@ -778,19 +765,11 @@ class Config: case "display_duration" | "fadeout_duration": if isinstance(value, int): self._OVERLAY_SMALL_LOG_SETTINGS[key] = value + case "opacity" | "ui_scaling": + if isinstance(value, (int, float)): + self._OVERLAY_SMALL_LOG_SETTINGS[key] = float(value) self.saveConfig(inspect.currentframe().f_code.co_name, self.OVERLAY_SMALL_LOG_SETTINGS) - @property - @json_serializable('OVERLAY_UI_TYPE') - def OVERLAY_UI_TYPE(self): - return self._OVERLAY_UI_TYPE - - @OVERLAY_UI_TYPE.setter - def OVERLAY_UI_TYPE(self, value): - if isinstance(value, str): - self._OVERLAY_UI_TYPE = value - # self.saveConfig(inspect.currentframe().f_code.co_name, value) - @property @json_serializable('SEND_MESSAGE_TO_VRC') def SEND_MESSAGE_TO_VRC(self): @@ -1063,10 +1042,6 @@ class Config: self._AUTO_CLEAR_MESSAGE_BOX = True self._SEND_ONLY_TRANSLATED_MESSAGES = False self._SEND_MESSAGE_BUTTON_TYPE = "show" - self._OVERLAY_SETTINGS = { - "opacity": 1.0, - "ui_scaling": 1.0, - } self._OVERLAY_SMALL_LOG = False self._OVERLAY_SMALL_LOG_SETTINGS = { "x_pos": 0.0, @@ -1077,8 +1052,9 @@ class Config: "z_rotation": 0.0, "display_duration": 5, "fadeout_duration": 2, + "opacity": 1.0, + "ui_scaling": 1.0, } - self._OVERLAY_UI_TYPE = "default" self._SEND_MESSAGE_TO_VRC = True self._SEND_RECEIVED_MESSAGE_TO_VRC = False self._LOGGER_FEATURE = False diff --git a/src-python/model.py b/src-python/model.py index f06db96d..eb3ee04c 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -80,7 +80,9 @@ class Model: self.previous_receive_message = "" self.translator = Translator() self.keyword_processor = KeywordProcessor() - self.overlay = Overlay( + self.overlay_small_log = Overlay( + "VRCT_SMALL_LOG", + "VRCT_SMALL_LOG", config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["z_pos"], @@ -89,8 +91,8 @@ class Model: config.OVERLAY_SMALL_LOG_SETTINGS["z_rotation"], config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"], config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"], - config.OVERLAY_SETTINGS["opacity"], - config.OVERLAY_SETTINGS["ui_scaling"], + config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], + config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"], ) self.overlay_image = OverlayImage() self.pre_overlay_message = None @@ -685,18 +687,16 @@ class Model: self.speaker_energy_recorder.stop() self.speaker_energy_recorder = None - def createOverlayImageShort(self, message, translation): + def createOverlayImageSmall(self, message, translation): your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] - ui_type = config.OVERLAY_UI_TYPE self.pre_overlay_message = { "message" : message, "your_language" : your_language, "translation" : translation, "target_language" : target_language, - "ui_type" : ui_type, } - return self.overlay_image.createOverlayImageShort(message, your_language, translation, target_language, ui_type) + return self.overlay_image.createOverlayImageSmall(message, your_language, translation, target_language) def createOverlayImage(self, message): ui_language = config.UI_LANGUAGE @@ -708,20 +708,19 @@ class Model: "zh-Hant":"Chinese Traditional", } language = convert_languages.get(ui_language, "Japanese") - ui_type = config.OVERLAY_UI_TYPE - return self.overlay_image.createOverlayImage(message, language, ui_type) + return self.overlay_image.createOverlayImage(message, language) - def clearOverlayImage(self): - self.overlay.clearImage() + def clearOverlayImageSmall(self): + self.overlay_small_log.clearImage() - def updateOverlay(self, img): - self.overlay.updateImage(img) + def updateOverlaySmall(self, img): + self.overlay_small_log.updateImage(img) - def startOverlay(self): - self.overlay.startOverlay() + def startOverlaySmall(self): + self.overlay_small_log.startOverlay() - def updateOverlayPosition(self): - self.overlay.updatePosition( + def updateOverlaySmallLogSettings(self): + self.overlay_small_log.updatePosition( config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["z_pos"], @@ -729,23 +728,13 @@ class Model: config.OVERLAY_SMALL_LOG_SETTINGS["y_rotation"], config.OVERLAY_SMALL_LOG_SETTINGS["z_rotation"], ) + self.overlay_small_log.updateDisplayDuration(config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"]) + self.overlay_small_log.updateFadeoutDuration(config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"]) + self.overlay_small_log.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], with_fade=True) + self.overlay_small_log.updateUiScaling(config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"]) - def updateOverlayTimes(self): - display_duration = config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"] - self.overlay.updateDisplayDuration(display_duration) - fadeout_duration = config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"] - self.overlay.updateFadeoutDuration(fadeout_duration) - - def updateOverlayImageOpacity(self): - opacity = config.OVERLAY_SETTINGS["opacity"] - self.overlay.updateOpacity(opacity, with_fade=True) - - def updateOverlayImageUiScaling(self): - ui_scaling = config.OVERLAY_SETTINGS["ui_scaling"] - self.overlay.updateUiScaling(ui_scaling) - - def shutdownOverlay(self): - self.overlay.shutdownOverlay() + def shutdownOverlaySmall(self): + self.overlay_small_log.shutdownOverlay() def startWatchdog(self): self.th_watchdog = threadFnc(self.watchdog.start) diff --git a/src-python/models/overlay/overlay.py b/src-python/models/overlay/overlay.py index 0888a298..c2c9d1a1 100644 --- a/src-python/models/overlay/overlay.py +++ b/src-python/models/overlay/overlay.py @@ -63,8 +63,10 @@ def getRightHandBaseMatrix(): return arr class Overlay: - def __init__(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, display_duration, fadeout_duration, opacity, ui_scaling): + def __init__(self, key, name, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, display_duration, fadeout_duration, opacity, ui_scaling): self.initialized = False + self.key = key + self.name = name settings = { "color": [1, 1, 1], "opacity": opacity, @@ -92,7 +94,7 @@ class Overlay: self.system = openvr.init(openvr.VRApplication_Background) self.overlay = openvr.IVROverlay() self.overlay_system = openvr.IVRSystem() - self.handle = self.overlay.createOverlay("Overlay_Speaker2log", "Overlay_Speaker2log_UI") + self.handle = self.overlay.createOverlay(self.key, self.name) self.overlay.showOverlay(self.handle) self.initialized = True @@ -281,7 +283,7 @@ if __name__ == "__main__": # time.sleep(1) # # Example usage - # img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") + # img = overlay_image.createOverlayImageSmall("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") # overlay.updateImage(img) # time.sleep(100000) @@ -292,11 +294,11 @@ if __name__ == "__main__": # time.sleep(1) # # Example usage - # img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese", ui_type="sakura") + # img = overlay_image.createOverlayImageSmall("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese", ui_type="sakura") # overlay.updateImage(img) # time.sleep(0.5) - # img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") + # img = overlay_image.createOverlayImageSmall("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") # overlay.updateImage(img) # time.sleep(0.5) diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index 23e24c93..e6340899 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -4,13 +4,6 @@ from typing import Tuple from PIL import Image, ImageDraw, ImageFont class OverlayImage: - # TEXT_COLOR_LARGE = (223, 223, 223) - # TEXT_COLOR_SMALL = (190, 190, 190) - # TEXT_COLOR_SEND = (70, 161, 146) - # TEXT_COLOR_RECEIVE = (220, 20, 60) - # TEXT_COLOR_TIME = (120, 120, 120) - # FONT_SIZE_LARGE = HEIGHT - # FONT_SIZE_SMALL = int(FONT_SIZE_LARGE * 2 / 3) LANGUAGES = { "Japanese": "NotoSansJP-Regular", "Korean": "NotoSansKR-Regular", @@ -37,115 +30,16 @@ class OverlayImage: result.paste(image, (left, top)) return result - # def create_textimage(self, message_type, size, text, language): - # font_size = self.FONT_SIZE_LARGE if size == "large" else self.FONT_SIZE_SMALL - # text_color = self.TEXT_COLOR_LARGE if size == "large" else self.TEXT_COLOR_SMALL - # anchor = "lm" if message_type == "receive" else "rm" - # text_x = 0 if message_type == "receive" else self.WIDTH - # align = "left" if message_type == "receive" else "right" - - # font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") - # img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) - # draw = ImageDraw.Draw(img) - # font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "fonts", f"{font_family}.ttf"), font_size) - # # font = ImageFont.truetype(os_path.join("./fonts", f"{font_family}.ttf"), font_size) - # text_width = draw.textlength(text, font) - # character_width = text_width // len(text) - # character_line_num = int(self.WIDTH // character_width) - # if len(text) > character_line_num: - # text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)]) - - # n_num = len(text.split("\n")) - 1 - # text_height = int(font_size*(n_num+2)) - - # img = Image.new("RGBA", (self.WIDTH, text_height), (0, 0, 0, 0)) - # draw = ImageDraw.Draw(img) - - # text_y = text_height // 2 - - # draw.multiline_text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font, align=align) - # return img - - # def create_textimage_message_type(self, message_type): - # anchor = "lm" if message_type == "receive" else "rm" - # text = "Receive" if message_type == "receive" else "Send" - # text_color = self.TEXT_COLOR_RECEIVE if message_type == "receive" else self.TEXT_COLOR_SEND - # text_color_time = self.TEXT_COLOR_TIME - - # now = datetime.now() - # formatted_time = now.strftime("%H:%M") - # font_size = self.FONT_SIZE_SMALL - # img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) - # draw = ImageDraw.Draw(img) - # font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "fonts", "NotoSansJP-Regular.ttf"), font_size) - # # font = ImageFont.truetype(os_path.join("./fonts", "NotoSansJP-Regular.ttf"), font_size) - # text_height = font_size*2 - # text_width = draw.textlength(formatted_time, font) - # character_width = text_width // len(formatted_time) - # img = Image.new("RGBA", (self.WIDTH, text_height), (0, 0, 0, 0)) - # draw = ImageDraw.Draw(img) - # text_y = text_height // 2 - # text_time_x = 0 if message_type == "receive" else self.WIDTH - (text_width + character_width) - # text_x = (text_width + character_width) if message_type == "receive" else self.WIDTH - - # draw.text((text_time_x, text_y), formatted_time, text_color_time, anchor=anchor, stroke_width=0, font=font) - # draw.text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font) - # return img - - # def create_textbox(self, message_type, message, your_language, translation, target_language): - # message_type_img = self.create_textimage_message_type(message_type) - # if len(translation) > 0 and target_language is not None: - # img = self.create_textimage(message_type, "small", message, your_language) - # translation_img = self.create_textimage(message_type, "large",translation, target_language) - # img = self.concatenateImagesVertically(img, translation_img) - # else: - # img = self.create_textimage(message_type, "large", message, your_language) - # return self.concatenateImagesVertically(message_type_img, img) - - # def create_overlay_image_long(self, message_type, message, your_language, translation="", target_language=None): - # if len(self.log_data) > 10: - # self.log_data.pop(0) - - # self.log_data.append( - # { - # "message_type":message_type, - # "message":message, - # "your_language":your_language, - # "translation":translation, - # "target_language":target_language, - # } - # ) - - # imgs = [] - # for log in self.log_data: - # message_type = log["message_type"] - # message = log["message"] - # your_language = log["your_language"] - # translation = log["translation"] - # target_language = log["target_language"] - # img = self.create_textbox(message_type, message, your_language, translation, target_language) - # imgs.append(img) - - # img = imgs[0] - # for i in imgs[1:]: - # img = self.concatenateImagesVertically(img, i) - # img = self.addImageMargin(img, 0, 20, 0, 20, (0, 0, 0, 0)) - - # width, height = img.size - # background = Image.new("RGBA", (width, height), (0, 0, 0, 0)) - # draw = ImageDraw.Draw(background) - # draw.rounded_rectangle([(0, 0), (width, height)], radius=15, fill=self.BACKGROUND_COLOR, outline=self.BACKGROUND_OUTLINE_COLOR, width=5) - # img = Image.alpha_composite(background, img) - # return img - - def getUiSize(self): + @staticmethod + def getUiSize(): return { "width": int(960*4), "height": int(23*4), "font_size": int(23*4), } - def getUiColors(self, ui_type): + @staticmethod + def getUiColors(ui_type): match ui_type: case "default": background_color = (41, 42, 45) @@ -161,7 +55,8 @@ class OverlayImage: "text_color": text_color } - def createDecorationImage(self, ui_type, image_size): + @staticmethod + def createDecorationImage(ui_type, image_size): decoration_image = Image.new("RGBA", image_size, (0, 0, 0, 0)) match ui_type: case "default": @@ -186,7 +81,7 @@ class OverlayImage: decoration_image.paste(overlay_br, (image_size[0]-overlay_br.size[0]-margin, image_size[1]-overlay_br.size[1]-margin)) return decoration_image - def createTextboxShort(self, text, language, text_color, base_width, base_height, font_size): + def createTextboxSmall(self, text, language, text_color, base_width, base_height, font_size): font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") img = Image.new("RGBA", (base_width, base_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) @@ -205,7 +100,7 @@ class OverlayImage: draw.text((text_x, text_y), text, text_color, anchor="mm", stroke_width=0, font=font, align="center") return img - def createOverlayImageShort(self, message, your_language, translation="", target_language=None, ui_type="default"): + def createOverlayImageSmall(self, message, your_language, translation="", target_language=None, ui_type="default"): ui_size = self.getUiSize() height = ui_size["height"] width = ui_size["width"] @@ -216,9 +111,9 @@ class OverlayImage: background_color = ui_colors["background_color"] background_outline_color = ui_colors["background_outline_color"] - img = self.createTextboxShort(message, your_language, text_color, width, height, font_size) + img = self.createTextboxSmall(message, your_language, text_color, width, height, font_size) if len(translation) > 0 and target_language is not None: - translation_img = self.createTextboxShort(translation, target_language, text_color, width, height, font_size) + translation_img = self.createTextboxSmall(translation, target_language, text_color, width, height, font_size) img = self.concatenateImagesVertically(img, translation_img) background = Image.new("RGBA", img.size, (0, 0, 0, 0)) @@ -241,7 +136,7 @@ class OverlayImage: background_color = ui_colors["background_color"] background_outline_color = ui_colors["background_outline_color"] - img = self.createTextboxShort(message, your_language, text_color, width, height, font_size) + img = self.createTextboxSmall(message, your_language, text_color, width, height, font_size) background = Image.new("RGBA", img.size, (0, 0, 0, 0)) draw = ImageDraw.Draw(background) diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 3022f78e..8b5eddf4 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -251,7 +251,7 @@ class Controller: model.logger.info(f"[SENT] {message}{translation}") # if config.OVERLAY_SMALL_LOG is True: - # overlay_image = model.createOverlayImageShort(message, translation) + # overlay_image = model.createOverlayImageSmall(message, translation) # model.updateOverlay(overlay_image) # overlay_image = model.createOverlayImageLong("send", message, translation) # model.updateOverlay(overlay_image) @@ -292,9 +292,9 @@ class Controller: if config.ENABLE_TRANSCRIPTION_RECEIVE is True: if config.OVERLAY_SMALL_LOG is True: - if model.overlay.initialized is True: - overlay_image = model.createOverlayImageShort(message, translation) - model.updateOverlay(overlay_image) + if model.overlay_small_log.initialized is True: + overlay_image = model.createOverlayImageSmall(message, translation) + model.updateOverlaySmall(overlay_image) # overlay_image = model.createOverlayImageLong("receive", message, translation) # model.updateOverlay(overlay_image) @@ -362,7 +362,7 @@ class Controller: model.oscSendMessage(osc_message) # if config.OVERLAY_SMALL_LOG is True: - # overlay_image = model.createOverlayImageShort(message, translation) + # overlay_image = model.createOverlayImageSmall(message, translation) # model.updateOverlay(overlay_image) # overlay_image = model.createOverlayImageLong("send", message, translation) # model.updateOverlay(overlay_image) @@ -1169,17 +1169,6 @@ class Controller: config.SEND_MESSAGE_BUTTON_TYPE = data return {"status":200, "result":config.SEND_MESSAGE_BUTTON_TYPE} - @staticmethod - def getOverlaySettings(*args, **kwargs) -> dict: - return {"status":200, "result":config.OVERLAY_SETTINGS} - - @staticmethod - def setOverlaySettings(data, *args, **kwargs) -> dict: - config.OVERLAY_SETTINGS = data - model.updateOverlayImageOpacity() - model.updateOverlayImageUiScaling() - return {"status":200, "result":config.OVERLAY_SETTINGS} - @staticmethod def getOverlaySmallLogSettings(*args, **kwargs) -> dict: return {"status":200, "result":config.OVERLAY_SMALL_LOG_SETTINGS} @@ -1187,7 +1176,7 @@ class Controller: @staticmethod def setOverlaySmallLogSettings(data, *args, **kwargs) -> dict: config.OVERLAY_SMALL_LOG_SETTINGS = data - model.updateOverlayPosition() + model.updateOverlaySmallLogSettings() return {"status":200, "result":config.OVERLAY_SMALL_LOG_SETTINGS} @staticmethod @@ -1198,16 +1187,16 @@ class Controller: def setEnableOverlaySmallLog(*args, **kwargs) -> dict: config.OVERLAY_SMALL_LOG = True if config.OVERLAY_SMALL_LOG is True and config.ENABLE_TRANSCRIPTION_RECEIVE is True: - if model.overlay.initialized is False and model.overlay.checkSteamvrRunning() is True: - model.startOverlay() + if model.overlay_small_log.initialized is False and model.overlay_small_log.checkSteamvrRunning() is True: + model.startOverlaySmall() return {"status":200, "result":config.OVERLAY_SMALL_LOG} @staticmethod def setDisableOverlaySmallLog(*args, **kwargs) -> dict: config.OVERLAY_SMALL_LOG = False if config.OVERLAY_SMALL_LOG is False: - model.clearOverlayImage() - model.shutdownOverlay() + model.clearOverlayImageSmall() + model.shutdownOverlaySmall() return {"status":200, "result":config.OVERLAY_SMALL_LOG} @staticmethod @@ -1356,8 +1345,8 @@ class Controller: def setEnableTranscriptionReceive(self, *args, **kwargs) -> dict: self.startThreadingTranscriptionReceiveMessage() if config.OVERLAY_SMALL_LOG is True: - if model.overlay.initialized is False and model.overlay.checkSteamvrRunning() is True: - model.startOverlay() + if model.overlay_small_log.initialized is False and model.overlay_small_log.checkSteamvrRunning() is True: + model.startOverlaySmall() config.ENABLE_TRANSCRIPTION_RECEIVE = True return {"status":200, "result":config.ENABLE_TRANSCRIPTION_RECEIVE} @@ -1385,9 +1374,9 @@ class Controller: @staticmethod def sendTextOverlaySmallLog(data, *args, **kwargs) -> dict: if config.OVERLAY_SMALL_LOG is True: - if model.overlay.initialized is True: + if model.overlay_small_log.initialized is True: overlay_image = model.createOverlayImage(data) - model.updateOverlay(overlay_image) + model.updateOverlaySmall(overlay_image) return {"status":200, "result":data} def swapYourLanguageAndTargetLanguage(self, *args, **kwargs) -> dict: From 78dad96124ba9406eff3c7821177f97aa632c879 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sat, 9 Nov 2024 16:49:45 +0900 Subject: [PATCH 02/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay=20Large=20log=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/config.py | 45 +++++ src-python/model.py | 57 ++++-- src-python/models/overlay/overlay_image.py | 200 +++++++++++++++------ src-python/webui_controller.py | 58 ++++-- src-python/webui_mainloop.py | 14 +- 5 files changed, 289 insertions(+), 85 deletions(-) diff --git a/src-python/config.py b/src-python/config.py index 7231981e..da663168 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -770,6 +770,38 @@ class Config: self._OVERLAY_SMALL_LOG_SETTINGS[key] = float(value) self.saveConfig(inspect.currentframe().f_code.co_name, self.OVERLAY_SMALL_LOG_SETTINGS) + @property + @json_serializable('OVERLAY_LARGE_LOG') + def OVERLAY_LARGE_LOG(self): + return self._OVERLAY_LARGE_LOG + + @OVERLAY_LARGE_LOG.setter + def OVERLAY_LARGE_LOG(self, value): + if isinstance(value, bool): + self._OVERLAY_LARGE_LOG = value + self.saveConfig(inspect.currentframe().f_code.co_name, value) + + @property + @json_serializable('OVERLAY_LARGE_LOG_SETTINGS') + def OVERLAY_LARGE_LOG_SETTINGS(self): + return self._OVERLAY_LARGE_LOG_SETTINGS + + @OVERLAY_LARGE_LOG_SETTINGS.setter + def OVERLAY_LARGE_LOG_SETTINGS(self, value): + if isinstance(value, dict) and set(value.keys()) == set(self.OVERLAY_LARGE_LOG_SETTINGS.keys()): + for key, value in value.items(): + match (key): + case "x_pos" | "y_pos" | "z_pos" | "x_rotation" | "y_rotation" | "z_rotation": + if isinstance(value, (int, float)): + self._OVERLAY_LARGE_LOG_SETTINGS[key] = float(value) + case "display_duration" | "fadeout_duration": + if isinstance(value, int): + self._OVERLAY_LARGE_LOG_SETTINGS[key] = value + case "opacity" | "ui_scaling": + if isinstance(value, (int, float)): + self._OVERLAY_LARGE_LOG_SETTINGS[key] = float(value) + self.saveConfig(inspect.currentframe().f_code.co_name, self.OVERLAY_LARGE_LOG_SETTINGS) + @property @json_serializable('SEND_MESSAGE_TO_VRC') def SEND_MESSAGE_TO_VRC(self): @@ -1055,6 +1087,19 @@ class Config: "opacity": 1.0, "ui_scaling": 1.0, } + self._OVERLAY_LARGE_LOG = False + self._OVERLAY_LARGE_LOG_SETTINGS = { + "x_pos": 0.0, + "y_pos": 0.0, + "z_pos": 0.0, + "x_rotation": 0.0, + "y_rotation": 0.0, + "z_rotation": 0.0, + "display_duration": 5, + "fadeout_duration": 2, + "opacity": 1.0, + "ui_scaling": 1.0, + } self._SEND_MESSAGE_TO_VRC = True self._SEND_RECEIVED_MESSAGE_TO_VRC = False self._LOGGER_FEATURE = False diff --git a/src-python/model.py b/src-python/model.py index eb3ee04c..c6a30735 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -94,9 +94,21 @@ class Model: config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"], ) + self.overlay_large_log = Overlay( + "VRCT_LARGE_LOG", + "VRCT_LARGE_LOG", + config.OVERLAY_LARGE_LOG_SETTINGS["x_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["y_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["z_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["x_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["y_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["z_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"], + config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"], + config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], + config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"], + ) self.overlay_image = OverlayImage() - self.pre_overlay_message = None - self.th_overlay = None self.mic_audio_queue = None self.mic_mute_status = None self.kks = kakasi() @@ -690,15 +702,9 @@ class Model: def createOverlayImageSmall(self, message, translation): your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] - self.pre_overlay_message = { - "message" : message, - "your_language" : your_language, - "translation" : translation, - "target_language" : target_language, - } return self.overlay_image.createOverlayImageSmall(message, your_language, translation, target_language) - def createOverlayImage(self, message): + def createOverlayImageSystemMessage(self, message): ui_language = config.UI_LANGUAGE convert_languages = { "en": "Japanese", @@ -708,7 +714,7 @@ class Model: "zh-Hant":"Chinese Traditional", } language = convert_languages.get(ui_language, "Japanese") - return self.overlay_image.createOverlayImage(message, language) + return self.overlay_image.createOverlayImageSmall(message, language) def clearOverlayImageSmall(self): self.overlay_small_log.clearImage() @@ -736,6 +742,37 @@ class Model: def shutdownOverlaySmall(self): self.overlay_small_log.shutdownOverlay() + def createOverlayImageLarge(self, message, translation): + your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] + target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] + return self.overlay_image.createOverlayImageLarge(message, your_language, translation, target_language) + + def clearOverlayImageLarge(self): + self.overlay_large_log.clearImage() + + def updateOverlayLarge(self, img): + self.overlay_large_log.updateImage(img) + + def startOverlayLarge(self): + self.overlay_large_log.startOverlay() + + def updateOverlayLargeLogSettings(self): + self.overlay_large_log.updatePosition( + config.OVERLAY_LARGE_LOG_SETTINGS["x_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["y_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["z_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["x_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["y_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["z_rotation"], + ) + self.overlay_large_log.updateDisplayDuration(config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"]) + self.overlay_large_log.updateFadeoutDuration(config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"]) + self.overlay_large_log.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], with_fade=True) + self.overlay_large_log.updateUiScaling(config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"]) + + def shutdownOverlayLarge(self): + self.overlay_large_log.shutdownOverlay() + def startWatchdog(self): self.th_watchdog = threadFnc(self.watchdog.start) self.th_watchdog.daemon = True diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index e6340899..df6e303f 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -1,5 +1,5 @@ from os import path as os_path -# from datetime import datetime +from datetime import datetime from typing import Tuple from PIL import Image, ImageDraw, ImageFont @@ -12,7 +12,7 @@ class OverlayImage: } def __init__(self): - pass + self.message_log = [] @staticmethod def concatenateImagesVertically(img1: Image, img2: Image) -> Image: @@ -31,7 +31,7 @@ class OverlayImage: return result @staticmethod - def getUiSize(): + def getUiSizeSmall(): return { "width": int(960*4), "height": int(23*4), @@ -39,48 +39,21 @@ class OverlayImage: } @staticmethod - def getUiColors(ui_type): + def getUiColorSmall(ui_type): + background_color = (41, 42, 45) + background_outline_color = (41, 42, 45) + text_color = (223, 223, 223) match ui_type: case "default": - background_color = (41, 42, 45) - background_outline_color = (41, 42, 45) - text_color = (223, 223, 223) - case "sakura": - background_color = (225, 40, 30) - background_outline_color = (255, 255, 255) - text_color = (223, 223, 223) + pass + case _: + pass return { "background_color": background_color, "background_outline_color": background_outline_color, "text_color": text_color } - @staticmethod - def createDecorationImage(ui_type, image_size): - decoration_image = Image.new("RGBA", image_size, (0, 0, 0, 0)) - match ui_type: - case "default": - pass - case "sakura": - margin = 7 - alpha_ratio = 0.4 - overlay_tl = Image.open(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "img", "overlay_tl_sakura.png")) - overlay_br = Image.open(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "img", "overlay_br_sakura.png")) - if overlay_tl.size[1] > image_size[1]: - overlay_tl = overlay_tl.resize((image_size[1]-margin, image_size[1]-margin)) - if overlay_br.size[1] > image_size[1]: - overlay_br = overlay_br.resize((image_size[1]-margin, image_size[1]-margin)) - - alpha = overlay_tl.getchannel("A") - alpha = alpha.point(lambda x: x * alpha_ratio) - overlay_tl.putalpha(alpha) - alpha = overlay_br.getchannel("A") - alpha = alpha.point(lambda x: x * alpha_ratio) - overlay_br.putalpha(alpha) - decoration_image.paste(overlay_tl, (margin, margin)) - decoration_image.paste(overlay_br, (image_size[0]-overlay_br.size[0]-margin, image_size[1]-overlay_br.size[1]-margin)) - return decoration_image - def createTextboxSmall(self, text, language, text_color, base_width, base_height, font_size): font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") img = Image.new("RGBA", (base_width, base_height), (0, 0, 0, 0)) @@ -101,12 +74,12 @@ class OverlayImage: return img def createOverlayImageSmall(self, message, your_language, translation="", target_language=None, ui_type="default"): - ui_size = self.getUiSize() + ui_size = self.getUiSizeSmall() height = ui_size["height"] width = ui_size["width"] font_size = ui_size["font_size"] - ui_colors = self.getUiColors(ui_type) + ui_colors = self.getUiColorSmall(ui_type) text_color = ui_colors["text_color"] background_color = ui_colors["background_color"] background_outline_color = ui_colors["background_outline_color"] @@ -120,29 +93,146 @@ class OverlayImage: draw = ImageDraw.Draw(background) draw.rounded_rectangle([(0, 0), img.size], radius=30, fill=background_color, outline=background_outline_color, width=5) - decoration_image = self.createDecorationImage(ui_type, img.size) - background = Image.alpha_composite(background, decoration_image) img = Image.alpha_composite(background, img) return img - def createOverlayImage(self, message, your_language, ui_type="default"): - ui_size = self.getUiSize() - height = ui_size["height"] + @staticmethod + def getUiSizeLarge(): + return { + "width": int(960*2), + "font_size_large": int(23*4), + "font_size_small": int(23*4*2/3), + } + + @staticmethod + def getUiColorLarge(): + background_color = (41, 42, 45) + background_outline_color = (41, 42, 45) + text_color_large = (223, 223, 223) + text_color_small = (190, 190, 190) + text_color_send = (70, 161, 146) + text_color_receive = (220, 20, 60) + text_color_time = (120, 120, 120) + return { + "background_color": background_color, + "background_outline_color": background_outline_color, + "text_color_large": text_color_large, + "text_color_small": text_color_small, + "text_color_send": text_color_send, + "text_color_receive": text_color_receive, + "text_color_time": text_color_time + } + + def createTextImageLarge(self, message_type, size, text, language): + ui_size = self.getUiSizeLarge() + font_size_large = ui_size["font_size_large"] + font_size_small = ui_size["font_size_small"] width = ui_size["width"] - font_size = ui_size["font_size"] - ui_colors = self.getUiColors(ui_type) - text_color = ui_colors["text_color"] - background_color = ui_colors["background_color"] - background_outline_color = ui_colors["background_outline_color"] + ui_color = self.getUiColorLarge() + text_color_large = ui_color["text_color_large"] + text_color_small = ui_color["text_color_small"] - img = self.createTextboxSmall(message, your_language, text_color, width, height, font_size) + font_size = font_size_large if size == "large" else font_size_small + text_color = text_color_large if size == "large" else text_color_small + anchor = "lm" if message_type == "receive" else "rm" + text_x = 0 if message_type == "receive" else width + align = "left" if message_type == "receive" else "right" + font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") - background = Image.new("RGBA", img.size, (0, 0, 0, 0)) + img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) + draw = ImageDraw.Draw(img) + font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + text_width = draw.textlength(text, font) + character_width = text_width // len(text) + character_line_num = int(width // character_width) + if len(text) > character_line_num: + text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)]) + n_num = len(text.split("\n")) - 1 + text_height = int(font_size*(n_num+2)) + img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0)) + draw = ImageDraw.Draw(img) + text_y = text_height // 2 + draw.multiline_text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font, align=align) + return img + + def createTextImageMessageType(self, message_type, date_time): + ui_size = self.getUiSizeLarge() + width = ui_size["width"] + font_size = ui_size["font_size_small"] + + ui_color = self.getUiColorLarge() + text_color_send = ui_color["text_color_send"] + text_color_receive = ui_color["text_color_receive"] + text_color_time = ui_color["text_color_time"] + + anchor = "lm" if message_type == "receive" else "rm" + text = "Receive" if message_type == "receive" else "Send" + text_color = text_color_receive if message_type == "receive" else text_color_send + + img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) + draw = ImageDraw.Draw(img) + font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", "NotoSansJP-Regular.ttf"), font_size) + text_height = font_size*2 + text_width = draw.textlength(date_time, font) + character_width = text_width // len(date_time) + img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0)) + draw = ImageDraw.Draw(img) + text_y = text_height // 2 + text_time_x = 0 if message_type == "receive" else width - (text_width + character_width) + text_x = (text_width + character_width) if message_type == "receive" else width + draw.text((text_time_x, text_y), date_time, text_color_time, anchor=anchor, stroke_width=0, font=font) + draw.text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font) + return img + + def createTextboxLarge(self, message_type, message, your_language, translation, target_language, date_time): + message_type_img = self.createTextImageMessageType(message_type, date_time) + if len(translation) > 0 and target_language is not None: + img = self.createTextImageLarge(message_type, "small", message, your_language) + translation_img = self.createTextImageLarge(message_type, "large",translation, target_language) + img = self.concatenateImagesVertically(img, translation_img) + else: + img = self.createTextImageLarge(message_type, "large", message, your_language) + return self.concatenateImagesVertically(message_type_img, img) + + def createOverlayImageLarge(self, message_type, message, your_language, translation="", target_language=None): + ui_color = self.getUiColorLarge() + background_color = ui_color["background_color"] + background_outline_color = ui_color["background_outline_color"] + + self.message_log.append( + { + "message_type":message_type, + "message":message, + "your_language":your_language, + "translation":translation, + "target_language":target_language, + "datetime":datetime.now().strftime("%H:%M") + } + ) + + if len(self.message_log) > 10: + self.message_log = self.message_log[-10:] + + imgs = [] + for log in self.message_log: + message_type = log["message_type"] + message = log["message"] + your_language = log["your_language"] + translation = log["translation"] + target_language = log["target_language"] + date_time = log["datetime"] + img = self.createTextboxLarge(message_type, message, your_language, translation, target_language, date_time) + imgs.append(img) + + img = imgs[0] + for i in imgs[1:]: + img = self.concatenateImagesVertically(img, i) + img = self.addImageMargin(img, 0, 20, 0, 20, (0, 0, 0, 0)) + + width, height = img.size + background = Image.new("RGBA", (width, height), (0, 0, 0, 0)) draw = ImageDraw.Draw(background) - draw.rounded_rectangle([(0, 0), img.size], radius=30, fill=background_color, outline=background_outline_color, width=5) - - decoration_image = self.createDecorationImage(ui_type, img.size) - background = Image.alpha_composite(background, decoration_image) + draw.rounded_rectangle([(0, 0), (width, height)], radius=15, fill=background_color, outline=background_outline_color, width=5) img = Image.alpha_composite(background, img) return img \ No newline at end of file diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 8b5eddf4..f46fd67e 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -1169,6 +1169,25 @@ class Controller: config.SEND_MESSAGE_BUTTON_TYPE = data return {"status":200, "result":config.SEND_MESSAGE_BUTTON_TYPE} + @staticmethod + def getOverlaySmallLog(*args, **kwargs) -> dict: + return {"status":200, "result":config.OVERLAY_SMALL_LOG} + + @staticmethod + def setEnableOverlaySmallLog(*args, **kwargs) -> dict: + config.OVERLAY_SMALL_LOG = True + if model.overlay_small_log.initialized is False and model.overlay_small_log.checkSteamvrRunning() is True: + model.startOverlaySmall() + return {"status":200, "result":config.OVERLAY_SMALL_LOG} + + @staticmethod + def setDisableOverlaySmallLog(*args, **kwargs) -> dict: + config.OVERLAY_SMALL_LOG = False + if config.OVERLAY_SMALL_LOG is False: + model.clearOverlayImageSmall() + model.shutdownOverlaySmall() + return {"status":200, "result":config.OVERLAY_SMALL_LOG} + @staticmethod def getOverlaySmallLogSettings(*args, **kwargs) -> dict: return {"status":200, "result":config.OVERLAY_SMALL_LOG_SETTINGS} @@ -1180,24 +1199,33 @@ class Controller: return {"status":200, "result":config.OVERLAY_SMALL_LOG_SETTINGS} @staticmethod - def getOverlaySmallLog(*args, **kwargs) -> dict: - return {"status":200, "result":config.OVERLAY_SMALL_LOG} + def getOverlayLargeLog(*args, **kwargs) -> dict: + return {"status":200, "result":config.OVERLAY_LARGE_LOG} @staticmethod - def setEnableOverlaySmallLog(*args, **kwargs) -> dict: - config.OVERLAY_SMALL_LOG = True - if config.OVERLAY_SMALL_LOG is True and config.ENABLE_TRANSCRIPTION_RECEIVE is True: - if model.overlay_small_log.initialized is False and model.overlay_small_log.checkSteamvrRunning() is True: - model.startOverlaySmall() - return {"status":200, "result":config.OVERLAY_SMALL_LOG} + def setEnableOverlayLargeLog(*args, **kwargs) -> dict: + config.OVERLAY_LARGE_LOG = True + if model.overlay_large_log.initialized is False and model.overlay_large_log.checkSteamvrRunning() is True: + model.startOverlayLarge() + return {"status":200, "result":config.OVERLAY_LARGE_LOG} @staticmethod - def setDisableOverlaySmallLog(*args, **kwargs) -> dict: - config.OVERLAY_SMALL_LOG = False - if config.OVERLAY_SMALL_LOG is False: - model.clearOverlayImageSmall() - model.shutdownOverlaySmall() - return {"status":200, "result":config.OVERLAY_SMALL_LOG} + def setDisableOverlayLargeLog(*args, **kwargs) -> dict: + config.OVERLAY_LARGE_LOG = False + if config.OVERLAY_LARGE_LOG is False: + model.clearOverlayImageLarge() + model.shutdownOverlayLarge() + return {"status":200, "result":config.OVERLAY_LARGE_LOG} + + @staticmethod + def getOverlayLargeLogSettings(*args, **kwargs) -> dict: + return {"status":200, "result":config.OVERLAY_LARGE_LOG_SETTINGS} + + @staticmethod + def setOverlayLargeLogSettings(data, *args, **kwargs) -> dict: + config.OVERLAY_LARGE_LOG_SETTINGS = data + model.updateOverlayLargeLogSettings() + return {"status":200, "result":config.OVERLAY_LARGE_LOG_SETTINGS} @staticmethod def getSendMessageToVrc(*args, **kwargs) -> dict: @@ -1375,7 +1403,7 @@ class Controller: def sendTextOverlaySmallLog(data, *args, **kwargs) -> dict: if config.OVERLAY_SMALL_LOG is True: if model.overlay_small_log.initialized is True: - overlay_image = model.createOverlayImage(data) + overlay_image = model.createOverlayImageSystemMessage(data) model.updateOverlaySmall(overlay_image) return {"status":200, "result":data} diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index 3e9df2ce..dacc1080 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -241,15 +241,19 @@ mapping = { "/run/download_whisper_weight": {"status": True, "variable":controller.downloadWhisperWeight}, # VR - "/get/data/overlay_settings": {"status": True, "variable":controller.getOverlaySettings}, - "/set/data/overlay_settings": {"status": True, "variable":controller.setOverlaySettings}, + "/get/data/overlay_small_log": {"status": True, "variable":controller.getOverlaySmallLog}, + "/set/enable/overlay_small_log": {"status": True, "variable":controller.setEnableOverlaySmallLog}, + "/set/disable/overlay_small_log": {"status": True, "variable":controller.setDisableOverlaySmallLog}, "/get/data/overlay_small_log_settings": {"status": True, "variable":controller.getOverlaySmallLogSettings}, "/set/data/overlay_small_log_settings": {"status": True, "variable":controller.setOverlaySmallLogSettings}, - "/get/data/overlay_small_log": {"status": True, "variable":controller.getOverlaySmallLog}, - "/set/enable/overlay_small_log": {"status": True, "variable":controller.setEnableOverlaySmallLog}, - "/set/disable/overlay_small_log": {"status": True, "variable":controller.setDisableOverlaySmallLog}, + "/get/data/overlay_large_log": {"status": True, "variable":controller.getOverlayLargeLog}, + "/set/enable/overlay_large_log": {"status": True, "variable":controller.setEnableOverlayLargeLog}, + "/set/disable/overlay_large_log": {"status": True, "variable":controller.setDisableOverlayLargeLog}, + + "/get/data/overlay_large_log_settings": {"status": True, "variable":controller.getOverlayLargeLogSettings}, + "/set/data/overlay_large_log_settings": {"status": True, "variable":controller.setOverlayLargeLogSettings}, # Others "/get/data/auto_clear_message_box": {"status": True, "variable":controller.getAutoClearMessageBox}, From d4f58fdb90c03055220aad5bfba1c5e4c475cbcf Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sat, 9 Nov 2024 17:16:35 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay=20Large=20log=E3=82=92=E6=96=87=E5=AD=97=E8=B5=B7?= =?UTF-8?q?=E3=81=93=E3=81=97=E5=8B=95=E4=BD=9C=E6=99=82=E3=81=AB=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/model.py | 4 ++-- src-python/webui_controller.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index c6a30735..98a6588a 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -742,10 +742,10 @@ class Model: def shutdownOverlaySmall(self): self.overlay_small_log.shutdownOverlay() - def createOverlayImageLarge(self, message, translation): + def createOverlayImageLarge(self, message_type:str, message:str, translation:str): your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] - return self.overlay_image.createOverlayImageLarge(message, your_language, translation, target_language) + return self.overlay_image.createOverlayImageLarge(message_type, message, your_language, translation, target_language) def clearOverlayImageLarge(self): self.overlay_large_log.clearImage() diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index f46fd67e..405617b8 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -250,11 +250,9 @@ class Controller: translation = " (" + "/".join(translation) + ")" model.logger.info(f"[SENT] {message}{translation}") - # if config.OVERLAY_SMALL_LOG is True: - # overlay_image = model.createOverlayImageSmall(message, translation) - # model.updateOverlay(overlay_image) - # overlay_image = model.createOverlayImageLong("send", message, translation) - # model.updateOverlay(overlay_image) + if config.OVERLAY_LARGE_LOG is True: + overlay_image = model.createOverlayImageLarge("send", message, translation) + model.updateOverlayLarge(overlay_image) def speakerMessage(self, message) -> None: if isinstance(message, bool) and message is False: @@ -295,8 +293,11 @@ class Controller: if model.overlay_small_log.initialized is True: overlay_image = model.createOverlayImageSmall(message, translation) model.updateOverlaySmall(overlay_image) - # overlay_image = model.createOverlayImageLong("receive", message, translation) - # model.updateOverlay(overlay_image) + + if config.OVERLAY_LARGE_LOG is True: + if model.overlay_large_log.initialized is True: + overlay_image = model.createOverlayImageLarge("receive", message, translation) + model.updateOverlayLarge(overlay_image) if config.SEND_RECEIVED_MESSAGE_TO_VRC is True: osc_message = self.messageFormatter("RECEIVED", translation, [message]) From 7f1b635f63e47440f110482c7f69f8b715ab66a0 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sat, 9 Nov 2024 17:33:51 +0900 Subject: [PATCH 04/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay=20settings=20=E3=81=ABtracker=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/config.py | 6 +++ src-python/model.py | 76 +++++++++++++++++++--------- src-python/models/overlay/overlay.py | 1 + 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src-python/config.py b/src-python/config.py index da663168..36cbfbd5 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -791,6 +791,10 @@ class Config: if isinstance(value, dict) and set(value.keys()) == set(self.OVERLAY_LARGE_LOG_SETTINGS.keys()): for key, value in value.items(): match (key): + case "tracker": + if isinstance(value, str): + # "HMD", "LeftHand", "RightHand" + self._OVERLAY_LARGE_LOG_SETTINGS[key] = value case "x_pos" | "y_pos" | "z_pos" | "x_rotation" | "y_rotation" | "z_rotation": if isinstance(value, (int, float)): self._OVERLAY_LARGE_LOG_SETTINGS[key] = float(value) @@ -1086,6 +1090,7 @@ class Config: "fadeout_duration": 2, "opacity": 1.0, "ui_scaling": 1.0, + "tracker": "HMD", } self._OVERLAY_LARGE_LOG = False self._OVERLAY_LARGE_LOG_SETTINGS = { @@ -1099,6 +1104,7 @@ class Config: "fadeout_duration": 2, "opacity": 1.0, "ui_scaling": 1.0, + "tracker": "LeftHand", } self._SEND_MESSAGE_TO_VRC = True self._SEND_RECEIVED_MESSAGE_TO_VRC = False diff --git a/src-python/model.py b/src-python/model.py index 98a6588a..161ab64a 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -94,6 +94,7 @@ class Model: config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"], ) + self.overlay_small_log_pre_settings = config.OVERLAY_SMALL_LOG_SETTINGS self.overlay_large_log = Overlay( "VRCT_LARGE_LOG", "VRCT_LARGE_LOG", @@ -108,6 +109,7 @@ class Model: config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"], ) + self.overlay_large_log_pre_settings = config.OVERLAY_LARGE_LOG_SETTINGS self.overlay_image = OverlayImage() self.mic_audio_queue = None self.mic_mute_status = None @@ -726,18 +728,31 @@ class Model: self.overlay_small_log.startOverlay() def updateOverlaySmallLogSettings(self): - self.overlay_small_log.updatePosition( - config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], - config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"], - config.OVERLAY_SMALL_LOG_SETTINGS["z_pos"], - config.OVERLAY_SMALL_LOG_SETTINGS["x_rotation"], - config.OVERLAY_SMALL_LOG_SETTINGS["y_rotation"], - config.OVERLAY_SMALL_LOG_SETTINGS["z_rotation"], - ) - self.overlay_small_log.updateDisplayDuration(config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"]) - self.overlay_small_log.updateFadeoutDuration(config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"]) - self.overlay_small_log.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], with_fade=True) - self.overlay_small_log.updateUiScaling(config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"]) + for key in self.overlay_small_log_pre_settings.keys(): + if self.overlay_small_log_pre_settings[key] != config.OVERLAY_SMALL_LOG_SETTINGS[key]: + match (key): + case "x_pos" | "y_pos" | "z_pos" | "x_rotation" | "y_rotation" | "z_rotation" | "tracker": + self.overlay_small_log.updatePosition( + config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], + config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"], + config.OVERLAY_SMALL_LOG_SETTINGS["z_pos"], + config.OVERLAY_SMALL_LOG_SETTINGS["x_rotation"], + config.OVERLAY_SMALL_LOG_SETTINGS["y_rotation"], + config.OVERLAY_SMALL_LOG_SETTINGS["z_rotation"], + config.OVERLAY_SMALL_LOG_SETTINGS["tracker"], + ) + case "display_duration": + self.overlay_small_log.updateDisplayDuration(config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"]) + case "fadeout_duration": + self.overlay_small_log.updateFadeoutDuration(config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"]) + case "opacity": + self.overlay_small_log.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], with_fade=True) + case "ui_scaling": + self.overlay_small_log.updateUiScaling(config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"]) + case _: + pass + break + self.overlay_small_log_pre_settings = config.OVERLAY_SMALL_LOG_SETTINGS def shutdownOverlaySmall(self): self.overlay_small_log.shutdownOverlay() @@ -757,18 +772,31 @@ class Model: self.overlay_large_log.startOverlay() def updateOverlayLargeLogSettings(self): - self.overlay_large_log.updatePosition( - config.OVERLAY_LARGE_LOG_SETTINGS["x_pos"], - config.OVERLAY_LARGE_LOG_SETTINGS["y_pos"], - config.OVERLAY_LARGE_LOG_SETTINGS["z_pos"], - config.OVERLAY_LARGE_LOG_SETTINGS["x_rotation"], - config.OVERLAY_LARGE_LOG_SETTINGS["y_rotation"], - config.OVERLAY_LARGE_LOG_SETTINGS["z_rotation"], - ) - self.overlay_large_log.updateDisplayDuration(config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"]) - self.overlay_large_log.updateFadeoutDuration(config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"]) - self.overlay_large_log.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], with_fade=True) - self.overlay_large_log.updateUiScaling(config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"]) + for key in self.overlay_large_log_pre_settings.keys(): + if self.overlay_large_log_pre_settings[key] != config.OVERLAY_LARGE_LOG_SETTINGS[key]: + match (key): + case "x_pos" | "y_pos" | "z_pos" | "x_rotation" | "y_rotation" | "z_rotation" | "tracker": + self.overlay_large_log.updatePosition( + config.OVERLAY_LARGE_LOG_SETTINGS["x_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["y_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["z_pos"], + config.OVERLAY_LARGE_LOG_SETTINGS["x_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["y_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["z_rotation"], + config.OVERLAY_LARGE_LOG_SETTINGS["tracker"], + ) + case "display_duration": + self.overlay_large_log.updateDisplayDuration(config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"]) + case "fadeout_duration": + self.overlay_large_log.updateFadeoutDuration(config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"]) + case "opacity": + self.overlay_large_log.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], with_fade=True) + case "ui_scaling": + self.overlay_large_log.updateUiScaling(config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"]) + case _: + pass + break + self.overlay_large_log_pre_settings = config.OVERLAY_LARGE_LOG_SETTINGS def shutdownOverlayLarge(self): self.overlay_large_log.shutdownOverlay() diff --git a/src-python/models/overlay/overlay.py b/src-python/models/overlay/overlay.py index c2c9d1a1..f26f8fc4 100644 --- a/src-python/models/overlay/overlay.py +++ b/src-python/models/overlay/overlay.py @@ -109,6 +109,7 @@ class Overlay: self.settings["x_rotation"], self.settings["y_rotation"], self.settings["z_rotation"], + self.settings["tracker"] ) except Exception as e: From d5066cb60d28a5a23e67f8e0ebb90befb05b0650 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sun, 10 Nov 2024 00:57:43 +0900 Subject: [PATCH 05/11] =?UTF-8?q?=F0=9F=90=9B[bugfix]=20Model=20:=20Overla?= =?UTF-8?q?y=E3=81=AE=E8=B5=B7=E5=8B=95=E5=87=A6=E7=90=86=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/models/overlay/overlay_image.py | 27 ++++++++++++++++++---- src-python/webui_controller.py | 21 +++++++++++------ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index df6e303f..e14ccacd 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -58,7 +58,11 @@ class OverlayImage: font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") img = Image.new("RGBA", (base_width, base_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) - font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + try: + font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + except Exception: + font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf"), font_size) + text_width = draw.textlength(text, font) character_width = text_width // len(text) character_line_num = int((base_width) // character_width) - 12 @@ -142,7 +146,10 @@ class OverlayImage: img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) - font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + try: + font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + except Exception: + font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf"), font_size) text_width = draw.textlength(text, font) character_width = text_width // len(text) character_line_num = int(width // character_width) @@ -172,7 +179,10 @@ class OverlayImage: img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) - font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", "NotoSansJP-Regular.ttf"), font_size) + try: + font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", "NotoSansJP-Regular.ttf"), font_size) + except Exception: + font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", "NotoSansJP-Regular.ttf"), font_size) text_height = font_size*2 text_width = draw.textlength(date_time, font) character_width = text_width // len(date_time) @@ -235,4 +245,13 @@ class OverlayImage: draw = ImageDraw.Draw(background) draw.rounded_rectangle([(0, 0), (width, height)], radius=15, fill=background_color, outline=background_outline_color, width=5) img = Image.alpha_composite(background, img) - return img \ No newline at end of file + return img + +if __name__ == "__main__": + overlay = OverlayImage() + img = overlay.createOverlayImageSmall("Hello, World!", "English", "こんにちは、世界!", "Japanese") + img.save("overlay_small.png") + img = overlay.createOverlayImageLarge("send", "Hello, World!", "English", "こんにちは、世界!", "Japanese") + img.save("overlay_large0.png") + img = overlay.createOverlayImageLarge("receive", "こんにちは、世界!", "Japanese", "Hello, World!", "English") + img.save("overlay_large1.png") \ No newline at end of file diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 405617b8..c505a295 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -250,7 +250,7 @@ class Controller: translation = " (" + "/".join(translation) + ")" model.logger.info(f"[SENT] {message}{translation}") - if config.OVERLAY_LARGE_LOG is True: + if config.OVERLAY_LARGE_LOG is True and model.overlay_large_log.initialized is True: overlay_image = model.createOverlayImageLarge("send", message, translation) model.updateOverlayLarge(overlay_image) @@ -289,13 +289,11 @@ class Controller: transliteration = model.convertMessageToTransliteration(message) if config.ENABLE_TRANSCRIPTION_RECEIVE is True: - if config.OVERLAY_SMALL_LOG is True: - if model.overlay_small_log.initialized is True: + if config.OVERLAY_SMALL_LOG is True and model.overlay_small_log.initialized is True: overlay_image = model.createOverlayImageSmall(message, translation) model.updateOverlaySmall(overlay_image) - if config.OVERLAY_LARGE_LOG is True: - if model.overlay_large_log.initialized is True: + if config.OVERLAY_LARGE_LOG is True and model.overlay_large_log.initialized is True: overlay_image = model.createOverlayImageLarge("receive", message, translation) model.updateOverlayLarge(overlay_image) @@ -1364,6 +1362,10 @@ class Controller: def setEnableTranscriptionSend(self, *args, **kwargs) -> dict: self.startThreadingTranscriptionSendMessage() config.ENABLE_TRANSCRIPTION_SEND = True + if (config.OVERLAY_LARGE_LOG is True and + model.overlay_large_log.initialized is False and + model.overlay_large_log.checkSteamvrRunning() is True): + model.startOverlayLarge() return {"status":200, "result":config.ENABLE_TRANSCRIPTION_SEND} def setDisableTranscriptionSend(self, *args, **kwargs) -> dict: @@ -1373,9 +1375,14 @@ class Controller: def setEnableTranscriptionReceive(self, *args, **kwargs) -> dict: self.startThreadingTranscriptionReceiveMessage() - if config.OVERLAY_SMALL_LOG is True: - if model.overlay_small_log.initialized is False and model.overlay_small_log.checkSteamvrRunning() is True: + if (config.OVERLAY_SMALL_LOG is True and + model.overlay_small_log.initialized is False and + model.overlay_small_log.checkSteamvrRunning() is True): model.startOverlaySmall() + if (config.OVERLAY_LARGE_LOG is True and + model.overlay_large_log.initialized is False and + model.overlay_large_log.checkSteamvrRunning() is True): + model.startOverlayLarge() config.ENABLE_TRANSCRIPTION_RECEIVE = True return {"status":200, "result":config.ENABLE_TRANSCRIPTION_RECEIVE} From 109b7e62ba47c915d03125a845cf12fea5a39346 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sun, 10 Nov 2024 03:58:13 +0900 Subject: [PATCH 06/11] =?UTF-8?q?=F0=9F=90=9B[bugfix]=20Model=20:=20Overla?= =?UTF-8?q?y=E3=81=AE=E8=B5=B7=E5=8B=95=E3=81=A7=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=83=96=E3=83=A9=E3=83=AA=E3=81=AE=E5=88=9D=E6=9C=9F=E5=8C=96?= =?UTF-8?q?=E3=82=92=E8=A1=8C=E3=81=86=E3=82=BF=E3=82=A4=E3=83=9F=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=81=A7small=E3=81=A8large=E3=81=AE2=E3=81=A4?= =?UTF-8?q?=E3=81=AE=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB=E3=82=92=E7=94=9F?= =?UTF-8?q?=E6=88=90=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/model.py | 76 ++++-------- src-python/models/overlay/overlay.py | 170 ++++++++++++++------------- src-python/webui_controller.py | 32 ++--- 3 files changed, 128 insertions(+), 150 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index 161ab64a..e66f3048 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -80,35 +80,11 @@ class Model: self.previous_receive_message = "" self.translator = Translator() self.keyword_processor = KeywordProcessor() - self.overlay_small_log = Overlay( - "VRCT_SMALL_LOG", - "VRCT_SMALL_LOG", - config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], - config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"], - config.OVERLAY_SMALL_LOG_SETTINGS["z_pos"], - config.OVERLAY_SMALL_LOG_SETTINGS["x_rotation"], - config.OVERLAY_SMALL_LOG_SETTINGS["y_rotation"], - config.OVERLAY_SMALL_LOG_SETTINGS["z_rotation"], - config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"], - config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"], - config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], - config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"], + self.overlay = Overlay( + config.OVERLAY_SMALL_LOG_SETTINGS, + config.OVERLAY_LARGE_LOG_SETTINGS ) self.overlay_small_log_pre_settings = config.OVERLAY_SMALL_LOG_SETTINGS - self.overlay_large_log = Overlay( - "VRCT_LARGE_LOG", - "VRCT_LARGE_LOG", - config.OVERLAY_LARGE_LOG_SETTINGS["x_pos"], - config.OVERLAY_LARGE_LOG_SETTINGS["y_pos"], - config.OVERLAY_LARGE_LOG_SETTINGS["z_pos"], - config.OVERLAY_LARGE_LOG_SETTINGS["x_rotation"], - config.OVERLAY_LARGE_LOG_SETTINGS["y_rotation"], - config.OVERLAY_LARGE_LOG_SETTINGS["z_rotation"], - config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"], - config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"], - config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], - config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"], - ) self.overlay_large_log_pre_settings = config.OVERLAY_LARGE_LOG_SETTINGS self.overlay_image = OverlayImage() self.mic_audio_queue = None @@ -719,20 +695,17 @@ class Model: return self.overlay_image.createOverlayImageSmall(message, language) def clearOverlayImageSmall(self): - self.overlay_small_log.clearImage() + self.overlay.clearImage("small") def updateOverlaySmall(self, img): - self.overlay_small_log.updateImage(img) - - def startOverlaySmall(self): - self.overlay_small_log.startOverlay() + self.overlay.updateImage(img, "small") def updateOverlaySmallLogSettings(self): for key in self.overlay_small_log_pre_settings.keys(): if self.overlay_small_log_pre_settings[key] != config.OVERLAY_SMALL_LOG_SETTINGS[key]: match (key): case "x_pos" | "y_pos" | "z_pos" | "x_rotation" | "y_rotation" | "z_rotation" | "tracker": - self.overlay_small_log.updatePosition( + self.overlay.updatePosition( config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["z_pos"], @@ -740,43 +713,38 @@ class Model: config.OVERLAY_SMALL_LOG_SETTINGS["y_rotation"], config.OVERLAY_SMALL_LOG_SETTINGS["z_rotation"], config.OVERLAY_SMALL_LOG_SETTINGS["tracker"], + "small", ) case "display_duration": - self.overlay_small_log.updateDisplayDuration(config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"]) + self.overlay.updateDisplayDuration(config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"], "small") case "fadeout_duration": - self.overlay_small_log.updateFadeoutDuration(config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"]) + self.overlay.updateFadeoutDuration(config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"], "small") case "opacity": - self.overlay_small_log.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], with_fade=True) + self.overlay.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], True, "small") case "ui_scaling": - self.overlay_small_log.updateUiScaling(config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"]) + self.overlay.updateUiScaling(config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"], "small") case _: pass break self.overlay_small_log_pre_settings = config.OVERLAY_SMALL_LOG_SETTINGS - def shutdownOverlaySmall(self): - self.overlay_small_log.shutdownOverlay() - def createOverlayImageLarge(self, message_type:str, message:str, translation:str): your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] return self.overlay_image.createOverlayImageLarge(message_type, message, your_language, translation, target_language) def clearOverlayImageLarge(self): - self.overlay_large_log.clearImage() + self.overlay.clearImage("large") def updateOverlayLarge(self, img): - self.overlay_large_log.updateImage(img) - - def startOverlayLarge(self): - self.overlay_large_log.startOverlay() + self.overlay.updateImage(img, "large") def updateOverlayLargeLogSettings(self): for key in self.overlay_large_log_pre_settings.keys(): if self.overlay_large_log_pre_settings[key] != config.OVERLAY_LARGE_LOG_SETTINGS[key]: match (key): case "x_pos" | "y_pos" | "z_pos" | "x_rotation" | "y_rotation" | "z_rotation" | "tracker": - self.overlay_large_log.updatePosition( + self.overlay.updatePosition( config.OVERLAY_LARGE_LOG_SETTINGS["x_pos"], config.OVERLAY_LARGE_LOG_SETTINGS["y_pos"], config.OVERLAY_LARGE_LOG_SETTINGS["z_pos"], @@ -784,22 +752,26 @@ class Model: config.OVERLAY_LARGE_LOG_SETTINGS["y_rotation"], config.OVERLAY_LARGE_LOG_SETTINGS["z_rotation"], config.OVERLAY_LARGE_LOG_SETTINGS["tracker"], + "large", ) case "display_duration": - self.overlay_large_log.updateDisplayDuration(config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"]) + self.overlay.updateDisplayDuration(config.OVERLAY_LARGE_LOG_SETTINGS["display_duration"], "large") case "fadeout_duration": - self.overlay_large_log.updateFadeoutDuration(config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"]) + self.overlay.updateFadeoutDuration(config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"], "large") case "opacity": - self.overlay_large_log.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], with_fade=True) + self.overlay.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], True, "large") case "ui_scaling": - self.overlay_large_log.updateUiScaling(config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"]) + self.overlay.updateUiScaling(config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"], "large") case _: pass break self.overlay_large_log_pre_settings = config.OVERLAY_LARGE_LOG_SETTINGS - def shutdownOverlayLarge(self): - self.overlay_large_log.shutdownOverlay() + def startOverlay(self): + self.overlay.startOverlay() + + def shutdownOverlay(self): + self.overlay.shutdownOverlay() def startWatchdog(self): self.th_watchdog = threadFnc(self.watchdog.start) diff --git a/src-python/models/overlay/overlay.py b/src-python/models/overlay/overlay.py index f26f8fc4..bd00eb9a 100644 --- a/src-python/models/overlay/overlay.py +++ b/src-python/models/overlay/overlay.py @@ -63,117 +63,119 @@ def getRightHandBaseMatrix(): return arr class Overlay: - def __init__(self, key, name, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, display_duration, fadeout_duration, opacity, ui_scaling): - self.initialized = False - self.key = key - self.name = name - settings = { - "color": [1, 1, 1], - "opacity": opacity, - "x_pos": x_pos, - "y_pos": y_pos, - "z_pos": z_pos, - "x_rotation": x_rotation, - "y_rotation": y_rotation, - "z_rotation": z_rotation, - "display_duration": display_duration, - "fadeout_duration": fadeout_duration, - "ui_scaling": ui_scaling, - } - self.settings = settings + def __init__(self, small_settings, large_settings): self.system = None self.overlay = None self.handle = None - self.lastUpdate = time.monotonic() - self.thread_overlay = None - self.fadeRatio = 1 + self.initialized = False self.loop = True + self.thread_overlay = None + + self.settings = {"small": small_settings, "large": large_settings} + self.lastUpdate = {"small": time.monotonic(), "large": time.monotonic()} + self.fadeRatio = {"small": 1, "large": 1} def init(self): try: self.system = openvr.init(openvr.VRApplication_Background) self.overlay = openvr.IVROverlay() self.overlay_system = openvr.IVRSystem() - self.handle = self.overlay.createOverlay(self.key, self.name) - self.overlay.showOverlay(self.handle) + self.handle_small = self.overlay.createOverlay("VRCT_SMALL_LOG", "VRCT_SMALL_LOG") + self.handle_large = self.overlay.createOverlay("VRCT_LARGE_LOG", "VRCT_LARGE_LOG") + self.overlay.showOverlay(self.handle_small) + self.overlay.showOverlay(self.handle_large) self.initialized = True - self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0))) - self.updateColor(self.settings["color"]) - self.updateOpacity(self.settings["opacity"]) - self.updateUiScaling(self.settings["ui_scaling"]) + self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), "small") + self.updateColor([1, 1, 1], "small") + self.updateOpacity(self.settings["small"]["opacity"], "small") + self.updateUiScaling(self.settings["small"]["ui_scaling"], "small") self.updatePosition( - self.settings["x_pos"], - self.settings["y_pos"], - self.settings["z_pos"], - self.settings["x_rotation"], - self.settings["y_rotation"], - self.settings["z_rotation"], - self.settings["tracker"] + self.settings["small"]["x_pos"], + self.settings["small"]["y_pos"], + self.settings["small"]["z_pos"], + self.settings["small"]["x_rotation"], + self.settings["small"]["y_rotation"], + self.settings["small"]["z_rotation"], + self.settings["small"]["tracker"] + ) + + self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), "large") + self.updateColor([1, 1, 1], "large") + self.updateOpacity(self.settings["large"]["opacity"], "large") + self.updateUiScaling(self.settings["large"]["ui_scaling"], "large") + self.updatePosition( + self.settings["large"]["x_pos"], + self.settings["large"]["y_pos"], + self.settings["large"]["z_pos"], + self.settings["large"]["x_rotation"], + self.settings["large"]["y_rotation"], + self.settings["large"]["z_rotation"], + self.settings["large"]["tracker"] ) except Exception as e: printLog("error:Could not initialise OpenVR", e) - def updateImage(self, img): + def updateImage(self, img, size: str = "small"): if self.initialized is True: width, height = img.size img = img.tobytes() img = (ctypes.c_char * len(img)).from_buffer_copy(img) try: - self.overlay.setOverlayRaw(self.handle, img, width, height, 4) + self.overlay.setOverlayRaw(self.handle_small[size], img, width, height, 4) except Exception as e: printLog("error:Could not update image", e) self.initialized = False self.reStartOverlay() while self.initialized is False: time.sleep(0.1) - self.overlay.setOverlayRaw(self.handle, img, width, height, 4) - self.updateOpacity(self.settings["opacity"]) - self.lastUpdate = time.monotonic() + self.overlay.setOverlayRaw(self.handle_small[size], img, width, height, 4) + self.updateOpacity(self.settings[size]["opacity"], "small") + self.lastUpdate[size] = time.monotonic() - def clearImage(self): + def clearImage(self, size: str = "small"): if self.initialized is True: - self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0))) + self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), size) - def updateColor(self, col): + def updateColor(self, col, size: str = "small"): """ col is a 3-tuple representing (r, g, b) """ - self.settings["color"] = col if self.initialized is True: - r, g, b = self.settings["color"] - self.overlay.setOverlayColor(self.handle, r, g, b) + r, g, b = col + self.overlay.setOverlayColor(self.handle[size], r, g, b) - def updateOpacity(self, opacity, with_fade=False): - self.settings["opacity"] = opacity + def updateOpacity(self, opacity, with_fade=False, size: str = "small"): + self.settings[size]["opacity"] = opacity if self.initialized is True: if with_fade is True: - if self.fadeRatio > 0: - self.overlay.setOverlayAlpha(self.handle, self.fadeRatio * self.settings["opacity"]) + if self.fadeRatio[size] > 0: + self.overlay.setOverlayAlpha(self.handle[size], self.fadeRatio[size] * self.settings[size]["opacity"]) else: - self.overlay.setOverlayAlpha(self.handle, self.settings["opacity"]) + self.overlay.setOverlayAlpha(self.handle[size], self.settings[size]["opacity"]) - def updateUiScaling(self, ui_scaling): - self.settings["ui_scaling"] = ui_scaling + def updateUiScaling(self, ui_scaling, size: str = "small"): + self.settings[size]["ui_scaling"] = ui_scaling if self.initialized is True: - self.overlay.setOverlayWidthInMeters(self.handle, self.settings["ui_scaling"]) + self.overlay.setOverlayWidthInMeters(self.handle[size], self.settings[size]["ui_scaling"]) - def updatePosition(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, tracker="HMD"): + def updatePosition(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, tracker: str="HMD", size: str = "small"): """ x_pos, y_pos, z_pos are floats representing the position of overlay x_rotation, y_rotation, z_rotation are floats representing the rotation of overlay tracker is a string representing the tracker to use ("HMD", "LeftHand", "RightHand") """ - self.settings["x_pos"] = x_pos - self.settings["y_pos"] = y_pos - self.settings["z_pos"] = z_pos - self.settings["x_rotation"] = x_rotation - self.settings["y_rotation"] = y_rotation - self.settings["z_rotation"] = z_rotation + self.settings[size]["x_pos"] = x_pos + self.settings[size]["y_pos"] = y_pos + self.settings[size]["z_pos"] = z_pos + self.settings[size]["x_rotation"] = x_rotation + self.settings[size]["y_rotation"] = y_rotation + self.settings[size]["z_rotation"] = z_rotation + self.settings[size]["tracker"] = tracker match tracker: case "HMD": @@ -189,23 +191,23 @@ class Overlay: base_matrix = getHMDBaseMatrix() trackerIndex = openvr.k_unTrackedDeviceIndex_Hmd - translation = (self.settings["x_pos"], self.settings["y_pos"], - self.settings["z_pos"]) - rotation = (self.settings["x_rotation"], self.settings["y_rotation"], self.settings["z_rotation"]) + translation = (self.settings[size]["x_pos"], self.settings[size]["y_pos"], - self.settings[size]["z_pos"]) + rotation = (self.settings[size]["x_rotation"], self.settings[size]["y_rotation"], self.settings[size]["z_rotation"]) transform = utils.transform_matrix(base_matrix, translation, rotation) - self.transform = mat34Id(transform) + transform = mat34Id(transform) if self.initialized is True: self.overlay.setOverlayTransformTrackedDeviceRelative( - self.handle, + self.handle[size], trackerIndex, - self.transform + transform ) - def updateDisplayDuration(self, display_duration): - self.settings["display_duration"] = display_duration + def updateDisplayDuration(self, display_duration, size: str = "small"): + self.settings[size]["display_duration"] = display_duration - def updateFadeoutDuration(self, fadeout_duration): - self.settings["fadeout_duration"] = fadeout_duration + def updateFadeoutDuration(self, fadeout_duration, size: str = "small"): + self.settings[size]["fadeout_duration"] = fadeout_duration def checkActive(self): try: @@ -219,26 +221,27 @@ class Overlay: printLog("error:Could not check SteamVR running", e) return False - def evaluateOpacityFade(self, lastUpdate, currentTime): - if (currentTime - lastUpdate) > self.settings["display_duration"]: - timeThroughInterval = currentTime - lastUpdate - self.settings["display_duration"] - self.fadeRatio = 1 - timeThroughInterval / self.settings["fadeout_duration"] - if self.fadeRatio < 0: - self.fadeRatio = 0 - self.overlay.setOverlayAlpha(self.handle, self.fadeRatio * self.settings["opacity"]) + def evaluateOpacityFade(self, lastUpdate, currentTime, size: str = "small"): + if (currentTime - lastUpdate) > self.settings[size]["display_duration"]: + timeThroughInterval = currentTime - lastUpdate - self.settings[size]["display_duration"] + self.fadeRatio[size] = 1 - timeThroughInterval / self.settings[size]["fadeout_duration"] + if self.fadeRatio[size] < 0: + self.fadeRatio[size] = 0 + self.overlay.setOverlayAlpha(self.handle[size], self.fadeRatio[size] * self.settings[size]["opacity"]) - def update(self): + def update(self, size: str = "small"): currTime = time.monotonic() - if self.settings["fadeout_duration"] != 0: - self.evaluateOpacityFade(self.lastUpdate, currTime) + if self.settings[size]["fadeout_duration"] != 0: + self.evaluateOpacityFade(self.lastUpdate[size], currTime, size) else: - self.updateOpacity(self.settings["opacity"]) + self.updateOpacity(self.settings[size]["opacity"], size) def mainloop(self): self.loop = True while self.checkActive() is True and self.loop is True: startTime = time.monotonic() - self.update() + self.update("small") + self.update("large") sleepTime = (1 / 16) - (time.monotonic() - startTime) if sleepTime > 0: time.sleep(sleepTime) @@ -258,8 +261,9 @@ class Overlay: self.loop = False self.thread_overlay.join() self.thread_overlay = None - if isinstance(self.overlay, openvr.IVROverlay) and isinstance(self.handle, int): - self.overlay.destroyOverlay(self.handle) + if isinstance(self.overlay, openvr.IVROverlay) and isinstance(self.handle["small"], int) and isinstance(self.handle["large"], int): + self.overlay.destroyOverlay(self.handle["small"]) + self.overlay.destroyOverlay(self.handle["large"]) self.overlay = None if isinstance(self.system, openvr.IVRSystem): openvr.shutdown() diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index c505a295..fa6cbd96 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -1175,8 +1175,8 @@ class Controller: @staticmethod def setEnableOverlaySmallLog(*args, **kwargs) -> dict: config.OVERLAY_SMALL_LOG = True - if model.overlay_small_log.initialized is False and model.overlay_small_log.checkSteamvrRunning() is True: - model.startOverlaySmall() + if model.overlay.initialized is False and model.overlay.checkSteamvrRunning() is True: + model.startOverlay() return {"status":200, "result":config.OVERLAY_SMALL_LOG} @staticmethod @@ -1184,7 +1184,8 @@ class Controller: config.OVERLAY_SMALL_LOG = False if config.OVERLAY_SMALL_LOG is False: model.clearOverlayImageSmall() - model.shutdownOverlaySmall() + if config.OVERLAY_LARGE_LOG is False: + model.shutdownOverlay() return {"status":200, "result":config.OVERLAY_SMALL_LOG} @staticmethod @@ -1204,8 +1205,8 @@ class Controller: @staticmethod def setEnableOverlayLargeLog(*args, **kwargs) -> dict: config.OVERLAY_LARGE_LOG = True - if model.overlay_large_log.initialized is False and model.overlay_large_log.checkSteamvrRunning() is True: - model.startOverlayLarge() + if model.overlay.initialized is False and model.overlay.checkSteamvrRunning() is True: + model.startOverlay() return {"status":200, "result":config.OVERLAY_LARGE_LOG} @staticmethod @@ -1213,7 +1214,8 @@ class Controller: config.OVERLAY_LARGE_LOG = False if config.OVERLAY_LARGE_LOG is False: model.clearOverlayImageLarge() - model.shutdownOverlayLarge() + if config.OVERLAY_SMALL_LOG is False: + model.shutdownOverlay() return {"status":200, "result":config.OVERLAY_LARGE_LOG} @staticmethod @@ -1363,9 +1365,9 @@ class Controller: self.startThreadingTranscriptionSendMessage() config.ENABLE_TRANSCRIPTION_SEND = True if (config.OVERLAY_LARGE_LOG is True and - model.overlay_large_log.initialized is False and - model.overlay_large_log.checkSteamvrRunning() is True): - model.startOverlayLarge() + model.overlay.initialized is False and + model.overlay.checkSteamvrRunning() is True): + model.startOverlay() return {"status":200, "result":config.ENABLE_TRANSCRIPTION_SEND} def setDisableTranscriptionSend(self, *args, **kwargs) -> dict: @@ -1376,13 +1378,13 @@ class Controller: def setEnableTranscriptionReceive(self, *args, **kwargs) -> dict: self.startThreadingTranscriptionReceiveMessage() if (config.OVERLAY_SMALL_LOG is True and - model.overlay_small_log.initialized is False and - model.overlay_small_log.checkSteamvrRunning() is True): - model.startOverlaySmall() + model.overlay.initialized is False and + model.overlay.checkSteamvrRunning() is True): + model.startOverlay() if (config.OVERLAY_LARGE_LOG is True and - model.overlay_large_log.initialized is False and - model.overlay_large_log.checkSteamvrRunning() is True): - model.startOverlayLarge() + model.overlay.initialized is False and + model.overlay.checkSteamvrRunning() is True): + model.startOverlay() config.ENABLE_TRANSCRIPTION_RECEIVE = True return {"status":200, "result":config.ENABLE_TRANSCRIPTION_RECEIVE} From 2df88bf698444b2810c6c174d518cc84837e46b0 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:55:06 +0900 Subject: [PATCH 07/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay()=20class=E3=82=92size=E3=81=AB=E4=BE=9D=E5=AD=98?= =?UTF-8?q?=E3=81=97=E3=81=AA=E3=81=84=E8=A8=AD=E8=A8=88=E3=81=AB=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/config.py | 2 +- src-python/model.py | 28 ++--- src-python/models/overlay/overlay.py | 129 ++++++++------------- src-python/models/overlay/overlay_image.py | 50 ++++---- src-python/webui_controller.py | 34 +++--- 5 files changed, 103 insertions(+), 140 deletions(-) diff --git a/src-python/config.py b/src-python/config.py index 36cbfbd5..02ebdd43 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -1103,7 +1103,7 @@ class Config: "display_duration": 5, "fadeout_duration": 2, "opacity": 1.0, - "ui_scaling": 1.0, + "ui_scaling": 0.5, "tracker": "LeftHand", } self._SEND_MESSAGE_TO_VRC = True diff --git a/src-python/model.py b/src-python/model.py index e66f3048..622979b8 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -81,8 +81,10 @@ class Model: self.translator = Translator() self.keyword_processor = KeywordProcessor() self.overlay = Overlay( - config.OVERLAY_SMALL_LOG_SETTINGS, - config.OVERLAY_LARGE_LOG_SETTINGS + { + "small": config.OVERLAY_SMALL_LOG_SETTINGS, + "large": config.OVERLAY_LARGE_LOG_SETTINGS, + } ) self.overlay_small_log_pre_settings = config.OVERLAY_SMALL_LOG_SETTINGS self.overlay_large_log_pre_settings = config.OVERLAY_LARGE_LOG_SETTINGS @@ -677,10 +679,10 @@ class Model: self.speaker_energy_recorder.stop() self.speaker_energy_recorder = None - def createOverlayImageSmall(self, message, translation): + def createOverlayImageSmallLog(self, message, translation): your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] - return self.overlay_image.createOverlayImageSmall(message, your_language, translation, target_language) + return self.overlay_image.createOverlayImageSmallLog(message, your_language, translation, target_language) def createOverlayImageSystemMessage(self, message): ui_language = config.UI_LANGUAGE @@ -692,12 +694,12 @@ class Model: "zh-Hant":"Chinese Traditional", } language = convert_languages.get(ui_language, "Japanese") - return self.overlay_image.createOverlayImageSmall(message, language) + return self.overlay_image.createOverlayImageSmallLog(message, language) - def clearOverlayImageSmall(self): + def clearOverlayImageSmallLog(self): self.overlay.clearImage("small") - def updateOverlaySmall(self, img): + def updateOverlaySmallLog(self, img): self.overlay.updateImage(img, "small") def updateOverlaySmallLogSettings(self): @@ -720,7 +722,7 @@ class Model: case "fadeout_duration": self.overlay.updateFadeoutDuration(config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"], "small") case "opacity": - self.overlay.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], True, "small") + self.overlay.updateOpacity(config.OVERLAY_SMALL_LOG_SETTINGS["opacity"], "small", True) case "ui_scaling": self.overlay.updateUiScaling(config.OVERLAY_SMALL_LOG_SETTINGS["ui_scaling"], "small") case _: @@ -728,15 +730,15 @@ class Model: break self.overlay_small_log_pre_settings = config.OVERLAY_SMALL_LOG_SETTINGS - def createOverlayImageLarge(self, message_type:str, message:str, translation:str): + def createOverlayImageLargeLog(self, message_type:str, message:str, translation:str): your_language = config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] target_language = config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO]["primary"]["language"] - return self.overlay_image.createOverlayImageLarge(message_type, message, your_language, translation, target_language) + return self.overlay_image.createOverlayImageLargeLog(message_type, message, your_language, translation, target_language) - def clearOverlayImageLarge(self): + def clearOverlayImageLargeLog(self): self.overlay.clearImage("large") - def updateOverlayLarge(self, img): + def updateOverlayLargeLog(self, img): self.overlay.updateImage(img, "large") def updateOverlayLargeLogSettings(self): @@ -759,7 +761,7 @@ class Model: case "fadeout_duration": self.overlay.updateFadeoutDuration(config.OVERLAY_LARGE_LOG_SETTINGS["fadeout_duration"], "large") case "opacity": - self.overlay.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], True, "large") + self.overlay.updateOpacity(config.OVERLAY_LARGE_LOG_SETTINGS["opacity"], "large", True) case "ui_scaling": self.overlay.updateUiScaling(config.OVERLAY_LARGE_LOG_SETTINGS["ui_scaling"], "large") case _: diff --git a/src-python/models/overlay/overlay.py b/src-python/models/overlay/overlay.py index bd00eb9a..55cfca65 100644 --- a/src-python/models/overlay/overlay.py +++ b/src-python/models/overlay/overlay.py @@ -63,7 +63,7 @@ def getRightHandBaseMatrix(): return arr class Overlay: - def __init__(self, small_settings, large_settings): + def __init__(self, settings_dict): self.system = None self.overlay = None self.handle = None @@ -71,75 +71,66 @@ class Overlay: self.loop = True self.thread_overlay = None - self.settings = {"small": small_settings, "large": large_settings} - self.lastUpdate = {"small": time.monotonic(), "large": time.monotonic()} - self.fadeRatio = {"small": 1, "large": 1} + self.settings = {} + self.lastUpdate = {} + self.fadeRatio = {} + for key, value in settings_dict.items(): + self.settings[key] = value + self.lastUpdate[key] = time.monotonic() + self.fadeRatio[key] = 1 def init(self): try: self.system = openvr.init(openvr.VRApplication_Background) self.overlay = openvr.IVROverlay() self.overlay_system = openvr.IVRSystem() - self.handle_small = self.overlay.createOverlay("VRCT_SMALL_LOG", "VRCT_SMALL_LOG") - self.handle_large = self.overlay.createOverlay("VRCT_LARGE_LOG", "VRCT_LARGE_LOG") - self.overlay.showOverlay(self.handle_small) - self.overlay.showOverlay(self.handle_large) + self.handle = {} + for i, size in enumerate(self.settings.keys()): + self.handle[size] = self.overlay.createOverlay(f"VRCT{i}", f"VRCT{i}") + self.overlay.showOverlay(self.handle[size]) self.initialized = True - self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), "small") - self.updateColor([1, 1, 1], "small") - self.updateOpacity(self.settings["small"]["opacity"], "small") - self.updateUiScaling(self.settings["small"]["ui_scaling"], "small") - self.updatePosition( - self.settings["small"]["x_pos"], - self.settings["small"]["y_pos"], - self.settings["small"]["z_pos"], - self.settings["small"]["x_rotation"], - self.settings["small"]["y_rotation"], - self.settings["small"]["z_rotation"], - self.settings["small"]["tracker"] - ) - - self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), "large") - self.updateColor([1, 1, 1], "large") - self.updateOpacity(self.settings["large"]["opacity"], "large") - self.updateUiScaling(self.settings["large"]["ui_scaling"], "large") - self.updatePosition( - self.settings["large"]["x_pos"], - self.settings["large"]["y_pos"], - self.settings["large"]["z_pos"], - self.settings["large"]["x_rotation"], - self.settings["large"]["y_rotation"], - self.settings["large"]["z_rotation"], - self.settings["large"]["tracker"] - ) + for size in self.settings.keys(): + self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), size) + self.updateColor([1, 1, 1], size) + self.updateOpacity(self.settings[size]["opacity"], size, True) + self.updateUiScaling(self.settings[size]["ui_scaling"], size, True) + self.updatePosition( + self.settings[size]["x_pos"], + self.settings[size]["y_pos"], + self.settings[size]["z_pos"], + self.settings[size]["x_rotation"], + self.settings[size]["y_rotation"], + self.settings[size]["z_rotation"], + self.settings[size]["tracker"] + ) except Exception as e: printLog("error:Could not initialise OpenVR", e) - def updateImage(self, img, size: str = "small"): + def updateImage(self, img, size): if self.initialized is True: width, height = img.size img = img.tobytes() img = (ctypes.c_char * len(img)).from_buffer_copy(img) try: - self.overlay.setOverlayRaw(self.handle_small[size], img, width, height, 4) + self.overlay.setOverlayRaw(self.handle[size], img, width, height, 4) except Exception as e: printLog("error:Could not update image", e) self.initialized = False self.reStartOverlay() while self.initialized is False: time.sleep(0.1) - self.overlay.setOverlayRaw(self.handle_small[size], img, width, height, 4) - self.updateOpacity(self.settings[size]["opacity"], "small") + self.overlay.setOverlayRaw(self.handle[size], img, width, height, 4) + self.updateOpacity(self.settings[size]["opacity"], size, True) self.lastUpdate[size] = time.monotonic() - def clearImage(self, size: str = "small"): + def clearImage(self, size): if self.initialized is True: self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), size) - def updateColor(self, col, size: str = "small"): + def updateColor(self, col, size): """ col is a 3-tuple representing (r, g, b) """ @@ -147,7 +138,7 @@ class Overlay: r, g, b = col self.overlay.setOverlayColor(self.handle[size], r, g, b) - def updateOpacity(self, opacity, with_fade=False, size: str = "small"): + def updateOpacity(self, opacity, size, with_fade=True): self.settings[size]["opacity"] = opacity if self.initialized is True: @@ -157,12 +148,12 @@ class Overlay: else: self.overlay.setOverlayAlpha(self.handle[size], self.settings[size]["opacity"]) - def updateUiScaling(self, ui_scaling, size: str = "small"): + def updateUiScaling(self, ui_scaling, size): self.settings[size]["ui_scaling"] = ui_scaling if self.initialized is True: self.overlay.setOverlayWidthInMeters(self.handle[size], self.settings[size]["ui_scaling"]) - def updatePosition(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, tracker: str="HMD", size: str = "small"): + def updatePosition(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, tracker, size): """ x_pos, y_pos, z_pos are floats representing the position of overlay x_rotation, y_rotation, z_rotation are floats representing the rotation of overlay @@ -203,10 +194,10 @@ class Overlay: transform ) - def updateDisplayDuration(self, display_duration, size: str = "small"): + def updateDisplayDuration(self, display_duration, size): self.settings[size]["display_duration"] = display_duration - def updateFadeoutDuration(self, fadeout_duration, size: str = "small"): + def updateFadeoutDuration(self, fadeout_duration, size): self.settings[size]["fadeout_duration"] = fadeout_duration def checkActive(self): @@ -221,7 +212,7 @@ class Overlay: printLog("error:Could not check SteamVR running", e) return False - def evaluateOpacityFade(self, lastUpdate, currentTime, size: str = "small"): + def evaluateOpacityFade(self, lastUpdate, currentTime, size): if (currentTime - lastUpdate) > self.settings[size]["display_duration"]: timeThroughInterval = currentTime - lastUpdate - self.settings[size]["display_duration"] self.fadeRatio[size] = 1 - timeThroughInterval / self.settings[size]["fadeout_duration"] @@ -229,19 +220,19 @@ class Overlay: self.fadeRatio[size] = 0 self.overlay.setOverlayAlpha(self.handle[size], self.fadeRatio[size] * self.settings[size]["opacity"]) - def update(self, size: str = "small"): + def update(self, size): currTime = time.monotonic() if self.settings[size]["fadeout_duration"] != 0: self.evaluateOpacityFade(self.lastUpdate[size], currTime, size) else: - self.updateOpacity(self.settings[size]["opacity"], size) + self.updateOpacity(self.settings[size]["opacity"], size, True) def mainloop(self): self.loop = True while self.checkActive() is True and self.loop is True: startTime = time.monotonic() - self.update("small") - self.update("large") + for size in self.settings.keys(): + self.update(size) sleepTime = (1 / 16) - (time.monotonic() - startTime) if sleepTime > 0: time.sleep(sleepTime) @@ -261,9 +252,10 @@ class Overlay: self.loop = False self.thread_overlay.join() self.thread_overlay = None - if isinstance(self.overlay, openvr.IVROverlay) and isinstance(self.handle["small"], int) and isinstance(self.handle["large"], int): - self.overlay.destroyOverlay(self.handle["small"]) - self.overlay.destroyOverlay(self.handle["large"]) + if isinstance(self.overlay, openvr.IVROverlay): + for size in self.settings.keys(): + if isinstance(self.handle[size], int): + self.overlay.destroyOverlay(self.handle[size]) self.overlay = None if isinstance(self.system, openvr.IVRSystem): openvr.shutdown() @@ -280,35 +272,6 @@ class Overlay: return _proc_name in (p.name() for p in process_iter()) if __name__ == "__main__": - # from overlay_image import OverlayImage - # overlay_image = OverlayImage() - - # overlay = Overlay(0, 0, 1, 1, 0, 1, 1) - # overlay.startOverlay() - # time.sleep(1) - - # # Example usage - # img = overlay_image.createOverlayImageSmall("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") - # overlay.updateImage(img) - # time.sleep(100000) - - # for i in range(100): - # print(i) - # overlay = Overlay(0, 0, 1, 1, 1, 1, 1) - # overlay.startOverlay() - # time.sleep(1) - - # # Example usage - # img = overlay_image.createOverlayImageSmall("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese", ui_type="sakura") - # overlay.updateImage(img) - # time.sleep(0.5) - - # img = overlay_image.createOverlayImageSmall("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") - # overlay.updateImage(img) - # time.sleep(0.5) - - # overlay.shutdownOverlay() - x_pos = 0 y_pos = 0 z_pos = 0 diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index e14ccacd..2f650e44 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -31,7 +31,7 @@ class OverlayImage: return result @staticmethod - def getUiSizeSmall(): + def getUiSizeSmallLog(): return { "width": int(960*4), "height": int(23*4), @@ -39,7 +39,7 @@ class OverlayImage: } @staticmethod - def getUiColorSmall(ui_type): + def getUiColorSmallLog(ui_type): background_color = (41, 42, 45) background_outline_color = (41, 42, 45) text_color = (223, 223, 223) @@ -54,7 +54,7 @@ class OverlayImage: "text_color": text_color } - def createTextboxSmall(self, text, language, text_color, base_width, base_height, font_size): + def createTextboxSmallLog(self, text, language, text_color, base_width, base_height, font_size): font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") img = Image.new("RGBA", (base_width, base_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) @@ -77,20 +77,20 @@ class OverlayImage: draw.text((text_x, text_y), text, text_color, anchor="mm", stroke_width=0, font=font, align="center") return img - def createOverlayImageSmall(self, message, your_language, translation="", target_language=None, ui_type="default"): - ui_size = self.getUiSizeSmall() + def createOverlayImageSmallLog(self, message, your_language, translation="", target_language=None, ui_type="default"): + ui_size = self.getUiSizeSmallLog() height = ui_size["height"] width = ui_size["width"] font_size = ui_size["font_size"] - ui_colors = self.getUiColorSmall(ui_type) + ui_colors = self.getUiColorSmallLog(ui_type) text_color = ui_colors["text_color"] background_color = ui_colors["background_color"] background_outline_color = ui_colors["background_outline_color"] - img = self.createTextboxSmall(message, your_language, text_color, width, height, font_size) + img = self.createTextboxSmallLog(message, your_language, text_color, width, height, font_size) if len(translation) > 0 and target_language is not None: - translation_img = self.createTextboxSmall(translation, target_language, text_color, width, height, font_size) + translation_img = self.createTextboxSmallLog(translation, target_language, text_color, width, height, font_size) img = self.concatenateImagesVertically(img, translation_img) background = Image.new("RGBA", img.size, (0, 0, 0, 0)) @@ -101,7 +101,7 @@ class OverlayImage: return img @staticmethod - def getUiSizeLarge(): + def getUiSizeLargeLog(): return { "width": int(960*2), "font_size_large": int(23*4), @@ -109,7 +109,7 @@ class OverlayImage: } @staticmethod - def getUiColorLarge(): + def getUiColorLargeLog(): background_color = (41, 42, 45) background_outline_color = (41, 42, 45) text_color_large = (223, 223, 223) @@ -127,13 +127,13 @@ class OverlayImage: "text_color_time": text_color_time } - def createTextImageLarge(self, message_type, size, text, language): - ui_size = self.getUiSizeLarge() + def createTextImageLargeLog(self, message_type, size, text, language): + ui_size = self.getUiSizeLargeLog() font_size_large = ui_size["font_size_large"] font_size_small = ui_size["font_size_small"] width = ui_size["width"] - ui_color = self.getUiColorLarge() + ui_color = self.getUiColorLargeLog() text_color_large = ui_color["text_color_large"] text_color_small = ui_color["text_color_small"] @@ -164,11 +164,11 @@ class OverlayImage: return img def createTextImageMessageType(self, message_type, date_time): - ui_size = self.getUiSizeLarge() + ui_size = self.getUiSizeLargeLog() width = ui_size["width"] font_size = ui_size["font_size_small"] - ui_color = self.getUiColorLarge() + ui_color = self.getUiColorLargeLog() text_color_send = ui_color["text_color_send"] text_color_receive = ui_color["text_color_receive"] text_color_time = ui_color["text_color_time"] @@ -195,18 +195,18 @@ class OverlayImage: draw.text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font) return img - def createTextboxLarge(self, message_type, message, your_language, translation, target_language, date_time): + def createTextboxLargeLog(self, message_type, message, your_language, translation, target_language, date_time): message_type_img = self.createTextImageMessageType(message_type, date_time) if len(translation) > 0 and target_language is not None: - img = self.createTextImageLarge(message_type, "small", message, your_language) - translation_img = self.createTextImageLarge(message_type, "large",translation, target_language) + img = self.createTextImageLargeLog(message_type, "small", message, your_language) + translation_img = self.createTextImageLargeLog(message_type, "large",translation, target_language) img = self.concatenateImagesVertically(img, translation_img) else: - img = self.createTextImageLarge(message_type, "large", message, your_language) + img = self.createTextImageLargeLog(message_type, "large", message, your_language) return self.concatenateImagesVertically(message_type_img, img) - def createOverlayImageLarge(self, message_type, message, your_language, translation="", target_language=None): - ui_color = self.getUiColorLarge() + def createOverlayImageLargeLog(self, message_type, message, your_language, translation="", target_language=None): + ui_color = self.getUiColorLargeLog() background_color = ui_color["background_color"] background_outline_color = ui_color["background_outline_color"] @@ -232,7 +232,7 @@ class OverlayImage: translation = log["translation"] target_language = log["target_language"] date_time = log["datetime"] - img = self.createTextboxLarge(message_type, message, your_language, translation, target_language, date_time) + img = self.createTextboxLargeLog(message_type, message, your_language, translation, target_language, date_time) imgs.append(img) img = imgs[0] @@ -249,9 +249,9 @@ class OverlayImage: if __name__ == "__main__": overlay = OverlayImage() - img = overlay.createOverlayImageSmall("Hello, World!", "English", "こんにちは、世界!", "Japanese") + img = overlay.createOverlayImageSmallLog("Hello, World!", "English", "こんにちは、世界!", "Japanese") img.save("overlay_small.png") - img = overlay.createOverlayImageLarge("send", "Hello, World!", "English", "こんにちは、世界!", "Japanese") + img = overlay.createOverlayImageLargeLog("send", "Hello, World!", "English", "こんにちは、世界!", "Japanese") img.save("overlay_large0.png") - img = overlay.createOverlayImageLarge("receive", "こんにちは、世界!", "Japanese", "Hello, World!", "English") + img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!", "Japanese", "Hello, World!", "English") img.save("overlay_large1.png") \ No newline at end of file diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index fa6cbd96..ff8fe4f7 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -250,9 +250,9 @@ class Controller: translation = " (" + "/".join(translation) + ")" model.logger.info(f"[SENT] {message}{translation}") - if config.OVERLAY_LARGE_LOG is True and model.overlay_large_log.initialized is True: - overlay_image = model.createOverlayImageLarge("send", message, translation) - model.updateOverlayLarge(overlay_image) + if config.OVERLAY_LARGE_LOG is True and model.overlay.initialized is True: + overlay_image = model.createOverlayImageLargeLog("send", message, translation) + model.updateOverlayLargeLog(overlay_image) def speakerMessage(self, message) -> None: if isinstance(message, bool) and message is False: @@ -289,13 +289,13 @@ class Controller: transliteration = model.convertMessageToTransliteration(message) if config.ENABLE_TRANSCRIPTION_RECEIVE is True: - if config.OVERLAY_SMALL_LOG is True and model.overlay_small_log.initialized is True: - overlay_image = model.createOverlayImageSmall(message, translation) - model.updateOverlaySmall(overlay_image) + if config.OVERLAY_SMALL_LOG is True and model.overlay.initialized is True: + overlay_image = model.createOverlayImageSmallLog(message, translation) + model.updateOverlaySmallLog(overlay_image) - if config.OVERLAY_LARGE_LOG is True and model.overlay_large_log.initialized is True: - overlay_image = model.createOverlayImageLarge("receive", message, translation) - model.updateOverlayLarge(overlay_image) + if config.OVERLAY_LARGE_LOG is True and model.overlay.initialized is True: + overlay_image = model.createOverlayImageLargeLog("receive", message, translation) + model.updateOverlayLargeLog(overlay_image) if config.SEND_RECEIVED_MESSAGE_TO_VRC is True: osc_message = self.messageFormatter("RECEIVED", translation, [message]) @@ -360,11 +360,9 @@ class Controller: osc_message = self.messageFormatter("SEND", translation, [message]) model.oscSendMessage(osc_message) - # if config.OVERLAY_SMALL_LOG is True: - # overlay_image = model.createOverlayImageSmall(message, translation) - # model.updateOverlay(overlay_image) - # overlay_image = model.createOverlayImageLong("send", message, translation) - # model.updateOverlay(overlay_image) + if config.OVERLAY_LARGE_LOG is True: + overlay_image = model.createOverlayImageLargeLog("send", message, translation) + model.updateOverlayLargeLog(overlay_image) # update textbox message log (Sent) if config.LOGGER_FEATURE is True: @@ -1183,7 +1181,7 @@ class Controller: def setDisableOverlaySmallLog(*args, **kwargs) -> dict: config.OVERLAY_SMALL_LOG = False if config.OVERLAY_SMALL_LOG is False: - model.clearOverlayImageSmall() + model.clearOverlayImageSmallLog() if config.OVERLAY_LARGE_LOG is False: model.shutdownOverlay() return {"status":200, "result":config.OVERLAY_SMALL_LOG} @@ -1213,7 +1211,7 @@ class Controller: def setDisableOverlayLargeLog(*args, **kwargs) -> dict: config.OVERLAY_LARGE_LOG = False if config.OVERLAY_LARGE_LOG is False: - model.clearOverlayImageLarge() + model.clearOverlayImageLargeLog() if config.OVERLAY_SMALL_LOG is False: model.shutdownOverlay() return {"status":200, "result":config.OVERLAY_LARGE_LOG} @@ -1412,9 +1410,9 @@ class Controller: @staticmethod def sendTextOverlaySmallLog(data, *args, **kwargs) -> dict: if config.OVERLAY_SMALL_LOG is True: - if model.overlay_small_log.initialized is True: + if model.overlay.initialized is True: overlay_image = model.createOverlayImageSystemMessage(data) - model.updateOverlaySmall(overlay_image) + model.updateOverlaySmallLog(overlay_image) return {"status":200, "result":data} def swapYourLanguageAndTargetLanguage(self, *args, **kwargs) -> dict: From b05c952148c540647762f98c56602a21fd81e789 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Thu, 14 Nov 2024 07:03:19 +0900 Subject: [PATCH 08/11] =?UTF-8?q?=F0=9F=90=9B[bugfix]=20Model=20:=20Overla?= =?UTF-8?q?y=20=E8=A8=AD=E5=AE=9A=E5=80=A4=E3=81=AE=E3=83=9F=E3=82=B9?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/models/overlay/overlay.py | 86 +++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/src-python/models/overlay/overlay.py b/src-python/models/overlay/overlay.py index 55cfca65..5b740728 100644 --- a/src-python/models/overlay/overlay.py +++ b/src-python/models/overlay/overlay.py @@ -10,7 +10,11 @@ try: from . import overlay_utils as utils except ImportError: import overlay_utils as utils -from utils import printLog +try: + from utils import printLog +except ImportError: + def printLog(*args): + print(*args) def mat34Id(array): arr = openvr.HmdMatrix34_t() @@ -94,7 +98,7 @@ class Overlay: self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)), size) self.updateColor([1, 1, 1], size) self.updateOpacity(self.settings[size]["opacity"], size, True) - self.updateUiScaling(self.settings[size]["ui_scaling"], size, True) + self.updateUiScaling(self.settings[size]["ui_scaling"], size) self.updatePosition( self.settings[size]["x_pos"], self.settings[size]["y_pos"], @@ -102,7 +106,8 @@ class Overlay: self.settings[size]["x_rotation"], self.settings[size]["y_rotation"], self.settings[size]["z_rotation"], - self.settings[size]["tracker"] + self.settings[size]["tracker"], + size ) except Exception as e: @@ -272,16 +277,67 @@ class Overlay: return _proc_name in (p.name() for p in process_iter()) if __name__ == "__main__": - x_pos = 0 - y_pos = 0 - z_pos = 0 - x_rotation = 0 - y_rotation = 0 - z_rotation = 0 + from overlay_image import OverlayImage - base_matrix = getLeftHandBaseMatrix() - translation = (x_pos * z_pos, y_pos * z_pos, z_pos) - rotation = (x_rotation, y_rotation, z_rotation) - transform = utils.transform_matrix(base_matrix, translation, rotation) - transform = mat34Id(transform) - print(transform) \ No newline at end of file + small_settings = { + "x_pos": 0.0, + "y_pos": 0.0, + "z_pos": 0.0, + "x_rotation": 0.0, + "y_rotation": 0.0, + "z_rotation": 0.0, + "display_duration": 5, + "fadeout_duration": 2, + "opacity": 1.0, + "ui_scaling": 0.5, + "tracker": "HMD", + } + + large_settings = { + "x_pos": 0.0, + "y_pos": 0.0, + "z_pos": 0.0, + "x_rotation": 0.0, + "y_rotation": 0.0, + "z_rotation": 0.0, + "display_duration": 5, + "fadeout_duration": 0, + "opacity": 1.0, + "ui_scaling": 0.5, + "tracker": "LeftHand", + } + + settings_dict = { + "small": small_settings, + "large": large_settings + } + + overlay_image = OverlayImage() + overlay = Overlay(settings_dict) + overlay.startOverlay() + time.sleep(1) + + # Example usage + img = overlay_image.createOverlayImageLargeLog("send", "こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") + overlay.updateImage(img, "large") + + img = overlay_image.createOverlayImageSmallLog("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") + overlay.updateImage(img, "small") + time.sleep(10) + + img = overlay_image.createOverlayImageSmallLog("こんにちは、世界!さようなら2", "Japanese", "Hello,World!Goodbye", "Japanese") + overlay.updateImage(img, "small") + time.sleep(10) + + for i in range(100): + print(i) + # Example usage + img = overlay_image.createOverlayImageSmallLog(f"こんにちは、世界!さようなら_{i}", "Japanese", "Hello,World!Goodbye", "Japanese") + overlay.updateImage(img, "small") + time.sleep(10) + + # img = overlay_image.createOverlayImageSmallLog("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese") + # overlay.updateImage(img, "small") + # time.sleep(0.5) + + overlay.shutdownOverlay() \ No newline at end of file From b6760877e3b397845bfaa805da9bde646c3f693d Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:55:18 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay=20LargeLog=E3=81=AEUI=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/models/overlay/overlay_image.py | 31 ++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index 2f650e44..035e2078 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -95,7 +95,7 @@ class OverlayImage: background = Image.new("RGBA", img.size, (0, 0, 0, 0)) draw = ImageDraw.Draw(background) - draw.rounded_rectangle([(0, 0), img.size], radius=30, fill=background_color, outline=background_outline_color, width=5) + draw.rounded_rectangle([(0, 0), img.size], radius=50, fill=background_color, outline=background_outline_color, width=5) img = Image.alpha_composite(background, img) return img @@ -103,9 +103,9 @@ class OverlayImage: @staticmethod def getUiSizeLargeLog(): return { - "width": int(960*2), - "font_size_large": int(23*4), - "font_size_small": int(23*4*2/3), + "width": int(960), + "font_size_large": int(15*2), + "font_size_small": int(15*2*2/3), } @staticmethod @@ -152,11 +152,11 @@ class OverlayImage: font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf"), font_size) text_width = draw.textlength(text, font) character_width = text_width // len(text) - character_line_num = int(width // character_width) + character_line_num = int(width // character_width) - 1 if len(text) > character_line_num: text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)]) n_num = len(text.split("\n")) - 1 - text_height = int(font_size*(n_num+2)) + text_height = int(font_size*(n_num+1.5)) img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) text_y = text_height // 2 @@ -183,7 +183,7 @@ class OverlayImage: font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", "NotoSansJP-Regular.ttf"), font_size) except Exception: font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", "NotoSansJP-Regular.ttf"), font_size) - text_height = font_size*2 + text_height = int(font_size*1.2) text_width = draw.textlength(date_time, font) character_width = text_width // len(date_time) img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0)) @@ -238,12 +238,12 @@ class OverlayImage: img = imgs[0] for i in imgs[1:]: img = self.concatenateImagesVertically(img, i) - img = self.addImageMargin(img, 0, 20, 0, 20, (0, 0, 0, 0)) + img = self.addImageMargin(img, 25, 25, 25, 25, (0, 0, 0, 0)) width, height = img.size background = Image.new("RGBA", (width, height), (0, 0, 0, 0)) draw = ImageDraw.Draw(background) - draw.rounded_rectangle([(0, 0), (width, height)], radius=15, fill=background_color, outline=background_outline_color, width=5) + draw.rounded_rectangle([(0, 0), (width, height)], radius=25, fill=background_color, outline=background_outline_color, width=5) img = Image.alpha_composite(background, img) return img @@ -252,6 +252,15 @@ if __name__ == "__main__": img = overlay.createOverlayImageSmallLog("Hello, World!", "English", "こんにちは、世界!", "Japanese") img.save("overlay_small.png") img = overlay.createOverlayImageLargeLog("send", "Hello, World!", "English", "こんにちは、世界!", "Japanese") - img.save("overlay_large0.png") img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!", "Japanese", "Hello, World!", "English") - img.save("overlay_large1.png") \ No newline at end of file + img = overlay.createOverlayImageLargeLog("send", "Hello, World!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "English", "aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああこんにちは、世界!", "Japanese") + img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "Japanese", "Hello, World!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "English") + img = overlay.createOverlayImageLargeLog("send", "Hello, World!", "English", "こんにちは、世界!", "Japanese") + img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!", "Japanese", "Hello, World!", "English") + img = overlay.createOverlayImageLargeLog("send", "Hello, World!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "English", "aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああこんにちは、世界!", "Japanese") + img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "Japanese", "Hello, World!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "English") + img = overlay.createOverlayImageLargeLog("send", "Hello, World!", "English", "こんにちは、世界!", "Japanese") + img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!", "Japanese", "Hello, World!", "English") + img = overlay.createOverlayImageLargeLog("send", "Hello, World!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "English", "aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああこんにちは、世界!", "Japanese") + img = overlay.createOverlayImageLargeLog("receive", "こんにちは、世界!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "Japanese", "Hello, World!aaaaaaaaaaaaaaaaaあああああああああああああああaaaaaaaaaaaaaaaaaあああああああああああああああ", "English") + img.save("overlay_large.png") \ No newline at end of file From 547a410eccc4bf8e56b1866915f84f53806d4219 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:52:58 +0900 Subject: [PATCH 10/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay=20LargeLog=E3=81=AEUI=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/models/overlay/overlay_image.py | 48 ++++++++++++++-------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index 035e2078..2a68c4d6 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -15,8 +15,9 @@ class OverlayImage: self.message_log = [] @staticmethod - def concatenateImagesVertically(img1: Image, img2: Image) -> Image: - dst = Image.new("RGBA", (img1.width, img1.height + img2.height)) + def concatenateImagesVertically(img1: Image, img2: Image, margin:int=0) -> Image: + total_height = img1.height + img2.height + margin + dst = Image.new("RGBA", (img1.width, total_height)) dst.paste(img1, (0, 0)) dst.paste(img2, (0, img1.height)) return dst @@ -106,6 +107,10 @@ class OverlayImage: "width": int(960), "font_size_large": int(15*2), "font_size_small": int(15*2*2/3), + "margin": 25, + "radius": 25, + "padding": 10, + "clause_margin": 20, } @staticmethod @@ -114,8 +119,8 @@ class OverlayImage: background_outline_color = (41, 42, 45) text_color_large = (223, 223, 223) text_color_small = (190, 190, 190) - text_color_send = (70, 161, 146) - text_color_receive = (220, 20, 60) + text_color_send = (97, 151, 180) + text_color_receive = (168, 97, 180) text_color_time = (120, 120, 120) return { "background_color": background_color, @@ -131,7 +136,8 @@ class OverlayImage: ui_size = self.getUiSizeLargeLog() font_size_large = ui_size["font_size_large"] font_size_small = ui_size["font_size_small"] - width = ui_size["width"] + ui_width = ui_size["width"] + ui_padding = ui_size["padding"] ui_color = self.getUiColorLargeLog() text_color_large = ui_color["text_color_large"] @@ -140,7 +146,7 @@ class OverlayImage: font_size = font_size_large if size == "large" else font_size_small text_color = text_color_large if size == "large" else text_color_small anchor = "lm" if message_type == "receive" else "rm" - text_x = 0 if message_type == "receive" else width + text_x = 0 if message_type == "receive" else ui_width align = "left" if message_type == "receive" else "right" font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") @@ -152,12 +158,12 @@ class OverlayImage: font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf"), font_size) text_width = draw.textlength(text, font) character_width = text_width // len(text) - character_line_num = int(width // character_width) - 1 + character_line_num = int(ui_width // character_width) - 1 # 1 is for margin if len(text) > character_line_num: text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)]) - n_num = len(text.split("\n")) - 1 - text_height = int(font_size*(n_num+1.5)) - img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0)) + n_num = len(text.split("\n")) + text_height = int(font_size*n_num) + ui_padding + img = Image.new("RGBA", (ui_width, text_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) text_y = text_height // 2 draw.multiline_text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font, align=align) @@ -165,8 +171,9 @@ class OverlayImage: def createTextImageMessageType(self, message_type, date_time): ui_size = self.getUiSizeLargeLog() - width = ui_size["width"] + ui_width = ui_size["width"] font_size = ui_size["font_size_small"] + ui_padding = ui_size["padding"] ui_color = self.getUiColorLargeLog() text_color_send = ui_color["text_color_send"] @@ -183,14 +190,14 @@ class OverlayImage: font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", "NotoSansJP-Regular.ttf"), font_size) except Exception: font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", "NotoSansJP-Regular.ttf"), font_size) - text_height = int(font_size*1.2) + text_height = int(font_size) + ui_padding text_width = draw.textlength(date_time, font) character_width = text_width // len(date_time) - img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0)) + img = Image.new("RGBA", (ui_width, text_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) text_y = text_height // 2 - text_time_x = 0 if message_type == "receive" else width - (text_width + character_width) - text_x = (text_width + character_width) if message_type == "receive" else width + text_time_x = 0 if message_type == "receive" else ui_width - (text_width + character_width) + text_x = (text_width + character_width) if message_type == "receive" else ui_width draw.text((text_time_x, text_y), date_time, text_color_time, anchor=anchor, stroke_width=0, font=font) draw.text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font) return img @@ -210,6 +217,11 @@ class OverlayImage: background_color = ui_color["background_color"] background_outline_color = ui_color["background_outline_color"] + ui_size = self.getUiSizeLargeLog() + ui_margin = ui_size["margin"] + ui_radius = ui_size["radius"] + ui_clause_margin = ui_size["clause_margin"] + self.message_log.append( { "message_type":message_type, @@ -237,13 +249,13 @@ class OverlayImage: img = imgs[0] for i in imgs[1:]: - img = self.concatenateImagesVertically(img, i) - img = self.addImageMargin(img, 25, 25, 25, 25, (0, 0, 0, 0)) + img = self.concatenateImagesVertically(img, i, ui_clause_margin) + img = self.addImageMargin(img, ui_margin, ui_margin, ui_margin, ui_margin, (0, 0, 0, 0)) width, height = img.size background = Image.new("RGBA", (width, height), (0, 0, 0, 0)) draw = ImageDraw.Draw(background) - draw.rounded_rectangle([(0, 0), (width, height)], radius=25, fill=background_color, outline=background_outline_color, width=5) + draw.rounded_rectangle([(0, 0), (width, height)], radius=ui_radius, fill=background_color, outline=background_outline_color, width=5) img = Image.alpha_composite(background, img) return img From d7231e17b820ea7489fda35722ea74de58dfe9ab Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Thu, 14 Nov 2024 14:07:51 +0900 Subject: [PATCH 11/11] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Overlay=20LargeLog=E3=81=AEUI=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/models/overlay/overlay_image.py | 192 +++++++++------------ 1 file changed, 81 insertions(+), 111 deletions(-) diff --git a/src-python/models/overlay/overlay_image.py b/src-python/models/overlay/overlay_image.py index 2a68c4d6..c5180650 100644 --- a/src-python/models/overlay/overlay_image.py +++ b/src-python/models/overlay/overlay_image.py @@ -15,60 +15,53 @@ class OverlayImage: self.message_log = [] @staticmethod - def concatenateImagesVertically(img1: Image, img2: Image, margin:int=0) -> Image: + def concatenateImagesVertically(img1: Image, img2: Image, margin: int = 0) -> Image: total_height = img1.height + img2.height + margin dst = Image.new("RGBA", (img1.width, total_height)) dst.paste(img1, (0, 0)) - dst.paste(img2, (0, img1.height)) + dst.paste(img2, (0, img1.height + margin)) return dst @staticmethod def addImageMargin(image: Image, top: int, right: int, bottom: int, left: int, color: Tuple[int, int, int, int]) -> Image: - width, height = image.size - new_width = width + right + left - new_height = height + top + bottom + new_width = image.width + right + left + new_height = image.height + top + bottom result = Image.new(image.mode, (new_width, new_height), color) result.paste(image, (left, top)) return result @staticmethod - def getUiSizeSmallLog(): + def getUiSizeSmallLog() -> dict: return { - "width": int(960*4), - "height": int(23*4), - "font_size": int(23*4), + "width": 3840, + "height": 92, + "font_size": 92, } @staticmethod - def getUiColorSmallLog(ui_type): - background_color = (41, 42, 45) - background_outline_color = (41, 42, 45) - text_color = (223, 223, 223) - match ui_type: - case "default": - pass - case _: - pass - return { - "background_color": background_color, - "background_outline_color": background_outline_color, - "text_color": text_color + def getUiColorSmallLog() -> dict: + colors = { + "background_color": (41, 42, 45), + "background_outline_color": (41, 42, 45), + "text_color": (223, 223, 223) } + return colors - def createTextboxSmallLog(self, text, language, text_color, base_width, base_height, font_size): + def createTextboxSmallLog(self, text:str, language:str, text_color:tuple, base_width:int, base_height:int, font_size:int) -> Image: font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") img = Image.new("RGBA", (base_width, base_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) + font_path = os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf") try: - font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + font = ImageFont.truetype(font_path, font_size) except Exception: - font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf"), font_size) + font = ImageFont.truetype(font_path, font_size) text_width = draw.textlength(text, font) character_width = text_width // len(text) - character_line_num = int((base_width) // character_width) - 12 + character_line_num = (base_width // character_width) - 12 if len(text) > character_line_num: - text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)]) + text = "\n".join([text[i:i + character_line_num] for i in range(0, len(text), character_line_num)]) text_height = font_size * (len(text.split("\n")) + 1) + 20 img = Image.new("RGBA", (base_width, text_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) @@ -78,19 +71,17 @@ class OverlayImage: draw.text((text_x, text_y), text, text_color, anchor="mm", stroke_width=0, font=font, align="center") return img - def createOverlayImageSmallLog(self, message, your_language, translation="", target_language=None, ui_type="default"): + def createOverlayImageSmallLog(self, message:str, your_language:str, translation:str="", target_language:str=None) -> Image: ui_size = self.getUiSizeSmallLog() - height = ui_size["height"] - width = ui_size["width"] - font_size = ui_size["font_size"] + width, height, font_size = ui_size["width"], ui_size["height"], ui_size["font_size"] - ui_colors = self.getUiColorSmallLog(ui_type) + ui_colors = self.getUiColorSmallLog() text_color = ui_colors["text_color"] background_color = ui_colors["background_color"] background_outline_color = ui_colors["background_outline_color"] img = self.createTextboxSmallLog(message, your_language, text_color, width, height, font_size) - if len(translation) > 0 and target_language is not None: + if translation and target_language: translation_img = self.createTextboxSmallLog(translation, target_language, text_color, width, height, font_size) img = self.concatenateImagesVertically(img, translation_img) @@ -98,15 +89,14 @@ class OverlayImage: draw = ImageDraw.Draw(background) draw.rounded_rectangle([(0, 0), img.size], radius=50, fill=background_color, outline=background_outline_color, width=5) - img = Image.alpha_composite(background, img) - return img + return Image.alpha_composite(background, img) @staticmethod - def getUiSizeLargeLog(): + def getUiSizeLargeLog() -> dict: return { - "width": int(960), - "font_size_large": int(15*2), - "font_size_small": int(15*2*2/3), + "width": 960, + "font_size_large": 30, + "font_size_small": 20, "margin": 25, "radius": 25, "padding": 10, @@ -114,105 +104,89 @@ class OverlayImage: } @staticmethod - def getUiColorLargeLog(): - background_color = (41, 42, 45) - background_outline_color = (41, 42, 45) - text_color_large = (223, 223, 223) - text_color_small = (190, 190, 190) - text_color_send = (97, 151, 180) - text_color_receive = (168, 97, 180) - text_color_time = (120, 120, 120) + def getUiColorLargeLog() -> dict: return { - "background_color": background_color, - "background_outline_color": background_outline_color, - "text_color_large": text_color_large, - "text_color_small": text_color_small, - "text_color_send": text_color_send, - "text_color_receive": text_color_receive, - "text_color_time": text_color_time + "background_color": (41, 42, 45), + "background_outline_color": (41, 42, 45), + "text_color_large": (223, 223, 223), + "text_color_small": (190, 190, 190), + "text_color_send": (97, 151, 180), + "text_color_receive": (168, 97, 180), + "text_color_time": (120, 120, 120) } - def createTextImageLargeLog(self, message_type, size, text, language): + def createTextImageLargeLog(self, message_type:str, size:str, text:str, language:str) -> Image: ui_size = self.getUiSizeLargeLog() - font_size_large = ui_size["font_size_large"] - font_size_small = ui_size["font_size_small"] - ui_width = ui_size["width"] - ui_padding = ui_size["padding"] - - ui_color = self.getUiColorLargeLog() - text_color_large = ui_color["text_color_large"] - text_color_small = ui_color["text_color_small"] - - font_size = font_size_large if size == "large" else font_size_small - text_color = text_color_large if size == "large" else text_color_small + font_size = ui_size["font_size_large"] if size == "large" else ui_size["font_size_small"] + text_color = self.getUiColorLargeLog()[f"text_color_{size}"] anchor = "lm" if message_type == "receive" else "rm" - text_x = 0 if message_type == "receive" else ui_width + text_x = 0 if message_type == "receive" else ui_size["width"] align = "left" if message_type == "receive" else "right" font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular") img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) + font_path = os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf") try: - font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size) + font = ImageFont.truetype(font_path, font_size) except Exception: - font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", f"{font_family}.ttf"), font_size) + font = ImageFont.truetype(font_path, font_size) + text_width = draw.textlength(text, font) character_width = text_width // len(text) - character_line_num = int(ui_width // character_width) - 1 # 1 is for margin + character_line_num = int((ui_size["width"] // character_width) - 1) if len(text) > character_line_num: - text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)]) - n_num = len(text.split("\n")) - text_height = int(font_size*n_num) + ui_padding - img = Image.new("RGBA", (ui_width, text_height), (0, 0, 0, 0)) + text = "\n".join([text[i:i + character_line_num] for i in range(0, len(text), character_line_num)]) + text_height = font_size * len(text.split("\n")) + ui_size["padding"] + img = Image.new("RGBA", (ui_size["width"], text_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) text_y = text_height // 2 draw.multiline_text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font, align=align) return img - def createTextImageMessageType(self, message_type, date_time): + def createTextImageMessageType(self, message_type:str, date_time:str) -> Image: ui_size = self.getUiSizeLargeLog() - ui_width = ui_size["width"] font_size = ui_size["font_size_small"] ui_padding = ui_size["padding"] ui_color = self.getUiColorLargeLog() - text_color_send = ui_color["text_color_send"] - text_color_receive = ui_color["text_color_receive"] + text_color = ui_color[f"text_color_{message_type}"] text_color_time = ui_color["text_color_time"] anchor = "lm" if message_type == "receive" else "rm" text = "Receive" if message_type == "receive" else "Send" - text_color = text_color_receive if message_type == "receive" else text_color_send img = Image.new("RGBA", (0, 0), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) + font_path = os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", "NotoSansJP-Regular.ttf") try: - font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", "NotoSansJP-Regular.ttf"), font_size) + font = ImageFont.truetype(font_path, font_size) except Exception: - font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "..", "..", "..", "fonts", "NotoSansJP-Regular.ttf"), font_size) - text_height = int(font_size) + ui_padding + font = ImageFont.truetype(font_path, font_size) + + text_height = font_size + ui_padding text_width = draw.textlength(date_time, font) character_width = text_width // len(date_time) - img = Image.new("RGBA", (ui_width, text_height), (0, 0, 0, 0)) + img = Image.new("RGBA", (ui_size["width"], text_height), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) text_y = text_height // 2 - text_time_x = 0 if message_type == "receive" else ui_width - (text_width + character_width) - text_x = (text_width + character_width) if message_type == "receive" else ui_width + text_time_x = 0 if message_type == "receive" else ui_size["width"] - (text_width + character_width) + text_x = (text_width + character_width) if message_type == "receive" else ui_size["width"] draw.text((text_time_x, text_y), date_time, text_color_time, anchor=anchor, stroke_width=0, font=font) draw.text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font) return img - def createTextboxLargeLog(self, message_type, message, your_language, translation, target_language, date_time): + def createTextboxLargeLog(self, message_type:str, message:str, your_language:str, translation:str, target_language:str, date_time:str) -> Image: message_type_img = self.createTextImageMessageType(message_type, date_time) - if len(translation) > 0 and target_language is not None: + if translation and target_language: img = self.createTextImageLargeLog(message_type, "small", message, your_language) - translation_img = self.createTextImageLargeLog(message_type, "large",translation, target_language) + translation_img = self.createTextImageLargeLog(message_type, "large", translation, target_language) img = self.concatenateImagesVertically(img, translation_img) else: img = self.createTextImageLargeLog(message_type, "large", message, your_language) return self.concatenateImagesVertically(message_type_img, img) - def createOverlayImageLargeLog(self, message_type, message, your_language, translation="", target_language=None): + def createOverlayImageLargeLog(self, message_type:str, message:str, your_language:str, translation:str="", target_language:str=None) -> Image: ui_color = self.getUiColorLargeLog() background_color = ui_color["background_color"] background_outline_color = ui_color["background_outline_color"] @@ -222,30 +196,27 @@ class OverlayImage: ui_radius = ui_size["radius"] ui_clause_margin = ui_size["clause_margin"] - self.message_log.append( - { - "message_type":message_type, - "message":message, - "your_language":your_language, - "translation":translation, - "target_language":target_language, - "datetime":datetime.now().strftime("%H:%M") - } - ) + self.message_log.append({ + "message_type": message_type, + "message": message, + "your_language": your_language, + "translation": translation, + "target_language": target_language, + "datetime": datetime.now().strftime("%H:%M") + }) if len(self.message_log) > 10: self.message_log = self.message_log[-10:] - imgs = [] - for log in self.message_log: - message_type = log["message_type"] - message = log["message"] - your_language = log["your_language"] - translation = log["translation"] - target_language = log["target_language"] - date_time = log["datetime"] - img = self.createTextboxLargeLog(message_type, message, your_language, translation, target_language, date_time) - imgs.append(img) + imgs = [ + self.createTextboxLargeLog( + log["message_type"], + log["message"], + log["your_language"], + log["translation"], + log["target_language"], + log["datetime"]) for log in self.message_log + ] img = imgs[0] for i in imgs[1:]: @@ -256,8 +227,7 @@ class OverlayImage: background = Image.new("RGBA", (width, height), (0, 0, 0, 0)) draw = ImageDraw.Draw(background) draw.rounded_rectangle([(0, 0), (width, height)], radius=ui_radius, fill=background_color, outline=background_outline_color, width=5) - img = Image.alpha_composite(background, img) - return img + return Image.alpha_composite(background, img) if __name__ == "__main__": overlay = OverlayImage()