Exemplo n.º 1
0
void mod_ui::try_add( const std::string &mod_to_add,
                      std::vector<std::string> &active_list )
{
    if( std::find( active_list.begin(), active_list.end(), mod_to_add ) != active_list.end() ) {
        // The same mod can not be added twice. That makes no sense.
        return;
    }
    if( active_manager->mod_map.count( mod_to_add ) == 0 ) {
        debugmsg( "Unable to load mod \"%s\".", mod_to_add.c_str() );
        return;
    }
    MOD_INFORMATION &mod = *active_manager->mod_map[mod_to_add];
    bool errs;
    try {
        dependency_node *checknode = mm_tree->get_node( mod.ident );
        if( !checknode ) {
            return;
        }
        errs = checknode->has_errors();
    } catch( std::exception &e ) {
        errs = true;
    }

    if( errs ) {
        // cannot add, something wrong!
        return;
    }
    // get dependencies of selection in the order that they would appear from the top of the active list
    std::vector<std::string> dependencies = mm_tree->get_dependencies_of_X_as_strings( mod.ident );

    // check to see if mod is a core, and if so check to see if there is already a core in the mod list
    if( mod._type == MT_CORE ) {
        //  (more than 0 active elements) && (active[0] is a CORE)                            &&    active[0] is not the add candidate
        if( ( !active_list.empty() ) && ( active_manager->mod_map[active_list[0]]->_type == MT_CORE ) &&
            ( active_list[0] != mod_to_add ) ) {
            // remove existing core
            try_rem( 0, active_list );
        }

        // add to start of active_list if it doesn't already exist in it
        active_list.insert( active_list.begin(), mod_to_add );
    } else { // _type == MT_SUPPLEMENTAL
        // now check dependencies and add them as necessary
        std::vector<std::string> mods_to_add;
        bool new_core = false;
        for( auto &i : dependencies ) {
            if( std::find( active_list.begin(), active_list.end(), i ) == active_list.end() ) {
                if( active_manager->mod_map[i]->_type == MT_CORE ) {
                    mods_to_add.insert( mods_to_add.begin(), i );
                    new_core = true;
                } else {
                    mods_to_add.push_back( i );
                }
            }
        }

        if( new_core && !active_list.empty() ) {
            try_rem( 0, active_list );
            active_list.insert( active_list.begin(), mods_to_add[0] );
            mods_to_add.erase( mods_to_add.begin() );
        }
        // now add the rest of the dependencies serially to the end
        for( auto &i : mods_to_add ) {
            active_list.push_back( i );
        }
        // and finally add the one we are trying to add!
        active_list.push_back( mod.ident );
    }
}
int mod_ui::gather_input(int &active_header, int &selection, std::vector<std::string> mod_list,
                         std::vector<std::string> &active_mods_list)
{
    char ch = input();
    const int next_header = (active_header == 1) ? 0 : 1;
    const int prev_header = (active_header == 0) ? 1 : 0;

    const int input_header = active_header;

    int next_selection = selection + 1;
    int prev_selection = selection - 1;

    if (active_header == 0) {
        if (prev_selection < 0) {
            prev_selection = mod_list.size() - 1;
        }
        if (next_selection >= mod_list.size()) {
            next_selection = 0;
        }
    } else if (active_header == 1) {
        if (prev_selection < 0) {
            prev_selection = active_mods_list.size() - 1;
        }
        if (next_selection >= active_mods_list.size()) {
            next_selection = 0;
        }
    }


    switch (ch) {
        case 'j': // move down
            selection = next_selection;
            break;
        case 'k': // move up
            selection = prev_selection;
            break;
        case 'l': // switch active section
            active_header = next_header;
            break;
        case 'h': // switch active section
            active_header = prev_header;
            break;
        case '\n': // select
            if (active_header == 0) {
                try_add(mod_list[selection], active_mods_list);
            } else if (active_header == 1) {
                try_rem(selection, active_mods_list);
            }
            break;
        case '+': // move active mod up
        case '-': // move active mod down
            if (active_header == 1) {
                try_shift(ch, selection, active_mods_list);
            }
            //try_shift(ch, sel2, active_mods);
            break;
        case KEY_ESCAPE: // exit!
            return -1;
    }

    if (input_header == 1 && active_header == 1) {
        if (active_mods_list.size() == 0) {
            selection = -1;
        } else {
            if (selection < 0) {
                selection = 0;
            } else if (selection >= active_mods_list.size()) {
                selection = active_mods_list.size() - 1;
            }
        }
    }

    return 0;
}