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

Author Topic: [SOLVED]Insert and remove text not working with Events  (Read 4920 times)

0 Members and 1 Guest are viewing this topic.

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
[SOLVED]Insert and remove text not working with Events
« on: February 10, 2014, 03:13:15 pm »
Hello everyone.
I'm trying to catch inputs from keyboard and save it to a string and also to remove the last char with backspace.
I can't get it working, i tried many possible combinations but no one worked.
In my aktual try i filter the backspace out on TextEntered event but then it don't consider my action with backspace.
while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if(event.type == sf::Event::TextEntered)
            {
                if(event.text.unicode != 0x000008)
                {
                   insert_last_char(text, event.text.unicode);
                   showing_text.setString(text);
                }
            }
            else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::BackSpace)
            {
                remove_last_char(text);
                showing_text.setString(text);
            }
        }
« Last Edit: February 10, 2014, 11:47:36 pm by canlot »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9909
    • View Profile
    • development blog
    • Email
Re: Insert and remove text not working with Events
« Reply #1 on: February 10, 2014, 04:25:19 pm »
So what's the question/problem?
What do you expect to happen? What happens? Where exactly does it fail?
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Insert and remove text not working with Events
« Reply #2 on: February 10, 2014, 05:12:27 pm »
How i can input and remove chars correctly?
This way i do it isn't working.
It accept char input but not going to third else if case, because it don't remove the char, don't execute remove_last_char(text); i think it is blocked by the if(event.text.unicode != 0x000008) statment but why.
Other ways isn't working also.

Hapax

  • Hero Member
  • *****
  • Posts: 3043
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Insert and remove text not working with Events
« Reply #3 on: February 10, 2014, 07:10:59 pm »
Wouldn't this make more sense:
else if(event.type == sf::Event::TextEntered)
            {
                if(event.text.unicode != 0x000008)
                {
                   insert_last_char(text, event.text.unicode);
                   showing_text.setString(text);
                }
                else
                {
                    remove_last_char(text);
                    showing_text.setString(text);
                }
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Insert and remove text not working with Events
« Reply #4 on: February 10, 2014, 07:21:05 pm »
As i wrote i tried some cases, this one isn't work either.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Insert and remove text not working with Events
« Reply #5 on: February 10, 2014, 07:27:27 pm »
You should seriously just use a debugger and step through this. Coming here and saying "it doesn't work" doesn't give us any information. I am sure if you used a debugger you could very easily figure out why it isn't working properly.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3043
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Insert and remove text not working with Events
« Reply #6 on: February 10, 2014, 07:29:52 pm »
Try this for your hex for backspace:
0x00000008
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Insert and remove text not working with Events
« Reply #7 on: February 10, 2014, 09:32:06 pm »
You should seriously just use a debugger and step through this. Coming here and saying "it doesn't work" doesn't give us any information. I am sure if you used a debugger you could very easily figure out why it isn't working properly.
and how it would be helping me?
my functions are ok i tested it, i think i handle the event wrong and thought you could help me.

Try this for your hex for backspace:
0x00000008
same thing

Hapax

  • Hero Member
  • *****
  • Posts: 3043
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Insert and remove text not working with Events
« Reply #8 on: February 10, 2014, 10:32:49 pm »
Does it work the other way around:
if(event.text.unicode == 0x000008)
                {
                   remove_last_char(text);
                   showing_text.setString(text);
                }
                else
                {
                    insert_last_char(text, event.text.unicode);
                    showing_text.setString(text);
                }

Same thing, or a different result?
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Insert and remove text not working with Events
« Reply #9 on: February 10, 2014, 11:00:21 pm »
Does it work the other way around:
if(event.text.unicode == 0x000008)
                {
                   remove_last_char(text);
                   showing_text.setString(text);
                }
                else
                {
                    insert_last_char(text, event.text.unicode);
                    showing_text.setString(text);
                }

Same thing, or a different result?
tried this before, most combinations unfortunately the same result: inputs works but can't remove.

Hapax

  • Hero Member
  • *****
  • Posts: 3043
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Insert and remove text not working with Events
« Reply #10 on: February 10, 2014, 11:15:45 pm »
So, same result?

I'd guess, then, that it could very well be something in the remove_last_char() and insert_last_char() functions.
Can we see those?  ;D
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Insert and remove text not working with Events
« Reply #11 on: February 10, 2014, 11:22:25 pm »
So, same result?

I'd guess, then, that it could very well be something in the remove_last_char() and insert_last_char() functions.
Can we see those?  ;D
Sure :) here the whole code:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>

#include <math.h>
#include <iostream>
#include <sstream>
//#include "include/Collision.h"

#define textsize 200


inline int give_letter_size(char &letter, sf::Font &font, int font_size)
{
    return font.getGlyph(int(letter), font_size, false).advance;
}

std::string &word_wrap(std::string &text, int width, int font_size, sf::Font &font)
{
    int s_width = 0;
    int last_word_position = 0;
    bool position_used = false;
    for(int i = 0; i <= text.size(); i++)
    {
        s_width += give_letter_size(text[i], font, font_size);

        if(s_width >= width)
        {
            if(!position_used)
            {
                text.replace(last_word_position, 1, "\n");
                i = last_word_position;
                position_used = true;
                s_width = 0;
            }
            else
            {
                text.insert(i, "\n");
                s_width = 0;
            }
        }
        if(text[i] == ' ')
        {
            last_word_position = i;
            position_used = false;
        }
    }
    return text;
}

std::string &remove_last_char(std::string &text)
{
    text.erase(text.size(), 1);
    return text;
}

std::string &insert_last_char(std::string &text, char char_)
{
    text.insert(text.size(), 1, char_);
    return text;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode::getDesktopMode() , "SFML App", sf::Style::Default);

    sf::Font font;
    if(!font.loadFromFile("arial.ttf"))
        std::cout << "Schriftart konnte nicht geladen werden" << std::endl;


    std::string text = "Ich habe etwas zu programmieren, und das ist toll nicht wahr? Ich liebe es wenn etwas bei mir funktioniert. Das ist immer gut. sdfgsghdfhdrtzdfhgdfjhdgsdfgsdfgghfgdhghdgjhgfghjsdfgsgfhgfhhj";

    sf::Text showing_text;

    text = word_wrap(text, textsize, 20, font);

    showing_text.setString(text);

    showing_text.setCharacterSize(20);
    showing_text.setFont(font);
    showing_text.setPosition(0,0);
    showing_text.setColor(sf::Color::Red);

    std::string angaben = "Copyright by jakob, made for jakeos. Maybe the worst Solution that exist for SFML";

    angaben = word_wrap(angaben, textsize, 20, font);

    sf::Text show;
    show.setString(angaben);
    show.setCharacterSize(20);
    show.setFont(font);
    show.setPosition(1720, 500);
    show.setColor(sf::Color::Green);


    int width = showing_text.getGlobalBounds().width;

    std::cout << width << std::endl;

    //bool backspace = false;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if(event.type == sf::Event::TextEntered)
            {
                if(event.text.unicode != 0x00000008)
                {
                   insert_last_char(text, event.text.unicode);
                   showing_text.setString(text);
                }
            }
            else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::BackSpace)
            {
                remove_last_char(text);
                showing_text.setString(text);
            }

        }
        window.clear();
        window.draw(showing_text);
        window.draw(show);
        window.display();
    }

    return 0;
}
 

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Insert and remove text not working with Events
« Reply #12 on: February 10, 2014, 11:32:36 pm »
text.erase(text.size(), 1);

If "text" contains "hello" then size() == 5, so the erase call would remove 1 character starting at position 5 (that is, string.end() - past the last char). I think you want text.erase(text.size() - 1, 1); offsets start at 0.
« Last Edit: February 10, 2014, 11:38:59 pm by Jesper Juhl »

Hapax

  • Hero Member
  • *****
  • Posts: 3043
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Insert and remove text not working with Events
« Reply #13 on: February 10, 2014, 11:36:03 pm »
std::string &remove_last_char(std::string &text)
{
        text.erase(text.size() - 1, 1);
        return text;
}
 

EDIT: Looks like Jesper beat me to it!  :)
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Insert and remove text not working with Events
« Reply #14 on: February 10, 2014, 11:46:43 pm »
text.erase(text.size(), 1);

If "text" contains "hello" then size() == 5, so the erase call would remove 1 character starting at position 5 (that is, string.end() - past the last char). I think you want text.erase(text.size() - 1, 1); offsets start at 0.
O that was easy, thanks it works.
I did have the same solution but with other event combination then i change that and the event, test it but forgot to chage the function back, my fault. ;) I do not know much about the String class in C++ because worked as always with char array and pointer in C :D

 

anything