Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2021 James Westman <james@jwestman.net> 3 : : * 4 : : * This library is free software; you can redistribute it and/or 5 : : * modify it under the terms of the GNU Lesser General Public 6 : : * License as published by the Free Software Foundation; either 7 : : * version 2.1 of the License, or (at your option) any later version. 8 : : * 9 : : * This library is distributed in the hope that it will be useful, 10 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : : * Lesser General Public License for more details. 13 : : * 14 : : * You should have received a copy of the GNU Lesser General Public 15 : : * License along with this library; if not, see <https://www.gnu.org/licenses/>. 16 : : */ 17 : : 18 : : #include <gtk/gtk.h> 19 : : #include "shumate-vector-background-layer-private.h" 20 : : #include "shumate-vector-expression-private.h" 21 : : #include "shumate-vector-utils-private.h" 22 : : 23 : : struct _ShumateVectorBackgroundLayer 24 : : { 25 : : ShumateVectorLayer parent_instance; 26 : : 27 : : ShumateVectorExpression *color; 28 : : ShumateVectorExpression *opacity; 29 : : }; 30 : : 31 [ + + + - ]: 18 : G_DEFINE_TYPE (ShumateVectorBackgroundLayer, shumate_vector_background_layer, SHUMATE_TYPE_VECTOR_LAYER) 32 : : 33 : : 34 : : ShumateVectorLayer * 35 : 6 : shumate_vector_background_layer_create_from_json (JsonObject *object, GError **error) 36 : : { 37 : 6 : ShumateVectorBackgroundLayer *layer = g_object_new (SHUMATE_TYPE_VECTOR_BACKGROUND_LAYER, NULL); 38 : 6 : JsonNode *paint_node; 39 : : 40 [ + - ]: 6 : if ((paint_node = json_object_get_member (object, "paint"))) 41 : : { 42 : 6 : JsonObject *paint; 43 : : 44 [ + - ]: 6 : if (!shumate_vector_json_get_object (paint_node, &paint, error)) 45 : 0 : return NULL; 46 : : 47 [ + - ]: 6 : if (!(layer->color = shumate_vector_expression_from_json (json_object_get_member (paint, "background-color"), error))) 48 : : return NULL; 49 : : 50 [ + - ]: 6 : if (!(layer->opacity = shumate_vector_expression_from_json (json_object_get_member (paint, "background-opacity"), error))) 51 : : return NULL; 52 : : } 53 : : 54 : : return (ShumateVectorLayer *)layer; 55 : : } 56 : : 57 : : 58 : : static void 59 : 6 : shumate_vector_background_layer_finalize (GObject *object) 60 : : { 61 : 6 : ShumateVectorBackgroundLayer *self = SHUMATE_VECTOR_BACKGROUND_LAYER (object); 62 : : 63 [ + - ]: 6 : g_clear_object (&self->color); 64 [ + - ]: 6 : g_clear_object (&self->opacity); 65 : : 66 : 6 : G_OBJECT_CLASS (shumate_vector_background_layer_parent_class)->finalize (object); 67 : 6 : } 68 : : 69 : : 70 : : static void 71 : 3 : shumate_vector_background_layer_render (ShumateVectorLayer *layer, ShumateVectorRenderScope *scope) 72 : : { 73 : 3 : ShumateVectorBackgroundLayer *self = SHUMATE_VECTOR_BACKGROUND_LAYER (layer); 74 : 3 : GdkRGBA color = SHUMATE_VECTOR_COLOR_BLACK; 75 : 3 : double opacity; 76 : : 77 : 3 : shumate_vector_expression_eval_color (self->color, scope, &color); 78 : 3 : opacity = shumate_vector_expression_eval_number (self->opacity, scope, 1.0); 79 : : 80 : 3 : cairo_set_source_rgba (scope->cr, color.red, color.green, color.blue, color.alpha * opacity); 81 : 3 : cairo_paint (scope->cr); 82 : 3 : } 83 : : 84 : : 85 : : static void 86 : 6 : shumate_vector_background_layer_class_init (ShumateVectorBackgroundLayerClass *klass) 87 : : { 88 : 6 : GObjectClass *object_class = G_OBJECT_CLASS (klass); 89 : 6 : ShumateVectorLayerClass *layer_class = SHUMATE_VECTOR_LAYER_CLASS (klass); 90 : : 91 : 6 : object_class->finalize = shumate_vector_background_layer_finalize; 92 : 6 : layer_class->render = shumate_vector_background_layer_render; 93 : : } 94 : : 95 : : 96 : : static void 97 : 6 : shumate_vector_background_layer_init (ShumateVectorBackgroundLayer *self) 98 : : { 99 : 6 : }