colorgreen19 Temat założony przez niniejszego użytkownika |
Funkcje nakładają sie chyba na siebie (brak funkcji virtualnych?) » 2014-01-30 15:22:46 Sory, ale nie mam pojecia jak nazwac ten temat, wiec opisze problem Stworzylem sobie klase przycisku (pisze w SFML2.1 ale to nie chodzi o bledy z ta biblioteka) //kod podaje bez preprocesora i namespace-ów by bylo krócej button.h class Button { public: enum ButtonStan { NotClicked, Clicked }; void Draw( int, int, int, int, RenderWindow & okno, Color, String ); void Draw( RenderWindow & okno ); ButtonStan HandleClick( RenderWindow & okno ); bool InButtonArea( RenderWindow & okno ); Rect < int > rect; int elseprzejazd; int przejazdPetli; private: Font font; String nazwa; Text text; }; button.cpp Button button;
void Button::Draw( int positionX, int positionY, int sizeX, int sizeY, RenderWindow & okno, Color color, String nazwa ) { font.loadFromFile( "czcionka.ttf" ); text.setString( nazwa ); text.setColor( color ); text.setFont( font ); text.setPosition( positionX, positionY ); text.setCharacterSize( 50 ); Rect < int > rect; button.rect.left = positionX - 10; button.rect.top = positionY + 3; button.rect.width = sizeX; button.rect.height = sizeY; }
void Button::Draw( RenderWindow & okno ) { okno.draw( text ); }
bool Button::InButtonArea( RenderWindow & okno ) { Vector2i mposition = Mouse::getPosition( okno ); if( mposition.x > button.rect.left && mposition.x < button.rect.left + button.rect.width && mposition.y > button.rect.top && mposition.y < button.rect.top + button.rect.height ) { return true; } else return false; }
Button::ButtonStan Button::HandleClick( RenderWindow & okno ) { Vector2i mposition = Mouse::getPosition( okno ); if( mposition.x > button.rect.left && mposition.x < button.rect.left + button.rect.width && mposition.y > button.rect.top && mposition.y < button.rect.top + button.rect.height ) { cout << "Button clicked!" << endl; } else return NotClicked; }
Jak widac zawira proste 4 funkcje. to "wieksze" Draw rysyuje obszar do klinkecia i "rysuje"(bo jeszcze ne wyswietla, nazwijmy to rysowanie logiczne) przyciek, "mniejsze" Draw wyswietla przycisk(nazwijmy to rysowanie fizyczne, InButtonArea sprawdza czy mysz jest w "obszarze" przycisku(bool), HandleClick to co sie dzieje przy nacisnieciu(zwraca enum). Mam teraz klase menu menu.h class Menu : public Button { public: enum MenuStan { Nothing, Exit, Play, Settings, Stats }; MenuStan Show( RenderWindow & okno ); MenuStan GetMenuResponse( RenderWindow & okno ); Button exitButton, playButton, settingsButton, statsButton; private: Color color; MenuStan HandleClick( int mouseX, int mouseY ); Menu::MenuStan menuStan; }; menu.cpp using namespace sf; using namespace std;
bool nowystan = false, poprzednistan = false;
RectangleShape menuP1( Vector2f( 200, 60 ) ), menuP2( Vector2f( 200, 60 ) ), menuP3( Vector2f( 200, 60 ) ), menuP4( Vector2f( 200, 60 ) );
Menu::MenuStan Menu::Show( RenderWindow & okno ) { }
Menu::MenuStan Menu::GetMenuResponse( RenderWindow & okno ) { okno.clear(); int ptextx1 = 100, ptexty1 = 100, ptextx2 = ptextx1, ptexty2 = ptexty1 + 65, ptextx3 = ptextx2, ptexty3 = ptexty2 + 65, ptextx4 = ptextx3, ptexty4 = ptexty3 + 65; playButton.Draw( ptextx1, ptexty1, 200, 60, okno, Color( 255, 255, 255 ), "Play" ); settingsButton.Draw( ptextx2, ptexty2, 200, 60, okno, Color( 255, 255, 255 ), "Settings" ); statsButton.Draw( ptextx3, ptexty3, 200, 60, okno, Color( 255, 255, 255 ), "Stats" ); exitButton.Draw( ptextx4, ptexty4, 200, 60, okno, Color( 255, 255, 255 ), "Exit" ); menuP1.setPosition( ptextx1 - 10, ptexty1 + 3 ); menuP1.setFillColor( Color( 70, 70, 70, 70 ) ); menuP2.setPosition( ptextx2 - 10, ptexty2 + 3 ); menuP2.setFillColor( Color( 70, 70, 70, 70 ) ); menuP3.setPosition( ptextx3 - 10, ptexty3 + 3 ); menuP3.setFillColor( Color( 70, 70, 70, 70 ) ); menuP4.setPosition( ptextx4 - 10, ptexty4 + 3 ); menuP4.setFillColor( Color( 70, 70, 70, 70 ) ); okno.display(); Event menuEvent; short oneDisplayer = 0; while( true ) { Vector2i mposition = Mouse::getPosition( okno ); while( okno.pollEvent( menuEvent ) ) { if( menuEvent.type == Event::Closed ) { return Exit; } if( menuEvent.type == Event::MouseButtonPressed && menuEvent.mouseButton.button == Mouse::Left ) { cout << "Left Click!" << endl; if( playButton.HandleClick( okno ) ) { return Play; } else cout << "not" << endl; if( settingsButton.HandleClick( okno ) ) { return Settings; } else cout << "not" << endl; if( statsButton.HandleClick( okno ) ) { return Stats; } else cout << "not" << endl; if( exitButton.HandleClick( okno ) ) { return Exit; } else cout << "not" << endl; } }; if( playButton.InButtonArea( okno ) == true ) { menuP1.setFillColor( Color( 70, 70, 70, 70 ) ); okno.draw( menuP1 ); playButton.Draw( okno ); } else { menuP1.setFillColor( Color( 50, 50, 50, 70 ) ); okno.draw( menuP1 ); playButton.Draw( okno ); } if( settingsButton.InButtonArea( okno ) == true ) { menuP2.setFillColor( Color( 70, 70, 70, 70 ) ); okno.draw( menuP2 ); settingsButton.Draw( okno ); } else { menuP2.setFillColor( Color( 50, 50, 50, 70 ) ); okno.draw( menuP2 ); settingsButton.Draw( okno ); } if( statsButton.InButtonArea( okno ) == true ) { menuP3.setFillColor( Color( 70, 70, 70, 70 ) ); okno.draw( menuP3 ); statsButton.Draw( okno ); } else { menuP3.setFillColor( Color( 50, 50, 50, 70 ) ); okno.draw( menuP3 ); statsButton.Draw( okno ); } if( exitButton.InButtonArea( okno ) == true ) { menuP4.setFillColor( Color( 70, 70, 70, 70 ) ); okno.draw( menuP4 ); exitButton.Draw( okno ); } else { menuP4.setFillColor( Color( 50, 50, 50, 70 ) ); okno.draw( menuP4 ); exitButton.Draw( okno ); } okno.display(); } okno.display(); cout << "Wyszlismy" << endl; } Problem jest taki - mimo, iż z kodu wynika ze jak mysz jest w obszarze danego przycisku(w Rect arei) , to działa to tylko przy najechaniu na ostatni (Exit) przycisk. W sensie ze jak najezdzam na play,settings,stats -button to nic sie nie dzieje, a jak najade na exitButton to podswietlaja sie wszystkie przyciski, tak jakby funkcja InButtonArea, ktora wywoluje (w if'ie) po kolei dla kazdego przycisku if( settingsButton.InButtonArea( okno ) == true ) { } else {...} if( statsButton.InButtonArea( okno ) == true )
byla jedna, i za kazdym razem "nadpisywana". Czy to sie jakos "kojazy/miesza/tenteguje" w funkcjami virtualnymi? Jak to naprawic? |