ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

lua_v1_text.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/foreach.h"
00024 #include "common/savefile.h"
00025 #include "common/system.h"
00026 
00027 #include "engines/grim/grim.h"
00028 #include "engines/grim/lua_v1.h"
00029 #include "engines/grim/localize.h"
00030 #include "engines/grim/actor.h"
00031 #include "engines/grim/savegame.h"
00032 #include "engines/grim/resource.h"
00033 #include "engines/grim/inputdialog.h"
00034 #include "engines/grim/textobject.h"
00035 
00036 #include "engines/grim/lua/lauxlib.h"
00037 
00038 namespace Grim {
00039 
00040 /* Destroy a text object since we don't need it anymore
00041  * note that the menu creates more objects than it needs,
00042  * so it deletes some objects right after creating them
00043  */
00044 void Lua_V1::KillTextObject() {
00045     lua_Object textObj = lua_getparam(1);
00046 
00047     if (lua_isuserdata(textObj) && lua_tag(textObj) == MKTAG('T', 'E', 'X', 'T')) {
00048         delete gettextobject(textObj);
00049     }
00050 }
00051 
00052 /* Make changes to a text object based on the parameters passed
00053  * in the table in the LUA parameter 2.
00054  */
00055 void Lua_V1::ChangeTextObject() {
00056     lua_Object textObj = lua_getparam(1);
00057     if (lua_isuserdata(textObj) && lua_tag(textObj) == MKTAG('T', 'E', 'X', 'T')) {
00058         TextObject *textObject = gettextobject(textObj);
00059         int paramId = 2;
00060         for (;;) {
00061             lua_Object paramObj = lua_getparam(paramId++);
00062             if (!paramObj)
00063                 break;
00064 
00065             // If the text object is invalid (deleted) ignore the request
00066             // We do the check here so that all of the params are used
00067             if (!textObject)
00068                 continue;
00069 
00070             if (!lua_isstring(paramObj)) {
00071                 if (!lua_istable(paramObj))
00072                     break;
00073                 setTextObjectParams(textObject, paramObj);
00074                 textObject->destroy();
00075             } else {
00076                 const char *line = lua_getstring(paramObj);
00077                 textObject->setText(line, false);
00078                 lua_getstring(paramObj);
00079 
00080             }
00081 
00082             lua_pushnumber(textObject->getBitmapWidth());
00083             lua_pushnumber(textObject->getBitmapHeight());
00084         }
00085     }
00086 }
00087 
00088 /* Return the "text speed", this option must be handled
00089  * to prevent errors in the "Options" menu even though
00090  * we're not currently using the value
00091  */
00092 void Lua_V1::GetTextSpeed() {
00093     lua_pushnumber(g_grim->getTextSpeed());
00094 }
00095 
00096 void Lua_V1::SetTextSpeed() {
00097     lua_Object speedObj = lua_getparam(1);
00098     if (!lua_isnumber(speedObj))
00099         return;
00100 
00101     int speed = (int)lua_getnumber(speedObj);
00102     g_grim->setTextSpeed(speed);
00103 }
00104 
00105 void Lua_V1::MakeTextObject() {
00106     lua_Object textObj = lua_getparam(1);
00107     if (!lua_isstring(textObj)) {
00108         return;
00109     }
00110 
00111     TextObject *textObject = new TextObject();
00112     const char *line = lua_getstring(textObj);
00113 
00114     textObject->setDefaults(&g_grim->_blastTextDefaults);
00115     lua_Object tableObj = lua_getparam(2);
00116     if (lua_istable(tableObj))
00117         setTextObjectParams(textObject, tableObj);
00118 
00119     textObject->setText(line, false);
00120 
00121     lua_pushusertag(textObject->getId(), MKTAG('T', 'E', 'X', 'T'));
00122     if (!(g_grim->getGameFlags() & ADGF_DEMO)) {
00123         lua_pushnumber(textObject->getBitmapWidth());
00124         lua_pushnumber(textObject->getBitmapHeight());
00125     }
00126 }
00127 
00128 void Lua_V1::GetTextObjectDimensions() {
00129     lua_Object textObj = lua_getparam(1);
00130 
00131     if (lua_isuserdata(textObj) && lua_tag(textObj) == MKTAG('T', 'E', 'X', 'T')) {
00132         TextObject *textObject = gettextobject(textObj);
00133         lua_pushnumber(textObject->getBitmapWidth());
00134         lua_pushnumber(textObject->getBitmapHeight());
00135     }
00136 }
00137 
00138 void Lua_V1::ExpireText() {
00139     // Cleanup actor references to deleted text objects
00140     foreach (Actor *a, Actor::getPool()) {
00141         a->lineCleanup();
00142     }
00143 }
00144 
00145 void Lua_V1::GetTextCharPosition() {
00146     lua_Object textObj = lua_getparam(1);
00147     if (lua_isuserdata(textObj) && lua_tag(textObj) == MKTAG('T', 'E', 'X', 'T')) {
00148         TextObject *textObject = gettextobject(textObj);
00149         int pos = (int)lua_getnumber(lua_getparam(2));
00150         lua_pushnumber(textObject->getTextCharPosition(pos));
00151     }
00152 }
00153 
00154 void Lua_V1::BlastText() {
00155     lua_Object textObj = lua_getparam(1);
00156     if (!lua_isstring(textObj)) {
00157         return;
00158     }
00159 
00160     const char *line = lua_getstring(textObj);
00161     if (!line || line[0] == 0)
00162         return;
00163 
00164     TextObject *textObject = new TextObject();
00165     textObject->setBlastDraw();
00166     textObject->setDefaults(&g_grim->_blastTextDefaults);
00167     lua_Object tableObj = lua_getparam(2);
00168     if (lua_istable(tableObj))
00169         setTextObjectParams(textObject, tableObj);
00170 
00171     textObject->setText(line, false);
00172     textObject->draw();
00173     delete textObject;
00174 }
00175 
00176 void Lua_V1::SetOffscreenTextPos() {
00177     // called with (0,0) on dialog entry, (nil, nil) on dialog exit
00178     warning("Lua_V1::SetOffscreenTextPos: implement opcode");
00179 }
00180 
00181 void Lua_V1::TextFileGetLine() {
00182     char textBuf[1000];
00183     lua_Object nameObj = lua_getparam(1);
00184     lua_Object posObj = lua_getparam(2);
00185     Common::SeekableReadStream *file;
00186 
00187     if (lua_isnil(nameObj) || lua_isnil(posObj)) {
00188         lua_pushnil();
00189         return;
00190     }
00191 
00192     const char *filename = lua_getstring(nameObj);
00193     Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
00194     file = saveFileMan->openForLoading(filename);
00195     if (!file) {
00196         lua_pushnil();
00197         return;
00198     }
00199 
00200     int pos = (int)lua_getnumber(posObj);
00201     file->seek(pos, SEEK_SET);
00202     memset(textBuf, 0, 1000);
00203     file->readLine(textBuf, 1000);
00204     delete file;
00205 
00206     lua_pushstring(textBuf);
00207 }
00208 
00209 void Lua_V1::TextFileGetLineCount() {
00210     char textBuf[1000];
00211     lua_Object nameObj = lua_getparam(1);
00212 
00213     if (lua_isnil(nameObj)) {
00214         lua_pushnil();
00215         return;
00216     }
00217 
00218     const char *filename = luaL_check_string(1);
00219     Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
00220     Common::SeekableReadStream *file = saveFileMan->openForLoading(filename);
00221     if (!file) {
00222         lua_pushnil();
00223         return;
00224     }
00225 
00226     lua_Object result = lua_createtable();
00227 
00228     int line = 0;
00229     for (;;) {
00230         if (file->eos())
00231             break;
00232         lua_pushobject(result);
00233         lua_pushnumber(line);
00234         int pos = file->pos();
00235         lua_pushnumber(pos);
00236         lua_settable();
00237         file->readLine(textBuf, 1000);
00238         line++;
00239     }
00240     delete file;
00241 
00242     lua_pushobject(result);
00243     lua_pushstring("count");
00244     lua_pushnumber(line);
00245     lua_settable();
00246     lua_pushobject(result);
00247 }
00248 
00249 // Localization function
00250 
00251 void Lua_V1::LocalizeString() {
00252     char msgId[50], buf[1000];
00253     lua_Object strObj = lua_getparam(1);
00254 
00255     if (lua_isstring(strObj)) {
00256         const char *str = lua_getstring(strObj);
00257         // If the string that we're passed isn't localized yet then
00258         // construct the localized string, otherwise spit back what
00259         // we've been given
00260         if (str[0] == '/' && str[strlen(str) - 1] == '/') {
00261             Common::String msg = parseMsgText(str, msgId);
00262             sprintf(buf, "/%s/%s", msgId, msg.c_str());
00263             str = buf;
00264         }
00265         lua_pushstring(str);
00266     }
00267 }
00268 
00269 void Lua_V1::SetSayLineDefaults() {
00270     lua_Object tableObj = lua_getparam(1);
00271     if (tableObj && lua_istable(tableObj))
00272         setTextObjectParams(&g_grim->_sayLineDefaults, tableObj);
00273 }
00274 
00275 void Lua_V1::SayLine() {
00276     int vol = 127, buffer = 64, paramId = 1;
00277     float x = -1, y = -1;
00278     bool background = true;
00279     const char *msgId = nullptr;
00280     Common::String msg;
00281     lua_Object paramObj = lua_getparam(paramId++);
00282 
00283     if ((lua_isuserdata(paramObj) && lua_tag(paramObj) == MKTAG('A','C','T','R'))
00284             || lua_isstring(paramObj) || lua_istable(paramObj)) {
00285         Actor *actor = nullptr;//some_Actor, maybe some current actor
00286         if (lua_isuserdata(paramObj) && lua_tag(paramObj) == MKTAG('A','C','T','R')) {
00287             actor = getactor(paramObj);
00288             paramObj = lua_getparam(paramId++);
00289         }
00290         if (actor) {
00291             while (!lua_isnil(paramObj)) {
00292                 if (!lua_isstring(paramObj) && !lua_isnumber(paramObj) && !lua_istable(paramObj))
00293                     break;
00294                 if (lua_istable(paramObj))
00295                     parseSayLineTable(paramObj, &background, &vol, &buffer, &x, &y);
00296                 else {
00297                     if (lua_isnumber(paramObj))
00298                         background = false;
00299                     else {
00300                         msgId = lua_getstring(paramObj);
00301                     }
00302                 }
00303                 paramObj = lua_getparam(paramId++);
00304             }
00305 
00306             actor->sayLine(msgId, background, x, y); //background, vol, pan, x, y
00307         }
00308     }
00309 }
00310 
00311 void Lua_V1::ShutUpActor() {
00312     lua_Object actorObj = lua_getparam(1);
00313 
00314     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00315         return;
00316     Actor *actor = getactor(actorObj);
00317     if (actor)
00318         actor->shutUp();
00319 }
00320 
00321 void Lua_V1::PrintLine() {
00322     int vol = 127, buffer = 64; /*paramId = 1, */
00323     float x = -1, y = -1;
00324     bool background = true;
00325     char msgId[50];
00326     Common::String msg;
00327     lua_Object param1Obj = lua_getparam(0);
00328     lua_Object param2Obj = lua_getparam(1);
00329 
00330     if ((lua_isstring(param1Obj) || lua_isnil(param1Obj)) && (lua_istable(param2Obj) || lua_isnil(param2Obj))) {
00331         if (lua_istable(param2Obj)) {
00332             setTextObjectParams(&g_grim->_printLineDefaults, param2Obj);
00333             parseSayLineTable(param2Obj, &background, &vol, &buffer, &x, &y);
00334         }
00335         if (lua_isstring(param1Obj)) {
00336             const char *tmpstr = lua_getstring(param1Obj);
00337             msg = parseMsgText(tmpstr, msgId);
00338         }
00339         if (!msg.empty()) {
00340 //          actor->sayLine(msg.c_str(), msgId); //background, vol, pan, x, y
00341         }
00342     }
00343 }
00344 
00345 void Lua_V1::InputDialog() {
00346     lua_Object titleObj = lua_getparam(1);
00347     lua_Object messageObj = lua_getparam(2);
00348     lua_Object defaultObj = lua_getparam(3);
00349 
00350     if (!lua_isstring(titleObj) || !lua_isstring(messageObj)) {
00351         lua_pushnil();
00352         return;
00353     }
00354 
00355     Common::String str = lua_getstring(titleObj);
00356     str += ": ";
00357     str += lua_getstring(messageObj);
00358     Grim::InputDialog d(str, lua_getstring(defaultObj));
00359     int res = d.runModal();
00360     // The KeyUp event for CTRL has been eat by the gui loop, so we
00361     // need to reset it manually.
00362     g_grim->clearEventQueue();
00363     if (res) {
00364         lua_pushstring(d.getString().c_str());
00365     } else {
00366         lua_pushnil();
00367     }
00368 }
00369 
00370 void Lua_V1::IsMessageGoing() {
00371     lua_Object actorObj = lua_getparam(1);
00372 
00373     if (!actorObj || (lua_isuserdata(actorObj) && lua_tag(actorObj) == MKTAG('A','C','T','R')) || lua_isnil(actorObj)) {
00374         if (lua_isuserdata(actorObj) && lua_tag(actorObj) == MKTAG('A','C','T','R')) {
00375             Actor *actor = getactor(actorObj);
00376             if (actor) {
00377                 pushbool(actor->isTalking());
00378             }
00379         } else {
00380             // NOTE: this part is quite delicate. If you do changes test the crying angelitos in set fo.
00381             pushbool(g_grim->areActorsTalking());
00382         }
00383     } else
00384         lua_pushnil();
00385 }
00386 
00387 } // end of namespace Grim


Generated on Sat Feb 23 2019 05:01:08 for ResidualVM by doxygen 1.7.1
curved edge   curved edge