👍️[Update] Model : overlay機能を修正
- 画像生成をoverlay_img.pyにまとめた
This commit is contained in:
@@ -2,71 +2,10 @@ import ctypes
|
||||
import time
|
||||
import asyncio
|
||||
import openvr
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from os import path as os_path
|
||||
|
||||
def create_overlay_image(message, your_language, translation=None, target_language=None):
|
||||
width, height = (1920, 46)
|
||||
background_color = (54, 63, 77)
|
||||
text_color = (223, 223, 224)
|
||||
font_size = 46
|
||||
languages = {
|
||||
"Japanese" : "NotoSansJP-Regular",
|
||||
"Korean" : "NotoSansKR-Regular",
|
||||
"Chinese Simplified" : "NotoSansSC-Regular",
|
||||
"Chinese Traditional" : "NotoSansTC-Regular",
|
||||
}
|
||||
|
||||
def get_concat_v(im1, im2):
|
||||
dst = Image.new('RGBA', (im1.width, im1.height + im2.height))
|
||||
dst.paste(im1, (0, 0))
|
||||
dst.paste(im2, (0, im1.height))
|
||||
return dst
|
||||
|
||||
def create_textbox(text, language):
|
||||
font_family = languages.get(language, "NotoSansJP-Regular")
|
||||
img = Image.new("RGBA", (width, height), (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)
|
||||
text_width = draw.textlength(text, font)
|
||||
character_width = text_width // len(text)
|
||||
character_line_num = int((width - 40) // 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)])
|
||||
text_height = font_size * (len(text.split("\n")) + 1) + 20
|
||||
img = Image.new("RGBA", (width, text_height), (0, 0, 0, 0))
|
||||
draw = ImageDraw.Draw(img)
|
||||
# draw.rounded_rectangle([(0, 0), (width, text_height)], radius=30, fill=background_color, outline=background_color, width=5)
|
||||
|
||||
text_x = width // 2
|
||||
text_y = text_height // 2
|
||||
draw.text((text_x, text_y), text, text_color, anchor="mm", stroke_width=0, font=font, align="center")
|
||||
return img
|
||||
|
||||
img = create_textbox(message, your_language)
|
||||
if translation is not None and target_language is not None:
|
||||
translation_img = create_textbox(translation, target_language)
|
||||
img = get_concat_v(img, translation_img)
|
||||
|
||||
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=30, fill=background_color, outline=background_color, width=5)
|
||||
img = Image.alpha_composite(background, img)
|
||||
|
||||
return img
|
||||
|
||||
settings = {
|
||||
"Colour": [1, 1, 1],
|
||||
"Transparency": 1,
|
||||
"Normalised icon X position": 0.0,
|
||||
"Normalised icon Y position": -0.41,
|
||||
"Icon plane depth": 1,
|
||||
"Normalised icon width": 1,
|
||||
"Fade time": 5,
|
||||
"Fade interval": 2,
|
||||
}
|
||||
from PIL import Image
|
||||
|
||||
# This code is based on the following source:
|
||||
# [GOpy](https://github.com/MeroFune/GOpy)
|
||||
def mat34Id():
|
||||
arr = openvr.HmdMatrix34_t()
|
||||
arr[0][0] = 1
|
||||
@@ -75,27 +14,26 @@ def mat34Id():
|
||||
return arr
|
||||
|
||||
class UIElement:
|
||||
def __init__(self, overlayRoot, key, name, pos, flip = False) -> 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
|
||||
"""
|
||||
self.overlay = overlayRoot
|
||||
self.overlayKey = key
|
||||
self.overlayName = name
|
||||
self.flip = flip
|
||||
|
||||
self.settings = settings
|
||||
pos = (self.settings['Normalised icon X position'], self.settings['Normalised icon Y position'])
|
||||
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.setColour(settings['Colour'])
|
||||
self.setTransparency(settings['Transparency'])
|
||||
self.setColour(self.settings['Colour'])
|
||||
self.setTransparency(self.settings['Transparency'])
|
||||
self.overlay.setOverlayWidthInMeters(
|
||||
self.handle,
|
||||
settings['Normalised icon width'] * settings['Icon plane depth']
|
||||
self.settings['Normalised icon width'] * self.settings['Icon plane depth']
|
||||
)
|
||||
|
||||
self.setPosition(pos)
|
||||
|
||||
self.overlay.showOverlay(self.handle)
|
||||
|
||||
def setImage(self, img):
|
||||
@@ -121,9 +59,9 @@ class UIElement:
|
||||
self.transform = mat34Id() # no rotation required for HMD attachment
|
||||
|
||||
# assign position
|
||||
self.transform[0][3] = pos[0] * settings['Icon plane depth']
|
||||
self.transform[1][3] = pos[1] * settings['Icon plane depth']
|
||||
self.transform[2][3] = -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[2][3] = - self.settings['Icon plane depth']
|
||||
|
||||
self.overlay.setOverlayTransformTrackedDeviceRelative(
|
||||
self.handle,
|
||||
@@ -132,45 +70,53 @@ class UIElement:
|
||||
)
|
||||
|
||||
class UIManager:
|
||||
def __init__(self):
|
||||
def __init__(self, settings):
|
||||
self.overlay = openvr.IVROverlay()
|
||||
|
||||
self.settings = settings
|
||||
self.overlayUI = UIElement(
|
||||
self.overlay,
|
||||
"VRCT",
|
||||
"Receive UI Element",
|
||||
(settings['Normalised icon X position'], settings['Normalised icon Y position']),
|
||||
self.settings,
|
||||
)
|
||||
self.lastUpdate = time.monotonic()
|
||||
|
||||
def update(self):
|
||||
currTime = time.monotonic()
|
||||
if settings['Fade interval'] != 0:
|
||||
if self.settings['Fade interval'] != 0:
|
||||
self.evaluateTransparencyFade(self.overlayUI, self.lastUpdate, currTime)
|
||||
|
||||
def uiUpdate(self, img):
|
||||
self.overlayUI.setImage(img)
|
||||
self.overlayUI.setTransparency(settings['Transparency'])
|
||||
self.overlayUI.setTransparency(self.settings['Transparency'])
|
||||
self.lastUpdate = time.monotonic()
|
||||
|
||||
def evaluateTransparencyFade(self, ui, lastUpdate, currentTime):
|
||||
if (currentTime - lastUpdate) > settings['Fade time']:
|
||||
timeThroughInterval = currentTime - lastUpdate - settings['Fade time']
|
||||
fadeRatio = 1 - timeThroughInterval / settings['Fade interval']
|
||||
if (currentTime - lastUpdate) > self.settings['Fade time']:
|
||||
timeThroughInterval = currentTime - lastUpdate - self.settings['Fade time']
|
||||
fadeRatio = 1 - timeThroughInterval / self.settings['Fade interval']
|
||||
if fadeRatio < 0:
|
||||
fadeRatio = 0
|
||||
|
||||
ui.setTransparency(fadeRatio * settings['Transparency'])
|
||||
ui.setTransparency(fadeRatio * self.settings['Transparency'])
|
||||
|
||||
def posUpdate(self, pos):
|
||||
self.overlayUI.setPosition(pos)
|
||||
|
||||
class Overlay:
|
||||
def __init__(self):
|
||||
self.initFlag = False
|
||||
|
||||
def checkHMD(self):
|
||||
return openvr.isHmdPresent()
|
||||
|
||||
def checkRuntime(self):
|
||||
return openvr.isRuntimeInstalled()
|
||||
settings = {
|
||||
"Colour": [1, 1, 1],
|
||||
"Transparency": 1,
|
||||
"Normalised icon X position": 0.0,
|
||||
"Normalised icon Y position": -0.41,
|
||||
"Icon plane depth": 1,
|
||||
"Normalised icon width": 1,
|
||||
"Fade time": 5,
|
||||
"Fade interval": 2,
|
||||
}
|
||||
self.settings = settings
|
||||
|
||||
def init(self):
|
||||
try:
|
||||
@@ -189,13 +135,14 @@ class Overlay:
|
||||
await asyncio.sleep(sleepTime)
|
||||
|
||||
async def init_main(self):
|
||||
self.uiMan = UIManager()
|
||||
self.uiMan = UIManager(self.settings)
|
||||
await self.mainLoop()
|
||||
|
||||
def startOverlay(self):
|
||||
asyncio.run(self.init_main())
|
||||
|
||||
if __name__ == '__main__':
|
||||
from overlay_image import OverlayImage
|
||||
from threading import Thread, Event
|
||||
class threadFnc(Thread):
|
||||
def __init__(self, fnc, end_fnc=None, daemon=True, *args, **kwargs):
|
||||
@@ -216,6 +163,7 @@ if __name__ == '__main__':
|
||||
self.fnc(*self._args, **self._kwargs)
|
||||
|
||||
overlay = Overlay()
|
||||
overlay_image = OverlayImage()
|
||||
|
||||
if overlay.initFlag is False:
|
||||
overlay.init()
|
||||
@@ -223,17 +171,18 @@ if __name__ == '__main__':
|
||||
t = threadFnc(overlay.startOverlay)
|
||||
t.start()
|
||||
|
||||
img = create_overlay_image("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese")
|
||||
time.sleep(1)
|
||||
img = overlay_image.create_overlay_image_short("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese")
|
||||
if overlay.initFlag is True:
|
||||
overlay.uiMan.uiUpdate(img)
|
||||
time.sleep(10)
|
||||
|
||||
img = create_overlay_image("こんにちは、世界!さようなら", "Japanese", "안녕하세요, 세계!안녕", "Korean")
|
||||
img = overlay_image.create_overlay_image_short("こんにちは、世界!さようなら", "Japanese", "안녕하세요, 세계!안녕", "Korean")
|
||||
if overlay.initFlag is True:
|
||||
overlay.uiMan.uiUpdate(img)
|
||||
time.sleep(10)
|
||||
|
||||
img = create_overlay_image("こんにちは、世界!さようなら", "Japanese", "你好世界!再见", "Chinese Simplified")
|
||||
img = overlay_image.create_overlay_image_short("こんにちは、世界!さようなら", "Japanese", "你好世界!再见", "Chinese Simplified")
|
||||
if overlay.initFlag is True:
|
||||
overlay.uiMan.uiUpdate(img)
|
||||
time.sleep(10)
|
||||
Reference in New Issue
Block a user