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

Revision 424, 7.3 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #107: patch by b0rland

  • Reworked inventory gui to use new inventory model
  • Made various additions and fixes to inventory model and base classes
  • Improved drag-and-drop support
  • Possibly broke dialog gui :(
  • Property svn:eol-style set to native
Line 
1#!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/>.
17
18import pychan
19import fife
20import pychan.widgets as widgets
21from scripts.dialogue import DialogueEngine
22
23class DialogueGUI(object):
24    def __init__(self, npc, quest_engine, pc):
25        self.pc = pc
26
27        # define dialogue engine callbacks
28        def start_quest(state, quest):
29            print "You've picked up the '%s' quest!" % quest
30            state['quest'].addQuest(quest)
31
32        def complete_quest(state, quest_id):
33            print "You've finished the quest %s" % quest_id
34            state['quest'].finishQuest(quest_id)
35
36        def delete_quest(state, quest_id):
37            print "You've deleted quest %s" % quest_id
38            state['quest'].deleteQuest(quest_id)
39
40        def increase_value(state, quest_id, variable, value):
41            print "Increased %s by %i"%(variable,value)
42            state['quest'][quest_id].increaseValue(variable,value)
43
44        def decrease_value(state, quest_id, variable, value):
45            print "Decreased %s by %i"%(variable,value)
46            state['quest'][quest_id].decreaseValue(variable,value)
47
48        def set_value(state,quest_id, variable, value):
49            print "Set %s to %s"%(variable,value)
50            state['quest'][quest_id].setValue(variable,value)
51
52        def meet(state, npc):
53            print "You've met %s!" % npc
54            state['pc'].meet(npc)
55
56        def get_stuff(state, thing):
57            if not state['pc'].inventory.has(thing):
58                state['pc'].inventory.placeItem(thing)
59                print "You've now have the %s" % thing
60
61        def take_stuff(state,thing):
62            if state['pc'].inventory.has(thing):
63                state['pc'].inventory.takeItem(thing)
64                print "You no longer have the %s" % thing
65
66        dialogue_callbacks = {
67            'complete_quest': complete_quest,
68            'decrease_value': decrease_value,
69            'delete_quest'  : delete_quest,
70            'end'           : self.handleEnd,
71            'get_stuff'     : get_stuff,
72            'increase_value': increase_value,
73            'meet'          : meet,
74            'npc_name'      : self.handleNpcName,
75            'npc_avatar'    : self.handleAvatarImage,
76            'responses'     : self.handleResponses,
77            'say'           : self.handleSay,
78            'set_value'     : set_value,
79            'start_quest'   : start_quest,
80            'take_stuff'    : take_stuff
81        }
82
83        self.npc = npc
84        state = {
85            'pc': self.pc,
86            'quest': quest_engine
87        }
88        self.dialogue_engine = DialogueEngine(npc.dialogue,
89                                              dialogue_callbacks, state)
90        self.dialogue_gui = pychan.loadXML("gui/dialogue.xml")
91
92    def initiateDialogue(self):
93        """Callback for starting a quest"""
94        stats_label = self.dialogue_gui.findChild(name='stats_label')
95        stats_label.text = u'Name: John Doe\nAn unnamed one'
96
97        events = {
98            'end_button': self.handleEnd
99        }
100        self.dialogue_gui.mapEvents(events)
101        self.dialogue_gui.show()
102        responses_list = self.dialogue_gui.findChild(name='choices_list')
103        responses = self.dialogue_engine.run()
104        self.setResponses(responses)
105
106    def handleSay(self, state, say):
107        """Callback for NPC speech"""
108        speech = self.dialogue_gui.findChild(name='speech')
109        # to append text to npc speech box, uncomment the following line
110        #speech.text = speech.text + "\n-----\n" + unicode(say)
111        speech.text = unicode(say)
112
113    def handleEntered(self, *args):
114        """Callback for when user hovers over response label"""
115        pass
116
117    def handleExited(self, *args):
118        """Callback for when user hovers out of response label"""
119        pass
120
121    def handleClicked(self, *args):
122        """Handle a response being clicked"""
123        response = int(args[0].name.replace('response', ''))
124        if not self.dialogue_engine.reply(response):
125            self.handleEnd()
126
127    def handleEnd(self):
128        """Handle the end of the conversation being reached. Either from the
129           GUI or from within the conversation itself."""
130        self.dialogue_engine = None
131        self.dialogue_gui.hide()
132        self.npc.behaviour.state = 1
133        self.npc.behaviour.idle()
134
135    def handleNpcName(self, name):
136        """Callback to handle setting the NPC name.
137           @type name: str
138           @param name: The name of the NPC to set. """
139       
140        stats_label = self.dialogue_gui.findChild(name='stats_label')
141        try:
142            (first_name, desc) = name.split(" ",1)
143            stats_label.text = u'Name: ' + first_name + "\n" + desc
144        except:
145            stats_label.text = u'Name: ' + name
146           
147        self.dialogue_gui.title = name
148       
149    def handleAvatarImage(self, image):
150        """Callback to handle when the dialogue engine wants to set the NPC
151        image
152           @type image: str
153           @param image: filename of avatar image"""
154        avatar_image = self.dialogue_gui.findChild(name='npc_avatar')
155        avatar_image.image = image
156
157    def handleResponses(self, *args):
158        """Callback to handle when the dialogue engine wants to display a new
159           list of options"""
160        self.setResponses(args[1])
161
162    def setResponses(self, responses):
163        """Creates the list of clickable response labels and sets their
164           respective on-click callbacks
165           @type responses: [ [ "response text", section, condition ], ...]
166           @param responses: the list of response objects from the dialogue
167                             engine"""
168        choices_list = self.dialogue_gui.findChild(name='choices_list')
169        choices_list.removeAllChildren()
170        for i,r in enumerate(responses):
171            button = widgets.Label(
172                name="response%s"%(i,),
173                text=unicode(r[0]),
174                hexpand="1",
175                min_size=(100,16),
176                max_size=(490,48),
177                position_technique='center:center'
178            )
179            button.margins = (5,5)
180            button.background_color = fife.Color(0,0,0)
181            button.color = fife.Color(0,255,0)
182            button.border_size = 0
183            button.wrap_text = 1
184            button.capture(lambda button=button: self.handleEntered(button), \
185                           event_name='mouseEntered')
186            button.capture(lambda button=button: self.handleExited(button), \
187                           event_name='mouseExited')
188            button.capture(lambda button=button: self.handleClicked(button), \
189                           event_name='mouseClicked')
190            choices_list.addChild(button)
191            self.dialogue_gui.adaptLayout(True)
192
193
Note: See TracBrowser for help on using the repository browser.