I am creating a personal SFML GUI library, but I have ran into an error, and I can't find any place online to fix it. In the main file, I am creating a panel and pushing buttons onto it. All the sizing is working correctly to be on screen and are out putted in the console. I am then drawing the panel which should be drawing the buttons, but that function is not activating. Please, how do I fix this?
main
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFMin");
sfm::Button button([]() {std::cout << "Hello World" << std::endl;}, "test");
sfm::Button button2([]() {std::cout << "Hello World" << std::endl;}, "really really long name for testing purposes only");
sfm::Panel panel(sfm::Layouts::LIST);
panel.pushBackElement(button);
panel.pushBackElement(button2);
while (window.isOpen())
{
sfm::RegionCoordinates<sf::Vector2f> windowArea(sf::Vector2f(0.0f, 0.0f), sf::Vector2f(window.getSize().x, window.getSize().y));
// ----------------polling----------------
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
window.setSize(sf::Vector2u(event.size.width, event.size.height));
}
}
// ----------------update----------------
panel.update(windowArea, &window);
window.clear(sf::Color::White);
// ----------------draw----------------
panel.draw(windowArea, &window);
window.display();
}
return 0;
}
Panel
void sfm::Panel::draw(sfm::RegionCoordinates<sf::Vector2f> drawingRegion, sf::RenderWindow* window)
{
drawingRegion.correctCoordinates();
float width = drawingRegion.endCoordinate.x - drawingRegion.startCoordinate.x;
float height = drawingRegion.endCoordinate.y - drawingRegion.startCoordinate.y;
//std::cout << "Width: " << width << "Height: " << height << "\n";
float topPadding = (TOP_ELEMENT_PADDING * height) + (TOP_PANEL_PADDING * height);
float bottomPadding = (BOTTOM_ELEMENT_PADDING * height) + (BOTTOM_PANEL_PADDING * height);
float leftPadding = (LEFT_ELEMENT_PADDING * width) + (LEFT_PANEL_PADDING * width);
float rightPadding = (RIGHT_ELEMENT_PADDING * width) + (RIGHT_PANEL_PADDING * width);
float heightDivision = (height - topPadding - bottomPadding) / elements.size();
float widthDivision = (width - leftPadding - rightPadding) / elements.size();
this->elements.at(1).draw(drawingRegion, window);
/*
switch (this->layout)
{
case Layouts::LIST:
break;
case Layouts::ROW:
break;
}
*/
for (unsigned int i = 0; i < this->elements.size(); i++)
{
sf::Vector2f startCoordinate(leftPadding, heightDivision * i + topPadding);
sf::Vector2f endCoordinate(width - rightPadding, heightDivision * (i + 1) + topPadding);
std::cout << "Start " << "X: " << startCoordinate.x << " Y: " << startCoordinate.y << "\n";
std::cout << "End " << "X: " << endCoordinate.x << " Y: " << endCoordinate.y << "\n";
RegionCoordinates<sf::Vector2f> elementRegion(startCoordinate, endCoordinate);
this->elements.at(i).draw(elementRegion, window);
}
}
Button
void sfm::Button::draw(sfm::RegionCoordinates<sf::Vector2f> drawingRegion, sf::RenderWindow* window)
{
std::cout << "Hello World" << std::endl;
drawingRegion.correctCoordinates();
// ----------------background----------------
sf::RectangleShape background;
background.setPosition(drawingRegion.startCoordinate.x, drawingRegion.startCoordinate.y);
// sizing
float width = drawingRegion.endCoordinate.x - drawingRegion.startCoordinate.x;
float height = drawingRegion.endCoordinate.y - drawingRegion.startCoordinate.y;
background.setSize(sf::Vector2f(width, height));
// styling
if (hover)
{
background.setFillColor(ELEMENT_COLOR_HOVER);
}
else
{
background.setFillColor(ELEMENT_COLOR_NORMAL);
}
background.setOutlineColor(OUTLINE_COLOR);
background.setOutlineThickness(OUTLINE_THICKNESS);
window->draw(background);
// ----------------text----------------
if (this->textDescription.length() > 0)
{
sf::Text text;
text.setPosition(drawingRegion.startCoordinate.x, drawingRegion.startCoordinate.y);
text.setString(this->textDescription);
// styling
text.setFillColor(TEXT_COLOR);
text.setFont(this->font);
// sizing
sf::FloatRect rectangle = text.getLocalBounds();
float ratio = width / rectangle.width;
text.setCharacterSize(unsigned int((float)text.getCharacterSize() * ratio));
rectangle = text.getLocalBounds();
ratio = width / rectangle.width;
text.setScale(ratio, ratio);
window->draw(text);
}
// test drawing
sf::RectangleShape shape;
shape.setPosition(0, 0);
shape.setSize(sf::Vector2f(100, 100));
shape.setFillColor(sf::Color::Cyan);
window->draw(shape);
}