Changeset f6df5a3 in mainline


Ignore:
Timestamp:
2020-10-13T20:06:47Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c9a7adc
Parents:
47728678
Message:

Button press visual feedback

Location:
uspace
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/uidemo/uidemo.c

    r47728678 rf6df5a3  
    3939#include <ui/pbutton.h>
    4040#include <ui/resource.h>
     41#include "uidemo.h"
    4142
    4243static void wnd_close_event(void *);
    4344static void wnd_kbd_event(void *, kbd_event_t *);
     45static void wnd_pos_event(void *, pos_event_t *);
    4446
    4547static display_wnd_cb_t wnd_cb = {
    4648        .close_event = wnd_close_event,
    47         .kbd_event = wnd_kbd_event
     49        .kbd_event = wnd_kbd_event,
     50        .pos_event = wnd_pos_event
    4851};
    4952
    5053static bool quit = false;
    5154
     55/** Print syntax. */
    5256static void print_syntax(void)
    5357{
     
    5559}
    5660
     61/** Handle window close event. */
    5762static void wnd_close_event(void *arg)
    5863{
     
    6166}
    6267
     68/** Handle window keyboard event */
    6369static void wnd_kbd_event(void *arg, kbd_event_t *event)
    6470{
     
    6672        if (event->type == KEY_PRESS)
    6773                quit = true;
     74}
     75
     76/** Handle window position event */
     77static void wnd_pos_event(void *arg, pos_event_t *event)
     78{
     79        ui_demo_t *demo = (ui_demo_t *) arg;
     80        gfx_rect_t rect1;
     81        gfx_rect_t rect2;
     82        gfx_coord2_t pos;
     83
     84        rect1.p0.x = 20;
     85        rect1.p0.y = 50;
     86        rect1.p1.x = 100;
     87        rect1.p1.y = 80;
     88
     89        rect2.p0.x = 120;
     90        rect2.p0.y = 50;
     91        rect2.p1.x = 200;
     92        rect2.p1.y = 80;
     93
     94        pos.x = event->hpos;
     95        pos.y = event->vpos;
     96
     97        if (event->type == POS_PRESS) {
     98                printf("Button press\n");
     99
     100                if (gfx_pix_inside_rect(&pos, &rect1)) {
     101                        printf("Press button 1\n");
     102                        ui_pbutton_press(demo->pb1);
     103                        (void) ui_pbutton_paint(demo->pb1);
     104                }
     105                if (gfx_pix_inside_rect(&pos, &rect2)) {
     106                        printf("Press button 2\n");
     107                        ui_pbutton_press(demo->pb2);
     108                        (void) ui_pbutton_paint(demo->pb2);
     109                }
     110        }
     111
     112        if (event->type == POS_RELEASE) {
     113                printf("Button release\n");
     114                if (gfx_pix_inside_rect(&pos, &rect1)) {
     115                        printf("Release button 1\n");
     116                        ui_pbutton_release(demo->pb1);
     117                        (void) ui_pbutton_paint(demo->pb1);
     118                }
     119                if (gfx_pix_inside_rect(&pos, &rect2)) {
     120                        printf("Release button 2\n");
     121                        ui_pbutton_release(demo->pb2);
     122                        (void) ui_pbutton_paint(demo->pb2);
     123                }
     124        }
    68125}
    69126
     
    76133        display_window_t *window = NULL;
    77134        ui_resource_t *ui_res;
    78         ui_pbutton_t *pb1;
    79         ui_pbutton_t *pb2;
     135        ui_demo_t demo;
    80136        gfx_rect_t rect;
    81137        errno_t rc;
     
    95151        params.rect.p1.y = 100;
    96152
    97         rc = display_window_create(display, &params, &wnd_cb, NULL, &window);
     153        rc = display_window_create(display, &params, &wnd_cb, (void *) &demo,
     154            &window);
    98155        if (rc != EOK) {
    99156                printf("Error creating window.\n");
     
    112169        if (rc != EOK) {
    113170                printf("Error creating UI.\n");
    114                 return 1;
    115         }
    116 
    117         rc = ui_pbutton_create(ui_res, "Confirm", &pb1);
     171                return rc;
     172        }
     173
     174        rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
    118175        if (rc != EOK) {
    119176                printf("Error creating button.\n");
    120                 return 1;
     177                return rc;
    121178        }
    122179
     
    125182        rect.p1.x = 100;
    126183        rect.p1.y = 80;
    127         ui_pbutton_set_rect(pb1, &rect);
    128 
    129         rc = ui_pbutton_create(ui_res, "Cancel", &pb2);
     184        ui_pbutton_set_rect(demo.pb1, &rect);
     185
     186        rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
    130187        if (rc != EOK) {
    131188                printf("Error creating button.\n");
    132                 return 1;
     189                return rc;
    133190        }
    134191
     
    137194        rect.p1.x = 200;
    138195        rect.p1.y = 80;
    139         ui_pbutton_set_rect(pb2, &rect);
    140 
    141         rc = ui_pbutton_paint(pb1);
     196        ui_pbutton_set_rect(demo.pb2, &rect);
     197
     198        rc = ui_pbutton_paint(demo.pb1);
    142199        if (rc != EOK) {
    143200                printf("Error painting button.\n");
    144                 return 1;
    145         }
    146 
    147         rc = ui_pbutton_paint(pb2);
     201                return rc;
     202        }
     203
     204        rc = ui_pbutton_paint(demo.pb2);
    148205        if (rc != EOK) {
    149206                printf("Error painting button.\n");
    150                 return 1;
     207                return rc;
    151208        }
    152209
     
    155212        }
    156213
    157         ui_pbutton_destroy(pb1);
    158         ui_pbutton_destroy(pb2);
     214        ui_pbutton_destroy(demo.pb1);
     215        ui_pbutton_destroy(demo.pb2);
    159216
    160217        rc = gfx_context_delete(gc);
  • uspace/lib/ui/include/ui/pbutton.h

    r47728678 rf6df5a3  
    4747extern void ui_pbutton_set_rect(ui_pbutton_t *, gfx_rect_t *);
    4848extern errno_t ui_pbutton_paint(ui_pbutton_t *);
     49extern void ui_pbutton_press(ui_pbutton_t *);
     50extern void ui_pbutton_release(ui_pbutton_t *);
    4951
    5052#endif
  • uspace/lib/ui/private/pbutton.h

    r47728678 rf6df5a3  
    4040#include <gfx/context.h>
    4141#include <gfx/coord.h>
     42#include <stdbool.h>
    4243
    4344/** Actual structure of push button.
     
    5253        /** Caption */
    5354        const char *caption;
     55        /** Button is currently held down */
     56        bool held;
    5457};
    5558
  • uspace/lib/ui/src/pbutton.c

    r47728678 rf6df5a3  
    4444#include "../private/pbutton.h"
    4545#include "../private/resource.h"
     46
     47/** Caption movement when button is pressed down */
     48enum {
     49        ui_pb_press_dx = 2,
     50        ui_pb_press_dy = 2
     51};
    4652
    4753/** Create new push button.
     
    106112        errno_t rc;
    107113
    108         rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
    109         if (rc != EOK)
    110                 goto error;
     114        if (pbutton->held) {
     115                rc = gfx_color_new_rgb_i16(0x8000, 0, 0, &color);
     116                if (rc != EOK)
     117                        goto error;
     118        } else {
     119                rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
     120                if (rc != EOK)
     121                        goto error;
     122        }
    111123
    112124        rc = gfx_set_color(pbutton->res->gc, color);
     
    132144        pos.y = (pbutton->rect.p0.y + pbutton->rect.p1.y) / 2;
    133145
     146        if (pbutton->held) {
     147                pos.x += ui_pb_press_dx;
     148                pos.y += ui_pb_press_dy;
     149        }
     150
    134151        gfx_text_fmt_init(&fmt);
    135152        fmt.halign = gfx_halign_center;
     
    149166}
    150167
     168/** Press down button.
     169 *
     170 * This does not automatically repaint the button.
     171 *
     172 * @param pbutton Push button
     173 */
     174void ui_pbutton_press(ui_pbutton_t *pbutton)
     175{
     176        pbutton->held = true;
     177}
     178
     179/** Release button.
     180 *
     181 * This does not automatically repaint the button.
     182 *
     183 * @param pbutton Push button
     184 */
     185void ui_pbutton_release(ui_pbutton_t *pbutton)
     186{
     187        pbutton->held = false;
     188}
     189
    151190/** @}
    152191 */
Note: See TracChangeset for help on using the changeset viewer.