void Level::loadBoundsElement(const CL_DomNode &p_boundsNode) { const CL_DomNodeList boundList = p_boundsNode.get_child_nodes(); const int boundListSize = boundList.get_length(); for (int i = 0; i < boundListSize; ++i) { const CL_DomNode boundNode = boundList.item(i); if (boundNode.get_node_name() == "bound") { CL_DomNamedNodeMap attrs = boundNode.get_attributes(); float x1 = CL_StringHelp::local8_to_float(attrs.get_named_item("x1").get_node_value()); float y1 = CL_StringHelp::local8_to_float(attrs.get_named_item("y1").get_node_value()); float x2 = CL_StringHelp::local8_to_float(attrs.get_named_item("x2").get_node_value()); float y2 = CL_StringHelp::local8_to_float(attrs.get_named_item("y2").get_node_value()); x1 *= Block::WIDTH; y1 *= Block::WIDTH; x2 *= Block::WIDTH; y2 *= Block::WIDTH; cl_log_event("debug", "Loading bound %1 x %2 -> %3 x %4", x1, y1, x2, y2); const CL_LineSegment2f segment(CL_Pointf(x1, y1), CL_Pointf(x2, y2)); m_bounds.push_back(CL_SharedPtr<Bound>(new Bound(segment))); } else { cl_log_event("race", "Unknown node '%1', ignoring", boundNode.get_node_name()); } } }
void CL_DomElement::remove_attribute(const CL_DomString &name) { if (impl) { CL_DomNamedNodeMap attributes = get_attributes(); attributes.remove_named_item(name); } }
CL_DomAttr CL_DomElement::get_attribute_node(const CL_DomString &name) const { if (impl) { CL_DomNamedNodeMap attributes = get_attributes(); CL_DomAttr attribute = attributes.get_named_item(name).to_attr(); return attribute; } return CL_DomAttr(); }
void CL_DomElement::remove_attribute_ns( const CL_DomString &namespace_uri, const CL_DomString &local_name) { if (impl) { CL_DomNamedNodeMap attributes = get_attributes(); attributes.remove_named_item_ns(namespace_uri, local_name); } }
CL_DomAttr CL_DomElement::get_attribute_node_ns( const CL_DomString &namespace_uri, const CL_DomString &local_name) const { if (impl) { CL_DomNamedNodeMap attributes = get_attributes(); CL_DomAttr attribute = attributes.get_named_item_ns(namespace_uri, local_name).to_attr(); return attribute; } return CL_DomAttr(); }
void Level::loadSandElement(const CL_DomNode &p_sandNode) { const CL_DomNodeList sandChildren = p_sandNode.get_child_nodes(); const int sandChildrenCount = sandChildren.get_length(); CL_DomNode sandChildNode, groupChildNode; for (int i = 0; i < sandChildrenCount; ++i) { sandChildNode = sandChildren.item(i); if (sandChildNode.get_node_name() == "group") { const CL_DomNodeList groupChildren = sandChildNode.get_child_nodes(); const int groupChildrenCount = groupChildren.get_length(); // create new sandpit m_sandpits.push_back(Sandpit()); Sandpit &sandpit = m_sandpits.back(); for (int j = 0; j < groupChildrenCount; ++j) { groupChildNode = groupChildren.item(j); if (groupChildNode.get_node_name() == "circle") { CL_DomNamedNodeMap attrs = groupChildNode.get_attributes(); const float x = CL_StringHelp::local8_to_float(attrs.get_named_item("x").get_node_value()); const float y = CL_StringHelp::local8_to_float(attrs.get_named_item("y").get_node_value()); const float radius = CL_StringHelp::local8_to_float(attrs.get_named_item("radius").get_node_value()); // add to sandpit // must save as integer const CL_Pointf centerFloat = real(CL_Pointf(x, y)); const CL_Point centerInt = CL_Point((int) floor(centerFloat.x), (int) floor(centerFloat.y)); sandpit.addCircle(centerInt, real(radius)); // m_resistanceMap.addGeometry(geom, 0.8f); } else { cl_log_event("error", "unknown element in <sand><group></group></sand>: <%1>", sandChildNode.get_node_name()); } } } else { cl_log_event("error", "unknown element in <sand></sand>: <%1>", sandChildNode.get_node_name()); } } }
void CL_DomElement::set_attribute(const CL_DomString &name, const CL_DomString &value) { if (impl) { CL_DomNamedNodeMap attributes = get_attributes(); CL_DomAttr attribute = attributes.get_named_item(name).to_attr(); if (attribute.is_attr()) { attribute.set_node_value(value); } else { attribute = get_owner_document().create_attribute(name); attribute.set_node_value(value); attributes.set_named_item(attribute); } } }
CL_DomString CL_DomNode::find_prefix(const CL_DomString &namespace_uri) const { CL_DomElement cur = to_element(); while (!cur.is_null()) { CL_DomNamedNodeMap attributes = cur.get_attributes(); int size = attributes.get_length(); for (int index = 0; index < size; index++) { CL_DomNode attribute = attributes.item(index); if (attribute.get_prefix() == "xmlns" && attribute.get_node_value() == namespace_uri) { return attribute.get_local_name(); } } cur = cur.get_parent_node().to_element(); } return CL_DomString(); }
//рекурсивное клонирование XML-элемента CL_DomElement cloneElement(CL_DomElement &element, CL_DomDocument &doc) { //создаем элемент-клон CL_DomElement clone(doc, element.get_tag_name()); //копируем атрибуты CL_DomNamedNodeMap attributes = element.get_attributes(); unsigned long length = attributes.get_length(); for (unsigned long i = 0; i < length; ++i) { CL_DomNode attr = attributes.item(i); clone.set_attribute(attr.get_node_name(), attr.get_node_value()); } //рекурсивно копируем дочерние элементы for (CL_DomElement child = element.get_first_child_element(); !child.is_null(); child = child.get_next_sibling_element()) clone.append_child(cloneElement(child, doc)); //возвращаем клонированный элемент return clone; }
void Level::loadTrackElement(const CL_DomNode &p_trackNode) { // build block type map typedef std::map<CL_String, Common::GroundBlockType> blockMap_t; blockMap_t blockMap; blockMap_t::iterator blockMapItor; blockMap["vert"] = Common::BT_STREET_VERT; blockMap["horiz"] = Common::BT_STREET_HORIZ; blockMap["turn_bottom_right"] = Common::BT_TURN_BOTTOM_RIGHT; blockMap["turn_bottom_left"] = Common::BT_TURN_BOTTOM_LEFT; blockMap["turn_top_right"] = Common::BT_TURN_TOP_RIGHT; blockMap["turn_top_left"] = Common::BT_TURN_TOP_LEFT; blockMap["start_line_up"] = Common::BT_START_LINE_UP; // prepare level blocks const int blocksCount = m_width * m_height; m_blocks.clear(); m_blocks.reserve(blocksCount); for (int i = 0; i < blocksCount; ++i) { m_blocks.push_back(CL_SharedPtr<Block>(new Block(Common::BT_GRASS))); } // create global resistance geometry CL_SharedPtr<RaceResistance::Geometry> globalResGeom(new RaceResistance::Geometry()); globalResGeom->addRectangle(CL_Rectf(real(0), real(0), real(m_width), real(m_height))); m_resistanceMap.addGeometry(globalResGeom, 0.3f); // add sand resistance foreach (const Sandpit &sandpit, m_sandpits) { const unsigned circleCount = sandpit.getCircleCount(); CL_SharedPtr<RaceResistance::Geometry> sandpitGeometry(new RaceResistance::Geometry()); for (unsigned i = 0; i < circleCount; ++i) { // sandpit values are real const Sandpit::Circle &circle = sandpit.circleAt(i); sandpitGeometry->addCircle(CL_Circlef(circle.getCenter().x, circle.getCenter().y, circle.getRadius())); } m_resistanceMap.addGeometry(sandpitGeometry, 0.8f); } // read blocks const CL_DomNodeList blockList = p_trackNode.get_child_nodes(); const int blockListSize = blockList.get_length(); cl_log_event("debug", "Track node child count: %1", blockListSize); CL_Pointf lastCP; // last checkpoint for (int i = 0; i < blockListSize; ++i) { const CL_DomNode blockNode = blockList.item(i); if (blockNode.get_node_name() == "block") { CL_DomNamedNodeMap attrs = blockNode.get_attributes(); const int x = CL_StringHelp::local8_to_int(attrs.get_named_item("x").get_node_value()); const int y = CL_StringHelp::local8_to_int(attrs.get_named_item("y").get_node_value()); const CL_String typeStr = attrs.get_named_item("type").get_node_value(); if (x < 0 || y < 0 || x >= m_width || y >= m_height) { cl_log_event("debug", "coords x=%1, y=%2", x, y); throw CL_Exception("Blocks coords out of bounds"); } blockMapItor = blockMap.find(typeStr); if (blockMapItor != blockMap.end()) { const Common::GroundBlockType blockType = blockMapItor->second; m_blocks[m_width * y + x]->setType(blockType); // add checkpoint to track if (blockType == Common::BT_START_LINE_UP) { lastCP = CL_Pointf((x + 0.5f) * Block::WIDTH, (y + 0.2f) * Block::WIDTH); const CL_Pointf firstCP((x + 0.5f) * Block::WIDTH, (y + 0.2 - 0.01f) * Block::WIDTH); m_track.addCheckpointAtPosition(firstCP); } else { const CL_Pointf checkPosition((x + 0.5f) * Block::WIDTH, (y + 0.5f) * Block::WIDTH); m_track.addCheckpointAtPosition(checkPosition); } // add resistance geometry based on block CL_SharedPtr<RaceResistance::Geometry> resGeom = buildResistanceGeometry(x, y, blockType); m_resistanceMap.addGeometry(resGeom, 0.0f); } else { cl_log_event("race", "Unknown block type: %1", typeStr); } } else { cl_log_event("race", "Unknown node '%1', ignoring", blockNode.get_node_name()); } } m_track.addCheckpointAtPosition(lastCP); m_track.close(); }
void CL_DomDocument::save(CL_IODevice &output, bool insert_whitespace) { CL_XMLWriter writer(output); writer.set_insert_whitespace(insert_whitespace); std::vector<CL_DomNode> node_stack; CL_DomNode cur_node = get_first_child(); while (!cur_node.is_null()) { // Create opening node: CL_XMLToken opening_node; opening_node.type = (CL_XMLToken::TokenType) cur_node.get_node_type(); opening_node.variant = cur_node.has_child_nodes() ? CL_XMLToken::BEGIN : CL_XMLToken::SINGLE; opening_node.name = cur_node.get_node_name(); opening_node.value = cur_node.get_node_value(); if (cur_node.is_element()) { CL_DomNamedNodeMap attributes = cur_node.get_attributes(); int length = attributes.get_length(); for (int i = 0; i < length; ++i) { CL_DomAttr attribute = attributes.item(i).to_attr(); opening_node.attributes.push_back( CL_XMLToken::Attribute( attribute.get_name(), attribute.get_value())); } } writer.write(opening_node); // Create any possible child nodes: if (cur_node.has_child_nodes()) { node_stack.push_back(cur_node); cur_node = cur_node.get_first_child(); continue; } // Create closing nodes until we reach next opening node in tree: while (true) { if (cur_node.has_child_nodes()) { CL_XMLToken closing_node; closing_node.type = (CL_XMLToken::TokenType) cur_node.get_node_type(); closing_node.name = cur_node.get_node_name(); closing_node.variant = CL_XMLToken::END; writer.write(closing_node); } cur_node = cur_node.get_next_sibling(); if (!cur_node.is_null()) break; if (node_stack.empty()) break; cur_node = node_stack.back(); node_stack.pop_back(); } } }