2019-01-07 19:46:27 +00:00
# include "regionmapeditor.h"
# include "ui_regionmapeditor.h"
2019-03-25 04:10:57 +00:00
# include "imageexport.h"
2020-11-09 10:50:15 +00:00
# include "shortcut.h"
2019-01-15 22:06:18 +00:00
# include "config.h"
2019-03-25 04:10:57 +00:00
# include "log.h"
2019-01-07 19:46:27 +00:00
# include <QDir>
2019-01-09 02:03:54 +00:00
# include <QDialog>
# include <QDialogButtonBox>
2019-03-25 04:10:57 +00:00
# include <QFileDialog>
2019-01-09 02:03:54 +00:00
# include <QFormLayout>
# include <QLineEdit>
2019-03-25 04:10:57 +00:00
# include <QSpinBox>
2019-01-09 02:03:54 +00:00
# include <QColor>
2019-01-22 20:06:49 +00:00
# include <QMessageBox>
2020-08-27 01:42:42 +01:00
# include <QTransform>
2019-02-16 22:56:44 +00:00
# include <math.h>
2019-01-07 19:46:27 +00:00
2019-01-22 20:06:49 +00:00
RegionMapEditor : : RegionMapEditor ( QWidget * parent , Project * project_ ) :
2019-01-07 19:46:27 +00:00
QMainWindow ( parent ) ,
ui ( new Ui : : RegionMapEditor )
{
2019-01-09 02:03:54 +00:00
this - > ui - > setupUi ( this ) ;
2019-01-22 20:06:49 +00:00
this - > project = project_ ;
2019-01-07 19:46:27 +00:00
this - > region_map = new RegionMap ;
2019-03-25 04:10:57 +00:00
this - > ui - > action_RegionMap_Resize - > setVisible ( false ) ;
2020-11-13 04:48:03 +00:00
this - > initShortcuts ( ) ;
2020-09-25 13:12:13 +01:00
this - > restoreWindowState ( ) ;
2019-01-07 19:46:27 +00:00
}
RegionMapEditor : : ~ RegionMapEditor ( )
{
delete ui ;
2019-01-14 00:27:28 +00:00
delete region_map ;
delete region_map_item ;
delete mapsquare_selector_item ;
delete region_map_layout_item ;
delete scene_region_map_image ;
delete city_map_selector_item ;
delete city_map_item ;
delete scene_city_map_tiles ;
delete scene_city_map_image ;
delete scene_region_map_layout ;
delete scene_region_map_tiles ;
2019-01-07 19:46:27 +00:00
}
2020-09-25 13:12:13 +01:00
void RegionMapEditor : : restoreWindowState ( ) {
logInfo ( " Restoring region map editor geometry from previous session. " ) ;
QMap < QString , QByteArray > geometry = porymapConfig . getRegionMapEditorGeometry ( ) ;
this - > restoreGeometry ( geometry . value ( " region_map_editor_geometry " ) ) ;
this - > restoreState ( geometry . value ( " region_map_editor_state " ) ) ;
}
2019-01-07 19:46:27 +00:00
void RegionMapEditor : : on_action_RegionMap_Save_triggered ( ) {
2019-03-23 20:56:30 +00:00
setCurrentSquareOptions ( ) ;
2019-01-07 19:46:27 +00:00
if ( project & & region_map ) {
2019-02-25 18:31:34 +00:00
this - > region_map - > save ( ) ;
this - > city_map_item - > save ( ) ;
2019-01-07 19:46:27 +00:00
}
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = false ;
2019-01-07 19:46:27 +00:00
}
2019-03-23 20:56:30 +00:00
void RegionMapEditor : : setCurrentSquareOptions ( ) {
if ( project & & region_map ) {
this - > region_map - > saveOptions (
this - > currIndex ,
this - > ui - > comboBox_RM_ConnectedMap - > currentText ( ) ,
this - > ui - > lineEdit_RM_MapName - > text ( ) ,
2019-03-25 04:10:57 +00:00
this - > region_map - > map_squares [ this - > currIndex ] . x ,
this - > region_map - > map_squares [ this - > currIndex ] . y
2019-03-23 20:56:30 +00:00
) ;
}
}
2020-02-12 21:45:21 +00:00
bool RegionMapEditor : : loadRegionMapData ( ) {
if ( ! this - > region_map - > init ( project ) ) {
return false ;
}
2019-01-15 22:06:18 +00:00
this - > currIndex = this - > region_map - > width ( ) * this - > region_map - > padTop + this - > region_map - > padLeft ;
2019-01-07 19:46:27 +00:00
displayRegionMap ( ) ;
2020-02-12 21:45:21 +00:00
return true ;
2019-01-07 19:46:27 +00:00
}
2020-02-12 21:45:21 +00:00
bool RegionMapEditor : : loadCityMaps ( ) {
2019-01-07 19:46:27 +00:00
QDir directory ( project - > root + " /graphics/pokenav/city_maps " ) ;
QStringList files = directory . entryList ( QStringList ( ) < < " *.bin " , QDir : : Files ) ;
QStringList without_bin ;
for ( QString file : files ) {
without_bin . append ( file . remove ( " .bin " ) ) ;
}
this - > ui - > comboBox_CityMap_picker - > addItems ( without_bin ) ;
2020-02-12 21:45:21 +00:00
return true ;
2019-01-07 19:46:27 +00:00
}
2020-11-09 10:50:15 +00:00
void RegionMapEditor : : initShortcuts ( ) {
2020-11-13 04:48:03 +00:00
auto * shortcut_RM_Options_delete = new Shortcut (
{ QKeySequence ( " Del " ) , QKeySequence ( " Backspace " ) } , this , SLOT ( on_pushButton_RM_Options_delete_clicked ( ) ) ) ;
shortcut_RM_Options_delete - > setObjectName ( " shortcut_RM_Options_delete " ) ;
shortcut_RM_Options_delete - > setWhatsThis ( " Map Layout: Delete Square " ) ;
2020-11-09 10:50:15 +00:00
shortcutsConfig . load ( ) ;
shortcutsConfig . setDefaultShortcuts ( shortcutableObjects ( ) ) ;
applyUserShortcuts ( ) ;
}
QObjectList RegionMapEditor : : shortcutableObjects ( ) const {
QObjectList shortcutable_objects ;
2020-11-13 04:48:03 +00:00
2020-11-09 10:50:15 +00:00
for ( auto * action : findChildren < QAction * > ( ) )
2020-11-13 04:48:03 +00:00
if ( ! action - > objectName ( ) . isEmpty ( ) )
2020-11-09 10:50:15 +00:00
shortcutable_objects . append ( qobject_cast < QObject * > ( action ) ) ;
for ( auto * shortcut : findChildren < Shortcut * > ( ) )
2020-11-13 04:48:03 +00:00
if ( ! shortcut - > objectName ( ) . isEmpty ( ) )
2020-11-09 10:50:15 +00:00
shortcutable_objects . append ( qobject_cast < QObject * > ( shortcut ) ) ;
2020-11-13 04:48:03 +00:00
2020-11-09 10:50:15 +00:00
return shortcutable_objects ;
}
void RegionMapEditor : : applyUserShortcuts ( ) {
for ( auto * action : findChildren < QAction * > ( ) )
2020-11-13 04:48:03 +00:00
if ( ! action - > objectName ( ) . isEmpty ( ) )
2020-11-09 10:50:15 +00:00
action - > setShortcuts ( shortcutsConfig . userShortcuts ( action ) ) ;
for ( auto * shortcut : findChildren < Shortcut * > ( ) )
2020-11-13 04:48:03 +00:00
if ( ! shortcut - > objectName ( ) . isEmpty ( ) )
2020-11-09 10:50:15 +00:00
shortcut - > setKeys ( shortcutsConfig . userShortcuts ( shortcut ) ) ;
}
2019-01-07 19:46:27 +00:00
void RegionMapEditor : : displayRegionMap ( ) {
displayRegionMapTileSelector ( ) ;
displayCityMapTileSelector ( ) ;
displayRegionMapImage ( ) ;
displayRegionMapLayout ( ) ;
displayRegionMapLayoutOptions ( ) ;
2019-04-13 02:25:43 +01:00
displayRegionMapEntriesImage ( ) ;
displayRegionMapEntryOptions ( ) ;
2019-04-29 23:24:35 +01:00
this - > hasUnsavedChanges = false ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : displayRegionMapImage ( ) {
2019-01-14 00:27:28 +00:00
if ( ! scene_region_map_image ) {
this - > scene_region_map_image = new QGraphicsScene ;
}
if ( region_map_item & & scene_region_map_image ) {
scene_region_map_image - > removeItem ( region_map_item ) ;
delete region_map_item ;
}
2019-01-07 19:46:27 +00:00
this - > region_map_item = new RegionMapPixmapItem ( this - > region_map , this - > mapsquare_selector_item ) ;
this - > region_map_item - > draw ( ) ;
2019-01-14 00:27:28 +00:00
connect ( this - > region_map_item , & RegionMapPixmapItem : : mouseEvent ,
this , & RegionMapEditor : : mouseEvent_region_map ) ;
connect ( this - > region_map_item , & RegionMapPixmapItem : : hoveredRegionMapTileChanged ,
this , & RegionMapEditor : : onHoveredRegionMapTileChanged ) ;
connect ( this - > region_map_item , & RegionMapPixmapItem : : hoveredRegionMapTileCleared ,
this , & RegionMapEditor : : onHoveredRegionMapTileCleared ) ;
2019-01-07 19:46:27 +00:00
this - > scene_region_map_image - > addItem ( this - > region_map_item ) ;
2019-01-15 22:06:18 +00:00
this - > scene_region_map_image - > setSceneRect ( this - > scene_region_map_image - > itemsBoundingRect ( ) ) ;
2019-01-07 19:46:27 +00:00
this - > ui - > graphicsView_Region_Map_BkgImg - > setScene ( this - > scene_region_map_image ) ;
2019-01-22 20:06:49 +00:00
2019-01-28 18:47:20 +00:00
if ( regionMapFirstDraw ) {
2019-04-08 00:25:29 +01:00
on_verticalSlider_Zoom_Map_Image_valueChanged ( this - > ui - > verticalSlider_Zoom_Map_Image - > value ( ) ) ;
2019-01-28 18:47:20 +00:00
RegionMapHistoryItem * commit = new RegionMapHistoryItem (
RegionMapEditorBox : : BackgroundImage , this - > region_map - > getTiles ( ) , this - > region_map - > width ( ) , this - > region_map - > height ( )
) ;
history . push ( commit ) ;
regionMapFirstDraw = false ;
}
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : displayRegionMapLayout ( ) {
2019-01-14 00:27:28 +00:00
if ( ! scene_region_map_layout ) {
this - > scene_region_map_layout = new QGraphicsScene ;
}
if ( region_map_layout_item & & scene_region_map_layout ) {
this - > scene_region_map_layout - > removeItem ( region_map_layout_item ) ;
delete region_map_layout_item ;
}
2019-01-07 19:46:27 +00:00
this - > region_map_layout_item = new RegionMapLayoutPixmapItem ( this - > region_map , this - > mapsquare_selector_item ) ;
2019-01-14 00:27:28 +00:00
2019-01-07 19:46:27 +00:00
connect ( this - > region_map_layout_item , & RegionMapLayoutPixmapItem : : selectedTileChanged ,
2019-01-14 00:27:28 +00:00
this , & RegionMapEditor : : onRegionMapLayoutSelectedTileChanged ) ;
2019-01-07 19:46:27 +00:00
connect ( this - > region_map_layout_item , & RegionMapLayoutPixmapItem : : hoveredTileChanged ,
this , & RegionMapEditor : : onRegionMapLayoutHoveredTileChanged ) ;
connect ( this - > region_map_layout_item , & RegionMapLayoutPixmapItem : : hoveredTileCleared ,
this , & RegionMapEditor : : onRegionMapLayoutHoveredTileCleared ) ;
2019-01-14 00:27:28 +00:00
2019-04-08 00:25:29 +01:00
updateRegionMapLayoutOptions ( this - > currIndex ) ;
2019-01-07 19:46:27 +00:00
this - > region_map_layout_item - > draw ( ) ;
2019-01-14 00:27:28 +00:00
this - > region_map_layout_item - > select ( this - > currIndex ) ;
2019-01-07 19:46:27 +00:00
this - > scene_region_map_layout - > addItem ( region_map_layout_item ) ;
2019-01-15 22:06:18 +00:00
this - > scene_region_map_layout - > setSceneRect ( this - > scene_region_map_layout - > itemsBoundingRect ( ) ) ;
2019-01-07 19:46:27 +00:00
this - > ui - > graphicsView_Region_Map_Layout - > setScene ( this - > scene_region_map_layout ) ;
}
void RegionMapEditor : : displayRegionMapLayoutOptions ( ) {
2019-01-15 22:06:18 +00:00
this - > ui - > comboBox_RM_ConnectedMap - > clear ( ) ;
2019-02-25 18:31:34 +00:00
this - > ui - > comboBox_RM_ConnectedMap - > addItems ( this - > project - > mapSectionValueToName . values ( ) ) ;
2019-01-07 19:46:27 +00:00
this - > ui - > frame_RM_Options - > setEnabled ( true ) ;
2019-03-23 20:56:30 +00:00
updateRegionMapLayoutOptions ( this - > currIndex ) ;
2019-01-22 20:06:49 +00:00
// TODO: implement when the code is decompiled
this - > ui - > label_RM_CityMap - > setVisible ( false ) ;
this - > ui - > comboBox_RM_CityMap - > setVisible ( false ) ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : updateRegionMapLayoutOptions ( int index ) {
2019-01-22 20:06:49 +00:00
this - > ui - > comboBox_RM_ConnectedMap - > blockSignals ( true ) ;
2021-02-15 09:21:41 +00:00
this - > ui - > lineEdit_RM_MapName - > setText ( this - > project - > mapSecToMapHoverName . value ( this - > region_map - > map_squares [ index ] . mapsec ) ) ;
2019-01-07 19:46:27 +00:00
this - > ui - > comboBox_RM_ConnectedMap - > setCurrentText ( this - > region_map - > map_squares [ index ] . mapsec ) ;
2019-01-22 20:06:49 +00:00
this - > ui - > comboBox_RM_ConnectedMap - > blockSignals ( false ) ;
2019-01-07 19:46:27 +00:00
}
2019-04-13 02:25:43 +01:00
void RegionMapEditor : : displayRegionMapEntriesImage ( ) {
if ( ! scene_region_map_entries ) {
this - > scene_region_map_entries = new QGraphicsScene ;
}
if ( region_map_entries_item & & scene_region_map_entries ) {
this - > scene_region_map_entries - > removeItem ( region_map_entries_item ) ;
delete region_map_entries_item ;
}
this - > region_map_entries_item = new RegionMapEntriesPixmapItem ( this - > region_map , this - > mapsquare_selector_item ) ;
2019-04-13 19:25:12 +01:00
connect ( this - > region_map_entries_item , & RegionMapEntriesPixmapItem : : entryPositionChanged ,
this , & RegionMapEditor : : onRegionMapEntryDragged ) ;
2019-04-13 02:25:43 +01:00
if ( entriesFirstDraw ) {
QString first = this - > project - > mapSectionValueToName . first ( ) ;
this - > region_map_entries_item - > currentSection = first ;
this - > activeEntry = first ;
updateRegionMapEntryOptions ( first ) ;
entriesFirstDraw = false ;
}
this - > region_map_entries_item - > draw ( ) ;
int idx = this - > region_map - > getMapSquareIndex ( this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . x + this - > region_map - > padLeft ,
this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . y + this - > region_map - > padTop ) ;
this - > region_map_entries_item - > select ( idx ) ;
this - > scene_region_map_entries - > addItem ( region_map_entries_item ) ;
this - > scene_region_map_entries - > setSceneRect ( this - > scene_region_map_entries - > itemsBoundingRect ( ) ) ;
this - > ui - > graphicsView_Region_Map_Entries - > setScene ( this - > scene_region_map_entries ) ;
}
void RegionMapEditor : : displayRegionMapEntryOptions ( ) {
this - > ui - > comboBox_RM_Entry_MapSection - > addItems ( this - > project - > mapSectionValueToName . values ( ) ) ;
int width = this - > region_map - > width ( ) - this - > region_map - > padLeft - this - > region_map - > padRight ;
int height = this - > region_map - > height ( ) - this - > region_map - > padTop - this - > region_map - > padBottom ;
this - > ui - > spinBox_RM_Entry_x - > setMaximum ( width - 1 ) ;
this - > ui - > spinBox_RM_Entry_y - > setMaximum ( height - 1 ) ;
this - > ui - > spinBox_RM_Entry_width - > setMinimum ( 1 ) ;
this - > ui - > spinBox_RM_Entry_height - > setMinimum ( 1 ) ;
this - > ui - > spinBox_RM_Entry_width - > setMaximum ( width ) ;
this - > ui - > spinBox_RM_Entry_height - > setMaximum ( height ) ;
}
void RegionMapEditor : : updateRegionMapEntryOptions ( QString section ) {
2019-04-13 04:07:00 +01:00
bool enabled = section = = " MAPSEC_NONE " ? false : true ;
this - > ui - > spinBox_RM_Entry_x - > setEnabled ( enabled ) ;
this - > ui - > spinBox_RM_Entry_y - > setEnabled ( enabled ) ;
this - > ui - > spinBox_RM_Entry_width - > setEnabled ( enabled ) ;
this - > ui - > spinBox_RM_Entry_height - > setEnabled ( enabled ) ;
2019-08-19 15:21:40 +01:00
this - > ui - > spinBox_RM_Entry_x - > blockSignals ( true ) ;
this - > ui - > spinBox_RM_Entry_y - > blockSignals ( true ) ;
this - > ui - > spinBox_RM_Entry_width - > blockSignals ( true ) ;
this - > ui - > spinBox_RM_Entry_height - > blockSignals ( true ) ;
2019-04-13 02:25:43 +01:00
this - > ui - > comboBox_RM_Entry_MapSection - > setCurrentText ( section ) ;
this - > activeEntry = section ;
this - > region_map_entries_item - > currentSection = section ;
RegionMapEntry entry = this - > region_map - > mapSecToMapEntry . value ( section ) ;
this - > ui - > spinBox_RM_Entry_x - > setValue ( entry . x ) ;
this - > ui - > spinBox_RM_Entry_y - > setValue ( entry . y ) ;
this - > ui - > spinBox_RM_Entry_width - > setValue ( entry . width ) ;
this - > ui - > spinBox_RM_Entry_height - > setValue ( entry . height ) ;
2019-08-19 15:21:40 +01:00
this - > ui - > spinBox_RM_Entry_x - > blockSignals ( false ) ;
this - > ui - > spinBox_RM_Entry_y - > blockSignals ( false ) ;
this - > ui - > spinBox_RM_Entry_width - > blockSignals ( false ) ;
this - > ui - > spinBox_RM_Entry_height - > blockSignals ( false ) ;
2019-04-13 02:25:43 +01:00
}
2019-01-07 19:46:27 +00:00
void RegionMapEditor : : displayRegionMapTileSelector ( ) {
2019-01-14 00:27:28 +00:00
if ( ! scene_region_map_tiles ) {
this - > scene_region_map_tiles = new QGraphicsScene ;
}
if ( mapsquare_selector_item & & scene_region_map_tiles ) {
this - > scene_region_map_tiles - > removeItem ( mapsquare_selector_item ) ;
delete mapsquare_selector_item ;
}
2019-01-28 18:47:20 +00:00
this - > mapsquare_selector_item = new TilemapTileSelector ( QPixmap ( this - > region_map - > pngPath ( ) ) ) ;
2019-01-07 19:46:27 +00:00
this - > mapsquare_selector_item - > draw ( ) ;
this - > scene_region_map_tiles - > addItem ( this - > mapsquare_selector_item ) ;
2019-01-15 22:06:18 +00:00
connect ( this - > mapsquare_selector_item , & TilemapTileSelector : : selectedTileChanged ,
this , & RegionMapEditor : : onRegionMapTileSelectorSelectedTileChanged ) ;
2019-01-07 19:46:27 +00:00
connect ( this - > mapsquare_selector_item , & TilemapTileSelector : : hoveredTileChanged ,
this , & RegionMapEditor : : onRegionMapTileSelectorHoveredTileChanged ) ;
connect ( this - > mapsquare_selector_item , & TilemapTileSelector : : hoveredTileCleared ,
this , & RegionMapEditor : : onRegionMapTileSelectorHoveredTileCleared ) ;
this - > ui - > graphicsView_RegionMap_Tiles - > setScene ( this - > scene_region_map_tiles ) ;
2019-04-08 00:25:29 +01:00
on_verticalSlider_Zoom_Image_Tiles_valueChanged ( this - > ui - > verticalSlider_Zoom_Image_Tiles - > value ( ) ) ;
2019-01-15 22:06:18 +00:00
this - > mapsquare_selector_item - > select ( this - > selectedImageTile ) ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : displayCityMapTileSelector ( ) {
2019-01-14 00:27:28 +00:00
if ( ! scene_city_map_tiles ) {
this - > scene_city_map_tiles = new QGraphicsScene ;
}
if ( city_map_selector_item & & scene_city_map_tiles ) {
scene_city_map_tiles - > removeItem ( city_map_selector_item ) ;
delete city_map_selector_item ;
}
2019-01-28 18:47:20 +00:00
this - > city_map_selector_item = new TilemapTileSelector ( QPixmap ( this - > region_map - > cityTilesPath ( ) ) ) ;
2019-01-07 19:46:27 +00:00
this - > city_map_selector_item - > draw ( ) ;
this - > scene_city_map_tiles - > addItem ( this - > city_map_selector_item ) ;
2019-01-15 22:06:18 +00:00
connect ( this - > city_map_selector_item , & TilemapTileSelector : : selectedTileChanged ,
this , & RegionMapEditor : : onCityMapTileSelectorSelectedTileChanged ) ;
2019-01-07 19:46:27 +00:00
this - > ui - > graphicsView_City_Map_Tiles - > setScene ( this - > scene_city_map_tiles ) ;
2019-04-08 00:25:29 +01:00
on_verticalSlider_Zoom_City_Tiles_valueChanged ( this - > ui - > verticalSlider_Zoom_City_Tiles - > value ( ) ) ;
2019-01-15 22:06:18 +00:00
this - > city_map_selector_item - > select ( this - > selectedCityTile ) ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : displayCityMap ( QString f ) {
QString file = this - > project - > root + " /graphics/pokenav/city_maps/ " + f + " .bin " ;
if ( ! scene_city_map_image ) {
scene_city_map_image = new QGraphicsScene ;
}
if ( city_map_item & & scene_city_map_image ) {
scene_city_map_image - > removeItem ( city_map_item ) ;
delete city_map_item ;
}
city_map_item = new CityMapPixmapItem ( file , this - > city_map_selector_item ) ;
city_map_item - > draw ( ) ;
2019-01-14 00:27:28 +00:00
connect ( this - > city_map_item , & CityMapPixmapItem : : mouseEvent ,
this , & RegionMapEditor : : mouseEvent_city_map ) ;
2019-01-07 19:46:27 +00:00
scene_city_map_image - > addItem ( city_map_item ) ;
scene_city_map_image - > setSceneRect ( this - > scene_city_map_image - > sceneRect ( ) ) ;
this - > ui - > graphicsView_City_Map - > setScene ( scene_city_map_image ) ;
2019-04-08 00:25:29 +01:00
on_verticalSlider_Zoom_City_Map_valueChanged ( this - > ui - > verticalSlider_Zoom_City_Map - > value ( ) ) ;
2019-01-07 19:46:27 +00:00
}
2019-01-09 02:03:54 +00:00
bool RegionMapEditor : : createCityMap ( QString name ) {
bool errored = false ;
QString file = this - > project - > root + " /graphics/pokenav/city_maps/ " + name + " .bin " ;
uint8_t filler = 0x30 ;
uint8_t border = 0x7 ;
uint8_t blank = 0x1 ;
QByteArray new_data ( 400 , filler ) ;
for ( int i = 0 ; i < new_data . size ( ) ; i + + ) {
if ( i % 2 ) continue ;
int x = i % 20 ;
int y = i / 20 ;
if ( y < = 1 | | y > = 8 | | x < = 3 | | x > = 16 )
new_data [ i ] = border ;
else
new_data [ i ] = blank ;
}
QFile binFile ( file ) ;
if ( ! binFile . open ( QIODevice : : WriteOnly ) ) errored = true ;
binFile . write ( new_data ) ;
binFile . close ( ) ;
loadCityMaps ( ) ;
this - > ui - > comboBox_CityMap_picker - > setCurrentText ( name ) ;
return ! errored ;
}
2019-01-07 19:46:27 +00:00
2019-04-13 04:07:00 +01:00
bool RegionMapEditor : : tryInsertNewMapEntry ( QString mapsec ) {
if ( ! this - > region_map - > mapSecToMapEntry . keys ( ) . contains ( mapsec ) & & mapsec ! = " MAPSEC_NONE " ) {
RegionMapEntry entry ( 0 , 0 , 1 , 1 , region_map - > fixCase ( mapsec ) ) ;
this - > region_map - > sMapNamesMap . insert ( region_map - > fixCase ( mapsec ) , QString ( ) ) ;
this - > region_map - > mapSecToMapEntry . insert ( mapsec , entry ) ;
this - > region_map - > sMapNames . append ( region_map - > fixCase ( mapsec ) ) ;
return true ;
}
return false ;
}
2019-01-15 22:06:18 +00:00
void RegionMapEditor : : onRegionMapTileSelectorSelectedTileChanged ( unsigned id ) {
this - > selectedImageTile = id ;
}
void RegionMapEditor : : onCityMapTileSelectorSelectedTileChanged ( unsigned id ) {
this - > selectedCityTile = id ;
}
2019-01-07 19:46:27 +00:00
void RegionMapEditor : : onRegionMapTileSelectorHoveredTileChanged ( unsigned tileId ) {
QString message = QString ( " Tile: 0x " ) + QString ( " %1 " ) . arg ( tileId , 4 , 16 , QChar ( ' 0 ' ) ) . toUpper ( ) ;
this - > ui - > statusbar - > showMessage ( message ) ;
}
void RegionMapEditor : : onRegionMapTileSelectorHoveredTileCleared ( ) {
2019-01-14 00:27:28 +00:00
this - > ui - > statusbar - > clearMessage ( ) ;
2019-01-07 19:46:27 +00:00
}
2019-04-13 19:25:12 +01:00
void RegionMapEditor : : onRegionMapEntryDragged ( int new_x , int new_y ) {
on_spinBox_RM_Entry_x_valueChanged ( new_x ) ;
on_spinBox_RM_Entry_y_valueChanged ( new_y ) ;
updateRegionMapEntryOptions ( activeEntry ) ;
}
2019-01-07 19:46:27 +00:00
void RegionMapEditor : : onRegionMapLayoutSelectedTileChanged ( int index ) {
2019-03-23 20:56:30 +00:00
setCurrentSquareOptions ( ) ;
2019-01-07 19:46:27 +00:00
QString message = QString ( ) ;
2019-01-14 00:27:28 +00:00
this - > currIndex = index ;
2019-01-28 18:47:20 +00:00
this - > region_map_layout_item - > highlightedTile = index ;
2019-01-07 19:46:27 +00:00
if ( this - > region_map - > map_squares [ index ] . has_map ) {
2021-02-15 09:21:41 +00:00
message = QString ( " \t %1 " ) . arg ( this - > project - > mapSecToMapHoverName . value (
2019-01-28 18:47:20 +00:00
this - > region_map - > map_squares [ index ] . mapsec ) ) . remove ( " {NAME_END} " ) ;
2019-01-07 19:46:27 +00:00
}
this - > ui - > statusbar - > showMessage ( message ) ;
updateRegionMapLayoutOptions ( index ) ;
2019-03-23 20:56:30 +00:00
this - > region_map_layout_item - > draw ( ) ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : onRegionMapLayoutHoveredTileChanged ( int index ) {
QString message = QString ( ) ;
int x = this - > region_map - > map_squares [ index ] . x ;
int y = this - > region_map - > map_squares [ index ] . y ;
if ( x > = 0 & & y > = 0 ) {
message = QString ( " (%1, %2) " ) . arg ( x ) . arg ( y ) ;
if ( this - > region_map - > map_squares [ index ] . has_map ) {
2021-02-15 09:21:41 +00:00
message + = QString ( " \t %1 " ) . arg ( this - > project - > mapSecToMapHoverName . value (
2019-01-28 18:47:20 +00:00
this - > region_map - > map_squares [ index ] . mapsec ) ) . remove ( " {NAME_END} " ) ;
2019-01-07 19:46:27 +00:00
}
}
this - > ui - > statusbar - > showMessage ( message ) ;
}
void RegionMapEditor : : onRegionMapLayoutHoveredTileCleared ( ) {
2019-01-14 00:27:28 +00:00
this - > ui - > statusbar - > clearMessage ( ) ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : onHoveredRegionMapTileChanged ( int x , int y ) {
2019-01-28 18:47:20 +00:00
QString message = QString ( " x: %1, y: %2 Tile: 0x " ) . arg ( x ) . arg ( y )
2019-01-15 22:06:18 +00:00
+ QString ( " %1 " ) . arg ( this - > region_map - > getTileId ( x , y ) , 4 , 16 , QChar ( ' 0 ' ) ) . toUpper ( ) ;
2019-01-14 00:27:28 +00:00
this - > ui - > statusbar - > showMessage ( message ) ;
2019-01-07 19:46:27 +00:00
}
void RegionMapEditor : : onHoveredRegionMapTileCleared ( ) {
this - > ui - > statusbar - > clearMessage ( ) ;
}
void RegionMapEditor : : mouseEvent_region_map ( QGraphicsSceneMouseEvent * event , RegionMapPixmapItem * item ) {
2019-01-22 20:06:49 +00:00
QPointF pos = event - > pos ( ) ;
int x = static_cast < int > ( pos . x ( ) ) / 8 ;
int y = static_cast < int > ( pos . y ( ) ) / 8 ;
int index = this - > region_map - > getMapSquareIndex ( x , y ) ;
if ( index > this - > region_map - > map_squares . size ( ) - 1 ) return ;
2019-01-07 19:46:27 +00:00
if ( event - > buttons ( ) & Qt : : RightButton ) {
item - > select ( event ) ;
2019-01-15 22:06:18 +00:00
//} else if (event->buttons() & Qt::MiddleButton) {// TODO
2019-01-07 19:46:27 +00:00
} else {
2019-01-22 20:06:49 +00:00
if ( event - > type ( ) = = QEvent : : GraphicsSceneMouseRelease ) {
RegionMapHistoryItem * current = history . current ( ) ;
bool addToHistory = ! ( current & & current - > tiles = = this - > region_map - > getTiles ( ) ) ;
if ( addToHistory ) {
RegionMapHistoryItem * commit = new RegionMapHistoryItem (
2019-01-28 18:47:20 +00:00
RegionMapEditorBox : : BackgroundImage , this - > region_map - > getTiles ( ) , this - > region_map - > width ( ) , this - > region_map - > height ( )
2019-01-22 20:06:49 +00:00
) ;
history . push ( commit ) ;
}
2019-03-25 04:10:57 +00:00
} else {
item - > paint ( event ) ;
this - > region_map_layout_item - > draw ( ) ;
this - > hasUnsavedChanges = true ;
2019-01-22 20:06:49 +00:00
}
2019-01-07 19:46:27 +00:00
}
}
void RegionMapEditor : : mouseEvent_city_map ( QGraphicsSceneMouseEvent * event , CityMapPixmapItem * item ) {
2019-01-28 18:47:20 +00:00
if ( cityMapFirstDraw ) {
RegionMapHistoryItem * commit = new RegionMapHistoryItem (
RegionMapEditorBox : : CityMapImage , this - > city_map_item - > getTiles ( ) , this - > city_map_item - > file
) ;
history . push ( commit ) ;
cityMapFirstDraw = false ;
}
2019-01-14 00:27:28 +00:00
if ( event - > buttons ( ) & Qt : : RightButton ) { // TODO
2019-01-15 22:06:18 +00:00
//} else if (event->buttons() & Qt::MiddleButton) {// TODO
2019-01-07 19:46:27 +00:00
} else {
2019-01-22 20:06:49 +00:00
if ( event - > type ( ) = = QEvent : : GraphicsSceneMouseRelease ) {
RegionMapHistoryItem * current = history . current ( ) ;
bool addToHistory = ! ( current & & current - > tiles = = this - > city_map_item - > getTiles ( ) ) ;
if ( addToHistory ) {
RegionMapHistoryItem * commit = new RegionMapHistoryItem (
RegionMapEditorBox : : CityMapImage , this - > city_map_item - > getTiles ( ) , this - > city_map_item - > file
) ;
history . push ( commit ) ;
}
2019-03-25 04:10:57 +00:00
} else {
item - > paint ( event ) ;
this - > hasUnsavedChanges = true ;
2019-01-22 20:06:49 +00:00
}
2019-01-07 19:46:27 +00:00
}
}
void RegionMapEditor : : on_tabWidget_Region_Map_currentChanged ( int index ) {
this - > ui - > stackedWidget_RM_Options - > setCurrentIndex ( index ) ;
2019-01-15 22:06:18 +00:00
switch ( index )
{
case 0 :
2019-01-22 20:06:49 +00:00
this - > ui - > verticalSlider_Zoom_Image_Tiles - > setVisible ( true ) ;
2019-04-13 02:25:43 +01:00
this - > region_map_item - > draw ( ) ;
2019-01-15 22:06:18 +00:00
break ;
case 1 :
2019-01-22 20:06:49 +00:00
this - > ui - > verticalSlider_Zoom_Image_Tiles - > setVisible ( false ) ;
2019-04-13 02:25:43 +01:00
this - > region_map_layout_item - > draw ( ) ;
break ;
case 2 :
this - > ui - > verticalSlider_Zoom_Image_Tiles - > setVisible ( false ) ;
this - > region_map_entries_item - > draw ( ) ;
2019-01-15 22:06:18 +00:00
break ;
}
2019-01-07 19:46:27 +00:00
}
2021-02-06 00:43:49 +00:00
void RegionMapEditor : : on_comboBox_RM_ConnectedMap_textActivated ( const QString & mapsec ) {
2021-05-04 03:56:51 +01:00
this - > ui - > lineEdit_RM_MapName - > setText ( this - > project - > mapSecToMapHoverName . value ( mapsec ) ) ;
2019-08-19 03:46:32 +01:00
onRegionMapLayoutSelectedTileChanged ( this - > currIndex ) ; // re-draw layout image
2019-04-30 01:13:22 +01:00
this - > hasUnsavedChanges = true ; // sometimes this is called for unknown reasons
2019-01-07 19:46:27 +00:00
}
2021-02-06 00:43:49 +00:00
void RegionMapEditor : : on_comboBox_RM_Entry_MapSection_textActivated ( const QString & text ) {
2019-04-13 02:25:43 +01:00
this - > activeEntry = text ;
this - > region_map_entries_item - > currentSection = activeEntry ;
updateRegionMapEntryOptions ( activeEntry ) ;
int idx = this - > region_map - > getMapSquareIndex ( this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . x + this - > region_map - > padLeft ,
this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . y + this - > region_map - > padTop ) ;
this - > region_map_entries_item - > select ( idx ) ;
this - > region_map_entries_item - > draw ( ) ;
}
void RegionMapEditor : : on_spinBox_RM_Entry_x_valueChanged ( int x ) {
2019-04-13 04:07:00 +01:00
tryInsertNewMapEntry ( activeEntry ) ;
2019-04-13 02:25:43 +01:00
this - > region_map - > mapSecToMapEntry [ activeEntry ] . setX ( x ) ;
int idx = this - > region_map - > getMapSquareIndex ( this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . x + this - > region_map - > padLeft ,
this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . y + this - > region_map - > padTop ) ;
this - > region_map_entries_item - > select ( idx ) ;
this - > region_map_entries_item - > draw ( ) ;
2019-04-30 01:13:22 +01:00
this - > ui - > spinBox_RM_Entry_width - > setMaximum ( this - > region_map - > width ( ) - this - > region_map - > padLeft - this - > region_map - > padRight - x ) ;
2019-04-13 02:25:43 +01:00
this - > hasUnsavedChanges = true ;
}
void RegionMapEditor : : on_spinBox_RM_Entry_y_valueChanged ( int y ) {
2019-04-13 04:07:00 +01:00
tryInsertNewMapEntry ( activeEntry ) ;
2019-04-13 02:25:43 +01:00
this - > region_map - > mapSecToMapEntry [ activeEntry ] . setY ( y ) ;
int idx = this - > region_map - > getMapSquareIndex ( this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . x + this - > region_map - > padLeft ,
this - > region_map - > mapSecToMapEntry . value ( activeEntry ) . y + this - > region_map - > padTop ) ;
this - > region_map_entries_item - > select ( idx ) ;
this - > region_map_entries_item - > draw ( ) ;
2019-04-30 01:13:22 +01:00
this - > ui - > spinBox_RM_Entry_height - > setMaximum ( this - > region_map - > height ( ) - this - > region_map - > padTop - this - > region_map - > padBottom - y ) ;
2019-04-13 02:25:43 +01:00
this - > hasUnsavedChanges = true ;
}
void RegionMapEditor : : on_spinBox_RM_Entry_width_valueChanged ( int width ) {
2019-04-13 04:07:00 +01:00
tryInsertNewMapEntry ( activeEntry ) ;
2019-04-13 02:25:43 +01:00
this - > region_map - > mapSecToMapEntry [ activeEntry ] . setWidth ( width ) ;
this - > region_map_entries_item - > draw ( ) ;
this - > hasUnsavedChanges = true ;
}
void RegionMapEditor : : on_spinBox_RM_Entry_height_valueChanged ( int height ) {
2019-04-13 04:07:00 +01:00
tryInsertNewMapEntry ( activeEntry ) ;
2019-04-13 02:25:43 +01:00
this - > region_map - > mapSecToMapEntry [ activeEntry ] . setHeight ( height ) ;
this - > region_map_entries_item - > draw ( ) ;
this - > hasUnsavedChanges = true ;
}
2019-08-07 04:35:02 +01:00
void RegionMapEditor : : on_lineEdit_RM_MapName_textEdited ( const QString & ) {
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = true ;
2019-01-07 19:46:27 +00:00
}
2019-01-09 02:03:54 +00:00
void RegionMapEditor : : on_pushButton_RM_Options_delete_clicked ( ) {
2019-01-14 00:27:28 +00:00
this - > region_map - > resetSquare ( this - > region_map_layout_item - > selectedTile ) ;
2019-03-25 04:10:57 +00:00
updateRegionMapLayoutOptions ( this - > region_map_layout_item - > selectedTile ) ;
2019-01-14 00:27:28 +00:00
this - > region_map_layout_item - > draw ( ) ;
this - > region_map_layout_item - > select ( this - > region_map_layout_item - > selectedTile ) ;
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = true ;
2019-01-09 02:03:54 +00:00
}
void RegionMapEditor : : on_pushButton_CityMap_add_clicked ( ) {
QDialog popup ( this , Qt : : WindowTitleHint | Qt : : WindowCloseButtonHint ) ;
popup . setWindowTitle ( " New City Map " ) ;
popup . setWindowModality ( Qt : : NonModal ) ;
QFormLayout form ( & popup ) ;
QLineEdit * input = new QLineEdit ( ) ;
2020-09-10 21:00:26 +01:00
input - > setClearButtonEnabled ( true ) ;
2019-01-09 02:03:54 +00:00
form . addRow ( new QLabel ( " Name: " ) , input ) ;
QDialogButtonBox buttonBox ( QDialogButtonBox : : Ok | QDialogButtonBox : : Cancel , Qt : : Horizontal , & popup ) ;
QString name ;
form . addRow ( & buttonBox ) ;
2021-02-14 15:07:55 +00:00
connect ( & buttonBox , & QDialogButtonBox : : rejected , & popup , & QDialog : : reject ) ;
2019-01-09 02:03:54 +00:00
connect ( & buttonBox , & QDialogButtonBox : : accepted , [ & popup , & input , & name ] ( ) {
name = input - > text ( ) . remove ( QRegularExpression ( " [^a-zA-Z0-9_]+ " ) ) ;
if ( ! name . isEmpty ( ) )
popup . accept ( ) ;
} ) ;
if ( popup . exec ( ) = = QDialog : : Accepted ) {
createCityMap ( name ) ;
}
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = true ;
2019-01-09 02:03:54 +00:00
}
2019-01-14 00:27:28 +00:00
void RegionMapEditor : : on_action_RegionMap_Resize_triggered ( ) {
QDialog popup ( this , Qt : : WindowTitleHint | Qt : : WindowCloseButtonHint ) ;
popup . setWindowTitle ( " New Region Map Dimensions " ) ;
popup . setWindowModality ( Qt : : NonModal ) ;
QFormLayout form ( & popup ) ;
QSpinBox * widthSpinBox = new QSpinBox ( ) ;
QSpinBox * heightSpinBox = new QSpinBox ( ) ;
widthSpinBox - > setMinimum ( 32 ) ;
heightSpinBox - > setMinimum ( 20 ) ;
2019-01-28 18:47:20 +00:00
widthSpinBox - > setMaximum ( 60 ) ; // TODO: find real limits
2019-01-14 00:27:28 +00:00
heightSpinBox - > setMaximum ( 40 ) ;
widthSpinBox - > setValue ( this - > region_map - > width ( ) ) ;
heightSpinBox - > setValue ( this - > region_map - > height ( ) ) ;
form . addRow ( new QLabel ( " Width " ) , widthSpinBox ) ;
form . addRow ( new QLabel ( " Height " ) , heightSpinBox ) ;
QDialogButtonBox buttonBox ( QDialogButtonBox : : Ok | QDialogButtonBox : : Cancel , Qt : : Horizontal , & popup ) ;
form . addRow ( & buttonBox ) ;
2021-02-14 15:07:55 +00:00
connect ( & buttonBox , & QDialogButtonBox : : rejected , & popup , & QDialog : : reject ) ;
connect ( & buttonBox , & QDialogButtonBox : : accepted , & popup , & QDialog : : accept ) ;
2019-01-14 00:27:28 +00:00
if ( popup . exec ( ) = = QDialog : : Accepted ) {
resize ( widthSpinBox - > value ( ) , heightSpinBox - > value ( ) ) ;
2019-01-28 18:47:20 +00:00
RegionMapHistoryItem * commit = new RegionMapHistoryItem (
RegionMapEditorBox : : BackgroundImage , this - > region_map - > getTiles ( ) , widthSpinBox - > value ( ) , heightSpinBox - > value ( )
) ;
history . push ( commit ) ;
2019-01-14 00:27:28 +00:00
}
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = true ;
2019-01-14 00:27:28 +00:00
}
2019-01-09 02:03:54 +00:00
void RegionMapEditor : : on_action_RegionMap_Undo_triggered ( ) {
2019-01-14 00:27:28 +00:00
undo ( ) ;
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = true ;
2019-01-14 00:27:28 +00:00
}
void RegionMapEditor : : undo ( ) {
2019-01-22 20:06:49 +00:00
RegionMapHistoryItem * commit = history . back ( ) ;
2019-01-09 02:03:54 +00:00
if ( ! commit ) return ;
switch ( commit - > which )
{
case RegionMapEditorBox : : BackgroundImage :
2019-01-28 18:47:20 +00:00
if ( commit - > mapWidth ! = this - > region_map - > width ( ) | | commit - > mapHeight ! = this - > region_map - > height ( ) )
this - > resize ( commit - > mapWidth , commit - > mapHeight ) ;
2019-01-22 20:06:49 +00:00
this - > region_map - > setTiles ( commit - > tiles ) ;
2019-01-09 02:03:54 +00:00
this - > region_map_item - > draw ( ) ;
2019-03-25 04:10:57 +00:00
this - > region_map_layout_item - > draw ( ) ;
2019-04-13 02:25:43 +01:00
this - > region_map_entries_item - > draw ( ) ;
2019-01-09 02:03:54 +00:00
break ;
case RegionMapEditorBox : : CityMapImage :
2019-01-22 20:06:49 +00:00
if ( commit - > cityMap = = this - > city_map_item - > file )
this - > city_map_item - > setTiles ( commit - > tiles ) ;
2019-01-09 02:03:54 +00:00
this - > city_map_item - > draw ( ) ;
break ;
}
}
void RegionMapEditor : : on_action_RegionMap_Redo_triggered ( ) {
2019-01-14 00:27:28 +00:00
redo ( ) ;
2019-01-22 20:06:49 +00:00
this - > hasUnsavedChanges = true ;
2019-01-14 00:27:28 +00:00
}
void RegionMapEditor : : redo ( ) {
2019-01-09 02:03:54 +00:00
RegionMapHistoryItem * commit = history . next ( ) ;
if ( ! commit ) return ;
switch ( commit - > which )
{
case RegionMapEditorBox : : BackgroundImage :
2019-01-28 18:47:20 +00:00
if ( commit - > mapWidth ! = this - > region_map - > width ( ) | | commit - > mapHeight ! = this - > region_map - > height ( ) )
this - > resize ( commit - > mapWidth , commit - > mapHeight ) ;
2019-01-22 20:06:49 +00:00
this - > region_map - > setTiles ( commit - > tiles ) ;
2019-01-09 02:03:54 +00:00
this - > region_map_item - > draw ( ) ;
2019-03-25 04:10:57 +00:00
this - > region_map_layout_item - > draw ( ) ;
2019-04-13 02:25:43 +01:00
this - > region_map_entries_item - > draw ( ) ;
2019-01-09 02:03:54 +00:00
break ;
case RegionMapEditorBox : : CityMapImage :
2019-01-22 20:06:49 +00:00
this - > city_map_item - > setTiles ( commit - > tiles ) ;
2019-01-09 02:03:54 +00:00
this - > city_map_item - > draw ( ) ;
break ;
}
}
2019-01-14 00:27:28 +00:00
void RegionMapEditor : : resize ( int w , int h ) {
this - > region_map - > resize ( w , h ) ;
2019-01-28 18:47:20 +00:00
this - > currIndex = this - > region_map - > padLeft * w + this - > region_map - > padTop ;
displayRegionMapImage ( ) ;
displayRegionMapLayout ( ) ;
displayRegionMapLayoutOptions ( ) ;
2019-01-14 00:27:28 +00:00
}
2019-02-16 22:56:44 +00:00
void RegionMapEditor : : on_action_Swap_triggered ( ) {
QDialog popup ( this , Qt : : WindowTitleHint | Qt : : WindowCloseButtonHint ) ;
2019-04-29 23:24:35 +01:00
popup . setWindowTitle ( " Swap Map Sections " ) ;
2019-02-16 22:56:44 +00:00
popup . setWindowModality ( Qt : : NonModal ) ;
QFormLayout form ( & popup ) ;
QComboBox * oldSecBox = new QComboBox ( ) ;
2019-02-25 18:31:34 +00:00
oldSecBox - > addItems ( this - > project - > mapSectionValueToName . values ( ) ) ;
2019-02-16 22:56:44 +00:00
form . addRow ( new QLabel ( " Old Map Section: " ) , oldSecBox ) ;
QComboBox * newSecBox = new QComboBox ( ) ;
2019-02-25 18:31:34 +00:00
newSecBox - > addItems ( this - > project - > mapSectionValueToName . values ( ) ) ;
2019-02-16 22:56:44 +00:00
form . addRow ( new QLabel ( " New Map Section: " ) , newSecBox ) ;
QDialogButtonBox buttonBox ( QDialogButtonBox : : Ok | QDialogButtonBox : : Cancel , Qt : : Horizontal , & popup ) ;
form . addRow ( & buttonBox ) ;
QString beforeSection , afterSection ;
uint8_t oldId , newId ;
2021-02-14 15:07:55 +00:00
connect ( & buttonBox , & QDialogButtonBox : : rejected , & popup , & QDialog : : reject ) ;
2019-02-16 22:56:44 +00:00
connect ( & buttonBox , & QDialogButtonBox : : accepted , [ this , & popup , & oldSecBox , & newSecBox ,
& beforeSection , & afterSection , & oldId , & newId ] ( ) {
beforeSection = oldSecBox - > currentText ( ) ;
afterSection = newSecBox - > currentText ( ) ;
if ( ! beforeSection . isEmpty ( ) & & ! afterSection . isEmpty ( ) ) {
2019-02-25 18:31:34 +00:00
oldId = static_cast < uint8_t > ( this - > project - > mapSectionNameToValue . value ( beforeSection ) ) ;
newId = static_cast < uint8_t > ( this - > project - > mapSectionNameToValue . value ( afterSection ) ) ;
2019-02-16 22:56:44 +00:00
popup . accept ( ) ;
}
} ) ;
if ( popup . exec ( ) = = QDialog : : Accepted ) {
this - > region_map - > replaceSectionId ( oldId , newId ) ;
2019-04-08 00:25:29 +01:00
displayRegionMapLayout ( ) ;
2019-02-16 22:56:44 +00:00
this - > region_map_layout_item - > select ( this - > region_map_layout_item - > selectedTile ) ;
this - > hasUnsavedChanges = true ;
}
}
2019-03-17 16:37:13 +00:00
void RegionMapEditor : : on_action_RegionMap_ClearImage_triggered ( ) {
this - > region_map - > clearImage ( ) ;
RegionMapHistoryItem * commit = new RegionMapHistoryItem (
RegionMapEditorBox : : BackgroundImage , this - > region_map - > getTiles ( ) , this - > region_map - > width ( ) , this - > region_map - > height ( )
2019-02-25 18:31:34 +00:00
) ;
2019-03-17 16:37:13 +00:00
history . push ( commit ) ;
2019-02-25 18:31:34 +00:00
2019-03-17 16:37:13 +00:00
displayRegionMapImage ( ) ;
displayRegionMapLayout ( ) ;
2019-02-25 18:31:34 +00:00
}
2019-03-17 16:37:13 +00:00
void RegionMapEditor : : on_action_RegionMap_ClearLayout_triggered ( ) {
2019-02-25 18:31:34 +00:00
QMessageBox : : StandardButton result = QMessageBox : : question (
this ,
" WARNING " ,
" This action will reset the entire map layout to MAPSEC_NONE, continue? " ,
QMessageBox : : Yes | QMessageBox : : Cancel ,
QMessageBox : : Yes
) ;
if ( result = = QMessageBox : : Yes ) {
2019-03-17 16:37:13 +00:00
this - > region_map - > clearLayout ( ) ;
2019-02-25 18:31:34 +00:00
displayRegionMapLayout ( ) ;
} else {
return ;
}
}
2019-03-25 04:10:57 +00:00
void RegionMapEditor : : on_action_Import_RegionMap_ImageTiles_triggered ( ) {
importTileImage ( false ) ;
}
void RegionMapEditor : : on_action_Import_CityMap_ImageTiles_triggered ( ) {
importTileImage ( true ) ;
}
void RegionMapEditor : : importTileImage ( bool city ) {
QString descriptor = city ? " City Map " : " Region Map " ;
QString infilepath = QFileDialog : : getOpenFileName ( this , QString ( " Import %1 Tiles Image " ) . arg ( descriptor ) ,
this - > project - > root , " Image Files (*.png *.bmp *.jpg *.dib) " ) ;
if ( infilepath . isEmpty ( ) ) {
return ;
}
logInfo ( QString ( " Importing %1 Tiles from '%2' " ) . arg ( descriptor ) . arg ( infilepath ) ) ;
// Read image data from buffer so that the built-in QImage doesn't try to detect file format
// purely from the extension name.
QFile file ( infilepath ) ;
QImage image ;
if ( file . open ( QIODevice : : ReadOnly ) ) {
QByteArray imageData = file . readAll ( ) ;
image = QImage : : fromData ( imageData ) ;
} else {
QString errorMessage = QString ( " Failed to open image file: '%1' " ) . arg ( infilepath ) ;
logError ( errorMessage ) ;
QMessageBox msgBox ( this ) ;
msgBox . setText ( " Failed to import tiles. " ) ;
msgBox . setInformativeText ( errorMessage ) ;
msgBox . setDefaultButton ( QMessageBox : : Ok ) ;
msgBox . setIcon ( QMessageBox : : Icon : : Critical ) ;
msgBox . exec ( ) ;
return ;
}
if ( image . width ( ) = = 0 | | image . height ( ) = = 0 | | image . width ( ) % 8 ! = 0 | | image . height ( ) % 8 ! = 0 ) {
QString errorMessage = QString ( " The image dimensions (%1 x %2) are invalid. Width and height must be multiples of 8 pixels. " )
. arg ( image . width ( ) )
. arg ( image . height ( ) ) ;
logError ( errorMessage ) ;
QMessageBox msgBox ( this ) ;
msgBox . setText ( " Failed to import tiles. " ) ;
msgBox . setInformativeText ( errorMessage ) ;
msgBox . setDefaultButton ( QMessageBox : : Ok ) ;
msgBox . setIcon ( QMessageBox : : Icon : : Critical ) ;
msgBox . exec ( ) ;
return ;
}
// Validate total number of tiles in image.
int numTilesWide = image . width ( ) / 8 ;
int numTilesHigh = image . height ( ) / 8 ;
int totalTiles = numTilesHigh * numTilesWide ;
int maxAllowedTiles = 0x100 ;
if ( totalTiles > maxAllowedTiles ) {
QString errorMessage = QString ( " The total number of tiles in the provided image (%1) is greater than the allowed number (%2). " )
2019-09-09 01:26:39 +01:00
. arg ( totalTiles )
. arg ( maxAllowedTiles ) ;
2019-03-25 04:10:57 +00:00
logError ( errorMessage ) ;
QMessageBox msgBox ( this ) ;
msgBox . setText ( " Failed to import tiles. " ) ;
msgBox . setInformativeText ( errorMessage ) ;
msgBox . setDefaultButton ( QMessageBox : : Ok ) ;
msgBox . setIcon ( QMessageBox : : Icon : : Critical ) ;
msgBox . exec ( ) ;
return ;
}
// Validate the image's palette.
QString palMessage = QString ( ) ;
bool palError = false ;
if ( image . colorCount ( ) = = 0 ) {
palMessage = QString ( " The provided image is not indexed. " ) ;
palError = true ;
} else if ( ! city & & image . colorCount ( ) ! = 256 ) {
palMessage = QString ( " The provided image has a palette with %1 colors. You must provide an indexed imaged with a 256 color palette. " ) . arg ( image . colorCount ( ) ) ;
palError = true ;
} else if ( city & & image . colorCount ( ) ! = 16 ) {
palMessage = QString ( " The provided image has a palette with %1 colors. You must provide an indexed imaged with a 16 color palette. " ) . arg ( image . colorCount ( ) ) ;
palError = true ;
}
if ( palError ) {
logError ( palMessage ) ;
QMessageBox msgBox ( this ) ;
msgBox . setText ( " Failed to import tiles. " ) ;
msgBox . setInformativeText ( palMessage ) ;
msgBox . setDefaultButton ( QMessageBox : : Ok ) ;
msgBox . setIcon ( QMessageBox : : Icon : : Critical ) ;
msgBox . exec ( ) ;
return ;
}
// Use the image from the correct path.
if ( city ) {
this - > region_map - > setTemporaryCityTilesPath ( infilepath ) ;
} else {
this - > region_map - > setTemporaryPngPath ( infilepath ) ;
}
this - > hasUnsavedChanges = true ;
// Redload and redraw images.
displayRegionMap ( ) ;
displayCityMap ( this - > ui - > comboBox_CityMap_picker - > currentText ( ) ) ;
}
2019-01-07 19:46:27 +00:00
void RegionMapEditor : : on_comboBox_CityMap_picker_currentTextChanged ( const QString & file ) {
this - > displayCityMap ( file ) ;
2019-01-28 18:47:20 +00:00
this - > cityMapFirstDraw = true ;
2019-01-07 19:46:27 +00:00
}
2019-01-22 20:06:49 +00:00
void RegionMapEditor : : closeEvent ( QCloseEvent * event )
{
if ( this - > hasUnsavedChanges ) {
QMessageBox : : StandardButton result = QMessageBox : : question (
this ,
" porymap " ,
" The region map has been modified, save changes? " ,
QMessageBox : : No | QMessageBox : : Yes | QMessageBox : : Cancel ,
QMessageBox : : Yes ) ;
if ( result = = QMessageBox : : Yes ) {
this - > on_action_RegionMap_Save_triggered ( ) ;
event - > accept ( ) ;
} else if ( result = = QMessageBox : : No ) {
event - > accept ( ) ;
} else if ( result = = QMessageBox : : Cancel ) {
event - > ignore ( ) ;
}
} else {
event - > accept ( ) ;
}
2020-09-16 00:03:15 +01:00
porymapConfig . setRegionMapEditorGeometry (
this - > saveGeometry ( ) ,
this - > saveState ( )
) ;
2019-01-09 02:03:54 +00:00
}
2019-01-07 19:46:27 +00:00
2019-01-22 20:06:49 +00:00
void RegionMapEditor : : on_verticalSlider_Zoom_Map_Image_valueChanged ( int val ) {
2019-02-16 22:56:44 +00:00
double scale = pow ( scaleUpFactor , static_cast < double > ( val - initialScale ) / initialScale ) ;
2019-01-07 19:46:27 +00:00
2020-08-27 01:42:42 +01:00
QTransform transform ;
transform . scale ( scale , scale ) ;
2019-02-16 22:56:44 +00:00
int width = ceil ( static_cast < double > ( this - > region_map - > imgSize ( ) . width ( ) ) * scale ) ;
int height = ceil ( static_cast < double > ( this - > region_map - > imgSize ( ) . height ( ) ) * scale ) ;
2019-01-07 19:46:27 +00:00
2019-02-16 22:56:44 +00:00
ui - > graphicsView_Region_Map_BkgImg - > setResizeAnchor ( QGraphicsView : : NoAnchor ) ;
2020-08-27 01:42:42 +01:00
ui - > graphicsView_Region_Map_BkgImg - > setTransform ( transform ) ;
2019-02-16 22:56:44 +00:00
ui - > graphicsView_Region_Map_BkgImg - > setFixedSize ( width + 2 , height + 2 ) ;
ui - > graphicsView_Region_Map_Layout - > setResizeAnchor ( QGraphicsView : : NoAnchor ) ;
2020-08-27 01:42:42 +01:00
ui - > graphicsView_Region_Map_Layout - > setTransform ( transform ) ;
2019-02-16 22:56:44 +00:00
ui - > graphicsView_Region_Map_Layout - > setFixedSize ( width + 2 , height + 2 ) ;
2019-04-13 02:25:43 +01:00
ui - > graphicsView_Region_Map_Entries - > setResizeAnchor ( QGraphicsView : : NoAnchor ) ;
2020-08-27 01:42:42 +01:00
ui - > graphicsView_Region_Map_Entries - > setTransform ( transform ) ;
2019-04-13 02:25:43 +01:00
ui - > graphicsView_Region_Map_Entries - > setFixedSize ( width + 2 , height + 2 ) ;
2019-01-09 02:03:54 +00:00
}
2019-01-07 19:46:27 +00:00
2019-01-22 20:06:49 +00:00
void RegionMapEditor : : on_verticalSlider_Zoom_Image_Tiles_valueChanged ( int val ) {
2019-02-16 22:56:44 +00:00
double scale = pow ( scaleUpFactor , static_cast < double > ( val - initialScale ) / initialScale ) ;
2020-08-27 01:42:42 +01:00
QTransform transform ;
transform . scale ( scale , scale ) ;
2019-02-16 22:56:44 +00:00
int width = ceil ( static_cast < double > ( this - > mapsquare_selector_item - > pixelWidth ) * scale ) ;
int height = ceil ( static_cast < double > ( this - > mapsquare_selector_item - > pixelHeight ) * scale ) ;
ui - > graphicsView_RegionMap_Tiles - > setResizeAnchor ( QGraphicsView : : NoAnchor ) ;
2020-08-27 01:42:42 +01:00
ui - > graphicsView_RegionMap_Tiles - > setTransform ( transform ) ;
2019-02-16 22:56:44 +00:00
ui - > graphicsView_RegionMap_Tiles - > setFixedSize ( width + 2 , height + 2 ) ;
2019-01-09 02:03:54 +00:00
}
2019-01-07 19:46:27 +00:00
2019-01-22 20:06:49 +00:00
void RegionMapEditor : : on_verticalSlider_Zoom_City_Map_valueChanged ( int val ) {
2019-02-16 22:56:44 +00:00
double scale = pow ( scaleUpFactor , static_cast < double > ( val - initialScale ) / initialScale ) ;
2019-01-22 20:06:49 +00:00
2020-08-27 01:42:42 +01:00
QTransform transform ;
transform . scale ( scale , scale ) ;
2019-02-16 22:56:44 +00:00
int width = ceil ( static_cast < double > ( 8 * city_map_item - > width ( ) ) * scale ) ;
int height = ceil ( static_cast < double > ( 8 * city_map_item - > height ( ) ) * scale ) ;
2019-01-22 20:06:49 +00:00
2019-02-16 22:56:44 +00:00
ui - > graphicsView_City_Map - > setResizeAnchor ( QGraphicsView : : NoAnchor ) ;
2020-08-27 01:42:42 +01:00
ui - > graphicsView_City_Map - > setTransform ( transform ) ;
2019-02-16 22:56:44 +00:00
ui - > graphicsView_City_Map - > setFixedSize ( width + 2 , height + 2 ) ;
2019-01-09 02:03:54 +00:00
}
2019-01-07 19:46:27 +00:00
2019-01-22 20:06:49 +00:00
void RegionMapEditor : : on_verticalSlider_Zoom_City_Tiles_valueChanged ( int val ) {
2019-02-16 22:56:44 +00:00
double scale = pow ( scaleUpFactor , static_cast < double > ( val - initialScale ) / initialScale ) ;
2019-01-22 20:06:49 +00:00
2020-08-27 01:42:42 +01:00
QTransform transform ;
transform . scale ( scale , scale ) ;
2019-02-16 22:56:44 +00:00
int width = ceil ( static_cast < double > ( this - > city_map_selector_item - > pixelWidth ) * scale ) ;
int height = ceil ( static_cast < double > ( this - > city_map_selector_item - > pixelHeight ) * scale ) ;
2019-01-22 20:06:49 +00:00
2019-02-16 22:56:44 +00:00
ui - > graphicsView_City_Map_Tiles - > setResizeAnchor ( QGraphicsView : : NoAnchor ) ;
2020-08-27 01:42:42 +01:00
ui - > graphicsView_City_Map_Tiles - > setTransform ( transform ) ;
2019-02-16 22:56:44 +00:00
ui - > graphicsView_City_Map_Tiles - > setFixedSize ( width + 2 , height + 2 ) ;
2019-01-09 02:03:54 +00:00
}