Merge branch 'master' of github.com:tito/kivy

This commit is contained in:
Thomas Hansen 2010-11-03 22:29:20 -05:00
commit 0bc7f69c2e
3 changed files with 135 additions and 17 deletions

View File

@ -5,12 +5,8 @@ Base: Main event loop, provider creation, window management...
__all__ = (
'EventLoop',
'runTouchApp', 'stopTouchApp',
'getCurrentTouches',
)
import kivy
import sys
import os
from kivy.config import Config
from kivy.logger import Logger
from kivy.exceptions import ExceptionManager
@ -20,11 +16,6 @@ from kivy.input import TouchFactory, kivy_postproc_modules
# private vars
EventLoop = None
def getCurrentTouches():
'''Return the list of all current touches
'''
return touch_list
class EventLoopBase(object):
'''Main event loop. This loop handle update of input + dispatch event
'''
@ -37,6 +28,18 @@ class EventLoopBase(object):
self.input_providers = []
self.event_listeners = []
self.window = None
self.touch_list = []
@property
def touches(self):
'''Return the list of all touches currently in down or move state
'''
return self.touch_list
def ensure_window(self):
'''Ensure that we have an window
'''
import kivy.core.window
def set_window(self, window):
'''Set the window used for event loop
@ -106,10 +109,10 @@ class EventLoopBase(object):
grabbed, it's dispatched through grabbed widgets'''
# update available list
if event == 'down':
touch_list.append(touch)
self.touch_list.append(touch)
elif event == 'up':
if touch in touch_list:
touch_list.remove(touch)
if touch in self.touch_list:
self.touch_list.remove(touch)
# dispatch to listeners
if not touch.grab_exclusive_class:
@ -279,6 +282,8 @@ def runTouchApp(widget=None, slave=False):
# Ok, we got one widget, and we are not in slave mode
# so, user don't create the window, let's create it for him !
if widget:
EventLoop.ensure_window()
# Instance all configured input
for key, value in Config.items('input'):

View File

@ -129,6 +129,13 @@ cdef class ListProperty(Property):
if type(value) is not list:
raise ValueError('Value of the property is not a list')
cdef class ObjectProperty(Property):
cdef check(self, obj, value):
if Property.check(self, obj, value):
return True
if not isinstance(value, object):
raise ValueError('Value accept only object')
cdef class BoundedNumericProperty(Property):
cdef int use_min
cdef int use_max

View File

@ -12,6 +12,9 @@ __all__ = ('Widget', )
from kivy.weakmethod import WeakMethod
from kivy.c_ext.event import EventDispatcher
from kivy.c_ext.properties import *
from kivy.base import EventLoop
EventLoop.ensure_window()
from kivy.graphics import Canvas
class Widget(EventDispatcher):
'''
@ -54,6 +57,73 @@ class Widget(EventDispatcher):
if isinstance(attr, Property):
attr.unlink(self)
def __init__(self, **kwargs):
super(Widget, self).__init__()
self.register_event_type('on_touch_down')
self.register_event_type('on_touch_move')
self.register_event_type('on_touch_up')
self.register_event_type('on_draw')
self.canvas = Canvas()
#
# Collision
#
def collide_point(self, x, y):
return self.x <= x <= self.right and self.y <= y <= self.top
#
# Default event handlers
#
def on_draw(self):
'''Dispatch the on_draw even in every child
'''
self.draw()
for child in reversed(self.children):
child.dispatch('on_draw')
def on_touch_down(self, touch):
'''Send the touch down event in every child
Return true if one child use it
'''
for child in self.children[:]:
if child.dispatch('on_touch_down', touch):
return True
def on_touch_move(self, touch):
'''Send the touch move event in every child
Return true if one child use it
'''
for child in self.children[:]:
if child.dispatch('on_touch_move', touch):
return True
def on_touch_up(self, touch):
'''Send the touch down event in every child
Return true if one child use it
'''
for child in self.children[:]:
if child.dispatch('on_touch_up', touch):
return True
#
# Drawing
#
def draw(self):
'''Draw the widget
'''
self.canvas.draw()
#
# Events
#
def bind(self, **kwargs):
'''Bind properties or event to handler
@ -82,6 +152,29 @@ class Widget(EventDispatcher):
self.__class__.__dict__[key].unbind(self, value)
#
# Tree management
#
def add_widget(self, widget):
'''Add a new widget as a child of current widget
'''
self.children = self.children + [widget]
def remove_widget(self, widget):
'''Remove a widget from the childs of current widget
'''
if widget in self.children:
self.children = self.children.remove(widget)
#
# Properties
#
def setter(self, name):
'''Return the setter of a property. Useful if you want to directly bind
a property to another. For example ::
@ -95,11 +188,6 @@ class Widget(EventDispatcher):
'''
return self.__class__.__dict__[name].__get__
#
# Properties
#
#: X position of the widget
x = NumericProperty(0)
@ -149,3 +237,21 @@ class Widget(EventDispatcher):
#: User id of the widget
id = StringProperty(None, allownone=True)
#: Children list
children = ListProperty([])
#: Parent
parent = ObjectProperty(None)
#: Size hint X
size_hint_x = NumericProperty(None, allownone=True)
#: Size hint Y
size_hint_y = NumericProperty(None, allownone=True)
#: Size hint
size_hint = ReferenceListProperty(size_hint_x, size_hint_y)
#: Canvas
canvas = None