Merge branch 'master' of git://ctpug.org.za/naja
[naja.git] / naja / widgets / info_area.py
index 85f4bda09e0fbc1fd032f75edfca4444e7a95e38..1ff6bbbd92168e52ec12c4a489cf6f54638f95a7 100644 (file)
@@ -4,29 +4,33 @@ Widget for the game board information area.
 import pygame
 import pygame.locals as pgl
 
-from naja.constants import (INFO_SIZE, EIGHT_BIT_SCALE, ACT, KEYS,
-                            EXAMINE)
+from naja.constants import (
+    INFO_SIZE, ACT, KEYS, EXAMINE, PALETTE,
+    ACTION_TEXT_OFFSET, INFO_LEFT_PADDING,
+    INFO_RIGHT_PADDING, BIT_SIZE, BITS)
 from naja.events import finish_event
 from naja.resources import resources
-from naja.resources.mutators import EIGHT_BIT
+from naja.resources.mutators import EIGHT_BIT, blender
 from naja.sound import sound
+from naja.utils import bit_glyphs
 
 from naja.widgets.base import Widget
 from naja.widgets.tile import BIT_MAP
 from naja.widgets.text import TextBoxWidget, TextWidget
+from naja import constants
 
 
 HINTS = {
-    ACT: ("Choose an action using the Up/Down keys.\n"
-          "Press Return to execute the action."),
-    EXAMINE: "Select a card to examine using the arrow keys.",
+    ACT: "Choose an action using {NORTH,SOUTH} keys.\n"
+         "Press {RETURN} to execute it.",
+    EXAMINE: "Browse the tiles with {NORTH,SOUTH,EAST,WEST} keys.",
 }
 
-HINT_LEGAL_MOVE = "\nPress Return to move to this card."
+HINT_LEGAL_MOVE = "\nPress {RETURN} to move to this tile."
 
 TITLES = {
     ACT: "Choose an Action",
-    EXAMINE: "Select a Card",
+    EXAMINE: "Select a Tile",
 }
 
 
@@ -40,62 +44,138 @@ class InfoAreaWidget(Widget):
         self.chosen = None
         self.card_position = state.player.position
         self.set_position(state.player.position)
+        self.flash_count = 0
+        self.flash_light = True
 
     def prepare(self):
         if self.state.gameboard.player_mode == ACT:
             self.set_position(self.state.player.position)
         self.surface = pygame.surface.Surface(INFO_SIZE)
         self.surface.fill((0, 0, 0))
-        # Extract actions and such from the card
-        title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
-                           colour=(255, 255, 255))
-        title.render(self.surface)
-        y_offset = title.surface.get_rect().height + 8
 
-        # TODO: Make this better.
+        box_width = INFO_SIZE[0] - INFO_LEFT_PADDING - INFO_RIGHT_PADDING
+        y_offset = 0
+        pos = lambda: (INFO_LEFT_PADDING, y_offset)
+
+        # Bits
+        y_offset += 12
         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
                             for bit in reversed(range(8)))
-        card_bits = TextWidget((0, y_offset), bits_text, colour=(255, 255, 0))
-        card_bits.render(self.surface)
-        y_offset += card_bits.surface.get_rect().height + 8
-
+        if self.card.bitwise_operand:
+            bits_text = '%s %s' % (
+                bits_text, bit_glyphs(self.card.bitwise_operand))
+        card_bits = TextBoxWidget(
+            (0, 0), bits_text, padding=4, centre=True,
+            colour=PALETTE.WHITE, border=2,
+            bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
+            box_width=box_width)
+        card_bits.prepare()
+        self.surface.blit(card_bits.surface, pos())
+        y_offset += card_bits.surface.get_rect().height + 12
+
+        # Actions
         for choice, action in enumerate(self.card.actions):
-            y_offset = self.prepare_action(choice, action, y_offset)
-        # We cheat horribly for layout reasons
+            y_offset = self.prepare_action(choice, action, y_offset, box_width)
+
+        # Hint text
         hint_text = HINTS[self.state.gameboard.player_mode]
         if self.state.gameboard.player_mode == EXAMINE:
             if self.card_position in self.state.player.legal_moves():
                 hint_text += HINT_LEGAL_MOVE
 
-        hint = TextBoxWidget((4, 0), hint_text, padding=2,
-                             box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
+        hint = TextBoxWidget(
+            (0, 0), hint_text, padding=4,
+            colour=PALETTE.WHITE, border=2,
+            bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
+            box_width=box_width)
         hint.prepare()
-        y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
-        self.surface.blit(hint.surface, (4, y_offset))
+        y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
+                    - BIT_SIZE[1] - 2)
+        self.surface.blit(hint.surface, pos())
+
+        # Game mode (puzzle, easy, standard, hard, etc ...)
+        game_mode = TextBoxWidget(
+            (0, 0), self.mode_hint(), padding=4, centre=True,
+            colour=PALETTE.WHITE, border=2,
+            bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
+            box_width=box_width)
+        game_mode.prepare()
+
+        y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
+        self.surface.blit(game_mode.surface, pos())
+
+    def mode_hint(self):
+        gameboard = self.state.gameboard
+        if gameboard.puzzle:
+            return "PUZZLE"
+        return {
+            0: "DEATH", 1: "LUDICROUS",
+            2: "RAMBO", 3: "HARD",
+            4: "STANDARD", 5: "EASY",
+        }.get(gameboard.max_health, "UNKNOWN")
+
+    def prepare_action(self, choice, action, y_offset, box_width):
+        x_offset = INFO_LEFT_PADDING
+        glyphs_x_offset = 2
+        glyphs_y_offset = y_offset
+        y_offset += ACTION_TEXT_OFFSET
+        action_viable = action.check_available(self.state.player)
+        text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
 
-    def prepare_action(self, choice, action, y_offset):
         text = TextBoxWidget(
-            (18, y_offset), action.get_text(),
-            box_width=(INFO_SIZE[0] - 16) // EIGHT_BIT_SCALE,
-            fontsize=28)
+            (x_offset, y_offset), action.get_text(self.card),
+            box_width=box_width,
+            fontsize=28, colour=text_colour,
+            border=2, border_colour=PALETTE.GREY)
         text.render(self.surface)
-        # self.chosen may be None, in which case we don't draw the border.
+
+        border_colour = None
         if choice == self.chosen:
-            if not action.check_available(self.state.player):
-                colour = (255, 0, 0, 255)
+            if self.flash_light:
+                border_colour = (PALETTE.GREEN if action_viable else
+                                 PALETTE.ORANGE)
             else:
-                colour = (255, 255, 0, 128)
+                border_colour = (PALETTE.DARK_GREEN if action_viable else
+                                 PALETTE.DARK_RED)
+
+        if border_colour:
             bottom = y_offset + text.surface.get_rect().height
-            right = text.surface.get_rect().width + 12
-            pygame.draw.lines(self.surface, colour, True,
-                              [(18, y_offset), (right, y_offset),
-                               (right, bottom), (18, bottom)], 4)
-        if action.required_bits:
-            img_name = BIT_MAP[action.required_bits].replace(
+            right = text.surface.get_rect().width + x_offset
+            pygame.draw.lines(self.surface, border_colour, True,
+                              [(x_offset, y_offset), (right, y_offset),
+                               (right, bottom), (x_offset, bottom)], 4)
+
+        required_keys = action.required_bits & frozenset([
+            BITS.RED, BITS.GREEN, BITS.BLUE])
+        if required_keys in BIT_MAP:
+            img_name = BIT_MAP[required_keys].replace(
                 '.png', '_small.png')
             img = resources.get_image(img_name,
                                       transforms=(EIGHT_BIT,))
-            self.surface.blit(img, (0, y_offset))
+            self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
+            glyphs_x_offset += img.get_width() + 4
+        else:
+            glyphs_x_offset = INFO_LEFT_PADDING
+
+        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, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
+            )
+
+        for glyph in action.get_glyphs():
+            img = resources.get_image(
+                glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
+            self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
+            glyphs_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, (glyphs_x_offset, glyphs_y_offset - 4))
+
         return y_offset + text.surface.get_rect().height + 16
 
     def set_position(self, position):
@@ -108,38 +188,56 @@ class InfoAreaWidget(Widget):
             self.chosen = None
 
     def draw(self, surface):
+        self.flash_count += 1
+        if self.flash_count >= (constants.FPS // 2):
+            self.flash_light = not self.flash_light
+            self.flash_count = 0
+            self.prepare()
         surface.blit(self.surface, self.pos)
 
+    def next_action(self, viable_only=False, step=1):
+        num_actions = len(self.card.actions)
+        if num_actions == 0:
+            return
+        player = self.state.player
+        chosen = self.chosen
+        for i in range(num_actions - 1):
+            # loop through each action at most once.
+            chosen = (chosen + step) % num_actions
+            action = self.card.actions[chosen]
+            if not viable_only or action.check_available(player):
+                sound.play_sound('zoop.ogg', volume=0.05)
+                self.chosen = chosen
+
+    def prev_action(self, viable_only=False):
+        return self.next_action(viable_only=viable_only, step=-1)
+
+    def try_perform_action(self):
+        player = self.state.player
+        action = self.card.actions[self.chosen]
+        if not action.check_available(player):
+            sound.play_sound('error.ogg')
+        else:
+            sound.play_sound('chirp.ogg', volume=0.5)
+            action.perform_action(self.state.gameboard, self.card)
+            self.state.gameboard.card_used(player.position)
+            self.state.gameboard.change_mode(EXAMINE)
+            self.set_position(player.position)
+
     def handle_event(self, ev):
         if self.state.gameboard.player_mode == EXAMINE:
             return super(InfoAreaWidget, self).handle_event(ev)
         if ev.type == pgl.KEYDOWN:
             if ev.key in KEYS.SELECT:
-                player = self.state.player
-                action = self.card.actions[self.chosen]
-                if not action.check_available(player):
-                    sound.play_sound('error.ogg')
-                else:
-                    action.perform_action(self.state.gameboard, self.card)
-                    self.state.gameboard.replace_card(player.position)
-                    self.state.gameboard.change_mode(EXAMINE)
-                    self.set_position(player.position)
+                self.try_perform_action()
                 return finish_event()
             if ev.key in KEYS.UP:
-                if self.chosen > 0:
-                    self.chosen -= 1
-                    sound.play_sound('change_action.ogg')
+                self.next_action()
                 return finish_event()
             if ev.key in KEYS.DOWN:
-                if self.chosen + 1 < len(self.card.actions):
-                    sound.play_sound('change_action.ogg')
-                    self.chosen += 1
+                self.prev_action()
                 return finish_event()
             if ev.key in KEYS.SWITCH:
-                sound.play_sound('change_action.ogg')
-                if self.chosen + 1 < len(self.card.actions):
-                    self.chosen += 1
-                else:
-                    self.chosen = 0
+                self.next_action(viable_only=True)
                 return finish_event()
         return super(InfoAreaWidget, self).handle_event(ev)