Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add historical changes support for the Editor #7779

Merged
merged 16 commits into from
Sep 22, 2023
2 changes: 2 additions & 0 deletions VisualStudio/fheroes2/sources.props
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<ClCompile Include="src\fheroes2\editor\editor_interface.cpp" />
<ClCompile Include="src\fheroes2\editor\editor_interface_panel.cpp" />
<ClCompile Include="src\fheroes2\editor\editor_mainmenu.cpp" />
<ClCompile Include="src\fheroes2\editor\history_manager.cpp" />
<ClCompile Include="src\fheroes2\game\difficulty.cpp" />
<ClCompile Include="src\fheroes2\game\fheroes2.cpp" />
<ClCompile Include="src\fheroes2\game\game.cpp" />
Expand Down Expand Up @@ -309,6 +310,7 @@
<ClInclude Include="src\fheroes2\editor\editor.h" />
<ClInclude Include="src\fheroes2\editor\editor_interface.h" />
<ClInclude Include="src\fheroes2\editor\editor_interface_panel.h" />
<ClInclude Include="src\fheroes2\editor\history_manager.h" />
<ClInclude Include="src\fheroes2\game\difficulty.h" />
<ClInclude Include="src\fheroes2\game\game.h" />
<ClInclude Include="src\fheroes2\game\game_credits.h" />
Expand Down
45 changes: 30 additions & 15 deletions src/fheroes2/editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "game_hotkeys.h"
#include "gamedefs.h"
#include "heroes.h"
#include "history_manager.h"
#include "icn.h"
#include "image.h"
#include "interface_base.h"
Expand Down Expand Up @@ -72,13 +73,13 @@ namespace

namespace Interface
{
Interface::Editor::Editor()
Interface::EditorInterface::EditorInterface()
: _editorPanel( *this )
{
Editor::reset();
// Do nothing.
}

void Editor::reset()
void EditorInterface::reset()
{
const fheroes2::Display & display = fheroes2::Display::instance();

Expand All @@ -102,9 +103,11 @@ namespace Interface

_gameArea.SetCenterInPixels( prevCenter + fheroes2::Point( newRoi.x + newRoi.width / 2, newRoi.y + newRoi.height / 2 )
- fheroes2::Point( prevRoi.x + prevRoi.width / 2, prevRoi.y + prevRoi.height / 2 ) );

_historyManager.reset();
}

void Editor::redraw( const uint32_t force )
void EditorInterface::redraw( const uint32_t force )
{
fheroes2::Display & display = fheroes2::Display::instance();

Expand Down Expand Up @@ -153,13 +156,13 @@ namespace Interface
_redraw = 0;
}

Interface::Editor & Interface::Editor::Get()
Interface::EditorInterface & Interface::EditorInterface::Get()
{
static Editor editorInterface;
static EditorInterface editorInterface;
return editorInterface;
}

fheroes2::GameMode Interface::Editor::startEdit()
fheroes2::GameMode Interface::EditorInterface::startEdit()
{
reset();

Expand Down Expand Up @@ -246,6 +249,15 @@ namespace Interface
fheroes2::showStandardTextMessage( _( "Warning!" ), "The Map Editor is still in development. Open focused object dialog is not implemented yet.",
Dialog::OK );
}
// TODO: remove this macro check once the Editor is ready for public.
#if defined( WITH_DEBUG )
else if ( HotKeyPressEvent( Game::HotKeyEvent::EDITOR_UNDO_LAST_ACTION ) ) {
undoAction();
}
else if ( HotKeyPressEvent( Game::HotKeyEvent::EDITOR_REDO_LAST_ACTION ) ) {
redoAction();
}
#endif
}

if ( res != fheroes2::GameMode::CANCEL ) {
Expand Down Expand Up @@ -350,6 +362,8 @@ namespace Interface
if ( isCursorOverGamearea && _editorPanel.getBrushSize() == 0 ) {
const int groundId = _editorPanel.selectedGroundType();

const fheroes2::ActionCreator action( _historyManager );

Maps::setTerrainOnTiles( _selectedTile, _tileUnderCursor, groundId );
}

Expand Down Expand Up @@ -397,7 +411,7 @@ namespace Interface
return res;
}

fheroes2::GameMode Editor::eventLoadMap()
fheroes2::GameMode EditorInterface::eventLoadMap()
{
return Dialog::YES
== fheroes2::showStandardTextMessage( "", _( "Are you sure you want to load a new map? (Any unsaved changes to the current map will be lost.)" ),
Expand All @@ -406,7 +420,7 @@ namespace Interface
: fheroes2::GameMode::CANCEL;
}

fheroes2::GameMode Editor::eventNewMap()
fheroes2::GameMode EditorInterface::eventNewMap()
{
return Dialog::YES
== fheroes2::showStandardTextMessage( "", _( "Are you sure you want to create a new map? (Any unsaved changes to the current map will be lost.)" ),
Expand All @@ -415,7 +429,7 @@ namespace Interface
: fheroes2::GameMode::CANCEL;
}

fheroes2::GameMode Editor::eventFileDialog()
fheroes2::GameMode EditorInterface::eventFileDialog()
{
const bool isEvilInterface = Settings::Get().isEvilInterfaceEnabled();
const int cpanbkg = isEvilInterface ? ICN::CPANBKGE : ICN::CPANBKG;
Expand Down Expand Up @@ -445,7 +459,7 @@ namespace Interface

display.render( back.rect() );

fheroes2::GameMode result = fheroes2::GameMode::QUIT_GAME;
fheroes2::GameMode result = fheroes2::GameMode::CANCEL;

LocalEvent & le = LocalEvent::Get();

Expand Down Expand Up @@ -487,7 +501,6 @@ namespace Interface
}
}
else if ( le.MouseClickLeft( buttonCancel.area() ) || Game::HotKeyCloseWindow() ) {
result = fheroes2::GameMode::CANCEL;
break;
}
else if ( le.MousePressRight( buttonNew.area() ) ) {
Expand All @@ -514,13 +527,13 @@ namespace Interface
return result;
}

void Editor::eventViewWorld()
void EditorInterface::eventViewWorld()
{
// TODO: Make proper borders restoration for low height resolutions, like for hide interface mode.
ViewWorld::ViewWorldWindow( 0, ViewWorldMode::ViewAll, *this );
}

void Editor::mouseCursorAreaClickLeft( const int32_t tileIndex )
void EditorInterface::mouseCursorAreaClickLeft( const int32_t tileIndex )
{
const Maps::Tiles & tile = world.GetTiles( tileIndex );

Expand All @@ -539,6 +552,8 @@ namespace Interface
const int32_t brushSize = _editorPanel.getBrushSize();
const int groundId = _editorPanel.selectedGroundType();

const fheroes2::ActionCreator action( _historyManager );

if ( brushSize > 0 ) {
Maps::setTerrainOnTiles( tileIndex, getBrushAreaEndIndex( brushSize, tileIndex ), groundId );
}
Expand All @@ -552,7 +567,7 @@ namespace Interface
}
}

void Editor::mouseCursorAreaPressRight( const int32_t tileIndex ) const
void EditorInterface::mouseCursorAreaPressRight( const int32_t tileIndex ) const
{
const Maps::Tiles & tile = world.GetTiles( tileIndex );

Expand Down
23 changes: 20 additions & 3 deletions src/fheroes2/editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@

#include "editor_interface_panel.h"
#include "game_mode.h"
#include "history_manager.h"
#include "interface_base.h"

namespace Interface
{
class Editor final : public BaseInterface
class EditorInterface final : public BaseInterface
{
public:
static Editor & Get();
static EditorInterface & Get();

void redraw( const uint32_t force ) override;

Expand All @@ -54,12 +55,28 @@ namespace Interface
void mouseCursorAreaClickLeft( const int32_t tileIndex ) override;
void mouseCursorAreaPressRight( const int32_t tileIndex ) const override;

void undoAction()
{
if ( _historyManager.undo() ) {
_redraw |= ( REDRAW_GAMEAREA | REDRAW_RADAR );
}
}

void redoAction()
{
if ( _historyManager.redo() ) {
_redraw |= ( REDRAW_GAMEAREA | REDRAW_RADAR );
}
}

private:
Editor();
EditorInterface();

EditorPanel _editorPanel;

int32_t _selectedTile{ -1 };
int32_t _tileUnderCursor{ -1 };

fheroes2::HistoryManager _historyManager;
};
}
13 changes: 7 additions & 6 deletions src/fheroes2/editor/editor_interface_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

namespace Interface
{
EditorPanel::EditorPanel( Editor & interface_ )
EditorPanel::EditorPanel( EditorInterface & interface_ )
: _interface( interface_ )
{
int32_t icnIndex = 0;
Expand Down Expand Up @@ -439,18 +439,19 @@ namespace Interface
if ( le.MouseClickLeft( _rectMagnify ) ) {
_interface.eventViewWorld();
}
else if ( le.MouseClickLeft( _rectUndo ) ) {
fheroes2::showStandardTextMessage( _( "Warning!" ), "The Map Editor is still in development. This function is not available yet.", Dialog::OK );
else if ( _buttonUndo.isEnabled() && le.MouseClickLeft( _rectUndo ) ) {
_interface.undoAction();
return fheroes2::GameMode::CANCEL;
}
else if ( le.MouseClickLeft( _rectNew ) ) {
res = Editor::eventNewMap();
res = EditorInterface::eventNewMap();
}
else if ( le.MouseClickLeft( _rectSpecs ) ) {
// TODO: Make the scenario info editor.
Dialog::GameInfo();
}
else if ( le.MouseClickLeft( _rectFile ) ) {
res = Interface::Editor::eventFileDialog();
res = Interface::EditorInterface::eventFileDialog();
}
else if ( le.MouseClickLeft( _rectSystem ) ) {
// Replace this with Editor options dialog.
Expand Down Expand Up @@ -478,7 +479,7 @@ namespace Interface
fheroes2::showStandardTextMessage( _( "Magnify" ), _( "Change between zoom and normal view." ), Dialog::ZERO );
}
else if ( le.MousePressRight( _rectUndo ) ) {
fheroes2::showStandardTextMessage( _( "Undo" ), _( "Undo your last action. Press again to redo the action." ), Dialog::ZERO );
fheroes2::showStandardTextMessage( _( "Undo" ), _( "Undo your last action." ), Dialog::ZERO );
}
else if ( le.MousePressRight( _rectNew ) ) {
fheroes2::showStandardTextMessage( _( "New Map" ), _( "Create a new map either from scratch or using the random map generator." ), Dialog::ZERO );
Expand Down
8 changes: 4 additions & 4 deletions src/fheroes2/editor/editor_interface_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

namespace Interface
{
class Editor;
class EditorInterface;

class EditorPanel
{
public:
explicit EditorPanel( Editor & interface_ );
explicit EditorPanel( EditorInterface & interface_ );

~EditorPanel() = default;

Expand Down Expand Up @@ -74,8 +74,6 @@ namespace Interface
void _redraw() const;

private:
Editor & _interface;

static int _getGroundId( const uint8_t brushId );

static const char * _getTerrainTypeName( const uint8_t brushId )
Expand Down Expand Up @@ -138,6 +136,8 @@ namespace Interface
BRUSH_SIZE_COUNT = 4U
};

EditorInterface & _interface;

fheroes2::Button _buttonMagnify;
fheroes2::Button _buttonUndo;
fheroes2::Button _buttonNew;
Expand Down
2 changes: 1 addition & 1 deletion src/fheroes2/editor/editor_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace Editor
fheroes2::fadeOutDisplay();
Game::setDisplayFadeIn();

return Interface::Editor::Get().startEdit();
return Interface::EditorInterface::Get().startEdit();
}
return fheroes2::GameMode::EDITOR_NEW_MAP;
}
Expand Down
Loading