add missing examples

This commit is contained in:
Andrea Blankenstijn 2021-07-20 12:12:29 +02:00
parent 57a95154c2
commit e884c27ccf
4 changed files with 129 additions and 2 deletions

View file

@ -0,0 +1,19 @@
#include <iostream>
#include <basic_widgets/w/button.hpp>
#include "run.hpp"
using widget::Button;
int main()
{
run_example<Button>([](Button* w, core::Font* f, int x, int y) {
w->click_handler = [x, y](const SDL_MouseButtonEvent&) {
std::cout << "button(" << x << ',' << y << "):click!" << std::endl;
};
w->font(f);
w->text("¡jello!");
});
return 0;
}

View file

@ -0,0 +1,15 @@
#include <basic_widgets/w/caption.hpp>
#include "run.hpp"
using widget::Caption;
int main()
{
run_example<Caption>([](Caption* w, core::Font* f, int, int) {
w->alignment.h = Caption::AlignmentH::CENTER;
w->text("¡jello!");
w->font(f);
});
return 0;
}

View file

@ -6,10 +6,10 @@ using widget::NumericInput;
int main()
{
run_example<NumericInput<float> >([](NumericInput<float>* w, core::Font* f) {
run_example<NumericInput<float> >([](NumericInput<float>* w, core::Font* f, int, int) {
w->font(f);
w->button_step = 0.5;
w->value_range(-3.14, 8);
w->value_range(-3.14, 8.5);
});
return 0;
}

93
examples/run.hpp Normal file
View file

@ -0,0 +1,93 @@
#ifndef BWIDGETS_EXAMPLES_RUN_HPP_
#define BWIDGETS_EXAMPLES_RUN_HPP_
#include <concepts>
#include <functional>
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <basic_widgets/core/font.hpp>
#include <basic_widgets/core/renderer.hpp>
#include <basic_widgets/w/base/widget.hpp>
#include <basic_widgets/w/horizontal_layout.hpp>
#include <basic_widgets/w/vertical_layout.hpp>
using namespace bwidgets;
template<typename T>
concept Widget = std::derived_from<T, widget::Widget>;
template<Widget W>
void run_example(std::function<void (W*, core::Font*, int, int)> setup)
{
try
{
core::SDLError::success_or_throw(SDL_Init(SDL_INIT_VIDEO),
__FILE__, __FUNCTION__, __LINE__);
core::SDLError::success_or_throw(TTF_Init(),
__FILE__, __FUNCTION__, __LINE__);
const core::Size size_init {854, 480};
auto* font {new core::Font(core::Font::find("Monospace"), 24)};
auto* win {SDL_CreateWindow("basic_widgets example",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
size_init.w, size_init.h,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_UTILITY)};
auto* renderer {new core::Renderer(win, -1, SDL_RENDERER_ACCELERATED)};
auto* layout {new widget::HorizontalLayout()};
for (auto y = 0; y < 3; y++)
{
auto* col = new widget::VerticalLayout();
for (auto x = 0; x < 3; x++)
{
auto* widget = new W();
setup(widget, font, x, y);
col->add_widget(widget);
}
layout->add_widget(col);
}
layout->renderer(renderer);
layout->viewport({0, 0, size_init.w, size_init.h});
bool quit {false};
while (!quit)
{
SDL_Event ev;
while (SDL_PollEvent(&ev) != 0)
{
switch (ev.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_WINDOWEVENT:
if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
auto size = renderer->output_size();
layout->viewport({0, 0, size.w, size.h});
}
break;
}
layout->handle_event(ev);
}
renderer->draw_color({25, 25, 25, SDL_ALPHA_OPAQUE});
renderer->clear();
layout->render();
renderer->present();
}
}
catch (const core::SDLError& e)
{
std::cerr << "Error: " << e.file << ":" << e.func << ":" << e.line
<< ": " << e.what << std::endl;
}
}
#endif