Skip to content

Commit 4a7843a

Browse files
authored
Merge branch 'master' into lobby-dsk-control-id-codestyle
2 parents 60e3bf8 + f070ce5 commit 4a7843a

15 files changed

Lines changed: 337 additions & 36 deletions

external/s25update

libs/common/include/Point.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <limits>
1111
#include <type_traits>
1212

13+
template<typename Source, typename Target>
14+
constexpr bool is_float_to_int_v = std::conjunction_v<std::is_floating_point<Source>, std::is_integral<Target>>;
15+
1316
/// Type for describing a 2D value (position, size, offset...)
1417
/// Note: Combining a signed with an unsigned point will result in a signed type!
1518
/// Allowed operations:
@@ -35,15 +38,15 @@ struct Point //-V690
3538
T x, y;
3639
constexpr Point() noexcept : x(getInvalidValue()), y(getInvalidValue()) {}
3740
constexpr Point(const T x, const T y) noexcept : x(x), y(y) {}
38-
template<typename U, std::enable_if_t<!(std::is_integral_v<T> && std::is_floating_point_v<U>), int> = 0>
41+
template<typename U, std::enable_if_t<!is_float_to_int_v<U, T>, int> = 0>
3942
constexpr explicit Point(const Point<U>& pt) noexcept : x(static_cast<T>(pt.x)), y(static_cast<T>(pt.y))
4043
{}
4144
/// Convert floating-point to integer by truncating
42-
template<typename U, std::enable_if_t<std::is_integral_v<T> && std::is_floating_point_v<U>, int> = 0>
45+
template<typename U, std::enable_if_t<is_float_to_int_v<U, T>, int> = 0>
4346
constexpr explicit Point(Truncate_t, const Point<U>& pt) noexcept : x(static_cast<T>(pt.x)), y(static_cast<T>(pt.y))
4447
{}
4548
/// Convert floating-point to integer with rounding (default behavior)
46-
template<typename U, std::enable_if_t<std::is_integral_v<T> && std::is_floating_point_v<U>, int> = 0>
49+
template<typename U, std::enable_if_t<is_float_to_int_v<U, T>, int> = 0>
4750
constexpr explicit Point(const Point<U>& pt) noexcept : x(helpers::iround<T>(pt.x)), y(helpers::iround<T>(pt.y))
4851
{}
4952
constexpr Point(const Point&) = default;

libs/common/include/helpers/mathFuncs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ constexpr T interpolate(const T startVal, const T endVal, const U elapsedTime, c
7070
}
7171

7272
/// Linear interpolation, similar to C++20's std::lerp()
73-
constexpr float lerp(const float startVal, const float endVal, const float ratio) noexcept
73+
template<typename T>
74+
constexpr T lerp(const T startVal, const T endVal, const T ratio) noexcept
7475
{
7576
return startVal + ratio * (endVal - startVal);
7677
}

libs/s25main/controls/ctrlBaseImage.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,44 @@ Rect ctrlBaseImage::GetImageRect() const
2020
return Rect(-img_->GetOrigin(), img_->GetSize());
2121
}
2222

23-
void ctrlBaseImage::DrawImage(const DrawPoint& pos) const
23+
void ctrlBaseImage::DrawImage(const Rect& dstArea) const
2424
{
25-
DrawImage(pos, modulationColor_);
25+
DrawImage(dstArea, modulationColor_);
2626
}
2727

28-
void ctrlBaseImage::DrawImage(const DrawPoint& pos, unsigned color) const
28+
void ctrlBaseImage::DrawImage(const Rect& dstArea, unsigned color) const
2929
{
30-
if(img_)
31-
img_->DrawFull(pos, color);
30+
if(img_ == nullptr)
31+
return;
32+
33+
auto dst = dstArea;
34+
auto imageSize = img_->GetSize();
35+
auto dstSize = dstArea.getSize();
36+
Rect srcArea = Rect(DrawPoint::all(0), imageSize);
37+
38+
if(imageSize.x > dstSize.x)
39+
{
40+
auto halfDelta = (imageSize.x - dstSize.x) / 2;
41+
srcArea.left += halfDelta;
42+
srcArea.right -= halfDelta;
43+
} else if(imageSize.x < dstSize.x)
44+
{
45+
auto halfDelta = (dstSize.x - imageSize.x) / 2;
46+
dst.left += halfDelta;
47+
dst.right -= halfDelta;
48+
}
49+
50+
if(imageSize.y > dstSize.y)
51+
{
52+
auto halfDelta = (imageSize.y - dstSize.y) / 2;
53+
srcArea.top += halfDelta;
54+
srcArea.bottom -= halfDelta;
55+
} else if(imageSize.y < dstSize.y)
56+
{
57+
auto halfDelta = (dstSize.y - imageSize.y) / 2;
58+
dst.top += halfDelta;
59+
dst.bottom -= halfDelta;
60+
}
61+
62+
img_->Draw(dst, srcArea, color);
3263
}

libs/s25main/controls/ctrlBaseImage.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ class ctrlBaseImage
2424
/// Swap the images of those controls
2525
void SwapImage(ctrlBaseImage& other);
2626
Rect GetImageRect() const;
27-
void DrawImage(const DrawPoint& pos) const;
28-
void DrawImage(const DrawPoint& pos, unsigned color) const;
27+
28+
/// Draw the image on specified rectangular area. The image is centered inside dstArea and cropped to its size.
29+
void DrawImage(const Rect& dstArea) const;
30+
void DrawImage(const Rect& dstArea, unsigned color) const;
2931

3032
private:
3133
ITexture* img_;

libs/s25main/controls/ctrlImage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ctrlImage::~ctrlImage() = default;
1717
*/
1818
void ctrlImage::Draw_()
1919
{
20-
DrawImage(GetDrawPos());
20+
DrawImage(Rect(GetDrawPos(), GetImageRect().getSize()));
2121
}
2222

2323
bool ctrlImage::Msg_MouseMove(const MouseCoords& mc)

libs/s25main/controls/ctrlImageButton.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#include "ctrlImageButton.h"
6+
#include <ogl/ITexture.h>
67

78
ctrlImageButton::ctrlImageButton(Window* parent, unsigned id, const DrawPoint& pos, const Extent& size,
89
const TextureColor tc, ITexture* const image, const std::string& tooltip)
@@ -11,11 +12,27 @@ ctrlImageButton::ctrlImageButton(Window* parent, unsigned id, const DrawPoint& p
1112

1213
void ctrlImageButton::DrawContent() const
1314
{
14-
DrawPoint pos = GetDrawPos() + DrawPoint(GetSize()) / 2;
15+
// Adding of origin compensates for its substraction inside ITexture::Draw()
16+
auto pos = GetDrawPos() + GetImage()->GetOrigin();
17+
auto size = GetSize();
18+
19+
if(hasBorder)
20+
{
21+
// Ensure that 3D border is not drawn on
22+
const unsigned borderThickness = 2;
23+
pos += DrawPoint::all(borderThickness);
24+
size -= Extent::all(2 * borderThickness);
25+
}
26+
1527
if((state == ButtonState::Pressed || isChecked) && isEnabled)
28+
{
1629
pos += DrawPoint::all(2);
30+
size -= Extent::all(2);
31+
}
32+
33+
Rect drawRect(pos, size);
1734
if(!isEnabled && GetModulationColor() == COLOR_WHITE)
18-
DrawImage(pos, 0xFF555555);
35+
DrawImage(drawRect, 0xFF555555);
1936
else
20-
DrawImage(pos);
37+
DrawImage(drawRect);
2138
}

libs/s25main/ogl/ITexture.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "Point.h"
8+
#include "Rect.h"
89

910
class ITexture
1011
{
@@ -15,4 +16,8 @@ class ITexture
1516
virtual Position GetOrigin() const = 0;
1617
virtual Extent GetSize() const = 0;
1718
virtual void DrawFull(const Position& dstPos, unsigned color = 0xFFFFFFFFu) = 0;
19+
20+
/// Draws portion of image specified by srcArea on area defined by dstArea.
21+
/// In case of srcArea and dstArea size mismatch, scaling will occur.
22+
virtual void Draw(Rect dstArea, Rect srcArea, unsigned color = 0xFFFFFFFFu) = 0;
1823
};

libs/s25main/ogl/glArchivItem_Bitmap.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class glArchivItem_Bitmap : public virtual libsiedler2::baseArchivItem_Bitmap, p
2626
void DrawPart(const Rect& destArea, const DrawPoint& offset, unsigned color = COLOR_WHITE);
2727
/// Draw a rectangular part of the texture from the origin of it
2828
void DrawPart(const Rect& destArea, unsigned color = COLOR_WHITE);
29-
/// Draw only percent% of the height of the image
29+
/// Draw only percent% of the height of the image, counting from the bottom of the image
3030
void DrawPercent(const DrawPoint& dstPos, unsigned percent, unsigned color = COLOR_WHITE);
31-
32-
protected:
3331
/// Draw the texture.
3432
/// src_w/h default to the full bitmap size
3533
/// dst_w/h default the src_w/h
36-
void Draw(Rect dstArea, Rect srcArea, unsigned color = COLOR_WHITE);
34+
void Draw(Rect dstArea, Rect srcArea, unsigned color = COLOR_WHITE) override;
35+
36+
protected:
3737
void FillTexture() override;
3838
Extent CalcTextureSize() const override;
3939
};

libs/s25main/ogl/glArchivItem_Bitmap_Player.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void glArchivItem_Bitmap_Player::drawForPlayer(const DrawPoint& dst, unsigned pl
4242
DrawFull(dst, COLOR_WHITE, playerColor);
4343
}
4444

45+
void glArchivItem_Bitmap_Player::Draw(Rect dstArea, Rect srcArea, unsigned color /*= COLOR_WHITE*/)
46+
{
47+
Draw(dstArea, srcArea, color, COLOR_WHITE);
48+
}
49+
4550
void glArchivItem_Bitmap_Player::Draw(Rect dstArea, Rect srcArea, unsigned color /*= COLOR_WHITE*/,
4651
unsigned player_color /*= COLOR_WHITE*/)
4752
{

0 commit comments

Comments
 (0)