/*
* Copyright (C) 2026 Linux Studio Plugins Project
* (C) 2026 Vladimir Sadovnikov
*
* This file is part of lsp-ws-lib
* Created on: 7 июл. 2022 г.
*
* lsp-ws-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* lsp-ws-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with lsp-ws-lib. If not, see .
*/
#include
#include
#include
#include
MTEST_BEGIN("ws.display", clipping)
class Handler: public ws::IEventHandler
{
private:
test_type_t *pTest;
ws::IWindow *pWnd;
bool bAntiAlias;
public:
inline Handler(test_type_t *test, ws::IWindow *wnd)
{
pTest = test;
pWnd = wnd;
bAntiAlias = true;
}
virtual status_t handle_event(const ws::event_t *ev)
{
switch (ev->nType)
{
case ws::UIE_REDRAW:
{
Color c(0.0f, 0.0f, 0.0f);
ws::ISurface *s = pWnd->get_surface();
if (s == NULL)
return STATUS_OK;
// Perform drawing
s->begin();
s->clear(c);
s->set_antialiasing(bAntiAlias);
s->clip_begin(32, 32, pWnd->width() - 64, pWnd->height() - 64);
c.green(0.75f);
s->clear(c);
float hw = pWnd->width() * 0.5f;
float hh = pWnd->height() * 0.5f;
s->clip_begin(64, 64, pWnd->width() - 128, pWnd->height() - 128);
// Clear with blue
c.set_rgb24(0x0088cc);
s->clear(c);
// Draw the clipped figure
c.set_rgb24(0xffee00);
constexpr const size_t N = 40;
float vx[N+1], vy[N+1];
for (size_t i=0; i<=N; ++i)
{
float a = (M_PI * 2.0f * i) / N;
float r = 1.0f - (i & 1) * 0.3f;
vx[i] = hw * (1.0f + r * cosf(a));
vy[i] = hh * (1.0f + r * sinf(a));
}
s->fill_poly(c, vx, vy, N + 1);
s->clip_end();
// Draw the multiline
c.set_rgb24(0x00ccff);
for (size_t i=0; i<=N; ++i)
{
float a = (M_PI * 2.0f * i) / N;
float r = 0.9f - (i & 1) * 0.3f;
vx[i] = hw * (1.0f + r * cosf(a));
vy[i] = hh * (1.0f + r * sinf(a));
}
s->wire_poly(c, 5, vx, vy, N + 1);
// Draw polygon with fill
for (size_t i=0; i<=N; ++i)
{
float a = (M_PI * 2.0f * i) / N;
vx[i] = hw + (i * 4.0f - N*2.0f);
vy[i] = hh + sinf(a) * N * 2.0f;
}
c.set_rgb(1.0f, 0.0f, 0.0f);
Color c2(c, 0.5f);
s->draw_poly(c2, c, 5.0f, vx, vy, N + 1);
s->clip_end();
s->end();
return STATUS_OK;
}
case ws::UIE_MOUSE_CLICK:
{
// Left mouse button toggles anti-aliasing
if (ev->nCode == ws::MCB_LEFT)
{
bAntiAlias = !bAntiAlias;
pTest->printf("Anti-aliasing enabled: %s\n", (bAntiAlias) ? "true" : "false");
pWnd->invalidate();
}
break;
}
case ws::UIE_CLOSE:
{
pWnd->hide();
pWnd->display()->quit_main();
break;
}
default:
return IEventHandler::handle_event(ev);
}
return STATUS_OK;
}
};
MTEST_MAIN
{
ws::IDisplay *dpy = ws::create_display(0, NULL);
MTEST_ASSERT(dpy != NULL);
lsp_finally { ws::free_display(dpy); };
ws::IWindow *wnd = dpy->create_window();
MTEST_ASSERT(wnd != NULL);
lsp_finally{
wnd->destroy();
delete wnd;
};
MTEST_ASSERT(wnd->init() == STATUS_OK);
MTEST_ASSERT(wnd->set_caption("Test clipping") == STATUS_OK);
MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
Handler h(this, wnd);
wnd->set_handler(&h);
MTEST_ASSERT(wnd->show() == STATUS_OK);
MTEST_ASSERT(dpy->main() == STATUS_OK);
}
MTEST_END