BarkGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2006  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "pent_include.h"
00020 #include "BarkGump.h"
00021 #include "TextWidget.h"
00022 #include "Kernel.h"
00023 #include "AudioProcess.h"
00024 #include "getObject.h"
00025 #include "SettingManager.h"
00026 
00027 #include "IDataSource.h"
00028 #include "ODataSource.h"
00029 
00030 DEFINE_RUNTIME_CLASSTYPE_CODE(BarkGump,ItemRelativeGump);
00031 
00032 // TODO: Remove all the hacks
00033 
00034 BarkGump::BarkGump()
00035         : ItemRelativeGump()
00036 {
00037 
00038 }
00039 
00040 BarkGump::BarkGump(uint16 owner, std::string msg, uint32 speechshapenum_) :
00041         ItemRelativeGump(0, 0, 100, 100, owner,
00042                                          FLAG_KEEP_VISIBLE, LAYER_ABOVE_NORMAL),
00043         barked(msg), counter(100), speechshapenum(speechshapenum_),
00044         speechlength(0), totaltextheight(0)
00045 {
00046         SettingManager::get_instance()->get("textdelay", textdelay);
00047 }
00048 
00049 BarkGump::~BarkGump(void)
00050 {
00051 }
00052 
00053 void BarkGump::InitGump(Gump* newparent, bool take_focus)
00054 {
00055         // OK, this is a bit of a hack, but it's how it has to be
00056         int     fontnum;
00057         if (owner == 1) fontnum = 6;
00058         else if (owner > 256) fontnum = 8;
00059         else switch (owner%3) {
00060                 case 1:
00061                         fontnum = 5;
00062                         break;
00063                         
00064                 case 2:
00065                         fontnum = 7;
00066                         break;
00067 
00068                 default:
00069                         fontnum = 0;
00070                         break;
00071         }
00072 
00073         // This is a hack. We init the gump twice...
00074         ItemRelativeGump::InitGump(newparent, take_focus);
00075 
00076         // Create the TextWidget
00077         TextWidget *widget = new TextWidget(0,0,barked,true,fontnum,194,55);
00078         widget->InitGump(this);
00079 
00080         textwidget = widget->getObjId();
00081 
00082         // see if we need to play speech
00083         AudioProcess *ap = AudioProcess::get_instance();
00084         speechlength = 0;
00085         if (speechshapenum && ap) {
00086                 if (ap->playSpeech(barked, speechshapenum, owner)) {
00087                         speechlength = ap->getSpeechLength(barked, speechshapenum) / 33;
00088                         if (speechlength == 0) speechlength = 1;
00089 
00090                         // We're playing speech, so need to sync the text with the speech.
00091                         // First we count the total height of all text blocks.
00092                         Pentagram::Rect d;
00093                         widget->GetDims(d);
00094                         totaltextheight = d.h;
00095                         while (widget->setupNextText()) {
00096                                 widget->GetDims(d);
00097                                 totaltextheight += d.h;
00098                         }
00099                         widget->rewind();
00100                 }
00101         }
00102 
00103         // This is just a hack
00104         Pentagram::Rect d;
00105         widget->GetDims(d);
00106         if (speechlength && totaltextheight) {
00107                 counter = (d.h * speechlength) / totaltextheight;
00108         } else {
00109                 counter = d.h * textdelay;
00110         }
00111         dims.h = d.h;
00112         dims.w = d.w;
00113 
00114         // Wait with ItemRelativeGump initialization until we calculated our size.
00115         ItemRelativeGump::InitGump(newparent, take_focus);
00116 }
00117 
00118 bool BarkGump::NextText()
00119 {
00120         TextWidget *widget = p_dynamic_cast<TextWidget*>(getGump(textwidget));
00121         assert(widget);
00122         if (widget->setupNextText()) {
00123                 // This is just a hack
00124                 Pentagram::Rect d;
00125                 widget->GetDims(d);
00126                 if (speechlength && totaltextheight) {
00127                         counter = (d.h * speechlength) / totaltextheight;
00128                 } else {
00129                         counter = d.h * textdelay;
00130                 }
00131                 dims.h = d.h;
00132                 dims.w = d.w;
00133                 return true;
00134         }
00135 
00136         return false;
00137 }
00138 
00139 bool BarkGump::Run(const uint32 framenum)
00140 {
00141         ItemRelativeGump::Run(framenum);
00142 
00143         // Auto close
00144         if (!Kernel::get_instance()->isPaused()) {
00145                 if (!--counter) {
00146                         // try next text
00147                         bool done = !NextText();
00148                         if (done) {
00149                                 bool speechplaying = false;
00150                                 if (speechlength) {
00151                                         // waiting for speech to finish?
00152                                         AudioProcess *ap = AudioProcess::get_instance();
00153                                         if (ap)
00154                                                 speechplaying = ap->isSpeechPlaying(barked,
00155                                                                                                                         speechshapenum);
00156                                 }
00157 
00158                                 // if speech done too, close
00159                                 if (!speechplaying)
00160                                         Close();
00161                                 else
00162                                         counter = textdelay;
00163                         }
00164                 }
00165         }
00166         return true;    // Always repaint, even though we could try to detect it
00167 }
00168 
00169 Gump *BarkGump::OnMouseDown(int button, int mx, int my)
00170 {
00171         Gump *g = ItemRelativeGump::OnMouseDown(button,mx,my);
00172         if (g) return g;
00173 
00174         // Scroll to next text, if possible
00175         if (!NextText()) {
00176                 if (speechlength) {
00177                         AudioProcess *ap = AudioProcess::get_instance();
00178                         if (ap) ap->stopSpeech(barked, speechshapenum, owner);
00179                 }
00180                 Close();
00181         }
00182         return this;
00183 }
00184 
00185 void BarkGump::saveData(ODataSource* ods)
00186 {
00187         ItemRelativeGump::saveData(ods);
00188 
00189         ods->write4(static_cast<uint32>(counter));
00190         ods->write2(textwidget);
00191         ods->write4(speechshapenum);
00192         ods->write4(speechlength);
00193         ods->write4(totaltextheight);
00194         ods->write4(barked.size());
00195         ods->write(barked.c_str(), barked.size());
00196 }
00197 
00198 bool BarkGump::loadData(IDataSource* ids, uint32 version)
00199 {
00200         if (!ItemRelativeGump::loadData(ids, version)) return false;
00201 
00202         counter = static_cast<sint32>(ids->read4());
00203         textwidget = ids->read2();
00204         speechshapenum = ids->read4();
00205         speechlength = ids->read4();
00206         totaltextheight = ids->read4();
00207 
00208         uint32 slen = ids->read4();
00209         if (slen > 0) {
00210                 char* buf = new char[slen+1];
00211                 ids->read(buf, slen);
00212                 buf[slen] = 0;
00213                 barked = buf;
00214                 delete[] buf;
00215         } else {
00216                 barked = "";
00217         }
00218 
00219 
00220         TextWidget *widget = p_dynamic_cast<TextWidget*>(getGump(textwidget));
00221 
00222         SettingManager::get_instance()->get("textdelay", textdelay);
00223 
00224         // This is just a hack
00225         Pentagram::Rect d;
00226         widget->GetDims(d);
00227         counter = d.h * textdelay;
00228         dims.h = d.h;
00229         dims.w = d.w;
00230 
00231         return true;
00232 }
00233 
00234 // Colourless Protection

Generated on Fri Jul 27 22:27:09 2007 for pentagram by  doxygen 1.4.7