| 1 |
/* Copyright (C) 2026 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 "Terrain.h" |
| 21 |
|
| 22 |
#include "tools/atlas/AtlasUI/CustomControls/Buttons/ToolButton.h" |
| 23 |
#include "tools/atlas/AtlasUI/General/Observable.h" |
| 24 |
#include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h" |
| 25 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Common/Sidebar.h" |
| 26 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Brushes.h" |
| 27 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/MiscState.h" |
| 28 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h" |
| 29 |
#include "tools/atlas/GameInterface/MessagePasser.h" |
| 30 |
#include "tools/atlas/GameInterface/Messages.h" |
| 31 |
#include "tools/atlas/GameInterface/Shareable.h" |
| 32 |
|
| 33 |
#include <algorithm> |
| 34 |
#include <chrono> |
| 35 |
#include <cstdlib> |
| 36 |
#include <cstring> |
| 37 |
#include <list> |
| 38 |
#include <string> |
| 39 |
#include <unordered_map> |
| 40 |
#include <utility> |
| 41 |
#include <vector> |
| 42 |
#include <wx/arrstr.h> |
| 43 |
#include <wx/bitmap.h> |
| 44 |
#include <wx/bmpbuttn.h> |
| 45 |
#include <wx/bookctrl.h> |
| 46 |
#include <wx/busyinfo.h> |
| 47 |
#include <wx/button.h> |
| 48 |
#include <wx/chartype.h> |
| 49 |
#include <wx/checkbox.h> |
| 50 |
#include <wx/choice.h> |
| 51 |
#include <wx/clntdata.h> |
| 52 |
#include <wx/colour.h> |
| 53 |
#include <wx/gdicmn.h> |
| 54 |
#include <wx/image.h> |
| 55 |
#include <wx/notebook.h> |
| 56 |
#include <wx/object.h> |
| 57 |
#include <wx/panel.h> |
| 58 |
#include <wx/scrolwin.h> |
| 59 |
#include <wx/sizer.h> |
| 60 |
#include <wx/statbmp.h> |
| 61 |
#include <wx/statbox.h> |
| 62 |
#include <wx/stattext.h> |
| 63 |
#include <wx/string.h> |
| 64 |
#include <wx/timer.h> |
| 65 |
#include <wx/toolbar.h> |
| 66 |
#include <wx/translation.h> |
| 67 |
#include <wx/unichar.h> |
| 68 |
#include <wx/window.h> |
| 69 |
#include <wx/wxcrt.h> |
| 70 |
|
| 71 |
namespace |
| 72 |
{ |
| 73 |
|
| 74 |
const int PREVIEW_RELOAD_DELAY_MILLISECONDS = 2000; |
| 75 |
const int PREVIEW_RELOAD_TIMEOUT_DELAY_MILLISECONDS = 200; |
| 76 |
const float PREVIEW_RELOAD_TIMEOUT_THRESHOLD_SECONDS = 0.1f; |
| 77 |
|
| 78 |
} // anonymous namespace |
| 79 |
|
| 80 |
class TextureNotebook; |
| 81 |
|
| 82 |
class TerrainBottomBar : public wxPanel |
| 83 |
{ |
| 84 |
public: |
| 85 |
TerrainBottomBar(ScenarioEditor& scenarioEditor, wxWindow* parent); |
| 86 |
void LoadTerrain(); |
| 87 |
void OnShutdown(); |
| 88 |
private: |
| 89 |
TextureNotebook* m_Textures; |
| 90 |
}; |
| 91 |
|
| 92 |
enum |
| 93 |
{ |
| 94 |
ID_Passability = 1, |
| 95 |
ID_ShowPriorities |
| 96 |
}; |
| 97 |
|
| 98 |
// Helper function for adding tooltips |
| 99 |
static wxWindow* Tooltipped(wxWindow* window, const wxString& tip) |
| 100 |
{ |
| 101 |
window->SetToolTip(tip); |
| 102 |
return window; |
| 103 |
} |
| 104 |
|
| 105 |
// Add spaces into the displayed name so there are more wrapping opportunities |
| 106 |
static wxString FormatTextureName(wxString name) |
| 107 |
{ |
| 108 |
if (name.Len()) |
| 109 |
name[0] = wxToupper(name[0]); |
| 110 |
name.Replace(_T("_"), _T(" ")); |
| 111 |
|
| 112 |
return name; |
| 113 |
} |
| 114 |
|
| 115 |
////////////////////////////////////////////////////////////////////////// |
| 116 |
|
| 117 |
class TexturePreviewPanel : public wxPanel |
| 118 |
{ |
| 119 |
private: |
| 120 |
static const int imageWidth = 120; |
| 121 |
static const int imageHeight = 40; |
| 122 |
|
| 123 |
public: |
| 124 |
TexturePreviewPanel(wxWindow* parent) |
| 125 |
: wxPanel(parent, wxID_ANY), m_Timer(this) |
| 126 |
{ |
| 127 |
m_Conn = g_SelectedTexture.RegisterObserver(0, &TexturePreviewPanel::OnTerrainChange, this); |
| 128 |
m_Sizer = new wxStaticBoxSizer(wxVERTICAL, this, _T("Texture")); |
| 129 |
SetSizer(m_Sizer); |
| 130 |
|
| 131 |
// Use placeholder bitmap for now |
| 132 |
m_Sizer->Add(new wxStaticBitmap(m_Sizer->GetStaticBox(), wxID_ANY, wxNullBitmap), wxSizerFlags(1).Expand()); |
| 133 |
} |
| 134 |
|
| 135 |
void LoadPreview() |
| 136 |
{ |
| 137 |
if (m_TextureName.IsEmpty()) |
| 138 |
{ |
| 139 |
// If we haven't got a texture yet, copy the global |
| 140 |
m_TextureName = g_SelectedTexture; |
| 141 |
} |
| 142 |
|
| 143 |
Freeze(); |
| 144 |
|
| 145 |
m_Sizer->Clear(true); |
| 146 |
|
| 147 |
AtlasMessage::qGetTerrainTexturePreview qry((std::wstring)m_TextureName.wc_str(), imageWidth, imageHeight); |
| 148 |
qry.Post(); |
| 149 |
|
| 150 |
AtlasMessage::sTerrainTexturePreview preview = qry.preview; |
| 151 |
|
| 152 |
// Check for invalid/missing texture - shouldn't happen |
| 153 |
if (!wxString(qry.preview->name.c_str()).IsEmpty()) |
| 154 |
{ |
| 155 |
// Construct the wrapped-text label |
| 156 |
wxStaticText* label = new wxStaticText(m_Sizer->GetStaticBox(), wxID_ANY, FormatTextureName(*qry.preview->name), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER); |
| 157 |
label->Wrap(m_Sizer->GetSize().GetX()); |
| 158 |
|
| 159 |
// cppcheck-suppress "memleak" |
| 160 |
unsigned char* buf = (unsigned char*)(malloc(preview.imageData.GetSize())); |
| 161 |
// imagedata.GetBuffer() gives a Shareable<unsigned char>*, which |
| 162 |
// is stored the same as a unsigned char*, so we can just copy it. |
| 163 |
memcpy(buf, preview.imageData.GetBuffer(), preview.imageData.GetSize()); |
| 164 |
wxImage img(qry.preview->imageWidth, qry.preview->imageHeight, buf); |
| 165 |
|
| 166 |
wxStaticBitmap* bitmap = new wxStaticBitmap(m_Sizer->GetStaticBox(), wxID_ANY, wxBitmap(img), wxDefaultPosition, wxSize(qry.preview->imageWidth, qry.preview->imageHeight), wxBORDER_SIMPLE); |
| 167 |
m_Sizer->Add(bitmap, wxSizerFlags(1).Align(wxALIGN_CENTER)); |
| 168 |
m_Sizer->Add(label, wxSizerFlags().Expand()); |
| 169 |
|
| 170 |
// We have to force the sidebar to layout manually |
| 171 |
GetParent()->GetParent()->Layout(); |
| 172 |
|
| 173 |
if (preview.loaded && m_Timer.IsRunning()) |
| 174 |
{ |
| 175 |
m_Timer.Stop(); |
| 176 |
} |
| 177 |
else if (!preview.loaded && !m_Timer.IsRunning()) |
| 178 |
{ |
| 179 |
m_Timer.Start(PREVIEW_RELOAD_DELAY_MILLISECONDS); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
Layout(); |
| 184 |
Thaw(); |
| 185 |
} |
| 186 |
|
| 187 |
void OnTerrainChange(const wxString& texture) |
| 188 |
{ |
| 189 |
// Check if texture really changed, to avoid doing this too often |
| 190 |
if (texture != m_TextureName) |
| 191 |
{ |
| 192 |
// Load new texture preview |
| 193 |
m_TextureName = texture; |
| 194 |
LoadPreview(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void OnTimer(wxTimerEvent& WXUNUSED(evt)) |
| 199 |
{ |
| 200 |
LoadPreview(); |
| 201 |
} |
| 202 |
|
| 203 |
private: |
| 204 |
ObservableScopedConnection m_Conn; |
| 205 |
wxStaticBoxSizer* m_Sizer; |
| 206 |
wxTimer m_Timer; |
| 207 |
wxString m_TextureName; |
| 208 |
|
| 209 |
DECLARE_EVENT_TABLE(); |
| 210 |
}; |
| 211 |
|
| 212 |
BEGIN_EVENT_TABLE(TexturePreviewPanel, wxPanel) |
| 213 |
EVT_TIMER(wxID_ANY, TexturePreviewPanel::OnTimer) |
| 214 |
END_EVENT_TABLE(); |
| 215 |
|
| 216 |
////////////////////////////////////////////////////////////////////////// |
| 217 |
|
| 218 |
TerrainSidebar::TerrainSidebar(ScenarioEditor& scenarioEditor, wxWindow* sidebarContainer, wxWindow* bottomBarContainer) : |
| 219 |
Sidebar(scenarioEditor, sidebarContainer, bottomBarContainer) |
| 220 |
{ |
| 221 |
wxSizer* scrollSizer = new wxBoxSizer(wxVERTICAL); |
| 222 |
wxScrolledWindow* scrolledWindow = new wxScrolledWindow(this); |
| 223 |
scrolledWindow->SetScrollRate(10, 10); |
| 224 |
scrolledWindow->SetSizer(scrollSizer); |
| 225 |
m_MainSizer->Add(scrolledWindow, wxSizerFlags().Proportion(1).Expand()); |
| 226 |
|
| 227 |
{ |
| 228 |
///////////////////////////////////////////////////////////////////////// |
| 229 |
// Terrain elevation |
| 230 |
wxStaticBoxSizer* sizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _("Elevation tools")); |
| 231 |
wxSizer* gridSizer = new wxGridSizer(4); |
| 232 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Modify"), _T("AlterElevation"), wxSize(48, -1)), |
| 233 |
_("Brush with left mouse buttons to raise terrain,\nright mouse button to lower it")), wxSizerFlags().Expand()); |
| 234 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Ridge"), _T("PikeElevation"), wxSize(48, -1)), |
| 235 |
_("Brush with left mouse buttons to raise terrain,\nright mouse button to lower it")), wxSizerFlags().Expand()); |
| 236 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Smooth"), _T("SmoothElevation"), wxSize(48, -1)), |
| 237 |
_("Brush with left mouse button to smooth terrain,\nright mouse button to roughen it")), wxSizerFlags().Expand()); |
| 238 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Flatten"), _T("FlattenElevation"), wxSize(48, -1)), |
| 239 |
_("Brush with left mouse button to flatten terrain")), wxSizerFlags().Expand()); |
| 240 |
sizer->Add(gridSizer, wxSizerFlags().Expand()); |
| 241 |
scrollSizer->Add(sizer, wxSizerFlags().Expand().Border(wxTOP, 10)); |
| 242 |
} |
| 243 |
|
| 244 |
{ |
| 245 |
///////////////////////////////////////////////////////////////////////// |
| 246 |
// Terrain texture |
| 247 |
wxStaticBoxSizer* sizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _("Texture tools")); |
| 248 |
wxSizer* gridSizer = new wxGridSizer(3); |
| 249 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Paint"), _T("PaintTerrain"), wxSize(48, -1)), |
| 250 |
_("Brush with left mouse button to paint texture dominantly,\nright mouse button to paint submissively.\nShift-left-click for eyedropper tool")), wxSizerFlags().Expand()); |
| 251 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Replace"), _T("ReplaceTerrain"), wxSize(48, -1)), |
| 252 |
_("Replace all of a terrain texture with a new one")), wxSizerFlags().Expand()); |
| 253 |
gridSizer->Add(Tooltipped(new ToolButton(scenarioEditor.GetToolManager(), sizer->GetStaticBox(), _("Fill"), _T("FillTerrain"), wxSize(48, -1)), |
| 254 |
_T("Bucket fill a patch of terrain texture with a new one")), wxSizerFlags().Expand()); |
| 255 |
sizer->Add(gridSizer, wxSizerFlags().Expand()); |
| 256 |
scrollSizer->Add(sizer, wxSizerFlags().Expand().Border(wxTOP, 10)); |
| 257 |
} |
| 258 |
|
| 259 |
{ |
| 260 |
///////////////////////////////////////////////////////////////////////// |
| 261 |
// Brush settings |
| 262 |
wxStaticBoxSizer* sizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _("Brush")); |
| 263 |
|
| 264 |
m_TexturePreview = new TexturePreviewPanel(sizer->GetStaticBox()); |
| 265 |
sizer->Add(m_TexturePreview, wxSizerFlags(1).Expand()); |
| 266 |
|
| 267 |
g_Brush_Elevation.CreateUI(sizer->GetStaticBox(), sizer); |
| 268 |
scrollSizer->Add(sizer, wxSizerFlags().Expand().Border(wxTOP, 10)); |
| 269 |
} |
| 270 |
|
| 271 |
{ |
| 272 |
///////////////////////////////////////////////////////////////////////// |
| 273 |
// Visualise |
| 274 |
wxStaticBoxSizer* sizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _("Visualise")); |
| 275 |
scrollSizer->Add(sizer, wxSizerFlags().Expand().Border(wxTOP, 10)); |
| 276 |
|
| 277 |
wxFlexGridSizer* visSizer = new wxFlexGridSizer(2, 5, 5); |
| 278 |
visSizer->AddGrowableCol(1); |
| 279 |
sizer->Add(visSizer, wxSizerFlags().Expand()); |
| 280 |
|
| 281 |
wxArrayString defaultChoices; |
| 282 |
defaultChoices.Add(_("(none)")); |
| 283 |
m_PassabilityChoice = new wxChoice(sizer->GetStaticBox(), ID_Passability, wxDefaultPosition, wxDefaultSize, defaultChoices); |
| 284 |
m_PassabilityChoice->SetSelection(0); |
| 285 |
|
| 286 |
visSizer->Add(new wxStaticText(sizer->GetStaticBox(), wxID_ANY, _("Passability")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT)); |
| 287 |
visSizer->Add(Tooltipped(m_PassabilityChoice, |
| 288 |
_("View passability classes")), wxSizerFlags().Expand()); |
| 289 |
|
| 290 |
visSizer->Add(new wxStaticText(sizer->GetStaticBox(), wxID_ANY, _("Priorities")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT)); |
| 291 |
visSizer->Add(Tooltipped(new wxCheckBox(sizer->GetStaticBox(), ID_ShowPriorities, _("")), |
| 292 |
_("Show terrain texture priorities"))); |
| 293 |
} |
| 294 |
|
| 295 |
m_BottomBar = new TerrainBottomBar(scenarioEditor, bottomBarContainer); |
| 296 |
} |
| 297 |
|
| 298 |
void TerrainSidebar::OnShutdown() |
| 299 |
{ |
| 300 |
static_cast<TerrainBottomBar*>(m_BottomBar)->OnShutdown(); |
| 301 |
} |
| 302 |
|
| 303 |
void TerrainSidebar::OnFirstDisplay() |
| 304 |
{ |
| 305 |
AtlasMessage::qGetTerrainPassabilityClasses qry; |
| 306 |
qry.Post(); |
| 307 |
std::vector<std::wstring> passClasses = *qry.classNames; |
| 308 |
for (size_t i = 0; i < passClasses.size(); ++i) |
| 309 |
m_PassabilityChoice->Append(passClasses[i].c_str()); |
| 310 |
|
| 311 |
static_cast<TerrainBottomBar*>(m_BottomBar)->LoadTerrain(); |
| 312 |
m_TexturePreview->LoadPreview(); |
| 313 |
} |
| 314 |
|
| 315 |
void TerrainSidebar::OnPassabilityChoice(wxCommandEvent& evt) |
| 316 |
{ |
| 317 |
if (evt.GetSelection() == 0) |
| 318 |
POST_MESSAGE(SetViewParamS, (AtlasMessage::eRenderView::GAME, L"passability", L"")); |
| 319 |
else |
| 320 |
POST_MESSAGE(SetViewParamS, (AtlasMessage::eRenderView::GAME, L"passability", (std::wstring)evt.GetString().wc_str())); |
| 321 |
} |
| 322 |
|
| 323 |
void TerrainSidebar::OnShowPriorities(wxCommandEvent& evt) |
| 324 |
{ |
| 325 |
POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::GAME, L"priorities", evt.IsChecked())); |
| 326 |
} |
| 327 |
|
| 328 |
BEGIN_EVENT_TABLE(TerrainSidebar, Sidebar) |
| 329 |
EVT_CHOICE(ID_Passability, TerrainSidebar::OnPassabilityChoice) |
| 330 |
EVT_CHECKBOX(ID_ShowPriorities, TerrainSidebar::OnShowPriorities) |
| 331 |
END_EVENT_TABLE(); |
| 332 |
|
| 333 |
////////////////////////////////////////////////////////////////////////// |
| 334 |
|
| 335 |
class TextureNotebookPage : public wxPanel |
| 336 |
{ |
| 337 |
private: |
| 338 |
static const int imageWidth = 120; |
| 339 |
static const int imageHeight = 40; |
| 340 |
|
| 341 |
public: |
| 342 |
TextureNotebookPage(ScenarioEditor& scenarioEditor, wxWindow* parent, const wxString& name) |
| 343 |
: wxPanel(parent, wxID_ANY), m_ScenarioEditor(scenarioEditor), m_Timer(this), m_Name(name), m_Loaded(false) |
| 344 |
{ |
| 345 |
m_ScrolledPanel = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxVSCROLL); |
| 346 |
m_ScrolledPanel->SetScrollRate(0, 10); |
| 347 |
|
| 348 |
wxSizer* sizer = new wxBoxSizer(wxVERTICAL); |
| 349 |
sizer->Add(m_ScrolledPanel, wxSizerFlags().Proportion(1).Expand()); |
| 350 |
SetSizer(sizer); |
| 351 |
|
| 352 |
m_ItemSizer = new wxGridSizer(6, 4, 0); |
| 353 |
m_ScrolledPanel->SetSizer(m_ItemSizer); |
| 354 |
} |
| 355 |
|
| 356 |
void OnDisplay() |
| 357 |
{ |
| 358 |
// Trigger the terrain loading on first display |
| 359 |
|
| 360 |
if (m_Loaded) |
| 361 |
return; |
| 362 |
|
| 363 |
m_Loaded = true; |
| 364 |
|
| 365 |
wxBusyInfo busy (_("Loading terrain previews")); |
| 366 |
|
| 367 |
AtlasMessage::qGetTerrainGroupTextures query((std::wstring)m_Name.wc_str()); |
| 368 |
query.Post(); |
| 369 |
m_Textures = *query.names; |
| 370 |
|
| 371 |
LayoutButtons(); |
| 372 |
ReloadPreviews(); |
| 373 |
} |
| 374 |
|
| 375 |
void LayoutButtons() |
| 376 |
{ |
| 377 |
Freeze(); |
| 378 |
|
| 379 |
m_ScrolledPanel->DestroyChildren(); |
| 380 |
m_ItemSizer->Clear(); |
| 381 |
|
| 382 |
m_LastTerrainSelection = nullptr; // clear any reference to deleted button |
| 383 |
|
| 384 |
for (const std::wstring& textureName : m_Textures) |
| 385 |
{ |
| 386 |
// Construct the wrapped-text label |
| 387 |
wxStaticText* label = new wxStaticText(m_ScrolledPanel, wxID_ANY, FormatTextureName(textureName), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER); |
| 388 |
label->Wrap(imageWidth); |
| 389 |
|
| 390 |
wxImage image(imageWidth, imageHeight); |
| 391 |
wxBitmapButton* button = new wxBitmapButton(m_ScrolledPanel, wxID_ANY, wxBitmap(image)); |
| 392 |
|
| 393 |
// Store the texture name in the clientdata slot |
| 394 |
button->SetClientObject(new wxStringClientData(textureName)); |
| 395 |
|
| 396 |
wxSizer* imageSizer = new wxBoxSizer(wxVERTICAL); |
| 397 |
imageSizer->Add(button, wxSizerFlags().Center()); |
| 398 |
imageSizer->Add(label, wxSizerFlags().Proportion(1).Center()); |
| 399 |
m_ItemSizer->Add(imageSizer, wxSizerFlags().Expand()); |
| 400 |
|
| 401 |
m_PreviewButtons.emplace(textureName, PreviewButton{button, false}); |
| 402 |
} |
| 403 |
|
| 404 |
m_ScrolledPanel->Fit(); |
| 405 |
Layout(); |
| 406 |
|
| 407 |
Thaw(); |
| 408 |
} |
| 409 |
|
| 410 |
void ReloadPreviews() |
| 411 |
{ |
| 412 |
bool allLoaded = true; |
| 413 |
bool timeout = false; |
| 414 |
const std::chrono::high_resolution_clock::time_point reloadingStart = |
| 415 |
std::chrono::high_resolution_clock::now(); |
| 416 |
for (const std::wstring& textureName : m_Textures) |
| 417 |
{ |
| 418 |
const auto it = m_PreviewButtons.find(textureName); |
| 419 |
if (it == m_PreviewButtons.end() || it->second.loaded) |
| 420 |
continue; |
| 421 |
|
| 422 |
if (timeout) |
| 423 |
{ |
| 424 |
// Mark allLoaded only in case we have a real not loaded texture, and not |
| 425 |
// because we have an exceeded timeout. |
| 426 |
allLoaded = false; |
| 427 |
continue; |
| 428 |
} |
| 429 |
|
| 430 |
AtlasMessage::qGetTerrainTexturePreview previewQuery(textureName, imageWidth, imageHeight); |
| 431 |
previewQuery.Post(); |
| 432 |
AtlasMessage::sTerrainTexturePreview preview = previewQuery.preview; |
| 433 |
|
| 434 |
if (!preview.loaded) |
| 435 |
allLoaded = false; |
| 436 |
else |
| 437 |
it->second.loaded = true; |
| 438 |
|
| 439 |
if (preview.imageData.GetSize()) |
| 440 |
{ |
| 441 |
unsigned char* buffer = reinterpret_cast<unsigned char*>(malloc(preview.imageData.GetSize())); |
| 442 |
// imagedata.GetBuffer() gives a Shareable<unsigned char>*, which |
| 443 |
// is stored the same as a unsigned char*, so we can just copy it. |
| 444 |
memcpy(buffer, preview.imageData.GetBuffer(), preview.imageData.GetSize()); |
| 445 |
wxImage image(imageWidth, imageHeight, buffer); |
| 446 |
it->second.button->SetBitmap(wxBitmap(image)); |
| 447 |
} |
| 448 |
|
| 449 |
// We need to load at least one preview so check for timeout inside real |
| 450 |
// loading. |
| 451 |
const std::chrono::high_resolution_clock::time_point now = |
| 452 |
std::chrono::high_resolution_clock::now(); |
| 453 |
const std::chrono::duration<float> delta = now - reloadingStart; |
| 454 |
if (delta.count() > PREVIEW_RELOAD_TIMEOUT_THRESHOLD_SECONDS) |
| 455 |
timeout = true; |
| 456 |
} |
| 457 |
|
| 458 |
// If not all textures were loaded yet, run a timer to reload the previews |
| 459 |
// every so often until they've all finished. |
| 460 |
if (allLoaded && m_Timer.IsRunning()) |
| 461 |
{ |
| 462 |
m_Timer.Stop(); |
| 463 |
m_PreviewButtons.clear(); |
| 464 |
} |
| 465 |
else if (!allLoaded) |
| 466 |
{ |
| 467 |
if (timeout) |
| 468 |
{ |
| 469 |
// In case we didn't have enough time to load all previews |
| 470 |
// start after a minimum delay to not freeze the whole UI. |
| 471 |
m_Timer.Start(PREVIEW_RELOAD_TIMEOUT_DELAY_MILLISECONDS); |
| 472 |
} |
| 473 |
else |
| 474 |
m_Timer.Start(PREVIEW_RELOAD_DELAY_MILLISECONDS); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
void OnButton(wxCommandEvent& evt) |
| 479 |
{ |
| 480 |
wxButton* button = wxDynamicCast(evt.GetEventObject(), wxButton); |
| 481 |
wxString name = static_cast<wxStringClientData*>(button->GetClientObject())->GetData(); |
| 482 |
g_SelectedTexture = name; |
| 483 |
g_SelectedTexture.NotifyObservers(); |
| 484 |
|
| 485 |
if (m_LastTerrainSelection) |
| 486 |
m_LastTerrainSelection->SetBackgroundColour(wxNullColour); |
| 487 |
|
| 488 |
button->SetBackgroundColour(wxColor(255, 255, 0)); |
| 489 |
m_LastTerrainSelection = button; |
| 490 |
|
| 491 |
// Slight hack: Default to Paint mode because that's probably what the user wanted |
| 492 |
// when they selected a terrain; unless already explicitly in Replace mode, because |
| 493 |
// then the user probably wanted that instead |
| 494 |
if (m_ScenarioEditor.GetToolManager().GetCurrentToolName() != _T("ReplaceTerrain") && m_ScenarioEditor.GetToolManager().GetCurrentToolName() != _T("FillTerrain")) |
| 495 |
m_ScenarioEditor.GetToolManager().SetCurrentTool(_T("PaintTerrain")); |
| 496 |
} |
| 497 |
|
| 498 |
void OnSize(wxSizeEvent& evt) |
| 499 |
{ |
| 500 |
int numCols = std::max(1, (int)(evt.GetSize().GetWidth() / (imageWidth + 16))); |
| 501 |
m_ItemSizer->SetCols(numCols); |
| 502 |
evt.Skip(); |
| 503 |
} |
| 504 |
|
| 505 |
void OnTimer(wxTimerEvent& WXUNUSED(evt)) |
| 506 |
{ |
| 507 |
ReloadPreviews(); |
| 508 |
} |
| 509 |
|
| 510 |
void OnShutdown() |
| 511 |
{ |
| 512 |
if (m_Timer.IsRunning()) |
| 513 |
m_Timer.Stop(); |
| 514 |
} |
| 515 |
|
| 516 |
private: |
| 517 |
ScenarioEditor& m_ScenarioEditor; |
| 518 |
bool m_Loaded; |
| 519 |
wxTimer m_Timer; |
| 520 |
wxString m_Name; |
| 521 |
wxScrolledWindow* m_ScrolledPanel; |
| 522 |
wxGridSizer* m_ItemSizer; |
| 523 |
wxButton* m_LastTerrainSelection; // button that was last selected, so we can undo its coloring |
| 524 |
|
| 525 |
std::vector<std::wstring> m_Textures; |
| 526 |
struct PreviewButton |
| 527 |
{ |
| 528 |
wxBitmapButton* button; |
| 529 |
bool loaded; |
| 530 |
}; |
| 531 |
std::unordered_map<std::wstring, PreviewButton> m_PreviewButtons; |
| 532 |
|
| 533 |
DECLARE_EVENT_TABLE(); |
| 534 |
}; |
| 535 |
|
| 536 |
BEGIN_EVENT_TABLE(TextureNotebookPage, wxPanel) |
| 537 |
EVT_BUTTON(wxID_ANY, TextureNotebookPage::OnButton) |
| 538 |
EVT_SIZE(TextureNotebookPage::OnSize) |
| 539 |
EVT_TIMER(wxID_ANY, TextureNotebookPage::OnTimer) |
| 540 |
END_EVENT_TABLE(); |
| 541 |
|
| 542 |
|
| 543 |
class TextureNotebook : public wxNotebook |
| 544 |
{ |
| 545 |
public: |
| 546 |
TextureNotebook(ScenarioEditor& scenarioEditor, wxWindow *parent) |
| 547 |
: wxNotebook(parent, wxID_ANY/*, wxDefaultPosition, wxDefaultSize, wxNB_FIXEDWIDTH*/), |
| 548 |
m_ScenarioEditor(scenarioEditor) |
| 549 |
{ |
| 550 |
} |
| 551 |
|
| 552 |
void LoadTerrain() |
| 553 |
{ |
| 554 |
wxBusyInfo busy (_("Loading terrain groups")); |
| 555 |
|
| 556 |
DeleteAllPages(); |
| 557 |
m_TerrainGroups.Clear(); |
| 558 |
|
| 559 |
// Get the list of terrain groups from the engine |
| 560 |
AtlasMessage::qGetTerrainGroups qry; |
| 561 |
qry.Post(); |
| 562 |
std::vector<std::wstring> groupnames = *qry.groupNames; |
| 563 |
for (std::vector<std::wstring>::iterator it = groupnames.begin(); it != groupnames.end(); ++it) |
| 564 |
m_TerrainGroups.Add(it->c_str()); |
| 565 |
|
| 566 |
for (size_t i = 0; i < m_TerrainGroups.GetCount(); ++i) |
| 567 |
{ |
| 568 |
wxString visibleName = FormatTextureName(m_TerrainGroups[i]); |
| 569 |
AddPage(new TextureNotebookPage(m_ScenarioEditor, this, m_TerrainGroups[i]), visibleName); |
| 570 |
} |
| 571 |
|
| 572 |
// On some platforms (wxOSX) there is no initial OnPageChanged event, so it loads with a blank page |
| 573 |
// and setting selection to 0 won't trigger it either, so just force first page to display |
| 574 |
// (this is safe because the sidebar has already been displayed) |
| 575 |
if (GetPageCount() > 0) |
| 576 |
{ |
| 577 |
static_cast<TextureNotebookPage*>(GetPage(0))->OnDisplay(); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
void OnShutdown() |
| 582 |
{ |
| 583 |
for (size_t index = 0; index < GetPageCount(); ++index) |
| 584 |
static_cast<TextureNotebookPage*>(GetPage(index))->OnShutdown(); |
| 585 |
} |
| 586 |
|
| 587 |
protected: |
| 588 |
void OnPageChanged(wxNotebookEvent& event) |
| 589 |
{ |
| 590 |
if (event.GetSelection() >= 0 && event.GetSelection() < (int)GetPageCount()) |
| 591 |
{ |
| 592 |
static_cast<TextureNotebookPage*>(GetPage(event.GetSelection()))->OnDisplay(); |
| 593 |
} |
| 594 |
event.Skip(); |
| 595 |
} |
| 596 |
|
| 597 |
private: |
| 598 |
ScenarioEditor& m_ScenarioEditor; |
| 599 |
wxArrayString m_TerrainGroups; |
| 600 |
|
| 601 |
DECLARE_EVENT_TABLE(); |
| 602 |
}; |
| 603 |
|
| 604 |
BEGIN_EVENT_TABLE(TextureNotebook, wxNotebook) |
| 605 |
EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, TextureNotebook::OnPageChanged) |
| 606 |
END_EVENT_TABLE(); |
| 607 |
|
| 608 |
////////////////////////////////////////////////////////////////////////// |
| 609 |
|
| 610 |
TerrainBottomBar::TerrainBottomBar(ScenarioEditor& scenarioEditor, wxWindow* parent) : |
| 611 |
wxPanel(parent, wxID_ANY) |
| 612 |
{ |
| 613 |
wxSizer* sizer = new wxBoxSizer(wxVERTICAL); |
| 614 |
m_Textures = new TextureNotebook(scenarioEditor, this); |
| 615 |
sizer->Add(m_Textures, wxSizerFlags().Expand().Proportion(1)); |
| 616 |
SetSizer(sizer); |
| 617 |
} |
| 618 |
|
| 619 |
void TerrainBottomBar::LoadTerrain() |
| 620 |
{ |
| 621 |
m_Textures->LoadTerrain(); |
| 622 |
} |
| 623 |
|
| 624 |
void TerrainBottomBar::OnShutdown() |
| 625 |
{ |
| 626 |
m_Textures->OnShutdown(); |
| 627 |
} |