137 lines
4.5 KiB
C
137 lines
4.5 KiB
C
/* source/widgets/sgl_led.c
|
|
*
|
|
* MIT License
|
|
*
|
|
* Copyright(c) 2023-present All contributors of SGL
|
|
* Document reference link: docs directory
|
|
*
|
|
* 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 <sgl_core.h>
|
|
#include <sgl_draw.h>
|
|
#include <sgl_math.h>
|
|
#include <sgl_log.h>
|
|
#include <sgl_mm.h>
|
|
#include <sgl_theme.h>
|
|
#include <sgl_cfgfix.h>
|
|
#include <string.h>
|
|
#include "sgl_led.h"
|
|
|
|
|
|
static void sgl_led_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t *evt)
|
|
{
|
|
sgl_led_t *led = sgl_container_of(obj, sgl_led_t, obj);
|
|
int16_t cx = 0, cy = 0;
|
|
sgl_color_t color = led->status ? led->on_color : led->off_color;
|
|
sgl_color_t *buf = NULL, *blend = NULL;
|
|
|
|
if(evt->type == SGL_EVENT_DRAW_MAIN) {
|
|
sgl_area_t clip;
|
|
|
|
cx = (led->obj.coords.x1 + led->obj.coords.x2) / 2;
|
|
cy = (led->obj.coords.y1 + led->obj.coords.y2) / 2;
|
|
|
|
if (!sgl_surf_clip(surf, &obj->area, &clip)) {
|
|
return;
|
|
}
|
|
|
|
sgl_area_t c_rect = {
|
|
.x1 = cx - obj->radius,
|
|
.x2 = cx + obj->radius,
|
|
.y1 = cy - obj->radius,
|
|
.y2 = cy + obj->radius
|
|
};
|
|
if (!sgl_area_selfclip(&clip, &c_rect)) {
|
|
return;
|
|
}
|
|
|
|
int y2 = 0, real_r2 = 0, edge_alpha = 0;
|
|
int r2 = sgl_pow2(obj->radius);
|
|
int r2_edge = sgl_pow2(obj->radius + 1);
|
|
int ds_alpha = SGL_ALPHA_MIN;
|
|
|
|
buf = sgl_surf_get_buf(surf, clip.x1 - surf->x1, clip.y1 - surf->y1);
|
|
for (int y = clip.y1; y <= clip.y2; y++) {
|
|
y2 = sgl_pow2(y - cy);
|
|
blend = buf;
|
|
|
|
for (int x = clip.x1; x <= clip.x2; x++, blend++) {
|
|
real_r2 = sgl_pow2(x - cx) + y2;
|
|
if (real_r2 >= r2_edge) {
|
|
if(x > cx)
|
|
break;
|
|
continue;
|
|
}
|
|
else if (real_r2 >= r2) {
|
|
edge_alpha = SGL_ALPHA_MAX - sgl_sqrt_error(real_r2);
|
|
sgl_color_t color_mix = sgl_color_mixer(led->bg_color, *blend, edge_alpha);
|
|
*blend = sgl_color_mixer(color_mix, *blend, led->alpha);
|
|
}
|
|
else {
|
|
ds_alpha = real_r2 * SGL_ALPHA_NUM / r2;
|
|
ds_alpha = sgl_pow2(ds_alpha) / SGL_ALPHA_NUM ;
|
|
*blend = sgl_color_mixer(sgl_color_mixer(led->bg_color, color, ds_alpha), *blend, led->alpha);//SGL_THEME_BG_COLOR
|
|
}
|
|
}
|
|
buf += surf->w;
|
|
}
|
|
}
|
|
else if (evt->type == SGL_EVENT_DRAW_INIT) {
|
|
if (obj->radius == SGL_RADIUS_INVALID) {
|
|
sgl_obj_set_radius(obj, SGL_POS_MAX);
|
|
}
|
|
}
|
|
else if (evt->type == SGL_EVENT_PRESSED || evt->type == SGL_EVENT_RELEASED) {
|
|
if (obj->event_fn) {
|
|
obj->event_fn(evt);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief create a led object
|
|
* @param parent parent of the led
|
|
* @return led object
|
|
*/
|
|
sgl_obj_t* sgl_led_create(sgl_obj_t* parent)
|
|
{
|
|
sgl_led_t *led = sgl_malloc(sizeof(sgl_led_t));
|
|
if(led == NULL) {
|
|
SGL_LOG_ERROR("sgl_led_create: malloc failed");
|
|
return NULL;
|
|
}
|
|
|
|
/* set object all member to zero */
|
|
memset(led, 0, sizeof(sgl_led_t));
|
|
|
|
sgl_obj_t *obj = &led->obj;
|
|
sgl_obj_init(&led->obj, parent);
|
|
obj->construct_fn = sgl_led_construct_cb;
|
|
|
|
obj->needinit = 1;
|
|
|
|
led->alpha = SGL_ALPHA_MAX;
|
|
led->on_color = SGL_THEME_COLOR;
|
|
led->off_color = SGL_THEME_BG_COLOR;
|
|
led->bg_color = SGL_THEME_BG_COLOR;
|
|
obj->radius = SGL_RADIUS_INVALID;
|
|
|
|
return obj;
|
|
}
|