Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/clipping.cpp

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-ws-lib
6 * Created on: 7 июл. 2022 г.
7 *
8 * lsp-ws-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-ws-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-ws-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/ws/factory.h>
23 #include <lsp-plug.in/ws/IEventHandler.h>
24 #include <lsp-plug.in/stdlib/math.h>
25 #include <lsp-plug.in/test-fw/mtest.h>
26
27 MTEST_BEGIN("ws.display", clipping)
28
29 class Handler: public ws::IEventHandler
30 {
31 private:
32 test_type_t *pTest;
33 ws::IWindow *pWnd;
34 bool bAntiAlias;
35
36 public:
37 inline Handler(test_type_t *test, ws::IWindow *wnd)
38 {
39 pTest = test;
40 pWnd = wnd;
41 bAntiAlias = true;
42 }
43
44 virtual status_t handle_event(const ws::event_t *ev)
45 {
46 switch (ev->nType)
47 {
48 case ws::UIE_REDRAW:
49 {
50 Color c(0.0f, 0.0f, 0.0f);
51 ws::ISurface *s = pWnd->get_surface();
52 if (s == NULL)
53 return STATUS_OK;
54
55 // Perform drawing
56 s->begin();
57 s->clear(c);
58 s->set_antialiasing(bAntiAlias);
59
60 s->clip_begin(32, 32, pWnd->width() - 64, pWnd->height() - 64);
61 c.green(0.75f);
62 s->clear(c);
63
64 float hw = pWnd->width() * 0.5f;
65 float hh = pWnd->height() * 0.5f;
66
67 s->clip_begin(64, 64, pWnd->width() - 128, pWnd->height() - 128);
68
69 // Clear with blue
70 c.set_rgb24(0x0088cc);
71 s->clear(c);
72
73 // Draw the clipped figure
74 c.set_rgb24(0xffee00);
75 constexpr const size_t N = 40;
76 float vx[N+1], vy[N+1];
77
78 for (size_t i=0; i<=N; ++i)
79 {
80 float a = (M_PI * 2.0f * i) / N;
81 float r = 1.0f - (i & 1) * 0.3f;
82
83 vx[i] = hw * (1.0f + r * cosf(a));
84 vy[i] = hh * (1.0f + r * sinf(a));
85 }
86
87 s->fill_poly(c, vx, vy, N + 1);
88
89 s->clip_end();
90
91 // Draw the multiline
92 c.set_rgb24(0x00ccff);
93
94 for (size_t i=0; i<=N; ++i)
95 {
96 float a = (M_PI * 2.0f * i) / N;
97 float r = 0.9f - (i & 1) * 0.3f;
98
99 vx[i] = hw * (1.0f + r * cosf(a));
100 vy[i] = hh * (1.0f + r * sinf(a));
101 }
102
103 s->wire_poly(c, 5, vx, vy, N + 1);
104
105 // Draw polygon with fill
106 for (size_t i=0; i<=N; ++i)
107 {
108 float a = (M_PI * 2.0f * i) / N;
109 vx[i] = hw + (i * 4.0f - N*2.0f);
110 vy[i] = hh + sinf(a) * N * 2.0f;
111 }
112
113 c.set_rgb(1.0f, 0.0f, 0.0f);
114 Color c2(c, 0.5f);
115
116 s->draw_poly(c2, c, 5.0f, vx, vy, N + 1);
117
118 s->clip_end();
119 s->end();
120
121 return STATUS_OK;
122 }
123
124 case ws::UIE_MOUSE_CLICK:
125 {
126 // Left mouse button toggles anti-aliasing
127 if (ev->nCode == ws::MCB_LEFT)
128 {
129 bAntiAlias = !bAntiAlias;
130 pTest->printf("Anti-aliasing enabled: %s\n", (bAntiAlias) ? "true" : "false");
131 pWnd->invalidate();
132 }
133 break;
134 }
135
136 case ws::UIE_CLOSE:
137 {
138 pWnd->hide();
139 pWnd->display()->quit_main();
140 break;
141 }
142
143 default:
144 return IEventHandler::handle_event(ev);
145 }
146
147 return STATUS_OK;
148 }
149 };
150
151 MTEST_MAIN
152 {
153 ws::IDisplay *dpy = ws::create_display(0, NULL);
154 MTEST_ASSERT(dpy != NULL);
155 lsp_finally { ws::free_display(dpy); };
156
157 ws::IWindow *wnd = dpy->create_window();
158 MTEST_ASSERT(wnd != NULL);
159 lsp_finally{
160 wnd->destroy();
161 delete wnd;
162 };
163
164 MTEST_ASSERT(wnd->init() == STATUS_OK);
165 MTEST_ASSERT(wnd->set_caption("Test clipping") == STATUS_OK);
166 MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
167 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
168 MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
169
170 Handler h(this, wnd);
171 wnd->set_handler(&h);
172
173 MTEST_ASSERT(wnd->show() == STATUS_OK);
174
175 MTEST_ASSERT(dpy->main() == STATUS_OK);
176 }
177
178 MTEST_END
179