Skip to content

Instantly share code, notes, and snippets.

@Geekid812
Created November 7, 2023 18:56
Show Gist options
  • Save Geekid812/30c989abbca51378d1ca9984f83e1898 to your computer and use it in GitHub Desktop.
Save Geekid812/30c989abbca51378d1ca9984f83e1898 to your computer and use it in GitHub Desktop.
BetterChat Channels Test Script
BetterChat::IChatChannelHook@ g_hook;
BetterChat::IChatMessageListener@ g_listener;
MessageSink@ g_sink;
array<string> g_messages;
array<string> g_listenedMessages;
string g_channelName = "My Channel";
string g_chatText = "";
string g_author = "";
string g_clubTag = "";
float g_hue = 0.;
bool g_system = false;
BetterChat::ChatEntryScope g_scope;
bool g_echo = false;
class MessageSink: BetterChat::IChatChannelSink {
void SendChatMessage(const string&in text) override {
g_messages.InsertLast(text);
if (g_echo && @g_hook !is null) {
g_hook.AddChatEntry(BetterChat::ChatEntry(text, GetLocalLogin()));
}
}
}
class Listener: BetterChat::IChatMessageListener {
void OnChatMessage(NGameScriptChat_SEntry@ entry) override {
g_listenedMessages.InsertLast(entry.Text);
}
}
void RenderChannelTest() {
UI::Begin("Better Chat Channel Test", UI::WindowFlags::AlwaysAutoResize);
if (@g_hook is null || @g_sink is null) {
g_channelName = UI::InputText("Channel Name", g_channelName);
if (UI::Button("Create Channel")) {
g_messages = {};
@g_sink = MessageSink();
@g_hook = BetterChat::AquireChatChannelHook(g_channelName, g_sink);
}
} else {
UI::Text("Text channel created: " + g_channelName);
g_chatText = UI::InputText("Message", g_chatText);
g_author = UI::InputText("Author", g_author);
g_clubTag = UI::InputText("Club Tag", g_clubTag);
g_hue = UI::SliderFloat("Team Color Hue", g_hue, 0., 1.);
if (UI::BeginCombo("Chat Scope", tostring(g_scope))) {
for (uint i = 0; i < 5; i++) {
auto scope = BetterChat::ChatEntryScope(i);
if (UI::Selectable(tostring(scope), scope == g_scope)) {
g_scope = scope;
}
}
UI::EndCombo();
}
g_system = UI::Checkbox("Is System", g_system);
if (UI::Button("Send")) {
g_hook.AddChatEntry(BetterChat::ChatEntry(g_chatText, g_author, g_clubTag, g_hue, g_system, g_scope));
}
UI::Separator();
if (UI::Button("Clear chat")) {
g_hook.Clear();
g_messages = {};
}
UI::SameLine();
if (UI::Button("Unload")) {
UninitChannel();
}
g_echo = UI::Checkbox("Echo Chat Messages", g_echo);
if (UI::BeginTable("chatmessages", 1)) {
UI::TableNextColumn();
UI::TextDisabled("Chat Messages Emitted");
for (uint i = 0; i < g_messages.Length; i++) {
UI::TableNextColumn();
UI::Text(g_messages[i]);
}
UI::EndTable();
}
}
UI::End();
}
void RenderListener() {
UI::Begin("Listener", UI::WindowFlags::AlwaysAutoResize);
if (@g_listener is null) {
if (UI::Button("Create Listener")) {
Listener listener = Listener();
@g_listener = listener;
g_listenedMessages = {};
BetterChat::RegisterListener(listener);
}
} else {
if (UI::Button("Unregister")) {
UninitListener();
}
if (UI::BeginTable("chatmessages", 1)) {
UI::TableNextColumn();
UI::TextDisabled("Listened Messages");
for (uint i = 0; i < g_listenedMessages.Length; i++) {
UI::TableNextColumn();
UI::Text(g_listenedMessages[i]);
}
UI::EndTable();
}
}
UI::End();
}
void UninitChannel() {
if (@g_hook !is null) {
BetterChat::ReleaseChatChannelHook(g_hook);
@g_hook = null;
}
}
void UninitListener() {
if (@g_listener !is null) {
BetterChat::UnregisterListener(g_listener);
@g_listener = null;
}
}
void Uninit() {
UninitChannel();
UninitListener();
}
void RenderInterface() {
RenderChannelTest();
RenderListener();
}
void OnDisabled() {
Uninit();
}
void OnDestroyed() {
Uninit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment