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] =?UTF-8?q?=F0=9F=90=9B[bugfix]=20Model=20:=20Overlay?= =?UTF-8?q?=E3=81=AE=E8=B5=B7=E5=8B=95=E3=81=A7=E3=83=A9=E3=82=A4=E3=83=96?= =?UTF-8?q?=E3=83=A9=E3=83=AA=E3=81=AE=E5=88=9D=E6=9C=9F=E5=8C=96=E3=82=92?= =?UTF-8?q?=E8=A1=8C=E3=81=86=E3=82=BF=E3=82=A4=E3=83=9F=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=81=A7small=E3=81=A8large=E3=81=AE2=E3=81=A4=E3=81=AE?= =?UTF-8?q?=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB=E3=82=92=E7=94=9F=E6=88=90?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=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}