speciesgen/src/gui/main_panel.cpp

210 lines
6.3 KiB
C++

/*
speciesgen
Copyright (C) 2022-2023 prisixia
This file is part of speciesgen.
speciesgen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
speciesgen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with speciesgen. If not, see <https://www.gnu.org/licenses/>.
*/
#include "gui/main_panel.h"
namespace SpeciesGen {
wxBEGIN_EVENT_TABLE(MainPanel, wxPanel)
#if wxUSE_DIRDLG
EVT_BUTTON(DIR_CHOOSE, MainPanel::DoChooseDir)
#endif // wxUSE_DIRDLG
wxEND_EVENT_TABLE()
// NOLINTBEGIN(cppcoreguidelines-owning-memory): wxWidgets already manages its
// memory more or less so that there's no need for owning the memory
MainPanel::MainPanel(wxWindow *window, int x, int y, int w, int h,
const std::string &handlerName)
: wxPanel(window, wxID_ANY, wxPoint(x, y), wxSize(w, h)),
ErrorHandler(handlerName) {
wxBoxSizer *column1 = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *column2 = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *row = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
m_authorText = new wxStaticText(this, wxID_ANY, "Author");
m_nameText = new wxStaticText(this, wxID_ANY, "Species (Internal)");
m_friendlyNameText = new wxStaticText(this, wxID_ANY, "Species (In-Game)");
m_modDestinationText = new wxStaticText(this, wxID_ANY, "Output directory");
m_author = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition,
wxSize(150, wxDefaultSize.GetHeight()));
m_author->SetHint("Author's name");
m_author->SetMaxLength(128);
#ifdef _WIN32
m_author->SetValue(wxGetUserName());
#else
m_author->SetValue(getenv("USER"));
#endif // _WIN32
m_name = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition,
wxSize(150, wxDefaultSize.GetHeight()));
m_name->SetHint("Internal species name");
m_name->SetMaxLength(128);
m_friendlyName =
new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition,
wxSize(150, wxDefaultSize.GetHeight()));
m_friendlyName->SetHint("In-Game species name");
m_friendlyName->SetMaxLength(128);
#ifdef _WIN32
m_modDestination = new wxTextCtrl(
this, wxID_ANY,
"C:\\Program Files (x86)\\Steam\\steamapps\\common\\Starbound\\mods",
wxDefaultPosition, wxSize(400, wxDefaultSize.GetHeight()));
#else
m_modDestination = new wxTextCtrl(
this, wxID_ANY,
wxString::Format("%s/.steam/steam/steamapps/common/Starbound/mods",
wxGetUserHome(wxGetUserName())));
#endif // _WIN32
m_generate = new wxButton(this, MOD_GENERATE, "Generate mod",
wxDefaultPosition, wxSize(280, 80));
column1->Add(m_authorText, 0, wxEXPAND | wxLEFT | wxUP, 10);
column1->Add(m_author, 0, wxEXPAND | wxALL, 10);
column1->Add(m_nameText, 0, wxEXPAND | wxLEFT | wxUP, 10);
column1->Add(m_name, 0, wxEXPAND | wxALL, 10);
column1->Add(m_friendlyNameText, 0, wxEXPAND | wxLEFT | wxUP, 10);
column1->Add(m_friendlyName, 0, wxEXPAND | wxALL, 10);
column2->Add(m_modDestinationText, 0, wxEXPAND | wxLEFT | wxUP, 10);
column2->Add(m_modDestination, 0, wxEXPAND | wxLEFT | wxUP, 10);
#if wxUSE_DIRDLG
m_browse = new wxButton(this, DIR_CHOOSE, "Browse...");
column2->Add(m_browse, 0, wxALL, 10);
#endif // wxUSE_DIRDLG
column2->AddSpacer(20);
column2->Add(m_generate, 0, wxALIGN_CENTER, 0);
row->Add(column1, 0, wxALL | wxALIGN_CENTER, 10);
row->Add(column2, 0, wxALL | wxALIGN_CENTER, 10);
topSizer->Add(row, 0, wxALL | wxALIGN_CENTER, 10);
SetSizer(topSizer);
}
// NOLINTEND(cppcoreguidelines-owning-memory)
#if wxUSE_CLIPBOARD
/*
wxTextCtrl *MainPanel::GetFocusedText() const
{
wxWindow *win = FindFocus();
wxTextCtrl *text = win ? wxDynamicCast(win, wxTextCtrl) : NULL;
return text;
}
bool MainPanel::HasSelection() const
{
long from;
long to;
GetFocusedText()->GetSelection(&from, &to);
return from != to;
}
void MainPanel::DoPasteFromClipboard()
{
// On X11, we want to get the data from the primary selection instead
// of the normal clipboard (which isn't normal under X11 at all). This
// call has no effect under MSW.
wxTheClipboard->UsePrimarySelection();
if (!wxTheClipboard->Open())
return;
wxTextDataObject data;
if (wxTheClipboard->IsSupported(data.GetFormat())) {
if (wxTheClipboard->GetData(data))
GetFocusedText()->AppendText(data.GetText());
}
wxTheClipboard->Close();
}
void MainPanel::DoCopyToClipboard()
{
// On X11, we want to get the data from the primary selection instead
// of the normal clipboard (which isn't normal under X11 at all). This
// call has no effect under MSW.
wxTheClipboard->UsePrimarySelection();
wxString text(GetFocusedText()->GetStringSelection());
if (text.IsEmpty())
return;
if (!wxTheClipboard->Open())
return;
wxTextDataObject *data = new wxTextDataObject(text);
wxTheClipboard->SetData(data);
wxTheClipboard->Close();
}
*/
#endif // wxUSE_CLIPBOARD
#if wxUSE_DIRDLG
void MainPanel::DoChooseDir(wxCommandEvent &WXUNUSED(event)) {
wxString dirHome =
wxStandardPaths::Get().GetUserDir(wxStandardPaths::Dir_Desktop);
wxDirDialog dialog(this, "Select a directory", dirHome,
wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
if (dialog.ShowModal() == wxID_OK) {
m_modDestination->SetValue(dialog.GetPath());
}
}
#endif // wxUSE_DIRDLG
size_t MainPanel::CheckForErrors() {
m_errorLog.clear();
size_t errorCount = 0;
if (m_author->IsEmpty()) {
m_errorLog.push_back("Empty author field, please fill it out.");
errorCount++;
}
if (m_name->IsEmpty()) {
m_errorLog.push_back("Empty internal name field, please fill it out.");
errorCount++;
}
if (m_friendlyName->IsEmpty()) {
m_errorLog.push_back("Empty in-game name field, please fill it out.");
errorCount++;
}
if (m_modDestination->IsEmpty()) {
m_errorLog.push_back("Empty path field, please fill it out.");
errorCount++;
}
if (!wxDir::Exists(m_modDestination->GetValue())) {
m_errorLog.push_back(
"Invalid directory, please check your entered path.");
errorCount++;
}
return errorCount;
}
} // namespace SpeciesGen