Skip to content

Commit

Permalink
add observer method
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHsunTsai committed Jun 26, 2024
1 parent b3b0118 commit a5c0fd6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/compas_viewer/observer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from PySide6.QtCore import QTimer

from compas_viewer.base import Base


class Observer(Base):
def __init__(self):
self._observers = set(
(
self.viewer.renderer,
self.viewer.ui.sidebar,
)
)

self._time = None
self.update_timer = QTimer()
self.update_timer.setSingleShot(True)
self.update_timer.timeout.connect(self.update_observers)
self.debounce_interval = 100

def add_observer(self, observer):
if observer not in self._observers:
self._observers.add(observer)

def remove_observer(self, observer):
if observer in self._observers:
self._observers.remove(observer)

def request_update(self):
if not self.update_timer.isActive():
self.update_timer.start(self.debounce_interval)

def update_observers(self):
for observer in self._observers:
observer.update()
16 changes: 15 additions & 1 deletion src/compas_viewer/scene/sceneobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from compas_viewer.gl import make_vertex_buffer
from compas_viewer.gl import update_index_buffer
from compas_viewer.gl import update_vertex_buffer
from compas_viewer.observer import Observer
from compas_viewer.renderer.shaders import Shader

# Type template of point/line/face data for generating the buffers.
Expand Down Expand Up @@ -101,6 +102,9 @@ def __init__(
):
# Basic
super().__init__(**kwargs)

self.observer = Observer()

self.show = show
self.show_points = show_points if show_points is not None else False
self.show_lines = show_lines if show_lines is not None else True
Expand All @@ -111,7 +115,7 @@ def __init__(

# Selection
self._is_locked = is_locked
self.is_selected = not is_locked and is_selected
self._is_selected = False

# Visual
self.background: bool = False
Expand All @@ -135,6 +139,16 @@ def __init__(

self._inited = False

@property
def is_selected(self):
return self._is_selected

@is_selected.setter
def is_selected(self, value):
if self._is_selected != value:
self._is_selected = value
self.observer.request_update()

@property
def is_locked(self):
return self._is_locked
Expand Down

0 comments on commit a5c0fd6

Please sign in to comment.