/* source/widgets/sgl_button.c * * MIT License * * Copyright(c) 2023-present All contributors of SGL * Document reference link: https://sgl-docs.readthedocs.io * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include #include #include #include #include #include "sgl_button.h" /** * @brief button construct callback * @param surf: surface pointer * @param obj: object pointer * @param evt: event pointer * @return none * @note this function is called when the object is created or redraw */ static void sgl_button_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t *evt) { sgl_button_t *button = sgl_container_of(obj, sgl_button_t, obj); sgl_pos_t align_pos; sgl_rect_t fill_area; sgl_draw_rect_t rect = { .alpha = button->alpha, .color = button->color, .border = obj->border, .border_color = button->border_color, .pixmap = button->pixmap, .radius = obj->radius, }; if(evt->type == SGL_EVENT_DRAW_MAIN) { sgl_draw_rect(surf, &obj->area, &obj->coords, &rect); if(button->text) { SGL_ASSERT(button->font != NULL); fill_area = sgl_obj_get_fill_rect(obj); align_pos = sgl_get_text_pos(&fill_area, button->font, button->text, 0, (sgl_align_type_t)button->align); sgl_draw_string(surf, &obj->area, align_pos.x, align_pos.y, button->text, button->text_color, button->alpha, button->font); } } else if(evt->type == SGL_EVENT_PRESSED) { if(sgl_obj_is_flexible(obj)) { sgl_obj_size_zoom(obj, 2); } sgl_obj_set_dirty(obj); } else if(evt->type == SGL_EVENT_RELEASED) { if(sgl_obj_is_flexible(obj)) { sgl_obj_size_zoom(obj, -2); } sgl_obj_set_dirty(obj); } } /** * @brief create a button object * @param parent: parent object * @return object pointer * @note if parent is NULL, the button object will be created at the top level. */ sgl_obj_t* sgl_button_create(sgl_obj_t* parent) { sgl_button_t *button = sgl_malloc(sizeof(sgl_button_t)); if(button == NULL) { SGL_LOG_ERROR("sgl_button_create: malloc failed"); return NULL; } /* set object all member to zero */ memset(button, 0, sizeof(sgl_button_t)); sgl_obj_t *obj = &button->obj; sgl_obj_init(&button->obj, parent); sgl_obj_set_clickable(obj); sgl_obj_set_flexible(obj); sgl_obj_set_border_width(obj, SGL_THEME_BORDER_WIDTH); sgl_obj_set_radius(obj, SGL_THEME_RADIUS); obj->construct_fn = sgl_button_construct_cb; button->alpha = SGL_THEME_ALPHA; button->color = SGL_THEME_COLOR; button->border_color = SGL_THEME_BORDER_COLOR; button->pixmap = NULL; button->text = " "; button->text_color = SGL_THEME_TEXT_COLOR; button->font = sgl_get_system_font(); button->align = SGL_ALIGN_CENTER; return obj; }