editbox in childwindow

Started by Soul, 03 May 2014, 18:26:59

Soul

hmmm i have this code, but it don't work, why?

        if (callback.id == 1)
        {
            std::string n,p;
            sf::String str;
            int s,xs,ys;
            create->get()
            str=name->getText();
            n="maps/"+str.toAnsiString();
            str=tilepath->getText();
            p="gfx/"+str.toAnsiString();
            str=tilesize->getText();
            s=atoi(str.toAnsiString().c_str());
            str=x->getText();
            xs=atoi(str.toAnsiString().c_str());
            str=y->getText();
            ys=atoi(str.toAnsiString().c_str());
            m.Create(xs,ys,s,n.c_str(),p.c_str());
        }

when i use getText() it return empty string, what's wrong?

texus

The getText function should work. Could you write a minimal but complete example code that reproduces the issue?

Btw, I really recommend using meaningful variable names, because you are making it ten times harder for someone else to understand your code by just using letters for variables.

Soul

it's strange, in my game client editbox work, but in map editor no, it's just when i use childwindow maybe?

complete source of my Menu class

menu.h  #ifndef MENU_H
#define MENU_H
#include <TGUI/TGUI.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include "../include/Map.h"
#define THEME_CONFIG_FILE "gui/Black.conf"

class Menu
{
    public:
        void LoadMenu(sf::RenderWindow &wnd);
        void Callback(Map &m);
        void Event(sf::Event &event);
        void Draw();
    protected:
    tgui::Gui gui;
    tgui::ChildWindow::Ptr Create;
    tgui::MenuBar::Ptr menu;
    tgui::Callback callback;
    tgui::ChildWindow::Ptr create;
    tgui::Button::Ptr BCreate;
    tgui::EditBox::Ptr name;
    tgui::EditBox::Ptr tilepath;
    tgui::EditBox::Ptr tilesize;
    tgui::EditBox::Ptr x;
    tgui::EditBox::Ptr y;
    sf::RenderWindow *W;
};

#endif // MENU_H


menu.cpp
#include "../include/Menu.h"
void Menu::LoadMenu(sf::RenderWindow &wnd)
{
    W=&wnd;
    gui.setWindow(wnd);
    gui.setGlobalFont("Font/djvs.ttf");
    tgui::MenuBar::Ptr m(gui);
    menu=m;
    menu->load(THEME_CONFIG_FILE);
    menu->setSize(wnd.getSize().x, 20);
    menu->addMenu("File");
    menu->addMenu("Layers");
    menu->addMenu("Tiles");
    menu->addMenu("Objects");
    menu->addMenuItem("File", "Create");
    menu->addMenuItem("File", "Load");
    menu->addMenuItem("File", "Save");
    menu->addMenuItem("File", "Exit");
    menu->bindCallback(tgui::MenuBar::MenuItemClicked);
    menu->setCallbackId(2);
    tgui::ChildWindow::Ptr c(gui);
    create=c;
    create->load(THEME_CONFIG_FILE);
    create->hide();
    create->setSize(400, 200);
    create->setBackgroundColor(sf::Color(80, 80, 80));
    create->setPosition(100, 460);
    create->setTitle("Map Creator");
    create->setIcon("../icon.jpg");
    tgui::EditBox::Ptr name(*create);
    tgui::EditBox::Ptr tilepath(*create);
    tgui::EditBox::Ptr tilesize(*create);
    tgui::EditBox::Ptr x(*create);
    tgui::EditBox::Ptr y(*create);
    name->load(THEME_CONFIG_FILE);
    name->setTextSize(10);
    name->setPosition(50, 50);
    name->setSize(100, 30);
    name->setText("Map name");
    tilepath->load(THEME_CONFIG_FILE);
    tilepath->setTextSize(10);
    tilepath->setPosition(50, 150);
    tilepath->setSize(100, 30);
    tilepath->setText("Tile path");
    tilesize->load(THEME_CONFIG_FILE);
    tilesize->setTextSize(10);
    tilesize->setPosition(50, 100);
    tilesize->setSize(100, 30);
    tilesize->setText("Tile size");
    x->load(THEME_CONFIG_FILE);
    x->setTextSize(10);
    x->setPosition(200, 50);
    x->setSize(100, 30);
    x->setText("X size");
    y->load(THEME_CONFIG_FILE);
    y->setTextSize(10);
    y->setPosition(200, 100);
    y->setSize(100, 30);
    y->setText("y size");
    tgui::Button::Ptr BCreate(*create);
    BCreate->load(THEME_CONFIG_FILE);
    BCreate->setPosition(250, 100);
    BCreate->setText("Create");
    BCreate->setCallbackId(1);
    BCreate->bindCallback(tgui::Button::LeftMouseClicked);
    BCreate->setSize(50, 40);
}

void Menu::Callback(Map &m)
{
    while (gui.pollCallback(callback))
    {
        if (callback.id == 1)
        {
            std::string Name, Path;
            sf::String str;
            int Size,xsize,ysize;
            str=name->getText();
            std::cout<<str.toAnsiString()<<std::endl; this line nothing print, same down
            Name="maps/"+str.toAnsiString();
            str=tilepath->getText();
            std::cout<<str.toAnsiString()<<std::endl;
            Path="gfx/"+str.toAnsiString();
            str=tilesize->getText();
            std::cout<<str.toAnsiString()<<std::endl;
            Size=atoi(str.toAnsiString().c_str());
            str=x->getText();
            std::cout<<str.toAnsiString()<<std::endl;
            xsize=atoi(str.toAnsiString().c_str());
            str=y->getText();
            std::cout<<str.toAnsiString()<<std::endl;
            ysize=atoi(str.toAnsiString().c_str());
            m.Create(xsize,ysize,Size,Name.c_str(),Path.c_str());
        }
        else if (callback.id == 2)
        {
            if (callback.text == "Exit")
            W->close();
            else if( callback.text == "Create")
            create->show();
            else if( callback.text == "Save")
                m.Save();
            else
            create->hide();
        }
    }
}

void Menu::Event(sf::Event &event)
{
    gui.handleEvent(event);
}

void Menu::Draw()
{
    gui.draw();
}

texus

#3
tgui::EditBox::Ptr name(*create);
That line is wrong.

What you are doing there is creating a new editbox variable, instead of using the one from the Menu class.

Replace that line (and the ones below it which have the same mistake) by the following:
create->add(name);

An alternative would be to call the constructor of the variable in the initialize list of the constructor of your Menu class, but that will just make it a lot more complicated in this case because the child window would not yet be fully initialized when initializing the edit box.

Soul

oh, I forgot about the principle footprint, thanks again for help and fast answer :), problem solved ^^