Skip to content
Permalink
Browse files

Added a first pass at scrolling the viewport.

  • Loading branch information...
dfloer committed Nov 5, 2018
1 parent 7bbd679 commit d0514599b750ae6f6039d35b957e8ef3937e44d0
Showing with 27 additions and 7 deletions.
  1. +27 −7 cocos2d.py
@@ -27,7 +27,6 @@

director.init(window_width, window_height, window_title, autoscale=False)
keyboard = key.KeyStateHandler()
scroller = ScrollingManager()


class Terrain:
@@ -170,7 +169,7 @@ def on_mouse_press(self, x, y, button, dy):
button: which button was pushed.
dy: no idea what this does.
"""
p = Point(x, y)
p = Point(x + scroller.offset[0], y + scroller.offset[1])
raw = hex_math.pixel_to_hex(layout, p)
h = hex_math.hex_round(raw)
position = hex_math.hex_to_pixel(layout, h, False)
@@ -182,7 +181,7 @@ def on_mouse_press(self, x, y, button, dy):
self.add(self.selected_batch)

def on_mouse_motion(self, x, y, dx, dy):
p = Point(x, y)
p = Point(x + scroller.offset[0], y + scroller.offset[1])
raw = hex_math.pixel_to_hex(layout, p)
h = hex_math.hex_round(raw)
position = hex_math.hex_to_pixel(layout, h, False)
@@ -196,10 +195,6 @@ def on_mouse_motion(self, x, y, dx, dy):
self.last_hex = sprite
# print(f"mouse move: ({x}, {y}), dx: {dx}, dy: {dy}.")

def on_key_press(self, key, modifiers):
print(key, modifiers)


def set_view(self, x, y, w, h, viewport_ox=0, viewport_oy=0):
"""
A stub to get things working.
@@ -236,6 +231,30 @@ def plop_building(self, cell, building_id):
pass


class InputScrolling(ScrollingManager):
is_event_handler = True

def __init__(self, center):
super().__init__()
self.center = list(center)
self.scroll_inc = 10
self.offset = [0, 0]

def on_key_press(self, key, modifiers):
if key == 65362: # up arrow
self.offset[1] -= self.scroll_inc
if key == 65364: # down arrow
self.offset[1] += self.scroll_inc
if key == 65363: # right arrow
self.offset[0] -= self.scroll_inc
if key == 65361: # left arrow
self.offset[0] += self.scroll_inc
new_center = [sum(x) for x in zip(self.center, self.offset)]
self.set_focus(*new_center)

def set_focus(self, *args, **kwargs):
super().set_focus(*args, **kwargs)


class MenuLayer(Menu):
is_event_handler = True
@@ -246,6 +265,7 @@ def __init__(self):


if __name__ == "__main__":
scroller = InputScrolling(layout.origin)
terrain_map = Terrain()
terrain_map.generate_chunk(layout.origin, (7, 7))
building_layer = BuildingLayer()

0 comments on commit d051459

Please sign in to comment.
You can’t perform that action at this time.