Skip to content

Commit

Permalink
Merge pull request #4 from project-slippi/refactor/chat-sys-text
Browse files Browse the repository at this point in the history
Refactor/chat
  • Loading branch information
JLaferri authored Jun 7, 2023
2 parents 0625526 + 97298c0 commit 2110272
Show file tree
Hide file tree
Showing 14 changed files with 640 additions and 126 deletions.
29 changes: 29 additions & 0 deletions Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ Text* Text_CreateTextWithGX(int SisIndex, int canvasID, u8 gx_link, u8 gx_pri){
return text;
}

/**
* Updates a Text color attributes
* @param text Text* pointer
* @param colorOffset Offset of text->text_start where encoded <Color> starts
* @param color 0x00RRGGBB
* @return
*/
void Text_UpdateColor(Text* text, u8 colorOffset, u8 r, u8 g, u8 b){
text->color.r = r;
text->color.g = g;
text->color.b = b;

*(text->text_start+colorOffset+0x1) = text->color.r;
*(text->text_start+colorOffset+0x2) = text->color.g;
*(text->text_start+colorOffset+0x3) = text->color.b;
}

void Text_UpdateColor2(Text* text, u8 colorOffset, u32 color){
text->color.r = color >> 10; // ff
text->color.g = (color >> 8) ^ (text->color.r << 8) ; // ffff ^ ff00 = ff
text->color.b = (((text->color.r << 8)+text->color.g) << 8) ^ color;
// (ff00 + ff) ^ ffff00

// OSReport("Text_UpdateColor2 r=0x%x g=0x%x b=0x%x\n", text->color.r, text->color.b, text->color.g);

*(text->text_start+colorOffset+0x1) = text->color.r;
*(text->text_start+colorOffset+0x2) = text->color.g;
*(text->text_start+colorOffset+0x3) = text->color.b;
}


#endif SLIPPI_COMMON_H
128 changes: 121 additions & 7 deletions Core/Notifications/Chat/ChatNotifications.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "Text.c"

#include "../../../Common.h"
#include "../../../Game/SysText.c"

#include "../Notifications.c"

int ChatMessagesLocalCount = 0;
Expand Down Expand Up @@ -54,7 +56,7 @@ void UpdateChatNotifications() {
int groupId = messageId >> 4; // 18 >> 4 == 1
int finalMessageId = (groupId << 4) ^ messageId; // 18 XOR 10 == 8
bool isValidStandardMsg = IsValidChatGroupId(groupId) && IsValidChatMessageId(finalMessageId);
bool isSpecialMessage = messageId == 0x10;
bool isSpecialMessage = IsSpecialChatMessageId(messageId);
if (!isValidStandardMsg && !isSpecialMessage) {
// OSReport("Invalid Chat Command: %i!\n", messageId);
return;
Expand Down Expand Up @@ -86,8 +88,7 @@ void CreateAndAddChatMessage(SlpCSSDesc *slpCss, MatchStateResponseBuffer *msrb,

NotificationMessage *chatMessage = CreateChatMessage(playerIndex, messageId);
CreateAndAddNotificationMessage(slpCss, chatMessage);
// chatMessage->text = CreateChatMessageText(chatMessage);
chatMessage->text = CreateChatMessageTextFromEXIDevice(chatMessage);
chatMessage->text = CreateChatMessageText(chatMessage);
SFX_Play(CHAT_SOUND_NEW_MESSAGE);

if (isLocalMessage) {
Expand All @@ -98,7 +99,76 @@ void CreateAndAddChatMessage(SlpCSSDesc *slpCss, MatchStateResponseBuffer *msrb,
//OSReport("Local: %i Remote: %i\n", ChatMessagesLocalCount, ChatMessagesRemoteCount);
}


//{SPT_CHAT_P1, "<LEFT><KERN><COLOR, 229, 76, 76>%s:<S><COLOR, 255, 255, 255>%s<END>"},
//{SPT_CHAT_P2, "<LEFT><KERN><COLOR, 59, 189, 255>%s:<S><COLOR, 255, 255, 255>%s<END>"},
//{SPT_CHAT_P3, "<LEFT><KERN><COLOR, 255, 203, 4>%s:<S><COLOR, 255, 255, 255>%s<END>"},
//{SPT_CHAT_P4, "<LEFT><KERN><COLOR, 0, 178, 2>%s:<S><COLOR, 255, 255, 255>%s<END>"},
//{SPT_LOGOUT, "<FIT><COLOR, 243, 75, 75>Are<S>You<COLOR, 0, 175, 75><S>Sure?<END>"},
//{SPT_CHAT_DISABLED, "<LEFT><KERN><COLOR, 0, 178, 2>%s<S><COLOR, 255, 255, 255>has<S>chat<S>disabled<S><END>"},
char *BuildChatTextData(char *playerName, u8 playerIndex, u8 groupId, u8 messageId) {
char *message;
SysText *color;

//OSReport("BuildChatTextData 0x%x \n", messageId);

switch (messageId) {
case CHAT_MESSAGE_ID_DISABLED: // DISABLED CHAT MESSAGE
return st_build(7,
st_left(), st_kern(), st_color(0, 178, 2),
st_sjis_text(playerName), st_space(),
st_color(255, 255, 255),
st_text("has chat disabled"));
default:
message = GetChatText(groupId, messageId, playerIndex, false);
}

switch (playerIndex) {
case 1:
color = st_color(59, 189, 255);
break;
case 2:
color = st_color(255, 203, 4);
break;
case 3:
color = st_color(0, 178, 2);
break;
default:
color = st_color(229, 76, 76);
break;
}


return st_build(8,
st_left(), st_kern(), color,
st_sjis_text(playerName), st_text(":"), st_space(),
st_color(255, 255, 255),
st_sjis_text(message));
}

Text *CreateChatMessageText(NotificationMessage *msg) {
Text *text = NULL;
switch (CHAT_MESSAGE_GEN_TYPE) {
case CHAT_MESSAGE_GEN_TYPE_SUBTEXT:
text = CreateChatMessageTextFromSubText(msg);
break;
case CHAT_MESSAGE_GEN_TYPE_SYSTEXT:
text = CreateChatMessageTextFromLocalSysText(msg);
break;
case CHAT_MESSAGE_GEN_TYPE_EXI:
text = CreateChatMessageTextFromEXIDevice(msg);
break;
default:
text = CreateChatMessageTextFromLocalSysText(msg);
break;

}

return text;
}


Text *CreateChatMessageTextFromSubText(NotificationMessage *msg) {
MatchStateResponseBuffer *msrb = MSRB();
bool isLocalMessage = msg->playerIndex == msrb->localPlayerIndex;

Expand All @@ -119,24 +189,60 @@ Text *CreateChatMessageText(NotificationMessage *msg) {
char *playerName = isLocalMessage ? msrb->localName : msrb->p1Name + (msg->playerIndex * 31);

char *name = strcat(playerName, ": ");
char *message = GetChatText(groupId, messageId, false);
char *message = GetChatText(groupId, messageId, msg->playerIndex, false);
float xPos = isWidescreen() ? -446.0f : -296.0f;
float yPos = -252.0f + ((msg->id) * 32.0f);
int colorIndex = msrb->localPlayerIndex + 1;
float scale = 0.4f;

createSubtext(text, &MSG_COLORS[colorIndex], 0x0, 0, (char **) {name}, scale, xPos, yPos, 0.0f, 0.0f);
createSubtext(text, &MSG_COLORS[0], 0x0, 0, (char **) {message}, scale, strlen(name) * 6.8f + xPos, yPos, 0.0f,
CreateSubtext(text, &MSG_COLORS[colorIndex], false, 0, (char **) {name}, scale, xPos, yPos, 0.0f, 0.0f);
CreateSubtext(text, &MSG_COLORS[0], false, 0, (char **) {message}, scale, strlen(name) * 6.8f + xPos, yPos, 0.0f,
0.0f);
return text;
}

Text *CreateChatMessageTextFromLocalSysText(NotificationMessage *msg) {
MatchStateResponseBuffer *msrb = MSRB();
bool isLocalMessage = msg->playerIndex == msrb->localPlayerIndex;

// Dolphin returns the group and message id joined together so we need to split them
int groupId = msg->messageId >> 4;
int messageId = (groupId << 4) ^ msg->messageId;

// Special Chat Message Ids need to be kept intact so restore it if it is and let
// BuildChatTextData return proper sys text buffer
if (IsSpecialChatMessageId(msg->messageId))
messageId = msg->messageId;

char *playerName = isLocalMessage ? msrb->localName : msrb->p1Name + (msg->playerIndex * 31);
char *textData = BuildChatTextData(playerName, msg->playerIndex, groupId, messageId);

float x = isWidescreen() ? -44.5f : -29.5f;
float y = -23.25f + (msg->id * 3.2f);

// Hack the text alloc info to use a different gx
stc_textcanvas_first[0]->gx_link = 3;
stc_textcanvas_first[0]->gx_pri = 129;

Text *text = Text_CreateText2(0, 0, x, y, 5.0f, 20.0f, 20.0f);
text->hidden = true; // hide by default
text->stretch.X = 0.04f;
text->stretch.Y = 0.04f;
Text_SetSisText(text, 0);
text->text_start = textData;

// Restore original gx
stc_textcanvas_first[0]->gx_link = 1;
stc_textcanvas_first[0]->gx_pri = 0x80;
return text;
}

Text *CreateChatMessageTextFromEXIDevice(NotificationMessage *msg) {
// Hack the text alloc info to use a different gx
stc_textcanvas_first[0]->gx_link = 3;
stc_textcanvas_first[0]->gx_pri = 129;
Text *text = createSlippiPremadeText(msg->playerIndex + 1, msg->messageId, 2, 0, -29.5f, -23.25f + (msg->id * 3.2f),
float x = isWidescreen() ? -44.5f : -29.5f;
Text *text = CreateSlippiPremadeText(msg->playerIndex + 1, msg->messageId, 0, x, -23.25f + (msg->id * 3.2f),
5.0f, 0.04f);
stc_textcanvas_first[0]->gx_link = 1;
stc_textcanvas_first[0]->gx_pri = 0x80;
Expand Down Expand Up @@ -174,6 +280,14 @@ bool IsValidChatGroupId(int groupId) {
return false;
};

bool IsSpecialChatMessageId(int messageId) {
switch (messageId) {
case CHAT_MESSAGE_ID_DISABLED:
return true;
}
return false;
}

bool IsValidChatMessageId(int messageId) {
// For now use same logic
return IsValidChatGroupId(messageId);
Expand Down
21 changes: 21 additions & 0 deletions Core/Notifications/Chat/ChatNotifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
#define CHAT_FRAMES 20 // Frames that a chat notification message lives for
#define CHAT_MAX_PLAYER_MESSAGES 4 // Max Messagees allowed per player

enum ChatMessageGenTypes {
CHAT_MESSAGE_GEN_TYPE_SUBTEXT,
CHAT_MESSAGE_GEN_TYPE_SYSTEXT,
CHAT_MESSAGE_GEN_TYPE_EXI,
};

enum ChatMessageSpecialIDs{
CHAT_MESSAGE_ID_DISABLED = 0x10,
};

#ifndef CHAT_MESSAGE_GEN_TYPE
#define CHAT_MESSAGE_GEN_TYPE CHAT_MESSAGE_GEN_TYPE_SYSTEXT
#endif

/** functions **/
void ListenForChatNotifications();
Expand All @@ -26,10 +39,18 @@ void UpdateChatMessage(GOBJ *gobj);

Text *CreateChatMessageText(NotificationMessage *msg);

Text *CreateChatMessageTextFromSubText(NotificationMessage *msg);

Text *CreateChatMessageTextFromLocalSysText(NotificationMessage *msg);

Text *CreateChatMessageTextFromEXIDevice(NotificationMessage *msg);

char* BuildChatTextData(char* playerName, u8 playerIndex, u8 groupId, u8 messageId);

bool IsValidChatGroupId(int groupId);

bool IsSpecialChatMessageId(int messageId);

bool IsValidChatMessageId(int messageId);

void FreeChatMessage(void *ptr);
Expand Down
32 changes: 23 additions & 9 deletions Core/Notifications/Chat/Text.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

#include "../../../Slippi.h"
#include "Text.h"
#include "../../Scenes/CSS/Chat/Chat.h"

char *GetChatText(int groupId, int messageId, bool useMessageIndex) {
// OSReport("GetChatText groupId: %i, msgId: %i \n", groupId, messageId);

int GetGroupIndex(int groupId) {
int groupIndex = 0;
int index = 0;

switch (groupId) {
case PAD_BUTTON_DPAD_UP:
Expand All @@ -28,6 +26,19 @@ char *GetChatText(int groupId, int messageId, bool useMessageIndex) {
break;
}

return groupIndex;
}

char *GetHeaderText(int groupId) {
int groupIndex = GetGroupIndex(groupId);
return HEADER_STRINGS[groupIndex];
}

char *GetChatText(int groupId, int messageId, int playerIdx, bool useMessageIndex) {
// OSReport("GetChatText groupId: %i, msgId: %i \n", groupId, messageId);

int groupIndex = GetGroupIndex(groupId);
int index = 0;

if (useMessageIndex) {
index = messageId;
Expand All @@ -53,10 +64,13 @@ char *GetChatText(int groupId, int messageId, bool useMessageIndex) {


// OSReport("GetChatText s: %s\n", ChatGroups[groupIndex][index]);
return CHAT_MSG_STRINGS[ChatGroups[groupIndex][index]];
return playerSettingsResp->settings[playerIdx].chatMessages[(groupIndex * 4) + (index - 1)];
};

Text *CreateChatWindowText(GOBJ *gobj, int groupId) {
MatchStateResponseBuffer *msrb = MSRB();
// OSReport("Idx: %d\n", msrb->localPlayerIndex);

Text *text = Text_CreateText(0, 0);
// OSReport("text.gxLink: %i gxPri: %i", text->gobj->gx_link, text->gobj->gx_pri);

Expand All @@ -72,18 +86,18 @@ Text *CreateChatWindowText(GOBJ *gobj, int groupId) {
float offset = 0; // should change if widescreen

char title[30];
sprintf(title, "Chat: %s", GetChatText(groupId, CHAT_STR_HEADER, true));
sprintf(title, "%s", GetHeaderText(groupId));
// Create Header
int titleColor = isConnected() ? MSG_COLOR_CHAT_WINDOW : MSG_COLOR_CHAT_WINDOW_IDLE;
createSubtext(text, &MSG_COLORS[titleColor], 0x0, 0, (char **) {title}, 0.45f, x + offset, 79.0f, 0.0f, 0.0f);
CreateSubtext(text, &MSG_COLORS[titleColor], false, 0, (char **) {title}, 0.45f, x + offset, 79.0f, 0.0f, 0.0f);

// Create Labels
for (int i = CHAT_STR_UP; i <= CHAT_STR_DOWN; i++) {
float margin = 25.0f * (i + 1); // starts with 2 lines from header
float yPos = 79.0f + margin;
char *label = GetChatText(groupId, i, true);
char *label = GetChatText(groupId, i, msrb->localPlayerIndex, true);

createSubtext(text, &MSG_COLORS[MSG_COLOR_WHITE], 0x0, 0, (char **) {label}, 0.45f, labelX + offset, yPos, 0.0f,
CreateSubtext(text, &MSG_COLORS[MSG_COLOR_WHITE], false, 0, (char **) {label}, 0.45f, labelX + offset, yPos, 0.0f,
0.0f);
}

Expand Down
Loading

0 comments on commit 2110272

Please sign in to comment.