From: NilsForssen Date: Mon, 11 Dec 2023 08:32:40 +0000 (+0100) Subject: fixed merge conflict and made functions smaller X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=0fb1bc015aca4955ccfbc9e377461cd89520e6e4;p=TDDC76_proj.git fixed merge conflict and made functions smaller --- 0fb1bc015aca4955ccfbc9e377461cd89520e6e4 diff --cc src/GameOver_menu.cc index 7bf2938,9838a9f..3a7cbaa --- a/src/GameOver_menu.cc +++ b/src/GameOver_menu.cc @@@ -176,25 -184,25 +184,14 @@@ void GameOver_menu::handle_input(sf::Ev switch (event.joystickButton.button) { case 0: // A -- if(menu_index == 1) -- { - load_to_csv(playerText); - load_to_csv(); -- menu = true; -- menu_index = 1; -- } -- -- else if(menu_index == 2) -- { -- exit_game = true; -- menu_index = 1; -- } ++ if_enter(); break; -- default: -- ++ ++ default: break; } -- break; ++ case sf::Event::JoystickMoved: switch(event.joystickMove.axis) { @@@ -202,115 -210,113 +199,69 @@@ case sf::Joystick::PovY: if (event.joystickMove.position <= -50) { -- if(menu_index == 1) -- { -- break; -- } -- -- else -- { -- menu_index -= 1; -- } ++ if_up(); break; } else if (event.joystickMove.position >= 50) { -- if(menu_index == 2) -- { -- break; -- } -- -- else -- { -- menu_index += 1; -- } ++ if_down(); break; } break; - + default: + break; } break; case sf::Event::KeyPressed: switch(event.key.code) { case sf::Keyboard::Enter: -- -- if(menu_index == 1) -- { - load_to_csv(playerText); - load_to_csv(); -- menu = true; -- menu_index = 1; -- } -- -- else if(menu_index == 2) -- { -- exit_game = true; -- menu_index = 1; -- } ++ if_enter(); break; -- -- case sf::Keyboard::Up: -- if(menu_index == 1) -- { -- break; -- } -- -- else -- { -- menu_index -= 1; -- } ++ if_up(); break; case sf::Keyboard::Down: -- if(menu_index == 2) -- { -- break; -- } -- -- else -- { -- menu_index += 1; -- } ++ if_down(); break; default: break; } ++ break; + ++ case sf::Event::TextEntered: + ++ text_input(event); break; default: break; } ++} -- // enter name -- if (event.type == sf::Event::TextEntered) ++void GameOver_menu::text_input(sf::Event& event) ++{ ++ // delete last charakter with backspace ++ if (sf::Keyboard::isKeyPressed( sf::Keyboard::Backspace )) { -- // delete last charakter with backspace -- if (sf::Keyboard::isKeyPressed( sf::Keyboard::Backspace )) ++ if(!playerInput.isEmpty()) { -- if(!playerInput.isEmpty()) -- { -- playerInput.erase(playerInput.getSize() - 1, 1); -- } -- -- else -- { -- return; -- } -- ++ playerInput.erase(playerInput.getSize() - 1, 1); } - // if not backspace, print letters - else if(playerInput.getSize() < 20) + - // if not backspace, print letters - else if(playerInput.getSize() < 20) ++ else { -- playerInput += event.text.unicode; ++ return; } - playerText.setString(playerInput); + - playerText.setString(playerInput); - } - - ++ // if not backspace, print letters ++ else if(playerInput.getSize() < 20) ++ { ++ playerInput += event.text.unicode; ++ } ++ playerText.setString(playerInput); } //spara namn till fil @@@ -348,4 -354,4 +299,46 @@@ void GameOver_menu::load_to_csv( std::ofstream highscore_file_w{data["game_constants"]["highscore_file"].get()}; for (std::string::size_type line_count{0}; line_count < lines_write.size(); highscore_file_w << lines_write[line_count++] << std::endl); highscore_file_w.close(); ++} ++ ++ ++void GameOver_menu::if_enter() ++{ ++ if(menu_index == 1) ++ { ++ load_to_csv(); ++ menu = true; ++ menu_index = 1; ++ } ++ ++ else if(menu_index == 2) ++ { ++ exit_game = true; ++ menu_index = 1; ++ } ++} ++ ++void GameOver_menu::if_up() ++{ ++ if(menu_index == 1) ++ { ++ return; ++ } ++ else ++ { ++ menu_index -= 1; ++ } ++} ++ ++void GameOver_menu::if_down() ++{ ++ if(menu_index == 2) ++ { ++ return; ++ } ++ ++ else ++ { ++ menu_index += 1; ++ } } diff --cc src/Pause_menu.cc index a8e8de5,7c14e63..a7d4971 --- a/src/Pause_menu.cc +++ b/src/Pause_menu.cc @@@ -117,23 -136,23 +136,7 @@@ void Pause_menu::handle_input(sf::Event switch (event.joystickButton.button) { case 0: // A -- if(menu_index == 1) -- { -- resume_game = true; -- menu_index = 1; -- } -- -- else if(menu_index == 2) -- { -- menu = true; -- menu_index = 1; -- } -- -- else if(menu_index == 3) -- { -- exit_game = true; -- menu_index = 1; -- } ++ if_enter(); break; default: @@@ -148,79 -167,81 +151,31 @@@ case sf::Joystick::PovY: if (event.joystickMove.position <= -50) { -- if(menu_index == 1) -- { -- break; -- } -- -- else -- { -- menu_index -= 1; -- } -- break; ++ if_up(); } else if (event.joystickMove.position >= 50) { -- if(menu_index == 3) -- { -- break; -- } -- -- else -- { -- menu_index += 1; -- } -- break; ++ if_down(); } break; + default: + break; } break; case sf::Event::KeyPressed: switch(event.key.code) { case sf::Keyboard::Enter: -- if(menu_index == 1) -- { -- resume_game = true; -- menu_index = 1; -- } -- -- else if(menu_index == 2) -- { -- menu = true; -- menu_index = 1; -- } -- -- else if(menu_index == 3) -- { -- exit_game = true; -- menu_index = 1; -- } ++ if_enter(); break; case sf::Keyboard::Up: -- if(menu_index == 1) -- { -- break; -- } -- -- else -- { -- menu_index -= 1; -- } ++ if_up(); break; case sf::Keyboard::Down: -- if(menu_index == 3) -- { -- break; -- } -- -- else -- { -- menu_index += 1; -- } ++ if_down(); break; default: @@@ -232,3 -253,3 +187,49 @@@ break; } } ++ ++void Pause_menu::if_enter() ++{ ++ if(menu_index == 1) ++ { ++ resume_game = true; ++ menu_index = 1; ++ } ++ ++ else if(menu_index == 2) ++ { ++ menu = true; ++ menu_index = 1; ++ } ++ ++ else if(menu_index == 3) ++ { ++ exit_game = true; ++ menu_index = 1; ++ } ++} ++ ++void Pause_menu::if_up() ++{ ++ if(menu_index == 1) ++ { ++ return; ++ } ++ else ++ { ++ menu_index -= 1; ++ } ++} ++ ++void Pause_menu::if_down() ++{ ++ if(menu_index == 3) ++ { ++ return; ++ } ++ ++ else ++ { ++ menu_index += 1; ++ } ++} diff --cc src/Scoreboard_menu.cc index f2b6b47,0c234de..242502d --- a/src/Scoreboard_menu.cc +++ b/src/Scoreboard_menu.cc @@@ -130,24 -148,24 +148,14 @@@ void Scoreboard_menu::handle_input(sf:: switch (event.joystickButton.button) { case 0: // A -- if(menu_index == 1) -- { -- menu = true; -- menu_index = 1; -- } -- -- else if(menu_index == 2) -- { -- exit_game = true; -- menu_index = 1; -- } ++ if_enter(); break; -- default: -- ++ ++ default: break; } -- break; ++ case sf::Event::JoystickMoved: switch(event.joystickMove.axis) { @@@ -155,28 -173,28 +163,12 @@@ case sf::Joystick::PovY: if (event.joystickMove.position <= -50) { -- if(menu_index == 1) -- { -- break; -- } -- -- else -- { -- menu_index -= 1; -- } ++ if_up(); break; } else if (event.joystickMove.position >= 50) { -- if(menu_index == 3) -- { -- break; -- } -- -- else -- { -- menu_index += 1; -- } ++ if_down(); break; } break; @@@ -186,43 -206,43 +180,16 @@@ switch(event.key.code) { case sf::Keyboard::Enter: -- -- if(menu_index == 1) -- { -- menu = true; -- menu_index = 1; -- } -- -- else if(menu_index == 2) -- { -- exit_game = true; -- menu_index = 1; -- } ++ if_enter(); break; case sf::Keyboard::Up: -- if(menu_index == 1) -- { -- break; -- } -- -- else -- { -- menu_index -= 1; -- } ++ if_up(); break; case sf::Keyboard::Down: -- if(menu_index == 2) -- { -- break; -- } -- -- else -- { -- menu_index += 1; -- } ++ if_down(); break; default: @@@ -234,3 -254,3 +201,44 @@@ break; } } ++ ++ ++void Scoreboard_menu::if_enter() ++{ ++ if(menu_index == 1) ++ { ++ menu = true; ++ menu_index = 1; ++ } ++ ++ else if(menu_index == 2) ++ { ++ exit_game = true; ++ menu_index = 1; ++ } ++} ++ ++void Scoreboard_menu::if_up() ++{ ++ if(menu_index == 1) ++ { ++ return; ++ } ++ else ++ { ++ menu_index -= 1; ++ } ++} ++ ++void Scoreboard_menu::if_down() ++{ ++ if(menu_index == 2) ++ { ++ return; ++ } ++ ++ else ++ { ++ menu_index += 1; ++ } ++} diff --cc src/States.h index 4e7a834,77f8bd3..2b050bb --- a/src/States.h +++ b/src/States.h @@@ -71,13 -69,17 +73,21 @@@ public void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; + + private: + void if_enter(); + void if_up(); + void if_down(); + - private: + bool start_game; + bool exit_game; + bool scoreboard; + + int menu_index; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; sf::Sprite mouse_l; sf::Sprite mouse_r; @@@ -103,8 -102,15 +110,19 @@@ public void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; private: ++ void if_enter(); ++ void if_up(); ++ void if_down(); ++ + int menu_index; + + bool resume_game; + bool exit_game; + bool menu; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; sf::Sprite mouse_l; sf::Sprite mouse_r; @@@ -130,9 -133,15 +145,19 @@@ public void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; -private: +private: ++ void if_enter(); ++ void if_up(); ++ void if_down(); ++ + int menu_index; + + bool exit_game; + bool menu; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; sf::Sprite mouse_l; sf::Sprite mouse_r; @@@ -157,16 -165,26 +181,31 @@@ public void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; - void load_to_csv(sf::Text playerText); - void load_to_csv(); private: ++ void load_to_csv(); ++ void if_enter(); ++ void if_up(); ++ void if_down(); ++ void text_input(sf::Event& event); ++ + int menu_index; + int points; + + bool exit_game; + bool menu; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; - sf::CircleShape rectangle; sf::Sprite mouse_l; sf::Sprite mouse_r; + + sf::CircleShape rectangle; + sf::String playerInput; + sf::Text playerText; sf::Text linetext; sf::Text funfacttext;