add: Simple widget system

This commit is contained in:
Simon Hardt
2022-03-06 03:04:32 +01:00
parent e1e410561c
commit 243bf41e90
14 changed files with 430 additions and 8 deletions

View File

@@ -7,15 +7,20 @@ namespace frame::render
RenderTarget::RenderTarget(Size size)
: image(size)
, root_size{0, 0, (int)size.width, (int)size.height}
{
}
void RenderTarget::DrawPixel(Vector const& position, Color color)
{
if(position.x < 0 || position.y < 0 || position.x >= image.getWidth()
|| position.y >= image.getHeight())
auto current = getCurrentSize();
if(position.x < 0 || position.y < 0 || position.x >= current.width
|| position.y >= current.height
|| (position.x + current.left) >= root_size.width
|| (position.y + current.top) >= root_size.height)
return;
image.at(position.x, position.y) = color;
image.at(position.x + current.left, position.y + current.top) = color;
}
void RenderTarget::DrawLine(Vector const& start,
@@ -77,12 +82,16 @@ namespace frame::render
void RenderTarget::DrawCircle(Vector const& center,
int32_t radius,
int32_t line,
Color color)
{
int32_t const r_2 = radius * radius;
Vector pos{0, -radius};
DrawPointsMirrorCircle(center, pos, color);
for(auto i = 0; i < line; ++i)
{
DrawPointsMirrorCircle(center, {pos.x, pos.y + i}, color);
}
while(pos.x <= radius / std::sqrt(2.f))
{
@@ -102,7 +111,10 @@ namespace frame::render
E += 2 * pos.x - 2 * pos.y + 5;
}
DrawPointsMirrorCircle(center, pos, color);
for(auto i = 0; i < line; ++i)
{
DrawPointsMirrorCircle(center, {pos.x, pos.y + i}, color);
}
}
}
@@ -199,7 +211,7 @@ namespace frame::render
switch(alignVertical)
{
case AlignVertical::TOP: start.y += lineSpacing_3 * 2; break;
case AlignVertical::TOP: start.y -= y_offset; break;
case AlignVertical::CENTER:
start.y += rect.height / 2;
start.y += text_height / 2;
@@ -207,7 +219,7 @@ namespace frame::render
break;
case AlignVertical::BOTTOM:
start.y += rect.height;
start.y += text_height + y_offset;
start.y -= text_height + y_offset;
break;
}
@@ -249,6 +261,30 @@ namespace frame::render
image.Clear(color);
}
Rect RenderTarget::getCurrentSize() const
{
if(Scissor.size() == 0)
{
return root_size;
}
return Scissor.top();
}
void RenderTarget::pushViewport(Rect rect)
{
auto current = getCurrentSize();
Scissor.push({current.top + rect.top,
current.left + rect.left,
rect.width,
rect.height});
}
void RenderTarget::popViewport()
{
Scissor.pop();
}
void RenderTarget::DrawPointsMirrorCircle(Vector const& center,
Vector const& pos,
Color color)