on going work on input field rendering
This commit is contained in:
parent
079136b82c
commit
649be9a6af
78
input.hpp
78
input.hpp
|
@ -1,18 +1,66 @@
|
|||
#ifndef INPUT_HPP
|
||||
#define INPUT_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "widget.hpp"
|
||||
|
||||
template<typename T>
|
||||
concept CanToString = requires(T value) { std::to_string(value); };
|
||||
|
||||
template<typename T>
|
||||
concept Printable = requires(T value) { std::declval<std::ostream&>() << value; };
|
||||
|
||||
template<typename T> class Input : public Widget
|
||||
{
|
||||
protected:
|
||||
SDL_Rect _input_field_area;
|
||||
SDL_Rect _input_field_area = {0, 0, 0, 0};
|
||||
SDL_Rect _input_area = {0, 0, 0, 0};
|
||||
T _value;
|
||||
|
||||
Input(SDL_Renderer* r) : Widget(r) {}
|
||||
virtual void _update_drawing_areas() = 0;
|
||||
virtual void _update_drawing_areas()
|
||||
{
|
||||
_input_area = {
|
||||
_input_field_area.x + 2,
|
||||
_input_field_area.y + 2,
|
||||
_input_field_area.w - 4,
|
||||
_input_field_area.h - 4
|
||||
};
|
||||
}
|
||||
|
||||
virtual std::string _value_to_string() const
|
||||
{
|
||||
std::string s;
|
||||
|
||||
if constexpr(std::is_same_v<std::string, T> || std::convertible_to<T, std::string>)
|
||||
{
|
||||
s = _value;
|
||||
}
|
||||
else if constexpr(CanToString<T>)
|
||||
{
|
||||
s = std::to_string(_value);
|
||||
}
|
||||
else if constexpr(Printable<T>)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << _value;
|
||||
return std::move(ss).str();
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(sizeof(T) && false, "_value cannot be converted to string.");
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public:
|
||||
SDL_Color border_color = {200, 200, 200, SDL_ALPHA_OPAQUE};
|
||||
SDL_Color background_color = {255, 255, 255, SDL_ALPHA_OPAQUE};
|
||||
virtual void viewport(SDL_Rect vp)
|
||||
{
|
||||
Widget::viewport(vp);
|
||||
|
@ -28,8 +76,30 @@ public:
|
|||
SDL_RenderGetViewport(_renderer, &old_vp);
|
||||
SDL_RenderSetViewport(_renderer, &_viewport);
|
||||
|
||||
SDL_SetRenderDrawColor(_renderer, 0, 0, 255, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderDrawRect(_renderer, &_input_field_area);
|
||||
for (int i = 2; i > 0; i--)
|
||||
{
|
||||
SDL_SetRenderDrawColor(
|
||||
_renderer,
|
||||
border_color.r / i,
|
||||
border_color.g / i,
|
||||
border_color.b / i,
|
||||
border_color.a
|
||||
);
|
||||
SDL_Rect border_area = {
|
||||
_input_field_area.x + 2 - i,
|
||||
_input_field_area.y + 2 - i,
|
||||
_input_field_area.w - 2 * (2 - i),
|
||||
_input_field_area.h - 2 * (2 - i)
|
||||
};
|
||||
}
|
||||
SDL_SetRenderDrawColor(
|
||||
_renderer,
|
||||
background_color.r,
|
||||
background_color.g,
|
||||
background_color.b,
|
||||
background_color.a
|
||||
);
|
||||
SDL_RenderFillRect(_renderer, &_input_area);
|
||||
|
||||
SDL_RenderSetViewport(_renderer, &old_vp);
|
||||
}
|
||||
|
|
|
@ -61,6 +61,8 @@ private:
|
|||
(int)(1.25 * widest_button / 2) + Widget::_viewport.w - widest_button - Widget::_viewport.x,
|
||||
heighest_button
|
||||
};
|
||||
|
||||
Input<T>::_update_drawing_areas();
|
||||
}
|
||||
|
||||
public:
|
||||
|
|
|
@ -27,12 +27,12 @@ int main()
|
|||
|
||||
SDL_Rect layout_vp = {0, 0, width, height};
|
||||
Layout* layout = new Vertical_Layout(rend);
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
std::cerr << "Layout " << i << " Widget:";
|
||||
Layout* column = new Horizontal_Layout(rend);
|
||||
layout->add_widget(column);
|
||||
for (int j = 0; j < 3; j++)
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
Numeric_Input<int>* widget = new Numeric_Input<int>(rend);
|
||||
widget->font(font);
|
||||
|
|
Loading…
Reference in a new issue