forked from dimi3o/MobAdaptUi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkivywidgets.py
More file actions
143 lines (124 loc) · 4.93 KB
/
kivywidgets.py
File metadata and controls
143 lines (124 loc) · 4.93 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import random
from kivy.metrics import dp
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.uix.slider import Slider
from kivy.uix.switch import Switch
from kivy.uix.spinner import Spinner
from kivy.uix.progressbar import ProgressBar
from kivy.uix.behaviors import TouchRippleBehavior
from kivy.uix.scatter import Scatter
from kivy.properties import ListProperty, BooleanProperty
from kivy.core.window import Window
from kivy.clock import Clock
from colors import allcolors
widgets = ['Image','TextInput','Label','Button','CheckBox','Slider','Switch','Spinner','ProgressBar','FlyLabel','FlatButton']
class Widgets:
@staticmethod
def get_random_widget():
ret = random.choice(widgets)
if ret == 'Image': return Image(source='data/icons/bug1.png')
elif ret == 'TextInput': return TextInput(text='textinput')
elif ret == 'Label': return Label(text='label', color=random.choice(allcolors))
elif ret == 'Button': return Button(text='button', background_color=random.choice(allcolors))
elif ret == 'CheckBox': return CheckBox(active=True)
elif ret == 'Slider': return Slider(min=1, max=10, value=1, step=1)
elif ret == 'Switch': return Switch(active=True)
elif ret == 'Spinner': return Spinner(text="Spinner", values=("Python", "Java", "C++", "C", "C#", "PHP"), background_color=(0.784,0.443,0.216,1))
elif ret == 'ProgressBar': return ProgressBar(max=1000, value=750)
elif ret == 'FlyLabel': return FlyLabel()
elif ret == 'FlatButton': return FlatButton().build()
return
KV = """
<FlatButton>:
ripple_color: 0, 0, 0, 0
background_color: 0, 0, 0, 0
color: root.primary_color
canvas.before:
Color:
rgba: root.primary_color
Line:
width: 1
rectangle: (self.x, self.y, self.width, self.height)
Screen:
canvas:
Color:
rgba: 0.9764705882352941, 0.9764705882352941, 0.9764705882352941, 1
# Rectangle:
# pos: (self.x, self.y)
# size: self.size
"""
class FlatButton(TouchRippleBehavior, Button):
primary_color = [0.12941176470588237, 0.5882352941176471, 0.9529411764705882, 1]
def build(self):
screen = Builder.load_string(KV)
btn = FlatButton(text="flatbutton", ripple_color=(0, 0, 0, 0))
screen.add_widget(btn)
return screen
def on_touch_down(self, touch):
collide_point = self.collide_point(touch.x, touch.y)
if collide_point:
touch.grab(self)
self.ripple_show(touch)
return True
return False
def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
self.ripple_fade()
return True
return False
class FlyLabel(TouchRippleBehavior, Label):
velocity = ListProperty([2, 1])
emulation = BooleanProperty(False)
def __init__(self, **kwargs):
super(FlyLabel, self).__init__(**kwargs)
self.velocity[0] *= random.choice([-2, 2])
self.velocity[1] *= random.choice([-2, 2])
self.text = 'flylabel'
self.color = random.choice(allcolors)
def update_pos(self, *args):
parent = self.parent
parent.x += self.velocity[0]
parent.y += self.velocity[1]
if parent.x < 0 or (parent.x + 2*parent.width//2) > Window.width:
self.velocity[0] *= -1
if parent.y < 0 or (parent.y + 2*parent.height//2) > Window.height:
self.velocity[1] *= -1
parent.pos = [parent.x, parent.y]
def on_touch_down(self, touch):
if self.emulation:
self.emulation = False
Clock.unschedule(self.update_pos)
return
self.emulation = True
Clock.schedule_interval(self.update_pos, 1. / 60.)
class FlyScatter(TouchRippleBehavior, Scatter):
velocity = ListProperty([2, 1])
emulation = BooleanProperty(False)
def __init__(self, **kwargs):
super(FlyScatter, self).__init__(**kwargs)
self.velocity[0] *= random.choice([-2, 2])
self.velocity[1] *= random.choice([-2, 2])
self.text = 'flylabel'
self.color = random.choice(allcolors)
def update_pos(self, *args):
parent = self
parent.x += self.velocity[0]
parent.y += self.velocity[1]
if parent.x < 0 or (parent.x + 2*parent.width//3) > Window.width:
self.velocity[0] *= -1
if parent.y < 0 or (parent.y + 2*parent.height//3) > Window.height:
self.velocity[1] *= -1
parent.pos = [parent.x, parent.y]
def on_touch_down(self, touch):
if self.emulation:
self.emulation = False
Clock.unschedule(self.update_pos)
return
self.emulation = True
Clock.schedule_interval(self.update_pos, 1. / 60.)