pixelal...@gmail.com writes:
[...]
> would someone be willing to post an example (primarily Kv, not from
> Python) that does the following?
[...]
> It is a simple exercise that would demonstrate several things that are
> not well explained. I'd like to see this "done the right way"
Challenge accepted! It will probably not be the good example you are
looking for but I decided it was a good exercise for me to learn kivy
also :-). I did not manage to avoid Python though because I needed a if
statement that the kivy language does not permit... I decided to post my
solution to see whether it could trigger interesting discussions.
> - Two widgets, each containing at a label.
I'd simply put the two labels in a float layout.
> - One widget is movable (a scatter) and one fixed in place.
I'd not use scatter since it becomes a real mess to handle collision
with it. Instead, I'd just handle the touch_down, touch_up and
touch_move events to update the position.
> - Each widget has a color so you can see its extent (AABB)
Simply add a coloured rectangle in the canvas of the Label. I am pretty
sure that this one is the correct way of doing.
> - You can drag the movable widget, but only by touching/clicking within
> it AABB.
Like said before, it could be done in the touch events.
> - When the movable widget is intersecting the stationary widget, it
> lights up (changes color).
Handling the collision in the touch_move event is pretty simple,
provided you avoided to use scatter stuff.
The kivy file would look like:
--8<---------------cut here---------------start------------->8---
#:kivy 1.0
FloatLayout:
A:
canvas.before:
Color:
rgb: 0,1,0
Rectangle:
pos: self.pos
size: self.size
id: a
text: "a"
size_hint: .2, .2
B:
canvas.before:
Color:
rgb: 1., self.green, 0
Rectangle:
pos: self.pos
size: self.size
text: "b"
size_hint: .2, .2
pos: root.width / 2., 0
on_touch_down:
self.handle_touch_down(args[1])
on_touch_up:
self.handle_touch_up(args[1])
on_touch_move:
self.handle_touch_move(args[1], a)
--8<---------------cut here---------------end--------------->8---
And the python file:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
--8<---------------cut here---------------start------------->8---
import kivy
From
kivy.app import App
From kivy.uix.scatterlayout import ScatterLayout
From kivy.uix.label import Label
From kivy.graphics import Color, Rectangle
From kivy.properties import NumericProperty, BooleanProperty
class A(Label):
pass
class B(Label):
green = NumericProperty(0)
touching = BooleanProperty(False)
def handle_touch_down(self, event):
if self.collide_point(*event.pos):
self.touching = True
def handle_touch_up(self, event):
self.touching = False
def handle_touch_move(self, event, other):
if self.touching == True and self.collide_point(*event.pos):
self.pos[0] = event.pos[0] - self.width / 2.
self.pos[1] = event.pos[1] - self.height / 2.
self.handle_collision(other)
def handle_collision(self, a):
if self.collide_widget(a):
self.green = 1.
else:
self.green = 0.
class TestApp(App):
pass
if __name__ == "__main__":
TestApp().run()
--8<---------------cut here---------------end--------------->8---
Hope that helps,
--
Konubinix
GPG Key : 7439106A
Fingerprint: 5993 BE7A DA65 E2D9 06CE 5C36 75D2 3CED 7439 106A