CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
polygon.cppm
Go to the documentation of this file.
1// Copyright (c) 2024, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4module;
5#include <cairomm-1.16/cairomm/cairomm.h>
6#include <tracy/Tracy.hpp>
7
9
10import std;
11
12export import cydui.dimensions;
14
15
16export namespace vg {
17 struct polygon:
19 attrs_core<polygon>,
20 attrs_fill<polygon>,
21 attrs_stroke<polygon>,
22 attr_points<polygon> {
24 //TracyAllocN(this, sizeof(decltype(*this)), "fragment_elements");
25 }
26 ~polygon() override {
27 //TracyFreeN(this, "fragment_elements");
28 }
29 void apply_to(pixelmap_editor_t &editor) const override {
30 apply_stroke(editor);
31 apply_fill(editor);
32
33 bool first = true;
34 double odd_offset = 0.0;
35 if ((_stroke_width % 2) != 0) {
36 odd_offset = 0.5;
37 }
38 for (const auto &p: _points) {
39 if (first) {
40 editor->move_to(origin_x + p[0] + odd_offset, origin_y + p[1] + odd_offset);
41 first = false;
42 } else {
43 editor->line_to(origin_x + p[0] + odd_offset, origin_y + p[1] + odd_offset);
44 }
45 }
46 // Close the polygon
47 editor->close_path();
48
49 set_source_to_fill(editor);
50 editor->fill_preserve();
51
53 editor->stroke();
54 }
55
56 footprint get_footprint() const override {
57 if (_points.empty()) {
58 return {0, 0, 0, 0};
59 }
60
61 int min_x = _points[0][0];
62 int min_y = _points[0][1];
63 int max_x = _points[0][0];
64 int max_y = _points[0][1];
65 int x, y;
66 for (std::size_t i = 1; i < _points.size(); ++i) {
67 x = _points[i][0];
68 y = _points[i][1];
69 if (x < min_x) min_x = x;
70 if (x > max_x) max_x = x;
71 if (y < min_y) min_y = y;
72 if (y > max_y) max_y = y;
73 }
74 return {
75 min_x,
76 min_y,
77 max_x - min_x,
78 max_y - min_y,
79 };
80 }
81 };
82}
void apply_fill(pixelmap_editor_t &editor) const
void set_source_to_fill(pixelmap_editor_t &editor) const
void set_source_to_stroke(pixelmap_editor_t &editor) const
void apply_stroke(pixelmap_editor_t &editor) const
void apply_to(pixelmap_editor_t &editor) const override
Definition polygon.cppm:29
footprint get_footprint() const override
Definition polygon.cppm:56
~polygon() override
Definition polygon.cppm:26