Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #!/usr/bin/kivy
  2.  
  3. import kivy
  4. kivy.require('1.0.6')
  5.  
  6. from glob import glob
  7. from random import randint
  8. from os.path import join, dirname
  9. from kivy.app import App
  10. from kivy.logger import Logger
  11. from kivy.uix.scatter import Scatter
  12. from kivy.properties import StringProperty
  13. # FIXME this shouldn't be necessary
  14. from kivy.core.window import Window
  15. from kivy.properties import ObjectProperty
  16. from kivy.graphics.transformation import Matrix
  17. from kivy.clock import Clock
  18. from kivy.lang import Builder
  19.  
  20. Builder.load_string("""
  21. <-SmoothedScatter>:
  22. canvas.before:
  23. PushMatrix
  24. MatrixInstruction:
  25. matrix: self.target_transform
  26. canvas.after:
  27. PopMatrix
  28. """)
  29.  
  30.  
  31. class SmoothedScatter(Scatter):
  32.  
  33. target_transform = ObjectProperty(Matrix())
  34.  
  35. def __init__(self, **kwargs):
  36. super(SmoothedScatter, self).__init__(**kwargs)
  37. Clock.schedule_interval(self.target_transform_update, 1 / 60.)
  38.  
  39. def target_transform_update(self, *args):
  40. transform = list(self.transform.get())
  41. target_transform = list(self.target_transform.get())
  42. for i in range(4):
  43. target_transform[i] = list(target_transform[i])
  44. for j in range(4):
  45. b = transform[i][j]
  46. a = target_transform[i][j]
  47. d = (b - a) / 3.
  48. target_transform[i][j] += d
  49. m = Matrix()
  50. m.set(target_transform)
  51. self.target_transform = m
  52.  
  53.  
  54. class Picture(SmoothedScatter):
  55. '''Picture is the class that will show the image with a white border and a
  56. shadow. They are nothing here because almost everything is inside the
  57. picture.kv. Check the rule named <Picture> inside the file, and you'll see
  58. how the Picture() is really constructed and used.
  59.  
  60. The source property will be the filename to show.
  61. '''
  62.  
  63. source = StringProperty(None)
  64.  
  65.  
  66. class PicturesApp(App):
  67.  
  68. def build(self):
  69.  
  70. # the root is created in pictures.kv
  71. root = self.root
  72.  
  73. # get any files into images directory
  74. curdir = dirname(__file__)
  75. for filename in glob(join(curdir, 'images', '*')):
  76. try:
  77. # load the image
  78. picture = Picture(source=filename, rotation=randint(-30, 30))
  79. # add to the main field
  80. root.add_widget(picture)
  81. except Exception as e:
  82. Logger.exception('Pictures: Unable to load <%s>' % filename)
  83.  
  84. def on_pause(self):
  85. return True
  86.  
  87.  
  88. if __name__ == '__main__':
  89. PicturesApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement