summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/sdl.py
blob: 8131a5345682f2364e7dd7c604991eb431f93fd5 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sdl2.ext
from sdl2 import *
import os
import time

Colors = {
    'red': sdl2.ext.Color(255, 0, 0, 255),
    'green': sdl2.ext.Color(0, 255, 0, 255),
    'blue': sdl2.ext.Color(0, 0, 255, 255),
    'white': sdl2.ext.Color(255, 255, 255, 255),
    'black': sdl2.ext.Color(0, 0, 0, 255),
}


class SDL2(object):
    __resources = None
    __w = 1280
    __h = 720
    __winName = "noname"
    __window = None
    __factory = None
    __renderer = None
    __srender = None

    def __init__(self, driver, display, w, h):
        os.environ['SDL_VIDEODRIVER'] = "x11"
        os.environ['DISPLAY'] = ":0"
        # self.__resources = sdl2.ext.Resources(parent.getAppPath(), 'files')
        sdl2.ext.init()
        self.__createWindow()

    def __createWindow(self):
        self.__window = sdl2.ext.Window(title='{}'.format(self.__winName), size=(self.__w, self.__h), position=(0, 0),
                                        flags=(
                                                SDL_WINDOW_HIDDEN | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS | SDL_WINDOW_MAXIMIZED))
        self.__renderer = sdl2.ext.Renderer(self.__window)
        self.__factory = sdl2.ext.SpriteFactory(sdl2.ext.TEXTURE, renderer=self.__renderer)
        self.__srender = self.__factory.create_sprite_render_system(self.__window)
        self.__window.show()
        SDL_ShowCursor(SDL_DISABLE)

    # def createFont(self, fontName, fontSize, fontColor):
    #     return sdl2.ext.FontManager(font_path=self.__resources.get_path(fontName), size=fontSize, color=fontColor)

    def WriteText(self, text, x, y, fontManager):
        imText = self.__factory.from_text(text, fontmanager=fontManager)
        self.__renderer.copy(imText, dstrect=(x, y, imText.size[0], imText.size[1]))

    def show(self):
        self.__window.show()

    def hide(self):
        self.__window.hide()

    def setColor(self, red, green, blue, alpha):
        self.__renderer.color = sdl2.ext.Color(red, green, blue, alpha)

    def fillColor(self, red, green, blue, alpha):
        self.setColor(red, green, blue, alpha)
        self.__renderer.clear()

    def fillbgColor(self, color, update=False):
        if color in Colors:
            self.__renderer.color = Colors[color]
            self.__renderer.clear()
            if update:
                self.update()

    def update(self):
        self.__renderer.present()

    # def showImage(self, filename):
    #     im = self.__factory.from_image(self.__resources.get_path(filename))
    #     self.__srender.render(im)


class SDL2_Test(object):
    __sdl2 = None

    def __init__(self, driver, display, w, h):
        self.__sdl2 = SDL2(driver, display, w, h)

    def Clear(self):
        self.__sdl2.fillbgColor('black', True)

    def Paint(self, dcolor):
        self.Clear()
        self.__sdl2.fillbgColor(dcolor.lower(), True)


class SDL2_Message(object):
    __sdl2 = None
    __fontManager_w = None
    __fontManager_b = None

    def __init__(self):
        self.__sdl2 = SDL2()
        self.__fontManager_w = self.__sdl2.createFont('OpenSans-Regular.ttf', 24, Colors['white'])
        self.__fontManager_b = self.__sdl2.createFont('OpenSans-Regular.ttf', 24, Colors['black'])

    def Clear(self):
        self.__sdl2.fillbgColor('black', True)

    def Paint(self, dcolor):
        self.Clear()
        self.__sdl2.fillbgColor(dcolor.lower(), True)

    def __write_w(self, x, y, text):
        self.__sdl2.WriteText(text, x, y, self.__fontManager_w)

    def __write_b(self, x, y, text):
        self.__sdl2.WriteText(text, x, y, self.__fontManager_b)

    def setTestOK(self, board_uuid, qrdata):
        self.Paint('GREEN')
        self.__write_b(100, 50, 'TEST OK')
        self.__write_b(100, 100, 'uuid={}'.format(board_uuid))
        self.__write_b(100, 150, 'qr={}'.format(qrdata))
        self.__sdl2.update()

    def setTestERROR(self, error):
        self.Paint('RED')
        self.__write_w(100, 50, 'TEST FAILED')
        self.__write_w(100, 100, '{}'.format(error))
        self.__sdl2.update()