Jump to content

Recommended Posts

Posted

This is the code the editor uses to read JSON component files and create the interface.

for (int n = 0; n < j3["component"]["properties"].size(); ++n)
{
	Vec3 range = Vec3(NAN);
	options.clear();
	WString name, label, pattern, filepath;
	int type = 0;
	int style = 0;
	if (not j3["component"]["properties"][n].is_object()) continue;
	if (j3["component"]["properties"][n].is_number_unsigned()) style = j3["component"]["properties"][n];
	name = std::string(j3["component"]["properties"][n]["name"]);
	if (j3["component"]["properties"][n]["label"].is_string()) label = std::string(j3["component"]["properties"][n]["label"]);
	switch (j3["component"]["properties"][n]["value"].type())
	{
	case nlohmann::json::value_t::null:
		type = PROPERTY_OBJECT;
		style = TEXTFIELD_READONLY;
		break;
	case nlohmann::json::value_t::number_integer:
	case nlohmann::json::value_t::number_unsigned:
		style = SPINNER_INTEGER;
		type = PROPERTY_NUMBER;
		if (j3["component"]["properties"][n]["options"].is_array())
		{
			style = 0;
			type = PROPERTY_OPTION;
			for (int k = 0; k < j3["component"]["properties"][n]["options"].size(); ++k)
			{
				if (j3["component"]["properties"][n]["options"][k].is_string())
				{
					std::string s = j3["component"]["properties"][n]["options"][k];
					options.push_back(s);
				}
			}
		}
		break;
	case nlohmann::json::value_t::number_float:
		style = SPINNER_FLOAT;
		type = PROPERTY_NUMBER;
		/*if (j3["component"]["properties"][n]["range"].is_array() and j3["component"]["properties"][n]["range"].size() == 2)
		{
			if (j3["component"]["properties"][n]["range"][0].is_number() and j3["component"]["properties"][n]["range"][1].is_number())
			{

			}
		}*/
		break;
	case nlohmann::json::value_t::string:
		type = PROPERTY_STRING;
		style = TEXTFIELD_LOSEFOCUSACTIONEVENT | TEXTFIELD_ENTERKEYACTIONEVENT;
		if (j3["component"]["properties"][n]["filecategory"].is_string())
		{
			style = 0;
			type = PROPERTY_FILE;
			std::string cat = j3["component"]["properties"][n]["filecategory"];
			if (cat == "SOUND")
			{
				pattern = GetFilePattern(FILECATEGORY_SOUND);
				filepath = "Sound/";
			}
			else if (cat == "MODEL")
			{
				pattern = GetFilePattern(FILECATEGORY_MODEL);
				filepath = "Models/";
			}
			else if (cat == "MATERIAL")
			{
				pattern = GetFilePattern(FILECATEGORY_MATERIAL);
				filepath = "Materials/";
			}
			else if (cat == "TEXTURE")
			{
				pattern = GetFilePattern(FILECATEGORY_TEXTURE);
				filepath = "Materials/";
			}
			else if (cat == "PREFAB")
			{
				pattern = GetFilePattern(FILECATEGORY_PREFAB);
				filepath = "Prefabs/";
			}
		}
		else if (j3["component"]["properties"][n]["filepattern"].is_string())
		{
			style = 0;
			type = PROPERTY_FILE;
			pattern = std::string(j3["component"]["properties"][n]["filepattern"]);
		}
		break;
	case nlohmann::json::value_t::boolean:
		type = PROPERTY_BOOLEAN;
		break;
	case nlohmann::json::value_t::array:
		switch (j3["component"]["properties"][n]["value"].size())
		{
		case 2:
			type = PROPERTY_VECTOR2;
			break;
		case 3:
			type = PROPERTY_VECTOR3;
			if (j3["component"]["properties"][n]["type"].is_string() and j3["component"]["properties"][n]["type"] == "COLOR")
			{
				type = PROPERTY_COLOR;
				style = COLOREDIT_RGB;
			}
			break;
		case 4:
			type = PROPERTY_VECTOR4;
			if (j3["component"]["properties"][n]["type"].is_string() and j3["component"]["properties"][n]["type"] == "COLOR")
			{
				type = PROPERTY_COLOR;
				style = COLOREDIT_RGBA;
			}
			break;
		default:
			continue;
		}
		break;
	default:
		continue;
	}
	c.varnames[label] = name;
	c.values[name] = j3["component"]["properties"][n]["value"];
	if (j3["component"]["properties"][n]["style"].is_number_unsigned()) style = j3["component"]["properties"][n]["style"];
	auto g = c.propertygroup->AddProperty(label, PropertyType(type), style);
	if (type == PROPERTY_OPTION)
	{
		for (auto s : options)
		{
			g->widget->AddItem(s);
		}
	}
	if (type == PROPERTY_FILE)
	{
		g->widget->As<PathEdit>()->SetPattern(pattern);
		g->widget->As<PathEdit>()->defaultpath = filepath;
	}
	if (type == PROPERTY_OBJECT)
	{
		Listen(EVENT_WIDGETACTION, g->button);
	}
	g->id = name;
}

 

  • Like 1

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...