Skip to content

Instantly share code, notes, and snippets.

@hansent
Created April 26, 2012 13:29
Show Gist options
  • Save hansent/2499575 to your computer and use it in GitHub Desktop.
Save hansent/2499575 to your computer and use it in GitHub Desktop.
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.scatter import ScatterPlane
class Viewport(ScatterPlane):
def __init__(self, **kwargs):
kwargs.setdefault('size', (1920, 1080))
kwargs.setdefault('size_hint', (None, None))
kwargs.setdefault('do_scale', False)
kwargs.setdefault('do_translation', False)
kwargs.setdefault('do_rotation', False)
super(Viewport, self).__init__( **kwargs)
Clock.schedule_once(self.fit_to_window, -1)
def fit_to_window(self, *args):
if self.width < self.height: #portrait
if Window.width < Window.height: #so is window
self.scale = Window.width/float(self.width)
else: #window is landscape..so rotate vieport
self.scale = Window.height/float(self.width)
self.rotation = -90
else: #landscape
if Window.width > Window.height: #so is window
self.scale = Window.width/float(self.width)
else: #window is portrait..so rotate vieport
self.scale = Window.height/float(self.width)
self.rotation = -90
self.center = Window.center
for c in self.children:
c.size = self.size
def add_widget(self, w, *args, **kwargs):
super(Viewport, self).add_widget(w, *args, **kwargs)
w.size = self.size
def on_touch_down(self, touch):
return super(Viewport, self).on_touch_down(touch)
def on_touch_move(self, touch):
return super(Viewport, self).on_touch_move(touch)
def on_touch_up(self, touch):
return super(Viewport, self).on_touch_up(touch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment