annotate skaapsteker/widgets/bubble.py @ 291:04be4219742b

Add support for auto_next_text to give the auto_next effect but with a different prompt.
author Simon Cross <hodgestar@gmail.com>
date Fri, 08 Apr 2011 21:58:35 +0200
parents 71f15f6e9274
children 342e30c92a85
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
252
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Widget for in-level dialogue / speech bubbles."""
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
259
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
3 from pygame.locals import (KEYDOWN, K_UP, K_p, K_q, K_x, K_z, K_RETURN, BLEND_ADD)
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
4 import pygame.draw
255
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
5 import pygame
252
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
7 from ..engine import OpenDialog, CloseDialog
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
8 from .text import Text, TextChoice
252
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
9
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
11 class DialogueWidget(object):
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
12
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
13 def __init__(self, npc):
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14 self.npc = npc
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
15 self.dsm = npc.dsm
255
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
16 self._text = None
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
17 self._text_choice = None
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
18 self._state_update()
255
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
19
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
20 def _state_update(self):
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
21 pos = pygame.Rect((0, 0), (300, 1))
255
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
22 state = self.dsm.get_state()
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
23 if state.text:
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
24 pos = pos.move(0, 0) # copy
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
25 self._text = Text(state.text, pos, wrap=True)
255
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
26 else:
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
27 self._text = None
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
28 options = [(text, i) for (i, text) in state.choices]
285
71f15f6e9274 Hook up auto_next dialogue events.
Simon Cross <hodgestar@gmail.com>
parents: 259
diff changeset
29 if state.auto_next:
291
04be4219742b Add support for auto_next_text to give the auto_next effect but with a different prompt.
Simon Cross <hodgestar@gmail.com>
parents: 285
diff changeset
30 text = state.auto_next_text if state.auto_next_text else "Next"
04be4219742b Add support for auto_next_text to give the auto_next effect but with a different prompt.
Simon Cross <hodgestar@gmail.com>
parents: 285
diff changeset
31 options.append((text, "N"))
285
71f15f6e9274 Hook up auto_next dialogue events.
Simon Cross <hodgestar@gmail.com>
parents: 259
diff changeset
32 else:
71f15f6e9274 Hook up auto_next dialogue events.
Simon Cross <hodgestar@gmail.com>
parents: 259
diff changeset
33 options.append(("Leave", "L"))
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
34 pos = pos.move(0, 0) # copy
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
35 self._text_choice = TextChoice(options, pos, wrap=True)
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
36 self._text_choice.callbacks.append(self._selected)
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
37
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
38 def _selected(self, i, data):
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
39 if data == "L":
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
40 CloseDialog.post(self.npc)
285
71f15f6e9274 Hook up auto_next dialogue events.
Simon Cross <hodgestar@gmail.com>
parents: 259
diff changeset
41 elif data == "N":
71f15f6e9274 Hook up auto_next dialogue events.
Simon Cross <hodgestar@gmail.com>
parents: 259
diff changeset
42 self.dsm.auto_next()
71f15f6e9274 Hook up auto_next dialogue events.
Simon Cross <hodgestar@gmail.com>
parents: 259
diff changeset
43 self._state_update()
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
44 else:
258
d7e6830fe051 Link up choices to dsm events.
Simon Cross <hodgestar@gmail.com>
parents: 257
diff changeset
45 self.dsm.choice(i)
d7e6830fe051 Link up choices to dsm events.
Simon Cross <hodgestar@gmail.com>
parents: 257
diff changeset
46 self._state_update()
252
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
48 def draw(self, level_surface):
255
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
49 if self._text:
8bd442fa89ad Render dialog on screen instead of printing to console.
Simon Cross <hodgestar@gmail.com>
parents: 252
diff changeset
50 self._text.rect.center = level_surface.get_clip().center
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
51 self._text_choice.rect.topleft = self._text.rect.bottomleft
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
52 self._text_choice.rect.move_ip((0, 10))
259
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
53
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
54 bgrect = self._text.rect.union(self._text_choice.rect)
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
55 bgrect.inflate_ip(10, 10)
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
56 level_surface.fill((255, 255, 255), bgrect)
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
57 pygame.draw.rect(level_surface, (0, 0, 0), bgrect, 1)
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
58
030387133ace Add background to make dialogue more readable.
Simon Cross <hodgestar@gmail.com>
parents: 258
diff changeset
59 self._text.draw(level_surface)
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
60 self._text_choice.draw(level_surface)
252
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
61
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
62 def close(self):
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
63 pass
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
64
ecd26fafbe70 Add missing file from previous commit.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
65 def dispatch(self, ev):
257
3f8ce3452cd6 Add support for selecting dialogue choices (and for moving TextChoice around the screen).
Simon Cross <hodgestar@gmail.com>
parents: 256
diff changeset
66 self._text_choice.dispatch(ev)