Beispiel #1
0
/** @copydoc widgetdata::event_func */
static int widget_event(widgetdata *widget, SDL_Event *event)
{
    uint32_t row, col;
    size_t i;

    if (EVENT_IS_MOUSE(event) && event->button.button == SDL_BUTTON_LEFT && list_mouse_get_pos(list_skills, event->motion.x, event->motion.y, &row, &col)) {
        size_t skill_id;

        skill_id = row * list_skills->cols + col;

        if (skill_id < skill_list_num) {
            if (event->type == SDL_MOUSEBUTTONUP) {
                if (selected_skill != skill_id) {
                    selected_skill = skill_id;
                    widget->redraw = 1;
                    return 1;
                }
            } else if (event->type == SDL_MOUSEBUTTONDOWN) {
                event_dragging_start(skill_list[skill_id]->skill->tag, event->motion.x, event->motion.y);
                return 1;
            }
        }
    }

    /* If the list has handled the mouse event, we need to redraw the
     * widget. */
    if (list_skills && list_handle_mouse(list_skills, event)) {
        widget->redraw = 1;
        return 1;
    }


    for (i = 0; i < BUTTON_NUM; i++) {
        if (button_event(&buttons[i], event)) {
            switch (i) {
            case BUTTON_CLOSE:
                widget->show = 0;
                break;

            case BUTTON_HELP:
                help_show("skill list");
                break;
            }

            widget->redraw = 1;
            return 1;
        }

        if (buttons[i].redraw) {
            widget->redraw = 1;
        }
    }

    return 0;
}
Beispiel #2
0
/**
 * Handle mouse events for one list. Checking whether the mouse is over
 * the list should have been done before calling this.
 * @param list
 * The list.
 * @param event
 * Event.
 * @return
 * 1 if the event was handled, 0 otherwise.
 */
int list_handle_mouse(list_struct *list, SDL_Event *event)
{
    uint32_t row, col, old_highlighted, old_selected;
    int mx, my;

    if (!list) {
        return 0;
    }

    if (event->type != SDL_MOUSEBUTTONDOWN && event->type != SDL_MOUSEBUTTONUP && event->type != SDL_MOUSEMOTION) {
        return 0;
    }

    mx = event->motion.x - list->px;
    my = event->motion.y - list->py;

    if (list->scrollbar_enabled) {
        list->scrollbar.px = list->px;
        list->scrollbar.py = list->py;

        if (scrollbar_event(&list->scrollbar, event)) {
            return 1;
        }
    }

    if (!LIST_MOUSE_OVER(list, mx, my)) {
        return 0;
    }

    old_highlighted = list->row_highlighted;
    old_selected = list->row_selected;

    /* No row is highlighted now. Will be switched back on as needed
     * below. */
    list->row_highlighted = 0;

    if (list_mouse_get_pos(list, event->motion.x, event->motion.y, &row, &col)) {
        if (list->handle_mouse_row_func) {
            list->handle_mouse_row_func(list, row, event);
        }

        /* Mouse click? */
        if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) {
            /* See if we clicked on this row earlier, and whether this
             * should be considered a double click. */
            if (SDL_GetTicks() - list->click_tick < DOUBLE_CLICK_DELAY) {
                /* Double click, handle it as if enter was used. */
                if (list->handle_enter_func) {
                    list->handle_enter_func(list, event);
                    list->click_tick = 0;
                }

                /* Update selected row (in case enter handling
                 * function did not actually jump to another GUI,
                 * thus removing the need for this list). */
                list->row_selected = row + 1;
            } else { /* Normal click. */
                /* Update selected row and click ticks for above
                 * double click calculation. */
                list->row_selected = row + 1;
                list->click_tick = SDL_GetTicks();
            }
        } else {
            /* Not a mouse click, so update highlighted row. */
            list->row_highlighted = row + 1;
        }
    }

    /* Handle mouse wheel for scrolling. */
    if (event->type == SDL_MOUSEBUTTONDOWN && (event->button.button == SDL_BUTTON_WHEELUP || event->button.button == SDL_BUTTON_WHEELDOWN)) {
        list_scroll(list, event->button.button == SDL_BUTTON_WHEELUP, 1);
        return 1;
    }

    if (old_highlighted != list->row_highlighted || old_selected != list->row_selected) {
        return 1;
    }

    return 0;
}