source: trunk/game/scripts/gui/dialoguegui.py @ 684

Revision 684, 6.9 KB checked in by technomage, 8 years ago (diff)

Patch by Technomage

  • Refactored the DialogueEngine? singleton into the DialogueProcessor? instance factory, eliminating some of the problems the singleton pattern caused with encapsulation and unit testing.
  • Renamed the dialogueengine.py module to dialogueprocessor.py for clarity.
  • Added type-checking code to the instance variables defined by the DialogueProcessor? and Dialogue classes using assert statements that are removed when the optimization '-O' flag is passed to the Python interpreter.
  • Moved the 'main' logic in dialogue_demo.py script into a 'main' method to avoid cluttering the module namespace with runtime variables.
  • Fixed a few code documentation issues pointed out by aspidites.
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17import logging
18
19from fife import fife
20from fife.extensions import pychan
21from fife.extensions.pychan import widgets
22
23from scripts.dialogueprocessor import DialogueProcessor
24
25def setupLogging():
26    """Set various logging parameters for this module."""
27    module_logger = logging.getLogger('dialoguegui')
28    if (__debug__):
29        module_logger.setLevel(logging.DEBUG)
30setupLogging()
31
32class DialogueGUI(object):
33    """Window that handles the dialogues."""
34    _logger = logging.getLogger('dialoguegui.DialogueGUI')
35   
36    def __init__(self, controller, npc, quest_engine, player_character):
37        self.active = False
38        self.controller = controller
39        self.dialogue_gui = pychan.loadXML("gui/dialogue.xml")
40        self.npc = npc
41        # TODO Technomage 2010-11-10: the QuestEngine should probably be
42        #     a singleton-like object, which would avoid all of this instance
43        #     handling.
44        self.quest_engine = quest_engine
45        self.player_character = player_character
46   
47    def initiateDialogue(self):
48        """Callback for starting a quest"""
49        self.active = True
50        stats_label = self.dialogue_gui.findChild(name='stats_label')
51        stats_label.text = u'Name: John Doe\nAn unnamed one'
52        events = {
53            'end_button': self.handleEnd
54        }
55        self.dialogue_gui.mapEvents(events)
56        self.dialogue_gui.show()
57        self.setNpcName(self.npc.name)
58        self.setAvatarImage(self.npc.dialogue.avatar_path)
59       
60        game_state = {'npc': self.npc, 'pc': self.player_character,
61                      'quest': self.quest_engine}
62        try:
63            self.dialogue_processor = DialogueProcessor(self.npc.dialogue,
64                                                        game_state)
65            self.dialogue_processor.initiateDialogue()
66        except (TypeError) as error:
67            self._logger.error(str(error))
68        else:
69            self.continueDialogue()
70   
71    def setDialogueText(self, text):
72        """Set the displayed dialogue text.
73           @param text: text to display."""
74        text = unicode(text)
75        speech = self.dialogue_gui.findChild(name='speech')
76        # to append text to npc speech box, uncomment the following line
77        #speech.text = speech.text + "\n-----\n" + unicode(say)
78        speech.text = text
79        self._logger.debug('set dialogue text to "{0}"'.format(text))
80   
81    def continueDialogue(self):
82        """Display the dialogue text and responses for the current
83           L{DialogueSection}."""
84        dialogue_processor = self.dialogue_processor
85        dialogue_text = dialogue_processor.getCurrentDialogueSection().text
86        self.setDialogueText(dialogue_text)
87        self.responses = dialogue_processor.continueDialogue()
88        self.setResponses(self.responses)
89   
90    def handleEntered(self, *args):
91        """Callback for when user hovers over response label."""
92        pass
93   
94    def handleExited(self, *args):
95        """Callback for when user hovers out of response label."""
96        pass
97   
98    def handleClicked(self, *args):
99        """Handle a response being clicked."""
100        response_n = int(args[0].name.replace('response', ''))
101        response = self.responses[response_n]
102        dialogue_processor = self.dialogue_processor
103        dialogue_processor.reply(response)
104        if (not dialogue_processor.in_dialogue):
105            self.handleEnd()
106        else:
107            self.continueDialogue()
108   
109    def handleEnd(self):
110        """Handle the end of the conversation being reached, either from the
111           GUI or from within the conversation itself."""
112        self.dialogue_gui.hide()
113        self.responses = []
114        self.npc.behaviour.state = 1
115        self.npc.behaviour.idle()
116        self.active = False
117   
118    def setNpcName(self, name):
119        """Set the NPC name to display on the dialogue GUI.
120           @param name: name of the NPC to set
121           @type name: basestring"""
122        name = unicode(name)
123        stats_label = self.dialogue_gui.findChild(name='stats_label')
124        try:
125            (first_name, desc) = name.split(" ", 1)
126            stats_label.text = u'Name: ' + first_name + "\n" + desc
127        except ValueError:
128            stats_label.text = u'Name: ' + name
129       
130        self.dialogue_gui.title = name
131        self._logger.debug('set NPC name to "{0}"'.format(name))
132   
133    def setAvatarImage(self, image_path):
134        """Set the NPC avatar image to display on the dialogue GUI
135           @param image_path: filepath to the avatar image
136           @type image_path: basestring"""
137        avatar_image = self.dialogue_gui.findChild(name='npc_avatar')
138        avatar_image.image = image_path
139   
140    def setResponses(self, dialogue_responses):
141        """Creates the list of clickable response labels and sets their
142           respective on-click callbacks.
143           @param responses: list of L{DialogueResponses} from the
144               L{DialogueProcessor}
145           @type responses: list of L{DialogueResponses}"""
146        choices_list = self.dialogue_gui.findChild(name='choices_list')
147        choices_list.removeAllChildren()
148        for index, response in enumerate(dialogue_responses):
149            button = widgets.Label(
150                name="response{0}".format(index),
151                text=unicode(response.text),
152                hexpand="1",
153                min_size=(100,16),
154                max_size=(490,48),
155                position_technique='center:center'
156            )
157            button.margins = (5, 5)
158            button.background_color = fife.Color(0, 0, 0)
159            button.color = fife.Color(0, 255, 0)
160            button.border_size = 0
161            button.wrap_text = 1
162            button.capture(lambda button=button: self.handleEntered(button),
163                           event_name='mouseEntered')
164            button.capture(lambda button=button: self.handleExited(button),
165                           event_name='mouseExited')
166            button.capture(lambda button=button: self.handleClicked(button),
167                           event_name='mouseClicked')
168            choices_list.addChild(button)
169            self.dialogue_gui.adaptLayout(True)
170            self._logger.debug(
171                'added {0} to response choice list'.format(response)
172            )
Note: See TracBrowser for help on using the repository browser.