@@ -19,79 +19,6 @@ def getName(self):
19
19
#############################################################################
20
20
# Keyword handlers
21
21
22
- # Create a keyboard
23
- def k_create (self , command ):
24
- if self .nextIs ('keyboard' ):
25
- command ['template' ] = self .nextValue ()
26
- buttonStyle = 'ellipse'
27
- buttonTemplate = None
28
- buttonTextWidth = getConstant ('50w' )
29
- buttonTextHeight = getConstant ('50h' )
30
- buttonTextColor = getConstant ('black' )
31
- buttonTextX = getConstant ('center' )
32
- buttonTextY = getConstant ('center' )
33
- buttonColor = getConstant ('white' )
34
- buttonFont = None
35
- while True :
36
- token = self .peek ()
37
- if token == 'button' :
38
- self .nextToken ()
39
- token = self .nextToken ()
40
- if token == 'style' :
41
- token = self .nextToken ()
42
- if token in ['ellipse' , 'rectangle' , 'image' ]:
43
- buttonStyle = token
44
- else : RuntimeError (self .program , f'Unknown style \' token\' ' )
45
- elif token == 'color' :
46
- buttonColor = self .nextValue ()
47
- elif token == 'font' :
48
- buttonFont = self .nextValue ()
49
- elif token == 'template' :
50
- buttonTemplate = self .nextValue ()
51
- elif token == 'text' :
52
- token = self .nextToken ()
53
- if token == 'width' :
54
- buttonTextWidth = self .nextValue ()
55
- elif token == 'height' :
56
- buttonTextHeight = self .nextValue ()
57
- elif token == 'color' :
58
- buttonTextColor = self .nextValue ()
59
- elif token == 'x' :
60
- buttonTextX = self .nextValue ()
61
- elif token == 'y' :
62
- buttonTextY = self .nextValue ()
63
- else : RuntimeError (self .program , f'Unknown property \' token\' ' )
64
- else :
65
- break
66
- command ['button-style' ] = buttonStyle
67
- command ['button-template' ] = buttonTemplate
68
- command ['button-text-width' ] = buttonTextWidth
69
- command ['button-text-height' ] = buttonTextHeight
70
- command ['button-text-color' ] = buttonTextColor
71
- command ['button-text-x' ] = buttonTextX
72
- command ['button-text-y' ] = buttonTextY
73
- command ['button-color' ] = buttonColor
74
- command ['button-font' ] = buttonFont
75
- self .add (command )
76
- return True
77
- return False
78
-
79
- def r_create (self , command ):
80
- self .keyboard = Object ()
81
- template = self .getRuntimeValue (command ['template' ])
82
- with open (f'{ template } ' ) as f : s = f .read ()
83
- self .keyboard .layout = json .loads (s )
84
- self .keyboard .buttonStyle = command ['button-style' ]
85
- self .keyboard .buttonTemplate = self .getRuntimeValue (command ['button-template' ])
86
- self .keyboard .buttonTextWidth = self .getRuntimeValue (command ['button-text-width' ])
87
- self .keyboard .buttonTextHeight = self .getRuntimeValue (command ['button-text-height' ])
88
- self .keyboard .buttonTextColor = self .getRuntimeValue (command ['button-text-color' ])
89
- self .keyboard .buttonTextX = self .getRuntimeValue (command ['button-text-x' ])
90
- self .keyboard .buttonTextY = self .getRuntimeValue (command ['button-text-y' ])
91
- self .keyboard .buttonColor = self .getRuntimeValue (command ['button-color' ])
92
- self .keyboard .buttonFont = self .getRuntimeValue (command ['button-font' ])
93
- return self .nextPC ()
94
-
95
22
# on click/tap keyboard
96
23
def k_on (self , command ):
97
24
token = self .nextToken ()
@@ -126,74 +53,90 @@ def r_on(self, command):
126
53
return self .nextPC ()
127
54
128
55
# Render a keyboard
129
- # render keyboard at {left} {bottom} width {width}
56
+ # render keyboard {layout) at {left} {bottom} width {width}
130
57
def k_render (self , command ):
131
58
if self .nextIs ('keyboard' ):
59
+ command ['layout' ] = self .nextValue ()
60
+ x = getConstant ('10w' )
61
+ y = getConstant ('10h' )
62
+ w = getConstant ('50w' )
132
63
token = self .peek ()
133
64
while token in ['at' , 'width' ]:
134
65
self .nextToken ()
135
66
if token == 'at' :
136
- command [ 'x' ] = self .nextValue ()
137
- command [ 'y' ] = self .nextValue ()
67
+ x = self .nextValue ()
68
+ y = self .nextValue ()
138
69
elif token == 'width' :
139
- command [ 'w' ] = self .nextValue ()
70
+ w = self .nextValue ()
140
71
token = self .peek ()
72
+ command ['x' ] = x
73
+ command ['y' ] = y
74
+ command ['w' ] = w
141
75
self .add (command )
142
76
return True
143
77
return False
144
78
145
79
def r_render (self , command ):
80
+ self .keyboard = Object ()
81
+ layout = self .getRuntimeValue (command ['layout' ])
82
+ with open (f'{ layout } ' ) as f : spec = f .read ()
83
+ self .keyboard .layout = json .loads (spec )
84
+ layout = self .keyboard .layout [0 ]
146
85
x = getActual (self .getRuntimeValue (command ['x' ]))
147
86
y = getActual (self .getRuntimeValue (command ['y' ]))
148
87
w = getActual (self .getRuntimeValue (command ['w' ]))
149
88
# Scan the keyboard layout to find the longest row
150
89
max = 0
151
- nrows = len (self .keyboard .layout )
90
+ rows = self .keyboard .layout [0 ]['rows' ]
91
+ nrows = len (rows )
152
92
for r in range (0 , nrows ):
153
- row = self . keyboard . layout [r ]
93
+ row = rows [r ]
154
94
# Count the number of buttons
155
- if len (row ) > max : max = len (row )
95
+ count = 0.0
96
+ for n in range (0 , len (row )):
97
+ key = row [n ]
98
+ if 'span' in key : count += float (key ['span' ])
99
+ else : count += 1.0
100
+ if count > max : max = count
156
101
# Divide the keyboard width by the number of buttons to get the button size
102
+ # The basic key is always a square
157
103
bs = w / max
158
104
# Compute the keyboard height
159
105
h = bs * nrows
160
106
# Build the spec
107
+ spec = {}
108
+ spec ['type' ] = 'image'
109
+ spec ['id' ] = 'face'
110
+ spec ['source' ] = layout ['face' ]
111
+ spec ['left' ] = x
112
+ spec ['bottom' ] = y
113
+ spec ['width' ] = w
114
+ spec ['height' ] = h
161
115
buttons = []
162
116
list = []
163
- by = y
164
- for r in reversed (range (0 , nrows )):
165
- row = self .keyboard .layout [r ]
166
- bx = x
117
+ by = h
118
+ for r in range (0 , nrows ):
119
+ by -= bs
120
+ row = rows [r ]
121
+ bx = 0
167
122
for b in range (0 , len (row )):
168
123
button = row [b ]
169
124
id = button ['id' ]
170
- button ['type' ] = self .keyboard .buttonStyle
171
- button ['source' ] = self .keyboard .buttonTemplate
125
+ if 'span' in button : span = float (button ['span' ])
126
+ else : span = 1.0
127
+ width = bs * span
128
+ button ['type' ] = 'hotspot'
172
129
button ['left' ] = bx
173
130
button ['bottom' ] = by
174
- button ['width' ] = bs
131
+ button ['width' ] = width
175
132
button ['height' ] = bs
176
- button ['fill' ] = self .keyboard .buttonColor
177
- label = {}
178
- label ['type' ] = 'text'
179
- label ['left' ] = self .keyboard .buttonTextX
180
- label ['bottom' ] = self .keyboard .buttonTextY
181
- label ['width' ] = self .keyboard .buttonTextWidth
182
- label ['height' ] = self .keyboard .buttonTextHeight
183
- label ['text' ] = id
184
- label ['color' ] = self .keyboard .buttonTextColor
185
- button ['#' ] = 'Label'
186
- button ['Label' ] = label
133
+ button ['parent' ] = spec
187
134
buttons .append (button )
188
135
list .append (id )
189
- bx += bs
190
- by += bs
191
- spec = {}
136
+ bx += width
192
137
spec ['#' ] = list
193
138
for n in range (0 , len (list )):
194
139
spec [list [n ]] = buttons [n ]
195
-
196
- spec ['font' ] = self .keyboard .buttonFont
197
140
try :
198
141
ScreenSpec ().render (spec , None )
199
142
except Exception as e :
0 commit comments