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

Author Topic: how to create a "input text fields" and save it in variables  (Read 7075 times)

0 Members and 1 Guest are viewing this topic.

LGoro

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
how to create a "input text fields" and save it in variables
« on: January 09, 2017, 04:41:47 pm »
Hi everyone  ;D
I am an c++ novice and especially a beginner with sfml (more specifically I started 3 days ago) and I want to create a replica of minesweeper game. All well and good, after working these days, I did the game but now I realy need help on how to create a Text field area in game where the player can enter a "custom" game (width, height and minenumber) as the original game. I need to place three for each (int) size on game windows. i really need some help  :'(

How should i display these TextEntered on the window and after that put them in (int) variables?

Exemple: The player write '16' on width text field. That '16' is drawn on screen but also put in my width variable. The same for the height and minenumber variables.

Can someone help me ? You can do this?
-LGoro-

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: how to create a "input text fields" and save it in variables
« Reply #1 on: January 09, 2017, 05:35:48 pm »
Append the unicode char from the TextEntered event into a string. From the string you can then create an sf::Text object to render to the window and you can use a std::stringstream to convert the string to the integer value (google "string to int c++").
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LGoro

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: how to create a "input text fields" and save it in variables
« Reply #2 on: January 09, 2017, 06:54:14 pm »
Thanks very much  ;D I get the idea now, but I still have some questions if you be willing to help me..

Can I use one string and one text for all 3 ?

I can somehow write only if I selected that position?

For example if I have this:

I can not figure out how to do this  :'(
-LGoro-

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: how to create a "input text fields" and save it in variables
« Reply #3 on: January 09, 2017, 09:33:32 pm »
creating a system for handling ui is actually more complicated than one could think it is.

The easiest (although not very flexible) solution here is storing a pointer or any kind of reference (not strictly in C++ meaning) to the active text field. Handling keyboard input differently if there is a valid pointer to a text field than when there is just a nullptr.

But that feels very hacky and not right for me. If it's same with you, try looking for an established GUI library like SFGUI or Imgui that handles these things for you. Or have a look at how they handle this kind of scenarios and get inspired.
 
« Last Edit: January 10, 2017, 08:30:22 pm by sjaustirni »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: how to create a "input text fields" and save it in variables
« Reply #4 on: January 10, 2017, 01:41:40 am »
Well one doesn't have to create an sophisticated system if you just want some text input fields.

To keep it simple, I highly recommend to use multiple string objects. Then you can simply use a variable that tracks the state of which input box is active and adjust the related string.
As for the text object, you can use just one and concatenate the different strings together.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything