From e8708f474952b63cfce6cc45b6ec872c261151b9 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 3 Nov 2010 23:06:05 -0400 Subject: [PATCH 1/3] base: remove getCurrentTouches -> EventLoop.touches --- kivy/base.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/kivy/base.py b/kivy/base.py index 0a3916742..a62ad12f4 100644 --- a/kivy/base.py +++ b/kivy/base.py @@ -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'): From 784e3a67c3ad2b78cb0de7ac81ae73a0604ae209 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 3 Nov 2010 23:06:17 -0400 Subject: [PATCH 2/3] properties: add ObjectProperty --- kivy/c_ext/properties.pyx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kivy/c_ext/properties.pyx b/kivy/c_ext/properties.pyx index f0eb67c14..48e2af982 100644 --- a/kivy/c_ext/properties.pyx +++ b/kivy/c_ext/properties.pyx @@ -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 From e194a96f38943f1f2140d4c6d101d1cec81dc8af Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 3 Nov 2010 23:06:48 -0400 Subject: [PATCH 3/3] widget: create the default widget with on_touch_down/move/up + on_draw, collide, tree management (add/remove widget) + canvas + draw --- kivy/uix/widget.py | 116 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/kivy/uix/widget.py b/kivy/uix/widget.py index cb24c4f9f..ecefc7055 100644 --- a/kivy/uix/widget.py +++ b/kivy/uix/widget.py @@ -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