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

Use drawOnState() for buttons precessing and cache buttons' position for event handler loop #9223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 103 additions & 59 deletions src/fheroes2/battle/battle_dialogs.cpp

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions src/fheroes2/battle/battle_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,9 @@ namespace Battle
SetScrollButtonUp( ICN::DROPLISL, 6, 7, fheroes2::Point( ax, area.y - 10 ) );
SetScrollButtonDn( ICN::DROPLISL, 8, 9, fheroes2::Point( ax, area.y + area.height - 11 ) );

const fheroes2::Rect & buttonPgUpArea = buttonPgUp.area();
const fheroes2::Rect & buttonPgUpArea = _buttonPgUp.area();

_scrollbarSliderAreaLength = buttonPgDn.area().y - ( buttonPgUpArea.y + buttonPgUpArea.height ) - 7;
_scrollbarSliderAreaLength = _buttonPgDn.area().y - ( buttonPgUpArea.y + buttonPgUpArea.height ) - 7;

setScrollBarArea( { ax + 5, buttonPgUpArea.y + buttonPgUpArea.height + 3, 12, _scrollbarSliderAreaLength } );

Expand Down Expand Up @@ -774,9 +774,9 @@ namespace Battle
{
fheroes2::Display & display = fheroes2::Display::instance();

const fheroes2::Rect & buttonPgUpArea = buttonPgUp.area();
const fheroes2::Rect & buttonPgUpArea = _buttonPgUp.area();
const int32_t buttonPgUpBottom = buttonPgUpArea.y + buttonPgUpArea.height;
const int32_t buttonPgDnAreaY = buttonPgDn.area().y;
const int32_t buttonPgDnAreaY = _buttonPgDn.area().y;

const int32_t ax = buttonPgUpArea.x;
const int32_t ah = buttonPgDnAreaY - buttonPgUpBottom;
Expand Down Expand Up @@ -1277,8 +1277,8 @@ Battle::Interface::Interface( Arena & battleArena, const int32_t tileIndex )
// Shadow that fits the hexagon grid.
_hexagonGridShadow = DrawHexagonShadow( 4, 1 );

btn_auto.setICNInfo( ICN::TEXTBAR, 4, 5 );
btn_settings.setICNInfo( ICN::TEXTBAR, 6, 7 );
_buttonAuto.setICNInfo( ICN::TEXTBAR, 4, 5 );
_buttonSettings.setICNInfo( ICN::TEXTBAR, 6, 7 );

// opponents
if ( HeroBase * opponent = arena.GetCommander1(); opponent != nullptr ) {
Expand All @@ -1294,13 +1294,13 @@ Battle::Interface::Interface( Arena & battleArena, const int32_t tileIndex )

const fheroes2::Rect & area = border.GetArea();

const fheroes2::Rect settingsRect = btn_settings.area();
const int32_t satusOffsetY = area.y + area.height - settingsRect.height - btn_auto.area().height;
btn_auto.setPosition( area.x, satusOffsetY );
btn_settings.setPosition( area.x, area.y + area.height - settingsRect.height );
const fheroes2::Rect settingsRect = _buttonSettings.area();
const int32_t satusOffsetY = area.y + area.height - settingsRect.height - _buttonAuto.area().height;
_buttonAuto.setPosition( area.x, satusOffsetY );
_buttonSettings.setPosition( area.x, area.y + area.height - settingsRect.height );

btn_skip.setICNInfo( ICN::TEXTBAR, 0, 1 );
btn_skip.setPosition( area.x + area.width - btn_skip.area().width, area.y + area.height - btn_skip.area().height );
_buttonSkip.setICNInfo( ICN::TEXTBAR, 0, 1 );
_buttonSkip.setPosition( area.x + area.width - _buttonSkip.area().width, area.y + area.height - _buttonSkip.area().height );

status.setPosition( area.x + settingsRect.width, satusOffsetY );

Expand Down Expand Up @@ -1472,9 +1472,9 @@ void Battle::Interface::RedrawInterface()
{
status.redraw( fheroes2::Display::instance() );

btn_auto.draw();
btn_settings.draw();
btn_skip.draw();
_buttonAuto.draw();
_buttonSettings.draw();
_buttonSkip.draw();

popup.redraw();

Expand Down Expand Up @@ -2864,7 +2864,7 @@ void Battle::Interface::HumanBattleTurn( const Unit & unit, Actions & actions, s
cursor.SetThemes( Cursor::POINTER );
_turnOrder.queueEventProcessing( msg, _interfacePosition.getPosition() );
}
else if ( le.isMouseCursorPosInArea( btn_auto.area() ) ) {
else if ( le.isMouseCursorPosInArea( _buttonAuto.area() ) ) {
cursor.SetThemes( Cursor::WAR_POINTER );
msg = _( "Enable auto combat" );
ButtonAutoAction( unit, actions );
Expand All @@ -2873,7 +2873,7 @@ void Battle::Interface::HumanBattleTurn( const Unit & unit, Actions & actions, s
fheroes2::showStandardTextMessage( _( "Auto Combat" ), _( "Allows the computer to fight out the battle for you." ), Dialog::ZERO );
}
}
else if ( le.isMouseCursorPosInArea( btn_settings.area() ) ) {
else if ( le.isMouseCursorPosInArea( _buttonSettings.area() ) ) {
cursor.SetThemes( Cursor::WAR_POINTER );
msg = _( "Customize system options" );
ButtonSettingsAction();
Expand All @@ -2882,7 +2882,7 @@ void Battle::Interface::HumanBattleTurn( const Unit & unit, Actions & actions, s
fheroes2::showStandardTextMessage( _( "System Options" ), _( "Allows you to customize the combat screen." ), Dialog::ZERO );
}
}
else if ( le.isMouseCursorPosInArea( btn_skip.area() ) ) {
else if ( le.isMouseCursorPosInArea( _buttonSkip.area() ) ) {
cursor.SetThemes( Cursor::WAR_POINTER );
msg = _( "Skip this unit" );
ButtonSkipAction( actions );
Expand Down Expand Up @@ -3167,9 +3167,9 @@ void Battle::Interface::_openBattleSettingsDialog()

void Battle::Interface::EventShowOptions()
{
btn_settings.drawOnPress();
_buttonSettings.drawOnPress();
_openBattleSettingsDialog();
btn_settings.drawOnRelease();
_buttonSettings.drawOnRelease();
humanturn_redraw = true;
}

Expand Down Expand Up @@ -3206,9 +3206,9 @@ void Battle::Interface::ButtonAutoAction( const Unit & unit, Actions & actions )
{
LocalEvent & le = LocalEvent::Get();

le.isMouseLeftButtonPressedInArea( btn_auto.area() ) ? btn_auto.drawOnPress() : btn_auto.drawOnRelease();
_buttonAuto.drawOnState( le.isMouseLeftButtonPressedInArea( _buttonAuto.area() ) );

if ( le.MouseClickLeft( btn_auto.area() ) ) {
if ( le.MouseClickLeft( _buttonAuto.area() ) ) {
EventStartAutoBattle( unit, actions );
}
}
Expand All @@ -3217,9 +3217,9 @@ void Battle::Interface::ButtonSettingsAction()
{
LocalEvent & le = LocalEvent::Get();

le.isMouseLeftButtonPressedInArea( btn_settings.area() ) ? btn_settings.drawOnPress() : btn_settings.drawOnRelease();
_buttonSettings.drawOnState( le.isMouseLeftButtonPressedInArea( _buttonSettings.area() ) );

if ( le.MouseClickLeft( btn_settings.area() ) ) {
if ( le.MouseClickLeft( _buttonSettings.area() ) ) {
_openBattleSettingsDialog();

humanturn_redraw = true;
Expand All @@ -3230,9 +3230,9 @@ void Battle::Interface::ButtonSkipAction( Actions & actions )
{
LocalEvent & le = LocalEvent::Get();

le.isMouseLeftButtonPressedInArea( btn_skip.area() ) ? btn_skip.drawOnPress() : btn_skip.drawOnRelease();
_buttonSkip.drawOnState( le.isMouseLeftButtonPressedInArea( _buttonSkip.area() ) );

if ( le.MouseClickLeft( btn_skip.area() ) && _currentUnit ) {
if ( le.MouseClickLeft( _buttonSkip.area() ) && _currentUnit ) {
actions.emplace_back( Command::SKIP, _currentUnit->GetUID() );
humanturn_exit = true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/fheroes2/battle/battle_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ namespace Battle
int _battleGroundIcn{ ICN::UNKNOWN };
int _borderObjectsIcn{ ICN::UNKNOWN };

fheroes2::Button btn_auto;
fheroes2::Button btn_settings;
fheroes2::Button btn_skip;
fheroes2::Button _buttonAuto;
fheroes2::Button _buttonSettings;
fheroes2::Button _buttonSkip;
Status status;

std::unique_ptr<OpponentSprite> _opponent1;
Expand Down
34 changes: 22 additions & 12 deletions src/fheroes2/battle/battle_only.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
fheroes2::Button buttonStart( cur_pt.x + 178, cur_pt.y + 428, ICN::BUTTON_START_GOOD, 0, 1 );
fheroes2::Button buttonExit( cur_pt.x + 366, cur_pt.y + 428, ICN::BUTTON_EXIT_GOOD, 0, 1 );

fheroes2::addGradientShadow( fheroes2::AGG::GetICN( ICN::BUTTON_RESET_GOOD, 0 ), display, buttonReset.area().getPosition(), { -5, 5 } );
fheroes2::addGradientShadow( fheroes2::AGG::GetICN( ICN::BUTTON_START_GOOD, 0 ), display, buttonStart.area().getPosition(), { -5, 5 } );
fheroes2::addGradientShadow( fheroes2::AGG::GetICN( ICN::BUTTON_EXIT_GOOD, 0 ), display, buttonExit.area().getPosition(), { -5, 5 } );
const fheroes2::Rect buttonResetArea = buttonReset.area();
const fheroes2::Rect buttonStartArea = buttonStart.area();
const fheroes2::Rect buttonExitArea = buttonExit.area();

fheroes2::addGradientShadow( fheroes2::AGG::GetICN( ICN::BUTTON_RESET_GOOD, 0 ), display, buttonResetArea.getPosition(), { -5, 5 } );
fheroes2::addGradientShadow( fheroes2::AGG::GetICN( ICN::BUTTON_START_GOOD, 0 ), display, buttonStartArea.getPosition(), { -5, 5 } );
fheroes2::addGradientShadow( fheroes2::AGG::GetICN( ICN::BUTTON_EXIT_GOOD, 0 ), display, buttonExitArea.getPosition(), { -5, 5 } );

buttonStart.draw();
buttonExit.draw();
Expand All @@ -215,33 +219,39 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
bool needRedrawOpponentsStats = false;
bool needRedrawControlInfo = false;

buttonStart.isEnabled() && le.isMouseLeftButtonPressedInArea( buttonStart.area() ) ? buttonStart.drawOnPress() : buttonStart.drawOnRelease();
buttonExit.isEnabled() && le.isMouseLeftButtonPressedInArea( buttonExit.area() ) ? buttonExit.drawOnPress() : buttonExit.drawOnRelease();
buttonReset.isEnabled() && le.isMouseLeftButtonPressedInArea( buttonReset.area() ) ? buttonReset.drawOnPress() : buttonReset.drawOnRelease();
if ( buttonStart.isEnabled() ) {
buttonStart.drawOnState( le.isMouseLeftButtonPressedInArea( buttonStartArea ) );
}
if ( buttonExit.isEnabled() ) {
buttonExit.drawOnState( le.isMouseLeftButtonPressedInArea( buttonExitArea ) );
}
if ( buttonReset.isEnabled() ) {
buttonReset.drawOnState( le.isMouseLeftButtonPressedInArea( buttonResetArea ) );
}

if ( ( buttonStart.isEnabled() && le.MouseClickLeft( buttonStart.area() ) ) || Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_OKAY ) ) {
if ( ( buttonStart.isEnabled() && le.MouseClickLeft( buttonStartArea ) ) || Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_OKAY ) ) {
result = true;

break;
}
if ( le.MouseClickLeft( buttonReset.area() ) ) {
if ( le.MouseClickLeft( buttonResetArea ) ) {
reset = true;
result = true;

break;
}

if ( le.MouseClickLeft( buttonExit.area() ) || Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_CANCEL ) ) {
if ( le.MouseClickLeft( buttonExitArea ) || Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_CANCEL ) ) {
break;
}

if ( le.isMouseRightButtonPressedInArea( buttonStart.area() ) ) {
if ( le.isMouseRightButtonPressedInArea( buttonStartArea ) ) {
fheroes2::showStandardTextMessage( _( "Start" ), _( "Start the battle." ), 0 );
}
else if ( le.isMouseRightButtonPressedInArea( buttonExit.area() ) ) {
else if ( le.isMouseRightButtonPressedInArea( buttonExitArea ) ) {
fheroes2::showStandardTextMessage( _( "Exit" ), _( "Exit this menu." ), 0 );
}
else if ( le.isMouseRightButtonPressedInArea( buttonReset.area() ) ) {
else if ( le.isMouseRightButtonPressedInArea( buttonResetArea ) ) {
fheroes2::showStandardTextMessage( _( "Reset" ), _( "Reset to default settings." ), 0 );
}

Expand Down
75 changes: 42 additions & 33 deletions src/fheroes2/castle/buildinginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,7 @@ bool BuildingInfo::DialogBuyBuilding( bool buttons ) const
const Dialog::FrameBox dialogFrame( totalDialogHeight, buttons );
const fheroes2::Rect & dialogRoi = dialogFrame.GetArea();

const bool isEvilInterface = Settings::Get().isEvilInterfaceEnabled();
const int buttonOkayIcnID = isEvilInterface ? ICN::UNIFORM_EVIL_OKAY_BUTTON : ICN::UNIFORM_GOOD_OKAY_BUTTON;

fheroes2::Point pos{ dialogRoi.x, dialogRoi.y + dialogRoi.height - fheroes2::AGG::GetICN( buttonOkayIcnID, 0 ).height() };
fheroes2::Button buttonOkay( pos.x, pos.y, buttonOkayIcnID, 0, 1 );

const int buttonCancelIcnID = isEvilInterface ? ICN::UNIFORM_EVIL_CANCEL_BUTTON : ICN::UNIFORM_GOOD_CANCEL_BUTTON;

pos.x = dialogRoi.x + dialogRoi.width - fheroes2::AGG::GetICN( buttonCancelIcnID, 0 ).width();
pos.y = dialogRoi.y + dialogRoi.height - fheroes2::AGG::GetICN( buttonCancelIcnID, 0 ).height();
fheroes2::Button buttonCancel( pos.x, pos.y, buttonCancelIcnID, 0, 1 );

pos.x = dialogRoi.x + ( dialogRoi.width - buildingFrame.width() ) / 2;
pos.y = dialogRoi.y + elementOffset;
fheroes2::Point pos{ dialogRoi.x + ( dialogRoi.width - buildingFrame.width() ) / 2, pos.y = dialogRoi.y + elementOffset };

fheroes2::Display & display = fheroes2::Display::instance();
fheroes2::Blit( buildingFrame, display, pos.x, pos.y );
Expand Down Expand Up @@ -540,45 +527,67 @@ bool BuildingInfo::DialogBuyBuilding( bool buttons ) const
rbs.SetPos( pos.x, pos.y );
rbs.Redraw();

if ( buttons ) {
if ( BuildingStatus::ALLOW_BUILD != castle.CheckBuyBuilding( _buildingType ) ) {
buttonOkay.disable();
LocalEvent & le = LocalEvent::Get();

if ( !buttons ) {
// This is a case when this dialog was called by the right mouse button press.

display.render();

while ( le.HandleEvents() ) {
if ( !le.isMouseRightButtonPressed() ) {
break;
}
}

buttonOkay.draw();
buttonCancel.draw();
return false;
}
else {
buttonOkay.disable();
buttonOkay.hide();

buttonCancel.disable();
buttonCancel.hide();
const bool isEvilInterface = Settings::Get().isEvilInterfaceEnabled();
const int buttonOkayIcnID = isEvilInterface ? ICN::UNIFORM_EVIL_OKAY_BUTTON : ICN::UNIFORM_GOOD_OKAY_BUTTON;

pos.x = dialogRoi.x;
pos.y = dialogRoi.y + dialogRoi.height - fheroes2::AGG::GetICN( buttonOkayIcnID, 0 ).height();

fheroes2::Button buttonOkay( pos.x, pos.y, buttonOkayIcnID, 0, 1 );

const int buttonCancelIcnID = isEvilInterface ? ICN::UNIFORM_EVIL_CANCEL_BUTTON : ICN::UNIFORM_GOOD_CANCEL_BUTTON;

pos.x = dialogRoi.x + dialogRoi.width - fheroes2::AGG::GetICN( buttonCancelIcnID, 0 ).width();
pos.y = dialogRoi.y + dialogRoi.height - fheroes2::AGG::GetICN( buttonCancelIcnID, 0 ).height();
fheroes2::Button buttonCancel( pos.x, pos.y, buttonCancelIcnID, 0, 1 );

if ( BuildingStatus::ALLOW_BUILD != castle.CheckBuyBuilding( _buildingType ) ) {
buttonOkay.disable();
}

buttonOkay.draw();
buttonCancel.draw();

const fheroes2::Rect buttonOkayArea = buttonOkay.area();
const fheroes2::Rect buttonCancelArea = buttonCancel.area();

display.render();

LocalEvent & le = LocalEvent::Get();
while ( le.HandleEvents() ) {
if ( !buttons && !le.isMouseRightButtonPressed() ) {
break;
if ( buttonOkay.isEnabled() ) {
buttonOkay.drawOnState( le.isMouseLeftButtonPressedInArea( buttonOkayArea ) );
}

le.isMouseLeftButtonPressedInArea( buttonOkay.area() ) ? buttonOkay.drawOnPress() : buttonOkay.drawOnRelease();
le.isMouseLeftButtonPressedInArea( buttonCancel.area() ) ? buttonCancel.drawOnPress() : buttonCancel.drawOnRelease();
buttonCancel.drawOnState( le.isMouseLeftButtonPressedInArea( buttonCancelArea ) );

if ( buttonOkay.isEnabled() && ( Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_OKAY ) || le.MouseClickLeft( buttonOkay.area() ) ) ) {
if ( buttonOkay.isEnabled() && ( Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_OKAY ) || le.MouseClickLeft( buttonOkayArea ) ) ) {
return true;
}

if ( Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_CANCEL ) || le.MouseClickLeft( buttonCancel.area() ) ) {
if ( Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_CANCEL ) || le.MouseClickLeft( buttonCancelArea ) ) {
break;
}

if ( buttonOkay.isVisible() && le.isMouseRightButtonPressedInArea( buttonOkay.area() ) ) {
if ( le.isMouseRightButtonPressedInArea( buttonOkayArea ) ) {
fheroes2::showStandardTextMessage( _( "Okay" ), GetConditionDescription(), Dialog::ZERO );
}
else if ( buttonCancel.isVisible() && le.isMouseRightButtonPressedInArea( buttonCancel.area() ) ) {
else if ( le.isMouseRightButtonPressedInArea( buttonCancelArea ) ) {
fheroes2::showStandardTextMessage( _( "Cancel" ), _( "Exit this menu without doing anything." ), Dialog::ZERO );
}
}
Expand Down
Loading
Loading