|
| 1 | +/* |
| 2 | + * canvas.c -- canvas, used to draw custom graphics |
| 3 | + * |
| 4 | + * Copyright (c) 2019, Liu chao <lc-soft@live.cn> All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of LCUI nor the names of its contributors may be used |
| 15 | + * to endorse or promote products derived from this software without |
| 16 | + * specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#include <stdlib.h> |
| 32 | +#include <LCUI_Build.h> |
| 33 | +#include <LCUI/LCUI.h> |
| 34 | +#include <LCUI/gui/widget.h> |
| 35 | +#include <LCUI/gui/metrics.h> |
| 36 | +#include <LCUI/gui/widget/canvas.h> |
| 37 | + |
| 38 | +typedef struct CanvasRec_ { |
| 39 | + LCUI_Graph buffer; |
| 40 | + LinkedList contexts; |
| 41 | +} CanvasRec, *Canvas; |
| 42 | + |
| 43 | +struct { |
| 44 | + LCUI_WidgetPrototype proto; |
| 45 | +} self; |
| 46 | + |
| 47 | +static void Canvas_OnResize(LCUI_Widget w, LCUI_WidgetEvent e, void *arg) |
| 48 | +{ |
| 49 | + float scale = LCUIMetrics_GetScale(); |
| 50 | + unsigned width = (unsigned)(w->box.content.width * scale); |
| 51 | + unsigned height = (unsigned)(w->box.content.height * scale); |
| 52 | + |
| 53 | + LCUI_Graph buffer; |
| 54 | + Canvas canvas = Widget_GetData(w, self.proto); |
| 55 | + |
| 56 | + Graph_Init(&buffer); |
| 57 | + buffer.color_type = LCUI_COLOR_TYPE_ARGB; |
| 58 | + Graph_Create(&buffer, width, height); |
| 59 | + Graph_Replace(&buffer, &canvas->buffer, 0, 0); |
| 60 | + Graph_Free(&canvas->buffer); |
| 61 | + canvas->buffer = buffer; |
| 62 | +} |
| 63 | + |
| 64 | +static void Canvas_OnInit(LCUI_Widget w) |
| 65 | +{ |
| 66 | + Canvas canvas = Widget_AddData(w, self.proto, sizeof(CanvasRec)); |
| 67 | + |
| 68 | + Graph_Init(&canvas->buffer); |
| 69 | + LinkedList_Init(&canvas->contexts); |
| 70 | + Widget_BindEventById(w, LCUI_WEVENT_RESIZE, Canvas_OnResize, NULL, |
| 71 | + NULL); |
| 72 | +} |
| 73 | + |
| 74 | +static void Canvas_OnDestroy(LCUI_Widget w) |
| 75 | +{ |
| 76 | + LinkedListNode *node; |
| 77 | + LCUI_CanvasContext ctx; |
| 78 | + Canvas canvas = Widget_AddData(w, self.proto, sizeof(CanvasRec)); |
| 79 | + |
| 80 | + for (LinkedList_Each(node, &canvas->contexts)) { |
| 81 | + ctx = node->data; |
| 82 | + ctx->available = FALSE; |
| 83 | + } |
| 84 | + LinkedList_ClearData(&canvas->contexts, NULL); |
| 85 | + Graph_Free(&canvas->buffer); |
| 86 | +} |
| 87 | + |
| 88 | +static void Canvas_AutoSize(LCUI_Widget w, float *width, float *height) |
| 89 | +{ |
| 90 | + *width = 300; |
| 91 | + *height = 150; |
| 92 | +} |
| 93 | + |
| 94 | +static void Canvas_OnPaint(LCUI_Widget w, LCUI_PaintContext paint, |
| 95 | + LCUI_WidgetActualStyle style) |
| 96 | +{ |
| 97 | + LCUI_Graph src, dest; |
| 98 | + LCUI_Rect content_rect, rect; |
| 99 | + Canvas canvas = Widget_GetData(w, self.proto); |
| 100 | + |
| 101 | + content_rect.width = style->content_box.width; |
| 102 | + content_rect.height = style->content_box.height; |
| 103 | + content_rect.x = style->content_box.x - style->canvas_box.x; |
| 104 | + content_rect.y = style->content_box.y - style->canvas_box.y; |
| 105 | + if (!LCUIRect_GetOverlayRect(&content_rect, &paint->rect, &rect)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + content_rect.x = rect.x - content_rect.x; |
| 109 | + content_rect.y = rect.y - content_rect.y; |
| 110 | + content_rect.width = rect.width; |
| 111 | + content_rect.height = rect.height; |
| 112 | + rect.x -= paint->rect.x; |
| 113 | + rect.y -= paint->rect.y; |
| 114 | + Graph_Quote(&dest, &paint->canvas, &rect); |
| 115 | + Graph_Quote(&src, &canvas->buffer, &content_rect); |
| 116 | + Graph_Replace(&dest, &src, 0, 0); |
| 117 | +} |
| 118 | + |
| 119 | +static void CanvasContext_ClearRect(LCUI_CanvasContext ctx, int x, int y, |
| 120 | + int width, int height) |
| 121 | +{ |
| 122 | + LCUI_Rect rect; |
| 123 | + |
| 124 | + rect.x = x; |
| 125 | + rect.y = y; |
| 126 | + rect.width = width; |
| 127 | + rect.height = height; |
| 128 | + Graph_FillRect(&ctx->buffer, ARGB(0, 0, 0, 0), &rect, TRUE); |
| 129 | +} |
| 130 | + |
| 131 | +static void CanvasContext_FillRect(LCUI_CanvasContext ctx, int x, int y, |
| 132 | + int width, int height) |
| 133 | +{ |
| 134 | + LCUI_Rect rect; |
| 135 | + |
| 136 | + rect.x = x; |
| 137 | + rect.y = y; |
| 138 | + rect.width = width; |
| 139 | + rect.height = height; |
| 140 | + Graph_FillRect(&ctx->buffer, ctx->fill_color, &rect, TRUE); |
| 141 | +} |
| 142 | + |
| 143 | +static void CanvasContext_Release(LCUI_CanvasContext ctx) |
| 144 | +{ |
| 145 | + Canvas canvas; |
| 146 | + |
| 147 | + if (ctx->available) { |
| 148 | + canvas = Widget_GetData(ctx->canvas, self.proto); |
| 149 | + LinkedList_Unlink(&canvas->contexts, &ctx->node); |
| 150 | + } |
| 151 | + free(ctx); |
| 152 | +} |
| 153 | + |
| 154 | +LCUI_CanvasContext Canvas_GetContext(LCUI_Widget w) |
| 155 | +{ |
| 156 | + ASSIGN(ctx, LCUI_CanvasRenderingContext); |
| 157 | + Canvas canvas = Widget_GetData(w, self.proto); |
| 158 | + |
| 159 | + ctx->canvas = w; |
| 160 | + ctx->available = TRUE; |
| 161 | + ctx->buffer = canvas->buffer; |
| 162 | + ctx->width = ctx->buffer.width; |
| 163 | + ctx->height = ctx->buffer.height; |
| 164 | + ctx->fill_color = RGB(0, 0, 0); |
| 165 | + ctx->scale = LCUIMetrics_GetScale(); |
| 166 | + ctx->clearRect = CanvasContext_ClearRect; |
| 167 | + ctx->fillRect = CanvasContext_FillRect; |
| 168 | + ctx->release = CanvasContext_Release; |
| 169 | + ctx->node.data = ctx; |
| 170 | + ctx->node.next = ctx->node.prev = NULL; |
| 171 | + LinkedList_AppendNode(&canvas->contexts, &ctx->node); |
| 172 | + return ctx; |
| 173 | +} |
| 174 | + |
| 175 | +void LCUIWidget_AddCanvas(void) |
| 176 | +{ |
| 177 | + self.proto = LCUIWidget_NewPrototype("canvas", NULL); |
| 178 | + self.proto->init = Canvas_OnInit; |
| 179 | + self.proto->destroy = Canvas_OnDestroy; |
| 180 | + self.proto->paint = Canvas_OnPaint; |
| 181 | + self.proto->autosize = Canvas_AutoSize; |
| 182 | +} |
0 commit comments