Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFUI - GUI for SFML  (Read 3776 times)

0 Members and 1 Guest are viewing this topic.

ideal phi

  • Newbie
  • *
  • Posts: 6
    • View Profile
SFUI - GUI for SFML
« on: January 28, 2011, 07:42:49 pm »
What I personally like about this library is the way GUI objects are grouped.
There is the GUI window ( such as "[Main]"). Inside the GUI window, there are GUI objects. They organize them self like a tree.  When you call gui->draw("[Main]","[Button]"), the window chosen is [Main] and the GUI object in location "[Button]" and everyone below it is drawn.
Again when you call gui->draw("[Main]","[Button]:One"), the window chosen is [Main] and the GUI object in location "[Button]:One" and everyone below it is drawn. The colon (":") act as a slash ("/") to denote a virtual folder. The same applies to poll(). There are also callbacks for several scenarios (see below).




Here what's the (messy) sample code look like:


Code: [Select]


#include <SFML/Graphics.hpp>
#include "sfui/sfui.h"
#include <iostream>

void printPressedCallback(void* n)
{
bool *bScreen = (bool*)n;
if(*bScreen)
*bScreen = false;
else
*bScreen = true;

std::cout << "printPressedCallback" << std::endl;
}

void printHeldCallback(void* n)
{
std::cout << "printHeldCallback" << std::endl;
}

void printReleasedCallback(void* n)
{
std::cout << "printReleasedCallback" << std::endl;
}

void printUntouchedCallback(void* n)
{
std::cout << "printUntouchedCallback" << std::endl;
}

void printHoverEnterCallback(void* n)
{
std::cout << "printHoverEnterCallback" << std::endl;
}

void printHoveringCallback(void* n)
{
std::cout << "printHoveringCallback" << std::endl;
}

void printHoverLeaveCallback(void* n)
{
std::cout << "printHoverLeaveCallback" << std::endl;
}

void printNoHoverCallback(void* n)
{
std::cout << "printNoHoverCallback" << std::endl;
}


int main()
{

bool bScreen = false;
sf::ui::Gui *gui = new sf::ui::Gui(sf::Color(255,255,0), sf::Color(0,0,0),sf::Color(255,255,255), 13.);

sf::ui::UIButton *button =  new sf::ui::UIButton("Switch!",13.);
sf::ui::UIButton *button2 = new sf::ui::UIButton("Switch!",13.);
sf::ui::UIButton *button3 = new sf::ui::UIButton("Button 3",13.);
sf::ui::UIButton *button4 = new sf::ui::UIButton("Button 4",13.);
sf::ui::UILabel *label = new sf::ui::UILabel("Label:",13.);
sf::ui::UICheckbox *checkbox = new sf::ui::UICheckbox(true);
sf::ui::UITextField *textbox = new sf::ui::UITextField("TextField  ",18., false, 300);

button2->setX(10);
button2->setY(60);
button->setX(10);
button->setY(120);
button3->setX(10);
button3->setY(150);
button4->setX(10);
button4->setY(300);
button4->setWidth(50);
button4->setHeight(50);
textbox->setX(10);
textbox->setY(5);
label->setX(10);
label->setY(250);

checkbox->setX(10);
checkbox->setY(180);


button->setPressedCallBack (new sf::ui::UICallBackStruct(&printPressedCallback,&bScreen));
button2->setPressedCallBack (new sf::ui::UICallBackStruct(&printPressedCallback,&bScreen));
button->setHeldCallBack (new sf::ui::UICallBackStruct(&printHeldCallback));
button->setReleasedCallBack (new sf::ui::UICallBackStruct(&printReleasedCallback));
button->setUnTouchedCallBack (new sf::ui::UICallBackStruct(&printUntouchedCallback));
button->setHoverEnterCallBack (new sf::ui::UICallBackStruct(&printHoverEnterCallback));
button->setHoveringCallBack (new sf::ui::UICallBackStruct(&printHoveringCallback));
button->setHoverLeaveCallBack (new sf::ui::UICallBackStruct(&printHoverLeaveCallback));
button->setNoHoverCallBack (new sf::ui::UICallBackStruct(&printNoHoverCallback));


    // Create main window
    sf::RenderWindow App(sf::VideoMode(500, 300), "SFUI");
App.EnableKeyRepeat(true);
gui->addWindow("[Main]",&App);
gui->addObject("[Main]","[Button]:One:Two",button);
gui->addObject("[Main]","[Button]:One:Two:Three",button3);
gui->addObject("[Main]","[Button]:One:Two:Four",button4);
gui->addObject("[Main]","[Button]:One:Three",button2);
gui->addObject("[Main]","[Button]:One:Two:Label",label);
gui->addObject("[Main]","[Button]:One:Three:Checkbox",checkbox);
gui->addObject("[Main]","[Button]:One:Three:textbox",textbox);

button->setColor(sf::Color(100,200,100));
button->setTextColor(sf::Color(255,255,0));
sf::ui::UICheckbox* ch = (sf::ui::UICheckbox*)gui->getObject("[Main]","[Button]:One:Two:Checkbox");

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        App.Clear(sf::Color(170,175,180));
gui->poll("[Main]");
if(bScreen)
{
gui->process("[Main]","[Button]:One:Three");
gui->draw("[Main]","[Button]:One:Three");
}else {
gui->process("[Main]","[Button]:One:Two");
gui->draw("[Main]","[Button]:One:Two");
}

        App.Display();
    }

delete gui;

    return EXIT_SUCCESS;
}



Download Here
There is a video there too.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
SFUI - GUI for SFML
« Reply #1 on: February 05, 2011, 01:59:39 am »
Interesting library :)

About your groups: You have some kind of string to gather similar elements, right? And about the memory, don't you have to delete the allocated instances? Because I'm personally not a big fan of GC-like stuff in C++, as it makes some things quite complicated...

By the way, I wouldn't use the namespace sf, or users confuse it with official SFML. Why don't you use "sfui"? And the prefix "UI" in front of almost every class isn't necessary, either ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything