| 1 |
/* Copyright (C) 2025 Wildfire Games. |
| 2 |
* This file is part of 0 A.D. |
| 3 |
* |
| 4 |
* 0 A.D. is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* 0 A.D. is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "precompiled.h" |
| 19 |
|
| 20 |
#include "ActorEditor.h" |
| 21 |
|
| 22 |
#include "tools/atlas/AtlasObject/AtlasObject.h" |
| 23 |
#include "tools/atlas/AtlasUI/ActorEditor/ActorEditorListCtrl.h" |
| 24 |
#include "tools/atlas/AtlasUI/CustomControls/Windows/AtlasWindow.h" |
| 25 |
#include "tools/atlas/AtlasUI/General/Datafile.h" |
| 26 |
|
| 27 |
#include <boost/algorithm/string/predicate.hpp> |
| 28 |
#include <cstring> |
| 29 |
#include <string> |
| 30 |
#include <wx/arrstr.h> |
| 31 |
#include <wx/chartype.h> |
| 32 |
#include <wx/checkbox.h> |
| 33 |
#include <wx/combobox.h> |
| 34 |
#include <wx/debug.h> |
| 35 |
#include <wx/file.h> |
| 36 |
#include <wx/filedlg.h> |
| 37 |
#include <wx/filename.h> |
| 38 |
#include <wx/gdicmn.h> |
| 39 |
#include <wx/icon.h> |
| 40 |
#include <wx/log.h> |
| 41 |
#include <wx/menu.h> |
| 42 |
#include <wx/msgdlg.h> |
| 43 |
#include <wx/panel.h> |
| 44 |
#include <wx/sizer.h> |
| 45 |
#include <wx/statbox.h> |
| 46 |
#include <wx/string.h> |
| 47 |
#include <wx/toolbar.h> |
| 48 |
#include <wx/translation.h> |
| 49 |
|
| 50 |
class wxWindow; |
| 51 |
|
| 52 |
BEGIN_EVENT_TABLE(ActorEditor, AtlasWindow) |
| 53 |
EVT_MENU(ID_CreateEntity, ActorEditor::OnCreateEntity) |
| 54 |
END_EVENT_TABLE() |
| 55 |
|
| 56 |
|
| 57 |
ActorEditor::ActorEditor(wxWindow* parent) |
| 58 |
: AtlasWindow(parent, _("Actor Editor"), wxSize(1024, 450)) |
| 59 |
{ |
| 60 |
SetIcon(wxIcon(_T("ICON_ActorEditor"))); |
| 61 |
|
| 62 |
wxMenu* menu = new wxMenu; |
| 63 |
menu->Append(ID_CreateEntity, _("Create &entity...")); |
| 64 |
AddCustomMenu(menu, _("&Actor")); |
| 65 |
|
| 66 |
////////////////////////////////////////////////////////////////////////// |
| 67 |
|
| 68 |
wxPanel* mainPanel = new wxPanel(this); |
| 69 |
|
| 70 |
m_ActorEditorListCtrl = new ActorEditorListCtrl(mainPanel); |
| 71 |
|
| 72 |
wxBoxSizer* vertSizer = new wxBoxSizer(wxVERTICAL); |
| 73 |
mainPanel->SetSizer(vertSizer); |
| 74 |
|
| 75 |
wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL); |
| 76 |
vertSizer->Add(topSizer, |
| 77 |
wxSizerFlags().Border(wxLEFT|wxRIGHT, 5)); |
| 78 |
|
| 79 |
vertSizer->Add( |
| 80 |
m_ActorEditorListCtrl, |
| 81 |
wxSizerFlags().Proportion(1).Expand().Border(wxALL, 10)); |
| 82 |
|
| 83 |
////////////////////////////////////////////////////////////////////////// |
| 84 |
// Properties panel: |
| 85 |
|
| 86 |
wxPanel* propertiesPanel = new wxPanel(mainPanel); |
| 87 |
topSizer->Add(propertiesPanel, wxSizerFlags().Expand().Border(wxLEFT|wxRIGHT, 5)); |
| 88 |
|
| 89 |
wxSizer* propertiesSizer = new wxStaticBoxSizer( |
| 90 |
new wxStaticBox(propertiesPanel, wxID_ANY, _("Actor properties")), |
| 91 |
wxHORIZONTAL); |
| 92 |
|
| 93 |
propertiesPanel->SetSizer(propertiesSizer); |
| 94 |
|
| 95 |
|
| 96 |
m_CastShadows = new wxCheckBox(propertiesPanel, wxID_ANY, _("Cast shadow")); |
| 97 |
propertiesSizer->Add(m_CastShadows, wxSizerFlags().Border(wxALL, 5)); |
| 98 |
|
| 99 |
m_Float = new wxCheckBox(propertiesPanel, wxID_ANY, _("Float on water")); |
| 100 |
propertiesSizer->Add(m_Float, wxSizerFlags().Border(wxALL, 5)); |
| 101 |
|
| 102 |
// TODO: Orientation property. |
| 103 |
|
| 104 |
////////////////////////////////////////////////////////////////////////// |
| 105 |
// Materials box: |
| 106 |
|
| 107 |
wxPanel* materialsPanel = new wxPanel(mainPanel); |
| 108 |
topSizer->Add(materialsPanel, wxSizerFlags().Expand().Border(wxLEFT|wxRIGHT, 5)); |
| 109 |
|
| 110 |
wxSizer* materialsSizer = new wxStaticBoxSizer( |
| 111 |
new wxStaticBox(materialsPanel, wxID_ANY, _("Material")), |
| 112 |
wxHORIZONTAL); |
| 113 |
|
| 114 |
materialsPanel->SetSizer(materialsSizer); |
| 115 |
|
| 116 |
// Get the list of XML materials |
| 117 |
wxArrayString materials = Datafile::EnumerateDataFiles(_T("mods/public/art/materials/"), _T("*.xml")); |
| 118 |
// Extract the filenames and discard the path |
| 119 |
for (size_t i = 0; i < materials.Count(); ++i) |
| 120 |
materials[i] = wxFileName(materials[i]).GetFullName(); |
| 121 |
|
| 122 |
m_Material = new wxComboBox(materialsPanel, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, materials); |
| 123 |
materialsSizer->Add(m_Material, wxSizerFlags().Border(wxALL, 2)); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
void ActorEditor::ThawData(AtObj& in) |
| 128 |
{ |
| 129 |
AtObj actor (*in["actor"]); |
| 130 |
m_ActorEditorListCtrl->ThawData(actor); |
| 131 |
|
| 132 |
m_CastShadows->SetValue(actor["castshadow"].defined()); |
| 133 |
m_Float->SetValue(actor["float"].defined()); |
| 134 |
m_Material->SetValue((wxString)actor["material"]); |
| 135 |
} |
| 136 |
|
| 137 |
AtObj ActorEditor::FreezeData() |
| 138 |
{ |
| 139 |
AtObj actor (m_ActorEditorListCtrl->FreezeData()); |
| 140 |
|
| 141 |
if (m_CastShadows->IsChecked()) |
| 142 |
actor.set("castshadow", ""); |
| 143 |
|
| 144 |
if (m_Float->IsChecked()) |
| 145 |
actor.set("float", ""); |
| 146 |
|
| 147 |
if (m_Material->GetValue().length()) |
| 148 |
actor.set("material", m_Material->GetValue().utf8_str()); |
| 149 |
|
| 150 |
AtObj out; |
| 151 |
out.set("actor", actor); |
| 152 |
return out; |
| 153 |
} |
| 154 |
|
| 155 |
static AtObj ConvertToLatestFormat(AtObj in) |
| 156 |
{ |
| 157 |
if (! in.defined()) |
| 158 |
{ |
| 159 |
// 'Importing' a new blank file. Fill it in with default values: |
| 160 |
AtObj actor; |
| 161 |
actor.add("@version", "1"); |
| 162 |
in.add("actor", actor); |
| 163 |
} |
| 164 |
|
| 165 |
// Determine the file format version |
| 166 |
long version; |
| 167 |
|
| 168 |
if (in["Object"].defined()) |
| 169 |
{ |
| 170 |
// old-style actor format |
| 171 |
version = -1; |
| 172 |
} |
| 173 |
else if (in["qualitylevels"].defined()) |
| 174 |
{ |
| 175 |
// New-style, multiple-quality-levels actor. |
| 176 |
wxLogError(_("Cannot edit actors with multiple quality levels. If you want to use the actor editor, use the `<actor file=\"\">` format and edit the referenced files.")); |
| 177 |
return AtObj(); |
| 178 |
} |
| 179 |
else if (in["actor"].defined()) |
| 180 |
version = (in["actor"]["@version"].defined()) ? (*in["actor"]["@version"]).getLong() : 0; |
| 181 |
else |
| 182 |
{ |
| 183 |
wxLogError(_("Failed to determine actor file format version")); |
| 184 |
return AtObj(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
// Do any necessary conversions into the most recent format: |
| 189 |
|
| 190 |
if (version == -1) |
| 191 |
{ |
| 192 |
AtObj actor; |
| 193 |
AtIter castShadows = in["Object"]["Properties"]["@castshadows"]; |
| 194 |
// Handle the global actor properties |
| 195 |
if (castShadows.defined() && (*castShadows).getInt() == 1) |
| 196 |
actor.set("castshadow", ""); |
| 197 |
|
| 198 |
// Things to strip leading strings (for converting filenames, since the |
| 199 |
// new format doesn't store the entire path) |
| 200 |
#define THING1(out,outname, in,inname, prefix) \ |
| 201 |
wxASSERT( wxString::FromUTF8(in["Object"][inname]).StartsWith(_T(prefix)) ); \ |
| 202 |
out.add(outname, wxString::FromUTF8(in["Object"][inname]).Mid(wxString(_T(prefix)).Length()).utf8_str()) |
| 203 |
#define THING2(out,outname, in,inname, prefix) \ |
| 204 |
wxASSERT( wxString::FromUTF8(in[inname]).StartsWith(_T(prefix)) ); \ |
| 205 |
out.add(outname, wxString::FromUTF8(in[inname]).Mid(wxString(_T(prefix)).Length()).utf8_str()) |
| 206 |
|
| 207 |
if (wxString::FromUTF8(in["Object"]["Material"]).Len()) |
| 208 |
{ |
| 209 |
THING1(actor,"material", in,"Material", "art/materials/"); |
| 210 |
} |
| 211 |
|
| 212 |
// Create a single variant to contain all the old data |
| 213 |
AtObj var; |
| 214 |
var.add("@name", "Base"); |
| 215 |
var.add("@frequency", "100"); // 100 == default frequency |
| 216 |
|
| 217 |
THING1(var,"mesh", in,"ModelName", "art/meshes/"); |
| 218 |
|
| 219 |
// XXX |
| 220 |
std::string textureName(in["Object"]["TextureName"]); |
| 221 |
if (boost::algorithm::starts_with(textureName, "art/textures/ui/session/portraits/ui_portrait_sheet_civ_")) |
| 222 |
var.add("texture", ("temp/" + textureName.substr(strlen("art/textures/ui/session/portraits/"))).c_str()); |
| 223 |
else |
| 224 |
THING1(var,"texture", in,"TextureName", "art/textures/skins/"); |
| 225 |
|
| 226 |
AtObj anims; |
| 227 |
for (AtIter animit = in["Object"]["Animations"]["Animation"]; animit.defined(); ++animit) |
| 228 |
{ |
| 229 |
if (strlen(animit["@file"])) |
| 230 |
{ |
| 231 |
AtObj anim; |
| 232 |
anim.add("@name", animit["@name"]); |
| 233 |
anim.add("@speed", animit["@speed"]); |
| 234 |
|
| 235 |
THING2(anim,"@file", animit,"@file", "art/animation/"); |
| 236 |
|
| 237 |
anims.add("animation", anim); |
| 238 |
} |
| 239 |
} |
| 240 |
var.add("animations", anims); |
| 241 |
|
| 242 |
AtObj props; |
| 243 |
for (AtIter propit = in["Object"]["Props"]["Prop"]; propit.defined(); ++propit) |
| 244 |
{ |
| 245 |
AtObj prop; |
| 246 |
prop.add("@attachpoint", propit["@attachpoint"]); |
| 247 |
prop.add("@actor", propit["@model"]); |
| 248 |
props.add("prop", prop); |
| 249 |
} |
| 250 |
var.add("props", props); |
| 251 |
|
| 252 |
AtObj grp; |
| 253 |
grp.add("variant", var); |
| 254 |
actor.add("group", grp); |
| 255 |
|
| 256 |
#undef THING1 |
| 257 |
#undef THING2 |
| 258 |
|
| 259 |
actor.set("@version", "1"); |
| 260 |
in = AtObj(); |
| 261 |
in.set("actor", actor); |
| 262 |
} |
| 263 |
else if (version == 0) |
| 264 |
{ |
| 265 |
AtObj actor; |
| 266 |
|
| 267 |
if (in["actor"]["castshadow"].defined()) actor.add("castshadow", in["actor"]["castshadow"]); |
| 268 |
if (in["actor"]["material"].defined()) actor.add("material", in["actor"]["material"]); |
| 269 |
|
| 270 |
for (AtIter grpit = in["actor"]["group"]; grpit.defined(); ++grpit) |
| 271 |
{ |
| 272 |
AtObj grp; |
| 273 |
for (AtIter varit = grpit["variant"]; varit.defined(); ++varit) |
| 274 |
{ |
| 275 |
AtObj var; |
| 276 |
var.add("@name", varit["name"]); |
| 277 |
var.add("@frequency", varit["frequency"]); |
| 278 |
var.add("mesh", varit["mesh"]); |
| 279 |
var.add("texture", varit["texture"]); |
| 280 |
|
| 281 |
AtObj anims; |
| 282 |
for (AtIter animit = varit["animations"]["animation"]; animit.defined(); ++animit) |
| 283 |
{ |
| 284 |
AtObj anim; |
| 285 |
anim.add("@name", animit["name"]); |
| 286 |
anim.add("@file", animit["file"]); |
| 287 |
anim.add("@speed", animit["speed"]); |
| 288 |
|
| 289 |
anims.add("animation", anim); |
| 290 |
} |
| 291 |
var.add("animations", anims); |
| 292 |
|
| 293 |
AtObj props; |
| 294 |
for (AtIter propit = varit["props"]["prop"]; propit.defined(); ++propit) |
| 295 |
{ |
| 296 |
AtObj prop; |
| 297 |
prop.add("@attachpoint", propit["attachpoint"]); |
| 298 |
prop.add("@actor", propit["model"]); |
| 299 |
|
| 300 |
props.add("prop", prop); |
| 301 |
} |
| 302 |
var.add("props", props); |
| 303 |
|
| 304 |
grp.add("variant", var); |
| 305 |
} |
| 306 |
actor.add("group", grp); |
| 307 |
} |
| 308 |
|
| 309 |
actor.set("@version", "1"); |
| 310 |
in.set("actor", actor); |
| 311 |
} |
| 312 |
else if (version == 1) |
| 313 |
{ |
| 314 |
// current format |
| 315 |
} |
| 316 |
else |
| 317 |
{ |
| 318 |
// ??? unknown format - this should have been noticed earlier |
| 319 |
wxFAIL_MSG(_T("Invalid actor format")); |
| 320 |
} |
| 321 |
|
| 322 |
return in; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
void ActorEditor::ImportData(AtObj& in) |
| 327 |
{ |
| 328 |
AtObj data = ConvertToLatestFormat(in); |
| 329 |
if (! data.defined()) |
| 330 |
return; |
| 331 |
|
| 332 |
// Copy the data into the appropriate UI controls: |
| 333 |
|
| 334 |
AtObj actor (*data["actor"]); |
| 335 |
m_ActorEditorListCtrl->ImportData(actor); |
| 336 |
|
| 337 |
m_Actor = actor; |
| 338 |
|
| 339 |
m_CastShadows->SetValue(actor["castshadow"].defined()); |
| 340 |
m_Float->SetValue(actor["float"].defined()); |
| 341 |
m_Material->SetValue((wxString)actor["material"]); |
| 342 |
} |
| 343 |
|
| 344 |
AtObj ActorEditor::ExportData() |
| 345 |
{ |
| 346 |
// Export the group/variant/etc data |
| 347 |
AtObj actor (m_ActorEditorListCtrl->ExportData()); |
| 348 |
|
| 349 |
actor.set("@version", "1"); |
| 350 |
|
| 351 |
|
| 352 |
AtObj castShadow = *m_Actor["castshadow"]; |
| 353 |
if (m_CastShadows->IsChecked() && castShadow.defined()) |
| 354 |
actor.set("castshadow", castShadow); |
| 355 |
else if (m_CastShadows->IsChecked()) |
| 356 |
actor.set("castshadow", ""); |
| 357 |
|
| 358 |
AtObj floatObj = *m_Actor["float"]; |
| 359 |
if (m_Float->IsChecked() && floatObj) |
| 360 |
actor.set("float", floatObj); |
| 361 |
else if (m_Float->IsChecked()) |
| 362 |
actor.set("float", ""); |
| 363 |
|
| 364 |
AtObj material = *m_Actor["material"]; |
| 365 |
actor.set("material", material); |
| 366 |
if (m_Material->GetValue().length()) |
| 367 |
actor.set("material", m_Material->GetValue().utf8_str()); |
| 368 |
|
| 369 |
AtObj out; |
| 370 |
out.set("actor", actor); |
| 371 |
return out; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
void ActorEditor::OnCreateEntity(wxCommandEvent& WXUNUSED(event)) |
| 376 |
{ |
| 377 |
// Create a very basic entity for this actor. |
| 378 |
|
| 379 |
// The output should be an XML file like: |
| 380 |
// |
| 381 |
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 382 |
// <Entity Parent="celt_csw_b"> |
| 383 |
// <Actor>units/celt_csw_a.xml</Actor> |
| 384 |
// </Entity> |
| 385 |
// |
| 386 |
// 'Actor' comes from this actor's filename. |
| 387 |
// 'Parent' comes from the filename of a user-selected entity. |
| 388 |
// The file will be saved into a user-selected location. |
| 389 |
|
| 390 |
// Get the entity's expected name |
| 391 |
wxFileName currentFilename (GetCurrentFilename()); |
| 392 |
if (! currentFilename.IsOk()) |
| 393 |
{ |
| 394 |
wxMessageDialog(this, _("Please save this actor before attempting to create an entity for it."), |
| 395 |
_("Gentle reminder"), wxOK | wxICON_INFORMATION).ShowModal(); |
| 396 |
return; |
| 397 |
} |
| 398 |
wxString entityName = currentFilename.GetName(); |
| 399 |
|
| 400 |
// Work out where the entities are stored |
| 401 |
wxFileName entityPath (_T("mods/public/entities/")); |
| 402 |
entityPath.MakeAbsolute(Datafile::GetDataDirectory()); |
| 403 |
|
| 404 |
// Make sure the user knows what's going on |
| 405 |
static bool instructed = false; // only tell them once per session |
| 406 |
if (! instructed) |
| 407 |
{ |
| 408 |
instructed = true; |
| 409 |
if (wxMessageBox(_("To create an entity, you will first be asked to choose a parent entity, and then asked where save the new entity."), |
| 410 |
_("Usage instructions"), wxICON_INFORMATION|wxOK|wxCANCEL, this) != wxOK) |
| 411 |
return; // cancelled by user |
| 412 |
} |
| 413 |
|
| 414 |
wxString parentEntityFilename |
| 415 |
(wxFileSelector(_("Choose a parent entity"), entityPath.GetPath(), _T(""), |
| 416 |
_T("xml"), _("XML files (*.xml)|*.xml|All files (*.*)|*.*"), wxFD_OPEN, this)); |
| 417 |
|
| 418 |
if (! parentEntityFilename.Length()) |
| 419 |
return; // cancelled by user |
| 420 |
|
| 421 |
// Get the parent's name |
| 422 |
wxString parentName (wxFileName(parentEntityFilename).GetName()); |
| 423 |
|
| 424 |
wxString outputEntityFilename |
| 425 |
(wxFileSelector(_("Choose a filename to save as"), entityPath.GetPath(), entityName, |
| 426 |
_T("xml"), _("XML files (*.xml)|*.xml|All files (*.*)|*.*"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT, this)); |
| 427 |
|
| 428 |
if (! outputEntityFilename.Length()) |
| 429 |
return; // cancelled by user |
| 430 |
|
| 431 |
// Get this actor's filename, relative to actors/ |
| 432 |
wxFileName actorPath (_T("mods/public/art/actors/")); |
| 433 |
actorPath.MakeAbsolute(Datafile::GetDataDirectory()); |
| 434 |
wxFileName actorFilename (currentFilename); |
| 435 |
actorFilename.MakeRelativeTo(actorPath.GetFullPath()); |
| 436 |
|
| 437 |
// Create the XML data to be written |
| 438 |
// TODO: Native line endings |
| 439 |
wxString xml = |
| 440 |
_T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n") |
| 441 |
_T("\r\n") |
| 442 |
_T("<Entity Parent=\"") + parentName + _T("\">\r\n") |
| 443 |
_T("\t<Actor>") + actorFilename.GetFullPath(wxPATH_UNIX) + _T("</Actor>\r\n") |
| 444 |
_T("</Entity>\r\n"); |
| 445 |
|
| 446 |
wxFile file (outputEntityFilename.c_str(), wxFile::write); |
| 447 |
if (! file.IsOpened()) |
| 448 |
{ |
| 449 |
wxLogError(_("Failed to open file")); |
| 450 |
return; |
| 451 |
} |
| 452 |
|
| 453 |
if (! file.Write(xml)) |
| 454 |
{ |
| 455 |
wxLogError(_("Failed to write XML data to file")); |
| 456 |
return; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
wxString ActorEditor::GetDefaultOpenDirectory() |
| 461 |
{ |
| 462 |
wxFileName dir (_T("mods/public/art/actors/"), wxPATH_UNIX); |
| 463 |
dir.MakeAbsolute(Datafile::GetDataDirectory()); |
| 464 |
return dir.GetPath(); |
| 465 |
} |