void XData::jumpOutOfBrackets(parser::DefTokeniser& tok, int currentDepth) const { while ( tok.hasMoreTokens() && currentDepth > 0) { std::string token = tok.nextToken(); if (token == "{") currentDepth += 1; else if (token == "}") currentDepth -= 1; } }
Vector4 GuiWindowDef::parseVector4(parser::DefTokeniser& tokeniser) { // Collect tokens until all four components are parsed std::vector<std::string> comp; while (comp.size() < 4 && tokeniser.hasMoreTokens()) { std::string token = getExpression(tokeniser); if (token == ",") continue; if (token.find(',') != std::string::npos) { std::vector<std::string> parts; boost::algorithm::split(parts, token, boost::algorithm::is_any_of(",")); for (std::size_t i = 0; i < parts.size(); ++i) { comp.push_back(boost::algorithm::trim_copy(parts[i])); } continue; } // TODO: Catch GUI expressions comp.push_back(token); } if (comp.size() != 4) { throw parser::ParseException("Couldn't parse Vector4, not enough components found."); } return Vector4(string::convert<float>(comp[0]), string::convert<float>(comp[1]), string::convert<float>(comp[2]), string::convert<float>(comp[3])); }
// Returns a GUI expression, which can be a number, a string or a formula ("gui::objVisible" == 1). std::string GuiWindowDef::getExpression(parser::DefTokeniser& tokeniser) { std::string returnValue = tokeniser.nextToken(); if (returnValue == "(") { // Assemble token until closing brace found std::size_t depth = 1; while (depth > 0 && tokeniser.hasMoreTokens()) { std::string token = tokeniser.nextToken(); if (token == ")") depth--; returnValue += token; } } // Strip quotes boost::algorithm::trim_if(returnValue, boost::algorithm::is_any_of("\"")); return returnValue; }
void GuiWindowDef::constructFromTokens(parser::DefTokeniser& tokeniser) { // The windowDef keyword has already been parsed, so expect a name plus an opening brace here name = tokeniser.nextToken(); tokeniser.assertNextToken("{"); while (tokeniser.hasMoreTokens()) { std::string token = tokeniser.nextToken(); boost::algorithm::to_lower(token); if (token == "rect") { rect = parseVector4(tokeniser); } else if (token == "visible") { visible = parseBool(tokeniser); } else if (token == "notime") { notime = parseBool(tokeniser); } else if (token == "forecolor") { forecolor = parseVector4(tokeniser); } else if (token == "backcolor") { backcolor = parseVector4(tokeniser); } else if (token == "bordercolor") { bordercolor = parseVector4(tokeniser); } else if (token == "matcolor") { matcolor = parseVector4(tokeniser); } else if (token == "rotate") { rotate = parseFloat(tokeniser); } else if (token == "text") { setText(parseString(tokeniser)); } else if (token == "font") { font = parseString(tokeniser); // Cut off the "fonts/" part boost::algorithm::replace_first(font, "fonts/", ""); } else if (token == "textscale") { textscale = parseFloat(tokeniser); } else if (token == "textalign") { textalign = parseInt(tokeniser); } else if (token == "textalignx") { textalignx = parseFloat(tokeniser); } else if (token == "textaligny") { textaligny = parseFloat(tokeniser); } else if (token == "forceaspectwidth") { forceaspectwidth = parseFloat(tokeniser); } else if (token == "forceaspectheight") { forceaspectheight = parseFloat(tokeniser); } else if (token == "background") { background = parseString(tokeniser); } else if (token == "noevents") { noevents = parseBool(tokeniser); } else if (token == "nocursor") { nocursor = parseBool(tokeniser); } else if (token == "noclip") { noclip = parseBool(tokeniser); } else if (token == "nowrap") { nowrap = parseBool(tokeniser); } else if (token == "modal") { noevents = parseBool(tokeniser); } else if (token == "menugui") { menugui = parseBool(tokeniser); } else if (token == "windowdef") { // Child windowdef GuiWindowDefPtr window(new GuiWindowDef(_owner)); window->constructFromTokens(tokeniser); addWindow(window); } else if (token == "ontime") { std::string timeStr = tokeniser.nextToken(); // Check the time for validity std::size_t time = string::convert<std::size_t>( timeStr, std::numeric_limits<std::size_t>::max() ); if (time == std::numeric_limits<std::size_t>::max()) { rWarning() << "Invalid time encountered in onTime event in " << name << ": " << timeStr << std::endl; } // Allocate a new GuiScript GuiScriptPtr script(new GuiScript(*this)); script->constructFromTokens(tokeniser); _timedEvents.insert(TimedEventMap::value_type(time, script)); } else if (token == "onnamedevent") { std::string eventName = tokeniser.nextToken(); // Parse the script GuiScriptPtr script(new GuiScript(*this)); script->constructFromTokens(tokeniser); // TODO: Save event } else if (token == "onevent") { GuiScriptPtr script(new GuiScript(*this)); script->constructFromTokens(tokeniser); // TODO } else if (token == "onesc") { GuiScriptPtr script(new GuiScript(*this)); script->constructFromTokens(tokeniser); // TODO } else if (token == "onmouseenter" || token == "onmouseexit") { GuiScriptPtr script(new GuiScript(*this)); script->constructFromTokens(tokeniser); // TODO } else if (token == "onaction") { GuiScriptPtr script(new GuiScript(*this)); script->constructFromTokens(tokeniser); // TODO } else if (token == "float" || token == "definefloat") { // TODO: Add variable std::string variableName = tokeniser.nextToken(); } else if (token == "definevec4") { // TODO: Add variable std::string variableName = tokeniser.nextToken(); parseVector4(tokeniser); } else if (token == "}") { break; } else { rWarning() << "Unknown token encountered in GUI: " << token << std::endl; } } }