tBane Temat założony przez niniejszego użytkownika |
» 2025-05-25 20:24:11 Ten bug nadal występuje :-/ #include <SFML/Graphics.hpp> #include <iostream> #include <vector> sf::RenderWindow * window;
sf::Font font; short characterSize;
std::wstring text; std::vector < sf::Text * > texts;
sf::Vector2i mousePosition; sf::Vector2f worldMousePosition;
sf::RectangleShape cursor; sf::Vector2i cursorPosition = sf::Vector2i( 0, 0 );
sf::Clock timeClock; sf::Time currentTime;
int selecting_start = 2; int selecting_end = 4; std::vector < sf::Text * > wrapText( int line_length = - 1 ) { std::vector < sf::Text * > t; std::wstring line = L""; std::wstring word = L""; wchar_t white_char = '\0'; for( auto & wchar: text ) { if( wchar == '\n' ) { if( white_char != '\0' && white_char != '\r' ) line = line + white_char + word; else line = line + word; line = line + L"\n"; sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); line = L""; word = L""; white_char = '\0'; continue; } if( wchar == L' ' || wchar == L'\t' ) { white_char = wchar; word = word + white_char; if( line_length > - 1 ) { if( line == L"" ) { sf::Text test_word( word, font, characterSize ); if( test_word.getGlobalBounds().width >= line_length ) { std::wstring part_of_word = L""; wchar_t character = '\0'; for( wchar_t & ch: word ) { character = ch; sf::Text w( part_of_word + character, font, characterSize ); if( w.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = character; } else part_of_word = part_of_word + character; } if( part_of_word != L"" ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = L""; } word = L""; continue; } } sf::Text test_text( line + white_char + word, font, characterSize ); if( test_text.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); line = word; word = L""; continue; } else line = line + word; } word = L""; white_char = '\0'; } else if( wchar != '\0' && wchar != '\r' ) { word += wchar; } } sf::Text test_word( word, font, characterSize ); if( test_word.getGlobalBounds().width >= line_length ) { std::wstring part_of_word = L""; wchar_t character = '\0'; for( wchar_t & ch: word ) { character = ch; sf::Text w( part_of_word + character, font, characterSize ); if( w.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = character; } else part_of_word = part_of_word + character; } if( part_of_word != L"" ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = L""; } word = L""; } if( !word.empty() || !line.empty() ) { sf::Text test_text( line + word, font, characterSize ); if( line_length > - 1 && test_text.getGlobalBounds().width >= line_length && !line.empty() ) { sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.empty() ? 0 : t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); new_text = new sf::Text( word, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); } else { sf::Text * new_text = new sf::Text( line + word, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.empty() ? 0 : t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); } } return t; }
sf::Vector2i getCursorPosition() { sf::Vector2i cur_pos = sf::Vector2i( 0, 0 ); for( int t = 0; t < texts.size(); t++ ) { for( size_t i = 0; i < texts[ t ]->getString().getSize(); ++i ) { sf::Vector2f charPos = texts[ t ]->findCharacterPos( i ); float nextX = texts[ t ]->findCharacterPos( i + 1 ).x; sf::FloatRect charRect( charPos.x, charPos.y, nextX - charPos.x, font.getLineSpacing( characterSize ) ); if( charRect.contains( worldMousePosition ) ) { if( worldMousePosition.x < charRect.left + charRect.width / 2 ) return sf::Vector2i( i, t ); else return sf::Vector2i( i + 1, t ); } bool isLastChar =( i == texts[ t ]->getString().getSize() - 1 ); if( isLastChar && worldMousePosition.x > charRect.left && worldMousePosition.y >= charRect.top && worldMousePosition.y <= charRect.top + charRect.height ) { return sf::Vector2i( i + 1, t ); } } } return cur_pos; }
int getCursorIndex( sf::Vector2i position ) { int index = 0; for( int i = 0; i < position.y && i < texts.size(); ++i ) { index += texts[ i ]->getString().getSize(); } index += position.x; return index; }
sf::Vector2i getCursorFromIndex( int index ) { int current = 0; for( int y = 0; y < texts.size(); ++y ) { int lineSize = texts[ y ]->getString().getSize(); if( index <= current + lineSize ) { return sf::Vector2i( index - current, y ); } current += lineSize; } if( !texts.empty() ) return sf::Vector2i( texts.back()->getString().getSize(), texts.size() - 1 ); return sf::Vector2i( 0, 0 ); }
void setCursorPosition( sf::Vector2i cursor_position ) { cursorPosition = cursor_position; for( int t = 0; t < texts.size(); t++ ) { if( t == cursor_position.y ) { std::wstring line = texts[ t ]->getString().toWideString(); if( cursor_position.x < line.size() ) { sf::Vector2f charPos = texts[ t ]->findCharacterPos( cursor_position.x ); cursor.setPosition( charPos.x, charPos.y ); return; } else { sf::Vector2f endPos = texts[ t ]->findCharacterPos( line.size() ); cursor.setPosition( endPos.x, endPos.y ); return; } } } }
int main() { sf::View view( sf::FloatRect( 0, 0, 480, 640 ) ); window = new sf::RenderWindow( sf::VideoMode( view.getSize().x, view.getSize().y ), "Easy Notepad!", sf::Style::Titlebar | sf::Style::Close ); font.loadFromFile( "arial.ttf" ); characterSize = 17; text = L"Gracz najpierw zagaduje handlarza gdyż ten jest najbliżej. Handlarz oferuje skórzane ubranie w zamian za dostarczenie kilku skór od myśliwego, " L"którego gracz mijał wcześniej. Zielarka da graczowi trochę złota w zamian za przyniesienie kilku roślin leczniczych. " L"U kowala gracz może zakupić oręż - zwyczajny prosty miecz gdyż jest to niewprawiony kowal w miecznictwie. " L"Zaś do wieży mędrca nie da się dostać.Gracz rusza spowrotem do myśliwego po skóry, lecz ten jest nieufny, " L"ale ostatecznie zgadza się i daje graczowi skóry.\n" L"Gracz wraca ze skórami do handlarza i odbiera nowe ubranie \"skórzane kurtka\" oraz \"skórzane spodnie\"." L"Handlarz jednak jeszcze jedno zadanie ma dla gracza. Dostawa towarów ze wschodu się opóźnia i trzeba sprawdzić " L"co się z nią stało i tak gracz rusza z kolejnym zadaniem \"spóźniona dostawa\"."; texts = wrapText( window->getSize().x ); cursor = sf::RectangleShape( sf::Vector2f( 2, characterSize ) ); cursor.setFillColor( sf::Color::Red ); sf::Clock clock; while( window->isOpen() ) { mousePosition = sf::Mouse::getPosition( * window ); worldMousePosition = window->mapPixelToCoords( mousePosition ); currentTime = timeClock.getElapsedTime(); sf::Event event; while( window->pollEvent( event ) ) { if( event.type == sf::Event::Closed ) window->close(); if( event.type == sf::Event::Resized ) { sf::View view; view.setSize( static_cast < float >( event.size.width ), static_cast < float >( event.size.height ) ); view.setCenter( view.getSize() / 2.f ); window->setView( view ); } else if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left ) { sf::Vector2i cur_pos = getCursorPosition(); setCursorPosition( cur_pos ); } else if( event.type == sf::Event::TextEntered ) { if( event.text.unicode < 128 ) { int index = 0; for( int i = 0; i < cursorPosition.y; i++ ) { index += texts[ i ]->getString().toWideString().size(); } index += cursorPosition.x; if( event.text.unicode == '\b' ) { if( index > 0 && !text.empty() ) { text.erase( index - 1, 1 ); index -= 1; } } else if( event.text.unicode == 32 ) { text.insert( index, 1, L' ' ); index += 1; } else if( event.text.unicode == 13 ) { text.insert( index, 1, L'\n' ); index += 1; } else { text.insert( index, 1, wchar_t( event.text.unicode ) ); index += 1; } for( auto & t: texts ) delete t; texts = wrapText( window->getSize().x ); cursorPosition = getCursorFromIndex( index ); setCursorPosition( cursorPosition ); } } else if( event.type == sf::Event::KeyPressed ) { if( event.key.code == sf::Keyboard::Left ) { if( cursorPosition.x > 0 ) { cursorPosition.x -= 1; } else { if( cursorPosition.y > 0 ) { cursorPosition.y -= 1; cursorPosition.x = texts[ cursorPosition.y ]->getString().toWideString().size() - 1; } } } else if( event.key.code == sf::Keyboard::Right ) { if( cursorPosition.x + 1 < texts[ cursorPosition.y ]->getString().toWideString().size() ) { cursorPosition.x += 1; } else { if( cursorPosition.y < texts.size() - 1 ) { cursorPosition.x = 0; cursorPosition.y += 1; } } } else if( event.key.code == sf::Keyboard::Up ) { if( cursorPosition.y > 0 ) cursorPosition.y -= 1; } else if( event.key.code == sf::Keyboard::Down ) { if( cursorPosition.y < texts.size() - 1 ) cursorPosition.y += 1; } setCursorPosition( cursorPosition ); } } window->clear( sf::Color( 48, 48, 48, 255 ) ); for( auto & text: texts ) window->draw( * text ); if( std::fmod( currentTime.asSeconds(), 0.6f ) < 0.3f ) window->draw( cursor ); window->display(); } }
|
|
pekfos |
» 2025-05-25 20:38:42 Teraz bug jest specyficzny dla świeżo dodanej spacji, błąd o 1 w sf::Vector2i getCursorFromIndex( int index ) { int current = 0; for( int y = 0; y < texts.size(); ++y ) { int lineSize = texts[ y ]->getString().getSize(); if( index < current + lineSize ) { return sf::Vector2i( index - current, y ); } Nie ryzykowałbyś takich problemów jakbyś polegał tylko na indeksie, a nie na osobno utrzymywanej pozycji, gdzie raz korzystasz z jednego a raz z drugiego. Pozycja pomaga tylko w ruszaniu się góra/dół, przy czym i tak musisz przeliczyć indeks więc to żadna oszczędność. Dodam że potrzebujesz teraz obsłużyć przypadek końca pliku, bo po ostatnich poprawkach nie można ustawić kursora za ostatnim znakiem. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-05-25 20:42:11 Ok Działa ^^ No ale już zakończe prace nad tym edytorem, bo ledwo co go rozumiem, potrzebuje czasu tak samo jak z wraperem. Teraz mogę przepisać program do Edytora i napisać system dialogów. Dziękuję bardzo za pomoc :-) // edit da sie ustawić kursor za ostatnim znakiem #include <SFML/Graphics.hpp> #include <iostream> #include <vector> sf::RenderWindow * window;
sf::Font font; short characterSize;
std::wstring text; std::vector < sf::Text * > texts;
sf::Vector2i mousePosition; sf::Vector2f worldMousePosition;
sf::RectangleShape cursor; sf::Vector2i cursorPosition = sf::Vector2i( 0, 0 );
sf::Clock timeClock; sf::Time currentTime;
int selecting_start = 2; int selecting_end = 4; std::vector < sf::Text * > wrapText( int line_length = - 1 ) { std::vector < sf::Text * > t; std::wstring line = L""; std::wstring word = L""; wchar_t white_char = '\0'; for( auto & wchar: text ) { if( wchar == '\n' ) { if( white_char != '\0' && white_char != '\r' ) line = line + white_char + word; else line = line + word; line = line + L"\n"; sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); line = L""; word = L""; white_char = '\0'; continue; } if( wchar == L' ' || wchar == L'\t' ) { white_char = wchar; word = word + white_char; if( line_length > - 1 ) { if( line == L"" ) { sf::Text test_word( word, font, characterSize ); if( test_word.getGlobalBounds().width >= line_length ) { std::wstring part_of_word = L""; wchar_t character = '\0'; for( wchar_t & ch: word ) { character = ch; sf::Text w( part_of_word + character, font, characterSize ); if( w.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = character; } else part_of_word = part_of_word + character; } if( part_of_word != L"" ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = L""; } word = L""; continue; } } sf::Text test_text( line + white_char + word, font, characterSize ); if( test_text.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); line = word; word = L""; continue; } line = line + word; } word = L""; white_char = '\0'; } else if( wchar != '\0' && wchar != '\r' ) { word += wchar; } } sf::Text test_word( word, font, characterSize ); if( test_word.getGlobalBounds().width >= line_length ) { std::wstring part_of_word = L""; wchar_t character = '\0'; for( wchar_t & ch: word ) { character = ch; sf::Text w( part_of_word + character, font, characterSize ); if( w.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = character; } else part_of_word = part_of_word + character; } if( part_of_word != L"" ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = L""; } word = L""; } if( !word.empty() || !line.empty() ) { sf::Text test_text( line + word, font, characterSize ); if( line_length > - 1 && test_text.getGlobalBounds().width >= line_length && !line.empty() ) { sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.empty() ? 0 : t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); new_text = new sf::Text( word, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); } else { sf::Text * new_text = new sf::Text( line + word, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.empty() ? 0 : t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); } } return t; }
sf::Vector2i getCursorPosition() { sf::Vector2i cur_pos = sf::Vector2i( 0, 0 ); for( int t = 0; t < texts.size(); t++ ) { for( size_t i = 0; i < texts[ t ]->getString().getSize(); ++i ) { sf::Vector2f charPos = texts[ t ]->findCharacterPos( i ); float nextX = texts[ t ]->findCharacterPos( i + 1 ).x; sf::FloatRect charRect( charPos.x, charPos.y, nextX - charPos.x, font.getLineSpacing( characterSize ) ); if( charRect.contains( worldMousePosition ) ) { if( worldMousePosition.x < charRect.left + charRect.width / 2 ) return sf::Vector2i( i, t ); else return sf::Vector2i( i + 1, t ); } bool isLastChar =( i == texts[ t ]->getString().getSize() - 1 ); if( isLastChar && worldMousePosition.x > charRect.left && worldMousePosition.y >= charRect.top && worldMousePosition.y <= charRect.top + charRect.height ) { return sf::Vector2i( i + 1, t ); } } } return cur_pos; }
int getCursorIndex( sf::Vector2i position ) { int index = 0; for( int i = 0; i < position.y && i < texts.size(); ++i ) { index += texts[ i ]->getString().getSize(); } index += position.x; return index; }
sf::Vector2i getCursorFromIndex( int index ) { int current = 0; for( int y = 0; y < texts.size(); ++y ) { int lineSize = texts[ y ]->getString().getSize(); if( index < current + lineSize ) { return sf::Vector2i( index - current, y ); } current += lineSize; } if( !texts.empty() ) return sf::Vector2i( texts.back()->getString().getSize(), texts.size() - 1 ); return sf::Vector2i( 0, 0 ); }
void setCursorPosition( sf::Vector2i cursor_position ) { cursorPosition = cursor_position; for( int t = 0; t < texts.size(); t++ ) { if( t == cursor_position.y ) { std::wstring line = texts[ t ]->getString().toWideString(); if( cursor_position.x < line.size() ) { sf::Vector2f charPos = texts[ t ]->findCharacterPos( cursor_position.x ); cursor.setPosition( charPos.x, charPos.y ); return; } else { sf::Vector2f endPos = texts[ t ]->findCharacterPos( line.size() ); cursor.setPosition( endPos.x, endPos.y ); return; } } } }
int main() { sf::View view( sf::FloatRect( 0, 0, 480, 640 ) ); window = new sf::RenderWindow( sf::VideoMode( view.getSize().x, view.getSize().y ), "Easy Notepad!", sf::Style::Titlebar | sf::Style::Close ); font.loadFromFile( "arial.ttf" ); characterSize = 17; text = L"Gracz najpierw zagaduje handlarza gdyż ten jest najbliżej. Handlarz oferuje skórzane ubranie w zamian za dostarczenie kilku skór od myśliwego, " L"którego gracz mijał wcześniej. Zielarka da graczowi trochę złota w zamian za przyniesienie kilku roślin leczniczych. " L"U kowala gracz może zakupić oręż - zwyczajny prosty miecz gdyż jest to niewprawiony kowal w miecznictwie. " L"Zaś do wieży mędrca nie da się dostać.Gracz rusza spowrotem do myśliwego po skóry, lecz ten jest nieufny, " L"ale ostatecznie zgadza się i daje graczowi skóry.\n" L"Gracz wraca ze skórami do handlarza i odbiera nowe ubranie \"skórzane kurtka\" oraz \"skórzane spodnie\"." L"Handlarz jednak jeszcze jedno zadanie ma dla gracza. Dostawa towarów ze wschodu się opóźnia i trzeba sprawdzić " L"co się z nią stało i tak gracz rusza z kolejnym zadaniem \"spóźniona dostawa\"."; texts = wrapText( window->getSize().x ); cursor = sf::RectangleShape( sf::Vector2f( 2, characterSize ) ); cursor.setFillColor( sf::Color::Red ); sf::Clock clock; while( window->isOpen() ) { mousePosition = sf::Mouse::getPosition( * window ); worldMousePosition = window->mapPixelToCoords( mousePosition ); currentTime = timeClock.getElapsedTime(); sf::Event event; while( window->pollEvent( event ) ) { if( event.type == sf::Event::Closed ) window->close(); if( event.type == sf::Event::Resized ) { sf::View view; view.setSize( static_cast < float >( event.size.width ), static_cast < float >( event.size.height ) ); view.setCenter( view.getSize() / 2.f ); window->setView( view ); } else if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left ) { sf::Vector2i cur_pos = getCursorPosition(); setCursorPosition( cur_pos ); } else if( event.type == sf::Event::TextEntered ) { if( event.text.unicode < 128 ) { int index = 0; for( int i = 0; i < cursorPosition.y; i++ ) { index += texts[ i ]->getString().toWideString().size(); } index += cursorPosition.x; if( event.text.unicode == '\b' ) { if( index > 0 && !text.empty() ) { text.erase( index - 1, 1 ); index -= 1; } } else if( event.text.unicode == 32 ) { text.insert( index, 1, L' ' ); index += 1; } else if( event.text.unicode == 13 ) { text.insert( index, 1, L'\n' ); index += 1; } else { text.insert( index, 1, wchar_t( event.text.unicode ) ); index += 1; } for( auto & t: texts ) delete t; texts = wrapText( window->getSize().x ); cursorPosition = getCursorFromIndex( index ); setCursorPosition( cursorPosition ); } } else if( event.type == sf::Event::KeyPressed ) { if( event.key.code == sf::Keyboard::Left ) { if( cursorPosition.x > 0 ) { cursorPosition.x -= 1; } else { if( cursorPosition.y > 0 ) { cursorPosition.y -= 1; cursorPosition.x = texts[ cursorPosition.y ]->getString().toWideString().size() - 1; } } } else if( event.key.code == sf::Keyboard::Right ) { if( cursorPosition.x < texts[ cursorPosition.y ]->getString().toWideString().size() ) { cursorPosition.x += 1; } else { if( cursorPosition.y < texts.size() - 1 ) { cursorPosition.x = 0; cursorPosition.y += 1; } } } else if( event.key.code == sf::Keyboard::Up ) { if( cursorPosition.y > 0 ) cursorPosition.y -= 1; } else if( event.key.code == sf::Keyboard::Down ) { if( cursorPosition.y < texts.size() - 1 ) cursorPosition.y += 1; } setCursorPosition( cursorPosition ); } } window->clear( sf::Color( 48, 48, 48, 255 ) ); for( auto & text: texts ) window->draw( * text ); if( std::fmod( currentTime.asSeconds(), 0.6f ) < 0.3f ) window->draw( cursor ); window->display(); } }
|
|
pekfos |
» 2025-05-25 21:54:26 Błędnie działa strzałka w dół gdy następna linia jest pusta. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-05-25 22:20:18 Ok. Namierzyłem błąd :-) void setCursorPosition( sf::Vector2i cursor_position ) { cursorPosition = cursor_position; for( int t = 0; t < texts.size(); t++ ) { if( t == cursor_position.y ) { std::wstring line = texts[ t ]->getString().toWideString(); if( line.size() == 0 ) { cursor.setPosition( texts[ t ]->getPosition() ); return; } if( cursor_position.x < line.size() ) { sf::Vector2f charPos = texts[ t ]->findCharacterPos( cursor_position.x ); cursor.setPosition( charPos.x, charPos.y ); return; } sf::Vector2f endPos = texts[ t ]->findCharacterPos( line.size() ); cursor.setPosition( endPos.x, endPos.y ); return; } } }
oraz dodałem jeszcze ograniczenie cursorPosition.x dla przekroczenia długości linii else if( event.key.code == sf::Keyboard::Up ) { if( cursorPosition.y > 0 ) cursorPosition.y -= 1; if( cursorPosition.x > texts[ cursorPosition.y ]->getString().toWideString().size() ) cursorPosition.x = texts[ cursorPosition.y ]->getString().toWideString().size(); } else if( event.key.code == sf::Keyboard::Down ) { if( cursorPosition.y < texts.size() - 1 ) cursorPosition.y += 1; if( cursorPosition.x > texts[ cursorPosition.y ]->getString().toWideString().size() ) cursorPosition.x = texts[ cursorPosition.y ]->getString().toWideString().size(); }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-05-25 23:00:37 Dziękuję Wszystkim za pomoc :-) |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-06-07 15:03:42 Cześć. Pracuję obecnie nadal nad kodem i udało mi się zaprogramować obsługę klawisza delete. Teraz próbuję obsłużyc funkcję wklej poprzez naduszenie klawiszy Ctrl + V. Funkcja ta nie działa poprawnie, ponieważ dokleja jakiś dziwny znak na początku wklejanej sentencji. Pomożecie ? Miejsce z kodem oznaczyłem komentarzem "// Ctrl + V "#include <SFML/Graphics.hpp> #include <iostream> #include <vector> #include <cmath>
sf::RenderWindow * window;
sf::Font font; short characterSize;
std::wstring text; std::vector < sf::Text * > lines;
sf::Vector2i mousePosition; sf::Vector2f worldMousePosition;
sf::RectangleShape cursor; sf::Vector2i cursorPosition = sf::Vector2i( 0, 0 );
sf::Clock timeClock; sf::Time currentTime;
int selecting_start = 2; int selecting_end = 4; std::vector < sf::Text * > wrapText( int line_length = - 1 ) { std::vector < sf::Text * > t; std::wstring line = L""; std::wstring word = L""; wchar_t white_char = '\0'; for( auto & wchar: text ) { if( wchar == '\n' ) { if( white_char != '\0' && white_char != '\r' ) line = line + white_char + word; else line = line + word; line = line + L"\n"; sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); line = L""; word = L""; white_char = '\0'; continue; } if( wchar == L' ' || wchar == L'\t' ) { white_char = wchar; word = word + white_char; if( line_length > - 1 ) { if( line == L"" ) { sf::Text test_word( word, font, characterSize ); if( test_word.getGlobalBounds().width >= line_length ) { std::wstring part_of_word = L""; wchar_t character = '\0'; for( wchar_t & ch: word ) { character = ch; sf::Text w( part_of_word + character, font, characterSize ); if( w.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = character; } else part_of_word = part_of_word + character; } if( part_of_word != L"" ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = L""; } word = L""; continue; } } sf::Text test_text( line + white_char + word, font, characterSize ); if( test_text.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); line = word; word = L""; continue; } line = line + word; } word = L""; white_char = '\0'; } else if( wchar != '\0' && wchar != '\r' ) { word += wchar; } } sf::Text test_word( word, font, characterSize ); if( test_word.getGlobalBounds().width >= line_length ) { std::wstring part_of_word = L""; wchar_t character = '\0'; for( wchar_t & ch: word ) { character = ch; sf::Text w( part_of_word + character, font, characterSize ); if( w.getGlobalBounds().width >= line_length ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = character; } else part_of_word = part_of_word + character; } if( part_of_word != L"" ) { sf::Text * new_text = new sf::Text( part_of_word, font, characterSize ); new_text->setFillColor( sf::Color::White ); if( !t.empty() ) new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); else new_text->setPosition( 0, 0 ); t.push_back( new_text ); part_of_word = L""; } word = L""; } if( !word.empty() || !line.empty() ) { sf::Text test_text( line + word, font, characterSize ); if( line_length > - 1 && test_text.getGlobalBounds().width >= line_length && !line.empty() ) { sf::Text * new_text = new sf::Text( line, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.empty() ? 0 : t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); new_text = new sf::Text( word, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); } else { sf::Text * new_text = new sf::Text( line + word, font, characterSize ); new_text->setFillColor( sf::Color::White ); new_text->setPosition( 0, t.empty() ? 0 : t.back()->getPosition().y + font.getLineSpacing( characterSize ) ); t.push_back( new_text ); } } return t; }
sf::Vector2i getCursorPosition() { sf::Vector2i cur_pos = sf::Vector2i( 0, 0 ); for( int t = 0; t < lines.size(); t++ ) { for( size_t i = 0; i < lines[ t ]->getString().getSize(); ++i ) { sf::Vector2f charPos = lines[ t ]->findCharacterPos( i ); float nextX = lines[ t ]->findCharacterPos( i + 1 ).x; sf::FloatRect charRect( charPos.x, charPos.y, nextX - charPos.x, font.getLineSpacing( characterSize ) ); if( charRect.contains( worldMousePosition ) ) { if( worldMousePosition.x < charRect.left + charRect.width / 2 ) return sf::Vector2i( i, t ); else return sf::Vector2i( i + 1, t ); } bool isLastChar =( i == lines[ t ]->getString().getSize() - 1 ); if( isLastChar && worldMousePosition.x > charRect.left && worldMousePosition.y >= charRect.top && worldMousePosition.y <= charRect.top + charRect.height ) { return sf::Vector2i( i + 1, t ); } } } return cur_pos; }
int getCursorIndex( sf::Vector2i position ) { int index = 0; for( int i = 0; i < position.y && i < lines.size(); ++i ) { index += lines[ i ]->getString().getSize(); } index += position.x; return index; }
sf::Vector2i getCursorFromIndex( int index ) { if( index == 0 || lines.empty() || lines.size() == 0 ) return sf::Vector2i( 0, 0 ); int current = 0; for( int y = 0; y < lines.size(); ++y ) { int lineSize = lines[ y ]->getString().getSize(); if( index == current + lineSize ) { return sf::Vector2i( index - current, y ); } current += lineSize; } return sf::Vector2i( lines.back()->getString().getSize(), lines.size() - 1 ); }
void setCursorUp() { if( lines.empty() ) return; if( cursorPosition.y > 0 ) { float targetX = cursor.getGlobalBounds().left; cursorPosition.y -= 1; sf::Text * line = lines[ cursorPosition.y ]; size_t lineLength = line->getString().toWideString().size(); size_t closestIndex = 0; float closestDistance = std::abs( line->findCharacterPos( 0 ).x - targetX ); for( size_t i = 1; i <= lineLength; ++i ) { sf::Vector2f pos = line->findCharacterPos( i ); float distance = std::abs( pos.x - targetX ); if( distance < closestDistance ) { closestIndex = i; closestDistance = distance; } } cursorPosition.x = closestIndex; cursor.setPosition( line->findCharacterPos( closestIndex ) ); } } void setCursorDown() { if( lines.empty() ) return; if( cursorPosition.y < lines.size() - 1 ) { float targetX = cursor.getGlobalBounds().left; cursorPosition.y += 1; sf::Text * line = lines[ cursorPosition.y ]; size_t lineLength = line->getString().toWideString().size(); size_t closestIndex = 0; float closestDistance = std::abs( line->findCharacterPos( 0 ).x - targetX ); for( size_t i = 1; i <= lineLength; ++i ) { sf::Vector2f pos = line->findCharacterPos( i ); float distance = std::abs( pos.x - targetX ); if( distance < closestDistance ) { closestIndex = i; closestDistance = distance; } } cursorPosition.x = closestIndex; cursor.setPosition( line->findCharacterPos( closestIndex ) ); } }
void setCursorPosition( sf::Vector2i cursor_position ) { cursorPosition = cursor_position; if( cursor_position == sf::Vector2i( 0, 0 ) ) { cursor.setPosition( sf::Vector2f( 0, 0 ) ); return; } for( int t = 0; t < lines.size(); t++ ) { if( t == cursor_position.y ) { std::wstring line = lines[ t ]->getString().toWideString(); if( line.size() == 0 ) { cursor.setPosition( lines[ t ]->getPosition() ); return; } if( cursor_position.x < line.size() ) { sf::Vector2f charPos = lines[ t ]->findCharacterPos( cursor_position.x ); cursor.setPosition( charPos.x, charPos.y ); return; } sf::Vector2f endPos = lines[ t ]->findCharacterPos( line.size() ); cursor.setPosition( endPos.x, endPos.y ); return; } } }
int main() { sf::View view( sf::FloatRect( 0, 0, 480, 640 ) ); window = new sf::RenderWindow( sf::VideoMode( view.getSize().x, view.getSize().y ), "Easy Notepad!", sf::Style::Titlebar | sf::Style::Close ); font.loadFromFile( "arial.ttf" ); characterSize = 17; lines = wrapText( window->getSize().x ); cursor = sf::RectangleShape( sf::Vector2f( 2, characterSize ) ); cursor.setFillColor( sf::Color::Red ); sf::Clock clock; while( window->isOpen() ) { mousePosition = sf::Mouse::getPosition( * window ); worldMousePosition = window->mapPixelToCoords( mousePosition ); currentTime = timeClock.getElapsedTime(); sf::Event event; while( window->pollEvent( event ) ) { if( event.type == sf::Event::Closed ) window->close(); if( event.type == sf::Event::Resized ) { sf::View view; view.setSize( static_cast < float >( event.size.width ), static_cast < float >( event.size.height ) ); view.setCenter( view.getSize() / 2.f ); window->setView( view ); } else if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left ) { sf::Vector2i cur_pos = getCursorPosition(); setCursorPosition( cur_pos ); } else if( event.type == sf::Event::KeyPressed && event.key.control && event.key.code == sf::Keyboard::V ) { int index = getCursorIndex( cursorPosition ); std::cout << index << "\n"; sf::String clipboard = sf::Clipboard::getString(); std::wcout << clipboard.toWideString() << L"\n"; text.insert( index, clipboard.toWideString() ); index = index + clipboard.getSize(); std::cout << index << "\n"; cursorPosition = getCursorFromIndex( index ); setCursorPosition( cursorPosition ); } else if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Delete ) { int index = getCursorIndex( cursorPosition ); if( !text.empty() ) { text.erase( index, 1 ); lines = wrapText( window->getSize().x ); } } else if( event.type == sf::Event::TextEntered ) { if( event.text.unicode < 128 ) { int index = getCursorIndex( cursorPosition ); if( event.text.unicode == 8 ) { if( index > 0 && !text.empty() ) { text.erase( index - 1, 1 ); index -= 1; } } else if( event.text.unicode == 32 ) { text.insert( index, 1, L' ' ); index += 1; } else if( event.text.unicode == 13 ) { text.insert( index, 1, L'\n' ); index += 1; } else { text.insert( index, 1, wchar_t( event.text.unicode ) ); index += 1; } for( auto & t: lines ) delete t; lines = wrapText( window->getSize().x ); cursorPosition = getCursorFromIndex( index ); setCursorPosition( cursorPosition ); } } else if( event.type == sf::Event::KeyPressed ) { if( event.key.code == sf::Keyboard::Left ) { if( cursorPosition.x > 0 ) { cursorPosition.x -= 1; } else { if( !lines.empty() && cursorPosition.y > 0 ) { cursorPosition.y -= 1; cursorPosition.x = lines[ cursorPosition.y ]->getString().toWideString().size() - 1; } } } else if( event.key.code == sf::Keyboard::Right ) { if( !lines.empty() && cursorPosition.x < lines[ cursorPosition.y ]->getString().toWideString().size() ) { cursorPosition.x += 1; } else { if( cursorPosition.y < lines.size() - 1 ) { cursorPosition.x = 0; cursorPosition.y += 1; } } } else if( event.key.code == sf::Keyboard::Up ) { setCursorUp(); } else if( event.key.code == sf::Keyboard::Down ) { setCursorDown(); } setCursorPosition( cursorPosition ); } } window->clear( sf::Color( 48, 48, 48, 255 ) ); for( auto & line: lines ) window->draw( * line ); if( std::fmod( currentTime.asSeconds(), 0.6f ) < 0.3f ) window->draw( cursor ); window->display(); } }
|
|
pekfos |
» 2025-06-07 17:12:07 Jak dostajesz zdarzenie TextEntered, to odrzucaj kody kontrolne ASCII. SFML dostaje to tak z systemu, może powinien to filtrować, ale tego nie robi. https://learn.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input#character-messagesSome CTRL key combinations are translated into ASCII control characters. For example, CTRL+A is translated to the ASCII ctrl-A (SOH) character (ASCII value 0x01). For text input, you should generally filter out the control characters. |
|
1 2 3 4 « 5 » 6 7 8 9 10 |