Wxwidgets: Basic subtitles editor

This commit is contained in:
Ilari Liusvaara 2012-09-08 20:29:44 +03:00
parent 7af494b92c
commit 2368c17ece
5 changed files with 176 additions and 0 deletions

View file

@ -19,6 +19,9 @@ private:
bool position_only;
};
std::set<std::pair<uint64_t, uint64_t>> get_subtitles();
std::string get_subtitle_for(uint64_t f, uint64_t l);
void set_subtitle_for(uint64_t f, uint64_t l, const std::string& x);
std::string s_unescape(std::string x);
std::string s_escape(std::string x);
void render_subtitles(lua_render_context& ctx);

View file

@ -38,6 +38,7 @@ void show_projectwindow(wxWindow* modwin);
void wxeditor_authors_display(wxWindow* parent);
void wxeditor_hotkeys_display(wxWindow* parent);
void wxeditor_memorywatch_display(wxWindow* parent);
void wxeditor_subtitles_display(wxWindow* parent);
std::string wxeditor_keyselect(wxWindow* parent, bool clearable);
void wxsetingsdialog_display(wxWindow* parent, bool hotkeys_only);
void open_rom_select_window();

View file

@ -158,4 +158,28 @@ void render_subtitles(lua_render_context& ctx)
}
}
std::set<std::pair<uint64_t, uint64_t>> get_subtitles()
{
std::set<std::pair<uint64_t, uint64_t>> r;
for(auto i = our_movie.subtitles.rbegin(); i != our_movie.subtitles.rend(); i++)
r.insert(std::make_pair(i->first.get_frame(), i->first.get_length()));
return r;
}
std::string get_subtitle_for(uint64_t f, uint64_t l)
{
moviefile_subtiming key(f, l);
if(!our_movie.subtitles.count(key))
return "";
else
return s_escape(our_movie.subtitles[key]);
}
void set_subtitle_for(uint64_t f, uint64_t l, const std::string& x)
{
moviefile_subtiming key(f, l);
if(x == "")
our_movie.subtitles.erase(key);
else
our_movie.subtitles[key] = s_unescape(x);
}

View file

@ -0,0 +1,143 @@
#include "core/subtitles.hpp"
#include "core/moviedata.hpp"
#include "platform/wxwidgets/platform.hpp"
#include <wx/wx.h>
#include <wx/event.h>
#include <wx/control.h>
#include <wx/combobox.h>
#include <wx/radiobut.h>
#include "library/string.hpp"
#include "core/emucore.hpp"
class wxeditor_subtitles : public wxDialog
{
public:
wxeditor_subtitles(wxWindow* parent);
bool ShouldPreventAppExit() const;
void on_subtitles_change(wxCommandEvent& e);
void on_ok(wxCommandEvent& e);
void on_cancel(wxCommandEvent& e);
private:
wxTextCtrl* subs;
wxButton* ok;
wxButton* cancel;
};
wxeditor_subtitles::wxeditor_subtitles(wxWindow* parent)
: wxDialog(parent, wxID_ANY, wxT("lsnes: Edit subtitles"), wxDefaultPosition, wxSize(-1, -1))
{
Centre();
wxFlexGridSizer* top_s = new wxFlexGridSizer(2, 1, 0, 0);
SetSizer(top_s);
top_s->Add(subs = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, 300),
wxTE_MULTILINE), 1, wxGROW);
subs->Connect(wxEVT_COMMAND_TEXT_UPDATED,
wxCommandEventHandler(wxeditor_subtitles::on_subtitles_change), NULL, this);
wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL);
pbutton_s->AddStretchSpacer();
pbutton_s->Add(ok = new wxButton(this, wxID_ANY, wxT("OK")), 0, wxGROW);
pbutton_s->Add(cancel = new wxButton(this, wxID_ANY, wxT("Cancel")), 0, wxGROW);
ok->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxeditor_subtitles::on_ok), NULL, this);
cancel->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxeditor_subtitles::on_cancel), NULL,
this);
top_s->Add(pbutton_s, 0, wxGROW);
pbutton_s->SetSizeHints(this);
top_s->SetSizeHints(this);
Fit();
std::string txt = "";
runemufn([&txt]() {
for(auto i : get_subtitles()) {
std::ostringstream line;
line << i.first << " " << i.second << " " << get_subtitle_for(i.first, i.second) << std::endl;
txt = txt + line.str();
}
});
subs->SetValue(towxstring(txt));
}
bool wxeditor_subtitles::ShouldPreventAppExit() const
{
return false;
}
void wxeditor_subtitles::on_subtitles_change(wxCommandEvent& e)
{
std::string txt = tostdstring(subs->GetValue());
std::string line;
while(txt != "") {
extract_token(txt, line, "\n");
istrip_CR(line);
if(line == "")
continue;
auto r = regex("([0-9]+)[ \t]+([0-9]+)[ \t]+(.+)", line);
if(!r) {
ok->Disable();
return;
}
try {
parse_value<uint64_t>(r[1]);
parse_value<uint64_t>(r[2]);
} catch(...) {
ok->Disable();
return;
}
}
ok->Enable();
}
void wxeditor_subtitles::on_ok(wxCommandEvent& e)
{
std::map<std::pair<uint64_t, uint64_t>, std::string> data;
runemufn([&data]() {
for(auto i : get_subtitles())
data[std::make_pair(i.first, i.second)] = "";
});
std::string txt = tostdstring(subs->GetValue());
std::string line;
while(txt != "") {
extract_token(txt, line, "\n");
istrip_CR(line);
if(line == "")
continue;
auto r = regex("([0-9]+)[ \t]+([0-9]+)[ \t]+(.+)", line);
if(!r)
return;
try {
uint64_t f = parse_value<uint64_t>(r[1]);
uint64_t l = parse_value<uint64_t>(r[2]);
data[std::make_pair(f, l)] = r[3];
} catch(...) {
return;
}
}
runemufn([&data]() {
for(auto i : data)
set_subtitle_for(i.first.first, i.first.second, i.second);
});
EndModal(wxID_OK);
}
void wxeditor_subtitles::on_cancel(wxCommandEvent& e)
{
EndModal(wxID_CANCEL);
}
void wxeditor_subtitles_display(wxWindow* parent)
{
modal_pause_holder hld;
wxDialog* editor;
try {
editor = new wxeditor_subtitles(parent);
editor->ShowModal();
} catch(...) {
}
editor->Destroy();
}

View file

@ -69,6 +69,7 @@ enum
wxID_EDIT_MEMORYWATCH,
wxID_SAVE_MEMORYWATCH,
wxID_LOAD_MEMORYWATCH,
wxID_EDIT_SUBTITLES,
wxID_DUMP_FIRST,
wxID_DUMP_LAST = wxID_DUMP_FIRST + 1023,
wxID_REWIND_MOVIE,
@ -734,6 +735,7 @@ wxwin_mainwindow::wxwin_mainwindow()
menu_entry_check(wxID_READONLY_MODE, wxT("Readonly mode"));
menu_check(wxID_READONLY_MODE, is_readonly_mode());
menu_entry(wxID_EDIT_AUTHORS, wxT("Edit game name && authors..."));
menu_entry(wxID_EDIT_SUBTITLES, wxT("Edit subtitles..."));
menu_separator();
menu_entry(wxID_REWIND_MOVIE, wxT("Rewind to start"));
@ -944,6 +946,9 @@ void wxwin_mainwindow::handle_menu_click_cancelable(wxCommandEvent& e)
case wxID_EDIT_AUTHORS:
wxeditor_authors_display(this);
return;
case wxID_EDIT_SUBTITLES:
wxeditor_subtitles_display(this);
return;
case wxID_EDIT_MEMORYWATCH:
wxeditor_memorywatch_display(this);
return;