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 swap buttons in hero meeting dialog #3106

Merged
merged 9 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add logic and UI
still one graphical defect
  • Loading branch information
ihhub committed Apr 17, 2021
commit 22d12331e1d06e4f235c0989a6b34b95ecbd28c5
2 changes: 1 addition & 1 deletion VisualStudio/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\src\engine;$(MSBuildThisFileDirectory)..\src\fheroes2\gui;$(MSBuildThisFileDirectory)..\src\fheroes2\maps;$(MSBuildThisFileDirectory)..\src\fheroes2\kingdom;$(MSBuildThisFileDirectory)..\src\fheroes2\campaign;$(MSBuildThisFileDirectory)..\src\fheroes2\game;$(MSBuildThisFileDirectory)..\src\fheroes2\dialog;$(MSBuildThisFileDirectory)..\src\fheroes2\system;$(MSBuildThisFileDirectory)..\src\fheroes2\spell;$(MSBuildThisFileDirectory)..\src\fheroes2\monster;$(MSBuildThisFileDirectory)..\src\fheroes2\castle;$(MSBuildThisFileDirectory)..\src\fheroes2\agg;$(MSBuildThisFileDirectory)..\src\fheroes2\heroes;$(MSBuildThisFileDirectory)..\src\fheroes2\resource;$(MSBuildThisFileDirectory)..\src\fheroes2\ai;$(MSBuildThisFileDirectory)..\src\fheroes2\army;$(MSBuildThisFileDirectory)..\src\fheroes2\battle;$(MSBuildThisFileDirectory)..\src\fheroes2\pocketpc;$(MSBuildThisFileDirectory)..\src\fheroes2\objects;$(MSBuildThisFileDirectory)..\src\fheroes2\world;$(MSBuildThisFileDirectory)..\src\fheroes2\image;$(MSBuildThisFileDirectory)..\src\thirdparty\libsmacker;$(MSBuildThisFileDirectory)..\..\zlib\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;WITH_ZLIB;WITH_MIXER;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatSpecificWarningsAsErrors>4800;4189;4309;4319</TreatSpecificWarningsAsErrors>
<TreatSpecificWarningsAsErrors>4100;4189;4309;4319;4715;4800</TreatSpecificWarningsAsErrors>
</ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\src\fheroes2\system;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
Expand Down
35 changes: 35 additions & 0 deletions src/engine/image_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,39 @@ namespace fheroes2

return sprite;
}

Sprite addButtonShadow( const Sprite & in, const Point & shadowOffset )
{
if ( in.empty() || shadowOffset.x > 0 || shadowOffset.y < 0 )
return in;

const int32_t width = in.width();
const int32_t height = in.height();

Sprite out( width - shadowOffset.x, height + shadowOffset.y );
out.reset();

Copy( in, 0, 0, out, -shadowOffset.x, 0, width, height );

const int32_t widthOut = out.width();

// Shadow has (-x, +y) offset.
const uint8_t * transformInY = out.transform() - shadowOffset.x ;
const uint8_t * transformInYEnd = transformInY + widthOut * height;
uint8_t * transformOutY = out.transform() + shadowOffset.y * widthOut;

for ( ; transformInY != transformInYEnd; transformInY += widthOut, transformOutY += widthOut ) {
const uint8_t * transformInX = transformInY;
uint8_t * transformOutX = transformOutY;
const uint8_t * transformInXEnd = transformInX + width;

for ( ; transformInX != transformInXEnd; ++transformInX, ++transformOutX ) {
if ( *transformInX == 0 && *transformOutX == 1 ) {
*transformOutX = 4;
}
}
}

return out;
}
}
3 changes: 3 additions & 0 deletions src/engine/image_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ namespace fheroes2
bool Load( const std::string & path, Image & image );

Sprite decodeICNSprite( const uint8_t * data, uint32_t sizeData, const int32_t width, const int32_t height, const int16_t offsetX, const int16_t offsetY );

// Generate a new image with a shadow of a button based on a shape of existing image. Shadow has only (-x, +y) offset.
Sprite addButtonShadow( const Sprite & in, const Point & shadowOffset );
}
74 changes: 71 additions & 3 deletions src/fheroes2/agg/agg_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "text.h"
#include "til.h"

#include "image_tool.h"

namespace fheroes2
{
namespace AGG
Expand Down Expand Up @@ -699,8 +701,53 @@ namespace fheroes2
}
return true;
}
case ICN::SWAP_ARROW_LEFT_TO_RIGHT: {
// Since the original game does not have such resources we could generate it from hero meeting sprite.
const Sprite & original = GetICN( ICN::SWAPWIN, 0 );
std::vector<Image> input( 4 );

const int32_t width = 43;
const int32_t height = 20;

for ( Image & image : input )
image.resize( width, height );

Copy( original, 297, 270, input[0], 0, 0, width, height );
Copy( original, 295, 291, input[1], 0, 0, width, height );
Copy( original, 297, 363, input[2], 0, 0, width, height );
Copy( original, 295, 384, input[3], 0, 0, width, height );

input[1] = Flip( input[1], true, false );
input[3] = Flip( input[3], true, false );

case ICN::SWAP_ARROW: {
_icnVsSprite[id].resize( 2 );
_icnVsSprite[id][0] = ExtractCommonPattern( input );
Sprite & out = _icnVsSprite[id][0];

// Here are 2 pixels which should be removed.
if ( out.width() == 43 && out.height() == 20 ) {
if ( out.image()[38] != 0 ) {
out.image()[38] = 0;
out.transform()[38] = 1;
}
if ( out.image()[28 + 3 * 43] != 0 ) {
out.image()[28 + 3 * 43] = 0;
out.transform()[28 + 3 * 43] = 1;
}
}

_icnVsSprite[id][1] = _icnVsSprite[id][0];
ApplyPalette( _icnVsSprite[id][1], 4 );

_icnVsSprite[id][0] = addButtonShadow( _icnVsSprite[id][0], Point( -3, 3 ) );
_icnVsSprite[id][1] = addButtonShadow( _icnVsSprite[id][1], Point( -2, 2 ) );
_icnVsSprite[id][0].setPosition( -3, 0 );
_icnVsSprite[id][1].setPosition( -2, 1 );

return true;
}
case ICN::SWAP_ARROW_RIGHT_TO_LEFT: {
// Since the original game does not have such resources we could generate it from hero meeting sprite.
const Sprite & original = GetICN( ICN::SWAPWIN, 0 );
std::vector<Image> input( 4 );

Expand All @@ -718,9 +765,30 @@ namespace fheroes2
input[1] = Flip( input[1], true, false );
input[3] = Flip( input[3], true, false );

Image output = ExtractCommonPattern( input );
_icnVsSprite[id].resize( 2 );
Image temp = ExtractCommonPattern( input );

// Here are 2 pixels which should be removed.
if ( temp.width() == 43 && temp.height() == 20 ) {
if ( temp.image()[38] != 0 ) {
temp.image()[38] = 0;
temp.transform()[38] = 1;
}
if ( temp.image()[28 + 3 * 43] != 0 ) {
temp.image()[28 + 3 * 43] = 0;
temp.transform()[28 + 3 * 43] = 1;
}
}

_icnVsSprite[id][0] = Flip( temp, true, false );

_icnVsSprite[id][1] = _icnVsSprite[id][0];
ApplyPalette( _icnVsSprite[id][1], 4 );

ApplyPalette( output, 4 );
_icnVsSprite[id][0] = addButtonShadow( _icnVsSprite[id][0], Point( -3, 3 ) );
_icnVsSprite[id][1] = addButtonShadow( _icnVsSprite[id][1], Point( -2, 2 ) );
_icnVsSprite[id][0].setPosition( -3, 0 );
_icnVsSprite[id][1].setPosition( -2, 1 );

return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/fheroes2/agg/icn.h
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,8 @@ namespace ICN
UNIFORM_EVIL_MIN_BUTTON,

WHITE_LARGE_FONT,
SWAP_ARROW,
SWAP_ARROW_LEFT_TO_RIGHT,
SWAP_ARROW_RIGHT_TO_LEFT,

// IMPORTANT! Put any new entry just above this one.
LASTICN
Expand Down
28 changes: 28 additions & 0 deletions src/fheroes2/army/army.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,34 @@ void Troops::JoinTroops( Troops & troops2 )
}
}

void Troops::MoveTroops( Troops & from )
{
if ( this == &from )
return;

size_t validTroops = 0;
for ( Troop * troop : from ) {
if ( troop && troop->isValid() ) {
++validTroops;
}
}

for ( Troop * troop : from ) {
if ( troop && troop->isValid() ) {
if ( validTroops == 1 ) {
if ( JoinTroop( troop->GetMonster(), troop->GetCount() - 1 ) ) {
troop->SetCount( 1 );
break;
}
}
else if ( JoinTroop( *troop ) ) {
--validTroops;
troop->Reset();
}
}
}
}

u32 Troops::GetUniqueCount( void ) const
{
std::set<int> monsters;
Expand Down
3 changes: 3 additions & 0 deletions src/fheroes2/army/army.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class Troops : protected std::vector<Troop *>
void JoinTroops( Troops & );
bool CanJoinTroops( const Troops & ) const;

// Used only for moving full army in hero's meeting dialog.
void MoveTroops( Troops & from );

void MergeTroops();
Troops GetOptimized( void ) const;

Expand Down
107 changes: 107 additions & 0 deletions src/fheroes2/heroes/heroes_meeting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@
#include "skill_bar.h"
#include "text.h"

namespace
{
void moveArtifacts( BagArtifacts & bagFrom, BagArtifacts & bagTo )
{
size_t toIdx = 0;

for ( size_t fromIdx = 0; fromIdx < bagFrom.size(); ++fromIdx ) {
if ( bagFrom[fromIdx]() != Artifact::UNKNOWN && bagFrom[fromIdx]() != Artifact::MAGIC_BOOK ) {
while ( toIdx < bagTo.size() ) {
if ( bagTo[toIdx]() == Artifact::UNKNOWN )
break;

++toIdx;
}

if ( toIdx == bagTo.size() )
break;

std::swap( bagFrom[fromIdx], bagTo[toIdx] );
}
}
}
}

class MeetingArmyBar : public ArmyBar
{
public:
Expand Down Expand Up @@ -331,6 +355,18 @@ void Heroes::MeetingDialog( Heroes & heroes2 )

buttonExit.draw();

fheroes2::Button moveArmyToHero2( cur_pt.x + 297, cur_pt.y + 270, ICN::SWAP_ARROW_LEFT_TO_RIGHT, 0, 1 );
moveArmyToHero2.draw();

fheroes2::Button moveArmyToHero1( cur_pt.x + 295, cur_pt.y + 291, ICN::SWAP_ARROW_RIGHT_TO_LEFT, 0, 1 );
moveArmyToHero1.draw();

fheroes2::Button moveArtifactsToHero2( cur_pt.x + 297, cur_pt.y + 363, ICN::SWAP_ARROW_LEFT_TO_RIGHT, 0, 1 );
moveArtifactsToHero2.draw();

fheroes2::Button moveArtifactsToHero1( cur_pt.x + 295, cur_pt.y + 384, ICN::SWAP_ARROW_RIGHT_TO_LEFT, 0, 1 );
moveArtifactsToHero1.draw();

cursor.Show();
display.render();

Expand All @@ -346,6 +382,11 @@ void Heroes::MeetingDialog( Heroes & heroes2 )
// message loop
while ( le.HandleEvents() ) {
le.MousePressLeft( buttonExit.area() ) ? buttonExit.drawOnPress() : buttonExit.drawOnRelease();
le.MousePressLeft( moveArmyToHero2.area() ) ? moveArmyToHero2.drawOnPress() : moveArmyToHero2.drawOnRelease();
le.MousePressLeft( moveArmyToHero1.area() ) ? moveArmyToHero1.drawOnPress() : moveArmyToHero1.drawOnRelease();
le.MousePressLeft( moveArtifactsToHero2.area() ) ? moveArtifactsToHero2.drawOnPress() : moveArtifactsToHero2.drawOnRelease();
le.MousePressLeft( moveArtifactsToHero1.area() ) ? moveArtifactsToHero1.drawOnPress() : moveArtifactsToHero1.drawOnRelease();

if ( le.MouseClickLeft( buttonExit.area() ) || HotKeyCloseWindow )
break;

Expand Down Expand Up @@ -457,6 +498,72 @@ void Heroes::MeetingDialog( Heroes & heroes2 )
moraleIndicator2.Redraw();
luckIndicator2.Redraw();

cursor.Show();
display.render();
}
else if ( le.MouseClickLeft( moveArmyToHero2.area() ) ) {
heroes2.GetArmy().MoveTroops( GetArmy() );

armyCountBackgroundRestorer.restore();

selectArmy1.ResetSelected();
selectArmy2.ResetSelected();
selectArmy1.Redraw();
selectArmy2.Redraw();
moraleIndicator1.Redraw();
moraleIndicator2.Redraw();

cursor.Show();
display.render();
}
else if ( le.MouseClickLeft( moveArmyToHero1.area() ) ) {
GetArmy().MoveTroops( heroes2.GetArmy() );

armyCountBackgroundRestorer.restore();

selectArmy1.ResetSelected();
selectArmy2.ResetSelected();
selectArmy1.Redraw();
selectArmy2.Redraw();
moraleIndicator1.Redraw();
moraleIndicator2.Redraw();

cursor.Show();
display.render();
}
else if ( le.MouseClickLeft( moveArtifactsToHero2.area() ) ) {
moveArtifacts( GetBagArtifacts(), heroes2.GetBagArtifacts() );

selectArtifacts1.ResetSelected();
selectArtifacts2.ResetSelected();
selectArtifacts1.Redraw();
selectArtifacts2.Redraw();

backPrimary.restore();
RedrawPrimarySkillInfo( cur_pt, &primskill_bar1, &primskill_bar2 );
moraleIndicator1.Redraw();
moraleIndicator2.Redraw();
luckIndicator1.Redraw();
luckIndicator2.Redraw();

cursor.Show();
display.render();
}
else if ( le.MouseClickLeft( moveArtifactsToHero1.area() ) ) {
moveArtifacts( heroes2.GetBagArtifacts(), GetBagArtifacts() );

selectArtifacts1.ResetSelected();
selectArtifacts2.ResetSelected();
selectArtifacts1.Redraw();
selectArtifacts2.Redraw();

backPrimary.restore();
RedrawPrimarySkillInfo( cur_pt, &primskill_bar1, &primskill_bar2 );
moraleIndicator1.Redraw();
moraleIndicator2.Redraw();
luckIndicator1.Redraw();
luckIndicator2.Redraw();

cursor.Show();
display.render();
}
Expand Down