tBane Temat założony przez niniejszego użytkownika |
» 2024-09-29 20:45:26 Ok. Dzięki. Kod scrollbar'a #ifndef Scrollbar_hpp #define Scrollbar_hpp
class Scrollbar { public: sf::Vector2f size; sf::Vector2f position; float minValue; float maxValue; float scrollValue; float scrollLength; sf::RectangleShape bar; sf::RectangleShape scroll; Scrollbar( sf::Vector2f size, sf::Vector2f position, float minValue, float maxValue, float scrollValue, float scrollLength ) { this->size = size; this->position = position; this->minValue = minValue; this->maxValue = maxValue; this->scrollValue = scrollValue; this->scrollLength = scrollLength; } void setValue( float value ) { scrollValue = value; if( scrollValue > maxValue - scrollLength + 1 ) scrollValue = maxValue - scrollLength + 1; if( scrollValue < minValue ) scrollValue = minValue; } void update() { bar = sf::RectangleShape( size ); bar.setFillColor( sf::Color::Blue ); bar.setPosition( position ); sf::Vector2f scrollPosition; scrollPosition.x = position.x + size.x / 4.0f; scrollPosition.y = position.y + scrollLength * size.x / 4.0f +( scrollValue - minValue ) /( maxValue - scrollLength + 1 - minValue ) *( size.y - scrollLength * size.x / 2.0f ); scroll = sf::RectangleShape( sf::Vector2f( size.x, scrollLength * size.x / 2.0f ) ); scroll.setOrigin( size.x / 4.0f, scrollLength * size.x / 4.0f ); scroll.setFillColor( sf::Color::Red ); scroll.setPosition( scrollPosition ); } void draw( sf::RenderWindow * window ) { window->draw( bar ); window->draw( scroll ); } }; #endif
Funkcja renderująca Open Dialog Box ze Scrollbar'em void testOpenDialogBox() { sf::RenderWindow window( sf::VideoMode( 600, 600 ), "test Open Dialog Box" ); window.setFramerateLimit( 4 ); sf::RectangleShape rect; sf::RectangleShape titlebar; sf::Text title; sf::RectangleShape submitbar; std::string folderPath = "assets"; sf::RectangleShape textField[ 8 ]; sf::Text texts[ 8 ]; Scrollbar * scrollbar;; enum class scrollbarState { plus, minus }; int scrollbarValue = 0; scrollbarState scrollState = scrollbarState::plus; while( window.isOpen() ) { sf::Event event; while( window.pollEvent( event ) ) { if( event.type == sf::Event::Closed ) window.close(); if( event.type == sf::Event::KeyPressed ) { if( sf::Keyboard::isKeyPressed( sf::Keyboard::Escape ) ) window.close(); } } sf::Vector2f position = sf::Vector2f( 300, 300 ); rect = sf::RectangleShape( sf::Vector2f( 512, 256 + 32 ) ); rect.setPosition( position.x - 256, position.y - 128 - 32 ); rect.setFillColor( sf::Color( 64, 64, 64 ) ); titlebar = sf::RectangleShape( sf::Vector2f( 512, 32 ) ); titlebar.setPosition( position.x - 256, position.y - 128 - 32 ); titlebar.setFillColor( sf::Color( 48, 48, 48 ) ); title = sf::Text(); title.setFont( basicFont ); title.setCharacterSize( 24 ); title.setFillColor( textColor ); title.setPosition( position.x - 256 + 8, position.y - 128 - 32 ); title.setString( "LOAD MAP" ); std::vector < std::string > paths; if( filesystem::exists( folderPath ) && filesystem::is_directory( folderPath ) ) { for( auto & entry: filesystem::directory_iterator( folderPath ) ) { paths.push_back( entry.path().string() ); } } sf::Vector2f scrollbarSize = sf::Vector2f( 16, 256 - 32 ); sf::Vector2f scrollbarPosition = sf::Vector2f( position.x + 256 - scrollbarSize.x, position.y - 128 ); scrollbar = new Scrollbar( scrollbarSize, scrollbarPosition, 0, paths.size() - 1, 0, 7 ); if( scrollState == scrollbarState::plus ) { scrollbarValue += 1; scrollbar->setValue( scrollbarValue ); if( scrollbarValue == scrollbar->maxValue - scrollbar->scrollLength + 1 ) scrollState = scrollbarState::minus; } else if( scrollState == scrollbarState::minus ) { scrollbarValue -= 1; scrollbar->setValue( scrollbarValue ); if( scrollbarValue == scrollbar->minValue ) scrollState = scrollbarState::plus; } scrollbar->setValue( scrollbarValue ); scrollbar->update(); for( int i = 0; i < 7; i++ ) { textField[ i ] = sf::RectangleShape( sf::Vector2f( 512, 32 ) ); textField[ i ].setPosition( position.x - 256, position.y - 128 + i * 32 ); textField[ i ].setFillColor( sf::Color::Transparent ); texts[ i ] = sf::Text(); texts[ i ].setFont( basicFont ); texts[ i ].setCharacterSize( 20 ); texts[ i ].setFillColor( textColor ); cout << i + int( scrollbar->scrollValue ) - int( scrollbar->scrollLength ) + 1 << " / " << paths.size() << "\n"; texts[ i ].setString( paths[ i + int( scrollbar->scrollValue ) ] ); texts[ i ].setPosition( position.x - 256 + 8, position.y - 128 + i * 32 ); } submitbar = sf::RectangleShape( sf::Vector2f( 512, 32 ) ); submitbar.setPosition( position.x - 256, position.y + 256 - 128 - 32 ); submitbar.setFillColor( sf::Color( 48, 48, 48 ) ); window.clear(); window.draw( rect ); window.draw( titlebar ); window.draw( title ); window.draw( submitbar ); scrollbar->draw( & window ); for( int i = 0; i < 8; i++ ) { window.draw( textField[ i ] ); window.draw( texts[ i ] ); } window.display(); } }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-30 16:33:20 Potrzebuję teraz posortować pliki i foldery według typu: if( filesystem::exists( folderPath ) && filesystem::is_directory( folderPath ) ) { for( auto & entry: filesystem::directory_iterator( folderPath ) ) { paths.push_back( entry.path().string() ); } }
|
|
pekfos |
» 2024-09-30 18:32:06 |
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-30 19:11:15 Klucz do sortowania: bool key( std::filesystem::directory_entry first, std::filesystem::directory_entry second ) { if( first.path().extension() < second.path().extension() ) { return true; } return false; }
Tworzenie listy plików oraz sortowanie jej według typu: std::vector < std::filesystem::directory_entry > paths;
if( filesystem::exists( folderPath ) && filesystem::is_directory( folderPath ) ) { for( auto & entry: filesystem::directory_iterator( folderPath ) ) { paths.push_back( entry ); } }
std::sort( paths.begin(), paths.end(), key );
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-30 19:14:47 Mam jeszcze jeden problem i to dość poważny. Nie wiem jak zaprogramować scrollowanie myszą scrollbara, znaczy się przesuwanie go. Jak na razie mam tylko to, bo nic więcej nie potrafię. while( window->isOpen() ) { sf::Event event; while( window->pollEvent( event ) ) { if( event.type == sf::Event::MouseButtonPressed ) { if( event.mouseButton.button == sf::Mouse::Left ) { if( scrollbar->cursorOnScroll() ) { scrollbar->press = true; } } } if( event.type == sf::Event::MouseButtonReleased ) { if( event.mouseButton.button == sf::Mouse::Left ) { scrollbar->press = false; } } } mousePosition = sf::Mouse::getPosition( * window ); worldMousePosition = window->mapPixelToCoords( mousePosition ); scrollbar->update(); window->clear(); scrollbar->draw(); window->display(); }
|
|
pekfos |
» 2024-09-30 19:56:48 Niewiele się to różni od przeciągania które ostatnio robiłeś. W momencie kliknięcia na scrollbar zapisz pozycję myszki i pozycję myszki względem scrollbara, tak byś mógł odzyskać pozycję Y scrollbara tylko na podstawie bieżącej pozycji myszki. Narzuć na to warunki graniczne min/max i w zasadzie gotowe. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-30 19:59:35 Próbowałem i tak, i tak mi nie wyszło. Może jakiś przykład byś podrzucił ? |
|
pekfos |
» 2024-09-30 20:33:14 Na podstawie jednego z wcześniejszych kodów #include <SFML/System.hpp> #include <SFML/Graphics.hpp>
sf::RenderWindow * window;
class Scrollbar { public: sf::Vector2f size; sf::Vector2f position; float minValue; float maxValue; float scrollValue; sf::RectangleShape bar; sf::RectangleShape scroll; bool pressed; sf::Vector2f mouseStartPos; sf::Vector2f mouseOffset; Scrollbar( sf::Vector2f size, sf::Vector2f position, float minValue, float maxValue, float scrollValue ) : pressed( false ) { this->size = size; this->position = position; this->minValue = minValue; this->maxValue = maxValue; this->scrollValue = scrollValue; scroll = sf::RectangleShape( sf::Vector2f( size.x * 2, size.x * 2 ) ); } void update( sf::Event & event ) { bar = sf::RectangleShape( size ); bar.setFillColor( sf::Color::Blue ); bar.setPosition( position ); scroll.setOrigin( size.x, size.x ); scroll.setFillColor( sf::Color::Red ); sf::Vector2f scrollPosition; scrollPosition.x = position.x; scrollPosition.y = position.y +( scrollValue - minValue ) /( maxValue - minValue ) * size.y; scroll.setPosition( scrollPosition ); if( event.type == sf::Event::MouseButtonPressed ) { if( event.mouseButton.button == sf::Mouse::Left ) { sf::Vector2f mousePos = window->mapPixelToCoords( sf::Vector2i( event.mouseButton.x, event.mouseButton.y ) ); if( scroll.getGlobalBounds().contains( mousePos ) ) { pressed = true; mouseStartPos = mousePos; mouseOffset = scrollPosition - mousePos; } } } if( event.type == sf::Event::MouseButtonReleased ) { if( event.mouseButton.button == sf::Mouse::Left ) { pressed = false; } } if( event.type == sf::Event::MouseMoved ) { if( pressed ) { sf::Vector2f mousePos = window->mapPixelToCoords( sf::Vector2i( event.mouseMove.x, event.mouseMove.y ) ); if( fabs( mousePos.x - mouseStartPos.x ) > 100 ) mousePos = mouseStartPos; float value =( mousePos.y + mouseOffset.y - position.y ) / size.y *( maxValue - minValue ) + minValue; if( value < minValue ) value = minValue; else if( value > maxValue ) value = maxValue; scrollValue = value; } } } void draw() { window->draw( bar ); window->draw( scroll ); } };
int main() { window = new sf::RenderWindow( sf::VideoMode::getDesktopMode(), "scroll" ); window->setFramerateLimit( 60 ); sf::Vector2f size = sf::Vector2f( 16, 512 ); sf::Vector2f position = sf::Vector2f( 200, 400 ); Scrollbar scrollbar = Scrollbar( size, position, 16, 32, 16 ); while( window->isOpen() ) { sf::Event event; while( window->pollEvent( event ) ) { scrollbar.update( event ); if( event.type == sf::Event::Closed ) window->close(); } window->clear(); scrollbar.draw(); window->display(); } return 0; } |
|
1 « 2 » 3 4 5 |