Skip to content

Commit f8c33b1

Browse files
author
Tom De Smedt
committed
Bug fix for multiple windows.
- Canvas._draw() calls Canvas._window.switch_to() - Canvas.fps = 60 by default - global "canvas" moved to nodebox.graphics, so importing * from nodebox.graphics.context gives you the Canvas class without creating a default global "canvas".
1 parent c199171 commit f8c33b1

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

nodebox/graphics/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,31 @@
99

1010
physics.line = context.line
1111
physics.ellipse = context.ellipse
12-
physics.Text = context.Text
12+
physics.Text = context.Text
13+
14+
#-----------------------------------------------------------------------------------------------------
15+
# Expose the canvas and some common canvas properties on global level.
16+
# Some magic constants from NodeBox are commands here:
17+
# - WIDTH => width()
18+
# - HEIGHT => height()
19+
# - FRAME => frame()
20+
21+
canvas = Canvas()
22+
23+
def size(width=None, height=None):
24+
if width is not None:
25+
canvas.width = width
26+
if height is not None:
27+
canvas.height = height
28+
return canvas.size
29+
30+
def speed(fps=None):
31+
if fps is not None:
32+
canvas.fps = fps
33+
return canvas.fps
34+
35+
def frame():
36+
return canvas.frame
37+
38+
def clear():
39+
canvas.clear()

nodebox/graphics/bezier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def curvepoint(t, x0, y0, x1, y1, x2, y2, x3, y3, handles=False):
6969
def curvelength(x0, y0, x1, y1, x2, y2, x3, y3, n=20):
7070
""" Returns the length of the spline.
7171
Integrates the estimated length of the cubic bezier spline defined by x0, y0, ... x3, y3,
72-
by adding the lengths of lineair lines between points at t.
72+
by adding the lengths of linear lines between points at t.
7373
The number of points is defined by n
7474
(n=10 would add the lengths of lines between 0.0 and 0.1, between 0.1 and 0.2, and so on).
7575
The default n=20 is fine for most cases, usually resulting in a deviation of less than 0.01.

nodebox/graphics/context.py

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,6 @@ def triangle(x1, y1, x2, y2, x3, y3, **kwargs):
611611
glVertex2f(x1, y1)
612612
glVertex2f(x2, y2)
613613
glVertex2f(x3, y3)
614-
#glVertex2f(x, y+height)
615614
glEnd()
616615

617616
_ellipses = {}
@@ -1361,6 +1360,7 @@ def directed(points):
13611360
pt0 = points.point(0.999)
13621361
angle = geometry.angle(pt0.x, pt0.y, pt.x, pt.y)
13631362
elif i == n-1 and isinstance(pt, bezier.DynamicPathElement) and pt.ctrl1.x != pt.x or pt.ctrl1.y != pt.y:
1363+
# For the last point in BezierPath.points(), use incoming handle (ctrl1) for curves.
13641364
angle = geometry.angle(pt.ctrl1.x, pt.ctrl1.y, pt.x, pt.y)
13651365
elif 0 < i:
13661366
# For any point, look back gives a good result, if enough points are given.
@@ -3472,15 +3472,15 @@ def __init__(self, width=640, height=480, name="NodeBox for OpenGL", resizable=F
34723472
height = height,
34733473
resizable = resizable,
34743474
style = border is False and WINDOW_BORDERLESS or WINDOW_DEFAULT,
3475-
config = _configure(settings),
3475+
config = _configure(settings),
34763476
vsync = vsync
34773477
)
34783478
Prototype.__init__(self)
34793479
EventHandler.__init__(self)
34803480
self.profiler = Profiler(self)
34813481
self._window = pyglet.window.Window(**window)
34823482
self._fps = None # Frames per second.
3483-
self._frame = 0 # The current frame.
3483+
self._frame = 60 # The current frame.
34843484
self._elapsed = 0 # dt = time elapsed since last frame.
34853485
self._active = False # Application is running?
34863486
self.paused = False # Pause animation?
@@ -3766,17 +3766,17 @@ def _on_resize(self, width, height):
37663766
self.on_resize()
37673767

37683768
# Event methods are meant to be overridden or patched with Prototype.set_method().
3769-
def on_key_press(self, key):
3769+
def on_key_press(self, keys):
37703770
""" The default behavior of the canvas:
37713771
- ESC exits the application,
37723772
- CTRL-P pauses the animation,
37733773
- CTRL-S saves a screenshot.
37743774
"""
3775-
if key.code == ESCAPE:
3775+
if keys.code == ESCAPE:
37763776
self.stop()
3777-
if key.code == "p" and CTRL in key.modifiers:
3777+
if keys.code == "p" and CTRL in keys.modifiers:
37783778
self.paused = not self.paused
3779-
if key.code == "s" and CTRL in key.modifiers:
3779+
if keys.code == "s" and CTRL in keys.modifiers:
37803780
self.save("nodebox-%s.png" % str(datetime.now()).split(".")[0].replace(" ","-").replace(":","-"))
37813781

37823782
def on_move(self):
@@ -3832,9 +3832,9 @@ def _draw(self, lapse=0):
38323832
""" Draws the canvas and its layers.
38333833
This method gives the same result each time it gets drawn; only _update() advances state.
38343834
"""
3835-
self._frame += 1
38363835
if self.paused:
38373836
return
3837+
self._window.switch_to()
38383838
glPushMatrix()
38393839
self.draw()
38403840
glPopMatrix()
@@ -3856,6 +3856,7 @@ def _update(self, lapse=0):
38563856
# This is only done when the canvas is not paused.
38573857
# Events will still be propagated during pause.
38583858
global TIME; TIME = time()
3859+
self._frame += 1
38593860
self.update()
38603861
for layer in self:
38613862
layer._update()
@@ -3904,6 +3905,7 @@ def run(self, draw=None, setup=None, update=None, stop=None):
39043905
self.set_method(update, name="update")
39053906
if isinstance(stop, FunctionType):
39063907
self.set_method(stop, name="stop")
3908+
self._frame += 1
39073909
self._setup()
39083910
self.fps = self._fps # Schedule the _update and _draw events.
39093911
pyglet.app.run()
@@ -3928,7 +3930,7 @@ def _set_fps(self, v):
39283930
pyglet.clock.schedule_interval(f, 1.0/v)
39293931
self._fps = v
39303932

3931-
fps = property(_get_fps, _set_fps)
3933+
fps = property(_get_fps, _set_fps)
39323934

39333935
#--- Frame export -----------------------------------
39343936

@@ -4059,30 +4061,3 @@ def ximport(library):
40594061
# Linear interpolation math for BezierPath.point() etc.
40604062

40614063
import bezier
4062-
4063-
#-----------------------------------------------------------------------------------------------------
4064-
# Expose the canvas and some common canvas properties on global level.
4065-
# Some magic constants from NodeBox are commands here:
4066-
# - WIDTH => width()
4067-
# - HEIGHT => height()
4068-
# - FRAME => frame()
4069-
4070-
canvas = Canvas()
4071-
4072-
def size(width=None, height=None):
4073-
if width is not None:
4074-
canvas.width = width
4075-
if height is not None:
4076-
canvas.height = height
4077-
return canvas.size
4078-
4079-
def speed(fps=None):
4080-
if fps is not None:
4081-
canvas.fps = fps
4082-
return canvas.fps
4083-
4084-
def frame():
4085-
return canvas.frame
4086-
4087-
def clear():
4088-
canvas.clear()

0 commit comments

Comments
 (0)