Highlighting Keywords

  • 2 Replies
  • 67 Views
Highlighting Keywords
« on: 29 January 2020, 03:25:39 »
I'm just messing around with TGUI and getting familiar with it. I'm just making a mock chat room and I was wondering if there is a way to have multiple colors on one line?
For example:

RamblingBaba: Hello, World.

And have the username color (say Red) and the message a different color (say Green).

I'm assuming there may be a way that isn't very apparent. I was messing with the index function, but I always came back to realizing there is only addline() and I couldn't figure out a way to emulate it.

Question 2
I was having a problem with the timing of pressing enter (so it doesn't spam the message). Frame management seemed to be ignored. I managed to get it working nicely, but I came across a question? I have a button that can be used to send the message from the edit box to the chatbox. Is there a way to activate the buttons connect callback without actually doing the command "pressed". I was basically looking for like an activate(true) method or something similar. I did "Focused" which was a guessing game to find what is required (tried 'up, keyup, released, etc..'). So I would type a message, press enter (didn't understand onReturnKeyPressed since it's not a bool), focus to the send button, which automatically activates the callback lambda on the button to send the edit box text to the chatbox, clear the edit box, and focus back to the edit box.

And question 2.5 is there a way to do that process quicker or perhaps more efficient?

Thanks!

*

texus

  • *****
  • 1495
    • View Profile
    • Texus's Blog
Re: Highlighting Keywords
« Reply #1 on: 31 January 2020, 18:36:08 »
It's currently not possible to have multiple text colors on the same line. It also won't be added anywhere soon (unless someone were to contribute it).

Next time the signal system gets a rewrite I will try to implement a way to trigger signals from another widget, but it currently isn't possible. But can't you simply bind the same function for both the edit box and button?
void func()
{
  [If edit box contains something, add a line to chatbox]

  editBox-> setText("");
  editBox->setFocused(true); // In case the user clicked on the button
}

button->connect("pressed", &func);
editBox->connect("ReturnKeyPressed", &func);

Re: Highlighting Keywords
« Reply #2 on: 31 January 2020, 21:33:33 »
As for Q2.
You could easily extend standard widget that it will fits your needs. 
For example so(here I just added custom callback for onEnterPressed):

Code: [Select]
#include <TGUI/TGUI.hpp>
#include <iostream>
#include <functional>

template<typename Func>
class MyEditBox : public tgui::EditBox
{
public:
    using Ptr = std::shared_ptr<MyEditBox<Func>> ; ///< Shared widget pointer

    MyEditBox(Func f) : tgui::EditBox() , m_callback(f) {}

    static Ptr create(Func f)
    {
        return std::make_shared<MyEditBox>(f);
    }

    void keyPressed(const sf::Event::KeyEvent& event) override
    {
        if (event.code == sf::Keyboard::Enter)
        {
            m_callback();
        }
        EditBox::keyPressed(event);
    }

private:
    Func m_callback;
};

class MyFrame
{
public:
    MyFrame()
    {
        window.create(sf::VideoMode(800, 600), "TGUI window");
        gui.setTarget(window);
        //you could activate here callback for button with usual connect
        //in runtime
        //and disconnect it after
        //or use bool variable as flag
        auto onEnter = []()
        {
            std::cout << "Entered was pressed";
        };
        auto edit = MyEditBox<decltype(onEnter)>::create(onEnter);

        gui.add(edit);
    }
    void main()
    {
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
                gui.handleEvent(event);
            }
            window.clear();
            gui.draw();
            window.display();
        }
    }

private:
    sf::RenderWindow window;
    tgui::Gui gui;
};

int main()
{
    MyFrame().main();
}

Another option for anti-spam would be usual Timer that runs and made "Send" button enabled only after timeout. And handler for "pressed" signal made button disabled after sending a message.

Or, for example, all messages writes to internal buffer first and it flushed by timer.

Well there are really many ways to do it ;-)