C++
openFrameworks

C++でのswapChildrenとgetChildIndexの実装例

More than 3 years have passed since last update.
vector<flash::DisplayObject*> children;

void DisplayObjectContainer::swapChildren(flash::DisplayObject* child1, flash::DisplayObject* child2) {
    int index1 = getChildIndex(child1);
    int index2 = getChildIndex(child2);

    if(index1 == -1 || index2 == -1) return;    
    for(int i = 0; i < children.size(); i++) {
        if(children[i] == child1 || children[i] == child2) {
            children.erase(children.begin() + i--);
        }
    }

    if(index1 < index2) {
        children.insert(children.begin() + index1, child2);
        children.insert(children.begin() + index2, child1);
    } else {
        children.insert(children.begin() + index2, child1);
        children.insert(children.begin() + index1, child2);
    }
}


const int& DisplayObjectContainer::getChildIndex(flash::DisplayObject* child){
    for(int i = 0; i < children.size(); i++){
        if(children[i] == child) return i;
    }

    return -1;
}