|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Node: |
|
|
class Node: |
|
|
def __init__(self, name="Node", parent=None): |
|
|
def __init__(self, name="Node", parent=None): |
|
|
|
|
|
self._parent = None |
|
|
|
|
|
self._name = name |
|
|
self._children = [] |
|
|
self._children = [] |
|
|
self.name = name |
|
|
|
|
|
|
|
|
if parent is not None: |
|
|
|
|
|
try: |
|
|
|
|
|
self.parent = parent |
|
|
|
|
|
except NodeError as e: |
|
|
|
|
|
raise e |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def type(self): |
|
|
def type(self): |
|
|
|
|
|
|
|
|
return self._parent |
|
|
return self._parent |
|
|
|
|
|
|
|
|
@parent.setter |
|
|
@parent.setter |
|
|
def parent(self, value): |
|
|
|
|
|
|
|
|
def parent(self, new_parent): |
|
|
try: |
|
|
try: |
|
|
self.parent_to_node(value) |
|
|
|
|
|
|
|
|
self.parent_to_node(new_parent) |
|
|
except NodeError as e: |
|
|
except NodeError as e: |
|
|
raise e |
|
|
raise e |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@name.setter |
|
|
@name.setter |
|
|
def name(self, value): |
|
|
def name(self, value): |
|
|
|
|
|
if self._parent is not None: |
|
|
|
|
|
if self._parent.get_node(value) is not None: |
|
|
|
|
|
raise NodeError("Parent already contains node named '{}'.".format(name)) |
|
|
self._name = value |
|
|
self._name = value |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
|
|
|
|
|
|
def parent_to_node(self, parent, allow_reparenting=False): |
|
|
def parent_to_node(self, parent, allow_reparenting=False): |
|
|
if not isinstance(value, Node): |
|
|
if not isinstance(value, Node): |
|
|
raise NodeError("Node may only parent to another Node instance.") |
|
|
raise NodeError("Node may only parent to another Node instance.") |
|
|
if self._parent is None: |
|
|
|
|
|
self._parent = value |
|
|
|
|
|
elif self._parent != value: |
|
|
|
|
|
if allow_Reparenting == False: |
|
|
|
|
|
raise NodeError("Node already assigned a parent Node.") |
|
|
|
|
|
if self._parent.remove_node(self) != self: |
|
|
|
|
|
raise NodeError("Failed to remove self from current parent.") |
|
|
|
|
|
parent.attach_node(self) |
|
|
|
|
|
|
|
|
if self._parent is None or self._parent != parent: |
|
|
|
|
|
if self._parent is not None: |
|
|
|
|
|
if allow_Reparenting == False: |
|
|
|
|
|
raise NodeError("Node already assigned a parent Node.") |
|
|
|
|
|
if self._parent.remove_node(self) != self: |
|
|
|
|
|
raise NodeError("Failed to remove self from current parent.") |
|
|
|
|
|
try: |
|
|
|
|
|
parent.attach_node(self) |
|
|
|
|
|
except NodeError as e: |
|
|
|
|
|
raise e |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def attach_node(self, node, reparent=False, index=-1): |
|
|
def attach_node(self, node, reparent=False, index=-1): |
|
|
|
|
|
|
|
|
raise NodeError("Node already parented.") |
|
|
raise NodeError("Node already parented.") |
|
|
if node.parent.remove_node(node) != node: |
|
|
if node.parent.remove_node(node) != node: |
|
|
raise NodeError("Failed to remove given node from it's current parent.") |
|
|
raise NodeError("Failed to remove given node from it's current parent.") |
|
|
|
|
|
if self.get_node(node.name) is not None: |
|
|
|
|
|
raise NodeError("Node with name '{}' already attached.".format(node.name)) |
|
|
node._parent = self |
|
|
node._parent = self |
|
|
if index < 0 or index >= len(self._children): |
|
|
if index < 0 or index >= len(self._children): |
|
|
self._children.append(node) |
|
|
self._children.append(node) |
|
|
|
|
|
|
|
|
return None |
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _update(self, dt): |
|
|
|
|
|
if hasattr(self, "on_update"): |
|
|
|
|
|
self.on_update(dt) |
|
|
|
|
|
|
|
|
|
|
|
for c in self._children: |
|
|
|
|
|
c._update(dt) |
|
|
|
|
|
|
|
|
|
|
|
def _render(self, surface): |
|
|
|
|
|
for c in self._children: |
|
|
|
|
|
c._render(surface) |
|
|
|
|
|
|
|
|
|
|
|
if hasattr(self, "on_render"): |
|
|
|
|
|
self.on_render(surface) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeSurface(Node): |
|
|
|
|
|
def __init__(self, name="NodeSurface", parent=None, display_ref=None): |
|
|
|
|
|
try: |
|
|
|
|
|
Node.__init__(self, name, parent) |
|
|
|
|
|
except NodeError as e: |
|
|
|
|
|
raise e |
|
|
|
|
|
self._surface = None |