🚧 [WIP/TEST] Model : Overlay リファクタリング
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 32 KiB |
15
model.py
15
model.py
@@ -613,25 +613,24 @@ class Model:
|
|||||||
|
|
||||||
def updateOverlayPosition(self):
|
def updateOverlayPosition(self):
|
||||||
pos = (config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"])
|
pos = (config.OVERLAY_SMALL_LOG_SETTINGS["x_pos"], config.OVERLAY_SMALL_LOG_SETTINGS["y_pos"])
|
||||||
self.overlay.setPosition(pos)
|
|
||||||
depth = config.OVERLAY_SMALL_LOG_SETTINGS["depth"]
|
depth = config.OVERLAY_SMALL_LOG_SETTINGS["depth"]
|
||||||
self.overlay.setDepth(depth)
|
self.overlay.updatePosition(pos, depth)
|
||||||
|
|
||||||
def updateOverlayTimes(self):
|
def updateOverlayTimes(self):
|
||||||
display_duration = config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"]
|
display_duration = config.OVERLAY_SMALL_LOG_SETTINGS["display_duration"]
|
||||||
self.overlay.setFadeTime(display_duration)
|
self.overlay.updateDisplayDuration(display_duration)
|
||||||
fadeout_duration = config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"]
|
fadeout_duration = config.OVERLAY_SMALL_LOG_SETTINGS["fadeout_duration"]
|
||||||
self.overlay.setFadeInterval(fadeout_duration)
|
self.overlay.updateFadeoutDuration(fadeout_duration)
|
||||||
|
|
||||||
def updateOverlayImageOpacity(self):
|
def updateOverlayImageOpacity(self):
|
||||||
opacity = config.OVERLAY_SETTINGS["opacity"]
|
opacity = config.OVERLAY_SETTINGS["opacity"]
|
||||||
self.overlay.setTransparency(opacity)
|
self.overlay.updateOpacity(opacity, with_fade=True)
|
||||||
|
|
||||||
def updateOverlayImageUiScaling(self):
|
def updateOverlayImageUiScaling(self):
|
||||||
ui_scaling = config.OVERLAY_SETTINGS["ui_scaling"]
|
ui_scaling = config.OVERLAY_SETTINGS["ui_scaling"]
|
||||||
self.overlay.setUiScaling(ui_scaling)
|
self.overlay.updateUiScaling(ui_scaling)
|
||||||
|
|
||||||
def shutdownOverlay(self):
|
def stopOverlay(self):
|
||||||
self.overlay.shutdown()
|
self.overlay.setStopOverlay()
|
||||||
|
|
||||||
model = Model()
|
model = Model()
|
||||||
@@ -8,6 +8,7 @@ import openvr
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
# from queue import Queue
|
# from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
import OverlayImage
|
||||||
|
|
||||||
def mat34Id():
|
def mat34Id():
|
||||||
arr = openvr.HmdMatrix34_t()
|
arr = openvr.HmdMatrix34_t()
|
||||||
@@ -17,25 +18,26 @@ def mat34Id():
|
|||||||
return arr
|
return arr
|
||||||
|
|
||||||
class Overlay:
|
class Overlay:
|
||||||
def __init__(self, x, y , depth, fade_time, fade_interval, transparency, ui_scaling):
|
def __init__(self, x, y , depth, fade_time, fade_interval, opacity, ui_scaling):
|
||||||
self.initialized = False
|
self.initialized = False
|
||||||
settings = {
|
settings = {
|
||||||
"Color": [1, 1, 1],
|
"color": [1, 1, 1],
|
||||||
"Transparency": transparency,
|
"opacity": opacity,
|
||||||
"Normalized_icon_X_position": x,
|
"x_pos": x,
|
||||||
"Normalized_icon_Y_position": y,
|
"y_pos": y,
|
||||||
"Icon_plane_depth": depth,
|
"depth": depth,
|
||||||
"Fade_time": fade_time,
|
"fade_time": fade_time,
|
||||||
"Fade_interval": fade_interval,
|
"fade_interval": fade_interval,
|
||||||
"Ui_scaling": ui_scaling,
|
"ui_scaling": ui_scaling,
|
||||||
}
|
}
|
||||||
self.settings = settings
|
self.settings = settings
|
||||||
self.system = None
|
self.system = None
|
||||||
self.overlay = None
|
self.overlay = None
|
||||||
self.handle = None
|
self.handle = None
|
||||||
# self.image_queue = Queue()
|
|
||||||
self.lastUpdate = time.monotonic()
|
self.lastUpdate = time.monotonic()
|
||||||
self.thread_overlay = None
|
self.thread_overlay = None
|
||||||
|
self.fadeRatio = 1
|
||||||
|
self.loop = True
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
try:
|
try:
|
||||||
@@ -44,76 +46,62 @@ class Overlay:
|
|||||||
self.handle = self.overlay.createOverlay("Overlay_Speaker2log", "SOverlay_Speaker2log_UI")
|
self.handle = self.overlay.createOverlay("Overlay_Speaker2log", "SOverlay_Speaker2log_UI")
|
||||||
|
|
||||||
self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
|
self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
|
||||||
self.setColor(self.settings['Color'])
|
self.updateColor(self.settings["color"])
|
||||||
self.updateColor()
|
self.updateOpacity(self.settings["opacity"])
|
||||||
self.setTransparency(self.settings['Transparency'])
|
self.updateUiScaling(self.settings["Ui_scaling"])
|
||||||
self.updateTransparency()
|
self.updatePosition(
|
||||||
self.setUiScaling(self.settings['Ui_scaling'])
|
(self.settings["x_pos"], self.settings["y_pos"]),
|
||||||
self.updateUiScaling()
|
self.settings["depth"]
|
||||||
self.setPosition((self.settings["Normalized_icon_X_position"], self.settings["Normalized_icon_Y_position"]))
|
)
|
||||||
self.updatePosition()
|
|
||||||
self.overlay.showOverlay(self.handle)
|
self.overlay.showOverlay(self.handle)
|
||||||
self.initialized = True
|
self.initialized = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Could not initialise OpenVR", e)
|
print("Could not initialise OpenVR", e)
|
||||||
|
|
||||||
# def setImage(self, img):
|
|
||||||
# self.image_queue.put(img)
|
|
||||||
|
|
||||||
def updateImage(self, img):
|
def updateImage(self, img):
|
||||||
# _ = self.image_queue.get()
|
|
||||||
# img = Image.open(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "img", "test_chatbox.png"))
|
|
||||||
width, height = img.size
|
width, height = img.size
|
||||||
img = img.tobytes()
|
img = img.tobytes()
|
||||||
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)
|
||||||
self.updateTransparency()
|
self.updateOpacity(self.settings["opacity"])
|
||||||
self.lastUpdate = time.monotonic()
|
self.lastUpdate = time.monotonic()
|
||||||
|
|
||||||
def clearImage(self):
|
def clearImage(self):
|
||||||
# while self.image_queue.empty() is False:
|
|
||||||
# self.image_queue.get()
|
|
||||||
self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
|
self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
|
||||||
|
|
||||||
def setColor(self, col):
|
def updateColor(self, col):
|
||||||
"""
|
"""
|
||||||
col is a 3-tuple representing (r, g, b)
|
col is a 3-tuple representing (r, g, b)
|
||||||
"""
|
"""
|
||||||
self.settings["Color"] = col
|
self.settings["color"] = col
|
||||||
|
r, g, b = self.settings["color"]
|
||||||
def updateColor(self):
|
|
||||||
r, g, b = self.settings["Color"]
|
|
||||||
self.overlay.setOverlayColor(self.handle, r, g, b)
|
self.overlay.setOverlayColor(self.handle, r, g, b)
|
||||||
|
|
||||||
def setTransparency(self, transparency):
|
def updateOpacity(self, opacity, with_fade=False):
|
||||||
self.settings['Transparency'] = transparency
|
self.settings["opacity"] = opacity
|
||||||
|
self.overlay.setOverlayAlpha(
|
||||||
|
self.handle,
|
||||||
|
self.settings["opacity"] if with_fade is False else self.settings["opacity"] * self.fadeRatio)
|
||||||
|
|
||||||
def updateTransparency(self):
|
def updateUiScaling(self, ui_scaling):
|
||||||
self.overlay.setOverlayAlpha(self.handle, self.settings['Transparency'])
|
self.settings['ui_scaling'] = ui_scaling
|
||||||
|
self.overlay.setOverlayWidthInMeters(self.handle, self.settings['ui_scaling'])
|
||||||
|
|
||||||
def setUiScaling(self, ui_scaling):
|
def updatePosition(self, pos, depth):
|
||||||
self.settings['Ui_scaling'] = ui_scaling
|
|
||||||
|
|
||||||
def updateUiScaling(self):
|
|
||||||
self.overlay.setOverlayWidthInMeters(self.handle, self.settings['Ui_scaling'])
|
|
||||||
|
|
||||||
def setPosition(self, pos):
|
|
||||||
"""
|
"""
|
||||||
pos is a 2-tuple representing normalized (x, y)
|
pos is a 2-tuple representing normalized (x, y)
|
||||||
|
depth is a float representing the depth of the icon plane
|
||||||
"""
|
"""
|
||||||
self.settings["Normalized_icon_X_position"] = pos[0]
|
self.settings["x_pos"] = pos[0]
|
||||||
self.settings["Normalized_icon_Y_position"] = pos[1]
|
self.settings["y_pos"] = pos[1]
|
||||||
|
self.settings["depth"] = depth
|
||||||
|
|
||||||
def setDepth(self, depth):
|
|
||||||
self.settings["Icon_plane_depth"] = depth
|
|
||||||
|
|
||||||
def updatePosition(self):
|
|
||||||
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] = self.settings["Normalized_icon_X_position"] * self.settings['Icon_plane_depth']
|
self.transform[0][3] = self.settings["x_pos"] * self.settings['depth']
|
||||||
self.transform[1][3] = self.settings["Normalized_icon_Y_position"] * self.settings['Icon_plane_depth']
|
self.transform[1][3] = self.settings["y_pos"] * self.settings['depth']
|
||||||
self.transform[2][3] = - self.settings['Icon_plane_depth']
|
self.transform[2][3] = - self.settings['depth']
|
||||||
|
|
||||||
self.overlay.setOverlayTransformTrackedDeviceRelative(
|
self.overlay.setOverlayTransformTrackedDeviceRelative(
|
||||||
self.handle,
|
self.handle,
|
||||||
@@ -121,11 +109,11 @@ class Overlay:
|
|||||||
self.transform
|
self.transform
|
||||||
)
|
)
|
||||||
|
|
||||||
def setFadeTime(self, fade_time):
|
def updateDisplayDuration(self, display_duration):
|
||||||
self.settings['Fade_time'] = fade_time
|
self.settings['display_duration'] = display_duration
|
||||||
|
|
||||||
def setFadeInterval(self, fade_interval):
|
def updateFadeoutDuration(self, fadeout_duration):
|
||||||
self.settings['Fade_interval'] = fade_interval
|
self.settings['fadeout_duration'] = fadeout_duration
|
||||||
|
|
||||||
def checkActive(self):
|
def checkActive(self):
|
||||||
try:
|
try:
|
||||||
@@ -140,32 +128,30 @@ class Overlay:
|
|||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def evaluateTransparencyFade(self, lastUpdate, currentTime):
|
def evaluateOpacityFade(self, lastUpdate, currentTime):
|
||||||
if (currentTime - lastUpdate) > self.settings['Fade_time']:
|
if (currentTime - lastUpdate) > self.settings['display_duration']:
|
||||||
timeThroughInterval = currentTime - lastUpdate - self.settings['Fade_time']
|
timeThroughInterval = currentTime - lastUpdate - self.settings['display_duration']
|
||||||
self.fadeRatio = 1 - timeThroughInterval / self.settings['Fade_interval']
|
self.fadeRatio = 1 - timeThroughInterval / self.settings['fadeout_duration']
|
||||||
if self.fadeRatio < 0:
|
if self.fadeRatio < 0:
|
||||||
self.fadeRatio = 0
|
self.fadeRatio = 0
|
||||||
self.overlay.setOverlayAlpha(self.handle, self.fadeRatio * self.settings['Transparency'])
|
self.overlay.setOverlayAlpha(self.handle, self.fadeRatio * self.settings['opacity'])
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
# self.updateUiScaling()
|
|
||||||
# self.updatePosition()
|
|
||||||
|
|
||||||
currTime = time.monotonic()
|
currTime = time.monotonic()
|
||||||
if self.settings['Fade_interval'] != 0:
|
if self.settings['fadeout_duration'] != 0:
|
||||||
self.evaluateTransparencyFade(self.lastUpdate, currTime)
|
self.evaluateOpacityFade(self.lastUpdate, currTime)
|
||||||
else:
|
else:
|
||||||
self.updateTransparency()
|
self.updateOpacity(self.settings["opacity"])
|
||||||
|
|
||||||
def mainloop(self):
|
def mainloop(self):
|
||||||
while self.checkActive() is True:
|
self.loop = True
|
||||||
|
while self.checkActive() is True and self.loop is True:
|
||||||
startTime = time.monotonic()
|
startTime = time.monotonic()
|
||||||
self.update()
|
self.update()
|
||||||
sleepTime = (1 / 16) - (time.monotonic() - startTime)
|
sleepTime = (1 / 16) - (time.monotonic() - startTime)
|
||||||
if sleepTime > 0:
|
if sleepTime > 0:
|
||||||
time.sleep(sleepTime)
|
time.sleep(sleepTime)
|
||||||
self.shutdown()
|
self.shutdownOverlay()
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
self.init()
|
self.init()
|
||||||
@@ -177,9 +163,12 @@ class Overlay:
|
|||||||
self.thread_overlay.daemon = True
|
self.thread_overlay.daemon = True
|
||||||
self.thread_overlay.start()
|
self.thread_overlay.start()
|
||||||
|
|
||||||
def shutdown(self):
|
def setStopOverlay(self):
|
||||||
|
self.loop = False
|
||||||
|
|
||||||
|
def shutdownOverlay(self):
|
||||||
if self.thread_overlay is not None:
|
if self.thread_overlay is not None:
|
||||||
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.thread_overlay.ident), ctypes.py_object(SystemExit))
|
self.thread_overlay.join()
|
||||||
self.thread_overlay = None
|
self.thread_overlay = None
|
||||||
if self.overlay is not None:
|
if self.overlay is not None:
|
||||||
self.overlay.destroyOverlay(self.handle)
|
self.overlay.destroyOverlay(self.handle)
|
||||||
|
|||||||
Reference in New Issue
Block a user