/* source/widgets/sgl_switch.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_switch.h" static void sgl_switch_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t *evt) { sgl_switch_t *p_switch = sgl_container_of(obj, sgl_switch_t, obj); int16_t margin = p_switch->knob_margin + obj->border; int16_t radius = sgl_min(obj->radius - margin, p_switch->knob_radius); int16_t width = obj->coords.y2 - obj->coords.y1 - 2 * margin; sgl_rect_t knob_rect = { .y1 = obj->coords.y1 + margin, .y2 = obj->coords.y2 - margin, }; sgl_draw_rect_t bg_desc = { .alpha = p_switch->alpha, .border = obj->border, .color = p_switch->color, .border_color = p_switch->border_color, .pixmap = p_switch->pixmap, .radius = obj->radius, }; if(evt->type == SGL_EVENT_DRAW_MAIN) { if(p_switch->status) { bg_desc.color = p_switch->color; knob_rect.x2 = obj->coords.x2 - margin; knob_rect.x1 = knob_rect.x2 - width; } else { bg_desc.color = p_switch->bg_color; knob_rect.x1 = obj->coords.x1 + margin; knob_rect.x2 = knob_rect.x1 + width; } sgl_draw_rect(surf, &obj->area, &obj->coords, &bg_desc); sgl_draw_fill_rect(surf, &obj->area, &knob_rect, radius, p_switch->knob_color, p_switch->alpha); } else if(evt->type == SGL_EVENT_PRESSED) { p_switch->status = !p_switch->status; sgl_obj_set_dirty(obj); } } /** * @brief create a switch object * @param parent parent of the switch * @return switch object */ sgl_obj_t* sgl_switch_create(sgl_obj_t* parent) { sgl_switch_t *p_switch = sgl_malloc(sizeof(sgl_switch_t)); if(p_switch == NULL) { SGL_LOG_ERROR("sgl_switch_create: malloc failed"); return NULL; } /* set object all member to zero */ memset(p_switch, 0, sizeof(sgl_switch_t)); sgl_obj_t *obj = &p_switch->obj; sgl_obj_init(&p_switch->obj, parent); obj->construct_fn = sgl_switch_construct_cb; sgl_obj_set_border_width(obj, SGL_THEME_BORDER_WIDTH); sgl_obj_set_radius(obj, SGL_THEME_RADIUS); p_switch->alpha = SGL_THEME_ALPHA; p_switch->color = SGL_THEME_COLOR; p_switch->border_color = SGL_THEME_BORDER_COLOR; p_switch->status = false; p_switch->bg_color = SGL_THEME_BG_COLOR; p_switch->knob_color = sgl_color_mixer(SGL_THEME_COLOR, SGL_THEME_BG_COLOR, 128); p_switch->knob_radius = 255; sgl_obj_set_clickable(obj); return obj; }