Browse Source

Merge pull request #42 from viciious/elems_by_class

Implement Element::GetElementsByClassName
Lloyd Weehuizen 13 years ago
parent
commit
cf5e68c068

+ 4 - 0
Include/Rocket/Core/Element.h

@@ -494,6 +494,10 @@ public:
 	/// @param[out] elements Resulting elements.
 	/// @param[in] tag Tag to search for.
 	void GetElementsByTagName(ElementList& elements, const String& tag);
+	/// Get all descendant elements with the given class set on them.
+	/// @param[out] elements Resulting elements.
+	/// @param[in] tag Tag to search for.
+	void GetElementsByClassName(ElementList& elements, const String& class_name);
 	//@}
 
 	/**

+ 5 - 0
Include/Rocket/Core/ElementUtilities.h

@@ -71,6 +71,11 @@ public:
 	/// @param[in] root_element First element to check.
 	/// @param[in] tag Tag to search for.
 	static void GetElementsByTagName(ElementList& elements, Element* root_element, const String& tag);
+	/// Get all elements with the given class set on them.
+	/// @param[out] elements Resulting elements.
+	/// @param[in] root_element First element to check.
+	/// @param[in] tag Class name to search for.
+	static void GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name);
 
 	/// Returns an element's font face.
 	/// @param[in] element The element to determine the font face for.

+ 6 - 0
Source/Core/Element.cpp

@@ -1203,6 +1203,12 @@ void Element::GetElementsByTagName(ElementList& elements, const String& tag)
 	return ElementUtilities::GetElementsByTagName(elements, this, tag);
 }
 
+// Get all elements with the given class set on them.
+void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
+{
+	return ElementUtilities::GetElementsByClassName(elements, this, class_name);
+}
+
 // Access the event dispatcher
 EventDispatcher* Element::GetEventDispatcher() const
 {

+ 22 - 0
Source/Core/ElementUtilities.cpp

@@ -87,6 +87,28 @@ void ElementUtilities::GetElementsByTagName(ElementList& elements, Element* root
 	}
 }
 
+void ElementUtilities::GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name)
+{
+	// Breadth first search on elements for the corresponding id
+	typedef std::queue< Element* > SearchQueue;
+	SearchQueue search_queue;
+	for (int i = 0; i < root_element->GetNumChildren(); ++i)
+		search_queue.push(root_element->GetChild(i));
+
+	while (!search_queue.empty())
+	{
+		Element* element = search_queue.front();
+		search_queue.pop();
+
+		if (element->IsClassSet(class_name))
+			elements.push_back(element);
+
+		// Add all children to search.
+		for (int i = 0; i < element->GetNumChildren(); i++)
+			search_queue.push(element->GetChild(i));
+	}
+}
+
 // Returns the element's font face.
 FontFaceHandle* ElementUtilities::GetFontFaceHandle(Element* element)
 {