Skip to content
Browse files

added zoom

  • Loading branch information...
1 parent b823095 commit b86415eb91f11747a2fe62c223ab6902a95a0463 @zielmicha committed Apr 15, 2012
Showing with 38 additions and 6 deletions.
  1. +16 −1 lib/gamemenu.py
  2. +22 −5 lib/gamescreen.py
View
17 lib/gamemenu.py
@@ -49,17 +49,32 @@ def get_order_sprite(name):
i = order_sprites_names.index(name)
return order_sprites[i][0]
+ZOOM_LEVELS = [0.3, 0.5, 0.7, 1, 1.3, 1.6, 2, 2.5]
+
class Menu(ui.LinearLayoutWidget):
def __init__(self, client):
ui.LinearLayoutWidget.__init__(self)
self.client = client
+ self.zoom_panel = ui.HorizontalLayoutWidget(spacing=4)
+ self.zoom_panel.add(ui.Button('+', self.incr_zoom, force_width=20))
+ self.zoom_panel.add(ui.Button(' - ', self.decr_zoom, force_width=20))
+ self.zoom_level = ZOOM_LEVELS.index(1)
self.panel = ui.HorizontalLayoutWidget(spacing=4)
+ self.add(self.zoom_panel)
self.add(self.panel)
self.panel.add(ui.Label('loading...', color=(0, 255, 0)))
+ def incr_zoom(self, i=1):
+ self.zoom_level += i
+ self.zoom_level = max(0, min(len(ZOOM_LEVELS) - 1, self.zoom_level))
+ self.client.ui.map.zoom = ZOOM_LEVELS[self.zoom_level]
+
+ def decr_zoom(self):
+ self.incr_zoom(-1)
+
def update(self, unit):
self.panel.items = []
- self.items = [self.panel]
+ self.items = [self.zoom_panel, self.panel]
if unit:
self.update_joystick()
self.update_actions(unit)
View
27 lib/gamescreen.py
@@ -398,7 +398,8 @@ def __init__(self, client):
self.client = client
self.last_size = (0, 0)
self.size = (0, 0)
-
+ self.zoom = 1
+ self.dest_surf = pygame.Surface(self.get_real_size())
self.last_recentered_at = None
self.start_drag = None
self.last_drag_pos = None
@@ -410,22 +411,38 @@ def tick(self):
pass
def draw(self, surf, pos):
- if self.size != self.last_size:
- self.client.set_map_size(self.size)
- self.last_size = self.size
+ if self.get_real_size() != self.last_size:
+ real = self.get_real_size()
+ self.client.set_map_size(real)
+ self.last_size = real
+ self.dest_surf = pygame.Surface(self.get_real_size())
#self.last_frame_updated += 1
#if self.last_frame_updated == 3:
# self.last_frame_updated = 0
#self.client.update_map_canvas_visible()
- self.client.draw_map(surf, pos)
+ if self.zoom != 1:
+ self.client.draw_map(self.dest_surf, (0,0))
+ try:
+ scale_dest = surf.subsurface(pos + self.size)
+ pygame.transform.scale(self.dest_surf, self.size, scale_dest)
+ except ValueError:
+ pass
+ else:
+ self.client.draw_map(surf, pos)
+
+ def get_real_size(self):
+ x, y = self.size
+ return int(x / self.zoom), int(y / self.zoom)
def back(self):
self.client.escape()
def event(self, ev):
+ if hasattr(ev, 'pos'):
+ ev.pos = (int(ev.pos[0] / self.zoom), int(ev.pos[1] / self.zoom))
if ev.type == pygame.MOUSEMOTION:
if self.start_drag:
if not self.was_dragged:

0 comments on commit b86415e

Please sign in to comment.
Something went wrong with that request. Please try again.