RefPtr<PropertyLine<float32> > PropertyLineYamlReader::CreateFloatPropertyLineFromYamlNode(YamlNode * parentNode, const String & propertyName, RefPtr<PropertyLine<float32> > defaultPropertyLine)
{
	YamlNode * node = parentNode->Get(propertyName);
	if (!node)return defaultPropertyLine;

	if (node->GetType() == YamlNode::TYPE_STRING)
	{
		return RefPtr< PropertyLine<float32> >(new PropertyLineValue<float32>(node->AsFloat()));
	}else if (node->GetType() == YamlNode::TYPE_ARRAY)
	{
		RefPtr<PropertyLineKeyframes<float32> > keyframes(new PropertyLineKeyframes<float32>());

		for (int k = 0; k < node->GetCount() / 2; ++k)
		{
			YamlNode * time = node->Get(k * 2);
			YamlNode * value = node->Get(k * 2 + 1);

			if (time && value)
			{
				keyframes->AddValue(time->AsFloat(), value->AsFloat());
			}
		}
		return keyframes;
	}
	return RefPtr<PropertyLine<float32> >();
}
RefPtr< PropertyLine<Vector2> > PropertyLineYamlReader::CreateVector2PropertyLineFromYamlNode( YamlNode * parentNode, const String & propertyName, RefPtr< PropertyLine<Vector2> > defaultPropertyLine /*= 0*/ )
{
	YamlNode * node = parentNode->Get(propertyName);
	if (!node)return defaultPropertyLine;

	if (node->GetType() == YamlNode::TYPE_STRING)
	{
		float32 v = node->AsFloat();
		return RefPtr< PropertyLine<Vector2> >(new PropertyLineValue<Vector2>(Vector2(v, v)));
	}else if (node->GetType() == YamlNode::TYPE_ARRAY)
	{
		if (node->GetCount() == 2)
		{
			Vector2 res(1.0f, 1.0f);
			res = node->AsPoint();
			return RefPtr< PropertyLine<Vector2> >(new PropertyLineValue<Vector2>(res));
		}

		RefPtr< PropertyLineKeyframes<Vector2> > keyframes (new PropertyLineKeyframes<Vector2>());

		for (int k = 0; k < node->GetCount() / 2; ++k)
		{
			YamlNode * time = node->Get(k * 2);
			YamlNode * value = node->Get(k * 2 + 1);

			if (time && value)
			{
				if (value->GetType() == YamlNode::TYPE_ARRAY)
				{
					keyframes->AddValue(time->AsFloat(), value->AsPoint());
				}
				else 
				{
					float32 v = value->AsFloat();
					keyframes->AddValue(time->AsFloat(), Vector2(v, v));
				}
			}
		}
		return keyframes;
	}

	return RefPtr< PropertyLine<Vector2> >();
}
RefPtr< PropertyLine<Color> > PropertyLineYamlReader::CreateColorPropertyLineFromYamlNode( YamlNode * parentNode, const String & propertyName, RefPtr< PropertyLine<Color> > defaultPropertyLine)
{
	YamlNode * node = parentNode->Get(propertyName);
	if (!node)return defaultPropertyLine;

	if (node->GetType() == YamlNode::TYPE_ARRAY)
	{
		bool allString = true;
		for (int k = 0; k < node->GetCount(); ++k)
			if (node->Get(k)->GetType() != YamlNode::TYPE_STRING)
				allString = false;

		if (allString && node->GetCount() == 4)
		{
			return RefPtr< PropertyLine<Color> >(new PropertyLineValue<Color>(ColorFromYamlNode(node)));
		}else
		{
			RefPtr< PropertyLineKeyframes<Color> > keyframes (new PropertyLineKeyframes<Color>());

			for (int k = 0; k < node->GetCount() / 2; ++k)
			{
				YamlNode * time = node->Get(k * 2);
				YamlNode * value = node->Get(k * 2 + 1);

				if (time && value)
				{
					if (value->GetType() == YamlNode::TYPE_ARRAY)
					{
						keyframes->AddValue(time->AsFloat(), ColorFromYamlNode(value));
					}
				}
			}
			return keyframes;
		}
	}
	return RefPtr< PropertyLine<Color> >();
}
 RefPtr< PropertyLine<Vector3> > PropertyLineYamlReader::CreateVector3PropertyLineFromYamlNode( YamlNode * parentNode, const String & propertyName, RefPtr< PropertyLine<Vector3> > defaultPropertyLine /*= 0*/ )
 {
     YamlNode * node = parentNode->Get(propertyName);
     if (!node)return defaultPropertyLine;
     
     if (node->GetType() == YamlNode::TYPE_STRING)
     {
         if(propertyName == "emissionAngle") // for old emissionAngle compatibility
         {
             Vector3 res(0, 0, 0);
             float32 angle = DegToRad(node->AsFloat());
             res.x = cosf(angle);
             res.y = sinf(angle);
             return RefPtr< PropertyLine<Vector3> >(new PropertyLineValue<Vector3>(res));
         }
         
         float32 v = node->AsFloat();
         return RefPtr< PropertyLine<Vector3> >(new PropertyLineValue<Vector3>(Vector3(v, v, v)));
     }
     else if (node->GetType() == YamlNode::TYPE_ARRAY)
     {
         if(node->GetCount() == 2) // for 2D forces compatibility
         {
             Vector3 res(node->AsVector2());
             res.z = 0.0f;
             return RefPtr< PropertyLine<Vector3> >(new PropertyLineValue<Vector3>(res));
         }
         if (node->GetCount() == 3 || node->GetCount() == 2) 
         {
             Vector3 res(0.0f, 0.0f, 0.0f);
             res = node->AsVector3();
             return RefPtr< PropertyLine<Vector3> >(new PropertyLineValue<Vector3>(res));
         }
         
         RefPtr< PropertyLineKeyframes<Vector3> > keyframes (new PropertyLineKeyframes<Vector3>());
         
         for (int k = 0; k < node->GetCount() / 2; ++k)
         {
             YamlNode * time = node->Get(k * 2);
             YamlNode * value = node->Get(k * 2 + 1);
             
             if (time && value)
             {
                 if (value->GetType() == YamlNode::TYPE_ARRAY)
                 {
                     keyframes->AddValue(time->AsFloat(), value->AsVector3());
                 }
                 else 
                 {
                     Vector3 v = value->AsVector3();
                     if(propertyName == "emissionAngle") // for old emissionAngle compatibility
                     {
                         float32 angle = DegToRad(value->AsFloat());
                         v.x = cosf(angle);
                         v.y = sinf(angle);
                         v.z = 0.0f;
                     }
                     keyframes->AddValue(time->AsFloat(), v);
                 }
             }
         }
         return keyframes;
     }
     
     return RefPtr< PropertyLine<Vector3> >();
 }
Ejemplo n.º 5
0
bool YamlParser::Parse(YamlDataHolder * dataHolder)
{
	yaml_parser_t parser;
	yaml_event_t event;
	
	int done = 0;
	
	/* Create the Parser object. */
	yaml_parser_initialize(&parser);
	
	yaml_parser_set_encoding(&parser, YAML_UTF8_ENCODING);
	
	/* Set a string input. */
	//yaml_parser_set_input_string(&parser, (const unsigned char*)pathName.c_str(), pathName.length());
		
	yaml_parser_set_input(&parser, read_handler, dataHolder);

	String lastMapKey;
	bool isKeyPresent = false;

	/* Read the event sequence. */
	while (!done) 
	{
		
		/* Get the next event. */
		if (!yaml_parser_parse(&parser, &event))
		{
			Logger::Error("[YamlParser::Parse] error: type: %d %s line: %d pos: %d", parser.error, parser.problem, parser.problem_mark.line, parser.problem_mark.column);
			break;
		}
		
		switch(event.type)
		{
		case YAML_ALIAS_EVENT:
			Logger::FrameworkDebug("[YamlParser::Parse] alias: %s", event.data.alias.anchor);
			break;
		
		case YAML_SCALAR_EVENT:
			{
				String scalarValue = (const char*)event.data.scalar.value;

				if (objectStack.empty())
				{
					YamlNode * node = YamlNode::CreateStringNode();
					node->Set(scalarValue);
					rootObject = node;
				}
				else
				{
					YamlNode * topContainer = objectStack.top();
					DVASSERT(topContainer->GetType() != YamlNode::TYPE_STRING);
					if (topContainer->GetType() == YamlNode::TYPE_MAP)
					{
						if (!isKeyPresent)
						{
							lastMapKey = scalarValue;
						}
						else
						{
							topContainer->Add(lastMapKey, scalarValue);
						}
						isKeyPresent = !isKeyPresent;
					}
					else if (topContainer->GetType() == YamlNode::TYPE_ARRAY)
					{
						topContainer->Add(scalarValue);
					}
				}
			}
			break;
		
		case YAML_DOCUMENT_START_EVENT:
			//Logger::FrameworkDebug("document start:");
			break;
		
		case YAML_DOCUMENT_END_EVENT:
			//Logger::FrameworkDebug("document end:");
			break;

		case YAML_SEQUENCE_START_EVENT:
			{
				YamlNode * node = YamlNode::CreateArrayNode();
				if (objectStack.empty())
				{
					rootObject = node;
				}
				else
				{
					YamlNode * topContainer = objectStack.top();
					DVASSERT(topContainer->GetType() != YamlNode::TYPE_STRING);
					if (topContainer->GetType() == YamlNode::TYPE_MAP)
					{
						DVASSERT(isKeyPresent);
						topContainer->AddNodeToMap(lastMapKey, node);
						isKeyPresent = false;
					}
					else if (topContainer->GetType() == YamlNode::TYPE_ARRAY)
					{
						topContainer->AddNodeToArray(node);
					}
				}
				objectStack.push(node);
			}break;
				
		case YAML_SEQUENCE_END_EVENT:
			{
				objectStack.pop();
			}break;
		
		case YAML_MAPPING_START_EVENT:
			{
				YamlNode * node = YamlNode::CreateMapNode();
				if (objectStack.empty())
				{
					rootObject = node;
				}
				else
				{
					YamlNode * topContainer = objectStack.top();
					if (topContainer->GetType() == YamlNode::TYPE_MAP)
					{
						DVASSERT(isKeyPresent);
						topContainer->AddNodeToMap(lastMapKey, node);
						isKeyPresent = false;
					}
					else if (topContainer->GetType() == YamlNode::TYPE_ARRAY)
					{
						topContainer->AddNodeToArray(node);
					}
				}
				objectStack.push(node);
			}
			break;
				
		case YAML_MAPPING_END_EVENT:
			{
				objectStack.pop();
			}
			break;
        default:
            break;
		};

		/* Are we finished? */
		done = (event.type == YAML_STREAM_END_EVENT);
		
		/* The application is responsible for destroying the event object. */
		yaml_event_delete(&event);
		
	}

	/* Destroy the Parser object. */
	yaml_parser_delete(&parser);

    DVASSERT(objectStack.size() == 0);

    return true;
}