extend Element API

This commit is contained in:
Alexey Fedoseev
2024-05-09 16:46:15 +03:00
parent 8938d86717
commit 510a3ddea9
2 changed files with 57 additions and 2 deletions

View File

@@ -60,6 +60,17 @@ Element::Element(Element* _parent, ElementType _type, const ID& _id, const Name&
set_name(_name);
}
int Element::index() const
{
if (parent) {
const ElementCollection* collection = static_cast<const ElementCollection*>(parent);
CYB_ASSERT(collection);
return collection->element_index(this);
} else {
return 0;
}
}
void Element::set_name(const Name& n)
{
name = n;
@@ -674,7 +685,7 @@ ElementList ElementCollection::find_elements_by_types(const ElementTypes& types)
size_t ElementCollection::elements_count() const
{
size_t count = 0;
size_t count = 1;
for (ElementList::const_iterator i = children.begin(); i != children.end(); i++) {
count += (*i)->elements_count();
}
@@ -691,6 +702,18 @@ bool ElementCollection::has_initial() const
return false;
}
int ElementCollection::element_index(const Element* e) const
{
CYB_ASSERT(e);
int index = 0;
for (ElementList::const_iterator i = children.begin(); i != children.end(); i++, index++) {
if (*i == e) {
return index;
}
}
return -1;
}
void ElementCollection::add_element(Element* e)
{
CYB_ASSERT(e);
@@ -788,6 +811,32 @@ Element* ElementCollection::first_element()
}
}
const Element* ElementCollection::get_element(int index) const
{
int idx = 0;
if (has_children()) {
for (ElementList::const_iterator i = children.begin(); i != children.end(); i++, idx++) {
if (idx == index) {
return *i;
}
}
}
return NULL;
}
Element* ElementCollection::get_element(int index)
{
int idx = 0;
if (has_children()) {
for (ElementList::iterator i = children.begin(); i != children.end(); i++, idx++) {
if (idx == index) {
return *i;
}
}
}
return NULL;
}
CyberiadaNode* ElementCollection::to_node() const
{
CyberiadaNode* node = Element::to_node();