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

Extend button resizing to include height #8562

Merged
merged 25 commits into from
Sep 18, 2024
Merged
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f9d11d8
Button height
zenseii Mar 18, 2024
0f0bef9
Initial draft of the extended button resize function
zenseii Apr 9, 2024
11d22de
Address clang analyzer
zenseii Apr 9, 2024
3fe0be0
Add heightening and widening buttons
zenseii Aug 27, 2024
fb493be
Code style
zenseii Aug 27, 2024
073e742
Typo
zenseii Aug 27, 2024
66a8407
Merge remote-tracking branch 'upstream/master' into button-height-resize
zenseii Aug 27, 2024
b2a0691
first run of fine-tuning button properties
zenseii Aug 28, 2024
568bfd5
Paraphrase comment
zenseii Aug 28, 2024
3abeeef
Add last button specific values
zenseii Aug 28, 2024
14c15d8
Increase max button size
zenseii Aug 28, 2024
8c5069e
Reduce the vertical empty button by 1px
zenseii Aug 28, 2024
a0c0051
Small adjustments to unify good and evil
zenseii Aug 28, 2024
66d734a
Code style
zenseii Aug 28, 2024
09d18b6
Remove Todos, add assertions
zenseii Aug 28, 2024
e2742cd
Get button background color from central point
zenseii Aug 30, 2024
95cc0da
Avoid widening of vertical buttons
zenseii Aug 30, 2024
7ae834a
Fine tune vertical button height
zenseii Aug 31, 2024
93c5c1b
Automatic center alignment of vertical buttons
zenseii Aug 31, 2024
e527353
Readd empty line
zenseii Sep 1, 2024
29b53b9
new height and width assertion
zenseii Sep 1, 2024
e843381
Use cached original width and height
zenseii Sep 5, 2024
849f702
Actually rename centralPlace to centralPosition
zenseii Sep 5, 2024
7a4e255
move resize and reset calls
zenseii Sep 5, 2024
d3d39ae
Cache released state buttons
zenseii Sep 5, 2024
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
134 changes: 123 additions & 11 deletions src/fheroes2/gui/ui_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,107 @@

namespace
{
// TODO: Replace the other resizeButton() function with this one.
fheroes2::Image resizeButton( const fheroes2::Image & original, const int32_t width, const int32_t height )
{
assert( height > 0 );

fheroes2::Image output;
output.resize( width, height );
output.reset();

const int32_t originalWidth = original.width();
const int32_t originalHeight = original.height();
if ( originalHeight == height && originalWidth == width ) {
fheroes2::Copy( original, output );
}
else if ( originalHeight >= height && originalWidth >= width ) {
fheroes2::Copy( original, 0, 0, output, 0, 0, width / 2, height / 2 );
const int32_t secondHalfHeight = height - height / 2;
const int32_t secondHalfWidth = width - width / 2;
fheroes2::Copy( original, 0, originalHeight - secondHalfHeight, output, 0, height - secondHalfHeight, secondHalfWidth, secondHalfHeight );
fheroes2::Copy( original, originalWidth - secondHalfWidth, 0, output, width - secondHalfWidth, 0, secondHalfWidth, secondHalfHeight );
fheroes2::Copy( original, originalWidth - secondHalfWidth, originalHeight - secondHalfHeight, output, width - secondHalfWidth, height - secondHalfHeight,
secondHalfWidth, secondHalfHeight );
}
// TODO: Add if condition for no change in height but change in width, or vice-versa, compared to original button.
else {
const int32_t middleWidth = originalWidth / 3;
const int32_t overallMiddleWidth = width - middleWidth * 2;
const int32_t middleWidthCount = overallMiddleWidth / middleWidth;
const int32_t middleWidthLeftOver = overallMiddleWidth - middleWidthCount * middleWidth;

const int32_t middleHeight = originalHeight / 3;
const int32_t overallMiddleHeight = height - middleHeight * 2;
const int32_t middleHeightCount = overallMiddleHeight / middleHeight;
const int32_t middleHeightLeftOver = overallMiddleHeight - middleHeightCount * middleHeight;

fheroes2::Copy( original, 0, 0, output, 0, 0, middleWidth, middleHeight );

int32_t offsetY = middleHeight;
for ( int32_t i = 0; i < middleHeightCount; ++i ) {
fheroes2::Copy( original, 0, middleHeight, output, 0, offsetY, middleWidth, middleHeight );
offsetY += middleHeight;
}

if ( middleHeightLeftOver > 0 ) {
fheroes2::Copy( original, 0, middleHeight, output, 0, offsetY, middleWidth, middleHeightLeftOver );
zenseii marked this conversation as resolved.
Show resolved Hide resolved
offsetY += middleHeightLeftOver;
}

const int32_t leftBottomPart = originalHeight - middleHeight * 2;

fheroes2::Copy( original, 0, originalHeight - leftBottomPart, output, 0, offsetY, middleWidth, leftBottomPart );

int32_t offsetX = middleWidth;
for ( int32_t i = 0; i < middleWidthCount; ++i ) {
fheroes2::Copy( original, middleWidth, 0, output, offsetX, 0, middleWidth, middleHeight );
offsetX += middleWidth;
}

if ( middleWidthLeftOver > 0 ) {
fheroes2::Copy( original, middleWidth, 0, output, offsetX, 0, middleWidthLeftOver, middleHeight );
zenseii marked this conversation as resolved.
Show resolved Hide resolved
offsetX += middleWidthLeftOver;
}

const int32_t rightPartWidth = originalWidth - middleWidth * 2;

fheroes2::Copy( original, originalWidth - rightPartWidth, 0, output, offsetX, 0, rightPartWidth, middleHeight );

// TODO: Rename this variable, or better unify the for loops.
int32_t offsetY2 = middleHeight;
for ( int32_t i = 0; i < middleHeightCount; ++i ) {
fheroes2::Copy( original, originalWidth - rightPartWidth, middleHeight, output, offsetX, offsetY2, middleWidth, middleHeight );
offsetY2 += middleHeight;
}

if ( middleHeightLeftOver > 0 ) {
fheroes2::Copy( original, originalWidth - rightPartWidth, middleHeight, output, offsetX, offsetY2, middleWidth, middleHeightLeftOver );
zenseii marked this conversation as resolved.
Show resolved Hide resolved
offsetY2 += middleHeightLeftOver;
}

const int32_t rightPartHeight = originalHeight - middleHeight * 2;

fheroes2::Copy( original, originalWidth - rightPartWidth, originalHeight - rightPartHeight, output, offsetX, offsetY, rightPartWidth, rightPartHeight );
// TODO: Rename this variable, or better unify the for loops.
int32_t offsetX2 = middleWidth;
for ( int32_t i = 0; i < middleWidthCount; ++i ) {
fheroes2::Copy( original, middleWidth, originalHeight - rightPartHeight, output, offsetX2, offsetY, middleWidth, originalHeight - rightPartHeight );
offsetX2 += middleWidth;
}

if ( middleWidthLeftOver > 0 ) {
fheroes2::Copy( original, middleWidth, originalHeight - rightPartHeight, output, offsetX2, offsetY, middleWidthLeftOver,
zenseii marked this conversation as resolved.
Show resolved Hide resolved
originalHeight - rightPartHeight );
offsetX2 += middleWidthLeftOver;
}

// Step 9 Todo: Get the button filling color according to Evil/Good interface setting and according to pressed and released state.
fheroes2::Fill( output, middleWidth, middleHeight, offsetX - middleWidth, offsetY - middleHeight, fheroes2::GetColorId( 216, 184, 152 ) );
}

return output;
}
fheroes2::Image resizeButton( const fheroes2::Image & original, const int32_t width )
{
const int32_t height = original.height();
Expand Down Expand Up @@ -653,13 +754,17 @@ namespace fheroes2
void getTextAdaptedButton( Sprite & released, Sprite & pressed, const char * text, const int emptyButtonIcnID, const int buttonBackgroundIcnID )
{
fheroes2::FontColor buttonFont = fheroes2::FontColor::WHITE;
int32_t textAreaBorder = 0;
// TODO: Change the pairs below to fheroes2::Point or fheroes2::Size
int32_t textAreaXBorder = 0;
int32_t textAreaYBorder = 3;
zenseii marked this conversation as resolved.
Show resolved Hide resolved
int32_t minimumTextAreaWidth = 0;
int32_t backgroundBorders = 0;
int32_t minimumTextAreaHeight = 15;
zenseii marked this conversation as resolved.
Show resolved Hide resolved
int32_t backgroundBordersX = 0;
int32_t backgroundBordersY = 7;
zenseii marked this conversation as resolved.
Show resolved Hide resolved
fheroes2::Point releasedOffset = {};
fheroes2::Point pressedOffset = {};

getButtonSpecificValues( emptyButtonIcnID, buttonFont, textAreaBorder, minimumTextAreaWidth, backgroundBorders, releasedOffset, pressedOffset );
getButtonSpecificValues( emptyButtonIcnID, buttonFont, textAreaXBorder, minimumTextAreaWidth, backgroundBordersX, releasedOffset, pressedOffset );

const fheroes2::FontType releasedButtonFont{ fheroes2::FontSize::BUTTON_RELEASED, buttonFont };

Expand All @@ -675,15 +780,22 @@ namespace fheroes2
const int32_t textWidth = releasedText.width( maximumTextAreaWidth );
assert( textWidth > 0 );

const int32_t borderedTextWidth = textWidth + textAreaBorder;
const int32_t borderedTextWidth = textWidth + textAreaXBorder;

const int32_t textAreaWidth = std::clamp( borderedTextWidth, minimumTextAreaWidth, maximumTextAreaWidth );
assert( textAreaWidth + backgroundBordersX > 0 );

const int32_t textHeight = releasedText.height( textAreaWidth );
assert( textHeight > 0 );

const int32_t borderedTextHeight = textHeight + textAreaYBorder;
const int32_t maximumTextAreaHeight = 200; // Placeholder maximum height. Not sure what height is meaningful.
const int32_t textAreaHeight = std::clamp( borderedTextHeight, minimumTextAreaHeight, maximumTextAreaHeight );

assert( textAreaWidth + backgroundBorders > 0 );
assert( textAreaHeight + backgroundBordersY > 0 );

// TODO: Make resizeButton() scale in vertical direction too, for vertical and larger buttons.
released = resizeButton( AGG::GetICN( emptyButtonIcnID, 0 ), textAreaWidth + backgroundBorders );
pressed = resizeButton( AGG::GetICN( emptyButtonIcnID, 1 ), textAreaWidth + backgroundBorders );
released = resizeButton( AGG::GetICN( emptyButtonIcnID, 0 ), textAreaWidth + backgroundBordersX, textAreaHeight + backgroundBordersY );
pressed = resizeButton( AGG::GetICN( emptyButtonIcnID, 1 ), textAreaWidth + backgroundBordersX, textAreaHeight + backgroundBordersY );

if ( buttonBackgroundIcnID != ICN::UNKNOWN ) {
makeTransparentBackground( released, pressed, buttonBackgroundIcnID );
Expand All @@ -693,9 +805,9 @@ namespace fheroes2
const fheroes2::Size pressedTextSize( pressedText.width( textAreaWidth ), pressedText.height( textAreaWidth ) );

// The button font letters are all shifted 1 pixel to the left due to shadows, so we have to add 1 to the x position when drawing
// to properly center-align
releasedText.draw( releasedOffset.x + 1, releasedOffset.y + ( releasedText.height() - releasedTextSize.height ) / 2, textAreaWidth, released );
pressedText.draw( pressedOffset.x + 1, pressedOffset.y + ( pressedText.height() - pressedTextSize.height ) / 2, textAreaWidth, pressed );
// to properly center-align.
releasedText.draw( releasedOffset.x + 1, ( released.height() - releasedTextSize.height ) / 2, textAreaWidth, released );
pressedText.draw( pressedOffset.x + 1, ( pressed.height() - pressedTextSize.height ) / 2, textAreaWidth, pressed );
}

void makeButtonSprites( Sprite & released, Sprite & pressed, const std::string & text, const int32_t buttonWidth, const bool isEvilInterface,
Expand Down
Loading