tibia-rme/source/palette_window.cpp
Eduardo Dantas e09f3e94c8
Decouple npcs from the creature class (it's create npc.xml file in the map folder) (#3)
Created the complete organization of npcs, that way, it is possible to create own files for npcs, no longer depending on being together with the monsters
this pull request was made to be worked together with the canary project: https://github.com/opentibiabr/canary

The spawns npcs is "yellow" and spawns monsters is "red" (the old)

With this change, the map will automatically create an "npc.xml" the first time it is saved.
It is possible to edit only the xml, it is not necessary to add or modify the npc by opening the editor map

Thus, there is a script for npcs, similar to how it works with old spawns
2021-05-25 00:29:12 -03:00

455 lines
13 KiB
C++

//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// 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 "settings.h"
#include "gui.h"
#include "brush.h"
#include "map_display.h"
#include "palette_window.h"
#include "palette_brushlist.h"
#include "palette_house.h"
#include "palette_monster.h"
#include "palette_npc.h"
#include "palette_waypoints.h"
#include "house_brush.h"
#include "map.h"
// ============================================================================
// Palette window
BEGIN_EVENT_TABLE(PaletteWindow, wxPanel)
EVT_CHOICEBOOK_PAGE_CHANGING(PALETTE_CHOICEBOOK, PaletteWindow::OnSwitchingPage)
EVT_CHOICEBOOK_PAGE_CHANGED(PALETTE_CHOICEBOOK, PaletteWindow::OnPageChanged)
EVT_CLOSE(PaletteWindow::OnClose)
EVT_KEY_DOWN(PaletteWindow::OnKey)
END_EVENT_TABLE()
PaletteWindow::PaletteWindow(wxWindow* parent, const TilesetContainer& tilesets) :
wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(230, 250)),
choicebook(nullptr),
terrain_palette(nullptr),
doodad_palette(nullptr),
item_palette(nullptr),
monster_palette(nullptr),
npc_palette(nullptr),
house_palette(nullptr),
waypoint_palette(nullptr),
raw_palette(nullptr)
{
SetMinSize(wxSize(225, 250));
// Create choicebook
choicebook = newd wxChoicebook(this, PALETTE_CHOICEBOOK, wxDefaultPosition, wxSize(230, 250));
terrain_palette = static_cast<BrushPalettePanel*>(CreateTerrainPalette(choicebook, tilesets));
choicebook->AddPage(terrain_palette, terrain_palette->GetName());
doodad_palette = static_cast<BrushPalettePanel*>(CreateDoodadPalette(choicebook, tilesets));
choicebook->AddPage(doodad_palette, doodad_palette->GetName());
item_palette = static_cast<BrushPalettePanel*>(CreateItemPalette(choicebook, tilesets));
choicebook->AddPage(item_palette, item_palette->GetName());
house_palette = static_cast<HousePalettePanel*>(CreateHousePalette(choicebook, tilesets));
choicebook->AddPage(house_palette, house_palette->GetName());
waypoint_palette = static_cast<WaypointPalettePanel*>(CreateWaypointPalette(choicebook, tilesets));
choicebook->AddPage(waypoint_palette, waypoint_palette->GetName());
monster_palette = static_cast<MonsterPalettePanel*>(CreateMonsterPalette(choicebook, tilesets));
choicebook->AddPage(monster_palette, monster_palette->GetName());
npc_palette = static_cast<NpcPalettePanel*>(CreateNpcPalette(choicebook, tilesets));
choicebook->AddPage(npc_palette, npc_palette->GetName());
raw_palette = static_cast<BrushPalettePanel*>(CreateRAWPalette(choicebook, tilesets));
choicebook->AddPage(raw_palette, raw_palette->GetName());
// Setup sizers
wxSizer* sizer = newd wxBoxSizer(wxVERTICAL);
choicebook->SetMinSize(wxSize(225, 300));
sizer->Add(choicebook, 1, wxEXPAND);
SetSizer(sizer);
// Load first page
LoadCurrentContents();
Fit();
}
PaletteWindow::~PaletteWindow()
{
////
}
PalettePanel* PaletteWindow::CreateTerrainPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
BrushPalettePanel* panel = newd BrushPalettePanel(parent, tilesets, TILESET_TERRAIN);
panel->SetListType(wxstr(g_settings.getString(Config::PALETTE_TERRAIN_STYLE)));
BrushToolPanel* tool_panel = newd BrushToolPanel(panel);
tool_panel->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_TERRAIN_TOOLBAR));
panel->AddToolPanel(tool_panel);
BrushSizePanel* size_panel = newd BrushSizePanel(panel);
size_panel->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_TERRAIN_TOOLBAR));
panel->AddToolPanel(size_panel);
return panel;
}
PalettePanel* PaletteWindow::CreateDoodadPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
BrushPalettePanel* panel = newd BrushPalettePanel(parent, tilesets, TILESET_DOODAD);
panel->SetListType(wxstr(g_settings.getString(Config::PALETTE_DOODAD_STYLE)));
panel->AddToolPanel(newd BrushThicknessPanel(panel));
BrushSizePanel* size_panel = newd BrushSizePanel(panel);
size_panel->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_DOODAD_SIZEBAR));
panel->AddToolPanel(size_panel);
return panel;
}
PalettePanel* PaletteWindow::CreateItemPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
BrushPalettePanel* panel = newd BrushPalettePanel(parent, tilesets, TILESET_ITEM);
panel->SetListType(wxstr(g_settings.getString(Config::PALETTE_ITEM_STYLE)));
BrushSizePanel* size_panel = newd BrushSizePanel(panel);
size_panel->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_ITEM_SIZEBAR));
panel->AddToolPanel(size_panel);
return panel;
}
PalettePanel* PaletteWindow::CreateHousePalette(wxWindow *parent, const TilesetContainer& tilesets)
{
HousePalettePanel* panel = newd HousePalettePanel(parent);
BrushSizePanel* size_panel = newd BrushSizePanel(panel);
size_panel->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_HOUSE_SIZEBAR));
panel->AddToolPanel(size_panel);
return panel;
}
PalettePanel* PaletteWindow::CreateWaypointPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
WaypointPalettePanel* panel = newd WaypointPalettePanel(parent);
return panel;
}
PalettePanel* PaletteWindow::CreateMonsterPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
MonsterPalettePanel* panel = newd MonsterPalettePanel(parent);
return panel;
}
PalettePanel* PaletteWindow::CreateNpcPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
NpcPalettePanel* panel = newd NpcPalettePanel(parent);
return panel;
}
PalettePanel* PaletteWindow::CreateRAWPalette(wxWindow *parent, const TilesetContainer& tilesets)
{
BrushPalettePanel* panel = newd BrushPalettePanel(parent, tilesets, TILESET_RAW);
panel->SetListType(wxstr(g_settings.getString(Config::PALETTE_RAW_STYLE)));
BrushSizePanel* size_panel = newd BrushSizePanel(panel);
size_panel->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_RAW_SIZEBAR));
panel->AddToolPanel(size_panel);
return panel;
}
void PaletteWindow::ReloadSettings(Map* map)
{
if(terrain_palette) {
terrain_palette->SetListType(wxstr(g_settings.getString(Config::PALETTE_TERRAIN_STYLE)));
terrain_palette->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_TERRAIN_TOOLBAR));
}
if(doodad_palette) {
doodad_palette->SetListType(wxstr(g_settings.getString(Config::PALETTE_DOODAD_STYLE)));
doodad_palette->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_DOODAD_SIZEBAR));
}
if(house_palette) {
house_palette->SetMap(map);
house_palette->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_HOUSE_SIZEBAR));
}
if(waypoint_palette) {
waypoint_palette->SetMap(map);
}
if(item_palette) {
item_palette->SetListType(wxstr(g_settings.getString(Config::PALETTE_ITEM_STYLE)));
item_palette->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_ITEM_SIZEBAR));
}
if(raw_palette) {
raw_palette->SetListType(wxstr(g_settings.getString(Config::PALETTE_RAW_STYLE)));
raw_palette->SetToolbarIconSize(g_settings.getBoolean(Config::USE_LARGE_RAW_SIZEBAR));
}
InvalidateContents();
}
void PaletteWindow::LoadCurrentContents()
{
if(!choicebook) return;
PalettePanel* panel = dynamic_cast<PalettePanel*>(choicebook->GetCurrentPage());
panel->LoadCurrentContents();
Fit();
Refresh();
Update();
}
void PaletteWindow::InvalidateContents()
{
if(!choicebook) return;
for(size_t iz = 0; iz < choicebook->GetPageCount(); ++iz) {
PalettePanel* panel = dynamic_cast<PalettePanel*>(choicebook->GetPage(iz));
panel->InvalidateContents();
}
LoadCurrentContents();
if(monster_palette) {
monster_palette->OnUpdate();
}
if(npc_palette) {
npc_palette->OnUpdate();
}
if(house_palette) {
house_palette->OnUpdate();
}
if(waypoint_palette) {
waypoint_palette->OnUpdate();
}
}
void PaletteWindow::SelectPage(PaletteType id)
{
if(!choicebook) return;
if(id == GetSelectedPage()) {
return;
}
for(size_t iz = 0; iz < choicebook->GetPageCount(); ++iz) {
PalettePanel* panel = dynamic_cast<PalettePanel*>(choicebook->GetPage(iz));
if(panel->GetType() == id) {
choicebook->SetSelection(iz);
//LoadCurrentContents();
break;
}
}
}
Brush* PaletteWindow::GetSelectedBrush() const
{
if(!choicebook) return nullptr;
PalettePanel* panel = dynamic_cast<PalettePanel*>(choicebook->GetCurrentPage());
return panel->GetSelectedBrush();
}
int PaletteWindow::GetSelectedBrushSize() const
{
if(!choicebook) return 0;
PalettePanel* panel = dynamic_cast<PalettePanel*>(choicebook->GetCurrentPage());
return panel->GetSelectedBrushSize();
}
PaletteType PaletteWindow::GetSelectedPage() const
{
if(!choicebook) return TILESET_UNKNOWN;
PalettePanel* panel = dynamic_cast<PalettePanel*>(choicebook->GetCurrentPage());
ASSERT(panel);
return panel->GetType();
}
bool PaletteWindow::OnSelectBrush(const Brush* whatbrush, PaletteType primary)
{
if(!choicebook || !whatbrush)
return false;
if(whatbrush->isHouse() && house_palette) {
house_palette->SelectBrush(whatbrush);
SelectPage(TILESET_HOUSE);
return true;
}
switch(primary) {
case TILESET_TERRAIN: {
// This is already searched first
break;
}
case TILESET_DOODAD: {
// Ok, search doodad before terrain
if(doodad_palette && doodad_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_DOODAD);
return true;
}
break;
}
case TILESET_ITEM: {
if(item_palette && item_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_ITEM);
return true;
}
break;
}
case TILESET_MONSTER: {
if(monster_palette && monster_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_MONSTER);
return true;
}
break;
}
case TILESET_NPC: {
if(npc_palette && npc_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_NPC);
return true;
}
break;
}
case TILESET_RAW: {
if(raw_palette && raw_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_RAW);
return true;
}
break;
}
default:
break;
}
// Test if it's a terrain brush
if(terrain_palette && terrain_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_TERRAIN);
return true;
}
// Test if it's a doodad brush
if(primary != TILESET_DOODAD) {
if(doodad_palette && doodad_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_DOODAD);
return true;
}
}
// Test if it's an item brush
if(primary != TILESET_ITEM) {
if(item_palette && item_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_ITEM);
return true;
}
}
// Test if it's a monster brush
if(primary != TILESET_MONSTER) {
if(monster_palette && monster_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_MONSTER);
return true;
}
}
// Test if it's a npc brush
if(primary != TILESET_NPC) {
if(npc_palette && npc_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_NPC);
return true;
}
}
// Test if it's a raw brush
if(primary != TILESET_RAW) {
if(raw_palette && raw_palette->SelectBrush(whatbrush)) {
SelectPage(TILESET_RAW);
return true;
}
}
return false;
}
void PaletteWindow::OnSwitchingPage(wxChoicebookEvent& event)
{
event.Skip();
if(!choicebook) return;
wxWindow* old_page = choicebook->GetPage(choicebook->GetSelection());
PalettePanel* old_panel = dynamic_cast<PalettePanel*>(old_page);
if(old_panel) {
old_panel->OnSwitchOut();
}
wxWindow* page = choicebook->GetPage(event.GetSelection());
PalettePanel* panel = dynamic_cast<PalettePanel*>(page);
if(panel) {
panel->OnSwitchIn();
}
}
void PaletteWindow::OnPageChanged(wxChoicebookEvent& event)
{
if(!choicebook) return;
g_gui.SelectBrush();
}
void PaletteWindow::OnUpdateBrushSize(BrushShape shape, int size)
{
if(!choicebook) return;
PalettePanel* page = dynamic_cast<PalettePanel*>(choicebook->GetCurrentPage());
ASSERT(page);
page->OnUpdateBrushSize(shape, size);
}
void PaletteWindow::OnUpdate(Map* map)
{
if(monster_palette) {
monster_palette->OnUpdate();
}
if(npc_palette) {
npc_palette->OnUpdate();
}
if(house_palette) {
house_palette->SetMap(map);
}
if(waypoint_palette) {
waypoint_palette->SetMap(map);
waypoint_palette->OnUpdate();
}
}
void PaletteWindow::OnKey(wxKeyEvent& event)
{
if(g_gui.GetCurrentTab() != nullptr) {
g_gui.GetCurrentMapTab()->GetEventHandler()->AddPendingEvent(event);
}
}
void PaletteWindow::OnClose(wxCloseEvent& event)
{
if(!event.CanVeto()) {
// We can't do anything! This sucks!
// (application is closed, we have to destroy ourselves)
Destroy();
} else {
Show(false);
event.Veto(true);
}
}