Files
MuditaOS/module-gui/gui/widgets/Rect.cpp
Adam Dobrowolski f0659c9945 [EGD-2394] [FIX] Rect - fixed size assignment
First asign size -> than addWidget to parent
Element created should have first assigned it properties, then there
should be try to add to parent widget - othervise parent widget tries to
position elements based on default values.

This design is generally silly at most as if we fail we cant just delete
the child, we cant react properly. We could throw and hope that someone
will try to catch it - but if they do, they should be able to first
create item, than try to add it to parent in the first place.
2020-01-31 14:05:42 +01:00

135 lines
2.9 KiB
C++

/*
* Rect.cpp
*
* Created on: 6 mar 2019
* Author: robert
*/
#include "../core/BoundingBox.hpp"
#include "../core/DrawCommand.hpp"
#include "Rect.hpp"
#include <log/log.hpp>
namespace gui {
Rect::Rect() :
borderColor(Color(0,0)),
fillColor(Color(15,15)),
penWidth{1},
penFocusWidth{1},
filled{false},
edges{RectangleEdgeFlags::GUI_RECT_ALL_EDGES},
flatEdges{ RectangleFlatFlags::GUI_RECT_FLAT_NO_FLAT},
corners{RectangleCornerFlags::GUI_RECT_ALL_CORNERS} {
}
Rect::Rect(Item *parent, const uint32_t &x, const uint32_t &y, const uint32_t &w, const uint32_t &h)
: borderColor(Color(0, 0)), fillColor(Color(15, 15)), penWidth{1}, penFocusWidth{1}, filled{false}, edges{RectangleEdgeFlags::GUI_RECT_ALL_EDGES},
flatEdges{RectangleFlatFlags::GUI_RECT_FLAT_NO_FLAT}, corners{RectangleCornerFlags::GUI_RECT_ALL_CORNERS}, yaps{RectangleYapFlags::GUI_RECT_YAP_NO_YAPS}
{
widgetArea.x = 0;
widgetArea.y = 0;
widgetArea.w = 0;
widgetArea.h = 0;
setPosition(x, y);
setSize(w, h);
setMaxSize(w, h);
this->parent = parent;
if (parent)
{
if (!parent->addWidget(this))
{
LOG_FATAL("Terrible fail - widget add item failure (dangling ptr)");
}
}
}
Rect::~Rect() {
}
void Rect::setFillColor( const Color& color ) {
fillColor = color;
}
void Rect::setBorderColor( const Color& color ) {
borderColor = color;
}
void Rect::setPenWidth( uint8_t width ) {
penWidth = width;
}
void Rect::setPenFocusWidth( uint8_t width ) {
penFocusWidth = width;
}
void Rect::setEdges( RectangleEdgeFlags edges ) {
this->edges = edges;
}
void Rect::setCorners( RectangleCornerFlags corners ) {
this->corners = corners;
}
void Rect::setFlat( RectangleFlatFlags flats)
{
flatEdges = flats;
}
void Rect::setFilled( bool val ) {
filled = val;
}
void Rect::setYaps(RectangleYapFlags yaps)
{
this->yaps = yaps;
}
std::list<DrawCommand*> Rect::buildDrawList() {
std::list<DrawCommand*> commands;
//check if widget is visible
if( visible == false ) {
return commands;
}
//get children draw commands
std::list<DrawCommand*> childrenCommands = Item::buildDrawList();
//set local draw commands
CommandRectangle* rect = new CommandRectangle();
rect->x = drawArea.x;
rect->y = drawArea.y;
rect->w = drawArea.w;
rect->h = drawArea.h;
rect->areaX = widgetArea.x;
rect->areaY = widgetArea.y;
rect->areaW = widgetArea.w;
rect->areaH = widgetArea.h;
rect->corners = corners;
rect->flatEdges = this->flatEdges;
rect->edges = edges;
rect->yaps = yaps;
rect->radius = radius;
if( focus )
rect->penWidth = penFocusWidth;
else
rect->penWidth = penWidth;
rect->filled = filled;
rect->borderColor = borderColor;
rect->fillColor = fillColor;
commands.push_back( rect );
if( !childrenCommands.empty() )
commands.insert( commands.end(), childrenCommands.begin(), childrenCommands.end());
return commands;
}
} /* namespace gui */