-
-
Notifications
You must be signed in to change notification settings - Fork 418
feature: Add aspect ratio info to resolution selection dialog #6587
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
base: master
Are you sure you want to change the base?
Changes from all commits
61b239d
3455744
74f3ca1
7434338
9ac0956
8797fc6
ed1d0ca
84390bf
051eeff
8c8b656
72017d9
7a08f22
808833d
175c89e
707519e
a178aae
0272e03
330fdc1
fe61b37
1874370
61ef4a6
e26cde4
8f9f8a1
2c9db0c
b9a2b4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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 ) | ||
| { | ||
|
|
@@ -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 ) | ||
|
|
@@ -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 | ||
|
|
@@ -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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
|
|
||
| void ActionListDoubleClick( fheroes2::ResolutionInfo & /*unused*/ ) override | ||
| { | ||
| _isDoubleClicked = true; | ||
|
|
@@ -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(); | ||
Osmodium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const int32_t textOffsetX = dst.x + 41 + editBoxLength / 2 - leftTextSize - middleTextSize / 2; | ||
| const int32_t textOffsetX = dst.x + 41 + textPadding; | ||
Osmodium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const int32_t textOffsetY = dst.y + 287 + ( resolutionItemHeight - text.height() + textOffsetYCorrection ) / 2; | ||
Osmodium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.