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

Portrait support #1534

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion doc/lua/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ Closes a spot kicking any player or AI there.
Add an AI or change its difficulty.

**SetName(name)**
Change the players name.
Change the player's name.

**SetPortrait(portraitIndex)**
Change the player's portrait.

[Back](#Lua-objects-and-their-methods)

Expand Down
5 changes: 4 additions & 1 deletion libs/s25main/BasePlayerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
#include "s25util/colors.h"

BasePlayerInfo::BasePlayerInfo()
: ps(PlayerState::Free), nation(Nation::Romans), color(PLAYER_COLORS[0]), team(Team::None)
: ps(PlayerState::Free), portraitIndex(0), nation(Nation::Romans), color(PLAYER_COLORS[0]), team(Team::None)
{}

BasePlayerInfo::BasePlayerInfo(Serializer& ser, bool lightData)
: ps(helpers::popEnum<PlayerState>(ser)), aiInfo(!lightData || ps == PlayerState::AI ? ser : AI::Info())
{
if(lightData && !isUsed())
{
portraitIndex = 0;
nation = Nation::Romans;
team = Team::None;
color = PLAYER_COLORS[0];
} else
{
name = ser.PopLongString();
portraitIndex = ser.PopUnsignedInt();
nation = helpers::popEnum<Nation>(ser);
color = ser.PopUnsignedInt();
team = helpers::popEnum<Team>(ser);
Expand All @@ -36,6 +38,7 @@ void BasePlayerInfo::Serialize(Serializer& ser, bool lightData) const
if(!lightData || ps == PlayerState::AI)
aiInfo.serialize(ser);
ser.PushLongString(name);
ser.PushUnsignedInt(portraitIndex);
tehKaiN marked this conversation as resolved.
Show resolved Hide resolved
helpers::pushEnum<uint8_t>(ser, nation);
ser.PushUnsignedInt(color);
helpers::pushEnum<uint8_t>(ser, team);
Expand Down
1 change: 1 addition & 0 deletions libs/s25main/BasePlayerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct BasePlayerInfo
PlayerState ps;
AI::Info aiInfo;
std::string name;
unsigned portraitIndex;
Nation nation;
/// Actual color (ARGB)
unsigned color;
Expand Down
5 changes: 5 additions & 0 deletions libs/s25main/GameLobbyController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,8 @@ void GameLobbyController::SetName(unsigned playerIdx, const std::string& name)
{
mainPlayer_.sendMsgAsync(new GameMessage_Player_Name(playerIdx, name));
}

void GameLobbyController::SetPortrait(unsigned playerIdx, unsigned portraitIndex)
{
mainPlayer_.sendMsgAsync(new GameMessage_Player_Portrait(playerIdx, portraitIndex));
}
1 change: 1 addition & 0 deletions libs/s25main/GameLobbyController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GameLobbyController : public IGameLobbyController
void SetPlayerState(unsigned playerIdx, PlayerState state, const AI::Info& aiInfo) override;
void TogglePlayerState(unsigned playerIdx);
void SetName(unsigned playerIdx, const std::string& name) override;
void SetPortrait(unsigned playerIdx, unsigned portraitIndex) override;
void SetColor(unsigned playerIdx, unsigned newColor) override;
void SetTeam(unsigned playerIdx, Team newTeam) override;
void SetNation(unsigned playerIdx, Nation newNation) override;
Expand Down
3 changes: 2 additions & 1 deletion libs/s25main/SerializedGameData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
/// 7: Use helpers::push/popContainer (uses var size)
/// 8: noFlag::Wares converted to static_vector
/// 9: Drop serialization of node BQ
static const unsigned currentGameDataVersion = 9;
/// 10: Add portrait index serialization in player state
static const unsigned currentGameDataVersion = 10;
// clang-format on

std::unique_ptr<GameObject> SerializedGameData::Create_GameObject(const GO_Type got, const unsigned obj_id)
Expand Down
1 change: 1 addition & 0 deletions libs/s25main/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void Settings::LoadDefaults()
// {

lobby.name = System::getUserName();
lobby.portraitIndex = 0;
lobby.password.clear();
lobby.save_password = false;
// }
Expand Down
1 change: 1 addition & 0 deletions libs/s25main/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Settings : public Singleton<Settings, SingletonPolicies::WithLongevity>
struct
{
std::string name;
unsigned portraitIndex;
std::string password;
bool save_password;
} lobby;
Expand Down
41 changes: 36 additions & 5 deletions libs/s25main/controls/ctrlBaseImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,44 @@ Rect ctrlBaseImage::GetImageRect() const
return Rect(-img_->GetOrigin(), img_->GetSize());
}

void ctrlBaseImage::DrawImage(const DrawPoint& pos) const
void ctrlBaseImage::DrawImage(const Rect& dstArea) const
{
DrawImage(pos, modulationColor_);
DrawImage(dstArea, modulationColor_);
}

void ctrlBaseImage::DrawImage(const DrawPoint& pos, unsigned color) const
void ctrlBaseImage::DrawImage(const Rect& dstArea, unsigned color) const
{
if(img_)
img_->DrawFull(pos, color);
if(img_ == nullptr)
return;

auto dst = dstArea;
auto imageSize = img_->GetSize();
auto dstSize = dstArea.getSize();
Rect srcArea = Rect(DrawPoint::all(0), imageSize);

if(imageSize.x > dstSize.x)
{
auto halfDelta = (imageSize.x - dstSize.x) / 2;
srcArea.left += halfDelta;
srcArea.right -= halfDelta;
} else if(imageSize.x < dstSize.x)
{
auto halfDelta = (dstSize.x - imageSize.x) / 2;
dst.left += halfDelta;
dst.right -= halfDelta;
}

if(imageSize.y > dstSize.y)
{
auto halfDelta = (imageSize.y - dstSize.y) / 2;
srcArea.top += halfDelta;
srcArea.bottom -= halfDelta;
} else if(imageSize.y < dstSize.y)
{
auto halfDelta = (dstSize.y - imageSize.y) / 2;
dst.top += halfDelta;
dst.bottom -= halfDelta;
}

img_->Draw(dst, srcArea, color);
}
4 changes: 2 additions & 2 deletions libs/s25main/controls/ctrlBaseImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class ctrlBaseImage
/// Swap the images of those controls
void SwapImage(ctrlBaseImage& other);
Rect GetImageRect() const;
void DrawImage(const DrawPoint& pos) const;
void DrawImage(const DrawPoint& pos, unsigned color) const;
void DrawImage(const Rect& dstArea) const;
void DrawImage(const Rect& dstArea, unsigned color) const;

private:
ITexture* img_;
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/controls/ctrlImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ctrlImage::~ctrlImage() = default;
*/
void ctrlImage::Draw_()
{
DrawImage(GetDrawPos());
DrawImage(Rect(GetDrawPos(), GetImageRect().getSize()));
}

bool ctrlImage::Msg_MouseMove(const MouseCoords& mc)
Expand Down
13 changes: 10 additions & 3 deletions libs/s25main/controls/ctrlImageButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ ctrlImageButton::ctrlImageButton(Window* parent, unsigned id, const DrawPoint& p

void ctrlImageButton::DrawContent() const
{
DrawPoint pos = GetDrawPos() + DrawPoint(GetSize()) / 2;
Copy link
Member

Choose a reason for hiding this comment

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

This is quite a major change: Previously it drew the image in full size centered and potentially offset by 2 pixels.

Are you sure that all images used for buttons have the origin correctly set? And why the offset of 2 or 4 pixels and size reduction by 4 or 6 pixels? That seems to be new and at least the latter (offset/size change) can use a comment in the code on why and/or a constant for why it is 2/4/6

Copy link
Contributor Author

Choose a reason for hiding this comment

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

answered in split PR

DrawPoint pos = GetDrawPos() - GetImageRect().getOrigin() + DrawPoint::all(2);
Extent size = GetSize() - Extent::all(4);

if((state == ButtonState::Pressed || isChecked) && isEnabled)
{
pos += DrawPoint::all(2);
size -= Extent::all(2);
}

Rect drawRect(pos, size);
if(!isEnabled && GetModulationColor() == COLOR_WHITE)
DrawImage(pos, 0xFF555555);
DrawImage(drawRect, 0xFF555555);
else
DrawImage(pos);
DrawImage(drawRect);
}
Loading