👍️[Update] Model : overlayのsmall logの処理を整理

- 変数名をsmall_log用に修正
- OVERLAY_SETTINGSを削除しOVERLAY_SMALL_LOG_SETTINGSに統合
- OVERLAY_UI_TYPEを廃止
This commit is contained in:
misyaguziya
2024-11-06 04:16:09 +09:00
parent 7648ebe493
commit c00542ef57
5 changed files with 59 additions and 208 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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: