diff --git a/kivy/data/images/cursor.png b/kivy/data/images/cursor.png new file mode 100644 index 000000000..cbce704f7 Binary files /dev/null and b/kivy/data/images/cursor.png differ diff --git a/kivy/modules/cursor.py b/kivy/modules/cursor.py new file mode 100644 index 000000000..14ca2505c --- /dev/null +++ b/kivy/modules/cursor.py @@ -0,0 +1,77 @@ +''' +Cursor +====== + +Shows a cursor following mouse motion events, useful on systems with no +visible native mouse cursor. + +Configuration +------------- + +:Parameters: + `texture`: str, defaults to + 'data/images/cursor.png' Image used to represent the cursor if + displayed + `size`: tuple, defaults to (40, 40) + Apparent size of the mouse cursor, if displayed, (None,None) value + will keep its real size. + `offset`: tuple, defaults to (None, None) + Offset of the texture image. The default value will align the + top-left corner of the image to the mouse pos. + +Example +------- + +In your configuration (`~/.kivy/config.ini`), you can add something like +this:: + + [modules] + cursor = texture=mypointer.png,size=20x20,offset=20x20 + +.. versionadded:: 1.9.2 +''' + +__all__ = ('start', 'stop') + +from kivy.core.image import Image +from kivy.graphics import Color, Rectangle +from kivy import kivy_data_dir +from kivy.compat import string_types +from os.path import join +from functools import partial + + +def _mouse_move(texture, size, offset, win, pos, *args): + if hasattr(win, '_cursor'): + c = win._cursor + else: + with win.canvas.after: + Color(1, 1, 1, 1, mode='rgba') + win._cursor = c = Rectangle(texture=texture, size=size) + + c.pos = pos[0] + offset[0], pos[1] - size[1] + offset[1] + + +def start(win, ctx): + cursor_texture = Image( + ctx.config.get('texture', join(kivy_data_dir, 'images', 'cursor.png')) + ).texture + cursor_size = ctx.config.get('size') + print type(cursor_size) + if isinstance(cursor_size, string_types): + cursor_size = [int(x) for x in cursor_size.split('x')] + elif not cursor_size: + cursor_size = cursor_texture.size + print cursor_size + + cursor_offset = ctx.config.get('offset', (0, 0)) + if isinstance(cursor_offset, string_types): + cursor_offset = [int(x) for x in cursor_offset.split('x')] + + win.bind( + mouse_pos=partial( + _mouse_move, cursor_texture, cursor_size, cursor_offset)) + + +def stop(win, ctx): + win.unbind(mouse_pos=_mouse_move) diff --git a/kivy/modules/touchring.py b/kivy/modules/touchring.py index af4218444..e579e28f8 100644 --- a/kivy/modules/touchring.py +++ b/kivy/modules/touchring.py @@ -15,23 +15,6 @@ Configuration Scale of the image. `alpha`: float, defaults to 1. Opacity of the image. - `show_cursor`: boolean, defaults to False - .. versionadded:: 1.8.0 - `cursor_image`: str, defaults to - 'atlas://data/images/defaulttheme/slider_cursor' Image used to - represent the cursor if displayed - - .. versionadded:: 1.8.0 - `cursor_size`: tuple, defaults to (None, None) - Apparent size of the mouse cursor, if displayed, default value - will keep its real size. - - .. versionadded:: 1.8.0 - `cursor_offset`: tuple, defaults to (None, None) - Offset of the texture image. The default value will align the - top-left corner of the image to the mouse pos. - - .. versionadded:: 1.8.0 Example ------- @@ -48,14 +31,12 @@ __all__ = ('start', 'stop') from kivy.core.image import Image from kivy.graphics import Color, Rectangle -from kivy.logger import Logger +from kivy import kivy_data_dir +from os.path import join pointer_image = None pointer_scale = 1.0 pointer_alpha = 0.7 -cursor_image = '' -cursor_offset = (0, 0) -cursor_size = (None, None) def _touch_down(win, touch): @@ -94,56 +75,22 @@ def _touch_up(win, touch): ud['tr.grab'] = False -def _mouse_move(win, pos, *args): - global cursor_size - if hasattr(win, '_cursor'): - c = win._cursor - else: - with win.canvas.after: - img = Image(cursor_image) - Color(1, 1, 1, 1, mode='rgba') - size = ( - cursor_size[0] or img.texture.size[0], - cursor_size[1] or img.texture.size[1] - ) - print(size) - win._cursor = c = Rectangle(texture=img.texture, - size=size) - - c.pos = pos[0] + cursor_offset[0], pos[1] - c.size[1] + cursor_offset[1] - - def start(win, ctx): # XXX use ctx ! - global pointer_image, pointer_scale, pointer_alpha, cursor_size,\ - cursor_image, cursor_offset + global pointer_image, pointer_scale, pointer_alpha + pointer_fn = ctx.config.get('image', 'atlas://data/images/defaulttheme/ring') pointer_scale = float(ctx.config.get('scale', 1.0)) pointer_alpha = float(ctx.config.get('alpha', 1.0)) pointer_image = Image(pointer_fn) - cursor_image = ctx.config.get( - 'cursor_image', - 'atlas://data/images/defaulttheme/slider_cursor') - cursor_size = ctx.config.get('cursor_size', (None, None)) - if isinstance(cursor_size, str): - cursor_size = [int(x) for x in cursor_size.split('x')] - - cursor_offset = ctx.config.get('cursor_offset', (0, 0)) - if isinstance(cursor_offset, str): - cursor_offset = [int(x) for x in cursor_offset.split('x')] win.bind(on_touch_down=_touch_down, on_touch_move=_touch_move, on_touch_up=_touch_up) - if ctx.config.get('show_cursor', False): - Logger.info('Base: Adding binding for mouse move') - win.bind(mouse_pos=_mouse_move) - def stop(win, ctx): win.unbind(on_touch_down=_touch_down, on_touch_move=_touch_move, - on_touch_up=_touch_up, - on_mouse_pos=_mouse_move) + on_touch_up=_touch_up)