From 675d06e4b9cc80fc2711c51f2451c106fa3d2bf0 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 25 Apr 2013 14:03:31 -0700 Subject: [PATCH] More chat bits; basic functionality now working. --- avatar-mixer/src/main.cpp | 7 ++++++- interface/src/Avatar.cpp | 29 ++++++++++++++++++++++++++++ interface/src/ChatEntry.cpp | 13 +++++++++++-- interface/src/ChatEntry.h | 2 ++ interface/src/main.cpp | 12 +++++++++--- libraries/avatars/src/AvatarData.cpp | 21 ++++++++++++++------ libraries/avatars/src/AvatarData.h | 21 +++++++++++++++----- 7 files changed, 88 insertions(+), 17 deletions(-) diff --git a/avatar-mixer/src/main.cpp b/avatar-mixer/src/main.cpp index 1d446f40f4..21c8967b2f 100644 --- a/avatar-mixer/src/main.cpp +++ b/avatar-mixer/src/main.cpp @@ -51,9 +51,14 @@ void attachAvatarDataToAgent(Agent *newAgent) { } } -int main(int argc, char* argv[]) +int main(int argc, const char* argv[]) { AgentList *agentList = AgentList::createInstance(AGENT_TYPE_AVATAR_MIXER, AVATAR_LISTEN_PORT); + if (cmdOptionExists(argc, argv, "--local")) { + printf("Local Domain MODE!\n"); + int ip = getLocalAddress(); + sprintf(DOMAIN_IP,"%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF)); + } setvbuf(stdout, NULL, _IOLBF, 0); agentList->linkedDataCreateCallback = attachAvatarDataToAgent; diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index 9f9d1febba..5ab16bfe7b 100644 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -653,6 +653,35 @@ void Avatar::render(bool lookingInMirror) { glEnd(); } } + + if (!_chatMessage.empty()) { + float width = 0; + for (string::iterator it = _chatMessage.begin(); it != _chatMessage.end(); it++) { + width += glutStrokeWidth(GLUT_STROKE_ROMAN, *it)*0.0005; + } + glPushMatrix(); + + // extract the view direction from the modelview matrix: transform (0, 0, 1) by the + // transpose of the modelview to get its direction in world space, then use the X/Z + // components to determine the angle + float modelview[16]; + glGetFloatv(GL_MODELVIEW_MATRIX, modelview); + + glTranslatef(_position.x, _position.y + 0.7, _position.z); + glRotatef(atan2(-modelview[2], -modelview[10]) * 180 / PI, 0, 1, 0); + glTranslatef(width * 0.5, 0, 0); + + // TODO: For no apparent reason, OpenGL is hiding the first character. This fixes it (it + // gets hidden instead), but we should find and fix the underlying problem. + glBegin(GL_LINE_STRIP); + glVertex2f(0, 0); + glVertex2f(0, 0); + glEnd(); + + drawtext(0, 0, 0.0005, 180, 1.0, 0, _chatMessage.c_str(), 1, 1, 1); + + glPopMatrix(); + } } diff --git a/interface/src/ChatEntry.cpp b/interface/src/ChatEntry.cpp index 39d82d6f16..370c68538f 100644 --- a/interface/src/ChatEntry.cpp +++ b/interface/src/ChatEntry.cpp @@ -12,6 +12,13 @@ using namespace std; +const int MAX_CONTENT_LENGTH = 140; + +void ChatEntry::clear () { + contents.clear(); + cursorPos = 0; +} + bool ChatEntry::key(unsigned char k) { switch (k) { case '\r': @@ -25,8 +32,10 @@ bool ChatEntry::key(unsigned char k) { return true; default: - contents.insert(cursorPos, 1, k); - cursorPos++; + if (contents.size() != MAX_CONTENT_LENGTH) { + contents.insert(cursorPos, 1, k); + cursorPos++; + } return true; } } diff --git a/interface/src/ChatEntry.h b/interface/src/ChatEntry.h index da91c847fe..b71a3d1b96 100644 --- a/interface/src/ChatEntry.h +++ b/interface/src/ChatEntry.h @@ -16,6 +16,8 @@ public: const std::string& getContents () const { return contents; } + void clear (); + bool key(unsigned char k); void specialKey(unsigned char k); diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 497b4b31b5..8c2efed563 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -1373,8 +1373,9 @@ void keyUp(unsigned char k, int x, int y) { void key(unsigned char k, int x, int y) { if (::chatEntryOn) { - if (!chatEntry.key()) { - + if (!chatEntry.key(k)) { + myAvatar.setChatMessage(chatEntry.getContents()); + chatEntry.clear(); ::chatEntryOn = false; } return; @@ -1656,7 +1657,12 @@ int main(int argc, const char * argv[]) return EXIT_SUCCESS; } - AgentList::createInstance(AGENT_TYPE_AVATAR); + unsigned int listenPort = AGENT_SOCKET_LISTEN_PORT; + const char* portStr = getCmdOption(argc, argv, "--listenPort"); + if (portStr) { + listenPort = atoi(portStr); + } + AgentList::createInstance(AGENT_TYPE_AVATAR, listenPort); gettimeofday(&applicationStartupTime, NULL); const char* domainIP = getCmdOption(argc, argv, "--domain"); diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index bbdcca9d19..853b69b611 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -16,6 +16,7 @@ #include "AvatarData.h" #include "avatars_Log.h" +using namespace std; using avatars_lib::printLog; @@ -112,10 +113,14 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) { memcpy(destinationBuffer, &_cameraFarClip, sizeof(_cameraFarClip)); destinationBuffer += sizeof(_cameraFarClip); - // Key State - memcpy(destinationBuffer, &_keyState, sizeof(char)); - destinationBuffer += sizeof(char); + // key state + *destinationBuffer++ = _keyState; + // chat message + *destinationBuffer++ = _chatMessage.size(); + memcpy(destinationBuffer, _chatMessage.data(), _chatMessage.size() * sizeof(char)); + destinationBuffer += _chatMessage.size() * sizeof(char); + return destinationBuffer - bufferStart; } @@ -171,9 +176,13 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { memcpy(&_cameraFarClip, sourceBuffer, sizeof(_cameraFarClip)); sourceBuffer += sizeof(_cameraFarClip); - // Key State - memcpy(&_keyState, sourceBuffer, sizeof(char)); - sourceBuffer += sizeof(char); + // key state + _keyState = (KeyState)*sourceBuffer++; + + // the rest is a chat message + int chatMessageSize = *sourceBuffer++; + _chatMessage = string((char*)sourceBuffer, chatMessageSize); + sourceBuffer += chatMessageSize * sizeof(char); return sourceBuffer - startPosition; } diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 13436d15df..34c33832d6 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -9,6 +9,8 @@ #ifndef __hifi__AvatarData__ #define __hifi__AvatarData__ +#include + #include #include @@ -74,9 +76,15 @@ public: void setCameraNearClip(float nearClip) { _cameraNearClip = nearClip; } void setCameraFarClip(float farClip) { _cameraFarClip = farClip; } - // Key state - void setKeyState(char s) { _keyState = s; } - char keyState() const { return _keyState; } + enum KeyState { NoKey, KeyDown, KeyUp, DeleteKey }; + + // key state + void setKeyState(KeyState s) { _keyState = s; } + KeyState keyState() const { return _keyState; } + + // chat message + void setChatMessage(const std::string& msg) { _chatMessage = msg; } + const std::string& chatMessage () const { return _chatMessage; } protected: glm::vec3 _position; @@ -110,8 +118,11 @@ protected: float _cameraNearClip; float _cameraFarClip; - // Key state (nothing, down, up, backspace) - char _keyState; + // key state (nothing, down, up, backspace) + KeyState _keyState; + + // chat message + std::string _chatMessage; }; #endif /* defined(__hifi__AvatarData__) */