Last active
December 2, 2019 17:24
-
-
Save clayote/5fa5305925d716275e9c333262ba6c39 to your computer and use it in GitHub Desktop.
Trying to hand-compile KvLang into Python for optimization. It isn't working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import uniform | |
from kivy.config import Config | |
Config.set('kivy', 'log_level', 'debug') | |
from kivy.app import App | |
from kivy.clock import Clock | |
from kivy.graphics import Line | |
from kivy.lang import Builder | |
from kivy.properties import ListProperty | |
from kivy.factory import Factory | |
from kivy.uix.widget import Widget | |
from kivy.uix.boxlayout import BoxLayout | |
kv = """ | |
<BorderedKv>: | |
canvas: | |
Line: | |
points: self.mypoints | |
<Test>: | |
Button: | |
text: 'Add a new BorderedKv' | |
on_press: root.add_bordered_kv() | |
Button: | |
text: 'Add a new BorderedNoKv' | |
on_press: root.add_bordered_nokv() | |
RelativeLayout: | |
id: box | |
""" | |
class Bordered(Widget): | |
def _get_points(self): | |
return [self.x, self.y, self.right, self.y, self.right, self.top, | |
self.x, self.top, self.x, self.y] | |
def _redo_points(self, *args): | |
raise NotImplementedError | |
def _trigger_redo_points(self, *args): | |
Clock.unschedule(self._redo_points) | |
Clock.schedule_once(self._redo_points, 0) | |
class BorderedKv(Bordered): | |
mypoints = ListProperty([0] * 10) | |
def _redo_points(self, *args): | |
self.mypoints = self._get_points() | |
def __init__(self, **kwargs): | |
super(BorderedKv, self).__init__(**kwargs) | |
self.bind( | |
pos=self._trigger_redo_points, | |
size=self._trigger_redo_points | |
) | |
self._trigger_redo_points() | |
class BorderedNoKv(Bordered): | |
def _redo_points(self, *args): | |
self.border.points = self._get_points() | |
def on_canvas(self, *args): | |
with self.canvas: | |
self.border = Line(points=self._get_points) | |
self.bind(**{att: self._trigger_redo_points for att in ( | |
'x', 'y', 'size', 'pos' | |
)}) | |
class Test(BoxLayout): | |
def add_bordered_kv(self, *args): | |
x = uniform(0, self.ids.box.width - 30) | |
y = uniform(0, self.ids.box.height - 30) | |
width = uniform(0, self.ids.box.width - x) | |
height = uniform(0, self.ids.box.height - y) | |
self.ids.box.add_widget(BorderedKv(x=x, y=y, width=width, height=height)) | |
def add_bordered_nokv(self, *args): | |
x = uniform(0, self.ids.box.width - 30) | |
y = uniform(0, self.ids.box.height - 30) | |
width = uniform(0, self.ids.box.width - x) | |
height = uniform(0, self.ids.box.height - y) | |
self.ids.box.add_widget(BorderedNoKv(x=x, y=y, width=width, height=height)) | |
class TestApp(App): | |
def build(self): | |
return Test() | |
if __name__ == '__main__': | |
Builder.load_string(kv) | |
TestApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment