Skip to content

Commit

Permalink
[ShipsLogControl] Fix update failure with duplicate text (daid#2018)
Browse files Browse the repository at this point in the history
* Add a sequence # to log messages
* Store seq in GuiAdvancedScrollText along with rest of log data
* Compare seq instead of text when sync'ing ScrollText to log
* Fixes Issue daid#1947
  • Loading branch information
csibbitt authored and Tsht committed Nov 22, 2023
1 parent 3ba3889 commit 7890ed9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
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{
log_text->setMargins(15, 12, 15, 0);
Expand All @@ -85,7 +85,7 @@ void ShipsLog::onDraw(sp::RenderTarget& renderer)
log_text->clearEntries();
}
if (log_text->getEntryCount() == 0 && logs.size() > 0)
log_text->addEntry("LOG : ", logs.back().text, logs.back().color);
log_text->addEntry("LOG : ", logs.back().text, logs.back().color, logs.back().seq);
// log_text->addEntry(logs.back().prefix, logs.back().text, logs.back().color);
}
}
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 @@ -39,12 +39,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 @@ -59,7 +59,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);
}
}
}
12 changes: 6 additions & 6 deletions src/spaceObjects/playerSpaceship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,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 << e.station; }
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.station; 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.station << 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.station; return packet >> e.seq; }

REGISTER_MULTIPLAYER_CLASS(PlayerSpaceship, "PlayerSpaceship");
PlayerSpaceship::PlayerSpaceship()
Expand Down Expand Up @@ -1435,31 +1435,31 @@ void PlayerSpaceship::addToSpecificShipLog(string message, glm::u8vec4 color, st
if (ships_log_generic.size() > 100)
ships_log_generic.erase(ships_log_generic.begin());
// Timestamp a log entry, color it, and add it to the end of the log.
ships_log_generic.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station);
ships_log_generic.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station, last_log_seq++);
timer_log_generic = 6;
}
else if (station == "intern")
{
if (ships_log_intern.size() > 100)
ships_log_intern.erase(ships_log_intern.begin());
// Timestamp a log entry, color it, and add it to the end of the log.
ships_log_intern.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station);
ships_log_intern.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station, last_log_seq++);
timer_log_intern = 6;
}
else if (station == "docks")
{
if (ships_log_docks.size() > 100)
ships_log_docks.erase(ships_log_docks.begin());
// Timestamp a log entry, color it, and add it to the end of the log.
ships_log_docks.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station);
ships_log_docks.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station, last_log_seq++);
timer_log_docks = 6;
}
else if (station == "science")
{
if (ships_log_science.size() > 100)
ships_log_science.erase(ships_log_science.begin());
// Timestamp a log entry, color it, and add it to the end of the log.
ships_log_science.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station);
ships_log_science.emplace_back(gameGlobalInfo->getMissionTime() + string(": "), message, color, station, last_log_seq++);
timer_log_science = 6;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/spaceObjects/playerSpaceship.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class PlayerSpaceship : public SpaceShip
string station;

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

bool operator!=(const ShipLogEntry& e) { return prefix != e.prefix || text != e.text || color != e.color || station != e.station; }
};
Expand Down Expand Up @@ -140,6 +140,7 @@ class PlayerSpaceship : public SpaceShip

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:
ESystem auto_repairing_system;
std::vector<CustomShipFunction> custom_functions;
Expand Down

0 comments on commit 7890ed9

Please sign in to comment.