Skip to content

Commit 6534fde

Browse files
committed
v250109.1
1 parent 5114a1e commit 6534fde

File tree

10 files changed

+119
-32
lines changed

10 files changed

+119
-32
lines changed
34.7 KB
Binary file not shown.
2.55 MB
Binary file not shown.

easycoder/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
from .ec_timestamp import *
1111
from .ec_value import *
1212

13-
__version__ = "250108.1"
13+
__version__ = "250109.1"

easycoder/ec_renderer.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from kivy.app import App
22
from kivy.uix.widget import Widget
3+
from kivy.graphics import Color, Ellipse, Rectangle
34
from kivy.uix.label import CoreLabel
45
from kivy.uix.image import AsyncImage
56
from kivy.core.window import Window
6-
from kivy.graphics import Color, Ellipse, Rectangle
77
from kivy.utils import colormap
88
from kivy.clock import Clock
99
from kivy.vector import Vector
10-
import math
10+
import math, os
11+
12+
os.environ['KIVY_TEXT'] = 'pango'
1113

1214
ec_ui = None
1315

@@ -114,10 +116,19 @@ def createElement(self, spec):
114116
Color(c[0], c[1], c[2])
115117
else:
116118
Color(c[0]/255, c[1]/255, c[2]/255)
117-
pos = (getActual(spec.pos[0], spec), getActual(spec.pos[1], spec))
118-
spec.realpos = pos
119119
size = (getActual(spec.size[0], spec), getActual(spec.size[1], spec))
120120
spec.realsize = size
121+
# Deal with special case of 'center'
122+
if spec.pos[0] == 'center':
123+
left = getActual('50w', spec) - spec.realsize[0]/2
124+
else:
125+
left = getActual(spec.pos[0], spec)
126+
if spec.pos[1] == 'center':
127+
bottom = getActual('50h', spec) - spec.realsize[1]/2
128+
else:
129+
bottom = getActual(spec.pos[1], spec)
130+
pos = (left, bottom)
131+
spec.realpos = pos
121132
if spec.parent != None:
122133
pos = Vector(pos) + spec.parent.realpos
123134
if spec.type == 'ellipse':
@@ -134,10 +145,12 @@ def createElement(self, spec):
134145
Color(c[0]/255, c[1]/255, c[2]/255)
135146
else:
136147
Color(1, 1, 1, 1)
137-
label = CoreLabel(text=spec.text, font_size=1000, halign='center', valign='center')
148+
if self.font == None:
149+
label = CoreLabel(text=spec.text, font_size=1000, halign='center', valign='center')
150+
else:
151+
label = CoreLabel(text=spec.text, font_context = None, font_name=self.font, font_size=1000, halign='center', valign='center')
138152
label.refresh()
139-
text = label.texture
140-
item = Rectangle(pos=pos, size=size, texture=text)
153+
item = Rectangle(pos=pos, size=size, texture=label.texture)
141154
elif spec.type == 'image':
142155
item = AsyncImage(pos=pos, size=size, source=spec.source)
143156
spec.item = item

easycoder/ec_screenspec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def createWidget(self, widget, parent):
5858

5959
# Render a complete specification
6060
def renderSpec(self, spec, parent):
61+
if 'font' in spec: getUI().font = spec['font']
62+
else: getUI().font = None
6163
widgets = spec['#']
6264
# If a list, iterate it
6365
if isinstance(widgets, list):

easycoder/ec_value.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
from .ec_classes import FatalError
22

3+
# Create a constant
4+
def getConstant(str):
5+
value = {}
6+
value['type'] = 'text'
7+
value['content'] = str
8+
return value
9+
310
class Value:
411

512
def __init__(self, compiler):
@@ -15,7 +22,7 @@ def getItem(self):
1522
return None
1623

1724
value = {}
18-
25+
1926
if token == 'true':
2027
value['type'] = 'boolean'
2128
value['content'] = True
@@ -80,7 +87,7 @@ def compileValue(self):
8087
value = domain.modifyValue(value)
8188

8289
return value
83-
90+
8491
def compileConstant(self, token):
8592
value = {}
8693
if type(token) == 'str':

plugins/ec_keyboard.py

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from easycoder import Object, FatalError, RuntimeError
22
from easycoder import Handler
3+
from easycoder import getConstant
34
from easycoder.ec_screenspec import ScreenSpec
45
from easycoder.ec_renderer import getActual, getUI
56
import json
@@ -21,16 +22,74 @@ def getName(self):
2122
# Create a keyboard
2223
def k_create(self, command):
2324
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
2575
self.add(command)
2676
return True
2777
return False
2878

2979
def r_create(self, command):
3080
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()
3383
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'])
3493
return self.nextPC()
3594

3695
# on click/tap keyboard
@@ -60,26 +119,24 @@ def k_on(self, command):
60119
self.getCommandAt(pcNext)['goto'] = self.getPC()
61120
return True
62121
return False
63-
122+
64123
# Set a handler
65124
def r_on(self, command):
66125
self.onTap = command['goto']
67126
return self.nextPC()
68127

69128
# Render a keyboard
70-
# render keyboard at {left} {bottom} width {width} using {button image}
129+
# render keyboard at {left} {bottom} width {width}
71130
def k_render(self, command):
72131
if self.nextIs('keyboard'):
73132
token = self.peek()
74-
while token in ['at', 'width', 'using']:
133+
while token in ['at', 'width']:
75134
self.nextToken()
76135
if token == 'at':
77136
command['x'] = self.nextValue()
78137
command['y'] = self.nextValue()
79138
elif token == 'width':
80139
command['w'] = self.nextValue()
81-
elif token == 'using':
82-
command['u'] = self.nextValue()
83140
token = self.peek()
84141
self.add(command)
85142
return True
@@ -89,7 +146,6 @@ def r_render(self, command):
89146
x = getActual(self.getRuntimeValue(command['x']))
90147
y = getActual(self.getRuntimeValue(command['y']))
91148
w = getActual(self.getRuntimeValue(command['w']))
92-
u = self.getRuntimeValue(command['u'])
93149
# Scan the keyboard layout to find the longest row
94150
max = 0
95151
nrows = len(self.keyboard.layout)
@@ -111,19 +167,21 @@ def r_render(self, command):
111167
for b in range(0, len(row)):
112168
button = row[b]
113169
id = button['id']
114-
button['type'] = 'ellipse'
170+
button['type'] = self.keyboard.buttonStyle
171+
button['source'] = self.keyboard.buttonTemplate
115172
button['left'] = bx
116173
button['bottom'] = by
117174
button['width'] = bs
118175
button['height'] = bs
119-
button['fill'] = 'magenta'
176+
button['fill'] = self.keyboard.buttonColor
120177
label = {}
121178
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
126183
label['text'] = id
184+
label['color'] = self.keyboard.buttonTextColor
127185
button['#'] = 'Label'
128186
button['Label'] = label
129187
buttons.append(button)
@@ -135,6 +193,7 @@ def r_render(self, command):
135193
for n in range(0, len(list)):
136194
spec[list[n]] = buttons[n]
137195

196+
spec['font'] = self.keyboard.buttonFont
138197
try:
139198
ScreenSpec().render(spec, None)
140199
except Exception as e:

plugins/keyboards/4-function.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@
4343
],
4444
[
4545
{
46-
"id": "C"
46+
"id": "0"
4747
},
4848
{
4949
"id": "="
5050
},
5151
{
52-
"id": "0"
52+
"id": "C"
5353
},
5454
{
5555
"id": "/"
5656
}
5757
]
58-
]
58+
]

plugins/keyboards/round-button.png

-662 Bytes
Loading

scripts/keyboard.ecg

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
! Keyboard tester
1+
! Keyboard demo
22

33
script Keyboard
44

55
import plugin Keyboard from plugins/ec_keyboard.py
66

77
! debug step
88

9-
create window title `Keyboard` at 300 300 size 640 480 fill color 255 255 200
9+
create window title `Number Pad` at 300 300 size 640 480 fill color 255 255 200
1010
run graphics
1111

12-
create keyboard `4-function`
13-
render keyboard at `25w` `10h` width `40w` using `keyboards/round-button.png`
12+
create keyboard `plugins/keyboards/4-function.json`
13+
button style image
14+
button template `plugins/keyboards/round-button.png`
15+
button text color `black`
16+
button text width `30w`
17+
button text height `30h`
18+
button font `/usr/share/fonts/truetype/msttcorefonts/arial.ttf`
19+
render keyboard at `25w` `10h` width `40w`
1420
on click keyboard print the key
1521

1622
stop

0 commit comments

Comments
 (0)