Some MSB is required when required.
[naja.git] / naja / widgets / tile.py
index cba4658e147b59558cba4e28709b9e0e77a359e2..06a0f78a3baaceca3047c8e40af29f38b07b46ef 100644 (file)
@@ -1,17 +1,23 @@
 # These will probably need to go away when we have images
 import pygame
-import pygame.locals as pgl
 
-from naja.constants import TILE_SIZE, BITS
+from naja.constants import (
+    TILE_SIZE, BITS, LOCK_HEIGHT, SMALL_LOCK_HEIGHT, EXAMINE, PALETTE)
 from naja.resources import resources
+from naja.resources.mutators import EIGHT_BIT, blender
 from naja.widgets.base import Widget
 
 
 BIT_MAP = {
-        BITS['CYAN']: 'board/tile_cyan.png',
-        BITS['MAGENTA']: 'board/tile_magenta.png',
-        BITS['YELLOW']: 'board/tile_yellow.png',
-        }
+    frozenset([BITS.RED]): 'board/tile_red.png',
+    frozenset([BITS.GREEN]): 'board/tile_green.png',
+    frozenset([BITS.BLUE]): 'board/tile_blue.png',
+    frozenset([BITS.RED, BITS.GREEN]): 'board/tile_red_green.png',
+    frozenset([BITS.RED, BITS.BLUE]): 'board/tile_red_blue.png',
+    frozenset([BITS.GREEN, BITS.BLUE]): 'board/tile_green_blue.png',
+    frozenset([BITS.RED, BITS.GREEN, BITS.BLUE]):
+        'board/tile_red_green_blue.png',
+}
 
 
 class TileWidget(Widget):
@@ -19,32 +25,135 @@ class TileWidget(Widget):
     def __init__(self, pos, state=None, board_pos=None):
         super(TileWidget, self).__init__(pos, TILE_SIZE)
         self.state = state
+        self.current_card = None
         self.board_pos = board_pos
+        self.highlighted = False
+        self.animation = TILE_SIZE[0]
 
     def prepare(self):
-        # Placeholder logic - just draw the outline of a square
+        # Draw background
+        x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
+
+        if self.state.gameboard.puzzle:
+            tile_1_name = 'board/tile_1.png'
+            tile_2_name = 'board/tile_2_puzzle.png'
+            tile_available_name = 'board/tile_available_puzzle.png'
+        else:
+            tile_1_name = 'board/tile_1.png'
+            tile_2_name = 'board/tile_2.png'
+            tile_available_name = 'board/tile_available.png'
+
+        if (x + y) % 2 == 0:
+            bg_name = tile_2_name
+        else:
+            bg_name = tile_1_name
+        bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
+        overlays = []
+
+        legal_move = (self.board_pos in self.state.player.legal_moves())
+
+        if self.state.gameboard.player_mode == EXAMINE and legal_move:
+            overlays.append(resources.get_image(
+                tile_available_name, transforms=(EIGHT_BIT,)))
+        if self.highlighted:
+            overlays.append(resources.get_image(
+                'board/tile_selected.png',
+                transforms=(EIGHT_BIT,)))
+
         self.surface = pygame.surface.Surface(TILE_SIZE)
-        pygame.draw.lines(self.surface, pgl.color.THECOLORS['yellow'],
-                          True, [(1, 1), (1, 95), (95, 95), (95, 1)], 2)
-        self.surface.convert_alpha(pygame.display.get_surface())
+        self.surface.blit(bg, (0, 0))
+        for overlay in overlays:
+            self.surface.blit(overlay, (0, 0))
         # Look up the required bits on the board location
-        card = self.state.gameboard.board_locations[self.board_pos]
-        bits = []
+        card = self.state.board_locations[self.board_pos]
+        if card is not self.current_card:
+            self.animation = TILE_SIZE[0]
+            self.current_card = card
+
+        y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
         for action in card.actions:
-            if action.required_bits:
-                bits.append(action.required_bits)
-        # Sort so we have longer lists of bits later
-        self.size = self.surface.get_rect().size
-        if bits:
-            bits.sort(key=lambda x: len(x))
-            y_offset = TILE_SIZE[1] - 32 * len(bits)
-            for pattern in bits:
-                x_offset = 10
-                for bit in pattern:
-                    img = resources.get_image(BIT_MAP[bit])
-                    self.surface.blit(img, (x_offset, y_offset))
-                    x_offset += 32
-                y_offset += 32
+            y_offset = self._prepare_action(action, y_offset)
+
+        self._prepare_countdown(card)
+
+    def _prepare_countdown(self, card):
+        if card.replacement_time is None:
+            return
+        elif card.replacement_time <= 1:
+            glyph = 'glyphs/countdown_1.png'
+        elif card.replacement_time == 2:
+            glyph = 'glyphs/countdown_2.png'
+        elif card.replacement_time == 3:
+            glyph = 'glyphs/countdown_3.png'
+        elif card.replacement_time < 8:
+            glyph = 'glyphs/countdown_4.png'
+        else:
+            glyph = 'glyphs/countdown_5.png'
+        img = resources.get_image(
+            glyph, transforms=(EIGHT_BIT, blender(PALETTE.DARK_VIOLET)))
+        self.surface.blit(img, (TILE_SIZE[0] - 20, 0))
+
+    def _prepare_lock(self, action, y_offset):
+        required_keys = action.required_bits & frozenset([
+            BITS.RED, BITS.GREEN, BITS.BLUE])
+        if required_keys not in BIT_MAP:
+            return 4
+
+        img_name = BIT_MAP[required_keys]
+
+        if self.board_pos != self.state.player.position:
+            x_offset = 0
+        else:
+            img_name = img_name.replace('.png', '_small.png')
+            x_offset = 2
+            if y_offset == LOCK_HEIGHT:
+                y_offset = 0  # middle -> top
+            elif y_offset == 2 * LOCK_HEIGHT:
+                # bottom -> further down
+                y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
+
+        img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
+        img_rect = img.get_rect()
+        self.surface.blit(img, (x_offset, y_offset))
+
+        if BITS.MSB in action.required_bits:
+            msb = resources.get_image('board/msb_lock_decoration.png',
+                                      transforms=(EIGHT_BIT,))
+            msb_rect = msb.get_rect()
+            self.surface.blit(
+                msb, (x_offset + img_rect.width - msb_rect.width, y_offset)
+            )
+
+        return x_offset + img_rect.width + 2
+
+    def _prepare_action(self, action, y_offset):
+        x_offset = self._prepare_lock(action, y_offset)
+        if self.board_pos != self.state.player.position:
+            for glyph in action.get_glyphs():
+                img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
+                self.surface.blit(img, (x_offset, y_offset + 4))
+                x_offset += img.get_width()
+            if action.get_msb_glyph() is not None:
+                img = resources.get_image(
+                    action.get_msb_glyph(),
+                    transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
+                self.surface.blit(img, (x_offset, y_offset + 4))
+                x_offset += img.get_width()
+        return y_offset + LOCK_HEIGHT
+
+    def set_highlight(self, pos):
+        self.highlighted = False
+        if (self.state.gameboard.player_mode == EXAMINE and
+                self.board_pos == pos):
+            self.highlighted = True
 
     def draw(self, surface):
-        surface.blit(self.surface, self.pos)
+        scaled_width = self.surface.get_width() - self.animation
+        scaled_height = self.surface.get_height() - self.animation
+        scaled_position = (self.pos[0] + (self.animation / 2),
+                           self.pos[1] + (self.animation / 2))
+        if self.animation > 0:
+            self.animation = max(0, self.animation - 4)
+        scaled_surface = pygame.transform.scale(
+            self.surface, (scaled_width, scaled_height))
+        surface.blit(scaled_surface, scaled_position)