2021-07-20 12:12:29 +02:00
|
|
|
#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>
|
2021-07-20 15:09:25 +02:00
|
|
|
void run_example(std::function<void (W*, core::Font*, int, int)> setup, int w=3, int h=3)
|
2021-07-20 12:12:29 +02:00
|
|
|
{
|
|
|
|
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};
|
2021-07-24 22:07:28 +02:00
|
|
|
auto* font {new core::Font(core::Font::find("Monospace"), 16)};
|
2021-07-20 12:12:29 +02:00
|
|
|
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)};
|
2021-07-26 22:57:28 +02:00
|
|
|
renderer->blend_mode(SDL_BLENDMODE_BLEND);
|
2021-07-20 12:12:29 +02:00
|
|
|
|
|
|
|
auto* layout {new widget::HorizontalLayout()};
|
|
|
|
|
2021-07-20 15:09:25 +02:00
|
|
|
for (auto x = 0; x < w; x++)
|
2021-07-20 12:12:29 +02:00
|
|
|
{
|
|
|
|
auto* col = new widget::VerticalLayout();
|
2021-07-20 15:09:25 +02:00
|
|
|
for (auto y = 0; y < h; y++)
|
2021-07-20 12:12:29 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-07-24 22:07:28 +02:00
|
|
|
renderer->draw_color({50, 60, 70, SDL_ALPHA_OPAQUE});
|
2021-07-20 12:12:29 +02:00
|
|
|
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
|