X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Flibgame%2Fgadgets.c;h=244db6ea8852b4a4df027f3ffbc95848b635210d;hp=9ff99690e96a1f3438a9cc1b4dca79747beb6a0b;hb=93d70984a668ae9a0a6c76c42e660978f34c3993;hpb=cf2191ef8a66ae7bba91f80de8d2e6193af44dc4 diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index 9ff99690..244db6ea 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -13,6 +13,7 @@ #include #include "gadgets.h" +#include "image.h" #include "text.h" #include "misc.h" @@ -24,8 +25,6 @@ #define DG_BUFFERED 0 #define DG_DIRECT 1 -#define GADGET_DEACTIVATED(g) ((g)->x < 0 || (g)->y < 0) - #define OPTION_TEXT_SELECTABLE(g, t) \ (t[0] != g->selectbox.char_unselectable && \ t[0] != '\0' && \ @@ -96,6 +95,16 @@ static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my, boolean check_horizontal = (IS_WHEEL_BUTTON_HORIZONTAL(button) || GetKeyModState() & KMOD_Shift); + /* check for the first active scrollbar directly under the mouse pointer */ + for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) + { + if (gi->mapped && gi->active && + (gi->type & GD_TYPE_SCROLLBAR) && + mx >= gi->x && mx < gi->x + gi->width && + my >= gi->y && my < gi->y + gi->height) + return gi; + } + /* check for the first active scrollbar with matching mouse wheel area */ for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) { @@ -210,7 +219,7 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED); boolean redraw_selectbox = FALSE; - if (gi == NULL) + if (gi == NULL || gi->deactivated) return; gd = (!gi->active ? &gi->alt_design[state] : @@ -221,15 +230,31 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) case GD_TYPE_NORMAL_BUTTON: case GD_TYPE_CHECK_BUTTON: case GD_TYPE_RADIO_BUTTON: + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, gi->width, gi->height, gi->x, gi->y); + if (gi->deco.design.bitmap) - BlitBitmap(gi->deco.design.bitmap, drawto, - gi->deco.design.x, gi->deco.design.y, - gi->deco.width, gi->deco.height, - gi->x + gi->deco.x + (pressed ? gi->deco.xshift : 0), - gi->y + gi->deco.y + (pressed ? gi->deco.yshift : 0)); + { + // make sure that decoration does not overlap gadget border + int deco_x = gi->deco.x + (pressed ? gi->deco.xshift : 0); + int deco_y = gi->deco.y + (pressed ? gi->deco.yshift : 0); + int deco_width = MIN(gi->deco.width, gi->width - deco_x); + int deco_height = MIN(gi->deco.height, gi->height - deco_y); + + if (gi->deco.masked) + BlitBitmapMasked(gi->deco.design.bitmap, drawto, + gi->deco.design.x, gi->deco.design.y, + deco_width, deco_height, + gi->x + deco_x, gi->y + deco_y); + else + BlitBitmap(gi->deco.design.bitmap, drawto, + gi->deco.design.x, gi->deco.design.y, + deco_width, deco_height, + gi->x + deco_x, gi->y + deco_y); + } + break; case GD_TYPE_TEXT_BUTTON: @@ -704,6 +729,10 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) return; } + // do not use direct gadget drawing anymore; this worked as a speed-up once, + // but would slow things down a lot now the screen is always fully redrawn + direct = FALSE; + if (direct) { BlitBitmap(drawto, window, @@ -747,13 +776,17 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) { int tag = first_tag; - if (gi == NULL) + if (gi == NULL || gi->deactivated) return; while (tag != GDI_END) { switch(tag) { + case GDI_IMAGE_ID: + gi->image_id = va_arg(ap, int); + break; + case GDI_CUSTOM_ID: gi->custom_id = va_arg(ap, int); break; @@ -971,6 +1004,10 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) gi->deco.yshift = va_arg(ap, int); break; + case GDI_DECORATION_MASKED: + gi->deco.masked = (boolean)va_arg(ap, int); + break; + case GDI_EVENT_MASK: gi->event_mask = va_arg(ap, unsigned int); break; @@ -1063,11 +1100,17 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) tag = va_arg(ap, int); /* read next tag */ } - /* check if gadget is complete */ + gi->deactivated = FALSE; + + /* check if gadget has undefined bitmaps */ if (gi->type != GD_TYPE_DRAWING_AREA && - (!gi->design[GD_BUTTON_UNPRESSED].bitmap || - !gi->design[GD_BUTTON_PRESSED].bitmap)) - Error(ERR_EXIT, "gadget incomplete (missing Bitmap)"); + (gi->design[GD_BUTTON_UNPRESSED].bitmap == NULL || + gi->design[GD_BUTTON_PRESSED].bitmap == NULL)) + gi->deactivated = TRUE; + + /* check if gadget is placed off-screen */ + if (gi->x < 0 || gi->y < 0) + gi->deactivated = TRUE; /* adjust gadget values in relation to other gadget values */ @@ -1229,7 +1272,7 @@ void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...) void RedrawGadget(struct GadgetInfo *gi) { - if (gi == NULL) + if (gi == NULL || gi->deactivated) return; if (gi->mapped) @@ -1243,6 +1286,7 @@ struct GadgetInfo *CreateGadget(int first_tag, ...) /* always start with reliable default values */ new_gadget->id = getNewGadgetID(); + new_gadget->image_id = -1; new_gadget->callback_info = default_callback_info; new_gadget->callback_action = default_callback_action; new_gadget->active = TRUE; @@ -1317,7 +1361,7 @@ static struct GadgetInfo *last_gi = NULL; static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw) { - if (gi == NULL || gi->mapped || GADGET_DEACTIVATED(gi)) + if (gi == NULL || gi->deactivated || gi->mapped) return; gi->mapped = TRUE; @@ -1333,7 +1377,7 @@ void MapGadget(struct GadgetInfo *gi) void UnmapGadget(struct GadgetInfo *gi) { - if (gi == NULL || !gi->mapped) + if (gi == NULL || gi->deactivated || !gi->mapped) return; gi->mapped = FALSE; @@ -1443,7 +1487,7 @@ static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my) void ClickOnGadget(struct GadgetInfo *gi, int button) { - if (!gi->mapped) + if (gi == NULL || gi->deactivated || !gi->mapped) return; /* simulate releasing mouse button over last gadget, if still pressed */ @@ -1567,6 +1611,8 @@ boolean HandleGadgets(int mx, int my, int button) if (gadget_pressed_inside_select_line) new_gi = NULL; + + StopTextInput(); } gadget_pressed = @@ -1641,16 +1687,20 @@ boolean HandleGadgets(int mx, int my, int button) { int last_x = gi->event.x; int last_y = gi->event.y; + int last_mx = gi->event.mx; + int last_my = gi->event.my; - gi->event.x = mx - gi->x; - gi->event.y = my - gi->y; + gi->event.x = gi->event.mx = mx - gi->x; + gi->event.y = gi->event.my = my - gi->y; if (gi->type == GD_TYPE_DRAWING_AREA) { gi->event.x /= gi->drawing.item_xsize; gi->event.y /= gi->drawing.item_ysize; - if (last_x != gi->event.x || last_y != gi->event.y) + if (last_x != gi->event.x || last_y != gi->event.y || + ((last_mx != gi->event.mx || last_my != gi->event.my) && + gi->event_mask & GD_EVENT_PIXEL_PRECISE)) changed_position = TRUE; } else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status) @@ -1668,6 +1718,9 @@ boolean HandleGadgets(int mx, int my, int button) if (gi->textinput.cursor_position != old_cursor_position) DrawGadget(gi, DG_PRESSED, gi->direct_draw); + + if (press_event) + StartTextInput(gi->x, gi->y, gi->width, gi->height); } else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status) { @@ -1682,6 +1735,9 @@ boolean HandleGadgets(int mx, int my, int button) if (gi->textarea.cursor_position != old_cursor_position) DrawGadget(gi, DG_PRESSED, gi->direct_draw); + + if (press_event) + StartTextInput(gi->x, gi->y, gi->width, gi->height); } else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open && !keep_selectbox_open) @@ -1712,7 +1768,7 @@ boolean HandleGadgets(int mx, int my, int button) new_gi->event.type = GD_EVENT_INFO_ENTERING; new_gi->callback_info(new_gi); } - else if (last_info_gi != NULL) + else if (last_info_gi != NULL && last_info_gi->mapped) { last_info_gi->event.type = GD_EVENT_INFO_LEAVING; last_info_gi->callback_info(last_info_gi); @@ -1775,7 +1831,7 @@ boolean HandleGadgets(int mx, int my, int button) { boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0); - item_steps = (scroll_single_step ? 1 : DEFAULT_WHEEL_STEPS); + item_steps = (scroll_single_step ? 1 : wheel_steps); item_direction = (button == MB_WHEEL_UP || button == MB_WHEEL_LEFT ? -1 : +1); } @@ -2038,13 +2094,17 @@ boolean HandleGadgetsKeyInput(Key key) { struct GadgetInfo *gi = last_gi; - if (gi == NULL || !gi->mapped || + if (gi == NULL || gi->deactivated || !gi->mapped || !(gi->type & GD_TYPE_TEXT_INPUT || gi->type & GD_TYPE_TEXT_AREA || gi->type & GD_TYPE_SELECTBOX)) return FALSE; - if (key == KSYM_Return) /* valid for both text input and selectbox */ + if (key == KSYM_Escape) + { + StopTextInput(); + } + else if (key == KSYM_Return) /* valid for both text input and selectbox */ { boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0); @@ -2056,6 +2116,8 @@ boolean HandleGadgetsKeyInput(Key key) strcpy(gi->textinput.last_value, gi->textinput.value); else gadget_changed = FALSE; + + StopTextInput(); } else if (gi->type & GD_TYPE_SELECTBOX) { @@ -2211,3 +2273,43 @@ boolean HandleGadgetsKeyInput(Key key) return TRUE; } + +void DumpGadgetIdentifiers() +{ + struct GadgetInfo *gi; + + Print("Gadgets on current screen:\n"); + + for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) + { + if (gi->mapped && gi->image_id != -1) + { + char *token = getTokenFromImageID(gi->image_id); + char *prefix = "gfx."; + + if (strPrefix(token, prefix)) + token = &token[strlen(prefix)]; + + Print("- '%s'\n", token); + } + } + + Print("Done.\n"); +} + +boolean DoGadgetAction(int image_id) +{ + struct GadgetInfo *gi; + + for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) + { + if (gi->mapped && gi->image_id == image_id) + { + gi->callback_action(gi); + + return TRUE; + } + } + + return FALSE; +}