Skip to content

Commit 563d226

Browse files
committed
Add simple hello_world.
1 parent ae91ff9 commit 563d226

File tree

5 files changed

+232
-2
lines changed

5 files changed

+232
-2
lines changed

CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,25 @@ target_link_libraries(tiny_ui PRIVATE
5252

5353
if( TINY_UI_SAMPLES)
5454
ADD_EXECUTABLE(tiny_ui_sample
55-
samples/main.cpp
55+
samples/demo/main.cpp
5656
)
57-
57+
58+
ADD_EXECUTABLE(tiny_ui_hello_world
59+
samples/hello_world/main.cpp
60+
)
61+
5862
target_link_libraries(tiny_ui_sample
5963
tiny_ui
6064
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
6165
$<IF:$<TARGET_EXISTS:SDL2_image::SDL2_image>,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static>
6266
$<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>
6367
)
6468

69+
target_link_libraries(tiny_ui_hello_world
70+
tiny_ui
71+
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
72+
$<IF:$<TARGET_EXISTS:SDL2_image::SDL2_image>,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static>
73+
$<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>
74+
)
75+
6576
endif()

samples/demo/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
```cpp
2+
#include "widgets.h"
3+
#include <cmath>
4+
#include <iostream>
5+
6+
using namespace tinyui;
7+
8+
static constexpr Id RootPanelId = 1;
9+
10+
static constexpr Id NextPanelId = 100;
11+
12+
int quit(Id, void *instance) {
13+
if (instance == nullptr) {
14+
return ErrorCode;
15+
}
16+
17+
auto *ctx = static_cast<Context *>(instance);
18+
ctx->mRequestShutdown = true;
19+
20+
return ResultOk;
21+
}
22+
23+
static uint32_t LastTick = 0;
24+
static uint32_t Diff = 0;
25+
static constexpr uint32_t TimeDiff = 10;
26+
27+
int updateProgressbar(Id, void *instance) {
28+
if (instance == nullptr) {
29+
return ErrorCode;
30+
}
31+
32+
if (LastTick == 0) {
33+
LastTick = TinyUi::getTicks();
34+
return ResultOk;
35+
}
36+
37+
auto *widget = (Widget*) instance;
38+
auto *eventPayload = (EventPayload*) widget->mContent;
39+
if (eventPayload->type == EventDataType::FillState) {
40+
FilledState *state = (FilledState*) (eventPayload->payload);
41+
uint32_t tick = TinyUi::getTicks();
42+
uint32_t diff = tick - LastTick;
43+
Diff += diff;
44+
if (Diff > TimeDiff) {
45+
state->filledState++;
46+
Diff = 0;
47+
if (state->filledState > 100) {
48+
state->filledState = 0;
49+
}
50+
}
51+
LastTick = tick;
52+
}
53+
54+
return ResultOk;
55+
}
56+
57+
int main(int argc, char *argv[]) {
58+
Style style = TinyUi::getDefaultStyle();
59+
if (!TinyUi::createContext("Sample-Screen", style)) {
60+
return -1;
61+
}
62+
63+
if (TinyUi::initScreen(20, 20, 1024, 768) == -1) {
64+
auto &ctx = TinyUi::getContext();
65+
ctx.mLogger(LogSeverity::Error, "Cannot init screen");
66+
return ErrorCode;
67+
}
68+
69+
constexpr int32_t ButtonHeight = 20;
70+
Widgets::panel(RootPanelId, 0, "Sample-Dialog", Rect(90, 5, 120, 600), nullptr);
71+
Widgets::label(2, RootPanelId, "Title", Rect(100, 10, 100, 20), Alignment::Center);
72+
Widgets::button(3, RootPanelId, "Test 1", Rect(100, 50, 100, ButtonHeight), nullptr);
73+
Widgets::button(4, RootPanelId, "Test 2", Rect(100, 100, 100, ButtonHeight), nullptr);
74+
Widgets::button(5, RootPanelId, "Test 3", Rect(100, 150, 100, ButtonHeight), nullptr);
75+
Widgets::imageButton(6, RootPanelId, "button_test.png", Rect(100, 200, 100, ButtonHeight), nullptr);
76+
77+
auto &ctx = TinyUi::getContext();
78+
79+
// Allocate callbacks dynamically to ensure they persist during event handling
80+
CallbackI *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);
81+
CallbackI *dynamicUpdateProgressBarCallback = new CallbackI(updateProgressbar, nullptr, Events::UpdateEvent);
82+
83+
Widgets::button(7, RootPanelId, "Quit", Rect(100, 250, 100, ButtonHeight), dynamicQuitCallback);
84+
Widgets::progressBar(8, RootPanelId, Rect(100, 300, 100, ButtonHeight), 50, dynamicUpdateProgressBarCallback);
85+
86+
Widgets::inputText(9, RootPanelId, Rect(100, 350, 100, ButtonHeight), Alignment::Left);
87+
88+
Widgets::treeView(10, RootPanelId, "tree", Rect(100, 400, 100, ButtonHeight));
89+
Widgets::treeItem(11, 10, "Item 1");
90+
//Widgets::treeItem(12, 11, "Item 1.1");
91+
Widgets::treeItem(13, 10, "Item 2");
92+
Widgets::treeItem(14, 13, "Item 2.1");
93+
94+
while (TinyUi::run()) {
95+
TinyUi::render();
96+
}
97+
98+
TinyUi::release();
99+
100+
return 0;
101+
}
102+
```
File renamed without changes.

samples/hello_world/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
```cpp
2+
#include "widgets.h"
3+
4+
using namespace tinyui;
5+
6+
static constexpr Id RootPanelId = 1;
7+
8+
static constexpr Id NextPanelId = 100;
9+
10+
int quit(Id, void *instance) {
11+
if (instance == nullptr) {
12+
return ErrorCode;
13+
}
14+
15+
auto *ctx = static_cast<Context *>(instance);
16+
ctx->mRequestShutdown = true;
17+
18+
return ResultOk;
19+
}
20+
21+
int main(int argc, char *argv[]) {
22+
Style style = TinyUi::getDefaultStyle();
23+
if (!TinyUi::createContext("Sample-Screen", style)) {
24+
return -1;
25+
}
26+
27+
if (TinyUi::initScreen(20, 20, 1024, 768) == -1) {
28+
auto &ctx = TinyUi::getContext();
29+
ctx.mLogger(LogSeverity::Error, "Cannot init screen");
30+
return ErrorCode;
31+
}
32+
33+
Widgets::panel(RootPanelId, 0, "Sample-Dialog", Rect(90, 5, 220, 60), nullptr);
34+
auto &ctx = TinyUi::getContext();
35+
CallbackI *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);
36+
37+
constexpr int32_t ButtonHeight = 20;
38+
Widgets::label(2, RootPanelId, "Hi, World!", Rect(100, 10, 200, ButtonHeight), Alignment::Center);
39+
Widgets::button(3, RootPanelId, "Quit", Rect(100, 30, 200, ButtonHeight), dynamicQuitCallback);
40+
while (TinyUi::run()) {
41+
TinyUi::render();
42+
}
43+
44+
TinyUi::release();
45+
46+
return 0;
47+
}
48+
```

samples/hello_world/main.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2022-2026 Kim Kulling
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
#include "widgets.h"
25+
26+
using namespace tinyui;
27+
28+
static constexpr Id RootPanelId = 1;
29+
30+
static constexpr Id NextPanelId = 100;
31+
32+
int quit(Id, void *instance) {
33+
if (instance == nullptr) {
34+
return ErrorCode;
35+
}
36+
37+
auto *ctx = static_cast<Context *>(instance);
38+
ctx->mRequestShutdown = true;
39+
40+
return ResultOk;
41+
}
42+
43+
int main(int argc, char *argv[]) {
44+
Style style = TinyUi::getDefaultStyle();
45+
if (!TinyUi::createContext("Sample-Screen", style)) {
46+
return -1;
47+
}
48+
49+
if (TinyUi::initScreen(20, 20, 1024, 768) == -1) {
50+
auto &ctx = TinyUi::getContext();
51+
ctx.mLogger(LogSeverity::Error, "Cannot init screen");
52+
return ErrorCode;
53+
}
54+
55+
constexpr int32_t ButtonHeight = 20;
56+
Widgets::panel(RootPanelId, 0, "Sample-Dialog", Rect(90, 5, 220, 60), nullptr);
57+
auto &ctx = TinyUi::getContext();
58+
CallbackI *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);
59+
60+
Widgets::label(2, RootPanelId, "Hi, World!", Rect(100, 10, 200, ButtonHeight), Alignment::Center);
61+
Widgets::button(3, RootPanelId, "Quit", Rect(100, 30, 200, ButtonHeight), dynamicQuitCallback);
62+
while (TinyUi::run()) {
63+
TinyUi::render();
64+
}
65+
66+
TinyUi::release();
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)