Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - starkhorn

#46
So what I am trying to do here is first setup a main menu panel where I had a background. I then want to put various widgets into the panel such as labels, radio buttons etc.

So I can create the panel without any issues.
Then the draw menu screen, I have a setupLabel function where one of the parameters to pass in is the tgui::Container::Ptr.
Now I use the gui.get to retrieve the panel but this returns type widget::ptr and so i get a compile error. I tried calling getparent but obviously this just returns gui.

Is there any to search the gui for the widget name and get the container back? Note right now it is a panel, but I also want to use setupLabel for say child windows or other container types.

Here is a full working example of what I'm trying to do.

See the drawLoadingScreenGUI() method.
It calls initMainMenuPanel() which adds a panel to gui. I now want to call setupLabel and pass in a ptr to the panel.
As you can see I'm calling gui.get("mainPanel") which does return the widget::ptr but I cannot add a label to a widget, I assume I need a container type?

Code (cpp) Select


#include <TGUI/TGUI.hpp>
#include <string>
#include <math.h>
#define THEME_CONFIG_FILE "C:\\programming\\rome_vs2013\\Debug\\widgets\\Black.txt"

using namespace std;

class Test
{
public:

Test();

sf::RenderWindow gameWindow;
sf::VideoMode desktop;
sf::Event event;
tgui::Callback callback;
tgui::Gui gui;
sf::Texture mainMenuBackGround;

tgui::Theme::Ptr theme;
tgui::Layout windowWidth;
tgui::Layout windowHeight;

void setThemeConfigFile(string passed_filename);
void initBackGroundTextures();
void initMainMenuPanel(sf::Vector2i pos, sf::Vector2i size);
void setupPanel(sf::Vector2i pos, sf::Vector2i size, sf::Color passed_backGroundColour, string passed_panelName, sf::Texture *passed_texture = NULL);
void setupLabel(sf::Vector2i pos, int passed_TextSize, sf::Color passed_colour, string labelText, tgui::Container::Ptr container = NULL);
bool drawLoadingScreenGUI();
};

Test::Test()
{
desktop = sf::VideoMode::getDesktopMode();
gameWindow.create(sf::VideoMode(desktop.width, desktop.height, desktop.bitsPerPixel), "Test");
gameWindow.setPosition(sf::Vector2i(0, 0));
gui.setWindow(gameWindow);
gui.setGlobalFont("C:\\programming\\rome_vs2013\\Debug\\inputfiles\\fonts\\DejaVuSans.ttf");
initBackGroundTextures();
setThemeConfigFile(THEME_CONFIG_FILE);
}

void Test::setThemeConfigFile(string passed_filename)
{
// Load the black theme
theme = std::make_shared<tgui::Theme>(passed_filename);

// Get a bound version of the window size
// Passing this to setPosition or setSize will make the widget automatically update when the view of the gui changes
windowWidth = tgui::bindWidth(gui);
windowHeight = tgui::bindHeight(gui);
}

void Test::initBackGroundTextures()
{
if (!mainMenuBackGround.loadFromFile("C:\\progamming\\rome_vs2013\\Debug\\images\\loading_screen.png"))
{
std::cout << "Failed to load mainMenuBackGround image!" << std::endl;
}
}

void Test::initMainMenuPanel(sf::Vector2i pos, sf::Vector2i size)
{
setupPanel(pos, size, sf::Color::Black, "mainPanel", &mainMenuBackGround);
}

void Test::setupPanel(sf::Vector2i pos, sf::Vector2i size, sf::Color passed_backGroundColour, string passed_panelName, sf::Texture *passed_texture)
{

if (gui.get(passed_panelName) == NULL)
{
auto mainMenuPanel = std::make_shared<tgui::Panel>();
mainMenuPanel->setPosition(pos.x, pos.y);
mainMenuPanel->setSize(size.x, size.y);
mainMenuPanel->setBackgroundColor(passed_backGroundColour);
if (passed_texture != NULL)
{
mainMenuPanel->add(std::make_shared<tgui::Picture>(*passed_texture));
}
gui.add(mainMenuPanel, passed_panelName);
}
}

void Test::setupLabel(sf::Vector2i pos, int passed_TextSize, sf::Color passed_colour, string labelText, tgui::Container::Ptr container)
{
if (container->get(labelText) == NULL)
{
tgui::Label::Ptr label = theme->load("Label");
container->add(label, labelText);
label->setPosition(pos.x, pos.y);
label->setTextSize(passed_TextSize);
label->setTextColor(passed_colour);
label->setText(labelText);
}
}

bool Test::drawLoadingScreenGUI()
{
sf::Vector2i pos, size((gameWindow.getSize().x), (gameWindow.getSize().y));
initMainMenuPanel(pos, size);

//Setup LoadingScreen Label
setupLabel(pos, 100, sf::Color::Red, "Roman Empire", gui.get("mainPanel"));



gameWindow.clear();
gui.draw();
gameWindow.display();

return true;
}


int main()
{
Test testClass;

    // Main loop
while (testClass.gameWindow.isOpen())
    {
        sf::Event event;
while (testClass.gameWindow.pollEvent(event))
        {
            // When the window is closed, the application ends
            if (event.type == sf::Event::Closed)
testClass.gameWindow.close();


            // Pass the event to all the widgets
testClass.gui.handleEvent(event);
        }

testClass.drawLoadingScreenGUI();

}

return 0;
}

#47
Help requests / Re: v0.7 panel background textures
05 September 2015, 01:20:29
Ah ok cool. I'll let you know how that goes.

BTW, I really really like the new way of adding widgets to the gui or a container. Also I find the new get method to be able to retrieve the widget pointer really, really useful.

EDIT:- both of the recommendations above worked a treat.
#48
Help requests / v0.7 panel background textures
05 September 2015, 00:40:36
Hey,

I was trying to set the background texture for a panel.

I tried the line below, but there doesn't appear to be a setbackgroundtexture method. I don't see any panel renderer class? Is this coming in a later version? Any recommendations to get around this currently?

Code (cpp) Select

void drawEngine::setupPanel(sf::Vector2i pos, sf::Vector2i size, sf::Color passed_backGroundColour, string passed_panelName, sf::Texture *passed_texture)
{

if (gui.get(passed_panelName) == NULL)
{
tgui::Panel::Ptr mainMenuPanel = theme->load(themeConfFile);
mainMenuPanel->setPosition(pos.x, pos.y);
mainMenuPanel->setSize(size.x, size.y);
mainMenuPanel->setBackgroundColor(passed_backGroundColour);
if (passed_texture != NULL)
{
mainMenuPanel->getRenderer()->setBackgroundTexture(passed_texture);
}
}
}
#49
Help requests / Re: v0.7 signals
04 September 2015, 22:59:18
Ok, so in the connect function, the signal "pressed" is actually not a user-definable signal. It is a signal that is part of the widget constructor,

i.e. in button.cpp I see the line below in the button constructor.

Code (cpp) Select


addSignal<sf::String>("Pressed");


For some reason I thought the first parameter of the connect function could be whatever we wanted, i.e. like the setCallbackId method.

Re-reading the class documentation of v0.7, I now see the signal section, so ok this makes more sense now.

https://tgui.eu/documentation/v0.7/classtgui_1_1Button.html

"Detailed Description

Button widget.

Signals:

    Pressed (the button was pressed)
        Optional parameter sf::String: Caption of the button
        Uses Callback member 'text'
    Inherited signals from ClickableWidget"

One question, what is difference between the button pressed and the clickableWidget clicked signal?
#50
Help requests / v0.7 signals
04 September 2015, 22:28:37
Hi,

Ok so I tried v0.7 for the first time today. I put in my v0.69 code and had zillions of compile errors as alot has changed. :)

Most are pretty easy to understand/modify, i.e. there is the renderer class and also there is no load method but we need to use the theme->load etc

However one thing that I am confused about is the change to signals.

In v0.69 there was the below in order to know when a button had been pressed with the left mouse button.

Code (cpp) Select

    button->bindCallback(tgui::Button::LeftMouseClicked);
    button->setCallbackId(1);


Now in v0.70, I get a compile error that tgui::Button::LeftMouseClicked does not exist.

I see in the v0.7 example code that it uses the connect method.

Code (cpp) Select

    // Call the login function when the button is pressed
    button->connect("pressed", login, editBoxUsername, editBoxPassword);


Ok seems simple enough but what I do not get there is how does the button widget know that this function gets triggered when the left mouse button is clicked. what if I wanted to do something if the right mouse button is clicked or if the middle button etc ?

I am sure I am missing something obvious here but I just don't quite get it. :)
#51
Help requests / Defining chatbox via a class
01 September 2015, 01:06:35
Ok, I feel a bit silly here but I'm confused and I'm looking for a bit of help.

So I have a class where one of the data items is a chatbox



class Test
{
public:
Test();

sf::RenderWindow gameWindow;
tgui::Gui gui;
tgui::ChatBox::Ptr chatbox;
};




Now in the class constructor, I want to set the chatbox container so that it is in the gui, i.e. like below.



tgui::ChatBox::Ptr chatbox(gui);


Now obviously if I do this in the class definition, it does not compile. So I'm assuming there is a method that I call call in the Test class constructor but I cannot for the life figure out which method to call.

#52
Looks awesome mate. I must get around to using the v0.7 more.....
#53
Help requests / The new renderer classes in v0.7
27 August 2015, 01:19:09
Hi Texus,

So I'm using v0.6.9 and I was looking to change the background image of a chatbox. I realized that there was no method for that but I spotted in the v0.7 docs that there is something called a chatbox renderer with such a method.

Now I am curious that is something new/different between v0.6.9 and v0.7 and I can see that lots of other widgets have such a class as well. So I was wondering how the renderer class would work/differ in v0.7?

I saw the below example code where we have to call the getRenderer() method. So just to confirm my understanding that from v0.7 onwards, all widget display methods need to go through the getRenderer() method first?

https://tgui.eu/example-code/v07/simple-console/
#54
Help requests / Accessing textures
04 July 2015, 04:45:21
In the theme conf file for child window, there is the setting for title bar where you specify the titlebar image file. I'm assuming tgui loads that in and stores it as a texture?

Now say I wanted to access that same texture again. For example say I want to sub divide the child window so I want to draw that titlebar again. Is there a way I can access that texture so I can assign it to a sf::sprite? Note I list childwindow as an example. I'd also be interested on how to access say the button png texture or radio button etc.

#55
Help requests / Re: m_callback size
01 July 2015, 01:46:46
Hi Texus,

Ok thanks again for your help and sorry it took me a while to figure this out....I'm trying to learn c++, sfml and tgui as I go. :) I really do appreciate you continued assistance and patience with my constant questions.
#56
Help requests / Re: m_callback size
30 June 2015, 05:35:22
Ok so I changed my initcallback function to below.....and had it run in the while gamewindow is open loop.  Now the m_callback size is zero whenever I click on a radiobutton.

So yeah clearly I should unbind first before rebind callbacks within the game loop. Right?

Code (cpp) Select


void Test::initRadioButtonCallbacks()
{
for (int i = 0; i < RadioButtonList.size(); i++)
{
RadioButtonList[i]->unbindAllCallback();
RadioButtonList[i]->setCallbackId(i);
RadioButtonList[i]->bindCallback(tgui::Button::LeftMouseClicked);
}
}

#57
Help requests / Re: m_callback size
30 June 2015, 03:40:14
No problem at all about being harsh and point taken that there isn't a valid use-case for it. I guess I was doing a hack for something else that I was doing incorrect.

I think you might have hit the nail on the head when you said

Code (cpp) Select

Are you sure you didn't accidentally give the other radio buttons the same id? Are you binding multiple triggers on the radio button?


I'm definitely not giving the other radio buttons the same id. I'm binding as below so you see each radio button within the radiobuttonlist vector is getting bound to the counter from the for loop.

Code (cpp) Select


void Test::initRadioButtonCallbacks()
{
for (int i = 0; i < RadioButtonList.size(); i++)
{
RadioButtonList[i]->setCallbackId(i);
RadioButtonList[i]->bindCallback(tgui::Button::LeftMouseClicked);
}
}



However, I'm calling the initRadioButtonCallbacks function each time I draw the menu, so I'm wondering if the multiple callback is what I am doing incorrectly. I did a few tests with some simple code - (see below if you wanted to copy/paste a full simple example).

When I have the initRadioButtonCallbacks outside the while gamewindow isOpen loop, then m_callback size is always zero when I press on a radiobutton. However say I put this with the while window isOpen loop (to simulate me calling it each time I draw a submenu), then the m_callback is very large.

So my question I guess becomes, does the bindCallback not overwrite the previous callbackID?

Code (cpp) Select


#include <TGUI/TGUI.hpp>
#include <string>
#define THEME_CONFIG_FILE "E:/programming/TGUI-0.6/widgets/Black.conf"

using namespace std;

class Test
{
public:

Test();

sf::RenderWindow gameWindow;
sf::VideoMode desktop;
sf::Event event;
tgui::Callback callback;
tgui::Gui gui;
vector<tgui::RadioButton::Ptr> RadioButtonList;

void initRadioButtonCallbacks();
void setupRadioButton(int passed_numWidgets, int maxNumWidgets,  sf::Color passed_colour, vector<string> stringList);
};

Test::Test()
{
desktop = sf::VideoMode::getDesktopMode();
gameWindow.create(sf::VideoMode(desktop.width * 0.5, desktop.height * 0.50, desktop.bitsPerPixel), "Test");
gameWindow.setPosition(sf::Vector2i(0, 0));
gui.setWindow(gameWindow);
}

void Test::setupRadioButton(int passed_numWidgets, int maxNumWidgets, sf::Color passed_colour, vector<string> stringList)
{
int listPreSize = RadioButtonList.size();
int listNewSize = passed_numWidgets + RadioButtonList.size();
int strListIndex = 0;
sf::Vector2i initPos(50,0);
sf::Vector2i initSize(25,25);

while (RadioButtonList.size() < listNewSize && RadioButtonList.size() < maxNumWidgets)
{
RadioButtonList.push_back(tgui::RadioButton::Ptr(gui));
}

int yposIncrementer = 0;

for (int i = listPreSize; i < RadioButtonList.size(); i++)
{
RadioButtonList[i]->load(THEME_CONFIG_FILE);
RadioButtonList[i]->setPosition(initPos.x, (initPos.y + yposIncrementer));
RadioButtonList[i]->setSize(initSize.x, initSize.y);
RadioButtonList[i]->setTextColor(passed_colour);
if (stringList.size() > 0)
{
RadioButtonList[i]->setText(stringList[strListIndex]);
}
yposIncrementer += initSize.y;
strListIndex++;
}
}

void Test::initRadioButtonCallbacks()
{
for (int i = 0; i < RadioButtonList.size(); i++)
{
RadioButtonList[i]->setCallbackId(i);
RadioButtonList[i]->bindCallback(tgui::Button::LeftMouseClicked);
}
}

int main()
{
Test testClass;
vector<string> widgetStrList;

//Do the radio buttons
widgetStrList.push_back("\tLow");
widgetStrList.push_back("\tNormal");
widgetStrList.push_back("\tHigh");

testClass.setupRadioButton(3, 3, sf::Color::Red, widgetStrList);
testClass.initRadioButtonCallbacks();

while (testClass.gameWindow.isOpen())
{

while (testClass.gameWindow.pollEvent(testClass.event))
        {
if (testClass.event.type == sf::Event::Closed)
{
testClass.gameWindow.close();
}

testClass.gui.handleEvent(testClass.event);
}



        while (testClass.gui.pollCallback(testClass.callback))
        {
if (testClass.callback.id == 0)
            {
int test = 0;
}
else if (testClass.callback.id == 1)
{
int test = 1;
}
else if (testClass.callback.id == 2)
{
int test = 2;
}
        }

testClass.gameWindow.clear(sf::Color::White);
        testClass.gui.draw();
        testClass.gameWindow.display();
}

return 0;
}







#58
Help requests / m_callback size
30 June 2015, 02:11:12
Hi Texus,

Sorry for another question......I have a few radio buttons and each radio button has it's own callback ID. Within my gui.pollCallback(callback) function, I check if the radio callback id is the one. Then I return true from the function...so basically I'm jumping out of the gui.pollCallback loop early here.

However I've realized that when i next call that pollCallback function, the radio button callback ID is still triggered. EVentually I realized that the gui.m_callback size is usually has 3-400 callback IDs...all of them the ID of the radio button. I'm not sure how/why this is the case?

Anyway, I wrote the below to clear out the m_callback queue when I no longer want to check for new events.

Code (cpp) Select

void drawEngine::clearCallBackQueue()
{
while (gui.pollCallback(callback))
{

}
}


I was wondering if there was any way to add a gui.m_callback.clear() instead as it would be a little faster? I cannot do that as m_callback is a private parameter.
#59
Help requests / Re: Different buttons
25 June 2015, 03:14:04
great thanks.
#60
ahhh - ok I see. Thank you yet again.