Mercurial > boomslang
changeset 70:213e47dea4d0
Funkier cursor handling
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Mon, 23 Aug 2010 20:54:04 +0200 |
parents | d4bbb26099cc |
children | 99c5506de7ea |
files | gamelib/constants.py gamelib/cursor.py gamelib/gamescreen.py gamelib/hand.py gamelib/popupmenu.py |
diffstat | 5 files changed, 47 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/constants.py Mon Aug 23 20:50:36 2010 +0200 +++ b/gamelib/constants.py Mon Aug 23 20:54:04 2010 +0200 @@ -1,6 +1,7 @@ # Useful constants # copyright boomslang team (see COPYRIGHT file for details) + SCREEN = (800, 600) FREQ = 44100 # same as audio CD BITSIZE = -16 # unsigned 16 bit
--- a/gamelib/cursor.py Mon Aug 23 20:50:36 2010 +0200 +++ b/gamelib/cursor.py Mon Aug 23 20:54:04 2010 +0200 @@ -3,9 +3,15 @@ # Sprite Cursor from albow.screen import Screen +from albow.widget import Widget from albow.resource import get_image import pygame.mouse as mouse from pygame.sprite import Sprite, RenderUpdates +import pygame.cursors +import pygame.mouse + +# FIXME: make this a all transparent cursor +BLANK_CURSOR=pygame.cursors.broken_x class CursorSprite(Sprite): "A Sprite that follows the Cursor" @@ -18,6 +24,15 @@ def update(self): self.rect.midtop = mouse.get_pos() +class CursorWidget(Widget): + """Mix-in widget to ensure that mouse_move is propogated to parents""" + + def mouse_move(self, event): + self.call_parent_handler('mouse_move', event) + + def get_cursor(self, event): + return BLANK_CURSOR + class CursorSpriteScreen(Screen): "A Screen with a CursorSprite" @@ -29,8 +44,12 @@ self.cursor_group = RenderUpdates(sprite) def draw(self, surface): - self.cursor_group.update() - self.cursor_group.draw(surface) + if self.get_cursor(None) == pygame.mouse.get_cursor(): + self.cursor_group.update() + self.cursor_group.draw(surface) def mouse_move(self, event): self.invalidate() + + def get_cursor(self, event): + return BLANK_CURSOR
--- a/gamelib/gamescreen.py Mon Aug 23 20:50:36 2010 +0200 +++ b/gamelib/gamescreen.py Mon Aug 23 20:54:04 2010 +0200 @@ -5,18 +5,17 @@ from albow.controls import Button, Label, Widget from albow.layout import Column from albow.palette_view import PaletteView -from albow.resource import get_font from pygame import Rect from pygame.color import Color from pygame.locals import BLEND_ADD from constants import BUTTON_SIZE -from cursor import CursorSpriteScreen +from cursor import CursorSpriteScreen, CursorWidget from hand import HandButton -from popupmenu import PopupMenu +from popupmenu import PopupMenu, PopupMenuButton from state import initial_state, Item -class InventoryView(PaletteView): +class InventoryView(PaletteView, CursorWidget): sel_color = Color("yellow") sel_width = 2 @@ -45,7 +44,7 @@ self.selection = None -class StateWidget(Widget): +class StateWidget(CursorWidget): def __init__(self, state): Widget.__init__(self, Rect(0, 0, 800, 600 - BUTTON_SIZE)) @@ -70,6 +69,7 @@ if self.state.check_for_new_description(event.pos): # queue a redraw self.invalidate() + CursorWidget.mouse_move(self, event) class GameScreen(CursorSpriteScreen): @@ -82,11 +82,9 @@ self.add(self.state_widget) self.popup_menu = PopupMenu(shell) - self.menubutton = Button('Menu', action=self.popup_menu.show_menu) - self.menubutton.font = get_font(16, 'Vera.ttf') - self.menubutton.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE)) + self.menubutton = PopupMenuButton('Menu', + action=self.popup_menu.show_menu) self.menubutton.bottomleft = self.bottomleft - self.menubutton.margin = (BUTTON_SIZE - self.menubutton.font.get_linesize()) / 2 self.add(self.menubutton) self.handbutton = HandButton(action=self.hand_pressed) self.handbutton.bottomleft = self.bottomleft
--- a/gamelib/hand.py Mon Aug 23 20:50:36 2010 +0200 +++ b/gamelib/hand.py Mon Aug 23 20:54:04 2010 +0200 @@ -1,6 +1,7 @@ # Button for the hand image from constants import BUTTON_SIZE +from cursor import CursorWidget from albow.controls import ImageButton from albow.resource import get_image @@ -8,7 +9,7 @@ from pygame.color import Color from pygame.rect import Rect -class HandButton(ImageButton): +class HandButton(ImageButton, CursorWidget): """The fancy hand button for the widget""" sel_colour = Color('red')
--- a/gamelib/popupmenu.py Mon Aug 23 20:50:36 2010 +0200 +++ b/gamelib/popupmenu.py Mon Aug 23 20:54:04 2010 +0200 @@ -3,9 +3,25 @@ # Popup menu for the game screen from constants import BUTTON_SIZE +from cursor import CursorWidget from albow.menu import Menu +from albow.controls import Button from albow.resource import get_font +from pygame.rect import Rect + +class PopupMenuButton(Button, CursorWidget): + + def __init__(self, text, action): + Button.__init__(self, text, action) + + self.font = get_font(16, 'Vera.ttf') + self.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE)) + self.margin = (BUTTON_SIZE - self.font.get_linesize()) / 2 + + def get_cursor(self, event): + # Draw standard arrow, not the sprite cursor + return None class PopupMenu(Menu):