Skip to content

Commit

Permalink
Chat log fix (copy and %%), crash on render gamepads fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FlavioFS committed Sep 12, 2021
1 parent d955deb commit ab89e23
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 46 deletions.
1 change: 1 addition & 0 deletions ParsecSoda/Commands/CommandDefaultMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ACommand.h"
#include "../Guest.h"
#include "../Tier.h"
#include "../Stringer.h"

class CommandDefaultMessage : public ACommand
{
Expand Down
8 changes: 6 additions & 2 deletions ParsecSoda/Hosting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,21 @@ void Hosting::handleMessage(const char* message, Guest& guest, bool isHost)

if (!defaultMessage.replyMessage().empty())
{
_chatLog.logMessage(defaultMessage.replyMessage());
broadcastChatMessage(defaultMessage.replyMessage());

string adjustedMessage = defaultMessage.replyMessage();
Stringer::replacePatternOnce(adjustedMessage, "%", "%%");
_chatLog.logMessage(adjustedMessage);

cout << endl << defaultMessage.replyMessage();
}
}

// Chatbot's command reply
if (!command->replyMessage().empty() && command->type() != COMMAND_TYPE::DEFAULT_MESSAGE)
{
_chatLog.logCommand(command->replyMessage());
broadcastChatMessage(command->replyMessage());
_chatLog.logCommand(command->replyMessage());
cout << endl << command->replyMessage();
_chatBot->setLastUserId();
}
Expand Down
23 changes: 23 additions & 0 deletions ParsecSoda/Stringer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,26 @@ void Stringer::replacePattern(string& source, string oldPattern, string newPatte
index++;
}
}

void Stringer::replacePatternOnce(string& source, string oldPattern, string newPattern)
{
size_t index = 0;
vector<size_t> positions;

while (true)
{
index = source.find(oldPattern, index);
if (index == std::string::npos) break;

positions.push_back(index);
index++;
}

size_t offset = 0;
int offsetSize = newPattern.size() - oldPattern.size();
for (int i = 0; i < positions.size(); i++)
{
source.replace(positions[offset] + (int)(offset * offsetSize), oldPattern.size(), newPattern);
offset++;
}
}
11 changes: 10 additions & 1 deletion ParsecSoda/Stringer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <cmath>
#include <vector>

#define STRINGER_MAX_WEIGHT (uint64_t)63
#define STRINGER_MAX_DISTANCE (uint64_t)(-1)
Expand Down Expand Up @@ -69,10 +70,18 @@ class Stringer
static int compareNoCase(const string a, const string b);

/**
* Replaces all occurrences of a pattern in a string with another pattern.
* Recursively replaces all occurrences of a pattern in a string with another pattern.
* @param source Original string reference to be edited.
* @param oldPattern Pattern to be replaced.
* @param newPattern Pattern to insert.
*/
static void replacePattern(string& source, string oldPattern, string newPattern);

/**
* Non-recursively replaces all occurrences of a pattern in a string with another pattern.
* @param source Original string reference to be edited.
* @param oldPattern Pattern to be replaced.
* @param newPattern Pattern to insert.
*/
static void replacePatternOnce(string& source, string oldPattern, string newPattern);
};
37 changes: 31 additions & 6 deletions ParsecSoda/Widgets/ChatWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ bool ChatWidget::render()
ImGui::BeginChild("Chat Log", ImVec2(size.x, size.y - 160));
for (; it != _chatLog.end(); ++it)
{
static ImVec2 textSize;
textSize = ImGui::CalcTextSize((*it).c_str());
static float textHeight;
cursor = ImGui::GetCursorPos();

ImGui::TextWrapped((*it).c_str());
textHeight = ImGui::GetCursorPosY() - cursor.y;

ImGui::SetCursorPos(cursor);
if (ImGui::Button(
(string() + "### Chat Message " + to_string(index)).c_str(),
ImVec2(size.x, textSize.y)
ImVec2(size.x, textHeight)
))
{
toClipboard((*it));
Expand All @@ -57,16 +59,34 @@ bool ChatWidget::render()
ImGui::Separator();

ImGui::BeginChild("Message Preview", ImVec2(size.x, 60));
ImGui::TextWrapped(_sendBuffer);
ImGui::TextWrapped(_previewBuffer);
ImGui::EndChild();

if (ImGui::IsWindowFocused() && !ImGui::IsAnyItemActive() && !ImGui::IsMouseClicked(0))
ImGui::SetKeyboardFocusHere(0);
ImGui::SetNextItemWidth(size.x);
try
{
strcpy_s(_lastBuffer, SEND_BUFFER_LEN, _sendBuffer);
}
catch (const std::exception&) {}
if (ImGui::InputText(" ", _sendBuffer, SEND_BUFFER_LEN, ImGuiInputTextFlags_EnterReturnsTrue))
{
sendMessage();
}
if (strcmp(_sendBuffer, _lastBuffer) != 0)
{
string newSendBuffer = _sendBuffer;
Stringer::replacePatternOnce(newSendBuffer, "%", "%%");

string check = newSendBuffer.substr(0, SEND_BUFFER_LEN-1);

try
{
strcpy_s(_previewBuffer, SEND_BUFFER_LEN, newSendBuffer.substr(0, SEND_BUFFER_LEN-1).c_str());
}
catch (const std::exception&) {}
}

cursor = ImGui::GetCursorPos();
ImGui::Dummy(ImVec2(0, 5));
Expand Down Expand Up @@ -126,6 +146,8 @@ bool ChatWidget::setSendBuffer(const char* value)
try
{
strcpy_s(_sendBuffer, SEND_BUFFER_LEN, value);
strcpy_s(_lastBuffer, SEND_BUFFER_LEN, value);
strcpy_s(_previewBuffer, SEND_BUFFER_LEN, value);
return true;
}
catch (const std::exception&)
Expand All @@ -138,14 +160,17 @@ bool ChatWidget::setSendBuffer(const char* value)

void ChatWidget::toClipboard(const string& message)
{
string adjustedMessage = message;
Stringer::replacePatternOnce(adjustedMessage, "%%", "%");

OpenClipboard(0);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, message.size());
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, adjustedMessage.size());
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), message.c_str(), message.size());
memcpy(GlobalLock(hg), adjustedMessage.c_str(), adjustedMessage.size());
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
Expand Down
2 changes: 2 additions & 0 deletions ParsecSoda/Widgets/ChatWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ChatWidget
// Attributes
string _logBuffer;
char _sendBuffer[SEND_BUFFER_LEN];
char _lastBuffer[SEND_BUFFER_LEN];
char _previewBuffer[SEND_BUFFER_LEN];
vector<string>& _chatLog;
size_t _messageCount;
};
Expand Down
66 changes: 30 additions & 36 deletions ParsecSoda/Widgets/GamepadsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,25 @@ bool GamepadsWidget::render()
ImGui::Separator();
ImGui::Dummy(ImVec2(0, 10));

static vector<Gamepad>::iterator gi;
gi = _gamepads.begin();
static int index;
index = 0;

for (; gi != _gamepads.end(); ++gi)
for (size_t i = 0; i < _gamepads.size(); ++i)
{
Gamepad& gi = _gamepads[i];
static uint32_t userID;
userID = (*gi).owner.guest.userID;
userID = gi.owner.guest.userID;

ImGui::BeginChild(
(string("##Gamepad " ) + to_string(index)).c_str(),
(string("##Gamepad " ) + to_string(i)).c_str(),
ImVec2(size.x, 50)
);

static ImVec2 cursor;
cursor = ImGui::GetCursorPos();

static int xboxIndex = 0, padIndex = 0;
xboxIndex = (int)(*gi).getIndex();
xboxIndex = (int)gi.getIndex();
padIndex = xboxIndex + 1;
static bool isIndexSuccess = false;
isIndexSuccess = (*gi).isConnected() && padIndex > 0 && padIndex <= 4;
isIndexSuccess = gi.isConnected() && padIndex > 0 && padIndex <= 4;

ImGui::BeginGroup();
if (isIndexSuccess)
Expand Down Expand Up @@ -110,25 +106,25 @@ bool GamepadsWidget::render()

if (IconButton::render(AppIcons::back, AppColors::primary))
{
(*gi).clearOwner();
gi.clearOwner();
}
TitleTooltipWidget::render("Strip gamepad", "Unlink current user from this gamepad.");

ImGui::SameLine();

//static ImVec4 colorOn;
//colorOn = padIndex > 0 ? AppColors::positive : AppColors::warning;
if (ToggleIconButtonWidget::render(AppIcons::padOn, AppIcons::padOff, (*gi).isConnected(), AppColors::positive))
if (ToggleIconButtonWidget::render(AppIcons::padOn, AppIcons::padOff, gi.isConnected(), AppColors::positive))
{
if ((*gi).isConnected())
if (gi.isConnected())
{
(*gi).disconnect();
gi.disconnect();
hasDisconnectedGamepads = true;
}
else
(*gi).connect();
gi.connect();
}
if ((*gi).isConnected()) TitleTooltipWidget::render("Connected gamepad", "Press to \"physically\" disconnect\nthis gamepad (at O.S. level).");
if (gi.isConnected()) TitleTooltipWidget::render("Connected gamepad", "Press to \"physically\" disconnect\nthis gamepad (at O.S. level).");
else TitleTooltipWidget::render("Disconnected gamepad", "Press to \"physically\" connect\nthis gamepad (at O.S. level).");

ImGui::SameLine();
Expand All @@ -137,7 +133,7 @@ bool GamepadsWidget::render()
gamepadLabelWidth = size.x - 180.0f;

ImGui::BeginChild(
(string("##name ") + to_string(index)).c_str(),
(string("##name ") + to_string(i)).c_str(),
ImVec2(gamepadLabelWidth, 50.0f)
);
cursor = ImGui::GetCursorPos();
Expand All @@ -146,21 +142,21 @@ bool GamepadsWidget::render()


static string name, id;
if (_hosting.getGamepadClient().isPuppetMaster && (*gi).isPuppet)
if (_hosting.getGamepadClient().isPuppetMaster && gi.isPuppet)
{
id = string() + "(# " + to_string(_hosting.getHost().userID) + ")\t";
name = _hosting.getHost().name;

}
else if ((*gi).owner.guest.isValid())
else if (gi.owner.guest.isValid())
{
id = string() + "(# " + to_string((*gi).owner.guest.userID) + ")\t";
name = (*gi).owner.guest.name;
id = string() + "(# " + to_string(gi.owner.guest.userID) + ")\t";
name = gi.owner.guest.name;
}
else
{
id = " ";
name = (*gi).owner.guest.name;
name = gi.owner.guest.name;
}

AppStyle::pushLabel();
Expand All @@ -179,17 +175,17 @@ bool GamepadsWidget::render()

ImGui::SetCursorPos(cursor);
ImGui::Button(
(string("##gamepad button") + to_string(index + 1)).c_str(),
(string("##gamepad button") + to_string(i + 1)).c_str(),
ImVec2(gamepadLabelWidth, 50.0f)
);

if (ImGui::BeginDragDropSource())
{
ImGui::SetDragDropPayload("Gamepad", &index, sizeof(int));
ImGui::SetDragDropPayload("Gamepad", &i, sizeof(int));

AppFonts::pushInput();
AppColors::pushPrimary();
ImGui::Text("%s", ((*gi).owner.guest.isValid() ? (*gi).owner.guest.name.c_str() : "Empty gamepad"));
ImGui::Text("%s", (gi.owner.guest.isValid() ? gi.owner.guest.name.c_str() : "Empty gamepad"));
AppColors::pop();
AppFonts::pop();

Expand All @@ -208,7 +204,7 @@ bool GamepadsWidget::render()
int guestIndex = *(const int*)payload->Data;
if (guestIndex >= 0 && guestIndex < _hosting.getGuestList().size())
{
(*gi).owner.guest.copy(_hosting.getGuestList()[guestIndex]);
gi.owner.guest.copy(_hosting.getGuestList()[guestIndex]);
}
}
}
Expand All @@ -220,11 +216,11 @@ bool GamepadsWidget::render()
if (sourceIndex >= 0 && sourceIndex < _gamepads.size())
{
static GuestDevice backupOwner;
backupOwner.copy(_gamepads[index].owner);
backupOwner.copy(_gamepads[i].owner);

_gamepads[index].copyOwner(_gamepads[sourceIndex]);
_gamepads[i].copyOwner(_gamepads[sourceIndex]);
_gamepads[sourceIndex].owner.copy(backupOwner);
_gamepads[index].clearState();
_gamepads[i].clearState();
_gamepads[sourceIndex].clearState();
}
}
Expand All @@ -243,15 +239,15 @@ bool GamepadsWidget::render()
ImGui::BeginGroup();
ImGui::Dummy(ImVec2(0.0f, 12.0f));
ImGui::SetNextItemWidth(40);
deviceIndices[index] = (*gi).owner.deviceID;
deviceIndices[i] = gi.owner.deviceID;

AppFonts::pushTitle();
if (ImGui::DragInt(
(string("##DeviceIndex") + to_string(index)).c_str(),
&deviceIndices[index], 0.1f, -1, 65536
(string("##DeviceIndex") + to_string(i)).c_str(),
&deviceIndices[i], 0.1f, -1, 65536
))
{
(*gi).owner.deviceID = deviceIndices[index];
gi.owner.deviceID = deviceIndices[i];
}
if (ImGui::IsItemHovered()) ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
AppFonts::pop();
Expand All @@ -267,13 +263,11 @@ bool GamepadsWidget::render()

ImGui::SameLine();

AnimatedGamepadWidget::render((*gi).getState().Gamepad);
AnimatedGamepadWidget::render(gi.getState().Gamepad);

ImGui::EndGroup();

ImGui::Dummy(ImVec2(0, 5));

index++;
}

ImGui::PopStyleVar();
Expand Down
2 changes: 1 addition & 1 deletion ParsecSoda/Widgets/VersionWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool VersionWidget::render()
ImGui::SetNextWindowSize(ImVec2(100, 32));
ImGui::Begin("##Version", (bool*)0, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBringToFrontOnFocus);
AppStyle::pushLabel();
ImGui::Text("v. 1.0.1-exp2");
ImGui::Text("v. 1.0.2");
AppStyle::pop();
ImGui::End();

Expand Down

0 comments on commit ab89e23

Please sign in to comment.