156 lines
4.4 KiB
Python
156 lines
4.4 KiB
Python
from juce_init import START_JUCE_COMPONENT
|
|
import popsicle as juce
|
|
import time
|
|
|
|
|
|
class ChangeRobot(juce.Component, juce.Timer):
|
|
backgroundColour = juce.Colours.lightblue
|
|
textColour = juce.Colours.black
|
|
|
|
robots = []
|
|
|
|
robotsLabel = juce.Label("", "Выберите робота")
|
|
robotsRadio = []
|
|
robotsControls = [robotsLabel] + robotsRadio
|
|
robotButtonsId = 1001
|
|
|
|
connectButton = juce.TextButton("Соединить")
|
|
|
|
old_status = None
|
|
counter = 0
|
|
|
|
def __init__(self, robots, updateRobot, status):
|
|
super().__init__()
|
|
juce.Timer.__init__(self)
|
|
|
|
self.robots = robots or []
|
|
self.updateRobot = updateRobot
|
|
self.status = status
|
|
|
|
for r in self.robots:
|
|
self.robotsRadio.append(
|
|
juce.ToggleButton(r["name"]),
|
|
)
|
|
self.robotsControls = [self.robotsLabel] + self.robotsRadio
|
|
|
|
for s in self.robotsControls:
|
|
self.addAndMakeVisible(s)
|
|
|
|
for s in self.robotsRadio:
|
|
s.onClick = self.setConnectText
|
|
s.setRadioGroupId(self.robotButtonsId, juce.dontSendNotification)
|
|
s.setClickingTogglesState(True)
|
|
|
|
self.addAndMakeVisible(self.connectButton)
|
|
self.connectButton.onClick = self.connectRobot
|
|
|
|
def paint(self, g: juce.Graphics):
|
|
g.fillAll(self.backgroundColour)
|
|
|
|
for i, s in enumerate(self.robotsControls):
|
|
s.setColour(s.textColourId, self.textColour)
|
|
if hasattr(s, "tickDisabledColourId"):
|
|
s.setColour(s.tickColourId, self.textColour)
|
|
s.setColour(s.tickDisabledColourId, self.textColour)
|
|
|
|
s.setBounds(10 if i == 0 else 20, (i * 20), self.getWidth() - 20, 20)
|
|
|
|
h = i + 2
|
|
self.connectButton.setBounds(10, (h * 20), self.getWidth() - 20, 20)
|
|
|
|
def setConnectText(self):
|
|
if self.status() == "not_connected":
|
|
self.connectButton.setButtonText("Connect")
|
|
if self.status() == "connected":
|
|
self.connectButton.setButtonText("Disconnect")
|
|
|
|
def timerCallback(self):
|
|
new_status = self.status()
|
|
self.counter += 1
|
|
if new_status == self.old_status and self.counter <= 10:
|
|
self.connectButton.setButtonText(self.connectButton.getButtonText() + ".")
|
|
else:
|
|
self.old_status = None
|
|
self.stopTimer()
|
|
self.setConnectText()
|
|
|
|
def connectRobot(self):
|
|
self.setConnectText()
|
|
res = None
|
|
for i, s in enumerate(self.robotsRadio):
|
|
if s.getToggleState() == True:
|
|
res = i
|
|
break
|
|
|
|
if res == None:
|
|
self.connectButton.setButtonText("Выберите робота")
|
|
return
|
|
|
|
self.old_status = self.status()
|
|
self.startTimer(500)
|
|
time.sleep(0.3)
|
|
self.updateRobot(self.robots[i])
|
|
|
|
|
|
class RightPanel(juce.Component):
|
|
def __init__(self, panels):
|
|
super().__init__()
|
|
self.panels = panels
|
|
|
|
for p in self.panels:
|
|
self.addAndMakeVisible(p)
|
|
|
|
def paint(self, g):
|
|
g.fillAll(juce.Colours.blue)
|
|
|
|
def resized(self):
|
|
bounds = self.getLocalBounds()
|
|
|
|
for p in self.panels:
|
|
p.setBounds(
|
|
bounds.removeFromTop(self.proportionOfHeight(1 / len(self.panels)))
|
|
)
|
|
pass
|
|
|
|
|
|
class MainPanel(juce.Component):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def paint(self, g):
|
|
g.fillAll(juce.Colours.hotpink)
|
|
|
|
|
|
class MainContentComponent(juce.Component):
|
|
def __init__(self, **kwargs):
|
|
super().__init__()
|
|
|
|
robotArgs = kwargs.get("robotPanel")
|
|
robotPanel = ChangeRobot(**robotArgs)
|
|
self.rightPanel = RightPanel(panels=[robotPanel])
|
|
self.mainPanel = MainPanel()
|
|
|
|
self.addAndMakeVisible(self.rightPanel)
|
|
self.addAndMakeVisible(self.mainPanel)
|
|
|
|
self.setSize(800, 600)
|
|
# self.setResizable(False, False)
|
|
|
|
def paint(self, g):
|
|
g.fillAll(
|
|
self.getLookAndFeel().findColour(juce.ResizableWindow.backgroundColourId)
|
|
)
|
|
|
|
def resized(self):
|
|
bounds = self.getLocalBounds()
|
|
self.rightPanel.setBounds(
|
|
bounds.removeFromRight(
|
|
self.proportionOfWidth(0.25),
|
|
),
|
|
)
|
|
self.mainPanel.setBounds(bounds)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
START_JUCE_COMPONENT(MainContentComponent, name="ROBOT GUI")
|