/* ScummVM - Graphic Adventure Engine * * ScummVM is the legal property of its developers, whose names * are too numerous to list here. Please refer to the COPYRIGHT * file distributed with this source distribution. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ /* * This file is based on WME Lite. * http://dead-code.org/redir.php?target=wmelite * Copyright (c) 2011 Jan Nedoma */ #include "engines/wintermute/dcgf.h" #include "engines/wintermute/Base/BGame.h" #include "engines/wintermute/Ad/AdLayer.h" #include "engines/wintermute/Ad/AdSceneNode.h" #include "engines/wintermute/Base/BParser.h" #include "engines/wintermute/Base/BDynBuffer.h" #include "engines/wintermute/Base/scriptables/ScValue.h" #include "engines/wintermute/Base/scriptables/ScScript.h" #include "engines/wintermute/Base/scriptables/ScStack.h" #include "engines/wintermute/Base/BFileManager.h" #include "engines/wintermute/PlatformSDL.h" #include "common/str.h" namespace WinterMute { IMPLEMENT_PERSISTENT(CAdLayer, false) ////////////////////////////////////////////////////////////////////////// CAdLayer::CAdLayer(CBGame *inGame): CBObject(inGame) { _main = false; _width = _height = 0; _active = true; _closeUp = false; } ////////////////////////////////////////////////////////////////////////// CAdLayer::~CAdLayer() { for (int i = 0; i < _nodes.GetSize(); i++) delete _nodes[i]; _nodes.RemoveAll(); } ////////////////////////////////////////////////////////////////////////// HRESULT CAdLayer::loadFile(const char *filename) { byte *buffer = Game->_fileManager->readWholeFile(filename); if (buffer == NULL) { Game->LOG(0, "CAdLayer::LoadFile failed for file '%s'", filename); return E_FAIL; } HRESULT ret; _filename = new char [strlen(filename) + 1]; strcpy(_filename, filename); if (FAILED(ret = loadBuffer(buffer, true))) Game->LOG(0, "Error parsing LAYER file '%s'", filename); delete [] buffer; return ret; } TOKEN_DEF_START TOKEN_DEF(LAYER) TOKEN_DEF(TEMPLATE) TOKEN_DEF(NAME) TOKEN_DEF(WIDTH) TOKEN_DEF(HEIGHT) TOKEN_DEF(MAIN) TOKEN_DEF(ENTITY) TOKEN_DEF(REGION) TOKEN_DEF(ACTIVE) TOKEN_DEF(EDITOR_SELECTED) TOKEN_DEF(SCRIPT) TOKEN_DEF(CAPTION) TOKEN_DEF(PROPERTY) TOKEN_DEF(CLOSE_UP) TOKEN_DEF(EDITOR_PROPERTY) TOKEN_DEF_END ////////////////////////////////////////////////////////////////////////// HRESULT CAdLayer::loadBuffer(byte *buffer, bool complete) { TOKEN_TABLE_START(commands) TOKEN_TABLE(LAYER) TOKEN_TABLE(TEMPLATE) TOKEN_TABLE(NAME) TOKEN_TABLE(WIDTH) TOKEN_TABLE(HEIGHT) TOKEN_TABLE(MAIN) TOKEN_TABLE(ENTITY) TOKEN_TABLE(REGION) TOKEN_TABLE(ACTIVE) TOKEN_TABLE(EDITOR_SELECTED) TOKEN_TABLE(SCRIPT) TOKEN_TABLE(CAPTION) TOKEN_TABLE(PROPERTY) TOKEN_TABLE(CLOSE_UP) TOKEN_TABLE(EDITOR_PROPERTY) TOKEN_TABLE_END byte *params; int cmd; CBParser parser(Game); if (complete) { if (parser.getCommand((char **)&buffer, commands, (char **)¶ms) != TOKEN_LAYER) { Game->LOG(0, "'LAYER' keyword expected."); return E_FAIL; } buffer = params; } while ((cmd = parser.getCommand((char **)&buffer, commands, (char **)¶ms)) > 0) { switch (cmd) { case TOKEN_TEMPLATE: if (FAILED(loadFile((char *)params))) cmd = PARSERR_GENERIC; break; case TOKEN_NAME: setName((char *)params); break; case TOKEN_CAPTION: setCaption((char *)params); break; case TOKEN_MAIN: parser.scanStr((char *)params, "%b", &_main); break; case TOKEN_CLOSE_UP: parser.scanStr((char *)params, "%b", &_closeUp); break; case TOKEN_WIDTH: parser.scanStr((char *)params, "%d", &_width); break; case TOKEN_HEIGHT: parser.scanStr((char *)params, "%d", &_height); break; case TOKEN_ACTIVE: parser.scanStr((char *)params, "%b", &_active); break; case TOKEN_REGION: { CAdRegion *region = new CAdRegion(Game); CAdSceneNode *node = new CAdSceneNode(Game); if (!region || !node || FAILED(region->loadBuffer(params, false))) { cmd = PARSERR_GENERIC; delete region; delete node; region = NULL; node = NULL; } else { node->setRegion(region); _nodes.Add(node); } } break; case TOKEN_ENTITY: { CAdEntity *entity = new CAdEntity(Game); CAdSceneNode *node = new CAdSceneNode(Game); if (entity) entity->_zoomable = false; // scene entites default to NOT zoom if (!entity || !node || FAILED(entity->loadBuffer(params, false))) { cmd = PARSERR_GENERIC; delete entity; delete node; entity = NULL; node = NULL; } else { node->setEntity(entity); _nodes.Add(node); } } break; case TOKEN_EDITOR_SELECTED: parser.scanStr((char *)params, "%b", &_editorSelected); break; case TOKEN_SCRIPT: addScript((char *)params); break; case TOKEN_PROPERTY: parseProperty(params, false); break; case TOKEN_EDITOR_PROPERTY: parseEditorProperty(params, false); break; } } if (cmd == PARSERR_TOKENNOTFOUND) { Game->LOG(0, "Syntax error in LAYER definition"); return E_FAIL; } return S_OK; } ////////////////////////////////////////////////////////////////////////// // high level scripting interface ////////////////////////////////////////////////////////////////////////// HRESULT CAdLayer::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // GetNode ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "GetNode") == 0) { stack->correctParams(1); CScValue *val = stack->pop(); int node = -1; if (val->_type == VAL_INT) node = val->getInt(); else { // get by name for (int i = 0; i < _nodes.GetSize(); i++) { if ((_nodes[i]->_type == OBJECT_ENTITY && scumm_stricmp(_nodes[i]->_entity->_name, val->getString()) == 0) || (_nodes[i]->_type == OBJECT_REGION && scumm_stricmp(_nodes[i]->_region->_name, val->getString()) == 0)) { node = i; break; } } } if (node < 0 || node >= _nodes.GetSize()) stack->pushNULL(); else { switch (_nodes[node]->_type) { case OBJECT_ENTITY: stack->pushNative(_nodes[node]->_entity, true); break; case OBJECT_REGION: stack->pushNative(_nodes[node]->_region, true); break; default: stack->pushNULL(); } } return S_OK; } ////////////////////////////////////////////////////////////////////////// // AddRegion / AddEntity ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "AddRegion") == 0 || strcmp(name, "AddEntity") == 0) { stack->correctParams(1); CScValue *val = stack->pop(); CAdSceneNode *node = new CAdSceneNode(Game); if (strcmp(name, "AddRegion") == 0) { CAdRegion *region = new CAdRegion(Game); if (!val->isNULL()) region->setName(val->getString()); node->setRegion(region); stack->pushNative(region, true); } else { CAdEntity *entity = new CAdEntity(Game); if (!val->isNULL()) entity->setName(val->getString()); node->setEntity(entity); stack->pushNative(entity, true); } _nodes.Add(node); return S_OK; } ////////////////////////////////////////////////////////////////////////// // InsertRegion / InsertEntity ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "InsertRegion") == 0 || strcmp(name, "InsertEntity") == 0) { stack->correctParams(2); int index = stack->pop()->getInt(); CScValue *val = stack->pop(); CAdSceneNode *node = new CAdSceneNode(Game); if (strcmp(name, "InsertRegion") == 0) { CAdRegion *region = new CAdRegion(Game); if (!val->isNULL()) region->setName(val->getString()); node->setRegion(region); stack->pushNative(region, true); } else { CAdEntity *entity = new CAdEntity(Game); if (!val->isNULL()) entity->setName(val->getString()); node->setEntity(entity); stack->pushNative(entity, true); } if (index < 0) index = 0; if (index <= _nodes.GetSize() - 1) _nodes.InsertAt(index, node); else _nodes.Add(node); return S_OK; } ////////////////////////////////////////////////////////////////////////// // DeleteNode ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "DeleteNode") == 0) { stack->correctParams(1); CScValue *val = stack->pop(); CAdSceneNode *toDelete = NULL; if (val->isNative()) { CBScriptable *temp = val->getNative(); for (int i = 0; i < _nodes.GetSize(); i++) { if (_nodes[i]->_region == temp || _nodes[i]->_entity == temp) { toDelete = _nodes[i]; break; } } } else { int index = val->getInt(); if (index >= 0 && index < _nodes.GetSize()) { toDelete = _nodes[index]; } } if (toDelete == NULL) { stack->pushBool(false); return S_OK; } for (int i = 0; i < _nodes.GetSize(); i++) { if (_nodes[i] == toDelete) { delete _nodes[i]; _nodes[i] = NULL; _nodes.RemoveAt(i); break; } } stack->pushBool(true); return S_OK; } else return CBObject::scCallMethod(script, stack, thisStack, name); } ////////////////////////////////////////////////////////////////////////// CScValue *CAdLayer::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "Type") == 0) { _scValue->setString("layer"); return _scValue; } ////////////////////////////////////////////////////////////////////////// // NumNodes (RO) ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "NumNodes") == 0) { _scValue->setInt(_nodes.GetSize()); return _scValue; } ////////////////////////////////////////////////////////////////////////// // Width ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Width") == 0) { _scValue->setInt(_width); return _scValue; } ////////////////////////////////////////////////////////////////////////// // Height ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Height") == 0) { _scValue->setInt(_height); return _scValue; } ////////////////////////////////////////////////////////////////////////// // Main (RO) ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Main") == 0) { _scValue->setBool(_main); return _scValue; } ////////////////////////////////////////////////////////////////////////// // CloseUp ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "CloseUp") == 0) { _scValue->setBool(_closeUp); return _scValue; } ////////////////////////////////////////////////////////////////////////// // Active ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Active") == 0) { _scValue->setBool(_active); return _scValue; } else return CBObject::scGetProperty(name); } ////////////////////////////////////////////////////////////////////////// HRESULT CAdLayer::scSetProperty(const char *name, CScValue *value) { ////////////////////////////////////////////////////////////////////////// // Name ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "Name") == 0) { setName(value->getString()); return S_OK; } ////////////////////////////////////////////////////////////////////////// // CloseUp ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "CloseUp") == 0) { _closeUp = value->getBool(); return S_OK; } ////////////////////////////////////////////////////////////////////////// // Width ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Width") == 0) { _width = value->getInt(); if (_width < 0) _width = 0; return S_OK; } ////////////////////////////////////////////////////////////////////////// // Height ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Height") == 0) { _height = value->getInt(); if (_height < 0) _height = 0; return S_OK; } ////////////////////////////////////////////////////////////////////////// // Active ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Active") == 0) { bool b = value->getBool(); if (b == false && _main) { Game->LOG(0, "Warning: cannot deactivate scene's main layer"); } else _active = b; return S_OK; } else return CBObject::scSetProperty(name, value); } ////////////////////////////////////////////////////////////////////////// const char *CAdLayer::scToString() { return "[layer]"; } ////////////////////////////////////////////////////////////////////////// HRESULT CAdLayer::saveAsText(CBDynBuffer *buffer, int indent) { buffer->putTextIndent(indent, "LAYER {\n"); buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", _name); buffer->putTextIndent(indent + 2, "CAPTION=\"%s\"\n", getCaption()); buffer->putTextIndent(indent + 2, "MAIN=%s\n", _main ? "TRUE" : "FALSE"); buffer->putTextIndent(indent + 2, "WIDTH=%d\n", _width); buffer->putTextIndent(indent + 2, "HEIGHT=%d\n", _height); buffer->putTextIndent(indent + 2, "ACTIVE=%s\n", _active ? "TRUE" : "FALSE"); buffer->putTextIndent(indent + 2, "EDITOR_SELECTED=%s\n", _editorSelected ? "TRUE" : "FALSE"); if (_closeUp) buffer->putTextIndent(indent + 2, "CLOSE_UP=%s\n", _closeUp ? "TRUE" : "FALSE"); int i; for (i = 0; i < _scripts.GetSize(); i++) { buffer->putTextIndent(indent + 2, "SCRIPT=\"%s\"\n", _scripts[i]->_filename); } if (_scProp) _scProp->saveAsText(buffer, indent + 2); for (i = 0; i < _nodes.GetSize(); i++) { switch (_nodes[i]->_type) { case OBJECT_ENTITY: _nodes[i]->_entity->saveAsText(buffer, indent + 2); break; case OBJECT_REGION: _nodes[i]->_region->saveAsText(buffer, indent + 2); break; default: error("CAdLayer::SaveAsText - Unhandled enum"); break; } } CBBase::saveAsText(buffer, indent + 2); buffer->putTextIndent(indent, "}\n\n"); return S_OK; } ////////////////////////////////////////////////////////////////////////// HRESULT CAdLayer::persist(CBPersistMgr *persistMgr) { CBObject::persist(persistMgr); persistMgr->transfer(TMEMBER(_active)); persistMgr->transfer(TMEMBER(_closeUp)); persistMgr->transfer(TMEMBER(_height)); persistMgr->transfer(TMEMBER(_main)); _nodes.persist(persistMgr); persistMgr->transfer(TMEMBER(_width)); return S_OK; } } // end of namespace WinterMute