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

[ShipsLogControl] Fix update failure with duplicate text #2018

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
10 changes: 9 additions & 1 deletion src/gui/gui2_advancedscrolltext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ GuiAdvancedScrollText::GuiAdvancedScrollText(GuiContainer* owner, string id)
scrollbar->setPosition(0, 0, sp::Alignment::TopRight)->setSize(50, GuiElement::GuiSizeMax);
}

GuiAdvancedScrollText* GuiAdvancedScrollText::addEntry(string prefix, string text, glm::u8vec4 color)
GuiAdvancedScrollText* GuiAdvancedScrollText::addEntry(string prefix, string text, glm::u8vec4 color, unsigned int seq)
{
entries.emplace_back();
entries.back().prefix = prefix;
entries.back().text = text;
entries.back().color = color;
entries.back().seq = seq;
return this;
}

Expand All @@ -28,6 +29,13 @@ string GuiAdvancedScrollText::getEntryText(int index) const
return entries[index].text;
}

unsigned int GuiAdvancedScrollText::getEntrySeq(unsigned int index) const
{
if (index >= getEntryCount())
return 0;
return entries[index].seq;
}

GuiAdvancedScrollText* GuiAdvancedScrollText::removeEntry(int index)
{
if (index < 0 || index > int(getEntryCount()))
Expand Down
4 changes: 3 additions & 1 deletion src/gui/gui2_advancedscrolltext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GuiAdvancedScrollText : public GuiElement
string prefix;
string text;
glm::u8vec4 color;
unsigned int seq;
};

std::vector<Entry> entries;
Expand All @@ -25,11 +26,12 @@ class GuiAdvancedScrollText : public GuiElement
GuiAdvancedScrollText* enableAutoScrollDown() { auto_scroll_down = true; return this; }
GuiAdvancedScrollText* disableAutoScrollDown() { auto_scroll_down = false; return this; }

GuiAdvancedScrollText* addEntry(string prefix, string text, glm::u8vec4 color);
GuiAdvancedScrollText* addEntry(string prefix, string text, glm::u8vec4 color, unsigned int seq);
GuiAdvancedScrollText* setTextSize(float text_size) { this->text_size = text_size; return this; }

unsigned int getEntryCount() const;
string getEntryText(int index) const;
unsigned int getEntrySeq(unsigned int index) const;
GuiAdvancedScrollText* removeEntry(int index);
GuiAdvancedScrollText* clearEntries();

Expand Down
8 changes: 4 additions & 4 deletions src/screenComponents/shipsLogControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ void ShipsLog::onDraw(sp::RenderTarget& renderer)
log_text->removeEntry(0);
}

if (log_text->getEntryCount() > 0 && logs.size() > 0 && log_text->getEntryText(0) != logs[0].text)
if (log_text->getEntryCount() > 0 && logs.size() > 0 && log_text->getEntrySeq(0) != logs[0].seq)
{
bool updated = false;
for(unsigned int n=1; n<log_text->getEntryCount(); n++)
{
if (log_text->getEntryText(n) == logs[0].text)
if (log_text->getEntrySeq(n) == logs[0].seq)
{
for(unsigned int m=0; m<n; m++)
log_text->removeEntry(0);
Expand All @@ -59,7 +59,7 @@ void ShipsLog::onDraw(sp::RenderTarget& renderer)
while(log_text->getEntryCount() < logs.size())
{
int n = log_text->getEntryCount();
log_text->addEntry(logs[n].prefix, logs[n].text, logs[n].color);
log_text->addEntry(logs[n].prefix, logs[n].text, logs[n].color, logs[n].seq);
}
}else{
if (log_text->getEntryCount() > 0 && logs.size() == 0)
Expand All @@ -70,7 +70,7 @@ void ShipsLog::onDraw(sp::RenderTarget& renderer)
log_text->clearEntries();
}
if (log_text->getEntryCount() == 0 && logs.size() > 0)
log_text->addEntry(logs.back().prefix, logs.back().text, logs.back().color);
log_text->addEntry(logs.back().prefix, logs.back().text, logs.back().color, logs.back().seq);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/screens/extra/shipLogScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ void ShipLogScreen::onDraw(sp::RenderTarget& renderer)
log_text->removeEntry(0);
}

if (log_text->getEntryCount() > 0 && logs.size() > 0 && log_text->getEntryText(0) != logs[0].text)
if (log_text->getEntryCount() > 0 && logs.size() > 0 && log_text->getEntrySeq(0) != logs[0].seq)
{
bool updated = false;
for(unsigned int n=1; n<log_text->getEntryCount(); n++)
{
if (log_text->getEntryText(n) == logs[0].text)
if (log_text->getEntrySeq(n) == logs[0].seq)
{
for(unsigned int m=0; m<n; m++)
log_text->removeEntry(0);
Expand All @@ -60,7 +60,7 @@ void ShipLogScreen::onDraw(sp::RenderTarget& renderer)
while(log_text->getEntryCount() < logs.size())
{
int n = log_text->getEntryCount();
log_text->addEntry(logs[n].prefix, logs[n].text, logs[n].color);
log_text->addEntry(logs[n].prefix, logs[n].text, logs[n].color, logs[n].seq);
}
}
}
6 changes: 3 additions & 3 deletions src/spaceObjects/playerSpaceship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ string alertLevelToLocaleString(EAlertLevel level)
}

// Configure ship's log packets.
static inline sp::io::DataBuffer& operator << (sp::io::DataBuffer& packet, const PlayerSpaceship::ShipLogEntry& e) { return packet << e.prefix << e.text << e.color.r << e.color.g << e.color.b << e.color.a; }
static inline sp::io::DataBuffer& operator >> (sp::io::DataBuffer& packet, PlayerSpaceship::ShipLogEntry& e) { packet >> e.prefix >> e.text >> e.color.r >> e.color.g >> e.color.b >> e.color.a; return packet; }
static inline sp::io::DataBuffer& operator << (sp::io::DataBuffer& packet, const PlayerSpaceship::ShipLogEntry& e) { return packet << e.prefix << e.text << e.color.r << e.color.g << e.color.b << e.color.a << e.seq; }
static inline sp::io::DataBuffer& operator >> (sp::io::DataBuffer& packet, PlayerSpaceship::ShipLogEntry& e) { packet >> e.prefix >> e.text >> e.color.r >> e.color.g >> e.color.b >> e.color.a >> e.seq; return packet; }

REGISTER_MULTIPLAYER_CLASS(PlayerSpaceship, "PlayerSpaceship");
PlayerSpaceship::PlayerSpaceship()
Expand Down Expand Up @@ -1123,7 +1123,7 @@ void PlayerSpaceship::addToShipLog(string message, glm::u8vec4 color)
ships_log.erase(ships_log.begin());

// Timestamp a log entry, color it, and add it to the end of the log.
ships_log.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color);
ships_log.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, last_log_seq++);
}

void PlayerSpaceship::addToShipLogBy(string message, P<SpaceObject> target)
Expand Down
6 changes: 4 additions & 2 deletions src/spaceObjects/playerSpaceship.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ class PlayerSpaceship : public SpaceShip
string prefix;
string text;
glm::u8vec4 color;
unsigned int seq;

ShipLogEntry() {}
ShipLogEntry(string prefix, string text, glm::u8vec4 color)
: prefix(prefix), text(text), color(color) {}
ShipLogEntry(string prefix, string text, glm::u8vec4 color, unsigned int seq)
: prefix(prefix), text(text), color(color), seq(seq) {}

bool operator!=(const ShipLogEntry& e) { return prefix != e.prefix || text != e.text || color != e.color; }
};
Expand Down Expand Up @@ -114,6 +115,7 @@ class PlayerSpaceship : public SpaceShip
std::vector<ShipLogEntry> ships_log;
float energy_shield_use_per_second = default_energy_shield_use_per_second;
float energy_warp_per_second = default_energy_warp_per_second;
unsigned int last_log_seq = 0;
public:
std::vector<CustomShipFunction> custom_functions;

Expand Down
Loading