diff --git a/application.cpp b/application.cpp index 9092de27083c0c30f7251aeae84764bf4d6e1360..02f66d315e2a47c7f0e4b1414267b3ce066e4b08 100644 --- a/application.cpp +++ b/application.cpp @@ -14,22 +14,9 @@ Application::Application(int szel, int mag) void Application::event_loop() { - Index focus(0,0); event ev; - widgets[focus_index]->kezel(ev); - - std::set<int> aura_halmaz = _game->aura(focus); // azert van itt is, hogy a megnyitaskor is rajzoljon aurat - for (int i=0; i<widgets.size(); i++) - { - if (aura_halmaz.find(i) != aura_halmaz.end()) - { - action("aura",i,1); - } - else - { - action("aura",i,0); - } - } + int focus = action("start", ev, 0); + widgets[focus]->kezel(ev); for (Widget *w : widgets) { @@ -37,72 +24,23 @@ void Application::event_loop() } gout << refresh; - while(gin>>ev) + while(gin >> ev) { - if (ev.type==ev_key) + if (ev.type==ev_mouse) { - if (ev.keycode==key_up || ev.keycode==key_down || - ev.keycode==key_left || ev.keycode==key_right) - { - widgets[focus_index]->unfocus(); - - if (ev.keycode==key_up) - { - if (focus.sor >0) - focus.sor -= 1; - } - else if (ev.keycode==key_down) - { - if (focus.sor <8) - focus.sor += 1; - } - else if (ev.keycode==key_left) - { - if (focus.oszlop >0) - focus.oszlop -= 1; - } - else if (ev.keycode==key_right) - { - if (focus.oszlop <8) - focus.oszlop += 1; - } - - std::set<int> aura_halmaz = _game->aura(focus); - // std::set<int> aura_ertekek; - - for (int i=0; i<widgets.size(); i++) - { - if (aura_halmaz.find(i) != aura_halmaz.end()) - { - action("aura",i,1); - - } - else - { - action("aura",i,0); - } - } - - widgets[focus_index]->kezel(ev); - } + focus = action("mouse", ev, focus); + } - if (ev.keycode >= 49 && ev.keycode <= 57) - { - action("ertek",focus_index, ev.keycode-48); - } + if (ev.type==ev_key) + { + focus = action("key", ev, focus); - if (ev.keycode==key_delete || ev.keycode==key_backspace) - { - action("ertek",focus_index, 0); - } } - - for (Widget *w : widgets) + for (Widget* w : widgets) { w->rajzol(); } gout << refresh; - _game->save_fajlba(); } } diff --git a/application.hpp b/application.hpp index 9b664d3b384e3d626c9a0c7188239e5d4c18de99..b934e0f1ace7759aa51b44f2ed2febfe03ab460a 100644 --- a/application.hpp +++ b/application.hpp @@ -25,6 +25,19 @@ struct Index oszlop=0; } int sor,oszlop; + + Index(int sorszam) : sor(sorszam/9), oszlop(sorszam%9) + { + if (sor>8) + sor=8; + else if (sor<0) + sor=0; + + if (oszlop>8) + oszlop=8; + else if (oszlop<0) + oszlop=0; + } }; class Application @@ -35,11 +48,12 @@ class Application virtual void event_loop(); virtual void register_widget(Widget*); virtual void register_gm(GameMaster*); - virtual void action(std::string,int,int) = 0; + virtual int action(std::string id, genv::event,int) = 0; protected: std::vector<Widget*> widgets; GameMaster *_game; + int _focus; }; #endif // APPLICATION_HPP diff --git a/gamemaster.cpp b/gamemaster.cpp index 15d814b0de55b7c39252db426b0657806d374fda..f826558a1a176a5e3858a2ea7b1b78417590513b 100644 --- a/gamemaster.cpp +++ b/gamemaster.cpp @@ -28,8 +28,9 @@ GameMaster::GameMaster(Application* parent, std::string mo, std::string fe) : } -set<int> GameMaster::aura(Index jelenlegi) +set<int> GameMaster::aura(int jelenlegi_int) { + Index jelenlegi(jelenlegi_int); set<int> s; int negyzet=negyzet_szama(jelenlegi); diff --git a/gamemaster.hpp b/gamemaster.hpp index 88cbbbc209662f0c4023bb04cb2e6835aa49086f..8127f965bef1d254e18ed80575af59f160ed6b8f 100644 --- a/gamemaster.hpp +++ b/gamemaster.hpp @@ -20,7 +20,7 @@ class GameMaster std::vector<int> get_save(); virtual void save(int,int); - std::set<int> aura(Index); + std::set<int> aura(int); virtual int negyzet_szama(Index); virtual bool szabalyos_e(int,std::set<int>); diff --git a/main.cpp b/main.cpp index eeb7ce945ff3c34b3a4f2f91796e252575e02d20..e389fd14bd4490e92cb898633f51d9860c442151 100644 --- a/main.cpp +++ b/main.cpp @@ -1,59 +1,4 @@ -#include "graphics.hpp" -#include "application.hpp" -#include "widget.hpp" -#include "field.hpp" -#include "gamemaster.hpp" - -#include <vector> - -#define sorszam_i 9*j+i - -using namespace std; - -class MyApp : public Application -{ - public: - MyApp(int szel, int mag) : Application(szel,mag) - { - game = new GameMaster(this,"megoldas.txt","feladat.txt"); - vector<int> game_feladat = game->get_feladat(); - vector<int> game_save = game->get_save(); - - for (int j=0; j<9; j++) // sor - { - for (int i=0; i<9; i++) // oszlop - { - Field *f = new Field(this,i*78,j*78,78,sorszam_i,game_feladat[sorszam_i]); - palya.push_back(f); - if (game_feladat[sorszam_i] == 0) - { - palya[sorszam_i]->set_ertek(game_save[sorszam_i]); - } - } - } - } - - void action(std::string id, int sorszam, int ertek) override - { - if (id=="ertek") - { - palya[sorszam]->set_ertek(ertek); - game->save(sorszam,ertek); - } - - if (id=="aura") - { - if (ertek==1) - palya[sorszam]->set_aura_true(); - else if (ertek==0) - palya[sorszam]->set_aura_false(); - } - } - - protected: - vector<Field*> palya; - GameMaster* game; -}; +#include "myapp.hpp" int main() { diff --git a/myapp.cpp b/myapp.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73f058743a50f5f9b36445097f31190f7d037906 --- /dev/null +++ b/myapp.cpp @@ -0,0 +1,139 @@ +#include "myapp.hpp" + +#define sorszam 9*sorszam_index.sor+sorszam_index.oszlop +#define focus_visszavaltas 9*focus_sorszam.sor+focus_sorszam.oszlop + +using namespace std; +using namespace genv; + +MyApp::MyApp(int szel, int mag) : Application(szel,mag) +{ + _game = new GameMaster(this,"megoldas.txt","feladat.txt"); + vector<int> game_feladat = _game->get_feladat(); + vector<int> game_save = _game->get_save(); + + for (int j=0; j<9; j++) // sor + { + for (int i=0; i<9; i++) // oszlop + { + Index sorszam_index(j,i); + Field *f = new Field(this,i*78,j*78,78,sorszam,game_feladat[sorszam]); + palya.push_back(f); + if (game_feladat[sorszam] == 0) + { + palya[sorszam]->set_ertek(game_save[sorszam]); + } + } + } +} + +int MyApp::action(std::string id, genv::event ev, int focus) +{ + if (id=="start") + { + std::set<int> aura_halmaz = _game->aura(focus); // azert van itt is, hogy a megnyitaskor is rajzoljon aurat + + for (int i=0; i<palya.size(); i++) + { + if (aura_halmaz.find(i) != aura_halmaz.end()) + { + palya[i]->set_aura_true(); + } + else + { + palya[i]->set_aura_false(); + } + } + } + + if (id=="key") + { + if (ev.keycode==key_up || ev.keycode==key_down || + ev.keycode==key_left || ev.keycode==key_right) + { + palya[focus]->unfocus(); + + Index focus_sorszam(focus); + + if (ev.keycode==key_up) + { + if (focus_sorszam.sor >0) + focus_sorszam.sor -= 1; + } + else if (ev.keycode==key_down) + { + if (focus_sorszam.sor <8) + focus_sorszam.sor += 1; + } + else if (ev.keycode==key_left) + { + if (focus_sorszam.oszlop >0) + focus_sorszam.oszlop -= 1; + } + else if (ev.keycode==key_right) + { + if (focus_sorszam.oszlop <8) + focus_sorszam.oszlop += 1; + } + focus = focus_visszavaltas; + palya[focus]->kezel(ev); + + std::set<int> aura_halmaz = _game->aura(focus); + + for (int i=0; i<palya.size(); i++) + { + if (aura_halmaz.find(i) != aura_halmaz.end()) + { + palya[i]->set_aura_true(); + } + else + { + palya[i]->set_aura_false(); + } + } + } + + if (ev.keycode >= 49 && ev.keycode <= 57) + { + palya[focus]->set_ertek(ev.keycode-48); + } + + if (ev.keycode==key_delete || ev.keycode==key_backspace) + { + palya[focus]->set_ertek(0); + } + + _game->save(focus,palya[focus]->get_ertek()); + } + + if (id=="mouse") + { + if (ev.button==btn_left) + { + for (size_t i=0; i<palya.size(); i++) + { + if (palya[i]->belul(ev.pos_x,ev.pos_y)) + { + palya[focus]->unfocus(); + focus=i; + palya[focus]->kezel(ev); + } + } + + std::set<int> aura_halmaz = _game->aura(focus); + + for (int i=0; i<palya.size(); i++) + { + if (aura_halmaz.find(i) != aura_halmaz.end()) + { + palya[i]->set_aura_true(); + } + else + { + palya[i]->set_aura_false(); + } + } + } + } + return focus; +} diff --git a/myapp.hpp b/myapp.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e25ca75a6c33e3d983d9ff4bdfce145a83a8eb89 --- /dev/null +++ b/myapp.hpp @@ -0,0 +1,20 @@ +#ifndef MYAPP_HPP +#define MYAPP_HPP + +#include "application.hpp" +#include "widget.hpp" +#include "field.hpp" +#include "gamemaster.hpp" +#include <vector> + +class MyApp : public Application +{ + public: + MyApp(int,int); + int action(std::string,genv::event,int) override; + + protected: + std::vector<Field*> palya; +}; + +#endif // MYAPP_HPP