Skip to content

Commit

Permalink
Add functionality to place monsters in the Editor (#7852)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihhub authored Oct 5, 2023
1 parent c1334f8 commit 28a3ae3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
24 changes: 22 additions & 2 deletions src/fheroes2/editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "maps_tiles.h"
#include "maps_tiles_helper.h"
#include "math_base.h"
#include "monster.h"
#include "mp2.h"
#include "screen.h"
#include "settings.h"
Expand Down Expand Up @@ -596,6 +597,20 @@ namespace Interface
}
}
}
else if ( _editorPanel.isMonsterSettingMode() ) {
if ( tile.isWater() ) {
fheroes2::showStandardTextMessage( _( "Monster" ), _( "Monsters cannot be placed on water." ), Dialog::OK );
}
else if ( !Maps::isClearGround( tile ) ) {
fheroes2::showStandardTextMessage( _( "Monster" ), _( "Choose a tile which does not contain any objects." ), Dialog::OK );
}
else if ( Monster{ _editorPanel.getMonsterId() }.isValid() ) {
const fheroes2::ActionCreator action( _historyManager );

Maps::setMonsterOnTile( tile, _editorPanel.getMonsterId(), 0 );
_redraw |= mapUpdateFlags;
}
}
}

void EditorInterface::mouseCursorAreaPressRight( const int32_t tileIndex ) const
Expand Down Expand Up @@ -633,8 +648,13 @@ namespace Interface
}
}

void EditorInterface::updateCursor( const int32_t /*tileIndex*/ )
void EditorInterface::updateCursor( const int32_t tileIndex )
{
Cursor::Get().SetThemes( Cursor::POINTER );
if ( _cursorUpdater && tileIndex >= 0 ) {
_cursorUpdater( tileIndex );
}
else {
Cursor::Get().SetThemes( Cursor::POINTER );
}
}
}
8 changes: 8 additions & 0 deletions src/fheroes2/editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include <cstdint>
#include <functional>

#include "editor_interface_panel.h"
#include "game_mode.h"
Expand Down Expand Up @@ -71,6 +72,11 @@ namespace Interface

void updateCursor( const int32_t tileIndex ) override;

void setCursorUpdater( const std::function<void( const int32_t )> & cursorUpdater )
{
_cursorUpdater = cursorUpdater;
}

private:
EditorInterface()
: _editorPanel( *this )
Expand All @@ -83,6 +89,8 @@ namespace Interface
int32_t _selectedTile{ -1 };
int32_t _tileUnderCursor{ -1 };

std::function<void( const int32_t )> _cursorUpdater;

fheroes2::HistoryManager _historyManager;
};
}
29 changes: 28 additions & 1 deletion src/fheroes2/editor/editor_interface_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <string>

#include "agg_image.h"
#include "cursor.h"
#include "dialog.h"
#include "dialog_selectitems.h"
#include "dialog_system_options.h"
#include "editor_interface.h"
#include "ground.h"
Expand Down Expand Up @@ -74,7 +76,7 @@ namespace Interface
int32_t EditorPanel::getBrushSize() const
{
// Roads and streams are placed using only 1x1 brush.
if ( _selectedInstrument == Instrument::STREAM || _selectedInstrument == Instrument::ROAD ) {
if ( _selectedInstrument == Instrument::STREAM || _selectedInstrument == Instrument::ROAD || isMonsterSettingMode() ) {
return 1;
}

Expand Down Expand Up @@ -301,6 +303,10 @@ namespace Interface
if ( le.MousePressLeft( _instrumentButtonsRect[i] ) ) {
if ( _instrumentButtons[i].drawOnPress() ) {
_selectedInstrument = static_cast<uint8_t>( i );

// Reset cursor updater since this UI element was clicked.
_interface.setCursorUpdater( {} );

setRedraw();
}
}
Expand Down Expand Up @@ -399,6 +405,10 @@ namespace Interface
for ( size_t i = 0; i < _objectButtonsRect.size(); ++i ) {
if ( ( _selectedObject != i ) && le.MousePressLeft( _objectButtonsRect[i] ) ) {
_selectedObject = static_cast<uint8_t>( i );

// Reset cursor updater since this UI element was clicked.
_interface.setCursorUpdater( {} );

setRedraw();

// There is no need to continue the loop as only one button can be pressed at one moment.
Expand Down Expand Up @@ -432,6 +442,23 @@ namespace Interface
else if ( le.MousePressRight( _objectButtonsRect[Brush::TREASURES] ) ) {
fheroes2::showStandardTextMessage( _getObjectTypeName( Brush::TREASURES ), _( "Used to place\na resource or treasure." ), Dialog::ZERO );
}
else if ( le.MouseClickLeft( _objectButtonsRect[Brush::MONSTERS] ) ) {
_monsterId = Monster::UNKNOWN;

const Monster monster = Dialog::selectMonster();
if ( monster.GetID() != Monster::UNKNOWN ) {
_monsterId = monster.GetID();

_interface.setCursorUpdater( [monster = monster]( const int32_t /*tileIndex*/ ) {
const fheroes2::Sprite & image = fheroes2::AGG::GetICN( ICN::MONS32, monster.GetSpriteIndex() );

Cursor::Get().setCustomImage( image, { -image.width() / 2, -image.height() / 2 } );
} );

_interface.updateCursor( 0 );
return res;
}
}
}

le.MousePressLeft( _rectMagnify ) ? _buttonMagnify.drawOnPress() : _buttonMagnify.drawOnRelease();
Expand Down
15 changes: 14 additions & 1 deletion src/fheroes2/editor/editor_interface_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "game_mode.h"
#include "ground.h"
#include "math_base.h"
#include "monster.h"
#include "ui_button.h"

namespace Interface
Expand Down Expand Up @@ -71,10 +72,20 @@ namespace Interface
return _selectedInstrument == Instrument::ERASE;
}

bool isMonsterSettingMode() const
{
return ( _selectedInstrument == OBJECT ) && ( _selectedObject == MONSTERS );
}

int32_t getMonsterId() const
{
return _monsterId;
}

bool showAreaSelectRect() const
{
return _selectedInstrument == Instrument::TERRAIN || _selectedInstrument == Instrument::STREAM || _selectedInstrument == Instrument::ROAD
|| _selectedInstrument == Instrument::ERASE;
|| _selectedInstrument == Instrument::ERASE || isMonsterSettingMode();
}

bool useMouseDragMovement() const
Expand Down Expand Up @@ -189,5 +200,7 @@ namespace Interface
uint8_t _selectedTerrain{ Brush::WATER };
uint8_t _selectedObject{ Brush::WATER };
uint8_t _selectedBrushSize{ BrushSize::MEDIUM };

int32_t _monsterId{ Monster::UNKNOWN };
};
}
2 changes: 1 addition & 1 deletion src/fheroes2/gui/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void Cursor::setCustomImage( const fheroes2::Image & image, const fheroes2::Poin
{
theme = NONE;

fheroes2::cursor().update( image, 0, 0 );
fheroes2::cursor().update( image, -offset.x, -offset.y );

// Immediately apply new mouse offset.
const fheroes2::Point & currentPos = LocalEvent::Get().GetMouseCursor();
Expand Down
11 changes: 7 additions & 4 deletions src/fheroes2/maps/maps_tiles_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2560,10 +2560,13 @@ namespace Maps
{
tile.SetObject( MP2::OBJ_MONSTER );

// If there was another object sprite here (shadow for example) push it down to Addons,
// except when there is already MONS32.ICN here.
if ( tile.getObjectIcnType() != MP2::OBJ_ICN_TYPE_UNKNOWN && tile.getObjectIcnType() != MP2::OBJ_ICN_TYPE_MONS32 && tile.GetObjectSpriteIndex() != 255 ) {
// Push object sprite to Level 1 Addons preserving the Layer Type.
if ( tile.getObjectIcnType() == MP2::OBJ_ICN_TYPE_UNKNOWN ) {
// No object exists on this tile. Add one.
tile.setObjectUID( getNewObjectUID() );
tile.setObjectIcnType( MP2::OBJ_ICN_TYPE_MONS32 );
}
else if ( tile.getObjectIcnType() != MP2::OBJ_ICN_TYPE_MONS32 ) {
// If there is another object sprite here (shadow for example) push it down to add-ons.
tile.pushBottomLayerAddon( TilesAddon( tile.getLayerType(), tile.GetObjectUID(), tile.getObjectIcnType(), tile.GetObjectSpriteIndex() ) );

// Set unique UID for placed monster.
Expand Down

0 comments on commit 28a3ae3

Please sign in to comment.