Skip to content

Instantly share code, notes, and snippets.

@hansent
Created July 24, 2010 18:07
Show Gist options
  • Select an option

  • Save hansent/488865 to your computer and use it in GitHub Desktop.

Select an option

Save hansent/488865 to your computer and use it in GitHub Desktop.
'''
Use keyboard to do some action
'''
__all__ = ('start', 'stop')
import sys
import logging
from pymt import *
class SceneGraphNode(MTBoxLayout):
selection = None
def __init__(self, **kwargs):
kwargs['invert'] = True
super(SceneGraphNode, self).__init__(**kwargs)
self.widget = kwargs['node']
self.child_layout = MTBoxLayout(size_hint=(None, None), spacing=10, orientation="vertical", animation_type='ease_out_elastic', animation_duration=0.7)
for c in self.widget.children:
self.child_layout.add_widget(SceneGraphNode(node=c, size_hint=(None, None)))
self.add_widget(self.child_layout)
self.node_btn = MTToggleButton(label=str(self.widget.__class__.__name__), size=(150,30))
self.title = MTAnchorLayout(size_hint=(None, None), size=(200,self.child_layout.height))
self.title.add_widget(self.node_btn)
self.add_widget(self.title)
self.node_btn.connect('on_release',self.select)
def draw(self):
if SceneGraphNode.selection == self:
self.node_btn.state = 'down'
set_color(1,0,0,0.3)
pos = Vector(self.to_widget(*self.widget.pos))
size = - pos + self.to_widget(*Vector(*self.widget.pos) + self.widget.size)
drawRectangle(self.to_widget(*self.widget.pos), size)
else:
self.node_btn.state = 'normal'
set_color(1,.3,0)
for c in self.child_layout.children:
#print self.child_layout.minimum_size
drawLine((self.node_btn.centerright,c.node_btn.centerleft), width=2)
def select(self, *args):
global _css_editor, _scene_graph_modal_layover
if SceneGraphNode.selection == self:
SceneGraphNode.selection = None
else:
SceneGraphNode.selection = self
_scene_graph_modal_layover.remove_widget(_css_editor)
_css_editor = CSSEditor(self.widget, size=_scene_graph_modal_layover.size)
_scene_graph_modal_layover.add_widget(_css_editor)
def add_new_widget(self, *args):
new_widget = MTButton(label="I'm new!!!")
self.widget.add_widget(new_widget)
self.child_layout.add_widget(SceneGraphNode(node=new_widget, size_hint=(None, None)))
self.title.size=(200,self.child_layout.height)
def print_props(self, *args):
for prop in self.widget.__dict__:
if not prop.startswith("_"):
print prop, ":", getattr(self.widget, prop)
class CSSEditor(MTAnchorLayout):
def __init__(self, widget, **kwargs):
kwargs['anchor_x'] = 'right'
kwargs['anchor_y'] = 'top'
super(CSSEditor, self).__init__(**kwargs)
self.widget = widget
grid = MTGridLayout(cols=2)
for style in widget.style:
grid.add_widget(MTLabel(label=style, font_size=10))
value = str(widget.style[style])
if type(widget.style[style]) == type([]):
value = "%.1f,"*len(widget.style[style]) % tuple(widget.style[style])
value = "["+value[:-1]+"]"
input = MTTextInput(label=value, size=(150,20), font_size=10,
keyboard_type='real', group="cssedit", switch=False)
def set_value(w, s, i, *args):
try:
print "setting ", w, s, "label:",i.value
w.style[s] = eval(i.value)
except:
print "Exception"
i.label = w.style[s]
i.notify_error()
return True
input.push_handlers(on_text_validate=curry(set_value, widget, style, input) )
grid.add_widget(input)
sections = MTBoxLayout(size_hint=(None, None), orientation='vertical')
sections.add_widget(MTWidget(size=(10,30))) #padding on top
sections.add_widget(MTLabel(label="style:", size_hint=(1,None), height=30,
style={'draw-background':1, 'bg-color':[.1,.2,.5,.8], 'draw-alpha-background':1})
)
sections.add_widget(grid)
list = MTList(do_x=False, size=(sections.width, getWindow().height),
style={'draw-background':1, 'bg-color':[.4,.4,.4,.8], 'draw-alpha-background':1})
sections.add_widget(MTWidget(size=(10,getWindow().height-sections.height)))
list.add_widget(sections)
self.add_widget(list)
_scene_graph_modal_layover = None
_css_editor = None
scene_graph = None
def toggle_scene_graph():
global _scene_graph_modal_layover, scene_graph
win = getWindow()
#if it already active..disable it..otherwise we start editing the editor (also cool..bu confusing :P)
if _scene_graph_modal_layover:
win.remove_widget(_scene_graph_modal_layover)
_scene_graph_modal_layover = None
return
_scene_graph_modal_layover = MTModalWindow()
#create the scene graph tree visualization for selecting widgets
scene_graph = SceneGraphNode(node=win, size_hint=(None, None))
plane = MTScatterPlane(do_rotation=False)
plane.add_widget(scene_graph)
#edit menu, the buttons at the top
edit_menu = MTBoxLayout(size=(getWindow().width, 30), pos=(0,getWindow().height-30))
def del_widget(*args):
widget = scene_graph.selection.widget
if widget:
widget.parent.remove_widget(widget)
scene_graph.selection.parent.remove_widget(scene_graph.selection)
del_btn = MTButton(size_hint=(1,1), label="remove")
del_btn.push_handlers(on_release=del_widget)
edit_menu.add_widget(del_btn)
def add_widget(*args):
global scene_graph
parent = scene_graph.selection.widget
if parent:
widget = MTWidget()
scene_graph.selection.widget.add_widget(widget)
scene_graph.selection.child_layout.add_widget(SceneGraphNode(node=widget, size_hint=(None, None), pos=scene_graph.selection.pos))
scene_graph.do_layout()
add_btn = MTButton(size_hint=(1,1), label="add widget")
add_btn.push_handlers(on_release=add_widget)
edit_menu.add_widget(add_btn)
_scene_graph_modal_layover.add_widget(plane)
_scene_graph_modal_layover.add_widget(edit_menu)
win.add_widget(_scene_graph_modal_layover)
def _on_keyboard_handler(key, scancode, unicode):
if key is None:
return
win = getWindow()
if key == 282: # F1
toggle_scene_graph()
def start(win, ctx):
win.push_handlers(on_keyboard=_on_keyboard_handler)
def stop(win, ctx):
win.remove_handlers(on_keyboard=_on_keyboard_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment