| 83 | } |
| 84 | |
| 85 | List<WidgetConstructResult> WidgetParser::constructor(Json const& config) { |
| 86 | List<WidgetConstructResult> widgets; |
| 87 | |
| 88 | auto addWidget = [this, &widgets](Json const& memberConfig) { |
| 89 | if (memberConfig.type() != Json::Type::Object || !memberConfig.contains("type") || !memberConfig.contains("name")) |
| 90 | throw WidgetParserException( |
| 91 | "Malformed gui json: member configuration is either not a map, or does not specify a widget name and type"); |
| 92 | String type = memberConfig.getString("type"); |
| 93 | if (type == "include") { |
| 94 | widgets.appendAll(constructor(Root::singleton().assets()->json(memberConfig.getString("file")))); |
| 95 | } else { |
| 96 | if (!m_constructors.contains(type)) { |
| 97 | throw WidgetParserException(strf("Unknown type in gui json. {}", type)); |
| 98 | } |
| 99 | auto constructResult = |
| 100 | m_constructors.get(memberConfig.getString("type"))(memberConfig.getString("name"), memberConfig); |
| 101 | if (constructResult.obj) |
| 102 | widgets.append(constructResult); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | if (config.isType(Json::Type::Object)) { |
| 107 | for (auto const& kvpair : config.iterateObject()) |
| 108 | addWidget(kvpair.second.set("name", kvpair.first)); |
| 109 | } else if (config.isType(Json::Type::Array)) { |
| 110 | for (auto const& elem : config.iterateArray()) |
| 111 | addWidget(elem); |
| 112 | } else { |
| 113 | throw WidgetParserException(strf("Malformed gui json, expected a Map or a List. Instead got {}", config)); |
| 114 | } |
| 115 | |
| 116 | return widgets; |
| 117 | } |
| 118 | |
| 119 | void WidgetParser::registerCallback(String const& name, WidgetCallbackFunc callback) { |
| 120 | m_callbacks[name] = callback; |
nothing calls this directly
no test coverage detected