This repository has been archived by the owner on Jun 16, 2019. It is now read-only.
/
__init__.py
86 lines (72 loc) · 3.19 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
__all__ = ('Collide2DPoly', 'CollideBezier', 'CollideEllipse')
__version__ = '0.1-dev'
from .collider import Collide2DPoly, CollideEllipse, CollideBezier
if __name__ == '__main__':
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Line, Color, Rectangle
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
import itertools
class CollideTester(Widget):
def __init__(self, **kwargs):
super(CollideTester, self).__init__(**kwargs)
self.state = 'drawing'
self.collider = None
def on_touch_down(self, touch):
if super(CollideTester, self).on_touch_down(touch):
return True
if not self.collide_point(*touch.pos):
return False
touch.grab(self)
if self.state == 'drawing':
with self.canvas:
Color(1, 0, 1, group='12345')
touch.ud['line'] = Line(points=[touch.x, touch.y],
close=True, group='12345')
def on_touch_move(self, touch):
if touch.grab_current is not self:
return super(CollideTester, self).on_touch_move(touch)
if self.state == 'drawing':
touch.ud['line'].points += [touch.x, touch.y]
def on_touch_up(self, touch):
if touch.grab_current is not self:
return super(CollideTester, self).on_touch_up(touch)
touch.ungrab(self)
if self.state == 'drawing':
self.state = 'testing'
self.collider = Collide2DPoly(touch.ud['line'].points, cache=True)
collider = self.collider
texture = Texture.create(size=self.size)
inside = [255, 255, 0]
outside = [255, 0, 255]
x_off, y_off = self.pos
width = int(self.width)
height = int(self.height)
buf = bytearray(width * height * 3)
for x in range(width):
for y in range(height):
pos = (x + y * width) * 3
buf[pos:pos + 3] = (inside if (x + x_off, y + y_off)
in collider else outside)
texture.blit_buffer(bytes(buf), colorfmt='rgb',
bufferfmt='ubyte')
self.canvas.remove_group('12345')
with self.canvas:
Rectangle(texture=texture, pos=self.pos, size=self.size,
group='12345')
class TestApp(App):
def build(self):
box = BoxLayout(orientation='vertical')
tester = CollideTester(size_hint_y=0.9)
btn = Button(text='Clear', size_hint_y=0.1)
box.add_widget(tester)
box.add_widget(btn)
def clear_state(*largs):
tester.state = 'drawing'
tester.collider = None
tester.canvas.remove_group('12345')
btn.bind(on_release=clear_state)
return box
TestApp().run()