Skip to content

Commit c2d95fe

Browse files
committed
Numeric input: replace value on first keypress after opening dialog or clicking buttons (#1631).
1 parent 7ecc11f commit c2d95fe

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/fheroes2/dialog/dialog_recruit.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
443443
}
444444
};
445445

446+
bool replaceOnKeypress = true;
446447
while ( le.HandleEvents() ) {
447448
bool redraw = false;
448449

@@ -503,6 +504,7 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
503504
}
504505
}
505506
updateMonsterInfo = true;
507+
replaceOnKeypress = true;
506508
}
507509
else if ( le.MouseClickLeft( monsterSwitchRight.area() ) || le.isKeyPressed( fheroes2::Key::KEY_RIGHT ) ) {
508510
for ( size_t i = 0; i < upgrades.size(); ++i ) {
@@ -517,6 +519,7 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
517519
}
518520
}
519521
updateMonsterInfo = true;
522+
replaceOnKeypress = true;
520523
}
521524
}
522525

@@ -553,14 +556,16 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
553556

554557
if ( le.MouseClickLeft( monsterArea ) ) {
555558
ArmyInfo( Troop( monster, available ), BUTTONS );
559+
replaceOnKeypress = true;
556560

557561
// Perform a full rendering to properly restore the parts of the screen outside of this dialog
558562
display.render();
559563
continue;
560564
}
561565

562-
if ( int32_t temp = static_cast<int32_t>( result ); fheroes2::processIntegerValueTyping( 0, static_cast<int32_t>( max ), temp ) ) {
566+
if ( int32_t temp = static_cast<int32_t>( result ); fheroes2::processIntegerValueTyping( 0, static_cast<int32_t>( max ), replaceOnKeypress, temp ) ) {
563567
result = temp;
568+
replaceOnKeypress = false;
564569

565570
updateCurrentInfo();
566571

@@ -577,6 +582,7 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
577582

578583
updateCurrentInfo();
579584

585+
replaceOnKeypress = true;
580586
redraw = true;
581587
}
582588
else if ( ( le.isMouseWheelUpInArea( rtWheel ) || le.MouseClickLeft( buttonUp.area() ) || le.isKeyPressed( fheroes2::Key::KEY_UP )
@@ -586,6 +592,7 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
586592

587593
updateCurrentInfo();
588594

595+
replaceOnKeypress = true;
589596
redraw = true;
590597
}
591598
else if ( ( le.isMouseWheelDownInArea( rtWheel ) || le.MouseClickLeft( buttonDn.area() ) || le.isKeyPressed( fheroes2::Key::KEY_DOWN )
@@ -595,20 +602,23 @@ Troop Dialog::RecruitMonster( const Monster & monster0, const uint32_t available
595602

596603
updateCurrentInfo();
597604

605+
replaceOnKeypress = true;
598606
redraw = true;
599607
}
600608
else if ( buttonMax.isEnabled() && le.MouseClickLeft( buttonMax.area() ) && result != max ) {
601609
result = max;
602610

603611
updateCurrentInfo();
604612

613+
replaceOnKeypress = true;
605614
redraw = true;
606615
}
607616
else if ( buttonMin.isEnabled() && le.MouseClickLeft( buttonMin.area() ) && result != 1 ) {
608617
result = 1;
609618

610619
updateCurrentInfo();
611620

621+
replaceOnKeypress = true;
612622
redraw = true;
613623
}
614624
else if ( le.isMouseRightButtonPressedInArea( buttonOk.area() ) ) {

src/fheroes2/dialog/dialog_selectcount.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ bool Dialog::SelectCount( std::string header, const int32_t min, const int32_t m
129129
const fheroes2::Rect uiRect = uiElement ? fheroes2::Rect{ uiOffset, uiElement->area() } : fheroes2::Rect{};
130130

131131
LocalEvent & le = LocalEvent::Get();
132+
bool replaceOnKeypress = true;
132133
while ( result == Dialog::ZERO && le.HandleEvents() ) {
133134
bool needRedraw = false;
134135

@@ -140,21 +141,25 @@ bool Dialog::SelectCount( std::string header, const int32_t min, const int32_t m
140141
buttonMin.drawOnState( le.isMouseLeftButtonPressedInArea( buttonMin.area() ) );
141142
}
142143

143-
if ( fheroes2::processIntegerValueTyping( min, max, selectedValue ) ) {
144+
if ( fheroes2::processIntegerValueTyping( min, max, replaceOnKeypress, selectedValue ) ) {
144145
valueSelectionElement.setValue( selectedValue );
146+
replaceOnKeypress = false;
145147
needRedraw = true;
146148
}
147149
else if ( buttonMax.isVisible() && le.MouseClickLeft( buttonMax.area() ) ) {
148150
selectedValue = max;
149151
valueSelectionElement.setValue( max );
152+
replaceOnKeypress = true;
150153
needRedraw = true;
151154
}
152155
else if ( buttonMin.isVisible() && le.MouseClickLeft( buttonMin.area() ) ) {
153156
selectedValue = min;
154157
valueSelectionElement.setValue( min );
158+
replaceOnKeypress = true;
155159
needRedraw = true;
156160
}
157161
else if ( valueSelectionElement.processEvents() ) {
162+
replaceOnKeypress = true;
158163
selectedValue = valueSelectionElement.getValue();
159164
needRedraw = true;
160165
}
@@ -485,6 +490,7 @@ int Dialog::ArmySplitTroop( const int32_t freeSlots, const int32_t redistributeM
485490

486491
// message loop
487492
int bres = Dialog::ZERO;
493+
bool replaceOnKeypress = true;
488494
while ( bres == Dialog::ZERO && le.HandleEvents() ) {
489495
bool redraw_count = false;
490496

@@ -496,22 +502,26 @@ int Dialog::ArmySplitTroop( const int32_t freeSlots, const int32_t redistributeM
496502
buttonMin.drawOnState( le.isMouseLeftButtonPressedInArea( buttonMin.area() ) );
497503
}
498504

499-
if ( fheroes2::processIntegerValueTyping( redistributeMin, redistributeMax, redistributeCount ) ) {
505+
if ( fheroes2::processIntegerValueTyping( redistributeMin, redistributeMax, replaceOnKeypress, redistributeCount ) ) {
500506
valueSelectionElement.setValue( redistributeCount );
507+
replaceOnKeypress = false;
501508
redraw_count = true;
502509
}
503510
else if ( buttonMax.isVisible() && le.MouseClickLeft( buttonMax.area() ) ) {
504511
redistributeCount = redistributeMax;
505512
valueSelectionElement.setValue( redistributeMax );
513+
replaceOnKeypress = true;
506514
redraw_count = true;
507515
}
508516
else if ( buttonMin.isVisible() && le.MouseClickLeft( buttonMin.area() ) ) {
509517
redistributeCount = redistributeMin;
510518
valueSelectionElement.setValue( redistributeMin );
519+
replaceOnKeypress = true;
511520
redraw_count = true;
512521
}
513522
else if ( valueSelectionElement.processEvents() ) {
514523
redistributeCount = valueSelectionElement.getValue();
524+
replaceOnKeypress = true;
515525
redraw_count = true;
516526
}
517527

src/fheroes2/gui/ui_tool.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ namespace fheroes2
790790
}
791791
}
792792

793-
bool processIntegerValueTyping( const int32_t minimum, const int32_t maximum, int32_t & value )
793+
bool processIntegerValueTyping( const int32_t minimum, const int32_t maximum, const bool replace, int32_t & value )
794794
{
795795
const LocalEvent & le = LocalEvent::Get();
796796

@@ -821,8 +821,13 @@ namespace fheroes2
821821
return false;
822822
}
823823

824-
value *= 10;
825-
value += ( value >= 0 ) ? newDigit : ( -newDigit );
824+
if ( replace ) {
825+
value = newDigit;
826+
}
827+
else {
828+
value *= 10;
829+
value += ( value >= 0 ) ? newDigit : ( -newDigit );
830+
}
826831

827832
value = std::clamp( value, minimum, maximum );
828833

src/fheroes2/gui/ui_tool.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ namespace fheroes2
224224

225225
void InvertedShadow( Image & image, const Rect & roi, const Rect & excludedRoi, const uint8_t paletteId, const int32_t paletteCount );
226226

227-
bool processIntegerValueTyping( const int32_t minimum, const int32_t maximum, int32_t & value );
227+
// Updates `value` based on keyboard input relevant to modifying an integer.
228+
// Parameter `replace` determines whether digit inputs will replace the value, instead of appending to it.
229+
bool processIntegerValueTyping( const int32_t minimum, const int32_t maximum, const bool replace, int32_t & value );
228230

229231
// Render "hero on a horse" portrait dependent from hero race. Used in Editor.
230232
void renderHeroRacePortrait( const int race, const fheroes2::Rect & portPos, fheroes2::Image & output );

0 commit comments

Comments
 (0)