Skip to content
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
61b239d
Add inline GCD calulation
Osmodium Feb 6, 2023
3455744
Add aspect ratio information to dialog_resolution.cpp
Osmodium Feb 6, 2023
74f3ca1
Use std::gcd from numeric instead of own implementation
Osmodium Feb 7, 2023
7434338
Remove unused variables
Osmodium Feb 7, 2023
9ac0956
Amend
Osmodium Feb 7, 2023
8797fc6
Rearrange include and remove trailing whitespaces
Osmodium Feb 7, 2023
ed1d0ca
Arrange includes
Osmodium Feb 11, 2023
84390bf
Show scale on the left, resolution in the middle and aspect ratio to …
Osmodium Feb 12, 2023
051eeff
New format with approximation of ratios
Osmodium Feb 13, 2023
8c8b656
Adjust factor left and ratio right
Osmodium Feb 13, 2023
72017d9
Fix formatting
Osmodium Feb 13, 2023
7a08f22
Fix includes
Osmodium Feb 13, 2023
808833d
Update src/fheroes2/dialog/dialog_resolution.cpp
Osmodium Feb 13, 2023
175c89e
Remove cmath
Osmodium Feb 13, 2023
707519e
Another attempt at fixing includes for IWYU configuration
Osmodium Feb 13, 2023
a178aae
Add cmath again
Osmodium Feb 13, 2023
0272e03
Text adjust 1/3 from each side
Osmodium Feb 14, 2023
330fdc1
Remove unused padding
Osmodium Feb 15, 2023
fe61b37
Reintroduced two column format
Osmodium Feb 21, 2023
1874370
Implement hidden
Osmodium Feb 21, 2023
61ef4a6
Comment name parameters
Osmodium Feb 21, 2023
e26cde4
Remove override method
Osmodium Apr 4, 2023
8f9f8a1
Reintroduced newline
Osmodium Apr 4, 2023
2c9db0c
Refactor aspect ratio approximation method
Osmodium Apr 4, 2023
b9a2b4a
Reintroduced ActionListDoubleClick to satisfy Clang
Osmodium Apr 4, 2023
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
60 changes: 49 additions & 11 deletions src/fheroes2/dialog/dialog_resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <cstddef>
#include "dialog_resolution.h"

#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <utility>
#include <vector>

#include "agg_image.h"
#include "cursor.h"
#include "dialog_resolution.h"
#include "embedded_image.h"
#include "game_hotkeys.h"
#include "gamedefs.h"
Expand All @@ -50,6 +53,8 @@ namespace
// TODO: this is a hack over partially incorrect text height calculation. Fix it later together with the Text classes.
const int32_t textOffsetYCorrection = 6;
const std::string middleText = " x ";
const int32_t textPadding = 6;
const int32_t aspectRatioApproximationIterations = 100;

std::pair<std::string, std::string> getResolutionStrings( const fheroes2::ResolutionInfo & resolution )
{
Expand All @@ -60,12 +65,38 @@ namespace
return std::make_pair( std::to_string( resolution.width ), std::to_string( resolution.height ) );
}

std::string getAspectRatio( const fheroes2::ResolutionInfo & resolution )
{
const double ratio = static_cast<double>( resolution.width ) / static_cast<double>( resolution.height );
double bestDelta = DBL_MAX;
int32_t width = 1;
int32_t height = 1;
int32_t bestWidth = 0;
int32_t bestHeight = 0;

for ( int32_t iterations = 0; iterations < aspectRatioApproximationIterations; iterations++ ) {
if ( const double delta = static_cast<double>( width ) / static_cast<double>( height ) - ratio; delta < 0 ) {
width++;
}
else {
height++;
}

if ( const std::double_t newDelta = std::abs( static_cast<double>( width ) / static_cast<double>( height ) - ratio ); newDelta < bestDelta ) {
bestDelta = newDelta;
bestWidth = width;
bestHeight = height;
}
}

return std::to_string( bestWidth ) + ":" + std::to_string( bestHeight );
}

class ResolutionList : public Interface::ListBox<fheroes2::ResolutionInfo>
{
public:
using Interface::ListBox<fheroes2::ResolutionInfo>::ActionListSingleClick;
using Interface::ListBox<fheroes2::ResolutionInfo>::ActionListPressRight;
using Interface::ListBox<fheroes2::ResolutionInfo>::ActionListDoubleClick;

explicit ResolutionList( const fheroes2::Point & offset )
: Interface::ListBox<fheroes2::ResolutionInfo>( offset )
Expand All @@ -79,15 +110,15 @@ namespace
const fheroes2::FontType fontType = current ? fheroes2::FontType::normalYellow() : fheroes2::FontType::normalWhite();

const auto [leftText, rightText] = getResolutionStrings( resolution );
const int32_t middleTextSize = fheroes2::Text( middleText, fontType ).width();
const int32_t leftTextSize = fheroes2::Text( leftText, fontType ).width();

const fheroes2::Text resolutionText( leftText + middleText + rightText, fontType );

const int32_t textOffsetX = offsetX + editBoxLength / 2 - leftTextSize - middleTextSize / 2;
const int32_t textOffsetX = offsetX + textPadding;
const int32_t textOffsetY = offsetY + ( resolutionItemHeight - resolutionText.height() + textOffsetYCorrection ) / 2;

resolutionText.draw( textOffsetX, textOffsetY, fheroes2::Display::instance() );

const fheroes2::Text aspectRatioText( getAspectRatio( resolution ), fontType );
aspectRatioText.draw( offsetX + editBoxLength - aspectRatioText.width() - textPadding, textOffsetY, fheroes2::Display::instance() );
}

void RedrawBackground( const fheroes2::Point & dst ) override
Expand Down Expand Up @@ -116,6 +147,11 @@ namespace
// Do nothing.
}

void ActionListDoubleClick( fheroes2::ResolutionInfo & item, const fheroes2::Point & /*unused*/, int32_t /*unused*/, int32_t /*unused*/ ) override
{
ActionListDoubleClick( item );
}
Comment on lines +150 to +153
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to override this method which calls another overridden method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was from some other implementation, just forgot to remove it.
e26cde4

Copy link
Contributor Author

@Osmodium Osmodium Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I remember, it was a clang error that told me to add it:
https://github.com/ihhub/fheroes2/actions/runs/4610597218/jobs/8149253282?pr=6587
Reintroduced here: b9a2b4a


void ActionListDoubleClick( fheroes2::ResolutionInfo & /*unused*/ ) override
{
_isDoubleClicked = true;
Expand All @@ -139,13 +175,15 @@ namespace
const fheroes2::FontType fontType = fheroes2::FontType::normalYellow();

const auto [leftText, rightText] = getResolutionStrings( resolution );
const int32_t middleTextSize = fheroes2::Text( middleText, fontType ).width();
const int32_t leftTextSize = fheroes2::Text( leftText, fontType ).width();

const int32_t textOffsetX = dst.x + 41 + editBoxLength / 2 - leftTextSize - middleTextSize / 2;
const int32_t textOffsetX = dst.x + 41 + textPadding;
const int32_t textOffsetY = dst.y + 287 + ( resolutionItemHeight - text.height() + textOffsetYCorrection ) / 2;

const fheroes2::Text resolutionText( leftText + middleText + rightText, fontType );
resolutionText.draw( textOffsetX, dst.y + 287 + ( resolutionItemHeight - text.height() + textOffsetYCorrection ) / 2, output );
resolutionText.draw( textOffsetX, textOffsetY, output );

const fheroes2::Text aspectRatioText( getAspectRatio( resolution ), fontType );
aspectRatioText.draw( dst.x + 41 + editBoxLength - aspectRatioText.width() - textPadding, textOffsetY, output );
}
}
}
Expand Down