Main Menu

Recent posts

#51
Help requests / Custom canvas keyboard input
Last post by InvisibleShoe - 19 December 2024, 06:02:32
Hi again,
I'm trying to hookup a controller to a custom CanvasSFML widget and can't get key events to register. Mouse move and click work fine.

Header:
class GameCanvas : public tgui::CanvasSFML{
public:
    using Ptr = std::shared_ptr<GameCanvas>;

    GameCanvas(flecs::world& w);
    static GameCanvas::Ptr create(flecs::world& w);

    bool canHandleKeyPress(const tgui::Event::KeyEvent& event) override;
    void keyPressed (const tgui::Event::KeyEvent& event) override;
    bool leftMousePressed(tgui::Vector2f pos) override;
    void leftMouseReleased(tgui::Vector2f pos) override;
    void rightMousePressed(tgui::Vector2f pos) override;
    void rightMouseReleased(tgui::Vector2f pos) override;
    void mouseMoved(tgui::Vector2f pos) override;

    bool isFocused() const{
        bool f = tgui::CanvasSFML::isFocused();
        std::cout << "GameCanvas::isFocused(): " << (f ? "True" : "False") << std::endl;

        return f;
    }

    tgui::SignalTyped<tgui::Event::KeyEvent> onKeyPress = {"onKeyPress"};
protected:
    tgui::Signal& getSignal(tgui::String signalName) override;
private:
    flecs::world world;
};

src:
#include "gameCanvas.hpp"

GameCanvas::GameCanvas(flecs::world& w) : tgui::CanvasSFML(), world(w) {
    onKeyPress(
        [this](const tgui::Event::KeyEvent& event){
            Tag::InputCtrl* ctrl = world.lookup("Tag::GameScene").get_mut<Tag::InputCtrl>();
            ctrl->ctrl->onKeyEvent(event);
        }
    );
}

GameCanvas::Ptr GameCanvas::create(flecs::world& w){
    auto canvas = std::make_shared<GameCanvas>(w);
    return canvas;
}

void GameCanvas::keyPressed (const tgui::Event::KeyEvent& event){
    std::cout << "GameCanvas::keyPressed()" << std::endl;
    tgui::CanvasSFML::keyPressed(event);

    onKeyPress.emit(this, event);
}
bool GameCanvas::leftMousePressed(tgui::Vector2f pos){
    tgui::CanvasSFML::leftMousePressed(pos);

    return false;
}
void GameCanvas::leftMouseReleased(tgui::Vector2f pos){
    tgui::CanvasSFML::leftMouseReleased(pos);

    std::cout << "GameCanvas::leftMouseReleased()" << std::endl;
}
void GameCanvas::rightMousePressed(tgui::Vector2f pos){
    tgui::CanvasSFML::rightMousePressed(pos);
}
void GameCanvas::rightMouseReleased(tgui::Vector2f pos){
    tgui::CanvasSFML::rightMouseReleased(pos);
}
void GameCanvas::mouseMoved(tgui::Vector2f pos){
    tgui::CanvasSFML::mouseMoved(pos);

    Tag::InputCtrl* ctrl = world.lookup("Tag::GameScene").get_mut<Tag::InputCtrl>();
    ctrl->ctrl->onMouseMoveEvent(pos - getPosition());
}

bool GameCanvas::canHandleKeyPress(const tgui::Event::KeyEvent& event){
    std::cout << "GameCanvas::canHandleKeyPress()" << std::endl;

    return true;
}

tgui::Signal& GameCanvas::getSignal(tgui::String signalName) {
    std::cout << "GameCanvas::getSignal()" << std::endl;

    if (signalName == onKeyPress.getName())
        return onKeyPress;
    else
        return tgui::CanvasSFML::getSignal(std::move(signalName));
}


The log messages from isFocused, keyPressed, canHandleKeyPress and getSignal never get called but the messages/actions in mouseMoved and leftMouseReleased do.

The canvas hierarchy is: GameCanvas > VerticalLayout["LeftPanel"] > HorizontalLayout["HudRoot"] > RootContainer.



I've looked through the forums, the tutorial pages on signals and custom widgets, and TGUI source code, especially EditBox, but I just can't seem to figure out how to get the canvas to receive keyboard input.

If someone could point out what I'm doing wrong in the above code or provide me some basic code showing how to create a custom canvas with keyPress functionality, I'd greatly appreciate it.
Cheers 
#52
Help requests / Re: CanvasSFML rendering froze...
Last post by InvisibleShoe - 16 December 2024, 10:37:52
Did some more investigating and its a problem with the gui capturing input events before they can get to the game controller.

Going to have to think about this one.
#53
Help requests / CanvasSFML rendering frozen
Last post by InvisibleShoe - 16 December 2024, 10:07:17
Hi
I have my game screen setup as below:

    auto gui = world.get_mut<Tag::GuiRoot>();
    auto hudRoot = tgui::HorizontalLayout::create();

// LEFT WIDGETS

    auto leftPanel = tgui::VerticalLayout::create();
    leftPanel->setFocusable(false);

    auto topLeft = tgui::Panel::create();
    topLeft->getRenderer()->setBackgroundColor(sf::Color::Green);
    leftPanel->add(topLeft);

    auto canvas = tgui::CanvasSFML::create();
    leftPanel->add(canvas, "GameCanvas");
    world.set( Tag::GameCanvas{canvas} );

    leftPanel->setRatio(canvas, 15.0f);

    auto bottomLeft = tgui::Panel::create();
    bottomLeft->getRenderer()->setBackgroundColor(sf::Color::Blue);
    leftPanel->add(bottomLeft);

    hudRoot->add(leftPanel, "LeftPanel");

// RIGHT WIDGETS
    auto rightPanel = tgui::VerticalLayout::create();
    // rightPanel->setFocusable(false);

    auto topRight = tgui::Panel::create();
    topRight->getRenderer()->setBackgroundColor(sf::Color::Green);
    rightPanel->add(topRight);

    auto bottomRight = tgui::Panel::create();
    bottomRight->getRenderer()->setBackgroundColor(sf::Color::Blue);
    rightPanel->add(bottomRight);

    hudRoot->add(rightPanel, "RightPanel");
    hudRoot->setRatio(rightPanel, 0.3f);

    hudRoot->setVisible(false);

    gui->gui->add(hudRoot, "HudRoot");

The render process looks similar to:

canvas->clear(sf::Color::Black);
window->clear(sf::Color::White);

// render sprites to canvas

canvas->display();

gui->draw();

window->display()


Canvas rendering works fine on startup but as soon as I click on a gui widget(eg the right panel above) the canvas stops redrawing and won't unfreeze.
If I disable focus on the widget then the canvas stops freezing, so it seems to be an issue with widgets stealing focus from the canvas.
I've run my code in debug with breakpoints and the sprite rendering system is running in the background but the window stops responding to input and the canvas rendering stops.

Can anyone help with this? I was hoping to embed canvas widgets in a few places in a similar manner.

Cheers
#54
General Discussion / Re: showWithEffect for colaps...
Last post by texus - 21 November 2024, 20:28:01
With TreeView you could collapse things in your code when receiving a callback from onExpand.
Smooth transitions is something that isn't supported and won't be possible unless you write it yourself entirely.
#55
General Discussion / showWithEffect for colapse an...
Last post by KSergeyP - 21 November 2024, 20:24:29
I looked at the router menu today)
Its submenus smoothly collapse and expand. While one collapses, another expands.
Is there a similar functionality in TGUI? For example, for TreeView
#56
Help requests / Re: Weird error after updating...
Last post by johnnywz00 - 19 November 2024, 08:36:17
Just letting you know that I have finally solved the EditBox error, and as suspected it was a result of my class structure. In this second project there is no StateManager and the GameState was getting some init called (in which the gui high scores are set up) in the Game constructor, *before* the gui's target had been set. Transposing the order of two lines fixed the problem... what an oversight! But I'm learning.
I've now got no remaining TGUI related errors and can keep happily programming away!
#57
Help requests / Re: Weird error after updating...
Last post by johnnywz00 - 17 November 2024, 09:47:22
Ah, that's it!! Thank you, and sorry that I'm so dense. I now have one TGUI project that's error-free and one project that still gets BAD ACCESS when I create EditBoxes unless I manually construct the backend. But I expect that that is a result of how my classes are structured and I am much more inspired to figure it out especially now that I know the restructuring fixed this problem.
Thanks Texus!
#58
Help requests / Re: Weird error after updating...
Last post by johnnywz00 - 17 November 2024, 09:43:58
Ah, the StateManager is indeed declared above the Gui. I will swap that now and see...
#59
Help requests / Re: Weird error after updating...
Last post by texus - 17 November 2024, 09:41:14
Is StateManager above or below the Gui in the declaration of Game?
Could you try placing the Gui object as first member in your Game object?
#60
Help requests / Re: Weird error after updating...
Last post by johnnywz00 - 17 November 2024, 09:38:31
Yes, it prints. After main() finishes, ~Game kicks in which (in this case) calls ~StateManager which calls ~PlayingState, which appears to be calling the destructor of some vector related to an EditBox or a sharedptr of EditBox... ultimately reaching unregisterFont...
The "LetterInvadersState" which you see stores a bare pointer to the Gui object which is a member of the Game class. But LetterInvaders is the class that creates and uses the EditBoxes. Would there be a difference if I constructed all the Widgets in the Game class, and only displayed them through the LetterInvaders class?