2012-06-29 16:44:26 +02:00
//////////////////////////////////////////////////////////////////////
// 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/>.
//////////////////////////////////////////////////////////////////////
2012-06-29 16:44:26 +02:00
# include "main.h"
# include "materials.h"
# include "brush.h"
# include "editor.h"
# include "items.h"
# include "map.h"
# include "item.h"
# include "complexitem.h"
# include "raw_brush.h"
# include "palette_window.h"
# include "gui.h"
# include "application.h"
# include "common_windows.h"
2015-07-11 21:20:07 -03:00
# include "positionctrl.h"
2012-06-29 16:44:26 +02:00
2014-08-02 18:29:37 -05:00
# ifdef _MSC_VER
# pragma warning(disable:4018) // signed/unsigned mismatch
2014-08-02 13:09:03 -05:00
# endif
2012-06-29 16:44:26 +02:00
// ============================================================================
// Map Properties Window
BEGIN_EVENT_TABLE ( MapPropertiesWindow , wxDialog )
EVT_CHOICE ( MAP_PROPERTIES_VERSION , MapPropertiesWindow : : OnChangeVersion )
EVT_BUTTON ( wxID_OK , MapPropertiesWindow : : OnClickOK )
EVT_BUTTON ( wxID_CANCEL , MapPropertiesWindow : : OnClickCancel )
END_EVENT_TABLE ( )
MapPropertiesWindow : : MapPropertiesWindow ( wxWindow * parent , MapTab * view , Editor & editor ) :
2016-11-05 15:28:14 +01:00
wxDialog ( parent , wxID_ANY , " Map Properties " , wxDefaultPosition , wxSize ( 300 , 200 ) , wxRESIZE_BORDER | wxCAPTION ) ,
2012-06-29 16:44:26 +02:00
view ( view ) ,
editor ( editor )
{
// Setup data variabels
Map & map = editor . map ;
wxSizer * topsizer = newd wxBoxSizer ( wxVERTICAL ) ;
2012-06-30 11:41:07 +02:00
wxFlexGridSizer * grid_sizer = newd wxFlexGridSizer ( 2 , 10 , 10 ) ;
2012-06-29 16:44:26 +02:00
grid_sizer - > AddGrowableCol ( 1 ) ;
// Description
2016-11-05 15:28:14 +01:00
grid_sizer - > Add ( newd wxStaticText ( this , wxID_ANY , " Map Description " ) ) ;
2012-06-29 16:44:26 +02:00
description_ctrl = newd wxTextCtrl ( this , wxID_ANY , wxstr ( map . getMapDescription ( ) ) , wxDefaultPosition , wxDefaultSize , wxTE_MULTILINE ) ;
grid_sizer - > Add ( description_ctrl , wxSizerFlags ( 1 ) . Expand ( ) ) ;
// Map version
2016-11-05 15:28:14 +01:00
grid_sizer - > Add ( newd wxStaticText ( this , wxID_ANY , " Map Version " ) ) ;
2012-06-29 16:44:26 +02:00
version_choice = newd wxChoice ( this , MAP_PROPERTIES_VERSION ) ;
2016-11-05 15:28:14 +01:00
version_choice - > Append ( " OTServ 0.5.0 " ) ;
version_choice - > Append ( " OTServ 0.6.0 " ) ;
version_choice - > Append ( " OTServ 0.6.1 " ) ;
version_choice - > Append ( " OTServ 0.7.0 (revscriptsys) " ) ;
2012-06-29 16:44:26 +02:00
2015-12-06 11:42:37 -03:00
switch ( map . getVersion ( ) . otbm ) {
2012-06-29 16:44:26 +02:00
case MAP_OTBM_1 :
version_choice - > SetSelection ( 0 ) ;
break ;
case MAP_OTBM_2 :
version_choice - > SetSelection ( 1 ) ;
break ;
case MAP_OTBM_3 :
version_choice - > SetSelection ( 2 ) ;
break ;
case MAP_OTBM_4 :
version_choice - > SetSelection ( 3 ) ;
break ;
default :
version_choice - > SetSelection ( 0 ) ;
}
2016-09-29 18:36:27 -03:00
2012-06-29 16:44:26 +02:00
grid_sizer - > Add ( version_choice , wxSizerFlags ( 1 ) . Expand ( ) ) ;
// Version
2016-11-05 15:28:14 +01:00
grid_sizer - > Add ( newd wxStaticText ( this , wxID_ANY , " Client Version " ) ) ;
2012-06-29 16:44:26 +02:00
protocol_choice = newd wxChoice ( this , wxID_ANY ) ;
2016-09-29 18:36:27 -03:00
2016-09-29 20:30:54 -03:00
protocol_choice - > SetStringSelection ( wxstr ( g_gui . GetCurrentVersion ( ) . getName ( ) ) ) ;
2012-06-29 16:44:26 +02:00
grid_sizer - > Add ( protocol_choice , wxSizerFlags ( 1 ) . Expand ( ) ) ;
// Dimensions
2016-11-05 15:28:14 +01:00
grid_sizer - > Add ( newd wxStaticText ( this , wxID_ANY , " Map Dimensions " ) ) ;
2012-06-29 16:44:26 +02:00
{
wxSizer * subsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
subsizer - > Add (
2016-09-29 18:36:27 -03:00
width_spin =
2012-06-29 16:44:26 +02:00
newd wxSpinCtrl ( this , wxID_ANY , wxstr ( i2s ( map . getWidth ( ) ) ) ,
2016-02-25 14:21:27 -03:00
wxDefaultPosition , wxDefaultSize , wxSP_ARROW_KEYS , 256 , MAP_MAX_WIDTH ) , wxSizerFlags ( 1 ) . Expand ( )
2012-06-29 16:44:26 +02:00
) ;
subsizer - > Add (
2016-09-29 18:36:27 -03:00
height_spin =
2012-06-29 16:44:26 +02:00
newd wxSpinCtrl ( this , wxID_ANY , wxstr ( i2s ( map . getHeight ( ) ) ) ,
2016-02-25 14:21:27 -03:00
wxDefaultPosition , wxDefaultSize , wxSP_ARROW_KEYS , 256 , MAP_MAX_HEIGHT ) , wxSizerFlags ( 1 ) . Expand ( )
2012-06-29 16:44:26 +02:00
) ;
grid_sizer - > Add ( subsizer , 1 , wxEXPAND ) ;
}
// External files
grid_sizer - > Add (
2021-05-25 00:29:12 -03:00
newd wxStaticText ( this , wxID_ANY , " External house file " )
2012-06-29 16:44:26 +02:00
) ;
grid_sizer - > Add (
2016-09-29 18:36:27 -03:00
house_filename_ctrl =
2012-06-29 16:44:26 +02:00
newd wxTextCtrl ( this , wxID_ANY , wxstr ( map . getHouseFilename ( ) ) ) , 1 , wxEXPAND
) ;
grid_sizer - > Add (
2021-05-25 00:29:12 -03:00
newd wxStaticText ( this , wxID_ANY , " External monster file " )
2012-06-29 16:44:26 +02:00
) ;
grid_sizer - > Add (
spawn_filename_ctrl =
newd wxTextCtrl ( this , wxID_ANY , wxstr ( map . getSpawnFilename ( ) ) ) , 1 , wxEXPAND
) ;
2021-05-25 00:29:12 -03:00
grid_sizer - > Add (
newd wxStaticText ( this , wxID_ANY , " External npc file " )
) ;
grid_sizer - > Add (
spawn_npc_filename_ctrl =
newd wxTextCtrl ( this , wxID_ANY , wxstr ( map . getSpawnNpcFilename ( ) ) ) , 1 , wxEXPAND
) ;
2012-06-29 16:44:26 +02:00
topsizer - > Add ( grid_sizer , wxSizerFlags ( 1 ) . Expand ( ) . Border ( wxALL , 20 ) ) ;
wxSizer * subsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
subsizer - > Add ( newd wxButton ( this , wxID_OK , " OK " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
subsizer - > Add ( newd wxButton ( this , wxID_CANCEL , " Cancel " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
2012-06-29 16:44:26 +02:00
topsizer - > Add ( subsizer , wxSizerFlags ( 0 ) . Center ( ) . Border ( wxLEFT | wxRIGHT | wxBOTTOM , 20 ) ) ;
SetSizerAndFit ( topsizer ) ;
2016-02-29 21:47:54 -03:00
Centre ( wxBOTH ) ;
2012-06-29 16:44:26 +02:00
UpdateProtocolList ( ) ;
2012-07-01 17:52:20 +02:00
ClientVersion * current_version = ClientVersion : : get ( map . getVersion ( ) . client ) ;
protocol_choice - > SetStringSelection ( wxstr ( current_version - > getName ( ) ) ) ;
2012-06-29 16:44:26 +02:00
}
void MapPropertiesWindow : : UpdateProtocolList ( )
{
wxString ver = version_choice - > GetStringSelection ( ) ;
wxString client = protocol_choice - > GetStringSelection ( ) ;
protocol_choice - > Clear ( ) ;
2016-09-29 18:36:27 -03:00
2012-07-01 13:41:59 +02:00
ClientVersionList versions ;
2016-09-29 20:37:43 -03:00
if ( g_settings . getInteger ( Config : : USE_OTBM_4_FOR_ALL_MAPS ) ) {
2012-07-01 13:41:59 +02:00
versions = ClientVersion : : getAllVisible ( ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-07-01 13:41:59 +02:00
MapVersionID map_version = MAP_OTBM_1 ;
2016-11-05 15:28:14 +01:00
if ( ver . Contains ( " 0.5.0 " ) )
2012-07-01 13:41:59 +02:00
map_version = MAP_OTBM_1 ;
2016-11-05 15:28:14 +01:00
else if ( ver . Contains ( " 0.6.0 " ) )
2012-07-01 13:41:59 +02:00
map_version = MAP_OTBM_2 ;
2016-11-05 15:28:14 +01:00
else if ( ver . Contains ( " 0.6.1 " ) )
2012-07-01 13:41:59 +02:00
map_version = MAP_OTBM_3 ;
2016-11-05 15:28:14 +01:00
else if ( ver . Contains ( " 0.7.0 " ) )
2012-07-01 13:41:59 +02:00
map_version = MAP_OTBM_4 ;
2012-07-01 17:52:20 +02:00
ClientVersionList protocols = ClientVersion : : getAllForOTBMVersion ( map_version ) ;
2015-12-06 11:42:37 -03:00
for ( ClientVersionList : : const_iterator p = protocols . begin ( ) ; p ! = protocols . end ( ) ; + + p )
2012-07-01 17:52:20 +02:00
protocol_choice - > Append ( wxstr ( ( * p ) - > getName ( ) ) ) ;
2012-06-29 16:44:26 +02:00
}
protocol_choice - > SetSelection ( 0 ) ;
protocol_choice - > SetStringSelection ( client ) ;
}
void MapPropertiesWindow : : OnChangeVersion ( wxCommandEvent & )
{
UpdateProtocolList ( ) ;
}
2012-07-05 17:32:36 +02:00
struct MapConversionContext
{
2021-05-25 00:29:12 -03:00
struct MonsterInfo
2012-07-05 17:32:36 +02:00
{
std : : string name ;
Outfit outfit ;
} ;
2021-05-25 00:29:12 -03:00
typedef std : : map < std : : string , MonsterInfo > MonsterMap ;
MonsterMap monsterType ;
struct NpcInfo
{
std : : string name ;
Outfit outfit ;
} ;
typedef std : : map < std : : string , NpcInfo > NpcMap ;
NpcMap npcType ;
2012-07-05 17:32:36 +02:00
void operator ( ) ( Map & map , Tile * tile , long long done )
{
2021-05-25 00:29:12 -03:00
if ( tile - > monster ) {
MonsterMap : : iterator f = monsterType . find ( tile - > monster - > getName ( ) ) ;
if ( f = = monsterType . end ( ) ) {
MonsterInfo info = {
tile - > monster - > getName ( ) ,
tile - > monster - > getLookType ( )
} ;
monsterType [ tile - > monster - > getName ( ) ] = info ;
}
}
if ( tile - > npc ) {
NpcMap : : iterator f = npcType . find ( tile - > npc - > getName ( ) ) ;
if ( f = = npcType . end ( ) ) {
NpcInfo info = {
tile - > npc - > getName ( ) ,
tile - > npc - > getLookType ( )
2012-07-05 17:32:36 +02:00
} ;
2021-05-25 00:29:12 -03:00
npcType [ tile - > npc - > getName ( ) ] = info ;
2012-07-05 17:32:36 +02:00
}
}
}
} ;
2016-09-29 18:36:27 -03:00
void MapPropertiesWindow : : OnClickOK ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
Map & map = editor . map ;
MapVersion old_ver = map . getVersion ( ) ;
MapVersion new_ver ;
2016-09-29 18:36:27 -03:00
2012-06-29 16:44:26 +02:00
wxString ver = version_choice - > GetStringSelection ( ) ;
new_ver . client = ClientVersion : : get ( nstr ( protocol_choice - > GetStringSelection ( ) ) ) - > getID ( ) ;
2016-11-05 15:28:14 +01:00
if ( ver . Contains ( " 0.5.0 " ) ) {
2012-06-29 16:44:26 +02:00
new_ver . otbm = MAP_OTBM_1 ;
2016-11-05 15:28:14 +01:00
} else if ( ver . Contains ( " 0.6.0 " ) ) {
2012-06-29 16:44:26 +02:00
new_ver . otbm = MAP_OTBM_2 ;
2016-11-05 15:28:14 +01:00
} else if ( ver . Contains ( " 0.6.1 " ) ) {
2012-06-29 16:44:26 +02:00
new_ver . otbm = MAP_OTBM_3 ;
2016-11-05 15:28:14 +01:00
} else if ( ver . Contains ( " 0.7.0 " ) ) {
2012-06-29 16:44:26 +02:00
new_ver . otbm = MAP_OTBM_4 ;
}
2015-12-06 11:42:37 -03:00
if ( new_ver . client ! = old_ver . client ) {
2016-09-29 20:30:54 -03:00
if ( g_gui . GetOpenMapCount ( ) > 1 ) {
2016-11-05 15:28:14 +01:00
g_gui . PopupDialog ( this , " Error " ,
" You can not change editor version with multiple maps open " , wxOK ) ;
2012-06-29 16:44:26 +02:00
return ;
}
wxString error ;
wxArrayString warnings ;
// Switch version
2016-09-29 20:30:54 -03:00
g_gui . GetCurrentEditor ( ) - > selection . clear ( ) ;
g_gui . GetCurrentEditor ( ) - > actionQueue - > clear ( ) ;
2012-06-29 16:44:26 +02:00
2016-01-17 08:12:13 -03:00
if ( new_ver . client < old_ver . client ) {
2016-11-05 15:28:14 +01:00
int ret = g_gui . PopupDialog ( this , " Notice " ,
" Converting to a previous version may have serious side-effects, are you sure you want to do this? " , wxYES | wxNO ) ;
2016-01-17 08:12:13 -03:00
if ( ret ! = wxID_YES ) {
2012-06-29 16:44:26 +02:00
return ;
}
2012-07-05 17:32:36 +02:00
UnnamedRenderingLock ( ) ;
2021-05-25 00:29:12 -03:00
// Remember all monsters types on the map
2012-07-05 17:32:36 +02:00
MapConversionContext conversion_context ;
foreach_TileOnMap ( map , conversion_context ) ;
2012-06-29 16:44:26 +02:00
2016-09-29 18:36:27 -03:00
// Perform the conversion
2012-06-29 16:44:26 +02:00
map . convert ( new_ver , true ) ;
2012-07-05 17:32:36 +02:00
// Load the new version
2016-09-29 20:30:54 -03:00
if ( ! g_gui . LoadVersion ( new_ver . client , error , warnings ) ) {
2016-11-05 15:28:14 +01:00
g_gui . ListDialog ( this , " Warnings " , warnings ) ;
g_gui . PopupDialog ( this , " Map Loader Error " , error , wxOK ) ;
g_gui . PopupDialog ( this , " Conversion Error " , " Could not convert map. The map will now be closed. " , wxOK ) ;
2012-06-29 16:44:26 +02:00
EndModal ( 0 ) ;
return ;
}
2021-05-25 00:29:12 -03:00
// Monsters
// Remove all monsters that were present are present in the new version
for ( MapConversionContext : : MonsterMap : : iterator cs = conversion_context . monsterType . begin ( ) ; cs ! = conversion_context . monsterType . end ( ) ; ) {
if ( g_monsters [ cs - > first ] )
cs = conversion_context . monsterType . erase ( cs ) ;
else
+ + cs ;
}
if ( conversion_context . monsterType . size ( ) > 0 ) {
int add = g_gui . PopupDialog ( this , " Unrecognized monsters " , " There were monsters on the old version that are not present in this and were on the map, do you want to add them to this version as well? " , wxYES | wxNO ) ;
if ( add = = wxID_YES ) {
for ( MapConversionContext : : MonsterMap : : iterator cs = conversion_context . monsterType . begin ( ) ; cs ! = conversion_context . monsterType . end ( ) ; + + cs ) {
MapConversionContext : : MonsterInfo info = cs - > second ;
g_monsters . addMonsterType ( info . name , info . outfit ) ;
}
}
}
// Npcs
// Remove all npcs that were present are present in the new version
for ( MapConversionContext : : NpcMap : : iterator cs = conversion_context . npcType . begin ( ) ; cs ! = conversion_context . npcType . end ( ) ; ) {
if ( g_npcs [ cs - > first ] )
cs = conversion_context . npcType . erase ( cs ) ;
2012-07-05 17:32:36 +02:00
else
+ + cs ;
}
2021-05-25 00:29:12 -03:00
if ( conversion_context . npcType . size ( ) > 0 ) {
int add = g_gui . PopupDialog ( this , " Unrecognized npcs " , " There were npcs on the old version that are not present in this and were on the map, do you want to add them to this version as well? " , wxYES | wxNO ) ;
2015-12-06 11:42:37 -03:00
if ( add = = wxID_YES ) {
2021-05-25 00:29:12 -03:00
for ( MapConversionContext : : NpcMap : : iterator cs = conversion_context . npcType . begin ( ) ; cs ! = conversion_context . npcType . end ( ) ; + + cs ) {
MapConversionContext : : NpcInfo info = cs - > second ;
g_npcs . addNpcType ( info . name , info . outfit ) ;
2012-07-05 17:32:36 +02:00
}
}
}
2012-06-29 16:44:26 +02:00
map . cleanInvalidTiles ( true ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
UnnamedRenderingLock ( ) ;
2016-09-29 20:30:54 -03:00
if ( ! g_gui . LoadVersion ( new_ver . client , error , warnings ) ) {
2016-11-05 15:28:14 +01:00
g_gui . ListDialog ( this , " Warnings " , warnings ) ;
g_gui . PopupDialog ( this , " Map Loader Error " , error , wxOK ) ;
g_gui . PopupDialog ( this , " Conversion Error " , " Could not convert map. The map will now be closed. " , wxOK ) ;
2012-06-29 16:44:26 +02:00
EndModal ( 0 ) ;
return ;
}
map . convert ( new_ver , true ) ;
}
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
map . convert ( new_ver , true ) ;
}
map . setMapDescription ( nstr ( description_ctrl - > GetValue ( ) ) ) ;
map . setHouseFilename ( nstr ( house_filename_ctrl - > GetValue ( ) ) ) ;
2021-05-25 00:29:12 -03:00
map . setSpawnMonsterFilename ( nstr ( spawn_filename_ctrl - > GetValue ( ) ) ) ;
map . setSpawnNpcFilename ( nstr ( spawn_npc_filename_ctrl - > GetValue ( ) ) ) ;
2012-06-29 16:44:26 +02:00
// Only resize if we have to
int new_map_width = width_spin - > GetValue ( ) ;
int new_map_height = height_spin - > GetValue ( ) ;
2016-01-17 08:12:13 -03:00
if ( new_map_width ! = map . getWidth ( ) | | new_map_height ! = map . getHeight ( ) ) {
2012-06-29 16:44:26 +02:00
map . setWidth ( new_map_width ) ;
map . setHeight ( new_map_height ) ;
2016-09-29 20:30:54 -03:00
g_gui . FitViewToMap ( view ) ;
2012-06-29 16:44:26 +02:00
}
2016-09-29 20:30:54 -03:00
g_gui . RefreshPalettes ( ) ;
2012-06-29 16:44:26 +02:00
EndModal ( 1 ) ;
}
2016-09-29 18:36:27 -03:00
void MapPropertiesWindow : : OnClickCancel ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
// Just close this window
EndModal ( 1 ) ;
}
2019-06-25 18:07:36 +02:00
MapPropertiesWindow : : ~ MapPropertiesWindow ( ) = default ;
2012-06-29 16:44:26 +02:00
// ============================================================================
// Map Import Window
BEGIN_EVENT_TABLE ( ImportMapWindow , wxDialog )
EVT_BUTTON ( MAP_WINDOW_FILE_BUTTON , ImportMapWindow : : OnClickBrowse )
EVT_BUTTON ( wxID_OK , ImportMapWindow : : OnClickOK )
EVT_BUTTON ( wxID_CANCEL , ImportMapWindow : : OnClickCancel )
END_EVENT_TABLE ( )
ImportMapWindow : : ImportMapWindow ( wxWindow * parent , Editor & editor ) :
2022-04-03 03:10:34 +02:00
wxDialog ( parent , wxID_ANY , " Import Map " , wxDefaultPosition , wxSize ( 420 , 315 ) ) ,
2012-06-29 16:44:26 +02:00
editor ( editor )
{
2016-02-27 20:19:33 -03:00
wxBoxSizer * sizer = newd wxBoxSizer ( wxVERTICAL ) ;
wxStaticBoxSizer * tmpsizer ;
2012-06-29 16:44:26 +02:00
// File
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( new wxStaticBox ( this , wxID_ANY , " Map File " ) , wxHORIZONTAL ) ;
2022-04-03 03:10:34 +02:00
file_text_field = newd wxTextCtrl ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , " " , wxDefaultPosition , wxSize ( 300 , 23 ) ) ;
2016-02-27 20:19:33 -03:00
tmpsizer - > Add ( file_text_field , 0 , wxALL , 5 ) ;
2016-11-05 15:28:14 +01:00
wxButton * browse_button = newd wxButton ( tmpsizer - > GetStaticBox ( ) , MAP_WINDOW_FILE_BUTTON , " Browse... " , wxDefaultPosition , wxSize ( 80 , 23 ) ) ;
2016-02-27 20:19:33 -03:00
tmpsizer - > Add ( browse_button , 0 , wxALL , 5 ) ;
sizer - > Add ( tmpsizer , 1 , wxEXPAND | wxLEFT | wxRIGHT | wxTOP , 5 ) ;
2012-06-29 16:44:26 +02:00
// Import offset
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( new wxStaticBox ( this , wxID_ANY , " Import Offset " ) , wxHORIZONTAL ) ;
tmpsizer - > Add ( newd wxStaticText ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , " Offset X: " ) , 0 , wxALL | wxEXPAND , 5 ) ;
2022-04-03 03:10:34 +02:00
x_offset_ctrl = newd wxSpinCtrl ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , wxEmptyString , wxDefaultPosition , wxSize ( 60 , 23 ) , wxSP_ARROW_KEYS , - MAP_MAX_HEIGHT , MAP_MAX_HEIGHT ) ;
2016-02-27 20:19:33 -03:00
tmpsizer - > Add ( x_offset_ctrl , 0 , wxALL , 5 ) ;
2016-11-05 15:28:14 +01:00
tmpsizer - > Add ( newd wxStaticText ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , " Offset Y: " ) , 0 , wxALL , 5 ) ;
2022-04-03 03:10:34 +02:00
y_offset_ctrl = newd wxSpinCtrl ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , wxEmptyString , wxDefaultPosition , wxSize ( 60 , 23 ) , wxSP_ARROW_KEYS , - MAP_MAX_HEIGHT , MAP_MAX_HEIGHT ) ;
2016-02-27 20:19:33 -03:00
tmpsizer - > Add ( y_offset_ctrl , 0 , wxALL , 5 ) ;
2022-04-03 03:10:34 +02:00
tmpsizer - > Add ( newd wxStaticText ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , " Offset Z: " ) , 0 , wxALL , 5 ) ;
z_offset_ctrl = newd wxSpinCtrl ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , wxEmptyString , wxDefaultPosition , wxSize ( 60 , 23 ) , wxSP_ARROW_KEYS , - MAP_MAX_LAYER , MAP_MAX_LAYER ) ;
tmpsizer - > Add ( z_offset_ctrl , 0 , wxALL , 5 ) ;
2016-02-27 20:19:33 -03:00
sizer - > Add ( tmpsizer , 1 , wxEXPAND | wxLEFT | wxRIGHT , 5 ) ;
2012-06-29 16:44:26 +02:00
// Import options
wxArrayString house_choices ;
2016-11-05 15:28:14 +01:00
house_choices . Add ( " Smart Merge " ) ;
house_choices . Add ( " Insert " ) ;
house_choices . Add ( " Merge " ) ;
house_choices . Add ( " Don't Import " ) ;
2012-06-29 16:44:26 +02:00
// House options
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( new wxStaticBox ( this , wxID_ANY , " House Import Behaviour " ) , wxVERTICAL ) ;
2016-02-27 20:19:33 -03:00
house_options = newd wxChoice ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , wxDefaultPosition , wxDefaultSize , house_choices ) ;
2012-06-29 16:44:26 +02:00
house_options - > SetSelection ( 0 ) ;
2016-02-27 20:19:33 -03:00
tmpsizer - > Add ( house_options , 0 , wxALL | wxEXPAND , 5 ) ;
sizer - > Add ( tmpsizer , 1 , wxEXPAND | wxLEFT | wxRIGHT , 5 ) ;
2012-06-29 16:44:26 +02:00
// Import options
wxArrayString spawn_choices ;
2016-11-05 15:28:14 +01:00
spawn_choices . Add ( " Merge " ) ;
spawn_choices . Add ( " Don't Import " ) ;
2012-06-29 16:44:26 +02:00
2021-05-25 00:29:12 -03:00
// Monster spawn options
tmpsizer = newd wxStaticBoxSizer ( new wxStaticBox ( this , wxID_ANY , " Monster Spawn Import Behaviour " ) , wxVERTICAL ) ;
spawn_monster_options = newd wxChoice ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , wxDefaultPosition , wxDefaultSize , spawn_choices ) ;
spawn_monster_options - > SetSelection ( 0 ) ;
tmpsizer - > Add ( spawn_monster_options , 0 , wxALL | wxEXPAND , 5 ) ;
sizer - > Add ( tmpsizer , 1 , wxEXPAND | wxLEFT | wxRIGHT , 5 ) ;
// Npc spawn options
tmpsizer = newd wxStaticBoxSizer ( new wxStaticBox ( this , wxID_ANY , " Npc Spawn Import Behaviour " ) , wxVERTICAL ) ;
spawn_npc_options = newd wxChoice ( tmpsizer - > GetStaticBox ( ) , wxID_ANY , wxDefaultPosition , wxDefaultSize , spawn_choices ) ;
spawn_npc_options - > SetSelection ( 0 ) ;
tmpsizer - > Add ( spawn_npc_options , 0 , wxALL | wxEXPAND , 5 ) ;
2016-02-27 20:19:33 -03:00
sizer - > Add ( tmpsizer , 1 , wxEXPAND | wxLEFT | wxRIGHT , 5 ) ;
2012-06-29 16:44:26 +02:00
// OK/Cancel buttons
2016-02-27 20:19:33 -03:00
wxBoxSizer * buttons = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
buttons - > Add ( newd wxButton ( this , wxID_OK , " Ok " ) , 0 , wxALL , 5 ) ;
buttons - > Add ( newd wxButton ( this , wxID_CANCEL , " Cancel " ) , 0 , wxALL , 5 ) ;
2016-02-27 20:19:33 -03:00
sizer - > Add ( buttons , wxSizerFlags ( 1 ) . Center ( ) ) ;
SetSizer ( sizer ) ;
Layout ( ) ;
Centre ( wxBOTH ) ;
2012-06-29 16:44:26 +02:00
}
2019-06-25 18:07:36 +02:00
ImportMapWindow : : ~ ImportMapWindow ( ) = default ;
2012-06-29 16:44:26 +02:00
void ImportMapWindow : : OnClickBrowse ( wxCommandEvent & WXUNUSED ( event ) )
{
2016-11-05 15:28:14 +01:00
wxFileDialog dialog ( this , " Import... " , " " , " " , " *.otbm " , wxFD_OPEN | wxFD_FILE_MUST_EXIST ) ;
2016-02-27 20:19:33 -03:00
int ok = dialog . ShowModal ( ) ;
2012-06-29 16:44:26 +02:00
2016-02-27 20:19:33 -03:00
if ( ok = = wxID_OK )
file_text_field - > ChangeValue ( dialog . GetPath ( ) ) ;
2012-06-29 16:44:26 +02:00
}
2016-09-29 18:36:27 -03:00
void ImportMapWindow : : OnClickOK ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
if ( Validate ( ) & & TransferDataFromWindow ( ) ) {
2016-02-27 20:19:33 -03:00
wxFileName fn = file_text_field - > GetValue ( ) ;
2016-01-17 08:12:13 -03:00
if ( ! fn . FileExists ( ) ) {
2016-11-05 15:28:14 +01:00
g_gui . PopupDialog ( this , " Error " , " The specified map file doesn't exist " , wxOK ) ;
2012-06-29 16:44:26 +02:00
return ;
}
ImportType spawn_import_type = IMPORT_DONT ;
2021-05-25 00:29:12 -03:00
ImportType spawn_npc_import_type = IMPORT_DONT ;
2012-06-29 16:44:26 +02:00
ImportType house_import_type = IMPORT_DONT ;
2021-05-25 00:29:12 -03:00
switch ( spawn_monster_options - > GetSelection ( ) ) {
2012-06-29 16:44:26 +02:00
case 0 : spawn_import_type = IMPORT_MERGE ; break ;
case 1 : spawn_import_type = IMPORT_DONT ; break ;
}
2021-05-25 00:29:12 -03:00
switch ( spawn_npc_options - > GetSelection ( ) ) {
case 0 : spawn_npc_import_type = IMPORT_MERGE ; break ;
case 1 : spawn_npc_import_type = IMPORT_DONT ; break ;
}
2016-01-17 08:12:13 -03:00
switch ( house_options - > GetSelection ( ) ) {
2012-06-29 16:44:26 +02:00
case 0 : house_import_type = IMPORT_SMART_MERGE ; break ;
case 1 : house_import_type = IMPORT_MERGE ; break ;
case 2 : house_import_type = IMPORT_INSERT ; break ;
case 3 : house_import_type = IMPORT_DONT ; break ;
}
EndModal ( 1 ) ;
2022-08-19 02:50:53 -03:00
editor . importMap ( fn , x_offset_ctrl - > GetValue ( ) , y_offset_ctrl - > GetValue ( ) , z_offset_ctrl - > GetValue ( ) , house_import_type , spawn_import_type , spawn_npc_import_type ) ;
2012-06-29 16:44:26 +02:00
}
}
2016-09-29 18:36:27 -03:00
void ImportMapWindow : : OnClickCancel ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
// Just close this window
EndModal ( 0 ) ;
}
// ============================================================================
// Export Minimap window
BEGIN_EVENT_TABLE ( ExportMiniMapWindow , wxDialog )
EVT_BUTTON ( MAP_WINDOW_FILE_BUTTON , ExportMiniMapWindow : : OnClickBrowse )
EVT_BUTTON ( wxID_OK , ExportMiniMapWindow : : OnClickOK )
EVT_BUTTON ( wxID_CANCEL , ExportMiniMapWindow : : OnClickCancel )
EVT_CHOICE ( wxID_ANY , ExportMiniMapWindow : : OnExportTypeChange )
END_EVENT_TABLE ( )
ExportMiniMapWindow : : ExportMiniMapWindow ( wxWindow * parent , Editor & editor ) :
2016-11-05 15:28:14 +01:00
wxDialog ( parent , wxID_ANY , " Export Minimap " , wxDefaultPosition , wxSize ( 400 , 300 ) ) ,
2012-06-29 16:44:26 +02:00
editor ( editor )
{
wxSizer * sizer = newd wxBoxSizer ( wxVERTICAL ) ;
wxSizer * tmpsizer ;
2016-10-18 13:54:41 -03:00
// Error field
2016-11-05 15:28:14 +01:00
error_field = newd wxStaticText ( this , wxID_VIEW_DETAILS , " " , wxDefaultPosition , wxDefaultSize ) ;
2016-10-18 13:54:41 -03:00
error_field - > SetForegroundColour ( * wxRED ) ;
tmpsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
tmpsizer - > Add ( error_field , 0 , wxALL , 5 ) ;
sizer - > Add ( tmpsizer , 0 , wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND , 5 ) ;
// Output folder
2016-11-05 15:28:14 +01:00
directory_text_field = newd wxTextCtrl ( this , wxID_ANY , " " , wxDefaultPosition , wxDefaultSize ) ;
2016-10-18 13:54:41 -03:00
directory_text_field - > Bind ( wxEVT_KEY_UP , & ExportMiniMapWindow : : OnDirectoryChanged , this ) ;
2020-05-18 21:20:44 -03:00
directory_text_field - > SetValue ( wxString ( g_settings . getString ( Config : : MINIMAP_EXPORT_DIR ) ) ) ;
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( wxHORIZONTAL , this , " Output Folder " ) ;
2016-10-18 13:54:41 -03:00
tmpsizer - > Add ( directory_text_field , 1 , wxALL , 5 ) ;
2016-11-05 15:28:14 +01:00
tmpsizer - > Add ( newd wxButton ( this , MAP_WINDOW_FILE_BUTTON , " Browse " ) , 0 , wxALL , 5 ) ;
2016-10-18 13:54:41 -03:00
sizer - > Add ( tmpsizer , 0 , wxALL | wxEXPAND , 5 ) ;
// File name
wxString mapName ( editor . map . getName ( ) . c_str ( ) , wxConvUTF8 ) ;
file_name_text_field = newd wxTextCtrl ( this , wxID_ANY , mapName . BeforeLast ( ' . ' ) , wxDefaultPosition , wxDefaultSize ) ;
file_name_text_field - > Bind ( wxEVT_KEY_UP , & ExportMiniMapWindow : : OnFileNameChanged , this ) ;
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( wxHORIZONTAL , this , " File Name " ) ;
2016-10-18 13:54:41 -03:00
tmpsizer - > Add ( file_name_text_field , 1 , wxALL , 5 ) ;
sizer - > Add ( tmpsizer , 0 , wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND , 5 ) ;
// Export options
2012-06-29 16:44:26 +02:00
wxArrayString choices ;
2016-11-05 15:28:14 +01:00
choices . Add ( " All Floors " ) ;
choices . Add ( " Ground Floor " ) ;
choices . Add ( " Specific Floor " ) ;
2016-10-25 09:47:00 -03:00
2016-10-18 13:54:41 -03:00
if ( editor . hasSelection ( ) )
2016-11-05 15:28:14 +01:00
choices . Add ( " Selected Area " ) ;
2016-10-18 13:54:41 -03:00
// Area options
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( wxHORIZONTAL , this , " Area Options " ) ;
2012-06-29 16:44:26 +02:00
floor_options = newd wxChoice ( this , wxID_ANY , wxDefaultPosition , wxDefaultSize , choices ) ;
2016-02-25 14:21:27 -03:00
floor_number = newd wxSpinCtrl ( this , wxID_ANY , i2ws ( GROUND_LAYER ) , wxDefaultPosition , wxDefaultSize , wxSP_ARROW_KEYS , 0 , MAP_MAX_LAYER , GROUND_LAYER ) ;
2012-06-29 16:44:26 +02:00
floor_number - > Enable ( false ) ;
floor_options - > SetSelection ( 0 ) ;
2016-10-18 13:54:41 -03:00
tmpsizer - > Add ( floor_options , 1 , wxALL , 5 ) ;
tmpsizer - > Add ( floor_number , 0 , wxALL , 5 ) ;
sizer - > Add ( tmpsizer , 0 , wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND , 5 ) ;
2012-06-29 16:44:26 +02:00
// OK/Cancel buttons
tmpsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
tmpsizer - > Add ( ok_button = newd wxButton ( this , wxID_OK , " OK " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
tmpsizer - > Add ( newd wxButton ( this , wxID_CANCEL , " Cancel " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
2016-10-18 13:54:41 -03:00
sizer - > Add ( tmpsizer , 0 , wxCENTER , 10 ) ;
2012-06-29 16:44:26 +02:00
2016-10-18 13:54:41 -03:00
SetSizer ( sizer ) ;
Layout ( ) ;
2016-02-29 21:47:54 -03:00
Centre ( wxBOTH ) ;
2016-10-18 13:54:41 -03:00
CheckValues ( ) ;
2012-06-29 16:44:26 +02:00
}
2019-06-25 18:07:36 +02:00
ExportMiniMapWindow : : ~ ExportMiniMapWindow ( ) = default ;
2012-06-29 16:44:26 +02:00
2016-09-29 18:36:27 -03:00
void ExportMiniMapWindow : : OnExportTypeChange ( wxCommandEvent & event )
2012-06-29 16:44:26 +02:00
{
2016-10-18 13:54:41 -03:00
floor_number - > Enable ( event . GetSelection ( ) = = 2 ) ;
2012-06-29 16:44:26 +02:00
}
2016-09-29 18:36:27 -03:00
void ExportMiniMapWindow : : OnClickBrowse ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
2016-10-18 13:54:41 -03:00
wxDirDialog dialog ( NULL , " Select the output folder " , " " , wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST ) ;
if ( dialog . ShowModal ( ) = = wxID_OK ) {
const wxString & directory = dialog . GetPath ( ) ;
directory_text_field - > ChangeValue ( directory ) ;
2012-06-29 16:44:26 +02:00
}
2016-10-18 13:54:41 -03:00
CheckValues ( ) ;
2012-06-29 16:44:26 +02:00
}
2016-10-18 13:54:41 -03:00
void ExportMiniMapWindow : : OnDirectoryChanged ( wxKeyEvent & event )
2012-06-29 16:44:26 +02:00
{
2016-10-18 13:54:41 -03:00
CheckValues ( ) ;
event . Skip ( ) ;
}
2012-06-29 16:44:26 +02:00
2016-10-18 13:54:41 -03:00
void ExportMiniMapWindow : : OnFileNameChanged ( wxKeyEvent & event )
{
CheckValues ( ) ;
event . Skip ( ) ;
}
2012-06-29 16:44:26 +02:00
2016-10-18 13:54:41 -03:00
void ExportMiniMapWindow : : OnClickOK ( wxCommandEvent & WXUNUSED ( event ) )
{
2016-11-05 15:28:14 +01:00
g_gui . CreateLoadBar ( " Exporting minimap " ) ;
2016-10-18 13:54:41 -03:00
2016-09-29 18:36:27 -03:00
try
2012-06-29 16:44:26 +02:00
{
2016-10-18 13:54:41 -03:00
FileName directory ( directory_text_field - > GetValue ( ) ) ;
2020-05-18 21:20:44 -03:00
g_settings . setString ( Config : : MINIMAP_EXPORT_DIR , directory_text_field - > GetValue ( ) . ToStdString ( ) ) ;
2016-10-18 13:54:41 -03:00
switch ( floor_options - > GetSelection ( ) )
{
2015-12-06 11:42:37 -03:00
case 0 : { // All floors
2016-02-28 19:44:08 -03:00
for ( int floor = 0 ; floor < MAP_LAYERS ; + + floor ) {
2016-09-29 20:30:54 -03:00
g_gui . SetLoadScale ( int ( floor * ( 100.f / 16.f ) ) , int ( ( floor + 1 ) * ( 100.f / 16.f ) ) ) ;
2016-11-05 15:28:14 +01:00
FileName file ( file_name_text_field - > GetValue ( ) + " _ " + i2ws ( floor ) + " .bmp " ) ;
2016-10-18 13:54:41 -03:00
file . Normalize ( wxPATH_NORM_ALL , directory . GetFullPath ( ) ) ;
editor . exportMiniMap ( file , floor , true ) ;
2015-12-06 11:42:37 -03:00
}
break ;
}
2016-10-18 13:54:41 -03:00
2015-12-06 11:42:37 -03:00
case 1 : { // Ground floor
2016-11-05 15:28:14 +01:00
FileName file ( file_name_text_field - > GetValue ( ) + " _ " + i2ws ( GROUND_LAYER ) + " .bmp " ) ;
2016-10-18 13:54:41 -03:00
file . Normalize ( wxPATH_NORM_ALL , directory . GetFullPath ( ) ) ;
editor . exportMiniMap ( file , GROUND_LAYER , true ) ;
2015-12-06 11:42:37 -03:00
break ;
2012-06-29 16:44:26 +02:00
}
2016-10-18 13:54:41 -03:00
2015-12-06 11:42:37 -03:00
case 2 : { // Specific floors
2012-06-29 16:44:26 +02:00
int floor = floor_number - > GetValue ( ) ;
2016-11-05 15:28:14 +01:00
FileName file ( file_name_text_field - > GetValue ( ) + " _ " + i2ws ( floor ) + " .bmp " ) ;
2016-10-18 13:54:41 -03:00
file . Normalize ( wxPATH_NORM_ALL , directory . GetFullPath ( ) ) ;
editor . exportMiniMap ( file , floor , true ) ;
break ;
}
case 3 : { // Selected area
editor . exportSelectionAsMiniMap ( directory , file_name_text_field - > GetValue ( ) ) ;
2015-12-06 11:42:37 -03:00
break ;
}
2012-06-29 16:44:26 +02:00
}
}
2016-09-29 18:36:27 -03:00
catch ( std : : bad_alloc & )
2012-06-29 16:44:26 +02:00
{
2016-11-05 15:28:14 +01:00
g_gui . PopupDialog ( " Error " , " There is not enough memory available to complete the operation. " , wxOK ) ;
2012-06-29 16:44:26 +02:00
}
2016-10-18 13:54:41 -03:00
2016-09-29 20:30:54 -03:00
g_gui . DestroyLoadBar ( ) ;
2012-06-29 16:44:26 +02:00
EndModal ( 1 ) ;
}
2016-09-29 18:36:27 -03:00
void ExportMiniMapWindow : : OnClickCancel ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
// Just close this window
EndModal ( 0 ) ;
}
2016-10-18 13:54:41 -03:00
void ExportMiniMapWindow : : CheckValues ( )
{
if ( directory_text_field - > IsEmpty ( ) ) {
2016-11-05 15:28:14 +01:00
error_field - > SetLabel ( " Type or select an output folder. " ) ;
2016-10-18 13:54:41 -03:00
ok_button - > Enable ( false ) ;
return ;
}
if ( file_name_text_field - > IsEmpty ( ) ) {
2016-11-05 15:28:14 +01:00
error_field - > SetLabel ( " Type a name for the file. " ) ;
2016-10-18 13:54:41 -03:00
ok_button - > Enable ( false ) ;
return ;
}
FileName directory ( directory_text_field - > GetValue ( ) ) ;
if ( ! directory . Exists ( ) ) {
2016-11-05 15:28:14 +01:00
error_field - > SetLabel ( " Output folder not found. " ) ;
2016-10-18 13:54:41 -03:00
ok_button - > Enable ( false ) ;
return ;
}
if ( ! directory . IsDirWritable ( ) ) {
2016-11-05 15:28:14 +01:00
error_field - > SetLabel ( " Output folder is not writable. " ) ;
2016-10-18 13:54:41 -03:00
ok_button - > Enable ( false ) ;
return ;
}
error_field - > SetLabel ( wxEmptyString ) ;
ok_button - > Enable ( true ) ;
}
2012-06-30 18:27:02 +02:00
// ============================================================================
// Numkey forwarding text control
BEGIN_EVENT_TABLE ( KeyForwardingTextCtrl , wxTextCtrl )
EVT_KEY_DOWN ( KeyForwardingTextCtrl : : OnKeyDown )
END_EVENT_TABLE ( )
2016-09-29 18:36:27 -03:00
void KeyForwardingTextCtrl : : OnKeyDown ( wxKeyEvent & event )
2012-06-30 18:27:02 +02:00
{
2016-09-29 18:36:27 -03:00
if ( event . GetKeyCode ( ) = = WXK_UP | | event . GetKeyCode ( ) = = WXK_DOWN | |
2015-12-06 11:42:37 -03:00
event . GetKeyCode ( ) = = WXK_PAGEDOWN | | event . GetKeyCode ( ) = = WXK_PAGEUP ) {
2012-06-30 18:27:02 +02:00
GetParent ( ) - > GetEventHandler ( ) - > AddPendingEvent ( event ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-06-30 18:27:02 +02:00
event . Skip ( ) ;
}
}
2012-06-29 16:44:26 +02:00
// ============================================================================
// Find Item Dialog (Jump to item)
BEGIN_EVENT_TABLE ( FindDialog , wxDialog )
EVT_TIMER ( wxID_ANY , FindDialog : : OnTextIdle )
EVT_TEXT ( JUMP_DIALOG_TEXT , FindDialog : : OnTextChange )
2012-06-30 18:27:02 +02:00
EVT_KEY_DOWN ( FindDialog : : OnKeyDown )
2012-06-29 16:44:26 +02:00
EVT_TEXT_ENTER ( JUMP_DIALOG_TEXT , FindDialog : : OnClickOK )
EVT_LISTBOX_DCLICK ( JUMP_DIALOG_LIST , FindDialog : : OnClickList )
EVT_BUTTON ( wxID_OK , FindDialog : : OnClickOK )
2012-06-30 18:27:02 +02:00
EVT_BUTTON ( wxID_CANCEL , FindDialog : : OnClickCancel )
2012-06-29 16:44:26 +02:00
END_EVENT_TABLE ( )
FindDialog : : FindDialog ( wxWindow * parent , wxString title ) :
2016-09-29 20:30:54 -03:00
wxDialog ( g_gui . root , wxID_ANY , title , wxDefaultPosition , wxDefaultSize , wxRESIZE_BORDER | wxCAPTION | wxCLOSE_BOX ) ,
2012-06-29 16:44:26 +02:00
idle_input_timer ( this ) ,
2014-01-22 20:16:17 +01:00
result_brush ( nullptr ) ,
2012-06-29 16:44:26 +02:00
result_id ( 0 )
{
wxSizer * sizer = newd wxBoxSizer ( wxVERTICAL ) ;
2016-11-05 15:28:14 +01:00
search_field = newd KeyForwardingTextCtrl ( this , JUMP_DIALOG_TEXT , " " , wxDefaultPosition , wxDefaultSize , wxTE_PROCESS_ENTER ) ;
2012-06-29 16:44:26 +02:00
search_field - > SetFocus ( ) ;
sizer - > Add ( search_field , 0 , wxEXPAND ) ;
item_list = newd FindDialogListBox ( this , JUMP_DIALOG_LIST ) ;
item_list - > SetMinSize ( wxSize ( 470 , 400 ) ) ;
2012-06-30 18:27:02 +02:00
sizer - > Add ( item_list , wxSizerFlags ( 1 ) . Expand ( ) . Border ( ) ) ;
2012-06-29 16:44:26 +02:00
wxSizer * stdsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
stdsizer - > Add ( newd wxButton ( this , wxID_OK , " OK " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
stdsizer - > Add ( newd wxButton ( this , wxID_CANCEL , " Cancel " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
2012-06-30 18:27:02 +02:00
sizer - > Add ( stdsizer , wxSizerFlags ( 0 ) . Center ( ) . Border ( ) ) ;
2012-06-29 16:44:26 +02:00
SetSizerAndFit ( sizer ) ;
2016-02-29 21:47:54 -03:00
Centre ( wxBOTH ) ;
2012-06-29 16:44:26 +02:00
// We can't call it here since it calls an abstract function, call in child constructors instead.
// RefreshContents();
}
2019-06-25 18:07:36 +02:00
FindDialog : : ~ FindDialog ( ) = default ;
2012-06-29 16:44:26 +02:00
2016-09-29 18:36:27 -03:00
void FindDialog : : OnKeyDown ( wxKeyEvent & event )
2012-06-29 16:44:26 +02:00
{
int w , h ;
item_list - > GetSize ( & w , & h ) ;
2016-09-29 18:36:27 -03:00
size_t amount = 1 ;
2012-06-29 16:44:26 +02:00
2015-12-06 11:42:37 -03:00
switch ( event . GetKeyCode ( ) ) {
2012-06-29 16:44:26 +02:00
case WXK_PAGEUP :
amount = h / 32 + 1 ;
2020-03-25 14:07:43 -07:00
[[fallthrough]] ;
2015-12-06 11:42:37 -03:00
case WXK_UP : {
if ( item_list - > GetItemCount ( ) > 0 ) {
2014-08-01 15:04:40 -05:00
ssize_t n = item_list - > GetSelection ( ) ;
2016-09-29 18:36:27 -03:00
if ( n = = wxNOT_FOUND )
2012-06-29 16:44:26 +02:00
n = 0 ;
2014-01-20 03:25:19 +01:00
else if ( n ! = amount & & n - amount < n ) // latter is needed for unsigned overflow
2012-06-29 16:44:26 +02:00
n - = amount ;
else
n = 0 ;
item_list - > SetSelection ( n ) ;
}
2015-12-06 11:42:37 -03:00
break ;
2012-06-29 16:44:26 +02:00
}
2015-12-06 11:42:37 -03:00
2012-06-29 16:44:26 +02:00
case WXK_PAGEDOWN :
amount = h / 32 + 1 ;
2020-03-25 14:07:43 -07:00
[[fallthrough]] ;
2015-12-06 11:42:37 -03:00
case WXK_DOWN : {
2012-06-29 16:44:26 +02:00
if ( item_list - > GetItemCount ( ) > 0 ) {
2014-08-01 15:04:40 -05:00
ssize_t n = item_list - > GetSelection ( ) ;
2012-06-29 16:44:26 +02:00
size_t itemcount = item_list - > GetItemCount ( ) ;
if ( n = = wxNOT_FOUND )
n = 0 ;
2014-02-28 22:34:25 +01:00
else if ( static_cast < uint32_t > ( n ) < itemcount - amount & & itemcount - amount < itemcount )
2012-06-29 16:44:26 +02:00
n + = amount ;
else
n = item_list - > GetItemCount ( ) - 1 ;
item_list - > SetSelection ( n ) ;
}
2015-12-06 11:42:37 -03:00
break ;
2012-06-29 16:44:26 +02:00
}
2015-12-06 11:42:37 -03:00
default :
event . Skip ( ) ;
break ;
2012-06-29 16:44:26 +02:00
}
}
2016-09-29 18:36:27 -03:00
void FindDialog : : OnTextIdle ( wxTimerEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
2012-06-30 18:27:02 +02:00
RefreshContents ( ) ;
2012-06-29 16:44:26 +02:00
}
2016-09-29 18:36:27 -03:00
void FindDialog : : OnTextChange ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
idle_input_timer . Start ( 800 , true ) ;
}
2016-09-29 18:36:27 -03:00
void FindDialog : : OnClickList ( wxCommandEvent & event )
2012-06-29 16:44:26 +02:00
{
OnClickListInternal ( event ) ;
}
2016-09-29 18:36:27 -03:00
void FindDialog : : OnClickOK ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
// This is to get virtual callback
OnClickOKInternal ( ) ;
}
2016-09-29 18:36:27 -03:00
void FindDialog : : OnClickCancel ( wxCommandEvent & WXUNUSED ( event ) )
2012-06-29 16:44:26 +02:00
{
EndModal ( 0 ) ;
}
2016-09-29 18:36:27 -03:00
void FindDialog : : RefreshContents ( )
2012-06-29 16:44:26 +02:00
{
// This is to get virtual callback
RefreshContentsInternal ( ) ;
}
// ============================================================================
// Find Brush Dialog (Jump to brush)
2016-09-29 18:36:27 -03:00
FindBrushDialog : : FindBrushDialog ( wxWindow * parent , wxString title ) : FindDialog ( parent , title )
2012-06-29 16:44:26 +02:00
{
RefreshContents ( ) ;
}
2019-06-25 18:07:36 +02:00
FindBrushDialog : : ~ FindBrushDialog ( ) = default ;
2012-06-29 16:44:26 +02:00
2016-09-29 18:36:27 -03:00
void FindBrushDialog : : OnClickListInternal ( wxCommandEvent & event )
2012-06-29 16:44:26 +02:00
{
Brush * brush = item_list - > GetSelectedBrush ( ) ;
2015-12-06 11:42:37 -03:00
if ( brush ) {
2012-06-29 16:44:26 +02:00
result_brush = brush ;
EndModal ( 1 ) ;
}
}
2016-09-29 18:36:27 -03:00
void FindBrushDialog : : OnClickOKInternal ( )
2012-06-29 16:44:26 +02:00
{
// This is kind of stupid as it would fail unless the "Please enter a search string" wasn't there
2015-12-06 11:42:37 -03:00
if ( item_list - > GetItemCount ( ) > 0 ) {
if ( item_list - > GetSelection ( ) = = wxNOT_FOUND ) {
2012-06-29 16:44:26 +02:00
item_list - > SetSelection ( 0 ) ;
}
Brush * brush = item_list - > GetSelectedBrush ( ) ;
2016-11-12 10:30:06 -03:00
if ( ! brush ) {
2012-06-29 16:44:26 +02:00
// It's either "Please enter a search string" or "No matches"
// Perhaps we can refresh now?
std : : string search_string = as_lower_str ( nstr ( search_field - > GetValue ( ) ) ) ;
bool do_search = ( search_string . size ( ) > = 2 ) ;
2015-12-06 11:42:37 -03:00
if ( do_search ) {
2016-10-25 10:19:42 -03:00
const BrushMap & map = g_brushes . getMap ( ) ;
2015-12-06 11:42:37 -03:00
for ( BrushMap : : const_iterator iter = map . begin ( ) ; iter ! = map . end ( ) ; + + iter ) {
2012-06-29 16:44:26 +02:00
const Brush * brush = iter - > second ;
if ( as_lower_str ( brush - > getName ( ) ) . find ( search_string ) = = std : : string : : npos )
continue ;
// Don't match RAWs now.
2016-11-12 10:30:06 -03:00
if ( brush - > isRaw ( ) )
2012-06-29 16:44:26 +02:00
continue ;
// Found one!
result_brush = brush ;
break ;
}
// Did we not find a matching brush?
2015-12-06 11:42:37 -03:00
if ( ! result_brush ) {
2012-06-29 16:44:26 +02:00
// Then let's search the RAWs
2016-10-25 09:47:00 -03:00
for ( int id = 0 ; id < = g_items . getMaxID ( ) ; + + id ) {
ItemType & it = g_items [ id ] ;
2012-06-29 16:44:26 +02:00
if ( it . id = = 0 )
continue ;
2016-11-12 10:30:06 -03:00
RAWBrush * raw_brush = it . raw_brush ;
if ( ! raw_brush )
2012-06-29 16:44:26 +02:00
continue ;
2016-11-12 10:30:06 -03:00
if ( as_lower_str ( raw_brush - > getName ( ) ) . find ( search_string ) = = std : : string : : npos )
2012-06-29 16:44:26 +02:00
continue ;
// Found one!
2016-11-12 10:30:06 -03:00
result_brush = raw_brush ;
2012-06-29 16:44:26 +02:00
break ;
}
}
// Done!
}
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
result_brush = brush ;
}
}
EndModal ( 1 ) ;
}
2016-09-29 18:36:27 -03:00
void FindBrushDialog : : RefreshContentsInternal ( )
2012-06-29 16:44:26 +02:00
{
item_list - > Clear ( ) ;
std : : string search_string = as_lower_str ( nstr ( search_field - > GetValue ( ) ) ) ;
bool do_search = ( search_string . size ( ) > = 2 ) ;
2015-12-06 11:42:37 -03:00
if ( do_search ) {
2016-11-06 18:57:58 +01:00
bool found_search_results = false ;
2016-10-25 10:19:42 -03:00
const BrushMap & brushes_map = g_brushes . getMap ( ) ;
2016-09-29 18:36:27 -03:00
2012-06-29 16:44:26 +02:00
// We store the raws so they display last of all results
std : : deque < const RAWBrush * > raws ;
2015-12-06 11:42:37 -03:00
for ( BrushMap : : const_iterator iter = brushes_map . begin ( ) ; iter ! = brushes_map . end ( ) ; + + iter ) {
2012-06-29 16:44:26 +02:00
const Brush * brush = iter - > second ;
if ( as_lower_str ( brush - > getName ( ) ) . find ( search_string ) = = std : : string : : npos )
continue ;
2016-11-12 10:30:06 -03:00
if ( brush - > isRaw ( ) )
2012-06-29 16:44:26 +02:00
continue ;
2016-11-06 18:57:58 +01:00
found_search_results = true ;
2012-06-29 16:44:26 +02:00
item_list - > AddBrush ( const_cast < Brush * > ( brush ) ) ;
}
2016-10-25 09:47:00 -03:00
for ( int id = 0 ; id < = g_items . getMaxID ( ) ; + + id ) {
ItemType & it = g_items [ id ] ;
2012-06-29 16:44:26 +02:00
if ( it . id = = 0 )
continue ;
2016-11-12 10:30:06 -03:00
RAWBrush * raw_brush = it . raw_brush ;
if ( ! raw_brush )
2012-06-29 16:44:26 +02:00
continue ;
2016-11-12 10:30:06 -03:00
if ( as_lower_str ( raw_brush - > getName ( ) ) . find ( search_string ) = = std : : string : : npos )
2012-06-29 16:44:26 +02:00
continue ;
2016-11-06 18:57:58 +01:00
found_search_results = true ;
2016-11-12 10:30:06 -03:00
item_list - > AddBrush ( raw_brush ) ;
2012-06-29 16:44:26 +02:00
}
2015-12-06 11:42:37 -03:00
while ( raws . size ( ) > 0 ) {
2012-06-29 16:44:26 +02:00
item_list - > AddBrush ( const_cast < RAWBrush * > ( raws . front ( ) ) ) ;
raws . pop_front ( ) ;
}
2016-11-06 18:57:58 +01:00
if ( found_search_results ) {
2012-06-29 16:44:26 +02:00
item_list - > SetSelection ( 0 ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
item_list - > SetNoMatches ( ) ;
}
2016-11-06 18:57:58 +01:00
2012-06-29 16:44:26 +02:00
}
2016-11-06 18:57:58 +01:00
item_list - > Refresh ( ) ;
2012-06-29 16:44:26 +02:00
}
2012-06-30 18:27:02 +02:00
// ============================================================================
// Listbox in find item / brush stuff
2012-06-29 16:44:26 +02:00
FindDialogListBox : : FindDialogListBox ( wxWindow * parent , wxWindowID id ) :
wxVListBox ( parent , id , wxDefaultPosition , wxDefaultSize , wxLB_SINGLE ) ,
cleared ( false ) ,
no_matches ( false )
{
Clear ( ) ;
}
2016-09-29 18:36:27 -03:00
FindDialogListBox : : ~ FindDialogListBox ( )
2012-06-29 16:44:26 +02:00
{
2015-12-06 11:42:37 -03:00
////
2012-06-29 16:44:26 +02:00
}
2016-09-29 18:36:27 -03:00
void FindDialogListBox : : Clear ( )
2012-06-29 16:44:26 +02:00
{
cleared = true ;
no_matches = false ;
brushlist . clear ( ) ;
SetItemCount ( 1 ) ;
}
2016-09-29 18:36:27 -03:00
void FindDialogListBox : : SetNoMatches ( )
2012-06-29 16:44:26 +02:00
{
cleared = false ;
no_matches = true ;
brushlist . clear ( ) ;
SetItemCount ( 1 ) ;
}
2016-09-29 18:36:27 -03:00
void FindDialogListBox : : AddBrush ( Brush * brush )
2012-06-29 16:44:26 +02:00
{
if ( cleared | | no_matches )
SetItemCount ( 0 ) ;
2016-09-29 18:36:27 -03:00
2012-06-29 16:44:26 +02:00
cleared = false ;
no_matches = false ;
SetItemCount ( GetItemCount ( ) + 1 ) ;
brushlist . push_back ( brush ) ;
}
2016-09-29 18:36:27 -03:00
Brush * FindDialogListBox : : GetSelectedBrush ( )
2012-06-29 16:44:26 +02:00
{
2014-08-01 15:04:40 -05:00
ssize_t n = GetSelection ( ) ;
2012-06-29 16:44:26 +02:00
if ( n = = wxNOT_FOUND | | no_matches | | cleared )
2014-01-22 20:16:17 +01:00
return nullptr ;
2012-06-29 16:44:26 +02:00
return brushlist [ n ] ;
}
void FindDialogListBox : : OnDrawItem ( wxDC & dc , const wxRect & rect , size_t n ) const
{
2015-12-06 11:42:37 -03:00
if ( no_matches ) {
2016-11-05 15:28:14 +01:00
dc . DrawText ( " No matches for your search. " , rect . GetX ( ) + 40 , rect . GetY ( ) + 6 ) ;
2015-12-06 11:42:37 -03:00
} else if ( cleared ) {
2016-11-05 15:28:14 +01:00
dc . DrawText ( " Please enter your search string. " , rect . GetX ( ) + 40 , rect . GetY ( ) + 6 ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
ASSERT ( n < brushlist . size ( ) ) ;
2016-09-29 20:30:54 -03:00
Sprite * spr = g_gui . gfx . getSprite ( brushlist [ n ] - > getLookID ( ) ) ;
2015-12-06 11:42:37 -03:00
if ( spr ) {
2012-06-29 16:44:26 +02:00
spr - > DrawTo ( & dc , SPRITE_SIZE_32x32 , rect . GetX ( ) , rect . GetY ( ) , rect . GetWidth ( ) , rect . GetHeight ( ) ) ;
}
2016-03-17 07:09:40 -03:00
if ( IsSelected ( n ) ) {
if ( HasFocus ( ) )
dc . SetTextForeground ( wxColor ( 0xFF , 0xFF , 0xFF ) ) ;
else
dc . SetTextForeground ( wxColor ( 0x00 , 0x00 , 0xFF ) ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
dc . SetTextForeground ( wxColor ( 0x00 , 0x00 , 0x00 ) ) ;
}
dc . DrawText ( wxstr ( brushlist [ n ] - > getName ( ) ) , rect . GetX ( ) + 40 , rect . GetY ( ) + 6 ) ;
}
}
wxCoord FindDialogListBox : : OnMeasureItem ( size_t n ) const
{
return 32 ;
}
2016-11-08 23:29:05 +01:00
// ============================================================================
// wxListBox that can be sorted
SortableListBox : : SortableListBox ( wxWindow * parent , wxWindowID id , const wxPoint & pos , const wxSize & size )
: wxListBox ( parent , id , pos , size , 0 , nullptr , wxLB_SINGLE | wxLB_NEEDED_SB )
{ }
SortableListBox : : ~ SortableListBox ( ) { }
void SortableListBox : : Sort ( ) {
2016-11-11 23:32:10 +01:00
if ( GetCount ( ) = = 0 )
return ;
wxASSERT_MSG ( GetClientDataType ( ) ! = wxClientData_Object , " Sorting a list with data of type wxClientData_Object is currently not implemented " ) ;
DoSort ( ) ;
}
void SortableListBox : : DoSort ( ) {
size_t count = GetCount ( ) ;
int selection = GetSelection ( ) ;
wxClientDataType dataType = GetClientDataType ( ) ;
wxArrayString stringList ;
wxArrayPtrVoid dataList ;
for ( size_t i = 0 ; i < count ; + + i ) {
stringList . Add ( GetString ( i ) ) ;
if ( dataType = = wxClientData_Void )
dataList . Add ( GetClientData ( i ) ) ;
}
//Insertion sort
for ( size_t i = 0 ; i < count ; + + i ) {
size_t j = i ;
while ( j > 0 & & stringList [ j ] . CmpNoCase ( stringList [ j - 1 ] ) < 0 ) {
wxString tmpString = stringList [ j ] ;
stringList [ j ] = stringList [ j - 1 ] ;
stringList [ j - 1 ] = tmpString ;
if ( dataType = = wxClientData_Void ) {
void * tmpData = dataList [ j ] ;
dataList [ j ] = dataList [ j - 1 ] ;
dataList [ j - 1 ] = tmpData ;
}
if ( selection = = j - 1 )
selection + + ;
else if ( selection = = j ) {
selection - - ;
}
2016-11-09 14:52:04 +01:00
j - - ;
}
}
2016-11-08 23:29:05 +01:00
2016-11-11 23:32:10 +01:00
Freeze ( ) ;
Clear ( ) ;
for ( size_t i = 0 ; i < count ; + + i ) {
if ( dataType = = wxClientData_Void )
Append ( stringList [ i ] , dataList [ i ] ) ;
else
Append ( stringList [ i ] ) ;
2016-11-09 14:52:04 +01:00
}
2016-11-11 23:32:10 +01:00
Thaw ( ) ;
2016-11-09 14:52:04 +01:00
2016-11-11 23:32:10 +01:00
SetSelection ( selection ) ;
2016-11-08 23:29:05 +01:00
}
2012-07-20 16:32:57 +02:00
// ============================================================================
// Object properties base
ObjectPropertiesWindowBase : : ObjectPropertiesWindowBase ( wxWindow * parent , wxString title , const Map * map , const Tile * tile , Item * item , wxPoint position /* = wxDefaultPosition */ ) :
wxDialog ( parent , wxID_ANY , title ,
position , wxSize ( 600 , 400 ) , wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER ) ,
edit_map ( map ) ,
edit_tile ( tile ) ,
edit_item ( item ) ,
2021-05-25 00:29:12 -03:00
edit_monster ( nullptr ) ,
edit_spawn_monster ( nullptr ) ,
edit_npc ( nullptr ) ,
edit_spawn_npc ( nullptr )
{
////
}
ObjectPropertiesWindowBase : : ObjectPropertiesWindowBase ( wxWindow * parent , wxString title , const Map * map , const Tile * tile , Monster * monster , wxPoint position /* = wxDefaultPosition */ ) :
wxDialog ( parent , wxID_ANY , title ,
position , wxSize ( 600 , 400 ) , wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER ) ,
edit_map ( map ) ,
edit_tile ( tile ) ,
edit_item ( nullptr ) ,
edit_monster ( monster ) ,
edit_spawn_monster ( nullptr ) ,
edit_npc ( nullptr ) ,
edit_spawn_npc ( nullptr )
{
////
}
ObjectPropertiesWindowBase : : ObjectPropertiesWindowBase ( wxWindow * parent , wxString title , const Map * map , const Tile * tile , SpawnMonster * spawnMonster , wxPoint position /* = wxDefaultPosition */ ) :
wxDialog ( parent , wxID_ANY , title ,
position , wxSize ( 600 , 400 ) , wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER ) ,
edit_map ( map ) ,
edit_tile ( tile ) ,
edit_item ( nullptr ) ,
edit_monster ( nullptr ) ,
edit_spawn_monster ( spawnMonster ) ,
edit_npc ( nullptr ) ,
edit_spawn_npc ( nullptr )
2012-07-20 16:32:57 +02:00
{
2015-12-06 11:42:37 -03:00
////
2012-07-20 16:32:57 +02:00
}
2021-05-25 00:29:12 -03:00
ObjectPropertiesWindowBase : : ObjectPropertiesWindowBase ( wxWindow * parent , wxString title , const Map * map , const Tile * tile , Npc * npc , wxPoint position /* = wxDefaultPosition */ ) :
2012-07-20 16:32:57 +02:00
wxDialog ( parent , wxID_ANY , title ,
position , wxSize ( 600 , 400 ) , wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER ) ,
edit_map ( map ) ,
edit_tile ( tile ) ,
2014-01-22 20:16:17 +01:00
edit_item ( nullptr ) ,
2021-05-25 00:29:12 -03:00
edit_monster ( nullptr ) ,
edit_spawn_monster ( nullptr ) ,
edit_npc ( npc ) ,
edit_spawn_npc ( nullptr )
2012-07-20 16:32:57 +02:00
{
2015-12-06 11:42:37 -03:00
////
2012-07-20 16:32:57 +02:00
}
2021-05-25 00:29:12 -03:00
ObjectPropertiesWindowBase : : ObjectPropertiesWindowBase ( wxWindow * parent , wxString title , const Map * map , const Tile * tile , SpawnNpc * spawnNpc , wxPoint position /* = wxDefaultPosition */ ) :
2012-07-20 16:32:57 +02:00
wxDialog ( parent , wxID_ANY , title ,
position , wxSize ( 600 , 400 ) , wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER ) ,
edit_map ( map ) ,
edit_tile ( tile ) ,
2014-01-22 20:16:17 +01:00
edit_item ( nullptr ) ,
2021-05-25 00:29:12 -03:00
edit_monster ( nullptr ) ,
edit_spawn_monster ( nullptr ) ,
edit_npc ( nullptr ) ,
edit_spawn_npc ( spawnNpc )
2012-07-20 16:32:57 +02:00
{
2015-12-06 11:42:37 -03:00
////
2012-07-20 16:32:57 +02:00
}
Item * ObjectPropertiesWindowBase : : getItemBeingEdited ( )
{
return edit_item ;
}
2012-06-29 16:44:26 +02:00
// ============================================================================
// Edit Towns Dialog
BEGIN_EVENT_TABLE ( EditTownsDialog , wxDialog )
EVT_LISTBOX ( EDIT_TOWNS_LISTBOX , EditTownsDialog : : OnListBoxChange )
EVT_BUTTON ( EDIT_TOWNS_SELECT_TEMPLE , EditTownsDialog : : OnClickSelectTemplePosition )
EVT_BUTTON ( EDIT_TOWNS_ADD , EditTownsDialog : : OnClickAdd )
EVT_BUTTON ( EDIT_TOWNS_REMOVE , EditTownsDialog : : OnClickRemove )
EVT_BUTTON ( wxID_OK , EditTownsDialog : : OnClickOK )
EVT_BUTTON ( wxID_CANCEL , EditTownsDialog : : OnClickCancel )
END_EVENT_TABLE ( )
EditTownsDialog : : EditTownsDialog ( wxWindow * parent , Editor & editor ) :
2016-11-05 15:28:14 +01:00
wxDialog ( parent , wxID_ANY , " Towns " , wxDefaultPosition , wxSize ( 280 , 330 ) ) ,
2012-06-29 16:44:26 +02:00
editor ( editor )
{
Map & map = editor . map ;
// Create topsizer
wxSizer * sizer = newd wxBoxSizer ( wxVERTICAL ) ;
wxSizer * tmpsizer ;
2015-12-06 11:42:37 -03:00
for ( TownMap : : const_iterator town_iter = map . towns . begin ( ) ; town_iter ! = map . towns . end ( ) ; + + town_iter ) {
2012-06-29 16:44:26 +02:00
Town * town = town_iter - > second ;
town_list . push_back ( newd Town ( * town ) ) ;
2016-01-17 08:12:13 -03:00
if ( max_town_id < town - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
max_town_id = town - > getID ( ) ;
}
}
// Town list
2015-01-23 18:30:56 -03:00
town_listbox = newd wxListBox ( this , EDIT_TOWNS_LISTBOX , wxDefaultPosition , wxSize ( 240 , 100 ) ) ;
sizer - > Add ( town_listbox , 1 , wxEXPAND | wxTOP | wxLEFT | wxRIGHT , 10 ) ;
2012-06-29 16:44:26 +02:00
tmpsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
tmpsizer - > Add ( newd wxButton ( this , EDIT_TOWNS_ADD , " Add " ) , 0 , wxTOP , 5 ) ;
tmpsizer - > Add ( remove_button = newd wxButton ( this , EDIT_TOWNS_REMOVE , " Remove " ) , 0 , wxRIGHT | wxTOP , 5 ) ;
2015-01-23 18:30:56 -03:00
sizer - > Add ( tmpsizer , 0 , wxEXPAND | wxLEFT | wxRIGHT , 10 ) ;
2012-06-29 16:44:26 +02:00
// House options
2016-11-05 15:28:14 +01:00
tmpsizer = newd wxStaticBoxSizer ( wxHORIZONTAL , this , " Name / ID " ) ;
name_field = newd wxTextCtrl ( this , wxID_ANY , " " , wxDefaultPosition , wxSize ( 190 , 20 ) , 0 , wxTextValidator ( wxFILTER_ASCII , & town_name ) ) ;
2015-01-23 18:30:56 -03:00
tmpsizer - > Add ( name_field , 2 , wxEXPAND | wxLEFT | wxBOTTOM , 5 ) ;
2012-06-29 16:44:26 +02:00
2016-11-05 15:28:14 +01:00
id_field = newd wxTextCtrl ( this , wxID_ANY , " " , wxDefaultPosition , wxSize ( 40 , 20 ) , 0 , wxTextValidator ( wxFILTER_NUMERIC , & town_id ) ) ;
2012-06-29 16:44:26 +02:00
id_field - > Enable ( false ) ;
2015-01-23 18:30:56 -03:00
tmpsizer - > Add ( id_field , 1 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
sizer - > Add ( tmpsizer , 0 , wxEXPAND | wxALL , 10 ) ;
2012-06-29 16:44:26 +02:00
// Temple position
2016-11-05 15:28:14 +01:00
temple_position = newd PositionCtrl ( this , " Temple Position " , 0 , 0 , 0 , map . getWidth ( ) , map . getHeight ( ) ) ;
select_position_button = newd wxButton ( this , EDIT_TOWNS_SELECT_TEMPLE , " Go To " ) ;
2015-07-11 21:20:07 -03:00
temple_position - > Add ( select_position_button , 0 , wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
sizer - > Add ( temple_position , 0 , wxEXPAND | wxLEFT | wxRIGHT , 10 ) ;
2012-06-29 16:44:26 +02:00
// OK/Cancel buttons
tmpsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
tmpsizer - > Add ( newd wxButton ( this , wxID_OK , " OK " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
tmpsizer - > Add ( newd wxButton ( this , wxID_CANCEL , " Cancel " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
2015-01-23 18:30:56 -03:00
sizer - > Add ( tmpsizer , 0 , wxCENTER | wxALL , 10 ) ;
2012-06-29 16:44:26 +02:00
SetSizerAndFit ( sizer ) ;
2016-02-29 21:47:54 -03:00
Centre ( wxBOTH ) ;
2012-06-29 16:44:26 +02:00
BuildListBox ( true ) ;
}
EditTownsDialog : : ~ EditTownsDialog ( )
{
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
2012-06-29 16:44:26 +02:00
delete * town_iter ;
}
}
void EditTownsDialog : : BuildListBox ( bool doselect )
{
long tmplong = 0 ;
max_town_id = 0 ;
wxArrayString town_name_list ;
uint32_t selection_before = 0 ;
2015-12-06 11:42:37 -03:00
if ( doselect & & id_field - > GetValue ( ) . ToLong ( & tmplong ) ) {
2012-06-29 16:44:26 +02:00
uint32_t old_town_id = tmplong ;
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
if ( old_town_id = = ( * town_iter ) - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
selection_before = ( * town_iter ) - > getID ( ) ;
break ;
}
}
}
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
2012-06-29 16:44:26 +02:00
Town * town = * town_iter ;
town_name_list . Add ( wxstr ( town - > getName ( ) ) ) ;
2015-12-06 11:42:37 -03:00
if ( max_town_id < town - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
max_town_id = town - > getID ( ) ;
}
}
town_listbox - > Set ( town_name_list ) ;
remove_button - > Enable ( town_listbox - > GetCount ( ) ! = 0 ) ;
select_position_button - > Enable ( false ) ;
2015-12-06 11:42:37 -03:00
if ( doselect ) {
if ( selection_before ) {
2012-06-29 16:44:26 +02:00
int i = 0 ;
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
if ( selection_before = = ( * town_iter ) - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
town_listbox - > SetSelection ( i ) ;
return ;
}
+ + i ;
}
}
UpdateSelection ( 0 ) ;
}
}
void EditTownsDialog : : UpdateSelection ( int new_selection )
{
long tmplong ;
// Save old values
2015-12-06 11:42:37 -03:00
if ( town_list . size ( ) > 0 ) {
if ( id_field - > GetValue ( ) . ToLong ( & tmplong ) ) {
2012-06-29 16:44:26 +02:00
uint32_t old_town_id = tmplong ;
2014-01-22 20:16:17 +01:00
Town * old_town = nullptr ;
2012-06-29 16:44:26 +02:00
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
if ( old_town_id = = ( * town_iter ) - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
old_town = * town_iter ;
break ;
}
}
if ( old_town ) {
2015-07-11 21:20:07 -03:00
Position templepos = temple_position - > GetPosition ( ) ;
2012-06-29 16:44:26 +02:00
//printf("Changed town %d:%s\n", old_town_id, old_town->getName().c_str());
//printf("New values %d:%s:%d:%d:%d\n", town_id, town_name.c_str(), templepos.x, templepos.y, templepos.z);
old_town - > setTemplePosition ( templepos ) ;
wxString new_name = name_field - > GetValue ( ) ;
wxString old_name = wxstr ( old_town - > getName ( ) ) ;
old_town - > setName ( nstr ( new_name ) ) ;
2015-12-06 11:42:37 -03:00
if ( new_name ! = old_name ) {
2012-06-29 16:44:26 +02:00
// Name has changed, update list
BuildListBox ( false ) ;
}
}
}
}
// Clear fields
town_name . Clear ( ) ;
town_id . Clear ( ) ;
2015-12-06 11:42:37 -03:00
if ( town_list . size ( ) > size_t ( new_selection ) ) {
2012-06-29 16:44:26 +02:00
name_field - > Enable ( true ) ;
2015-07-11 21:20:07 -03:00
temple_position - > Enable ( true ) ;
2015-01-20 16:43:23 -03:00
select_position_button - > Enable ( true ) ;
2012-06-29 16:44:26 +02:00
// Change the values to reflect the newd selection
Town * town = town_list [ new_selection ] ;
ASSERT ( town ) ;
//printf("Selected %d:%s\n", new_selection, town->getName().c_str());
town_name < < wxstr ( town - > getName ( ) ) ;
name_field - > SetValue ( town_name ) ;
town_id < < long ( town - > getID ( ) ) ;
id_field - > SetValue ( town_id ) ;
2015-07-11 21:20:07 -03:00
temple_position - > SetPosition ( town - > getTemplePosition ( ) ) ;
2012-06-29 16:44:26 +02:00
town_listbox - > SetSelection ( new_selection ) ;
2015-12-06 11:42:37 -03:00
} else {
2012-06-29 16:44:26 +02:00
name_field - > Enable ( false ) ;
2015-07-11 21:20:07 -03:00
temple_position - > Enable ( false ) ;
2012-06-29 16:44:26 +02:00
select_position_button - > Enable ( false ) ;
}
Refresh ( ) ;
}
void EditTownsDialog : : OnListBoxChange ( wxCommandEvent & event )
{
UpdateSelection ( event . GetSelection ( ) ) ;
}
void EditTownsDialog : : OnClickSelectTemplePosition ( wxCommandEvent & WXUNUSED ( event ) )
{
2015-07-11 21:20:07 -03:00
Position templepos = temple_position - > GetPosition ( ) ;
2016-09-29 20:30:54 -03:00
g_gui . SetScreenCenterPosition ( templepos ) ;
2012-06-29 16:44:26 +02:00
}
void EditTownsDialog : : OnClickAdd ( wxCommandEvent & WXUNUSED ( event ) )
{
Town * new_town = newd Town ( + + max_town_id ) ;
new_town - > setName ( " Unnamed Town " ) ;
new_town - > setTemplePosition ( Position ( 0 , 0 , 0 ) ) ;
town_list . push_back ( new_town ) ;
BuildListBox ( false ) ;
UpdateSelection ( town_list . size ( ) - 1 ) ;
town_listbox - > SetSelection ( town_list . size ( ) - 1 ) ;
}
void EditTownsDialog : : OnClickRemove ( wxCommandEvent & WXUNUSED ( event ) )
{
long tmplong ;
2015-12-06 11:42:37 -03:00
if ( id_field - > GetValue ( ) . ToLong ( & tmplong ) ) {
2012-06-29 16:44:26 +02:00
uint32_t old_town_id = tmplong ;
2014-01-22 20:16:17 +01:00
Town * town = nullptr ;
2012-06-29 16:44:26 +02:00
std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ;
int selection_index = 0 ;
2015-12-06 11:42:37 -03:00
while ( town_iter ! = town_list . end ( ) ) {
if ( old_town_id = = ( * town_iter ) - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
town = * town_iter ;
break ;
}
+ + selection_index ;
+ + town_iter ;
}
if ( ! town ) return ;
Map & map = editor . map ;
2015-12-06 11:42:37 -03:00
for ( HouseMap : : iterator house_iter = map . houses . begin ( ) ; house_iter ! = map . houses . end ( ) ; + + house_iter ) {
2012-06-29 16:44:26 +02:00
House * house = house_iter - > second ;
2015-12-06 11:42:37 -03:00
if ( house - > townid = = town - > getID ( ) ) {
2016-11-05 15:28:14 +01:00
g_gui . PopupDialog ( this , " Error " , " You cannot delete a town which still has houses associated with it. " , wxOK ) ;
2012-06-29 16:44:26 +02:00
return ;
}
}
delete town ;
town_list . erase ( town_iter ) ;
BuildListBox ( false ) ;
UpdateSelection ( selection_index - 1 ) ;
}
}
void EditTownsDialog : : OnClickOK ( wxCommandEvent & WXUNUSED ( event ) )
{
long tmplong = 0 ;
2015-12-06 11:42:37 -03:00
if ( Validate ( ) & & TransferDataFromWindow ( ) ) {
2012-06-29 16:44:26 +02:00
// Save old values
2015-12-06 11:42:37 -03:00
if ( town_list . size ( ) > 0 & & id_field - > GetValue ( ) . ToLong ( & tmplong ) ) {
2012-06-29 16:44:26 +02:00
uint32_t old_town_id = tmplong ;
2014-01-22 20:16:17 +01:00
Town * old_town = nullptr ;
2012-06-29 16:44:26 +02:00
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
if ( old_town_id = = ( * town_iter ) - > getID ( ) ) {
2012-06-29 16:44:26 +02:00
old_town = * town_iter ;
break ;
}
}
2015-12-06 11:42:37 -03:00
if ( old_town ) {
2015-07-11 21:20:07 -03:00
Position templepos = temple_position - > GetPosition ( ) ;
2012-06-29 16:44:26 +02:00
//printf("Changed town %d:%s\n", old_town_id, old_town->getName().c_str());
//printf("New values %d:%s:%d:%d:%d\n", town_id, town_name.c_str(), templepos.x, templepos.y, templepos.z);
old_town - > setTemplePosition ( templepos ) ;
wxString new_name = name_field - > GetValue ( ) ;
wxString old_name = wxstr ( old_town - > getName ( ) ) ;
old_town - > setName ( nstr ( new_name ) ) ;
2015-12-06 11:42:37 -03:00
if ( new_name ! = old_name ) {
2012-06-29 16:44:26 +02:00
// Name has changed, update list
BuildListBox ( true ) ;
}
}
}
Towns & towns = editor . map . towns ;
// Verify the newd information
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
2012-06-29 16:44:26 +02:00
Town * town = * town_iter ;
if ( town - > getName ( ) = = " " ) {
2016-11-05 15:28:14 +01:00
g_gui . PopupDialog ( this , " Error " , " You can't have a town with an empty name. " , wxOK ) ;
2012-06-29 16:44:26 +02:00
return ;
}
2015-12-06 11:42:37 -03:00
if ( ! town - > getTemplePosition ( ) . isValid ( ) | |
town - > getTemplePosition ( ) . x > editor . map . getWidth ( ) | |
town - > getTemplePosition ( ) . y > editor . map . getHeight ( ) ) {
2012-06-29 16:44:26 +02:00
wxString msg ;
2016-11-05 15:28:14 +01:00
msg < < " The town " < < wxstr ( town - > getName ( ) ) < < " has an invalid temple position. " ;
g_gui . PopupDialog ( this , " Error " , msg , wxOK ) ;
2012-06-29 16:44:26 +02:00
return ;
}
}
// Clear old towns
towns . clear ( ) ;
// Build the newd town map
2015-12-06 11:42:37 -03:00
for ( std : : vector < Town * > : : iterator town_iter = town_list . begin ( ) ; town_iter ! = town_list . end ( ) ; + + town_iter ) {
2012-06-29 16:44:26 +02:00
towns . addTown ( * town_iter ) ;
}
town_list . clear ( ) ;
editor . map . doChange ( ) ;
EndModal ( 1 ) ;
2016-09-29 20:30:54 -03:00
g_gui . RefreshPalettes ( ) ;
2012-06-29 16:44:26 +02:00
}
}
void EditTownsDialog : : OnClickCancel ( wxCommandEvent & WXUNUSED ( event ) )
{
// Just close this window
EndModal ( 0 ) ;
}
// ============================================================================
2015-05-08 12:50:15 -03:00
// Go To Position Dialog
2012-06-29 16:44:26 +02:00
// Jump to a position on the map by entering XYZ coordinates
BEGIN_EVENT_TABLE ( GotoPositionDialog , wxDialog )
EVT_BUTTON ( wxID_OK , GotoPositionDialog : : OnClickOK )
EVT_BUTTON ( wxID_CANCEL , GotoPositionDialog : : OnClickCancel )
END_EVENT_TABLE ( )
GotoPositionDialog : : GotoPositionDialog ( wxWindow * parent , Editor & editor ) :
2016-11-05 15:28:14 +01:00
wxDialog ( parent , wxID_ANY , " Go To Position " , wxDefaultPosition , wxDefaultSize ) ,
2012-06-29 16:44:26 +02:00
editor ( editor )
{
Map & map = editor . map ;
2015-07-11 21:20:07 -03:00
// create topsizer
2012-06-29 16:44:26 +02:00
wxSizer * sizer = newd wxBoxSizer ( wxVERTICAL ) ;
2016-11-05 15:28:14 +01:00
posctrl = newd PositionCtrl ( this , " Destination " , map . getWidth ( ) / 2 , map . getHeight ( ) / 2 , GROUND_LAYER , map . getWidth ( ) , map . getHeight ( ) ) ;
2015-07-11 21:20:07 -03:00
sizer - > Add ( posctrl , 0 , wxTOP | wxLEFT | wxRIGHT , 20 ) ;
2012-06-29 16:44:26 +02:00
// OK/Cancel buttons
2015-07-11 21:20:07 -03:00
wxSizer * tmpsizer = newd wxBoxSizer ( wxHORIZONTAL ) ;
2016-11-05 15:28:14 +01:00
tmpsizer - > Add ( newd wxButton ( this , wxID_OK , " OK " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
tmpsizer - > Add ( newd wxButton ( this , wxID_CANCEL , " Cancel " ) , wxSizerFlags ( 1 ) . Center ( ) ) ;
2012-06-29 16:44:26 +02:00
sizer - > Add ( tmpsizer , 0 , wxALL | wxCENTER , 20 ) ; // Border to top too
SetSizerAndFit ( sizer ) ;
2016-02-29 21:47:54 -03:00
Centre ( wxBOTH ) ;
2012-06-29 16:44:26 +02:00
}
void GotoPositionDialog : : OnClickCancel ( wxCommandEvent & )
{
EndModal ( 0 ) ;
}
void GotoPositionDialog : : OnClickOK ( wxCommandEvent & )
{
2016-09-29 20:30:54 -03:00
g_gui . SetScreenCenterPosition ( posctrl - > GetPosition ( ) ) ;
2012-06-29 16:44:26 +02:00
EndModal ( 1 ) ;
}