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

Author Topic: Textbox in Window  (Read 14301 times)

0 Members and 1 Guest are viewing this topic.

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Textbox in Window
« on: March 19, 2016, 10:24:44 pm »
Hello, i am trying to create a Textbox (rectangle shape in which i can type text DIRECTLY in the window), and i keep trying to use window.getinput() yet in version 2.3 i seem to either using it wrong OR it doesn't exist... Can you please help me create this TextBox?
I already have the mouse detection (i know IF i am in the rectangle on the screen) i simply need the while () loop to create it... please i have been at it all day

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Textbox in Window
« Reply #1 on: March 19, 2016, 10:42:56 pm »
SFML 2.x doesn't have a GetInput function.

Use the TextEntered event. See the tutorial and documentation for details.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Textbox in Window
« Reply #2 on: March 19, 2016, 10:45:00 pm »
Also sounds like you're looking for something like SFGUI (?)

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textbox in Window
« Reply #3 on: March 19, 2016, 10:48:01 pm »
Thank you for clearing that up... But now that we got that out of the way can you help me create said object ? Just the keyboard input of the alphabet please ? i desperately need it

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textbox in Window
« Reply #4 on: March 19, 2016, 10:50:02 pm »
Also sounds like you're looking for something like SFGUI (?)

Pretty much yeah, i can handle about 80% of the project, but the keyboard input has taken me my entire day and it STILL doesn't work.... i am at the end of my rope here, please help me

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: Textbox in Window
« Reply #5 on: March 19, 2016, 10:52:11 pm »
Do you know how to use the TextEntered event? http://www.sfml-dev.org/tutorials/2.3/window-events.php#the-textentered-event
Do you know how to draw a rectangle? http://www.sfml-dev.org/tutorials/2.3/graphics-shape.php
Do you know how to draw a string? http://www.sfml-dev.org/tutorials/2.3/graphics-text.php

Entire day, seriously? oO

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textbox in Window
« Reply #6 on: March 19, 2016, 10:58:38 pm »
Do you know how to use the TextEntered event? http://www.sfml-dev.org/tutorials/2.3/window-events.php#the-textentered-event
Do you know how to draw a rectangle? http://www.sfml-dev.org/tutorials/2.3/graphics-shape.php
Do you know how to draw a string? http://www.sfml-dev.org/tutorials/2.3/graphics-text.php

Entire day, seriously? oO

I did the rectangle and the string that is the not the issue, i did everything needed. The only thing missing is the text entered and NO i don't know how to use it otherwise I wouldn't be here begging for help now would I

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Textbox in Window
« Reply #7 on: March 19, 2016, 11:04:22 pm »
In my event polling part i have something looking like this at the moment. It does its work nicely:

    while(this->window.pollEvent(event)) {
        switch(event.type) {
            case sf::Event::TextEntered:
                if (gui.selectedItem.type == GUI_TEXTFIELD) {
                    gui.handleTextEntered(event.text.unicode);
                }
                break;
        }
    }
 

And then in my gui class the handleTextEntered function looks a bit like this:

void GuiClass::handleTextEntered(sf::Uint32 key) {
    if (key >= 128 || key ==  27 || key == 13) return;
    if (key ==   8) {
        if (wins[selectedItem.win].textfields[selectedItem.nr].text != "")
            wins[selectedItem.win].textfields[selectedItem.nr].text.erase(wins[selectedItem.win].textfields[selectedItem.nr].text.getSize()-1, 1);
        return;
    }
    if (selectedItem.type == GUI_TEXTFIELD) {
        wins[selectedItem.win].textfields[selectedItem.nr].text += static_cast<char>(key);
    }
}
 

It's a bit crude and requires an overhaul, but it does the job.

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textbox in Window
« Reply #8 on: March 19, 2016, 11:29:38 pm »
Thank you very much, i have to modify it a little but THANK YOU SO MUCH :')

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textbox in Window
« Reply #9 on: March 20, 2016, 12:05:46 am »
n the code you sent me, could you explain to me the second part ?
you see i am using a generic code by having a Interface class. Everybody inherits it.
I have a TextBox class with :
class TextBox : public IObject
  20 {
  21   private:
  22    int                   _x;
  23    int                   _y;
  24    int                   _height;
  25    int                   _width;
  26    int                   _type;
  27    bool                  _isVisible;
  28    bool                  _isConcerned;
  29    std::string           _color;
  30    sf::RectangleShape    *_box;
  31    sf::Font              *_font;
  32    sf::Text              *_text;


i have a CreateObject() function and a DrawObject() function for each classes (objects). How do i apply your code to my class ?

Deadpool18

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textbox in Window
« Reply #10 on: March 20, 2016, 12:17:22 am »
It's a bit crude and requires an overhaul, but it does the job.

Help ?

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Textbox in Window
« Reply #11 on: March 20, 2016, 08:54:42 am »
Well...it's quite tricky really without knowing the full code and implementation.

I have a similar GUI class which stores a reference to all created objects in vector arrays.
When the mouse is moved or buttons are clicked, the vector arrays are consulted to determine whether the mouse is on top of any of the gui objects.

If so, i store a reference to the item in my GUI's hoverItem struct. On a mouseclick the hoverItem is activated and set as a reference in a selectedItem struct.

When typing, the TextEntered event passes the text to the GUI text handler which consults the selectedItem struct. If it's a textfield, that textfield's text variable is updated with the typed text.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Textbox in Window
« Reply #12 on: March 21, 2016, 01:05:14 pm »
You may also be interested in this thread.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*