Skip to content

Commit

Permalink
Refactor & streamline the event handling logic for adventure map and …
Browse files Browse the repository at this point in the history
…map editor UI elements (#9178)
  • Loading branch information
oleg-derevenetz authored Oct 13, 2024
1 parent 48901a9 commit ff72bb7
Show file tree
Hide file tree
Showing 27 changed files with 735 additions and 619 deletions.
2 changes: 1 addition & 1 deletion src/fheroes2/ai/ai_planner_hero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2738,7 +2738,7 @@ bool AI::Planner::HeroesTurn( VecHeroes & heroes, uint32_t & currentProgressValu
addHeroToMove( hero, availableHeroes );
}

Interface::StatusWindow & status = Interface::AdventureMap::Get().getStatusWindow();
Interface::StatusPanel & status = Interface::AdventureMap::Get().getStatusPanel();

uint32_t heroesToMoveTotalCount = static_cast<uint32_t>( availableHeroes.size() );
uint32_t startProgressValue = currentProgressValue;
Expand Down
2 changes: 1 addition & 1 deletion src/fheroes2/ai/ai_planner_kingdom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ void AI::Planner::KingdomTurn( Kingdom & kingdom )
}

// Reset the turn progress indicator
Interface::StatusWindow & status = Interface::AdventureMap::Get().getStatusWindow();
Interface::StatusPanel & status = Interface::AdventureMap::Get().getStatusPanel();
status.drawAITurnProgress( 0 );

AudioManager::PlayMusicAsync( MUS::COMPUTER_TURN, Music::PlaybackMode::RESUME_AND_PLAY_INFINITE );
Expand Down
3 changes: 3 additions & 0 deletions src/fheroes2/dialog/dialog_graphics_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <utility>

#include "agg_image.h"
#include "cursor.h"
#include "dialog_resolution.h"
#include "game_hotkeys.h"
#include "icn.h"
Expand Down Expand Up @@ -224,6 +225,8 @@ namespace fheroes2
{
bool openGraphicsSettingsDialog( const std::function<void()> & updateUI )
{
const CursorRestorer cursorRestorer( true, ::Cursor::POINTER );

Settings & conf = Settings::Get();

bool saveConfiguration = false;
Expand Down
3 changes: 3 additions & 0 deletions src/fheroes2/dialog/dialog_interface_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <utility>

#include "agg_image.h"
#include "cursor.h"
#include "game_hotkeys.h"
#include "icn.h"
#include "image.h"
Expand Down Expand Up @@ -277,6 +278,8 @@ namespace fheroes2
{
bool openInterfaceSettingsDialog( const std::function<void()> & updateUI )
{
const CursorRestorer cursorRestorer( true, ::Cursor::POINTER );

Settings & conf = Settings::Get();

bool saveConfiguration = false;
Expand Down
181 changes: 104 additions & 77 deletions src/fheroes2/editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,6 @@ namespace Interface
int32_t fastScrollRepeatCount = 0;
const int32_t fastScrollStartThreshold = 2;

bool isCursorOverGamearea = false;

const std::vector<Game::DelayType> delayTypes = { Game::MAPS_DELAY };

LocalEvent & le = LocalEvent::Get();
Expand All @@ -758,14 +756,15 @@ namespace Interface
if ( !le.HandleEvents( Game::isDelayNeeded( delayTypes ), true ) ) {
if ( EventExit() == fheroes2::GameMode::QUIT_GAME ) {
res = fheroes2::GameMode::QUIT_GAME;

break;
}

continue;
}

// Process hot-keys.
// Hotkeys
if ( le.isAnyKeyPressed() ) {
// adventure map control
if ( HotKeyPressEvent( Game::HotKeyEvent::MAIN_MENU_QUIT ) || HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_CANCEL ) ) {
res = EventExit();
}
Expand All @@ -787,7 +786,6 @@ namespace Interface
else if ( HotKeyPressEvent( Game::HotKeyEvent::WORLD_VIEW_WORLD ) ) {
eventViewWorld();
}
// map scrolling control
else if ( HotKeyPressEvent( Game::HotKeyEvent::WORLD_SCROLL_LEFT ) ) {
_gameArea.SetScroll( SCROLL_LEFT );
}
Expand Down Expand Up @@ -829,75 +827,98 @@ namespace Interface
break;
}

if ( fheroes2::cursor().isFocusActive() && !_gameArea.isDragScroll() && !_radar.isDragRadar() && ( conf.ScrollSpeed() != SCROLL_SPEED_NONE ) ) {
int scrollPosition = SCROLL_NONE;
bool isCursorOverGameArea = false;

if ( isScrollLeft( le.getMouseCursorPos() ) )
scrollPosition |= SCROLL_LEFT;
else if ( isScrollRight( le.getMouseCursorPos() ) )
scrollPosition |= SCROLL_RIGHT;
if ( isScrollTop( le.getMouseCursorPos() ) )
scrollPosition |= SCROLL_TOP;
else if ( isScrollBottom( le.getMouseCursorPos() ) )
scrollPosition |= SCROLL_BOTTOM;
// Mouse is captured by radar
if ( _radar.isMouseCaptured() ) {
cursor.SetThemes( Cursor::POINTER );

_radar.QueueEventProcessing();
}
// Mouse is captured by the game area for scrolling by dragging
else if ( _gameArea.isDragScroll() ) {
_gameArea.QueueEventProcessing();
}
else {
if ( fheroes2::cursor().isFocusActive() && conf.ScrollSpeed() != SCROLL_SPEED_NONE ) {
int scrollDirection = SCROLL_NONE;

if ( scrollPosition != SCROLL_NONE ) {
if ( Game::validateAnimationDelay( Game::SCROLL_START_DELAY ) && ( fastScrollRepeatCount < fastScrollStartThreshold ) ) {
++fastScrollRepeatCount;
if ( isScrollLeft( le.getMouseCursorPos() ) ) {
scrollDirection |= SCROLL_LEFT;
}
else if ( isScrollRight( le.getMouseCursorPos() ) ) {
scrollDirection |= SCROLL_RIGHT;
}
if ( isScrollTop( le.getMouseCursorPos() ) ) {
scrollDirection |= SCROLL_TOP;
}
else if ( isScrollBottom( le.getMouseCursorPos() ) ) {
scrollDirection |= SCROLL_BOTTOM;
}

if ( scrollDirection != SCROLL_NONE && _gameArea.isFastScrollEnabled() ) {
if ( Game::validateAnimationDelay( Game::SCROLL_START_DELAY ) ) {
if ( fastScrollRepeatCount < fastScrollStartThreshold ) {
++fastScrollRepeatCount;
}
}

if ( fastScrollRepeatCount >= fastScrollStartThreshold ) {
_gameArea.SetScroll( scrollPosition );
if ( fastScrollRepeatCount >= fastScrollStartThreshold ) {
_gameArea.SetScroll( scrollDirection );
}
}
else {
fastScrollRepeatCount = 0;
}
}
else {
fastScrollRepeatCount = 0;
}
}
else {
fastScrollRepeatCount = 0;
}

isCursorOverGamearea = false;
// Re-enable fast scrolling if the cursor movement indicates the need
if ( !_gameArea.isFastScrollEnabled() && _gameArea.mouseIndicatesFastScroll( le.getMouseCursorPos() ) ) {
_gameArea.setFastScrollStatus( true );
}

// Cursor is over the radar.
if ( le.isMouseCursorPosInArea( _radar.GetArea() ) ) {
cursor.SetThemes( Cursor::POINTER );
// Cursor is over the radar
if ( le.isMouseCursorPosInArea( _radar.GetRect() ) ) {
cursor.SetThemes( Cursor::POINTER );

// TODO: Add checks for object placing/moving, and other Editor functions that uses mouse dragging.
if ( !_gameArea.isDragScroll() && ( _editorPanel.getBrushArea().width > 0 || _areaSelectionStartTileId == -1 ) ) {
_radar.QueueEventProcessing();
}
}
else if ( !_gameArea.NeedScroll() ) {
if ( le.isMouseCursorPosInArea( _gameArea.GetROI() ) ) {
// Cursor is over the game area.
isCursorOverGamearea = true;
// TODO: Add checks for object placing/moving, and other Editor functions that uses mouse dragging.
if ( _editorPanel.getBrushArea().width > 0 || _areaSelectionStartTileId == -1 ) {
_radar.QueueEventProcessing();
}
}
else {
// Cursor is not over the game area.
cursor.SetThemes( Cursor::POINTER );
// Cursor is over the editor panel
else if ( le.isMouseCursorPosInArea( _editorPanel.getRect() ) ) {
// At lower resolutions, the Editor panel has no border at the bottom. If the mouse cursor is over
// this bottom section, then the game area may scroll. In this case, the mouse cursor shouldn't be
// changed, but the editor panel should still handle events.
if ( !_gameArea.NeedScroll() ) {
cursor.SetThemes( Cursor::POINTER );
}

_gameArea.ResetCursorPosition();
res = _editorPanel.queueEventProcessing();
}
else if ( !_gameArea.NeedScroll() ) {
// Cursor is over the game area
if ( le.isMouseCursorPosInArea( _gameArea.GetROI() ) ) {
_gameArea.QueueEventProcessing();

if ( le.isMouseCursorPosInArea( _editorPanel.getRect() ) ) {
// Cursor is over the buttons area.
res = _editorPanel.queueEventProcessing();
isCursorOverGameArea = true;
}
// Cursor is somewhere else
else {
cursor.SetThemes( Cursor::POINTER );
}
}
}

// gamearea
if ( !_gameArea.NeedScroll() ) {
if ( !_radar.isDragRadar() ) {
_gameArea.QueueEventProcessing( isCursorOverGamearea );
}
else if ( le.isMouseLeftButtonReleased() ) {
_radar.QueueEventProcessing();
}
if ( res != fheroes2::GameMode::CANCEL ) {
break;
}

if ( isCursorOverGamearea ) {
if ( isCursorOverGameArea ) {
// Get relative tile position under the cursor. This position can be outside the map size.
const fheroes2::Point posInGameArea = _gameArea.getInternalPosition( le.getMouseCursorPos() );
const fheroes2::Point tilePos{ posInGameArea.x / fheroes2::tileWidthPx, posInGameArea.y / fheroes2::tileWidthPx };
Expand Down Expand Up @@ -928,7 +949,7 @@ namespace Interface
}
}

if ( _areaSelectionStartTileId == -1 && isValidTile && isBrushEmpty && !_radar.isDragRadar() && le.isMouseLeftButtonPressed() ) {
if ( _areaSelectionStartTileId == -1 && isValidTile && isBrushEmpty && le.isMouseLeftButtonPressed() ) {
_areaSelectionStartTileId = tilePos.y * world.w() + tilePos.x;
_redraw |= REDRAW_GAMEAREA;
}
Expand All @@ -939,7 +960,7 @@ namespace Interface
}

if ( _areaSelectionStartTileId > -1 && le.isMouseLeftButtonReleased() ) {
if ( isCursorOverGamearea && _tileUnderCursor > -1 && _editorPanel.getBrushArea().width == 0 ) {
if ( isCursorOverGameArea && _tileUnderCursor > -1 && _editorPanel.getBrushArea().width == 0 ) {
if ( _editorPanel.isTerrainEdit() ) {
// Fill the selected area in terrain edit mode.
fheroes2::ActionCreator action( _historyManager, _mapFormat );
Expand Down Expand Up @@ -976,39 +997,45 @@ namespace Interface
_redraw |= mapUpdateFlags;
}

// fast scroll
if ( ( Game::validateAnimationDelay( Game::SCROLL_DELAY ) && _gameArea.NeedScroll() ) || _gameArea.needDragScrollRedraw() ) {
if ( ( isScrollLeft( le.getMouseCursorPos() ) || isScrollRight( le.getMouseCursorPos() ) || isScrollTop( le.getMouseCursorPos() )
|| isScrollBottom( le.getMouseCursorPos() ) )
&& !_gameArea.isDragScroll() ) {
// Scrolling the game area
if ( _gameArea.NeedScroll() && Game::validateAnimationDelay( Game::SCROLL_DELAY ) ) {
assert( !_gameArea.isDragScroll() );

if ( isScrollLeft( le.getMouseCursorPos() ) || isScrollRight( le.getMouseCursorPos() ) || isScrollTop( le.getMouseCursorPos() )
|| isScrollBottom( le.getMouseCursorPos() ) ) {
cursor.SetThemes( _gameArea.GetScrollCursor() );
}

_gameArea.Scroll();

_redraw |= REDRAW_GAMEAREA | REDRAW_RADAR_CURSOR;
setRedraw( REDRAW_GAMEAREA | REDRAW_RADAR_CURSOR );
}
else if ( _gameArea.needDragScrollRedraw() ) {
setRedraw( REDRAW_GAMEAREA | REDRAW_RADAR_CURSOR );
}

if ( res == fheroes2::GameMode::CANCEL ) {
// map objects animation
if ( Game::validateAnimationDelay( Game::MAPS_DELAY ) ) {
if ( conf.isEditorAnimationEnabled() ) {
Game::updateAdventureMapAnimationIndex();
}
_redraw |= REDRAW_GAMEAREA;
}
assert( res == fheroes2::GameMode::CANCEL );

if ( needRedraw() ) {
if ( conf.isEditorPassabilityEnabled() ) {
_redraw |= REDRAW_PASSABILITIES;
}
redraw( 0 );
// Map objects animation
if ( Game::validateAnimationDelay( Game::MAPS_DELAY ) ) {
if ( conf.isEditorAnimationEnabled() ) {
Game::updateAdventureMapAnimationIndex();
}

// If this assertion blows up it means that we are holding a RedrawLocker lock for rendering which should not happen.
assert( getRedrawMask() == 0 );
_redraw |= REDRAW_GAMEAREA;
}

validateFadeInAndRender();
if ( needRedraw() ) {
if ( conf.isEditorPassabilityEnabled() ) {
_redraw |= REDRAW_PASSABILITIES;
}

redraw( 0 );

// If this assertion blows up it means that we are holding a RedrawLocker lock for rendering which should not happen.
assert( getRedrawMask() == 0 );

validateFadeInAndRender();
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/fheroes2/editor/editor_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <vector>

#include "agg_image.h"
#include "cursor.h"
#include "dialog.h"
#include "dialog_audio.h"
#include "dialog_graphics_settings.h"
Expand Down Expand Up @@ -233,6 +234,8 @@ namespace Editor
{
void openEditorSettings()
{
const CursorRestorer cursorRestorer( true, Cursor::POINTER );

// We should write to the configuration file only once to avoid extra I/O operations.
bool saveConfiguration = false;
Settings & conf = Settings::Get();
Expand Down
Loading

0 comments on commit ff72bb7

Please sign in to comment.