UKN_API ConfigParserPtr ConfigParser::MakeParser(ResourcePtr resource) { if(*resource) { ConfigParserPtr ptr = MakeSharedPtr<ConfigParserXmlImpl>(); if(ptr && ptr->open(resource)) { return ptr; } } else { return ConfigParserPtr(); } return ConfigParserPtr(); }
UKN_API ConfigParserPtr ConfigParser::MakeEmptyParser(ConfigParserType type) { switch(type) { case CPT_XML: { ConfigParserPtr ptr = MakeSharedPtr<ConfigParserXmlImpl>(); ptr->create(); return ptr; } case CPT_JSON: default: break; } return ConfigParserPtr(); }
bool Font::deserialize(const ConfigParserPtr& config) { mGlyphs.clear(); if(config && config->toNode(L"font")) { UknString font_name = config->getString(L"name"); if(!font_name.empty()) { UknString fullFontPath = Path::CheckAndGetFontPath(font_name); if(fullFontPath.empty()) { log_error(L"ukn::Font::deserialize: error finding font name " + font_name); return false; } ResourcePtr fontResource = ResourceLoader::Instance().loadResource(fullFontPath); if(fontResource && fontResource->getResourceStream()) { bool result = mFace->load(fontResource); if(result) { mGlyphs.resize(mFace->face->num_glyphs); for(int i = 0; i < mFace->face->num_glyphs; ++i) { mGlyphs[i].size = mFontSize; mGlyphs[i].face = &mFace->face; } } } } mEnableShadow = config->getBool(L"shadow", false); mEnableStroke = config->getBool(L"stroke", false); if(config->getBool(L"bold", false)) { this->setFTStyle(FT_STYLE_FLAG_BOLD); } if(config->getBool(L"italic", false)) { this->setFTStyle(FT_STYLE_FLAG_ITALIC); } mShadowXOffset = config->getInt(L"shadow_offset_x", 0); mShadowYOffset = config->getInt(L"shadow_offset_y", 0); mStrokeWidth = config->getInt(L"stroke_width", 0); mFontSize = config->getInt(L"size", 14); mFontName = font_name; config->toParent(); return true; } return false; }
bool Font::serialize(const ConfigParserPtr& cfg) { if(cfg && !mGlyphs.empty()) { cfg->beginNode(L"font"); cfg->setString(L"name", mFontName); cfg->setBool(L"shadow", mEnableShadow); cfg->setBool(L"stroke", mEnableStroke); cfg->setInt(L"shadow_offset_x", mShadowXOffset); cfg->setInt(L"shadow_offset_y", mShadowYOffset); cfg->setInt(L"stroke_width", mStrokeWidth); cfg->setInt(L"size", mFontSize); cfg->setBool(L"bold", (bool)(mFace->face->style_flags & FT_STYLE_FLAG_BOLD)); cfg->setBool(L"italic", (bool)(mFace->face->style_flags & FT_STYLE_FLAG_ITALIC)); cfg->endNode(); return true; } else { log_error(L"ukn::Font::serialize: unable to serialize font "+mFontName); } return false; }
bool AnimationPlayer::deserialize(const ConfigParserPtr& parser) { if(parser->toNode(L"animations")) { if(parser->toFirstChild()) { do { StoryBoardPtr storyboard = MakeSharedPtr<StoryBoard>(); MistString name = parser->getString(L"name"); if(!name.empty()) { bool isdefault = parser->getBool(L"default", false); if(isdefault) setDefault(name); if(parser->toFirstChild()) { do { MistString type = parser->getCurrentNodeName(); // linear animation if(type == L"linear") { MistString prop_name = parser->getString(L"prop"); MistString prop_type = parser->getString(L"prop_type"); void* prop = getProperty(prop_name); if(prop) { create_linear_animation(prop_type, prop, parser, storyboard.get()); } // keyframe animation } else if(type == L"keyframe") { MistString prop_name = parser->getString(L"prop"); MistString prop_type = parser->getString(L"prop_type"); void* prop = getProperty(prop_name); if(prop) { create_key_frame_animation(prop_type, prop, parser, storyboard.get()); // back to parent node parser->toParent(); } } } while(parser->toNextChild()); // back to parent node parser->toParent(); // add storyboard mStoryBoards[name] = storyboard; // connect to finish event storyboard->onComplete() += Bind(this, &AnimationPlayer::onAnimationFinished); } } } while(parser->toNextChild()); parser->toParent(); } parser->toParent(); } else return false; if(!mDefault.empty()) play(mDefault); return true; }
inline void create_key_frame_animation(const MistString& type, void* prop, const ConfigParserPtr& parser, StoryBoard* storyboard) { if(type == L"int") { SharedPtr<KeyFrameAnimation<int> > anim = MakeSharedPtr<KeyFrameAnimation<int> >(); int32 time = parser->getInt(L"time", 0); int32 duration = parser->getInt(L"duration", 0); if(duration == 0) return; anim->setDuration(duration); if(parser->toFirstChild()) { do { if(parser->hasAttribute(L"val")) { int32 val = parser->getInt(L"val", 0); int32 dur = parser->getInt(L"duration", 0); int32 type = parser->getInt(L"type", 0); anim->getKeyFrames().push_back(KeyFrameAnimation<int>::KeyFrame(val, dur, type == 0 ? KFT_Linear : KFT_Discrete)); } } while(parser->toNextChild()); storyboard->children().push_back(StoryBoard::AnimationInfo(time, anim, prop)); } return; } if(type == L"float") { SharedPtr<KeyFrameAnimation<float> > anim = MakeSharedPtr<KeyFrameAnimation<float> >(); int32 time = parser->getInt(L"time", 0); int32 duration = parser->getInt(L"duration", 0); if(duration == 0) return; anim->setDuration(duration); if(parser->toFirstChild()) { do { if(parser->hasAttribute(L"val")) { float val = parser->getFloat(L"val", 0); int32 dur = parser->getInt(L"duration", 0); int32 type = parser->getInt(L"type", 0); anim->getKeyFrames().push_back(KeyFrameAnimation<float>::KeyFrame(val, dur, type == 0 ? KFT_Linear : KFT_Discrete)); } } while(parser->toNextChild()); storyboard->children().push_back(StoryBoard::AnimationInfo(time, anim, prop)); } return; } if(type == L"double") { SharedPtr<KeyFrameAnimation<double> > anim = MakeSharedPtr<KeyFrameAnimation<double> >(); int32 time = parser->getInt(L"time", 0); int32 duration = parser->getInt(L"duration", 0); if(duration == 0) return; anim->setDuration(duration); if(parser->toFirstChild()) { do { if(parser->hasAttribute(L"val")) { int32 val = parser->getInt(L"val", 0); int32 dur = parser->getInt(L"duration", 0); int32 type = parser->getInt(L"type", 0); anim->getKeyFrames().push_back(KeyFrameAnimation<double>::KeyFrame(val, dur, type == 0 ? KFT_Linear : KFT_Discrete)); } } while(parser->toNextChild()); storyboard->children().push_back(StoryBoard::AnimationInfo(time, anim, prop)); } return; } if(type == L"uint") { SharedPtr<KeyFrameAnimation<uint32> > anim = MakeSharedPtr<KeyFrameAnimation<uint32> >(); int32 time = parser->getInt(L"time", 0); int32 duration = parser->getInt(L"duration", 0); if(duration == 0) return; anim->setDuration(duration); if(parser->toFirstChild()) { do { if(parser->hasAttribute(L"val")) { int32 val = parser->getInt(L"val", 0); int32 dur = parser->getInt(L"duration", 0); int32 type = parser->getInt(L"type", 0); anim->getKeyFrames().push_back(KeyFrameAnimation<uint32>::KeyFrame(val, dur, type == 0 ? KFT_Linear : KFT_Discrete)); } } while(parser->toNextChild()); storyboard->children().push_back(StoryBoard::AnimationInfo(time, anim, prop)); } return; } if(type == L"color") { // to do } }
inline void create_linear_animation(const MistString& type, void* prop, const ConfigParserPtr& parser, StoryBoard* storyboard) { if(type == L"int") { SharedPtr<LinearAnimation<int> > anim = MakeSharedPtr<LinearAnimation<int> >(); anim->setFrom(parser->getInt(L"from", 0)); anim->setTo(parser->getInt(L"to", 0)); anim->setDuration(parser->getInt(L"duration", 0)); storyboard->children().push_back(StoryBoard::AnimationInfo(parser->getInt(L"time", 0), anim, prop)); return; } if(type == L"float") { SharedPtr<LinearAnimation<float> > anim = MakeSharedPtr<LinearAnimation<float> >(); anim->setFrom(parser->getFloat(L"from", 0)); anim->setTo(parser->getFloat(L"to", 0)); anim->setDuration(parser->getInt(L"duration", 0)); storyboard->children().push_back(StoryBoard::AnimationInfo(parser->getInt(L"time", 0), anim, prop)); return; } if(type == L"uint") { SharedPtr<LinearAnimation<uint32> > anim = MakeSharedPtr<LinearAnimation<uint32> >(); anim->setFrom(parser->getInt(L"from", 0)); anim->setTo(parser->getInt(L"to", 0)); anim->setDuration(parser->getInt(L"duration", 0)); storyboard->children().push_back(StoryBoard::AnimationInfo(parser->getInt(L"time", 0), anim, prop)); return; } if(type == L"double") { SharedPtr<LinearAnimation<double> > anim = MakeSharedPtr<LinearAnimation<double> >(); anim->setFrom(parser->getFloat(L"from", 0)); anim->setTo(parser->getFloat(L"to", 0)); anim->setDuration(parser->getInt(L"duration", 0)); storyboard->children().push_back(StoryBoard::AnimationInfo(parser->getInt(L"time", 0), anim, prop)); return; } if(type == L"color") { // to do return; } }
void Context::saveCfgFile(const UknString& name) { ConfigParserPtr configParser = ConfigParser::MakeEmptyParser(CPT_XML); if(configParser) { configParser->beginNode(L"cfg"); configParser->beginNode(L"graphics"); configParser->setInt(L"width", mCfg.render_cfg.width); configParser->setInt(L"height", mCfg.render_cfg.height); configParser->setInt(L"top", mCfg.render_cfg.top); configParser->setInt(L"left", mCfg.render_cfg.left); configParser->setBool(L"show_mouse", mCfg.render_cfg.show_mouse); configParser->setBool(L"full_screen", mCfg.render_cfg.full_screen); configParser->setBool(L"resizable", mCfg.render_cfg.resizable); configParser->setInt(L"sample_count", mCfg.render_cfg.sample_count); configParser->setInt(L"sample_quality", mCfg.render_cfg.sample_quality); configParser->setString(L"color_fmt", element_format_to_string(mCfg.render_cfg.color_fmt)); configParser->setString(L"depth_stencil_fmt", element_format_to_string(mCfg.render_cfg.depth_stencil_fmt)); configParser->toNode(L"/cfg"); configParser->beginNode(L"plugins"); if(!mCfg.graphic_factory_name.empty()) configParser->setString(L"graphic_factory", mCfg.graphic_factory_name); } UknString formattedString = configParser->writeToString(); ResourcePtr resource = ResourceLoader::Instance().createFileResource(name); if(resource) { resource->getResourceStream()->write((const uint8*)formattedString.data(), formattedString.size()); } }
void Context::loadCfgFile(const UknString& name) { ContextCfg cfg; cfg.render_cfg.width = 800; cfg.render_cfg.height = 600; cfg.render_cfg.top = 0; cfg.render_cfg.left = 0; cfg.render_cfg.color_fmt = EF_RGBA8; cfg.render_cfg.depth_stencil_fmt = EF_D16; cfg.render_cfg.resizable = false; cfg.render_cfg.full_screen = false; cfg.render_cfg.show_mouse = true; cfg.render_cfg.sample_count = 0; cfg.render_cfg.sample_quality = 0; ConfigParserPtr configParser = AssetManager::Instance().load<ConfigParser>(name); if(configParser) { if(configParser->toNode(L"/cfg/graphics")) { cfg.render_cfg.width = configParser->getInt(L"width", cfg.render_cfg.width); cfg.render_cfg.height = configParser->getInt(L"height", cfg.render_cfg.height); cfg.render_cfg.top = configParser->getInt(L"top", cfg.render_cfg.top); cfg.render_cfg.left = configParser->getInt(L"left", cfg.render_cfg.left); cfg.render_cfg.resizable = configParser->getBool(L"resizable", cfg.render_cfg.resizable); cfg.render_cfg.show_mouse = configParser->getBool(L"show_mouse", cfg.render_cfg.show_mouse); cfg.render_cfg.full_screen = configParser->getBool(L"full_screen", cfg.render_cfg.full_screen); cfg.render_cfg.sample_count = configParser->getInt(L"sample_count", cfg.render_cfg.sample_count); cfg.render_cfg.sample_quality = configParser->getInt(L"sample_quality", cfg.render_cfg.sample_quality); UknString fmt_string; int fmt_id; fmt_string = configParser->getString(L"color_fmt", L"EF_ARGB8"); fmt_id = fmt_string_to_element_format(fmt_string); if(fmt_id == -1) { log_warning(format_string("ukn::Context::loadCfgFile: unknown color format %s, using EF_ARGB8", fmt_string.c_str())); cfg.render_cfg.color_fmt = (ElementFormat)fmt_id; } fmt_string = configParser->getString(L"depth_stencil_fmt", L"EF_D16"); fmt_id = fmt_string_to_element_format(fmt_string); if(fmt_id == -1) { log_warning(format_string("ukn::Context::loadCfgFile: unknown depth stencil format %s, using EF_D16", fmt_string.c_str())); cfg.render_cfg.depth_stencil_fmt = (ElementFormat)fmt_id; } } if(configParser->toNode(L"/cfg/plugins")) { cfg.graphic_factory_name = configParser->getString(L"graphic_factory", L""); } } else { log_warning(L"ukn::Context::loadCfgFile: unable to open config file " + name); } setCfg(cfg); if(!configParser) { saveCfgFile(L"config.xml"); } }