ultimatepp/tutorial/Gui23/main.cpp
2023-10-28 21:20:29 +02:00

68 lines
1.3 KiB
C++

#include <CtrlLib/CtrlLib.h>
using namespace Upp;
struct RandomRectCtrl : public Ctrl {
Rect rect;
Color color;
void Paint(Draw& w) override {
Size sz = GetSize();
w.DrawRect(sz, White());
if (!rect.IsEmpty()) {
w.DrawRect(rect, color);
}
}
void RandomizeRect() {
Size sz = GetSize();
int length = 50;
int x = Random() % (sz.cx - length);
int y = Random() % (sz.cy - length);
rect = Rect(x, y, x + length, y + length);
color = Color(Random() % 255, Random() % 255, Random() % 255);
}
};
#define LAYOUTFILE <Gui23/myapp.lay>
#include <CtrlCore/lay.h>
struct MyAppWindow : public WithMyAppLayout<TopWindow> {
MyAppWindow() {
CtrlLayout(*this, "MyApp");
start_stop_btn << [=] { OnStartStop(); };
}
~MyAppWindow() {
// Not needed will be automatically call upon Ctrl destruction.
// KillTimeCallback();
}
void OnStartStop() {
if (random_rect_ctrl.rect.IsEmpty()) {
start_stop_btn.SetLabel("Stop!");
SetTimeCallback(-2000, [=] { OnTimer(); });
} else {
KillTimeCallback();
start_stop_btn.SetLabel("Start!");
random_rect_ctrl.rect = {};
random_rect_ctrl.Refresh();
}
}
void OnTimer() {
random_rect_ctrl.RandomizeRect();
random_rect_ctrl.Refresh();
}
};
GUI_APP_MAIN
{
MyAppWindow().Run();
}