Spaces:
Running
Running
File size: 2,164 Bytes
3bb804c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import __main__
BaseNode = __main__.BaseNode
QtGui = __main__.QtGui
import cv2
import numpy as np
class ButtonNode(BaseNode):
"""
A simple clickable button node.
"""
NODE_CATEGORY = "Input"
NODE_COLOR = QtGui.QColor(200, 200, 100)
def __init__(self, label="Button", mode="Toggle"):
super().__init__()
self.node_title = "Button"
self.label = str(label)
self.mode = str(mode) # "Toggle" or "Hold"
self.inputs = {}
self.outputs = {'signal_out': 'signal'}
self.is_pressed = False
self.value = 0.0
def get_output(self, port_name):
if port_name == 'signal_out':
return self.value
return None
def step(self):
if self.mode == "Hold":
self.value = 1.0 if self.is_pressed else 0.0
# For "Toggle", value is changed in mousePressEvent
def mousePressEvent(self, event):
self.is_pressed = True
if self.mode == "Toggle":
self.value = 1.0 - self.value # Flip
self.update_display()
def mouseReleaseEvent(self, event):
self.is_pressed = False
self.update_display()
def get_display_image(self):
w, h = 256, 128
img = np.zeros((h, w, 3), dtype=np.uint8)
if self.value > 0.5:
# Active state
cv2.rectangle(img, (0, 0), (w-1, h-1), (0, 255, 0), -1)
cv2.putText(img, self.label, (w//2 - 4*len(self.label), h//2),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
else:
# Inactive state
cv2.rectangle(img, (5, 5), (w-6, h-6), (100, 100, 100), -1)
cv2.putText(img, self.label, (w//2 - 4*len(self.label), h//2),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
return QtGui.QImage(img.data, w, h, w*3, QtGui.QImage.Format.Format_RGB888)
def get_config_options(self):
return [
("Label", "label", self.label, None),
("Mode", "mode", self.mode, ["Toggle", "Hold"])
] |