]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/graphic-helpers: Add multi_rect
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Fri, 19 Aug 2022 22:01:06 +0000 (00:01 +0200)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sat, 20 Aug 2022 21:16:52 +0000 (22:16 +0100)
include/common/graphic-helpers.h [new file with mode: 0644]
src/common/graphic-helpers.c [new file with mode: 0644]
src/common/meson.build

diff --git a/include/common/graphic-helpers.h b/include/common/graphic-helpers.h
new file mode 100644 (file)
index 0000000..ab66de0
--- /dev/null
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+struct wlr_scene_tree;
+struct wlr_scene_rect;
+struct wl_listener;
+
+struct multi_rect {
+       struct wlr_scene_tree *tree;
+       int line_width; /* read-only */
+
+       /* Private */
+       struct wlr_scene_rect *top[3];
+       struct wlr_scene_rect *bottom[3];
+       struct wlr_scene_rect *left[3];
+       struct wlr_scene_rect *right[3];
+       struct wl_listener destroy;
+};
+
+/**
+ * Create a new multi_rect.
+ * A multi_rect consists of 3 nested rectangular outlines.
+ * Each of the rectangular outlines is using the same @line_width
+ * but its own color based on the @colors argument.
+ *
+ * The multi-rect can be positioned by positioning multi_rect->tree->node.
+ *
+ * It can be destroyed by destroying its tree node (or one of its
+ * parent nodes). Once the tree node has been destroyed the struct
+ * will be free'd automatically.
+ */
+struct multi_rect *multi_rect_create(struct wlr_scene_tree *parent,
+               float *colors[3], int line_width);
+
+void multi_rect_set_size(struct multi_rect *rect, int width, int height);
diff --git a/src/common/graphic-helpers.c b/src/common/graphic-helpers.c
new file mode 100644 (file)
index 0000000..0dfcd02
--- /dev/null
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <assert.h>
+#include <stdlib.h>
+#include <wlr/types/wlr_scene.h>
+#include "common/graphic-helpers.h"
+
+static void
+multi_rect_destroy_notify(struct wl_listener *listener, void *data)
+{
+       struct multi_rect *rect = wl_container_of(listener, rect, destroy);
+       free(rect);
+}
+
+struct multi_rect *
+multi_rect_create(struct wlr_scene_tree *parent, float *colors[3], int line_width)
+{
+       struct multi_rect *rect = calloc(1, sizeof(*rect));
+       rect->line_width = line_width;
+       rect->tree = wlr_scene_tree_create(parent);
+       rect->destroy.notify = multi_rect_destroy_notify;
+       wl_signal_add(&rect->tree->node.events.destroy, &rect->destroy);
+       for (size_t i = 0; i < 3; i++) {
+               rect->top[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
+               rect->right[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
+               rect->bottom[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
+               rect->left[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
+               wlr_scene_node_set_position(&rect->top[i]->node,
+                       i * line_width, i * line_width);
+               wlr_scene_node_set_position(&rect->left[i]->node,
+                       i * line_width, i * line_width);
+       }
+       return rect;
+}
+
+void
+multi_rect_set_size(struct multi_rect *rect, int width, int height)
+{
+       assert(rect);
+       int line_width = rect->line_width;
+
+       for (size_t i = 0; i < 3; i++) {
+
+               /* Reposition, top and left don't ever change */
+               wlr_scene_node_set_position(&rect->right[i]->node,
+                       width - (i + 1) * line_width, i * line_width);
+               wlr_scene_node_set_position(&rect->bottom[i]->node,
+                       i * line_width, height - (i + 1) * line_width);
+
+               /* Update sizes */
+               wlr_scene_rect_set_size(rect->top[i],
+                       width - i * line_width * 2, line_width);
+               wlr_scene_rect_set_size(rect->bottom[i],
+                       width - i * line_width * 2, line_width);
+               wlr_scene_rect_set_size(rect->left[i],
+                       line_width, height - i * line_width * 2);
+               wlr_scene_rect_set_size(rect->right[i],
+                       line_width, height - i * line_width * 2);
+       }
+}
index 3b099537ccc993d2de18b6d8f6f7d0a3f2680884..72a6d2157a5ecb644dde5147a902686d6ec0efb3 100644 (file)
@@ -4,6 +4,7 @@ labwc_sources += files(
   'fd_util.c',
   'font.c',
   'grab-file.c',
+  'graphic-helpers.c',
   'nodename.c',
   'scaled_font_buffer.c',
   'scaled_scene_buffer.c',