00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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
00074 ItemRelativeGump::InitGump(newparent, take_focus);
00075
00076
00077 TextWidget *widget = new TextWidget(0,0,barked,true,fontnum,194,55);
00078 widget->InitGump(this);
00079
00080 textwidget = widget->getObjId();
00081
00082
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
00091
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
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
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
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
00144 if (!Kernel::get_instance()->isPaused()) {
00145 if (!--counter) {
00146
00147 bool done = !NextText();
00148 if (done) {
00149 bool speechplaying = false;
00150 if (speechlength) {
00151
00152 AudioProcess *ap = AudioProcess::get_instance();
00153 if (ap)
00154 speechplaying = ap->isSpeechPlaying(barked,
00155 speechshapenum);
00156 }
00157
00158
00159 if (!speechplaying)
00160 Close();
00161 else
00162 counter = textdelay;
00163 }
00164 }
00165 }
00166 return true;
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
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
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