tibia-rme/source/properties_window.cpp

329 lines
11 KiB
C++
Raw Permalink Normal View History

//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
2020-07-30 11:42:28 -03:00
// Remere's Map Editor 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.
//
// Remere's Map Editor 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 this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "properties_window.h"
#include "gui_ids.h"
#include "complexitem.h"
#include "container_properties_window.h"
BEGIN_EVENT_TABLE(PropertiesWindow, wxDialog)
2023-10-09 19:12:16 -07:00
EVT_BUTTON(wxID_OK, PropertiesWindow::OnClickOK)
EVT_BUTTON(wxID_CANCEL, PropertiesWindow::OnClickCancel)
2023-10-09 19:12:16 -07:00
EVT_BUTTON(ITEM_PROPERTIES_ADD_ATTRIBUTE, PropertiesWindow::OnClickAddAttribute)
EVT_BUTTON(ITEM_PROPERTIES_REMOVE_ATTRIBUTE, PropertiesWindow::OnClickRemoveAttribute)
2023-10-09 19:12:16 -07:00
EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, PropertiesWindow::OnNotebookPageChanged)
2023-10-09 19:12:16 -07:00
EVT_GRID_CELL_CHANGED(PropertiesWindow::OnGridValueChanged)
END_EVENT_TABLE()
PropertiesWindow::PropertiesWindow(wxWindow* parent, const Map* map, const Tile* tile_parent, Item* item, wxPoint pos) :
ObjectPropertiesWindowBase(parent, "Item Properties", map, tile_parent, item, pos),
2023-10-09 19:12:16 -07:00
currentPanel(nullptr) {
ASSERT(edit_item);
notebook = newd wxNotebook(this, wxID_ANY, wxDefaultPosition, wxSize(600, 300));
2016-11-05 15:28:14 +01:00
notebook->AddPage(createGeneralPanel(notebook), "Simple", true);
2023-10-09 19:12:16 -07:00
if (dynamic_cast<Container*>(item)) {
2016-11-05 15:28:14 +01:00
notebook->AddPage(createContainerPanel(notebook), "Contents");
}
2016-11-05 15:28:14 +01:00
notebook->AddPage(createAttributesPanel(notebook), "Advanced");
wxSizer* topSizer = newd wxBoxSizer(wxVERTICAL);
topSizer->Add(notebook, wxSizerFlags(1).DoubleBorder());
wxSizer* optSizer = newd wxBoxSizer(wxHORIZONTAL);
2016-11-05 15:28:14 +01:00
optSizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(0).Center());
optSizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(0).Center());
topSizer->Add(optSizer, wxSizerFlags(0).Center().DoubleBorder());
SetSizerAndFit(topSizer);
2016-02-29 21:47:54 -03:00
Centre(wxBOTH);
}
2023-10-09 19:12:16 -07:00
PropertiesWindow::~PropertiesWindow() {
;
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::Update() {
Container* container = dynamic_cast<Container*>(edit_item);
2023-10-09 19:12:16 -07:00
if (container) {
for (uint32_t i = 0; i < container->getVolume(); ++i) {
container_items[i]->setItem(container->getItem(i));
}
}
wxDialog::Update();
}
2023-10-09 19:12:16 -07:00
wxWindow* PropertiesWindow::createGeneralPanel(wxWindow* parent) {
wxPanel* panel = newd wxPanel(parent, ITEM_PROPERTIES_GENERAL_TAB);
wxFlexGridSizer* gridsizer = newd wxFlexGridSizer(2, 10, 10);
gridsizer->AddGrowableCol(1);
2016-11-05 15:28:14 +01:00
gridsizer->Add(newd wxStaticText(panel, wxID_ANY, "ID " + i2ws(edit_item->getID())));
gridsizer->Add(newd wxStaticText(panel, wxID_ANY, "\"" + wxstr(edit_item->getName()) + "\""));
2016-11-05 15:28:14 +01:00
gridsizer->Add(newd wxStaticText(panel, wxID_ANY, "Action ID"));
wxSpinCtrl* action_id_field = newd wxSpinCtrl(panel, wxID_ANY, i2ws(edit_item->getActionID()), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 0xFFFF, edit_item->getActionID());
gridsizer->Add(action_id_field, wxSizerFlags(1).Expand());
2016-11-05 15:28:14 +01:00
gridsizer->Add(newd wxStaticText(panel, wxID_ANY, "Unique ID"));
wxSpinCtrl* unique_id_field = newd wxSpinCtrl(panel, wxID_ANY, i2ws(edit_item->getUniqueID()), wxDefaultPosition, wxSize(-1, 20), wxSP_ARROW_KEYS, 0, 0xFFFF, edit_item->getUniqueID());
gridsizer->Add(unique_id_field, wxSizerFlags(1).Expand());
panel->SetSizerAndFit(gridsizer);
return panel;
}
2023-10-09 19:12:16 -07:00
wxWindow* PropertiesWindow::createContainerPanel(wxWindow* parent) {
Container* container = (Container*)edit_item;
wxPanel* panel = newd wxPanel(parent, ITEM_PROPERTIES_CONTAINER_TAB);
wxSizer* topSizer = newd wxBoxSizer(wxVERTICAL);
2016-09-29 19:24:45 -03:00
wxSizer* gridSizer = newd wxGridSizer(6, 5, 5);
bool use_large_sprites = g_settings.getBoolean(Config::USE_LARGE_CONTAINER_ICONS);
2023-10-09 19:12:16 -07:00
for (uint32_t i = 0; i < container->getVolume(); ++i) {
Item* item = container->getItem(i);
ContainerItemButton* containerItemButton = newd ContainerItemButton(panel, use_large_sprites, i, edit_map, item);
container_items.push_back(containerItemButton);
gridSizer->Add(containerItemButton, wxSizerFlags(0));
}
topSizer->Add(gridSizer, wxSizerFlags(1).Expand());
/*
wxSizer* optSizer = newd wxBoxSizer(wxHORIZONTAL);
2016-11-05 15:28:14 +01:00
optSizer->Add(newd wxButton(panel, ITEM_PROPERTIES_ADD_ATTRIBUTE, "Add Item"), wxSizerFlags(0).Center());
// optSizer->Add(newd wxButton(panel, ITEM_PROPERTIES_REMOVE_ATTRIBUTE, "Remove Attribute"), wxSizerFlags(0).Center());
topSizer->Add(optSizer, wxSizerFlags(0).Center().DoubleBorder());
*/
panel->SetSizer(topSizer);
return panel;
}
2023-10-09 19:12:16 -07:00
wxWindow* PropertiesWindow::createAttributesPanel(wxWindow* parent) {
wxPanel* panel = newd wxPanel(parent, wxID_ANY);
wxSizer* topSizer = newd wxBoxSizer(wxVERTICAL);
attributesGrid = newd wxGrid(panel, ITEM_PROPERTIES_ADVANCED_TAB, wxDefaultPosition, wxSize(-1, 160));
topSizer->Add(attributesGrid, wxSizerFlags(1).Expand());
wxFont time_font(*wxSWISS_FONT);
attributesGrid->SetDefaultCellFont(time_font);
attributesGrid->CreateGrid(0, 3);
attributesGrid->DisableDragRowSize();
attributesGrid->DisableDragColSize();
attributesGrid->SetSelectionMode(wxGrid::wxGridSelectRows);
attributesGrid->SetRowLabelSize(0);
2023-10-09 19:12:16 -07:00
// log->SetColLabelSize(0);
// log->EnableGridLines(false);
attributesGrid->EnableEditing(true);
2016-11-05 15:28:14 +01:00
attributesGrid->SetColLabelValue(0, "Key");
attributesGrid->SetColSize(0, 100);
2016-11-05 15:28:14 +01:00
attributesGrid->SetColLabelValue(1, "Type");
attributesGrid->SetColSize(1, 80);
2016-11-05 15:28:14 +01:00
attributesGrid->SetColLabelValue(2, "Value");
attributesGrid->SetColSize(2, 410);
// contents
ItemAttributeMap attrs = edit_item->getAttributes();
attributesGrid->AppendRows(attrs.size());
int i = 0;
2023-10-09 19:12:16 -07:00
for (ItemAttributeMap::iterator aiter = attrs.begin(); aiter != attrs.end(); ++aiter, ++i) {
SetGridValue(attributesGrid, i, aiter->first, aiter->second);
2023-10-09 19:12:16 -07:00
}
wxSizer* optSizer = newd wxBoxSizer(wxHORIZONTAL);
2016-11-05 15:28:14 +01:00
optSizer->Add(newd wxButton(panel, ITEM_PROPERTIES_ADD_ATTRIBUTE, "Add Attribute"), wxSizerFlags(0).Center());
optSizer->Add(newd wxButton(panel, ITEM_PROPERTIES_REMOVE_ATTRIBUTE, "Remove Attribute"), wxSizerFlags(0).Center());
topSizer->Add(optSizer, wxSizerFlags(0).Center().DoubleBorder());
panel->SetSizer(topSizer);
return panel;
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::SetGridValue(wxGrid* grid, int rowIndex, std::string label, const ItemAttribute &attr) {
wxArrayString types;
2016-11-05 15:28:14 +01:00
types.Add("Number");
types.Add("Float");
types.Add("Boolean");
types.Add("String");
2016-03-03 09:57:36 -03:00
grid->SetCellValue(rowIndex, 0, label);
2015-12-06 11:42:37 -03:00
switch (attr.type) {
case ItemAttribute::STRING: {
2016-11-05 15:28:14 +01:00
grid->SetCellValue(rowIndex, 1, "String");
2016-03-03 09:57:36 -03:00
grid->SetCellValue(rowIndex, 2, wxstr(*attr.getString()));
break;
}
2015-12-06 11:42:37 -03:00
case ItemAttribute::INTEGER: {
2016-11-05 15:28:14 +01:00
grid->SetCellValue(rowIndex, 1, "Number");
2016-03-03 09:57:36 -03:00
grid->SetCellValue(rowIndex, 2, i2ws(*attr.getInteger()));
grid->SetCellEditor(rowIndex, 2, new wxGridCellNumberEditor);
break;
}
2015-12-06 11:42:37 -03:00
case ItemAttribute::DOUBLE:
case ItemAttribute::FLOAT: {
2016-11-05 15:28:14 +01:00
grid->SetCellValue(rowIndex, 1, "Float");
wxString f;
f << *attr.getFloat();
2016-03-03 09:57:36 -03:00
grid->SetCellValue(rowIndex, 2, f);
grid->SetCellEditor(rowIndex, 2, new wxGridCellFloatEditor);
break;
}
2015-12-06 11:42:37 -03:00
case ItemAttribute::BOOLEAN: {
2016-11-05 15:28:14 +01:00
grid->SetCellValue(rowIndex, 1, "Boolean");
grid->SetCellValue(rowIndex, 2, *attr.getBoolean() ? "1" : "");
grid->SetCellRenderer(rowIndex, 2, new wxGridCellBoolRenderer);
grid->SetCellEditor(rowIndex, 2, new wxGridCellBoolEditor);
break;
}
2015-12-06 11:42:37 -03:00
default: {
2016-11-05 15:28:14 +01:00
grid->SetCellValue(rowIndex, 1, "Unknown");
2016-03-03 09:57:36 -03:00
grid->SetCellBackgroundColour(rowIndex, 1, *wxLIGHT_GREY);
grid->SetCellBackgroundColour(rowIndex, 2, *wxLIGHT_GREY);
grid->SetReadOnly(rowIndex, 1, true);
grid->SetReadOnly(rowIndex, 2, true);
break;
}
}
grid->SetCellEditor(rowIndex, 1, new wxGridCellChoiceEditor(types));
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnResize(wxSizeEvent &evt) {
/*
2016-11-05 15:28:14 +01:00
if(wxGrid* grid = (wxGrid*)currentPanel->FindWindowByName("AdvancedGrid")) {
int tWidth = 0;
2015-12-06 11:42:37 -03:00
for(int i = 0; i < 3; ++i)
tWidth += grid->GetColumnWidth(i);
int wWidth = grid->GetParent()->GetSize().GetWidth();
2016-09-29 19:24:45 -03:00
grid->SetColumnWidth(2, wWidth - 100 - 80);
}
*/
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnNotebookPageChanged(wxNotebookEvent &evt) {
wxWindow* page = notebook->GetCurrentPage();
// TODO: Save
2015-12-06 11:42:37 -03:00
switch (page->GetId()) {
case ITEM_PROPERTIES_GENERAL_TAB: {
2023-10-09 19:12:16 -07:00
// currentPanel = createGeneralPanel(page);
break;
}
2015-12-06 11:42:37 -03:00
case ITEM_PROPERTIES_ADVANCED_TAB: {
2023-10-09 19:12:16 -07:00
// currentPanel = createAttributesPanel(page);
break;
}
default:
break;
}
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::saveGeneralPanel() {
2015-12-06 11:42:37 -03:00
////
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::saveContainerPanel() {
2015-12-06 11:42:37 -03:00
////
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::saveAttributesPanel() {
edit_item->clearAllAttributes();
2023-10-09 19:12:16 -07:00
for (int32_t rowIndex = 0; rowIndex < attributesGrid->GetNumberRows(); ++rowIndex) {
ItemAttribute attr;
2014-01-23 14:33:10 +01:00
wxString type = attributesGrid->GetCellValue(rowIndex, 1);
2023-10-09 19:12:16 -07:00
if (type == "String") {
attr.set(nstr(attributesGrid->GetCellValue(rowIndex, 2)));
2023-10-09 19:12:16 -07:00
} else if (type == "Float") {
2014-01-23 14:33:10 +01:00
double value;
2023-10-09 19:12:16 -07:00
if (attributesGrid->GetCellValue(rowIndex, 2).ToDouble(&value)) {
2014-01-23 14:33:10 +01:00
attr.set(value);
}
2023-10-09 19:12:16 -07:00
} else if (type == "Number") {
2014-01-23 14:33:10 +01:00
long value;
2023-10-09 19:12:16 -07:00
if (attributesGrid->GetCellValue(rowIndex, 2).ToLong(&value)) {
2014-01-23 14:33:10 +01:00
attr.set(static_cast<int32_t>(value));
}
2023-10-09 19:12:16 -07:00
} else if (type == "Boolean") {
attr.set(attributesGrid->GetCellValue(rowIndex, 2) == "1");
2014-01-23 14:33:10 +01:00
} else {
continue;
2014-01-23 14:33:10 +01:00
}
edit_item->setAttribute(nstr(attributesGrid->GetCellValue(rowIndex, 0)), attr);
}
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnGridValueChanged(wxGridEvent &event) {
if (event.GetCol() == 1) {
wxString newType = attributesGrid->GetCellValue(event.GetRow(), 1);
2023-10-09 19:12:16 -07:00
if (newType == event.GetString()) {
return;
2014-01-23 17:35:45 +01:00
}
ItemAttribute attr;
2023-10-09 19:12:16 -07:00
if (newType == "String") {
attr.set("");
2023-10-09 19:12:16 -07:00
} else if (newType == "Float") {
attr.set(0.0f);
2023-10-09 19:12:16 -07:00
} else if (newType == "Number") {
attr.set(0);
2023-10-09 19:12:16 -07:00
} else if (newType == "Boolean") {
attr.set(false);
2014-01-23 17:35:45 +01:00
}
SetGridValue(attributesGrid, event.GetRow(), nstr(attributesGrid->GetCellValue(event.GetRow(), 0)), attr);
}
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnClickOK(wxCommandEvent &) {
saveAttributesPanel();
EndModal(1);
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnClickAddAttribute(wxCommandEvent &) {
attributesGrid->AppendRows(1);
ItemAttribute attr(0);
SetGridValue(attributesGrid, attributesGrid->GetNumberRows() - 1, "", attr);
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnClickRemoveAttribute(wxCommandEvent &) {
wxArrayInt rowIndexes = attributesGrid->GetSelectedRows();
2023-10-09 19:12:16 -07:00
if (rowIndexes.Count() != 1) {
return;
2023-10-09 19:12:16 -07:00
}
int rowIndex = rowIndexes[0];
attributesGrid->DeleteRows(rowIndex, 1);
}
2023-10-09 19:12:16 -07:00
void PropertiesWindow::OnClickCancel(wxCommandEvent &) {
EndModal(0);
}