124 lines
2.9 KiB
C++
124 lines
2.9 KiB
C++
#include "ChatClient.h"
|
|
|
|
|
|
ChatClient::ChatClient(Archimedes::App* a, void* h) : Module(a, h) {
|
|
|
|
name = "ChatClient";
|
|
|
|
ClientModule* cm = new ClientModule(a, h);
|
|
deps[*cm] = cm;
|
|
cm->shouldHandleEvents(ClientModule::CMEventEnum::ConnectionStatusChanged | ClientModule::CMEventEnum::DataSent);
|
|
|
|
ImguiModule* im = new ImguiModule(a, h);
|
|
|
|
deps[*im] = im;
|
|
}
|
|
|
|
ChatClient::~ChatClient() {
|
|
if(app) {
|
|
|
|
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
|
|
|
|
im->releaseContext(ImGui::GetCurrentContext());
|
|
}
|
|
}
|
|
|
|
void ChatClient::onLoad() {
|
|
|
|
std::cerr << "onLoad\n";
|
|
|
|
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
|
|
|
|
if(!im) {
|
|
std::cout << "No ClientModule for ChatClient!\n";
|
|
std::abort();
|
|
}
|
|
|
|
ImGui::SetCurrentContext(im->aquireContext());
|
|
|
|
}
|
|
|
|
void ChatClient::run() {
|
|
|
|
std::cerr << "run\n";
|
|
|
|
static ClientModule* cm; { cm = (ClientModule*) moduleInstances[ClientModule()]; }
|
|
|
|
if(!cm) {
|
|
std::cout << "No ClientModule for ChatClient!\n";
|
|
std::abort();
|
|
}
|
|
|
|
if(open) {
|
|
static std::string s, addr = "127.0.0.1:9932";
|
|
|
|
ImGui::Begin("ChatClient Module", &open);
|
|
|
|
ImGui::InputText("Server Address: ", &addr);
|
|
|
|
if(cm->isRunning() && cm->isConnected()) {
|
|
|
|
if(ImGui::Button("Disconnect") && cm->isRunning()) {
|
|
cm->stopClient();
|
|
}
|
|
|
|
} else {
|
|
|
|
if(ImGui::Button("Connect") && !cm->isRunning()) {
|
|
static SteamNetworkingIPAddr serverAddr;
|
|
serverAddr.ParseString(addr.c_str());
|
|
cm->startClient(serverAddr);
|
|
}
|
|
}
|
|
|
|
|
|
ImGui::Text("%s", messages.c_str());
|
|
|
|
ImGui::InputText("Message: ", &s);
|
|
|
|
ImGui::SameLine();
|
|
|
|
if(ImGui::Button("send")) {
|
|
cm->sendReliable(s.c_str(), (uint32) s.length());
|
|
}
|
|
|
|
ImGui::End();
|
|
} else {
|
|
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
|
|
}
|
|
}
|
|
|
|
bool ChatClient::onEvent(const Archimedes::Event& event) {
|
|
|
|
std::cerr << "onEvent: " << (std::string) event << std::endl;
|
|
unsigned int type = app->getEventType(event);
|
|
|
|
|
|
/*if(type == app->getEventType("DataSentEvent")) {
|
|
//we did this?
|
|
return true;
|
|
} else */
|
|
|
|
if(type == app->getEventType(Archimedes::DataRecievedEvent())) {
|
|
|
|
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
|
|
|
|
static std::string s; s = std::string((const char*)e.msg->m_pData, e.msg->m_cbSize);
|
|
|
|
std::cerr << "Client Recieved: " << s << std::endl;
|
|
|
|
messages += "\n\n" + s;
|
|
|
|
return true;
|
|
}
|
|
/*else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
|
|
|
//Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event;
|
|
|
|
return false;
|
|
|
|
}*/
|
|
|
|
return false;
|
|
}
|