1
1
from easycoder import Object , FatalError , RuntimeError
2
2
from easycoder import Handler
3
+ from easycoder import getConstant
3
4
from easycoder .ec_screenspec import ScreenSpec
4
5
from easycoder .ec_renderer import getActual , getUI
5
6
import json
@@ -21,16 +22,74 @@ def getName(self):
21
22
# Create a keyboard
22
23
def k_create (self , command ):
23
24
if self .nextIs ('keyboard' ):
24
- command ['style' ] = self .nextValue ()
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
25
75
self .add (command )
26
76
return True
27
77
return False
28
78
29
79
def r_create (self , command ):
30
80
self .keyboard = Object ()
31
- style = self .getRuntimeValue (command ['style ' ])
32
- with open (f'plugins/keyboards/ { style } .json ' ) as f : s = f .read ()
81
+ template = self .getRuntimeValue (command ['template ' ])
82
+ with open (f'{ template } ' ) as f : s = f .read ()
33
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' ])
34
93
return self .nextPC ()
35
94
36
95
# on click/tap keyboard
@@ -60,26 +119,24 @@ def k_on(self, command):
60
119
self .getCommandAt (pcNext )['goto' ] = self .getPC ()
61
120
return True
62
121
return False
63
-
122
+
64
123
# Set a handler
65
124
def r_on (self , command ):
66
125
self .onTap = command ['goto' ]
67
126
return self .nextPC ()
68
127
69
128
# Render a keyboard
70
- # render keyboard at {left} {bottom} width {width} using {button image}
129
+ # render keyboard at {left} {bottom} width {width}
71
130
def k_render (self , command ):
72
131
if self .nextIs ('keyboard' ):
73
132
token = self .peek ()
74
- while token in ['at' , 'width' , 'using' ]:
133
+ while token in ['at' , 'width' ]:
75
134
self .nextToken ()
76
135
if token == 'at' :
77
136
command ['x' ] = self .nextValue ()
78
137
command ['y' ] = self .nextValue ()
79
138
elif token == 'width' :
80
139
command ['w' ] = self .nextValue ()
81
- elif token == 'using' :
82
- command ['u' ] = self .nextValue ()
83
140
token = self .peek ()
84
141
self .add (command )
85
142
return True
@@ -89,7 +146,6 @@ def r_render(self, command):
89
146
x = getActual (self .getRuntimeValue (command ['x' ]))
90
147
y = getActual (self .getRuntimeValue (command ['y' ]))
91
148
w = getActual (self .getRuntimeValue (command ['w' ]))
92
- u = self .getRuntimeValue (command ['u' ])
93
149
# Scan the keyboard layout to find the longest row
94
150
max = 0
95
151
nrows = len (self .keyboard .layout )
@@ -111,19 +167,21 @@ def r_render(self, command):
111
167
for b in range (0 , len (row )):
112
168
button = row [b ]
113
169
id = button ['id' ]
114
- button ['type' ] = 'ellipse'
170
+ button ['type' ] = self .keyboard .buttonStyle
171
+ button ['source' ] = self .keyboard .buttonTemplate
115
172
button ['left' ] = bx
116
173
button ['bottom' ] = by
117
174
button ['width' ] = bs
118
175
button ['height' ] = bs
119
- button ['fill' ] = 'magenta'
176
+ button ['fill' ] = self . keyboard . buttonColor
120
177
label = {}
121
178
label ['type' ] = 'text'
122
- label ['left' ] = '25w'
123
- label ['bottom' ] = '25h'
124
- label ['width' ] = '50w'
125
- label ['height' ] = '50h'
179
+ label ['left' ] = self . keyboard . buttonTextX
180
+ label ['bottom' ] = self . keyboard . buttonTextY
181
+ label ['width' ] = self . keyboard . buttonTextWidth
182
+ label ['height' ] = self . keyboard . buttonTextHeight
126
183
label ['text' ] = id
184
+ label ['color' ] = self .keyboard .buttonTextColor
127
185
button ['#' ] = 'Label'
128
186
button ['Label' ] = label
129
187
buttons .append (button )
@@ -135,6 +193,7 @@ def r_render(self, command):
135
193
for n in range (0 , len (list )):
136
194
spec [list [n ]] = buttons [n ]
137
195
196
+ spec ['font' ] = self .keyboard .buttonFont
138
197
try :
139
198
ScreenSpec ().render (spec , None )
140
199
except Exception as e :
0 commit comments