From b13e9ef12cf4b04be6b11896389d431affc2f036 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Mon, 9 Jun 2014 18:24:00 +0300 Subject: [PATCH] Clean up window-fileupload.cpp --- include/core/ui-services.hpp | 13 +++++++ src/core/ui-services.cpp | 39 ++++++++++++++++++++ src/platform/wxwidgets/window-fileupload.cpp | 29 +++++---------- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/include/core/ui-services.hpp b/include/core/ui-services.hpp index d3a94437..8adae955 100644 --- a/include/core/ui-services.hpp +++ b/include/core/ui-services.hpp @@ -6,6 +6,7 @@ #include #include #include +#include /********************************************************************************************************************* UI services. @@ -115,5 +116,17 @@ void UI_end_dump(emulator_instance& inst, dumper_factory_base& factory); */ void UI_do_keypress(emulator_instance& inst, const keyboard::modifier_set& mods, keyboard::key_key& key, bool polarity); +/** + * Is there a valid movie? + */ +bool UI_has_movie(emulator_instance& inst); +/** + * Save a movie into buffer. + */ +void UI_save_movie(emulator_instance& inst, std::ostringstream& buffer); +/** + * Look up (platform,game) pair. + */ +std::pair UI_lookup_platform_and_game(emulator_instance& inst); #endif diff --git a/src/core/ui-services.cpp b/src/core/ui-services.cpp index 853402a8..82559277 100644 --- a/src/core/ui-services.cpp +++ b/src/core/ui-services.cpp @@ -5,6 +5,7 @@ #include "core/moviedata.hpp" #include "core/project.hpp" #include "core/queue.hpp" +#include "core/rom.hpp" #include "core/ui-services.hpp" #include "library/keyboard.hpp" @@ -213,3 +214,41 @@ void UI_do_keypress(emulator_instance& inst, const keyboard::modifier_set& mods, _key->set_state(mods, polarity ? 1 : 0); }, [](std::exception& e) {}); } + +bool UI_has_movie(emulator_instance& inst) +{ + bool ret = false; + lsnes_instance.iqueue->run([&inst, &ret]() { + ret = !!*inst.mlogic && !inst.rom->isnull(); + }); + return ret; +} + +void UI_save_movie(emulator_instance& inst, std::ostringstream& stream) +{ + lsnes_instance.iqueue->run([&inst, &stream]() { + inst.mlogic->get_mfile().is_savestate = false; + auto prj = inst.project->get(); + if(prj) { + inst.mlogic->get_mfile().gamename = prj->gamename; + inst.mlogic->get_mfile().authors = prj->authors; + } + inst.mlogic->get_mfile().active_macros.clear(); + inst.mlogic->get_mfile().save(stream, inst.mlogic->get_rrdata()); + }); +} + +std::pair UI_lookup_platform_and_game(emulator_instance& inst) +{ + std::string plat; + std::string game; + lsnes_instance.iqueue->run([&inst, &plat, &game]() { + auto prj = inst.project->get(); + if(prj) + game = prj->gamename; + else + game = inst.mlogic->get_mfile().gamename; + plat = lookup_sysregion_mapping(inst.mlogic->get_mfile().gametype->get_name()); + }); + return std::make_pair(plat, game); +} diff --git a/src/platform/wxwidgets/window-fileupload.cpp b/src/platform/wxwidgets/window-fileupload.cpp index a7346f0c..67899e78 100644 --- a/src/platform/wxwidgets/window-fileupload.cpp +++ b/src/platform/wxwidgets/window-fileupload.cpp @@ -7,6 +7,7 @@ #include "core/rom.hpp" #include "core/project.hpp" #include "core/moviedata.hpp" +#include "core/ui-services.hpp" #include "library/directory.hpp" #include "library/skein.hpp" #include "library/zip.hpp" @@ -638,7 +639,7 @@ wxeditor_uploaddialog::wxeditor_uploaddialog(wxWindow* parent, emulator_instance wxCommandEventHandler(wxeditor_uploaddialog::on_source_sel), NULL, this); file->Connect(wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler(wxeditor_uploaddialog::on_source_sel), NULL, this); - if(!inst.mlogic || inst.rom->isnull()) { + if(!UI_has_movie(inst)) { current->Enable(false); file->SetValue(true); } @@ -751,15 +752,8 @@ void wxeditor_uploaddialog::on_ok(wxCommandEvent& e) } else { if(fn.length() < 6 || fn.substr(fn.length() - 5) != ".lsmv") filename->SetValue(towxstring(fn + ".lsmv")); - inst.mlogic->get_mfile().is_savestate = false; - auto prj = inst.project->get(); - if(prj) { - inst.mlogic->get_mfile().gamename = prj->gamename; - inst.mlogic->get_mfile().authors = prj->authors; - } - inst.mlogic->get_mfile().active_macros.clear(); std::ostringstream stream; - inst.mlogic->get_mfile().save(stream, inst.mlogic->get_rrdata()); + UI_save_movie(inst, stream); std::string _stream = stream.str(); content = std::vector(_stream.begin(), _stream.end()); } @@ -781,15 +775,9 @@ void wxeditor_uploaddialog::on_source_sel(wxCommandEvent& e) file_select->Enable(file->GetValue()); if(!games_req) { if(current->GetValue()) { - std::string curgame; - auto prj = inst.project->get(); - if(prj) - curgame = prj->gamename; - else - curgame = inst.mlogic->get_mfile().gamename; - - std::string plat = lookup_sysregion_mapping( - inst.mlogic->get_mfile().gametype->get_name()) + " "; + auto g = UI_lookup_platform_and_game(inst); + std::string plat = g.first + " "; + std::string curgame = g.second; size_t platlen = plat.length(); std::string c = tostdstring(game->GetLabel()); std::string fullname = plat + curgame; @@ -830,8 +818,9 @@ void wxeditor_uploaddialog::on_game_sel(wxCommandEvent& e) { auto pos = game_sel_button->GetScreenPosition(); std::string system; - if(current->GetValue()) - system = lookup_sysregion_mapping(inst.mlogic->get_mfile().gametype->get_name()); + if(current->GetValue()) { + system = UI_lookup_platform_and_game(inst).first; + } wxwin_gameselect* gs = new wxwin_gameselect(this, games_list, tostdstring(game->GetLabel()), system, pos.x, pos.y); if(gs->ShowModal() != wxID_OK) {