void AObjectTypeHandler::init(const JsonNode & input, boost::optional<std::string> name) { base = input["base"]; if (!input["rmg"].isNull()) { rmgInfo.value = input["rmg"]["value"].Float(); rmgInfo.mapLimit = loadJsonOrMax(input["rmg"]["mapLimit"]); rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]); rmgInfo.rarity = input["rmg"]["rarity"].Float(); } // else block is not needed - set in constructor for (auto entry : input["templates"].Struct()) { entry.second.setType(JsonNode::DATA_STRUCT); JsonUtils::inherit(entry.second, base); ObjectTemplate tmpl; tmpl.id = Obj(type); tmpl.subid = subtype; tmpl.stringID = entry.first; // FIXME: create "fullID" - type.object.template? tmpl.readJson(entry.second); templates.push_back(tmpl); } if (input["name"].isNull()) objectName = name; else objectName.reset(input["name"].String()); initTypeData(input); }
void AObjectTypeHandler::addTemplate(JsonNode config) { config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null JsonUtils::inherit(config, base); ObjectTemplate tmpl; tmpl.id = Obj(type); tmpl.subid = subtype; tmpl.stringID = ""; // TODO? tmpl.readJson(config); templates.push_back(tmpl); }
QVariant MapToVariantConverter::toVariant(const ObjectTemplate &objectTemplate, const QDir &directory) { mMapDir = directory; QVariantMap objectTemplateVariant; objectTemplateVariant[QLatin1String("type")] = QLatin1String("template"); mGidMapper.clear(); if (Tileset *tileset = objectTemplate.object()->cell().tileset()) { unsigned firstGid = 1; mGidMapper.insert(firstGid, tileset->sharedPointer()); objectTemplateVariant[QLatin1String("tileset")] = toVariant(*tileset, firstGid); } objectTemplateVariant[QLatin1String("object")] = toVariant(*objectTemplate.object()); return objectTemplateVariant; }
Local<Function> FunctionTemplate::GetFunction(JSObject* parent) { HandleScope scope; PrivateData* pd = PrivateData::Get(cx(), InternalObject()); Local<Function> fn = pd->cachedFunction.get(); if (!fn.IsEmpty()) { return scope.Close(Local<Function>::New(fn)); } Handle<Value> name = pd->name.get(); if (name.IsEmpty()) name = String::Empty(); String::AsciiValue fnName(name); JSFunction* func = JS_NewFunction(cx(), CallCallback, 0, JSFUN_CONSTRUCTOR, NULL, *fnName); JSObject* obj = JS_GetFunctionObject(func); Object o(obj); Local<String> prototypeStr = String::NewSymbol("prototype"); (void)o.Set(prototypeStr, PrototypeTemplate()->NewInstance(parent)); Local<Value> thiz = Local<Value>::New(&InternalObject()); o.SetInternalField(0, thiz); fn = Local<Function>::New(reinterpret_cast<Function*>(&o)); pd->cachedFunction = fn; // Set all attributes that were added with Template::Set on the object. AttributeStorage::Range attributes = pd->attributes.all(); while (!attributes.empty()) { AttributeStorage::Entry& entry = attributes.front(); Handle<Value> v = entry.value.get(); if (IsFunctionTemplate(v)) { FunctionTemplate *tmpl = reinterpret_cast<FunctionTemplate*>(*v); v = tmpl->GetFunction(parent); } else if (IsObjectTemplate(v)) { ObjectTemplate *tmpl = reinterpret_cast<ObjectTemplate*>(*v); v = tmpl->NewInstance(parent); } (void)o.Set(String::FromJSID(entry.key), v); attributes.popFront(); } return scope.Close(fn); }
static QString saveObjectTemplate(const MapObject *mapObject) { FormatHelper<ObjectTemplateFormat> helper(FileFormat::ReadWrite); QString filter = helper.filter(); QString selectedFilter = XmlObjectTemplateFormat().nameFilter(); Preferences *prefs = Preferences::instance(); QString suggestedFileName = prefs->lastPath(Preferences::ObjectTemplateFile); suggestedFileName += QLatin1Char('/'); if (!mapObject->name().isEmpty()) suggestedFileName += mapObject->name(); else suggestedFileName += QCoreApplication::translate("Tiled::Internal::MainWindow", "untitled"); suggestedFileName += QLatin1String(".tx"); QWidget *parent = DocumentManager::instance()->widget()->window(); QString fileName = QFileDialog::getSaveFileName(parent, QCoreApplication::translate("Tiled::Internal::MainWindow", "Save Template"), suggestedFileName, filter, &selectedFilter); if (fileName.isEmpty()) return QString(); ObjectTemplateFormat *format = helper.formatByNameFilter(selectedFilter); ObjectTemplate objectTemplate; objectTemplate.setObject(mapObject); if (!format->write(&objectTemplate, fileName)) { QMessageBox::critical(nullptr, QCoreApplication::translate("Tiled::Internal::MainWindow", "Error Saving Template"), format->errorString()); return QString(); } prefs->setLastPath(Preferences::ObjectTemplateFile, QFileInfo(fileName).path()); return fileName; }
std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize) { CLegacyConfigParser parser("Data/Objects.txt"); size_t totalNumber = parser.readNumber(); // first line contains number of objects to read and nothing else parser.endLine(); for (size_t i=0; i<totalNumber; i++) { ObjectTemplate templ; templ.readTxt(parser); parser.endLine(); std::pair<si32, si32> key(templ.id.num, templ.subid); legacyTemplates.insert(std::make_pair(key, templ)); } std::vector<JsonNode> ret(dataSize);// create storage for 256 objects assert(dataSize == 256); CLegacyConfigParser namesParser("Data/ObjNames.txt"); for (size_t i=0; i<256; i++) { ret[i]["name"].String() = namesParser.readString(); namesParser.endLine(); } CLegacyConfigParser cregen1Parser("data/crgen1"); do customNames[Obj::CREATURE_GENERATOR1].push_back(cregen1Parser.readString()); while(cregen1Parser.endLine()); CLegacyConfigParser cregen4Parser("data/crgen4"); do customNames[Obj::CREATURE_GENERATOR4].push_back(cregen4Parser.readString()); while(cregen4Parser.endLine()); return ret; }
//---------------------------------------------------------------------------------- CBaseEditor *OgitorsClipboardManager::instantiateTemplate(const Ogre::String& templatename) { CBaseEditor *retObject = OgitorsRoot::getSingletonPtr()->GetSelection(); CBaseEditor *item = 0; ObjectTemplateMap::const_iterator it; it = mGeneralTemplates.find(templatename); if(it == mGeneralTemplates.end()) { it = mProjectTemplates.find(templatename); if(it == mProjectTemplates.end()) return 0; } ObjectTemplate objTemplate = it->second; OgitorsPropertyValueMap::iterator pit; NameObjectPairList list; NameObjectPairList objlist; std::vector<CBaseEditor*> objlist2; NameObjectPairList::iterator nit; Ogre::String parentname; int numParentObjects = 0; for(unsigned int i = 0;i < objTemplate.size();i++) { OgitorsPropertyValueMap objMap = objTemplate[i].mObjectProperties; if((pit = objMap.find("parentnode")) != objMap.end()) { parentname = Ogre::any_cast<Ogre::String>(pit->second.val); if((nit = list.find(parentname)) != list.end()) { pit->second.val = Ogre::Any(nit->second->getName()); } else { objMap.erase(pit); ++numParentObjects; } } else ++numParentObjects; parentname = Ogre::any_cast<Ogre::String>(objMap["name"].val); objMap["name"].val = Ogre::Any(parentname + OgitorsRoot::getSingletonPtr()->CreateUniqueID(parentname,"",0)); item = OgitorsRoot::getSingletonPtr()->CreateEditorObject(0, objTemplate[i].mTypeName, objMap, true, false); if(item) { item->load(); item->getCustomProperties()->initFromSet(*(objTemplate[i].mCustomProperties)); list.insert(NameObjectPairList::value_type(parentname, item)); objlist.insert(NameObjectPairList::value_type(item->getName(), item)); objlist2.push_back(item); } } Ogre::Vector3 pos(999999, 999999, 999999); if(numParentObjects == 0) return 0; else if(numParentObjects == 1) { retObject = objlist2[0]; OgitorsRoot::getSingletonPtr()->GetSelection()->setSelection(retObject); } else static_cast<CMultiSelEditor*>(retObject)->setSelection(objlist); if(retObject->getProperties()->hasProperty("position")) retObject->getProperties()->setValue("position", pos); return retObject; }