From 1a79bbd80d590578bbc7a823cb32ca2514be964f Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 24 Apr 2013 18:30:46 -0700 Subject: [PATCH] Fleshing out the chat entry widget. --- interface/src/ChatEntry.cpp | 51 +++++++++++++++++++++++++++++++++++++ interface/src/ChatEntry.h | 12 +++++++++ interface/src/main.cpp | 10 +++++++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/interface/src/ChatEntry.cpp b/interface/src/ChatEntry.cpp index 575674bb01..39d82d6f16 100644 --- a/interface/src/ChatEntry.cpp +++ b/interface/src/ChatEntry.cpp @@ -5,8 +5,59 @@ // Created by Andrzej Kapolka on 4/24/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +#include "InterfaceConfig.h" + #include "ChatEntry.h" +#include "Util.h" + +using namespace std; + +bool ChatEntry::key(unsigned char k) { + switch (k) { + case '\r': + return false; + + case '\b': + if (cursorPos != 0) { + contents.erase(cursorPos - 1, 1); + cursorPos--; + } + return true; + + default: + contents.insert(cursorPos, 1, k); + cursorPos++; + return true; + } +} + +void ChatEntry::specialKey(unsigned char k) { + switch (k) { + case GLUT_KEY_LEFT: + if (cursorPos != 0) { + cursorPos--; + } + break; + + case GLUT_KEY_RIGHT: + if (cursorPos != contents.size()) { + cursorPos++; + } + break; + } +} void ChatEntry::render(int screenWidth, int screenHeight) { + drawtext(20, screenHeight - 150, 0.10, 0, 1.0, 0, contents.c_str(), 1, 1, 1); + float width = 0; + for (string::iterator it = contents.begin(), end = it + cursorPos; it != end; it++) { + width += glutStrokeWidth(GLUT_STROKE_ROMAN, *it)*0.10; + } + glDisable(GL_LINE_SMOOTH); + glBegin(GL_LINE_STRIP); + glVertex2f(20 + width, screenHeight - 165); + glVertex2f(20 + width, screenHeight - 150); + glEnd(); + glEnable(GL_LINE_SMOOTH); } diff --git a/interface/src/ChatEntry.h b/interface/src/ChatEntry.h index 4a5bab717b..da91c847fe 100644 --- a/interface/src/ChatEntry.h +++ b/interface/src/ChatEntry.h @@ -9,12 +9,24 @@ #ifndef __interface__ChatEntry__ #define __interface__ChatEntry__ +#include + class ChatEntry { public: + const std::string& getContents () const { return contents; } + + bool key(unsigned char k); + + void specialKey(unsigned char k); + void render(int screenWidth, int screenHeight); private: + + std::string contents; + + int cursorPos; }; #endif /* defined(__interface__ChatEntry__) */ diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 485e55411b..497b4b31b5 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -1331,6 +1331,11 @@ void specialkeyUp(int k, int x, int y) { void specialkey(int k, int x, int y) { + if (::chatEntryOn) { + chatEntry.specialKey(k); + return; + } + if (k == GLUT_KEY_UP || k == GLUT_KEY_DOWN || k == GLUT_KEY_LEFT || k == GLUT_KEY_RIGHT) { if (k == GLUT_KEY_UP) { if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myAvatar.setDriveKeys(UP, 1); @@ -1368,7 +1373,10 @@ void keyUp(unsigned char k, int x, int y) { void key(unsigned char k, int x, int y) { if (::chatEntryOn) { - + if (!chatEntry.key()) { + + ::chatEntryOn = false; + } return; }