From f0c3f7b61c47d4a7af9537bad8bfda293e09921b Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Thu, 22 Sep 2011 14:54:00 +0300 Subject: [PATCH] Use dedicated callbacks for mouse/close, not commands --- SDL/window-sdl.cpp | 14 ++++++------- generic/mainloop.cpp | 55 +++++++++++++++++++++++++++++++++++----------------- generic/window.cpp | 31 +++++++++++++++++++++++++++++ generic/window.hpp | 29 +++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 25 deletions(-) diff --git a/SDL/window-sdl.cpp b/SDL/window-sdl.cpp index be42730f..d226c179 100644 --- a/SDL/window-sdl.cpp +++ b/SDL/window-sdl.cpp @@ -948,7 +948,7 @@ namespace return; } if(e.type == SDL_QUIT) { - command::invokeC("quit-emulator"); + window_callback::do_close(); state = WINSTATE_NORMAL; return; } @@ -978,11 +978,7 @@ namespace else mouse_mask &= ~4; } - { - std::ostringstream x; - x << "mouse_button " << xc << " " << yc << " " << mouse_mask; - command::invokeC(x.str()); - } + window_callback::do_click(xc, yc, mouse_mask); } if(e.type == SDL_KEYDOWN && key == SDLK_ESCAPE) return; @@ -1170,7 +1166,7 @@ bool window::modal_message(const std::string& msg, bool confirm) throw(std::bad_ bool ret = modconfirm; if(delayed_close_flag) { delayed_close_flag = false; - command::invokeC("quit-emulator"); + window_callback::do_close(); } return ret; } @@ -1364,6 +1360,10 @@ void poll_inputs_internal() throw(std::bad_alloc) while(state != WINSTATE_NORMAL) { if(SDL_WaitEvent(&e)) do_event(e); + if(delayed_close_flag) { + state = WINSTATE_NORMAL; + return; + } } } diff --git a/generic/mainloop.cpp b/generic/mainloop.cpp index 41b543c2..61b34cab 100644 --- a/generic/mainloop.cpp +++ b/generic/mainloop.cpp @@ -780,24 +780,6 @@ namespace while(1); }); - function_ptr_command mouse_button_handler("mouse_button", "no description available", - "No help available\n", - [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { - std::string x = t; - std::string y = t; - std::string b = t; - int _x = atoi(x.c_str()); - int _y = atoi(y.c_str()); - int _b = atoi(b.c_str()); - if(_b & ~prev_mouse_mask & 1) - send_analog_input(_x, _y, 0); - if(_b & ~prev_mouse_mask & 2) - send_analog_input(_x, _y, 1); - if(_b & ~prev_mouse_mask & 4) - send_analog_input(_x, _y, 2); - prev_mouse_mask = _b; - }); - class button_action : public command { public: @@ -867,6 +849,42 @@ namespace } } bah; + bool on_quit_prompt = false; + class mywindowcallbacks : public window_callback + { + public: + void on_close() throw() + { + if(on_quit_prompt) { + amode = ADVANCE_QUIT; + window::paused(false); + window::cancel_wait(); + return; + } + on_quit_prompt = true; + try { + if(window::modal_message("Really quit?", true)) { + amode = ADVANCE_QUIT; + window::paused(false); + window::cancel_wait(); + } + } catch(...) { + } + on_quit_prompt = false; + } + + void on_click(int32_t x, int32_t y, uint32_t buttonmask) throw() + { + if(buttonmask & ~prev_mouse_mask & 1) + send_analog_input(x, y, 0); + if(buttonmask & ~prev_mouse_mask & 2) + send_analog_input(x, y, 1); + if(buttonmask & ~prev_mouse_mask & 4) + send_analog_input(x, y, 2); + prev_mouse_mask = buttonmask; + } + } mywcb; + //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load //failing. int handle_load() @@ -987,6 +1005,7 @@ void main_loop(struct loaded_rom& rom, struct moviefile& initial, bool load_has_ SNES::system.interface = &intrf; status = &window::get_emustatus(); autofire_pattern.push_back(controls_t()); + window_callback::set_callback_handler(mywcb); //Load our given movie. bool first_round = false; diff --git a/generic/window.cpp b/generic/window.cpp index 9f76a28f..b0cb9bee 100644 --- a/generic/window.cpp +++ b/generic/window.cpp @@ -48,6 +48,8 @@ namespace protected: std::vector stream; }; + + window_callback* wcb = NULL; } std::ostream& window::out() throw(std::bad_alloc) @@ -58,3 +60,32 @@ std::ostream& window::out() throw(std::bad_alloc) cached = new boost::iostreams::stream(win); return *cached; } + +window_callback::~window_callback() throw() +{ +} + +void window_callback::on_close() throw() +{ +} + +void window_callback::on_click(int32_t x, int32_t y, uint32_t buttonmask) throw() +{ +} + +void window_callback::do_close() throw() +{ + if(wcb) + wcb->on_close(); +} + +void window_callback::do_click(int32_t x, int32_t y, uint32_t buttonmask) throw() +{ + if(wcb) + wcb->on_click(x, y, buttonmask); +} + +void window_callback::set_callback_handler(window_callback& cb) throw() +{ + wcb = &cb; +} diff --git a/generic/window.hpp b/generic/window.hpp index 2d269f63..a758625d 100644 --- a/generic/window.hpp +++ b/generic/window.hpp @@ -15,6 +15,35 @@ class window; /** + * Some backnotifications. + */ +class window_callback +{ +public: + virtual ~window_callback() throw(); +/** + * Called when user tries to close the window. + */ + virtual void on_close() throw(); +/** + * Called when user clicks on the screen. + */ + virtual void on_click(int32_t x, int32_t y, uint32_t buttonmask) throw(); +/** + * Do try to close the window. + */ + static void do_close() throw(); +/** + * Do click on the screen. + */ + static void do_click(int32_t x, int32_t y, uint32_t buttonmask) throw(); +/** + * Set the callback handler. + */ + static void set_callback_handler(window_callback& cb) throw(); +}; + +/** * This is a handle to graphics system. Note that creating multiple contexts produces undefined results. */ class window -- 2.11.4.GIT