summaryrefslogtreecommitdiffstats
path: root/DummyRig.py
blob: 093a655e855a531c6bc74d1c7f2f837a73b7ada8 (plain) (blame)
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
69
70
71
72
from Tkinter import *
import re
import threading
import time

def rgbToTkColor(r, g, b):
    if not (0 <= r <= 1 and 0 <= g <= 1 and 0 <= b <= 1):
        print "invalid r={}, g={}, b={}".format(r, g, b)
        assert(False)

    r_hex = int(r * 0xFF)
    g_hex = int(g * 0xFF)
    b_hex = int(b * 0xFF)
    return "#{:02x}{:02x}{:02x}".format(r_hex, g_hex, b_hex)

class DummyLight(object):
    WIDTH_PX = 8
    HEIGHT_PX = 20

    def __init__(self, canvas):
        self.canvas = canvas
        self.color = (0, 0, 0)

    def setRGBRaw(self, r, g, b):
        self.color = (r, g, b)
        self.canvas.itemconfig(self.rect_id, fill=rgbToTkColor(*self.color))


class DummyRig(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.margin = 5

    def init(self):
        self.root = Tk()
        self.canvas = Canvas(self.root,
                             width=self.width*DummyLight.WIDTH_PX + self.margin*2,
                             height=self.height*DummyLight.HEIGHT_PX + self.margin*2)
        self.canvas.pack()
        self.root.resizable(width=0, height=0)

        self.lights = [[DummyLight(self.canvas) for _ in xrange(self.height)]
                       for _ in xrange(self.width)]

        self.rootThread = threading.Thread(target=self.root.mainloop)
        self.rootThread.start()

        self.initRects()

    def select(self, pattern):
        match = re.search("\$side=(.*?)\[\$sequence=(.*?)\]", pattern)
        if match.group(1) == "bot":
            y = 1
        elif match.group(1) == "top":
            y = 0

        x = int(match.group(2)) - 1
        return self.lights[x][y]

    def updateOnce(self):
        pass

    def initRects(self):
        for x in xrange(self.width):
            for y in xrange(self.height):
                light = self.lights[x][y]
                x0 = self.margin + x * DummyLight.WIDTH_PX
                y0 = self.margin + y * DummyLight.HEIGHT_PX
                light.rect_id = self.canvas.create_rectangle(x0, y0, x0 + DummyLight.WIDTH_PX,
                                                     y0 + DummyLight.HEIGHT_PX,
                                                     width=1)