tBane Temat założony przez niniejszego użytkownika |
» 2025-06-07 17:23:24 Jak odrzucać znaki kontrolne ? Pokażesz krótki przykład ? Ja już napisałem napisałem ten kod i działa. Ale może te znaki kontrolne lepiej sprawdzać ? #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(); text.insert( index, clipboard.toWideString() ); index = index + clipboard.getSize(); lines = wrapText( window->getSize().x ); 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 ) { wchar_t character = static_cast < wchar_t >( event.text.unicode ); if( character == 32 || character == 13 || character == 8 ||( character >= 32 && character != 127 ) ) { 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, character ); 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:51:03 Może być, tylko niepotrzebnie traktujesz specjalnie spację. To znak o kodzie 32, traktuj go jak inne znaki. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-06-07 18:01:50 Ok. Jeszcze mam problem z enterami gdy wkleja się tekst. Źle pozycjonuje wtedy kursor #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() ) 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::wstring clip_text = sf::Clipboard::getString().toWideString(); text.insert( index, clip_text ); index += clip_text.size(); lines = wrapText( window->getSize().x ); 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 ) { 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 == 13 ) { text.insert( index, 1, L'\n' ); index += 1; } else { wchar_t character = static_cast < wchar_t >( event.text.unicode ); if( character >= 32 && character != 127 ) { text.insert( index, 1, character ); 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 18:22:12 Nie wiadomo o co chodzi, a kod jest niekompletny. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-06-07 18:31:25 Gdy wkleja się tekst. To kursor pokazuje na koniec całego tekstu ale tak na prawdę gdy zacznie się wprowadzać tekst to wprowadzany tekst jest przesunięty o parę znaków w lewo. Nie wiem jak to inaczej wytłumaczyć. Kod jest kompletny ... no z wyjątkiem tego SFML_intro. Dobra już to skasowalem. Spróbuj wkleić to i coś napisać: Ala ma kota. #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() ) 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::wstring clip_text = sf::Clipboard::getString().toWideString(); text.insert( index, clip_text ); index += clip_text.size(); lines = wrapText( window->getSize().x ); 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 ) { 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 == 13 ) { text.insert( index, 1, L'\n' ); index += 1; } else { wchar_t character = static_cast < wchar_t >( event.text.unicode ); if( character >= 32 && character != 127 ) { text.insert( index, 1, character ); 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 18:59:52 Wklejony tekst ma zakończenia linii w formie \r\n. Musisz wklejany tekst dostosować do konwencji przyjętej w programie. Nie wiem jak to inaczej wytłumaczyć. Dokładnie tak, jak gdy przestałeś próbować to wytłumaczyć. Kroki do reprodukcji problemu. Nigdzie nie napisałeś że wklejasz więcej niż jedną linię tekstu. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-06-07 19:09:29 Napisałem dwie funkcje funkcje kasującą ostatnie znaki ale żadna nie zadziałała. std::wstring clip_text = sf::Clipboard::getString().toWideString(); if( clip_text[ clip_text.size() - 2 ] == L'\r' && clip_text[ clip_text.size() - 1 ] == L'\n' ) clip_text = std::wstring( clip_text.begin(), clip_text.end() - 2 );
std::wstring clip_text = sf::Clipboard::getString().toWideString(); if( clip_text.size() >= 2 && clip_text[ clip_text.size() - 2 ] == L'\r' && clip_text[ clip_text.size() - 1 ] == L'\n' ) { clip_text.erase( clip_text.size() - 2, 2 ); }
|
|
pekfos |
» 2025-06-07 19:14:19 Czemu ostatnie znaki? Wklejasz Ala\r\nma\r\nkota.. Zrób clip_text.find("\r\n") w pętli i kasuj 1 znak. |
|
1 2 3 4 5 « 6 » 7 8 9 10 |