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] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:=20Ove?= =?UTF-8?q?rlay=20Large=20log=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD=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},