Changeset 151:082868bea873


Ignore:
Timestamp:
Sep 3, 2009, 8:32:56 PM (11 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@152
Message:

Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.

Location:
gamelib
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • gamelib/engine.py

    r142 r151  
    1515        self.main_app = main_app
    1616        self.clock = pygame.time.Clock()
     17        self.main_menu = mainmenu.make_main_menu()
     18        self._open_window = None
    1719
    1820    def tick(self):
     
    2022        pygame.time.wait(10)
    2123
     24    def open_window(self, window):
     25        """Open a widget as the main window."""
     26        if self._open_window is not None:
     27            self.main_app.close(self._open_window)
     28        self.main_app.open(window)
     29        self._open_window = window
     30
    2231    def create_game_board(self):
    23         self.gameboard = gameboard.GameBoard()
     32        """Create and open a gameboard window."""
     33        self.gameboard = gameboard.GameBoard(self.main_app)
     34        self.open_window(self.gameboard.get_top_widget())
    2435
    2536    def set_main_menu(self):
    26         """Create the main menu"""
    27         mainmenu.add_main_menu(self.main_app)
     37        """Open the main menu"""
     38        self.open_window(self.main_menu)
    2839
    29     def generate_score(self):
    30         """Create the Game Over state"""
    31         gameover.add_game_over(self.main_app, self.gameboard)
     40    def create_game_over(self):
     41        """Create and open the Game Over window"""
     42        game_over = gameover.create_game_over(self.gameboard)
     43        self.open_window(game_over)
    3244
    3345class MainMenuState(State):
     
    7587            return NightState(self.game)
    7688        elif e.type is KEYDOWN and e.key == K_ESCAPE:
    77             return MainMenuState(self.game)
     89            return GameOver(self.game)
    7890        elif e.type is KEYDOWN and e.key == K_n:
    7991            return pygame.event.post(START_NIGHT)
     
    8193            return MainMenuState(self.game)
    8294        elif e.type is not QUIT:
    83             self.game.gameboard.event(e)
     95            self.game.main_app.event(e)
    8496
    8597    def paint(self, screen):
    86         self.game.gameboard.paint(screen)
     98        self.game.main_app.paint(screen)
    8799        pygame.display.flip()
    88100
    89101    def update(self, screen):
    90         update = self.game.gameboard.update(screen)
     102        self.game.gameboard.update()
     103        update = self.game.main_app.update(screen)
    91104        pygame.display.update(update)
    92105
     
    117130            return pygame.event.post(START_DAY)
    118131        elif e.type is KEYDOWN and e.key == K_ESCAPE:
    119             return MainMenuState(self.game)
     132            return GameOver(self.game)
    120133        elif e.type is MOVE_FOX_ID:
    121134            self.cycle_count += 1
     
    126139                return pygame.event.post(START_DAY)
    127140        elif e.type is not QUIT:
    128             self.game.gameboard.event(e)
     141            self.game.main_app.event(e)
    129142
    130143    def loop(self):
     
    132145
    133146    def paint(self, screen):
    134         self.game.gameboard.paint(screen)
     147        self.game.main_app.paint(screen)
    135148        pygame.display.flip()
    136149
    137150    def update(self, screen):
    138         update = self.game.gameboard.update(screen)
     151        self.game.gameboard.update()
     152        update = self.game.main_app.update(screen)
    139153        pygame.display.update(update)
    140154
     
    142156    def init(self):
    143157        """Setup everything"""
    144         self.game.generate_score()
     158        self.game.create_game_over()
    145159        pygame.time.set_timer(MOVE_FOX_ID, 0)
    146160
  • gamelib/gameboard.py

    r148 r151  
    4949        self.add_counter(icons.CHKN_ICON, self.chicken_counter)
    5050        self.add_counter(icons.KILLED_FOX, self.killed_foxes)
     51        self.add_spacer()
    5152
    5253        self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS)
     
    6162            self.add_tool_button("Buy %s" % (equipment_cls.NAME,), equipment_cls)
    6263        self.add_spacer()
     64
    6365        self.add_button("Finished Day", self.day_done)
    6466
     
    7577    def add_spacer(self, height=30):
    7678        self.tr()
    77         self.add(gui.Spacer(0, height))
     79        self.td(gui.Spacer(0, height), colspan=2)
    7880
    7981    def add_tool_button(self, text, tool):
     
    8183
    8284    def add_button(self, text, func):
    83         button = gui.Button(text)
     85        button = gui.Button(text, width=self.rect.w)
    8486        button.connect(gui.CLICK, func)
    8587        self.tr()
    86         self.add(button, colspan=2)
     88        self.td(button, align=-1, colspan=2)
    8789
    8890    def add_counter(self, icon, label):
    8991        self.tr()
    90         if icon:
    91             self.td(icon, align=-1, width=self.rect.w/2)
    92         self.add(label)
     92        self.td(icon, align=-1, width=self.rect.w/2)
     93        self.td(label, align=-1, width=self.rect.w/2)
    9394
    9495class VidWidget(gui.Widget):
     
    9697        gui.Widget.__init__(self, **params)
    9798        self.gameboard = gameboard
    98         vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size))
    9999        self.vid = vid
    100         self.width = params.get('width', 0)
    101         self.height = params.get('height', 0)
     100        self.vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size))
    102101
    103102    def paint(self, surface):
     
    106105    def update(self, surface):
    107106        return self.vid.update(surface)
    108 
    109     def resize(self, width=0, height=0):
    110         if width is not None:
    111             self.width = width
    112         if height is not None:
    113             self.height = height
    114         return self.width, self.height
    115107
    116108    def move_view(self, x, y):
     
    131123    BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
    132124
    133     def __init__(self):
     125    def __init__(self, main_app):
     126        self.disp = main_app
    134127        self.tv = tiles.FarmVid()
    135128        self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
    136129        self.tv.png_folder_load_tiles(data.filepath('tiles'))
    137130        self.tv.tga_load_level(data.filepath('level1.tga'))
    138         self.create_disp()
     131        self.create_display()
    139132
    140133        self.selected_tool = None
     
    153146        self.add_some_chickens()
    154147
    155     def create_disp(self):
    156         width, height = pygame.display.get_surface().get_size()
     148    def get_top_widget(self):
     149        return self.top_widget
     150
     151    def create_display(self):
     152        width, height = self.disp.rect.w, self.disp.rect.h
    157153        tbl = gui.Table()
    158154        tbl.tr()
    159         self.toolbar = ToolBar(self)
    160         tbl.td(self.toolbar, width=self.TOOLBAR_WIDTH)
    161         self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height)
     155        self.toolbar = ToolBar(self, width=self.TOOLBAR_WIDTH)
     156        tbl.td(self.toolbar, valign=-1)
     157        self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH-19, height=height)
    162158        tbl.td(self.tvw)
    163         self.disp = gui.App()
    164         self.disp.init(tbl)
    165 
    166     def paint(self, screen):
    167         screen.fill(constants.BG_COLOR)
    168         self.disp.paint(screen)
    169 
    170     def update(self, screen):
     159        self.top_widget = tbl
     160
     161    def update(self):
    171162        self.tvw.reupdate()
    172         return self.disp.update(screen)
    173163
    174164    def loop(self):
  • gamelib/gameover.py

    r139 r151  
    88import imagecache
    99
    10 def add_game_over(app, gameboard):
    11     """Add the game over menu to the app"""
    12     for widget in app.widgets[:]:
    13         app.remove(widget)
     10def create_game_over(gameboard):
     11    """Create a game over screen"""
    1412    game_over = GameOver(gameboard)
    1513
     
    1715    c.add(game_over, 0, 0)
    1816
    19     app.init(c)
     17    return c
    2018
    2119class GameOverContainer(gui.Container):
  • gamelib/main.py

    r139 r151  
    1111from pygame.locals import SWSURFACE
    1212
    13 from mainmenu import MenuContainer, MainMenu
    14 from engine import Engine, MainMenuState
     13#from engine import Engine, MainMenuState
    1514from sound import init_sound
    1615import constants
    1716
    18 
    19 
    20 def create_app():
    21     """Create the app."""
     17def create_main_app(screen):
     18    """Create an app with a background widget."""
    2219    app = gui.App()
     20    background = pygame.Surface(screen.get_size())
     21    widget = gui.Image(background)
     22    app.init(widget, screen)
    2323    return app
    2424
     
    2727    init_sound()
    2828    screen = pygame.display.set_mode(constants.SCREEN, SWSURFACE)
    29     main_app = create_app()
     29    main_app = create_main_app(screen)
     30
     31    from engine import Engine, MainMenuState
     32
    3033    engine = Engine(main_app)
    3134    try:
  • gamelib/mainmenu.py

    r141 r151  
    77import imagecache
    88
    9 def add_main_menu(app):
    10     """Add the main menu to the app"""
    11     for widget in app.widgets[:]:
    12         app.remove(widget)
    13 
     9def make_main_menu():
     10    """Create a main menu"""
    1411    main_menu = MainMenu()
    1512
     
    1714    c.add(main_menu, 0, 0)
    1815
    19     app.init(c)
     16    return c
    2017
    2118class MenuContainer(gui.Container):
Note: See TracChangeset for help on using the changeset viewer.