Skip to content

Commit

Permalink
Add an FPS display to the kiva explorer demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiggins committed Dec 11, 2016
1 parent 3555620 commit 1679b1b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions examples/kiva_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
Interactive editor for exploring Kiva drawing commands.
"""
import time

from traits.api import Any, Code, Instance, Str
from traits.api import Any, Code, Float, Instance, Property, Str
from traitsui.api import HSplit, ModelView, UItem, VGroup, View
from enable.api import Component, ComponentEditor

Expand Down Expand Up @@ -50,6 +51,10 @@ class ScriptedComponent(Component):
#: any errors which occur
error = Str

#: how long did the last draw take
last_draw_time = Float(0.0)
fps_display = Property(Str, depends_on='last_draw_time')

#: compiled code
_draw_code = Any

Expand All @@ -58,7 +63,9 @@ def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
with gc:
try:
self.error = ''
start_time = time.time()
exec self._draw_code in {}, {'gc': gc}
self.last_draw_time = time.time() - start_time
except Exception as exc:
self.error = str(exc)

Expand All @@ -83,6 +90,14 @@ def __draw_code_default(self):
code = compile("", "<script>", "exec")
return code

def _get_fps_display(self):
if self.last_draw_time == 0.0:
return ""

draw_time_ms = self.last_draw_time * 1000.0
draw_fps = 1000.0 / draw_time_ms
return "{:.2f}ms ({:.2f} fps)".format(draw_time_ms, draw_fps)


class ScriptedComponentView(ModelView):
""" ModelView of a ScriptedComponent displaying the script and image """
Expand All @@ -100,7 +115,10 @@ class ScriptedComponentView(ModelView):
style='readonly',
height=100)
),
UItem('model', editor=ComponentEditor()),
VGroup(
UItem('model', editor=ComponentEditor()),
UItem('model.fps_display', height=20)
),
),
resizable=True
)
Expand Down

0 comments on commit 1679b1b

Please sign in to comment.