Skip to content

Instantly share code, notes, and snippets.

@dirkjot
Last active August 29, 2015 14:10
Show Gist options
  • Save dirkjot/2b67f7ffca4c53684ed0 to your computer and use it in GitHub Desktop.
Save dirkjot/2b67f7ffca4c53684ed0 to your computer and use it in GitHub Desktop.
in a kivy scatter with background image, only part of the image is dragable
Images to go with this are on google drive
==========================================
boring-bg.jpg
big background image (almost 800Kb), no useful content but created similarly to our actual backgrounds
https://drive.google.com/file/d/0B7pjQZ7_MEk5M2o0WlFuX2YzNDQ/view?usp=sharing
football_PNG1082.png
small football icon, our objects are full color but created in similar ways
https://drive.google.com/file/d/0B7pjQZ7_MEk5QS1TekpUUW0yc1k/view?usp=sharing
from kivy.config import Config
# this determines the size of the dragable box on the scatter, it seems
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '600')
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
class SceneScreen(App):
def build(self):
scatter = Scatter(#size=(3840, 2160), no effect
# pos=(100, 100), no effect
auto_bring_to_front=False,
do_rotation=False)
# background image:
img = Image(source="boring-bg.jpg",
pos=(0, 0),
size=(3840, 2160), #, <- CRUCIAL setting this will
# limit the dragable region to the size of the
# initial window, projected onto the background
# image (using scale, result appr 700x600 px)
# HOWEVER with size unset, the full background
# image is a dragable region, and in fact some
# area around it. However, the other 2 assets
# disappear off the window
size_hint=(None, None))
scatter.add_widget(img)
# with the image size above unset, this appears at very large scale and well
# above the background image.
obj = Image(source="football_PNG1082.png",
pos=(110, 110),
size_hint=(None, None))
scatter.add_widget(obj)
obj2 = Image(source="football_PNG1082.png",
pos=(1130, 2160 - 352),
size_hint=(None, None))
scatter.add_widget(obj2)
obj3 = Button(text="Click me", pos=(20,500), size=(150,150))
scatter.add_widget(obj3)
obj4 = Button(text="Click me too", pos=(1500,1500), size=(150,150))
scatter.add_widget(obj4)
from kivy.graphics.transformation import Matrix
optscale = min(Window.height / img.height,
Window.width / img.width)
print "Scaling to", optscale
mat = Matrix().scale(optscale, optscale, optscale)
scatter.apply_transform(mat)
# scatter.bbox = ((0,0),(80,60)) # setting this crashes kivy
float = FloatLayout(pos=(0, 0))
float.add_widget(scatter)
scatter.size = Window.size
return float
SceneScreen().run()
@dirkjot
Copy link
Author

dirkjot commented Nov 30, 2014

I ended up getting this to work, however in slightly different source. I haven't had the time yet to retest this with modifications, but in a nutshell:

  • make sure the scatter is not wrapped in a boxlayout , or something that may squish it.
  • do not use the transforms (scatter.apply_transform(mat) ) as they seem to shrink the touch area

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment