| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "DialogStack.h" |
| 10 |
|
| 11 |
#include "Dialog.h" |
| 12 |
#include "ResDialogPack.h" |
| 13 |
#include "PlannedDialog.h" |
| 14 |
#include "StringTool.h" |
| 15 |
|
| 16 |
//----------------------------------------------------------------- |
| 17 |
DialogStack::DialogStack() |
| 18 |
{ |
| 19 |
m_dialogs = new ResDialogPack(); |
| 20 |
m_activeDialog = NULL; |
| 21 |
} |
| 22 |
//----------------------------------------------------------------- |
| 23 |
/** |
| 24 |
* Releases resources and stops all cycling dialogs. |
| 25 |
*/ |
| 26 |
DialogStack::~DialogStack() |
| 27 |
{ |
| 28 |
removeAll(); |
| 29 |
delete m_dialogs; |
| 30 |
} |
| 31 |
//----------------------------------------------------------------- |
| 32 |
/** |
| 33 |
* Removes finished dialogs from stack. |
| 34 |
*/ |
| 35 |
void |
| 36 |
DialogStack::updateStack() |
| 37 |
{ |
| 38 |
removeFirstNotTalking(); |
| 39 |
} |
| 40 |
//----------------------------------------------------------------- |
| 41 |
/** |
| 42 |
* Store new dialog. |
| 43 |
*/ |
| 44 |
void |
| 45 |
DialogStack::addDialog(const std::string &name, Dialog *dialog) |
| 46 |
{ |
| 47 |
m_dialogs->addRes(name, dialog); |
| 48 |
} |
| 49 |
//----------------------------------------------------------------- |
| 50 |
/** |
| 51 |
* Run talk. |
| 52 |
* Dialog name could contain "name@arg1@arg2..." |
| 53 |
* to fill %1, %2, ... in subtitles. |
| 54 |
* |
| 55 |
* @param actor actor index |
| 56 |
* @param name dialog name |
| 57 |
* @param volume dialog volume |
| 58 |
* @param loops number of loops, 0=play once, 1=play twice, -1=play infinite |
| 59 |
* @param dialogFlag whether it is blocking dialog |
| 60 |
*/ |
| 61 |
void |
| 62 |
DialogStack::actorTalk(int actor, const std::string &name, |
| 63 |
int volume, int loops, bool dialogFlag) |
| 64 |
{ |
| 65 |
StringTool::t_args args = StringTool::split(name, '@'); |
| 66 |
|
| 67 |
const Dialog *subtitle = m_dialogs->findDialogHard(args[0]); |
| 68 |
if (subtitle) { |
| 69 |
subtitle->runSubtitle(args); |
| 70 |
|
| 71 |
const Dialog *dialog = m_dialogs->findDialogSpeech(args[0]); |
| 72 |
if (dialog) { |
| 73 |
PlannedDialog *talker = new PlannedDialog(actor, dialog, |
| 74 |
subtitle->getMinTime()); |
| 75 |
talker->talk(volume, loops); |
| 76 |
|
| 77 |
if (loops == -1) { |
| 78 |
m_cycling.push_back(talker); |
| 79 |
} |
| 80 |
else { |
| 81 |
m_running.push_back(talker); |
| 82 |
} |
| 83 |
|
| 84 |
if (dialogFlag) { |
| 85 |
m_activeDialog = talker; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
//----------------------------------------------------------------- |
| 91 |
bool |
| 92 |
DialogStack::isTalking(int actor) const |
| 93 |
{ |
| 94 |
return isTalkingIn(actor, m_running) || |
| 95 |
isTalkingIn(actor, m_cycling); |
| 96 |
} |
| 97 |
//----------------------------------------------------------------- |
| 98 |
bool |
| 99 |
DialogStack::isTalkingIn(int actor, const t_running &fifo) const |
| 100 |
{ |
| 101 |
t_running::const_iterator end = fifo.end(); |
| 102 |
for (t_running::const_iterator i = fifo.begin(); i != end; ++i) { |
| 103 |
if ((*i)->equalsActor(actor) && (*i)->isTalking()) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
} |
| 107 |
return false; |
| 108 |
} |
| 109 |
//----------------------------------------------------------------- |
| 110 |
/** |
| 111 |
* Remove first not talking dialog from m_running. |
| 112 |
*/ |
| 113 |
void |
| 114 |
DialogStack::removeFirstNotTalking() |
| 115 |
{ |
| 116 |
t_running::iterator end = m_running.end(); |
| 117 |
for (t_running::iterator i = m_running.begin(); i != end; ++i) { |
| 118 |
if (!(*i)->isTalking()) { |
| 119 |
releaseDialog(*i); |
| 120 |
m_running.erase(i); |
| 121 |
return; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
//----------------------------------------------------------------- |
| 126 |
/** |
| 127 |
* Delete all running dialogs made by this actor. |
| 128 |
*/ |
| 129 |
void |
| 130 |
DialogStack::killSound(int actor) |
| 131 |
{ |
| 132 |
killSoundIn(actor, m_running); |
| 133 |
killSoundIn(actor, m_cycling); |
| 134 |
} |
| 135 |
//----------------------------------------------------------------- |
| 136 |
void |
| 137 |
DialogStack::killSoundIn(int actor, t_running &fifo) |
| 138 |
{ |
| 139 |
//NOTE: erase on list invalidates only the erased iterator |
| 140 |
t_running::iterator run_end = fifo.end(); |
| 141 |
for (t_running::iterator i = fifo.begin(); i != run_end; /* empty */) { |
| 142 |
t_running::iterator toKill = i; |
| 143 |
++i; |
| 144 |
|
| 145 |
if ((*toKill)->equalsActor(actor)) { |
| 146 |
releaseDialog(*toKill); |
| 147 |
fifo.erase(toKill); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
//----------------------------------------------------------------- |
| 153 |
/** |
| 154 |
* Kill all running dialogs. |
| 155 |
*/ |
| 156 |
void |
| 157 |
DialogStack::killTalks() |
| 158 |
{ |
| 159 |
killTalksIn(m_running); |
| 160 |
killTalksIn(m_cycling); |
| 161 |
} |
| 162 |
//----------------------------------------------------------------- |
| 163 |
void |
| 164 |
DialogStack::killTalksIn(t_running &fifo) |
| 165 |
{ |
| 166 |
t_running::iterator end = fifo.end(); |
| 167 |
for (t_running::iterator i = fifo.begin(); i != end; ++i) { |
| 168 |
releaseDialog(*i); |
| 169 |
} |
| 170 |
fifo.clear(); |
| 171 |
} |
| 172 |
//----------------------------------------------------------------- |
| 173 |
void |
| 174 |
DialogStack::releaseDialog(PlannedDialog *dialog) |
| 175 |
{ |
| 176 |
if (dialog == m_activeDialog) { |
| 177 |
m_activeDialog = NULL; |
| 178 |
} |
| 179 |
dialog->killTalk(); |
| 180 |
delete dialog; |
| 181 |
} |
| 182 |
//----------------------------------------------------------------- |
| 183 |
/** |
| 184 |
* Delete all shared dialogs and kill talks. |
| 185 |
*/ |
| 186 |
void |
| 187 |
DialogStack::removeAll() |
| 188 |
{ |
| 189 |
killTalks(); |
| 190 |
m_dialogs->removeAll(); |
| 191 |
} |
| 192 |
//----------------------------------------------------------------- |
| 193 |
/** |
| 194 |
* Returns true when there is active dialog. |
| 195 |
*/ |
| 196 |
bool |
| 197 |
DialogStack::isDialog() const |
| 198 |
{ |
| 199 |
return m_activeDialog && m_activeDialog->isTalking(); |
| 200 |
} |
| 201 |
|