🚧[WIP/TEST] Model : 変数名の修正

関数の処理が一部変だったので修正
This commit is contained in:
misyaguziya
2024-04-19 15:13:19 +09:00
parent d715f0b6d6
commit 0f4fe7c350
3 changed files with 53 additions and 45 deletions

View File

@@ -565,14 +565,14 @@ class Model:
ui_type = config.OVERLAY_UI_TYPE ui_type = config.OVERLAY_UI_TYPE
return self.overlay_image.createOverlayImageShort(message, your_language, translation, target_language, ui_type) return self.overlay_image.createOverlayImageShort(message, your_language, translation, target_language, ui_type)
def createOverlayImageLong(self, message_type, message, translation): # def createOverlayImageLong(self, message_type, message, translation):
your_language = config.TARGET_LANGUAGE if message_type == "receive" else config.SOURCE_LANGUAGE # your_language = config.TARGET_LANGUAGE if message_type == "receive" else config.SOURCE_LANGUAGE
target_language = config.SOURCE_LANGUAGE if message_type == "receive" else config.TARGET_LANGUAGE # target_language = config.SOURCE_LANGUAGE if message_type == "receive" else config.TARGET_LANGUAGE
return self.overlay_image.create_overlay_image_long(message_type, message, your_language, translation, target_language) # return self.overlay_image.create_overlay_image_long(message_type, message, your_language, translation, target_language)
def setOverlayImage(self, img): def setOverlayImage(self, img):
if self.overlay.initFlag is True: if self.overlay.initFlag is True:
self.overlay.uiMan.uiUpdate(img) self.overlay.uiManager.uiUpdate(img)
def startOverlay(self): def startOverlay(self):
if self.overlay.initFlag is False: if self.overlay.initFlag is False:

View File

@@ -5,7 +5,7 @@ import asyncio
import openvr import openvr
from PIL import Image from PIL import Image
def check_steamvr_running(): def checkSteamvrRunning():
for proc in psutil.process_iter(): for proc in psutil.process_iter():
if "vrserver" in proc.name().lower() or "vrcompositor" in proc.name().lower(): if "vrserver" in proc.name().lower() or "vrcompositor" in proc.name().lower():
return True return True
@@ -23,21 +23,21 @@ def mat34Id():
class UIElement: class UIElement:
def __init__(self, overlayRoot, key: str, name: str, settings: dict = None) -> None: def __init__(self, overlayRoot, key: str, name: str, settings: dict = None) -> None:
""" """
pos is a 2-tuple representing (x, y) normalised position of the overlay on the screen pos is a 2-tuple representing (x, y) normalized position of the overlay on the screen
""" """
self.overlay = overlayRoot self.overlay = overlayRoot
self.overlayKey = key self.overlayKey = key
self.overlayName = name self.overlayName = name
self.settings = settings self.settings = settings
pos = (self.settings['Normalised icon X position'], self.settings['Normalised icon Y position']) pos = (self.settings['Normalized_icon_X_position'], self.settings['Normalized_icon_Y_position'])
self.handle = self.overlay.createOverlay(self.overlayKey, self.overlayName) self.handle = self.overlay.createOverlay(self.overlayKey, self.overlayName)
self.setImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0))) # blank image for default self.setImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0))) # blank image for default
self.setColour(self.settings['Colour']) self.setColor(self.settings['Color'])
self.setTransparency(self.settings['Transparency']) self.setTransparency(self.settings['Transparency'])
self.overlay.setOverlayWidthInMeters( self.overlay.setOverlayWidthInMeters(
self.handle, self.handle,
self.settings['Normalised icon width'] * self.settings['Icon plane depth'] self.settings['Normalized_icon_width'] * self.settings['Icon_plane_depth']
) )
self.setPosition(pos) self.setPosition(pos)
@@ -50,7 +50,7 @@ class UIElement:
img = (ctypes.c_char * len(img)).from_buffer_copy(img) img = (ctypes.c_char * len(img)).from_buffer_copy(img)
self.overlay.setOverlayRaw(self.handle, img, width, height, 4) self.overlay.setOverlayRaw(self.handle, img, width, height, 4)
def setColour(self, col): def setColor(self, col):
""" """
col is a 3-tuple representing (r, g, b) col is a 3-tuple representing (r, g, b)
""" """
@@ -61,14 +61,14 @@ class UIElement:
def setPosition(self, pos): def setPosition(self, pos):
""" """
pos is a 2-tuple representing normalised (x, y) pos is a 2-tuple representing normalized (x, y)
""" """
self.transform = mat34Id() # no rotation required for HMD attachment self.transform = mat34Id() # no rotation required for HMD attachment
# assign position # assign position
self.transform[0][3] = pos[0] * self.settings['Icon plane depth'] self.transform[0][3] = pos[0] * self.settings['Icon_plane_depth']
self.transform[1][3] = pos[1] * self.settings['Icon plane depth'] self.transform[1][3] = pos[1] * self.settings['Icon_plane_depth']
self.transform[2][3] = - self.settings['Icon plane depth'] self.transform[2][3] = - self.settings['Icon_plane_depth']
self.overlay.setOverlayTransformTrackedDeviceRelative( self.overlay.setOverlayTransformTrackedDeviceRelative(
self.handle, self.handle,
@@ -77,20 +77,20 @@ class UIElement:
) )
class UIManager: class UIManager:
def __init__(self, settings): def __init__(self, overlay_key, overlay_name, settings):
self.overlay = openvr.IVROverlay() self.overlay = openvr.IVROverlay()
self.settings = settings self.settings = settings
self.overlayUI = UIElement( self.overlayUI = UIElement(
self.overlay, self.overlay,
"VRCT", overlay_key,
"Receive UI Element", overlay_name,
self.settings, self.settings,
) )
self.lastUpdate = time.monotonic() self.lastUpdate = time.monotonic()
def update(self): def update(self):
currTime = time.monotonic() currTime = time.monotonic()
if self.settings['Fade interval'] != 0: if self.settings['Fade_interval'] != 0:
self.evaluateTransparencyFade(self.overlayUI, self.lastUpdate, currTime) self.evaluateTransparencyFade(self.overlayUI, self.lastUpdate, currTime)
def uiUpdate(self, img): def uiUpdate(self, img):
@@ -99,9 +99,9 @@ class UIManager:
self.lastUpdate = time.monotonic() self.lastUpdate = time.monotonic()
def evaluateTransparencyFade(self, ui, lastUpdate, currentTime): def evaluateTransparencyFade(self, ui, lastUpdate, currentTime):
if (currentTime - lastUpdate) > self.settings['Fade time']: if (currentTime - lastUpdate) > self.settings['Fade_time']:
timeThroughInterval = currentTime - lastUpdate - self.settings['Fade time'] timeThroughInterval = currentTime - lastUpdate - self.settings['Fade_time']
fadeRatio = 1 - timeThroughInterval / self.settings['Fade interval'] fadeRatio = 1 - timeThroughInterval / self.settings['Fade_interval']
if fadeRatio < 0: if fadeRatio < 0:
fadeRatio = 0 fadeRatio = 0
@@ -114,20 +114,20 @@ class Overlay:
def __init__(self): def __init__(self):
self.initFlag = False self.initFlag = False
settings = { settings = {
"Colour": [1, 1, 1], "Color": [1, 1, 1],
"Transparency": 1, "Transparency": 1,
"Normalised icon X position": 0.0, "Normalized_icon_X_position": 0.0,
"Normalised icon Y position": -0.41, "Normalized_icon_Y_position": -0.41,
"Icon plane depth": 1, "Icon_plane_depth": 1,
"Normalised icon width": 1, "Normalized_icon_width": 1,
"Fade time": 5, "Fade_time": 5,
"Fade interval": 2, "Fade_interval": 2,
} }
self.settings = settings self.settings = settings
def init(self): def init(self):
try: try:
if check_steamvr_running() is True: if checkSteamvrRunning() is True:
openvr.init(openvr.VRApplication_Overlay) openvr.init(openvr.VRApplication_Overlay)
self.initFlag = True self.initFlag = True
except Exception as e: except Exception as e:
@@ -136,18 +136,18 @@ class Overlay:
async def mainLoop(self): async def mainLoop(self):
while True: while True:
startTime = time.monotonic() startTime = time.monotonic()
self.uiMan.update() self.uiManager.update()
sleepTime = (1 / 60) - (time.monotonic() - startTime) sleepTime = (1 / 60) - (time.monotonic() - startTime)
if sleepTime > 0: if sleepTime > 0:
await asyncio.sleep(sleepTime) await asyncio.sleep(sleepTime)
async def init_main(self): async def initMain(self):
self.uiMan = UIManager(self.settings) self.uiManager = UIManager("Overlay_Speaker2log", "SOverlay_Speaker2log_UI", self.settings)
await self.mainLoop() await self.mainLoop()
def startOverlay(self): def startOverlay(self):
asyncio.run(self.init_main()) asyncio.run(self.initMain())
if __name__ == '__main__': if __name__ == '__main__':
from overlay_image import OverlayImage from overlay_image import OverlayImage
@@ -181,16 +181,17 @@ if __name__ == '__main__':
time.sleep(1) time.sleep(1)
img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese", ui_type="sakura") img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese", ui_type="sakura")
# img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", ui_type="sakura")
if overlay.initFlag is True: if overlay.initFlag is True:
overlay.uiMan.uiUpdate(img) overlay.uiManager.uiUpdate(img)
time.sleep(10) time.sleep(10)
img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "안녕하세요, 세계!안녕", "Korean") img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "안녕하세요, 세계!안녕", "Korean")
if overlay.initFlag is True: if overlay.initFlag is True:
overlay.uiMan.uiUpdate(img) overlay.uiManager.uiUpdate(img)
time.sleep(10) time.sleep(10)
img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "你好世界!再见", "Chinese Simplified") img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "你好世界!再见", "Chinese Simplified")
if overlay.initFlag is True: if overlay.initFlag is True:
overlay.uiMan.uiUpdate(img) overlay.uiManager.uiUpdate(img)
time.sleep(10) time.sleep(10)

View File

@@ -152,12 +152,12 @@ class OverlayImage:
def getUiColors(self, ui_type): def getUiColors(self, ui_type):
match ui_type: match ui_type:
case "default": case "default":
background_color = (41, 42, 45, 127) background_color = (41, 42, 45, 240)
background_outline_color = (41, 42, 45, 127) background_outline_color = (41, 42, 45, 240)
text_color = (223, 223, 223) text_color = (223, 223, 223)
case "sakura": case "sakura":
background_color = (225, 40, 30, 20) background_color = (225, 40, 30, 80)
background_outline_color = (255, 255, 255, 50) background_outline_color = (255, 255, 255, 200)
text_color = (223, 223, 223) text_color = (223, 223, 223)
return { return {
"background_color": background_color, "background_color": background_color,
@@ -171,16 +171,23 @@ class OverlayImage:
case "default": case "default":
pass pass
case "sakura": 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_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")) 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 = overlay_tl.getchannel("A")
alpha = alpha.point(lambda x: x * 0.1) alpha = alpha.point(lambda x: x * alpha_ratio)
overlay_tl.putalpha(alpha) overlay_tl.putalpha(alpha)
alpha = overlay_br.getchannel("A") alpha = overlay_br.getchannel("A")
alpha = alpha.point(lambda x: x * 0.1) alpha = alpha.point(lambda x: x * alpha_ratio)
overlay_br.putalpha(alpha) overlay_br.putalpha(alpha)
decoration_image.paste(overlay_tl, (7, 7)) decoration_image.paste(overlay_tl, (margin, margin))
decoration_image.paste(overlay_br, (image_size[0]-overlay_br.size[0]-7, image_size[1]-overlay_br.size[1]-7)) decoration_image.paste(overlay_br, (image_size[0]-overlay_br.size[0]-margin, image_size[1]-overlay_br.size[1]-margin))
return decoration_image return decoration_image
def createTextboxShort(self, text, language, text_color, base_width, base_height, font_size): def createTextboxShort(self, text, language, text_color, base_width, base_height, font_size):