Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/origin.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: 28 мар. 2025 г.
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", origin)
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.5f, 0.75f);
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 // Center: circle
61 const ssize_t dw = s->width() / 4;
62 const ssize_t dh = s->height() / 4;
63
64 c.set_rgb24(0xff0000);
65 const ws::point_t origin = s->set_origin(dw * 2, dh * 2);
66 lsp_finally {
67 s->set_origin(origin);
68 };
69 s->fill_circle(c, 0.0f, 0.0f, 32);
70
71 // Left top: triangle
72 c.set_rgb24(0x00ff00);
73 s->set_origin(dw, dh);
74 {
75 s->fill_triangle(c,
76 32.0f, 0.0f,
77 cosf(2.0f * M_PI / 3.0f) * 32.0f, sinf(2.0f * M_PI / 3.0f) * 32.0f,
78 cosf(4.0f * M_PI / 3.0f) * 32.0f, sinf(4.0f * M_PI / 3.0f) * 32.0f);
79 }
80
81 // Right top: solid quad
82 c.set_rgb24(0x0000ff);
83 s->set_origin(dw * 3, dh);
84 s->fill_rect(c,
85 SURFMASK_NO_CORNER,
86 0.0f,
87 -32.0f, -32.0f,
88 64.0f, 64.0f);
89
90 // Left bottom: linear gradient triangle
91 s->set_origin(dw, dh * 3);
92 ws::IGradient *g = s->linear_gradient(-16.0f, -16.0f, 16.0f, 16.0f);
93 if (g != NULL)
94 {
95 lsp_finally {
96 delete g;
97 g = NULL;
98 };
99
100 c.set_rgb24(0x0000ff);
101 c.alpha(0.25f);
102 g->set_start(c);
103 c.set_rgb24(0xffff00);
104 c.alpha(0.0f);
105 g->set_stop(c);
106
107 s->fill_triangle(g,
108 32.0f, 0.0f,
109 cosf(2.0f * M_PI / 3.0f) * 32.0f, sinf(2.0f * M_PI / 3.0f) * 32.0f,
110 cosf(4.0f * M_PI / 3.0f) * 32.0f, sinf(4.0f * M_PI / 3.0f) * 32.0f);
111 }
112
113 // Right bottom: radial gradient quad
114 s->set_origin(dw * 3, dh * 3);
115
116 g = s->radial_gradient(-8.0f, -8.0f, -8.0f, -8.0f, 64.0f);
117 if (g != NULL)
118 {
119 lsp_finally {
120 delete g;
121 g = NULL;
122 };
123
124 c.set_rgb24(0xff00ff);
125 c.alpha(0.0f);
126 g->set_start(c);
127 c.set_rgb24(0x00ffff);
128 c.alpha(0.25f);
129 g->set_stop(c);
130
131 s->fill_rect(g,
132 SURFMASK_NO_CORNER,
133 0.0f,
134 -32.0f, -32.0f,
135 64.0f, 64.0f);
136 }
137
138 s->end();
139
140 return STATUS_OK;
141 }
142
143 case ws::UIE_MOUSE_CLICK:
144 {
145 // Left mouse button toggles anti-aliasing
146 if (ev->nCode == ws::MCB_LEFT)
147 {
148 bAntiAlias = !bAntiAlias;
149 pTest->printf("Anti-aliasing enabled: %s\n", (bAntiAlias) ? "true" : "false");
150 pWnd->invalidate();
151 }
152 break;
153 }
154
155 case ws::UIE_CLOSE:
156 {
157 pWnd->hide();
158 pWnd->display()->quit_main();
159 break;
160 }
161
162 default:
163 return IEventHandler::handle_event(ev);
164 }
165
166 return STATUS_OK;
167 }
168 };
169
170 MTEST_MAIN
171 {
172 ws::IDisplay *dpy = ws::create_display(0, NULL);
173 MTEST_ASSERT(dpy != NULL);
174 lsp_finally { ws::free_display(dpy); };
175
176 ws::IWindow *wnd = dpy->create_window();
177 MTEST_ASSERT(wnd != NULL);
178 lsp_finally {
179 wnd->destroy();
180 delete wnd;
181 };
182
183 MTEST_ASSERT(wnd->init() == STATUS_OK);
184 MTEST_ASSERT(wnd->set_caption("Test surface origin") == STATUS_OK);
185 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
186 MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
187 MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
188
189 Handler h(this, wnd);
190 wnd->set_handler(&h);
191
192 MTEST_ASSERT(wnd->show() == STATUS_OK);
193
194 MTEST_ASSERT(dpy->main() == STATUS_OK);
195 }
196
197 MTEST_END
198
199
200
201