Very hack'ish scrolling support
authorNeil <neil@dip.sun.ac.za>
Tue, 16 Apr 2013 14:50:50 +0000 (16:50 +0200)
committerNeil <neil@dip.sun.ac.za>
Tue, 16 Apr 2013 14:50:50 +0000 (16:50 +0200)
erdslangetjie/__main__.py

index 45f907db55428f435f20fe21c35dac56391cee3e..3234d1b2dab8242b954c5517729ccbcc83762797 100644 (file)
@@ -7,8 +7,10 @@ kivy.require('1.6.0')
 from kivy.app import App
 from kivy.core.window import Window
 from kivy.uix.widget import Widget
-from kivy.uix.floatlayout import FloatLayout
+from kivy.uix.relativelayout import RelativeLayout
+from kivy.uix.scrollview import ScrollView
 from kivy.graphics import Color, Rectangle
+from kivy.utils import platform
 
 from erdslangetjie.level import LevelList
 from erdslangetjie.player import ThePlayer, Nemesis
@@ -16,14 +18,20 @@ from erdslangetjie.player import ThePlayer, Nemesis
 TILE_SIZE = 40
 
 
-class GameWindow(FloatLayout):
+class GameWindow(RelativeLayout):
 
-    def __init__(self, level_list):
-        super(GameWindow, self).__init__(size=(1080, 800))
+    def __init__(self, level_list, view):
         self.level_list = level_list
         self.level_obj = self.level_list.get_current_level()
         self.level_obj.load_tiles()
         self.tiles = {}
+        self.view = view
+
+        rows, cols = self.level_obj.get_size()
+
+        super(GameWindow, self).__init__(
+                size=(cols*TILE_SIZE, rows*TILE_SIZE),
+                size_hint=(None, None))
 
         self.mouse_move = False
 
@@ -35,8 +43,10 @@ class GameWindow(FloatLayout):
         self.nemesis_tile = None
 
         self.player.pos = self.level_obj.enter_pos
-        self.keyboard = Window.request_keyboard(self._closed, self)
-        self.keyboard.bind(on_key_down=self._on_key_down)
+        if platform() != 'android':
+            # Very hack'ish
+            self.keyboard = Window.request_keyboard(self._closed, self)
+            self.keyboard.bind(on_key_down=self._on_key_down)
 
     def build(self):
         self.clear_widgets()
@@ -46,12 +56,14 @@ class GameWindow(FloatLayout):
         for tile_line in tiles:
             bx = 0
             for tile in tile_line:
-                node = Widget(size=(TILE_SIZE, TILE_SIZE), pos=(bx, by))
+                node = Widget(size=(TILE_SIZE, TILE_SIZE),
+                        pos=(bx, by),
+                        size_hint=(None, None))
+                self.add_widget(node)
                 with node.canvas:
                     Color(1, 1, 1)
                     Rectangle(pos=node.pos, size=node.size,
                             texture=tile.texture)
-                self.add_widget(node)
                 self.tiles[(bx, by)] = node
                 bx += TILE_SIZE
             by += TILE_SIZE
@@ -71,6 +83,34 @@ class GameWindow(FloatLayout):
             Rectangle(pos=sprite_pos, size=self.player_tile.size,
                     texture=self.player.get_texture())
         self.add_widget(self.player_tile)
+        # Player position in viewpoint coordinates
+        check_point = (sprite_pos[0] + TILE_SIZE / 2,
+                sprite_pos[1] + TILE_SIZE / 2)
+        true_point = self.to_parent(*check_point)
+        if not self.included(true_point):
+            # Scroll ourselves
+            while true_point[0] >= self.view.size[0] - TILE_SIZE:
+                self.pos = (self.pos[0] - 1.5 * TILE_SIZE, self.pos[1])
+                true_point = self.to_parent(*check_point)
+            while true_point[0] < TILE_SIZE:
+                self.pos = (self.pos[0] + 1.5 * TILE_SIZE, self.pos[1])
+                true_point = self.to_parent(*check_point)
+            while true_point[1] >= self.view.size[1] - TILE_SIZE:
+                self.pos = (self.pos[0], self.pos[1] - 1.5 * TILE_SIZE)
+                true_point = self.to_parent(*check_point)
+            while true_point[1] < TILE_SIZE:
+                self.pos = (self.pos[0], self.pos[1] + 1.5 * TILE_SIZE)
+                true_point = self.to_parent(*check_point)
+
+    def included(self, point):
+        if point[0] < TILE_SIZE:
+            return False
+        if point[0] >= self.view.pos[0] - TILE_SIZE:
+            return False
+        if point[1] < TILE_SIZE:
+            return False
+        if point[1] >= self.view.pos[1] - TILE_SIZE:
+            return False
 
     def draw_nemesis(self):
         if not self.nemesis.on_board():
@@ -130,6 +170,7 @@ class GameWindow(FloatLayout):
             sys.exit(1)
 
     def _calc_mouse_pos(self, pos):
+        pos = self.to_local(*pos)
         return (int(pos[0] / TILE_SIZE), int(pos[1] / TILE_SIZE))
 
     def on_touch_down(self, touch):
@@ -159,9 +200,13 @@ class GameApp(App):
         super(GameApp, self).__init__()
 
     def build(self):
-        game = GameWindow(self.levels)
+        root = ScrollView(size=(640, 480), size_hint=(None, None))
+        game = GameWindow(self.levels, root)
         game.build()
-        return game
+        root.add_widget(game)
+        # Ensure the player is visible
+        game.draw_player()
+        return root
 
 
 def main():