Skip to content

Commit 9dbdad6

Browse files
committed
v241215.1
1 parent 9102a4b commit 9dbdad6

File tree

4 files changed

+54
-40
lines changed

4 files changed

+54
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ The language comprises a general-purpose core package, which can be enhanced by
6363

6464
**_EasyCoder_** can be extended to add new functionality with the use of 'plugins'. These contain compiler and runtime modules for the added language features. **_EasyCoder_** can use the added keywords, values and conditions freely; the effect is completely seamless. There is an outline example in the `plugins` directory called `example.py`, which comprises a module called `Points` with new language syntax to deal with two-valued items such as coordinates. In the `scripts` directory there is `points.ecs`, which exercises the new functionality.
6565

66-
A plugin can act as a wrapper around any Python functionality that has a sensible API, thereby hiding its complexity. The only challenge is to devise an unambiguous syntax that doesn't clash with anything already existing in **_EasyCoder_**
66+
A plugin can act as a wrapper around any Python functionality that has a sensible API, thereby hiding its complexity. The only challenge is to devise an unambiguous syntax that doesn't clash with anything already existing in **_EasyCoder_**.
58 Bytes
Binary file not shown.

dist/easycoder-241215.1.tar.gz

58 Bytes
Binary file not shown.

easycoder/ec_renderer.py

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def onClick(event):
5050
id = list(element)[0]
5151
values = element[id]
5252
x1 = values['left']
53-
x2 = x1 + values['width']
54-
y1 = values['top']
53+
x2 = x1 + values['width'] + getScreenLeft(values['parent'])
54+
y1 = values['top'] + getScreenTop(values['parent'])
5555
y2 = y1 + values['height']
5656
if x >= x1 and x < x2 and y >= y1 and y < y2:
5757
if id in elements:
@@ -110,26 +110,26 @@ def getValue(args, item):
110110
return args[item]
111111
return item
112112

113-
def renderIntoRectangle(widgetType, values, offset, args):
113+
def renderIntoRectangle(widgetType, values, parent, args):
114114
global zlist
115115
left = getValue(args, values['left']) if 'left' in values else 10
116+
screenLeft = left + getScreenLeft(parent)
116117
top = getValue(args, values['top']) if 'top' in values else 10
117-
left = offset['dx'] + left
118-
top = offset['dy'] + top
118+
screenTop = top + getScreenTop(parent)
119119
width = getValue(args, values['width']) if 'width' in values else 100
120120
height = getValue(args, values['height']) if 'height' in values else 100
121-
right = left + width
122-
bottom = top + height
121+
right = screenLeft + width
122+
bottom = screenTop + height
123123
fill = values['fill'] if 'fill' in values else None
124124
outline = values['outline'] if 'outline' in values else None
125125
if outline != None:
126126
outlineWidth = getValue(args, values['outlineWidth']) if 'outlineWidth' in values else 1
127127
else:
128128
outlineWidth = 0
129129
if widgetType == 'rectangle':
130-
widgetId = getCanvas().create_rectangle(left, top, right, bottom, fill=fill, outline=outline, width=outlineWidth)
130+
widgetId = getCanvas().create_rectangle(screenLeft, screenTop, right, bottom, fill=fill, outline=outline, width=outlineWidth)
131131
elif widgetType == 'ellipse':
132-
widgetId = getCanvas().create_oval(left, top, right, bottom, fill=fill, outline=outline, width=outlineWidth)
132+
widgetId = getCanvas().create_oval(screenLeft, screenTop, right, bottom, fill=fill, outline=outline, width=outlineWidth)
133133
else:
134134
return f'Unknown widget type \'{widgetType}\''
135135
if 'name' in values:
@@ -144,6 +144,7 @@ def renderIntoRectangle(widgetType, values, offset, args):
144144
"top": top,
145145
"width": width,
146146
"height": height,
147+
"parent": parent,
147148
"children": []
148149
}
149150
elements[widgetName] = widgetSpec
@@ -154,23 +155,23 @@ def renderIntoRectangle(widgetType, values, offset, args):
154155
for item in children:
155156
if item in values:
156157
child = values[item]
157-
childSpec = renderWidget(child, {'dx': left, 'dy': top}, args)
158+
childSpec = renderWidget(child, widgetSpec, args)
158159
widgetSpec['children'].append(childSpec['name'])
159160
else:
160161
child = values[children]
161-
childSpec = renderWidget(child, {'dx': left, 'dy': top}, args)
162+
childSpec = renderWidget(child, widgetSpec, args)
162163
widgetSpec['children'].append(childSpec['name'])
163164
return widgetSpec
164165

165-
def renderText(values, offset, args):
166+
def renderText(values, parent, args):
166167
left = getValue(args, values['left']) if 'left' in values else 10
168+
screenLeft = left + getScreenLeft(parent)
167169
top = getValue(args, values['top']) if 'top' in values else 10
168-
left = offset['dx'] + left
169-
top = offset['dy'] + top
170+
screenTop = top + getScreenTop(parent)
170171
width = getValue(args, values['width']) if 'width' in values else 100
171172
height = getValue(args, values['height']) if 'height' in values else 100
172-
right = left + width
173-
bottom = top + height
173+
right = screenLeft + width
174+
bottom = screenTop + height
174175
shape = getValue(args, values['shape']) if 'shape' in values else 'rectangle'
175176
fill = getValue(args, values['fill']) if 'fill' in values else None
176177
outline = getValue(args, values['outline']) if 'outline' in values else None
@@ -180,7 +181,7 @@ def renderText(values, offset, args):
180181
fontFace = getValue(args, values['fontFace']) if 'fontFace' in values else 'Helvetica'
181182
fontWeight = getValue(args, values['fontWeight']) if 'fontWeight' in values else 'normal'
182183
fontSize = round(height*2/5) if shape == 'ellipse' else round(height*3/5)
183-
fontTop = top + height/2
184+
fontTop = screenTop + height/2
184185
if 'fontSize' in values:
185186
fontSize = getValue(args, values['fontSize'])
186187
fontTop = top + round(fontSize * 5 / 4)
@@ -198,10 +199,10 @@ def renderText(values, offset, args):
198199
if xoff < 3:
199200
xoff = 3
200201
if shape == 'ellipse':
201-
containerId = getCanvas().create_oval(left, top, right, bottom, fill=fill, outline=outline, width=outlineWidth)
202+
containerId = getCanvas().create_oval(screenLeft, screenTop, right, bottom, fill=fill, outline=outline, width=outlineWidth)
202203
else:
203-
containerId = getCanvas().create_rectangle(left, top, right, bottom, fill=fill, outline=outline, width=outlineWidth)
204-
textId = canvas.create_text(left + xoff, fontTop + adjust, fill=color, font=f'"{fontFace}" {fontSize} {fontWeight}', text=text, anchor=anchor)
204+
containerId = getCanvas().create_rectangle(screenLeft, screenTop, right, bottom, fill=fill, outline=outline, width=outlineWidth)
205+
textId = canvas.create_text(screenLeft + xoff, fontTop + adjust, fill=color, font=f'"{fontFace}" {fontSize} {fontWeight}', text=text, anchor=anchor)
205206
if 'name' in values:
206207
widgetName = getValue(args, values['name'])
207208
else:
@@ -214,24 +215,25 @@ def renderText(values, offset, args):
214215
"left": left,
215216
"top": top,
216217
"width": width,
217-
"height": height
218+
"height": height,
219+
"parent": parent
218220
}
219221
elements[widgetName] = widgetSpec
220222
zlist.append({widgetName: widgetSpec})
221223
return widgetSpec
222224

223-
def renderImage(values, offset, args):
225+
def renderImage(values, parent, args):
224226
global images
225227
left = getValue(args, values['left']) if 'left' in values else 10
228+
screenLeft = left + getScreenLeft(parent)
226229
top = getValue(args, values['top']) if 'top' in values else 10
227-
left = offset['dx'] + left
228-
top = offset['dy'] + top
230+
screenTop = top + getScreenTop(parent)
229231
width = getValue(args, values['width']) if 'width' in values else 100
230232
height = getValue(args, values['height']) if 'height' in values else 100
231-
right = left + width
232-
bottom = top + height
233+
right = screenLeft + width
234+
bottom = screenTop + height
233235
src = getValue(args, values['src']) if 'src' in values else None
234-
containerId = getCanvas().create_rectangle(left, top, right, bottom, width=0)
236+
containerId = getCanvas().create_rectangle(screenLeft, screenTop, right, bottom, width=0)
235237
if 'name' in values:
236238
widgetName = values['name']
237239
else:
@@ -243,7 +245,8 @@ def renderImage(values, offset, args):
243245
"left": left,
244246
"top": top,
245247
"width": width,
246-
"height": height
248+
"height": height,
249+
"parent": parent
247250
}
248251
elements[widgetName] = widgetSpec
249252
zlist.append({widgetName: widgetSpec})
@@ -252,45 +255,44 @@ def renderImage(values, offset, args):
252255
img = (Image.open(src))
253256
resized_image= img.resize((width, height), Image.ANTIALIAS)
254257
new_image= ImageTk.PhotoImage(resized_image)
255-
imageid = getCanvas().create_image(left, top, anchor='nw', image=new_image)
258+
imageid = getCanvas().create_image(screenLeft, screenTop, anchor='nw', image=new_image)
256259
images[containerId] = {'id': imageid, "image": new_image}
257260
return widgetSpec
258261

259262
# Create a canvas or render a widget
260-
def renderWidget(widget, offset, args):
263+
def renderWidget(widget, parent, args):
261264
widgetType = widget['type']
262265
if widgetType in ['rectangle', 'ellipse']:
263-
return renderIntoRectangle(widgetType, widget, offset, args)
266+
return renderIntoRectangle(widgetType, widget, parent, args)
264267
elif widgetType == 'text':
265-
return renderText(widget, offset, args)
268+
return renderText(widget, parent, args)
266269
elif widgetType == 'image':
267-
return renderImage(widget, offset, args)
270+
return renderImage(widget, parent, args)
268271

269272
# Render a complete specification
270-
def renderSpec(spec, offset, args):
273+
def renderSpec(spec, parent, args):
271274
widgets = spec['#']
272275
# If a list, iterate it
273276
if type(widgets) is list:
274277
for widget in widgets:
275-
renderWidget(spec[widget], offset, args)
278+
renderWidget(spec[widget], parent, args)
276279
# Otherwise, process the single widget
277280
else:
278-
renderWidget(spec[widgets], offset, args)
281+
renderWidget(spec[widgets], parent, args)
279282

280283
# Main entry point
281-
offset = {'dx': 0, 'dy': 0}
282284
if parent != screen:
283285
RuntimeError(None, 'Can\'t yet render into parent widget')
284286

285287
# If it'a string, process it
286288
if type(spec) is str:
287-
renderSpec(json.loads(spec), offset, {})
289+
renderSpec(json.loads(spec), None, {})
288290

289291
# If it's a 'dict', extract the spec and the args
290292
if type(spec) is dict:
291293
args = spec['args']
292294
spec = json.loads(spec['spec'])
293-
renderSpec(spec, offset, args)
295+
renderSpec(spec, None, args)
294296

295297
# Get the widget whose name is given
296298
def getElement(name):
@@ -328,3 +330,15 @@ def moveElementTo(name, left, top):
328330
def getAttribute(name, attribute):
329331
element = getElement(name)
330332
return element[attribute]
333+
334+
# Get the screen left position of an element
335+
def getScreenLeft(element):
336+
if element == None:
337+
return 0
338+
return element['left'] + getScreenLeft(element['parent'])
339+
340+
# Get the screen top position of an element
341+
def getScreenTop(element):
342+
if element == None:
343+
return 0
344+
return element['top'] + getScreenTop(element['parent'])

0 commit comments

Comments
 (0)