Chat.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. ==============================================================================
  3. This is an automatically generated GUI class created by the Introjucer!
  4. Be careful when adding custom code to these files, as only the code within
  5. the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  6. and re-saved.
  7. Created with Introjucer version: 3.1.1
  8. ------------------------------------------------------------------------------
  9. The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions"
  10. Copyright 2004-13 by Raw Material Software Ltd.
  11. ==============================================================================
  12. */
  13. //[Headers] You can add your own extra header files here...
  14. #include "LinJam.h"
  15. #include "./Trace/TraceChat.h"
  16. //[/Headers]
  17. #include "Chat.h"
  18. //[MiscUserDefs] You can add your own user definitions and misc code here...
  19. //[/MiscUserDefs]
  20. //==============================================================================
  21. Chat::Chat (Value font_size)
  22. : fontSize(font_size)
  23. {
  24. //[Constructor_pre] You can add your own custom stuff here..
  25. //[/Constructor_pre]
  26. setName ("Chat");
  27. addAndMakeVisible (chatText = new TextEditor ("chatText"));
  28. chatText->setMultiLine (true);
  29. chatText->setReturnKeyStartsNewLine (false);
  30. chatText->setReadOnly (true);
  31. chatText->setScrollbarsShown (true);
  32. chatText->setCaretVisible (false);
  33. chatText->setPopupMenuEnabled (true);
  34. chatText->setColour (TextEditor::textColourId, Colours::grey);
  35. chatText->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  36. chatText->setColour (TextEditor::highlightColourId, Colour (0x00000000));
  37. chatText->setColour (TextEditor::outlineColourId, Colour (0x00000000));
  38. chatText->setText (String());
  39. addAndMakeVisible (chatEntryText = new TextEditor ("chatEntryText"));
  40. chatEntryText->setExplicitFocusOrder (1);
  41. chatEntryText->setMultiLine (false);
  42. chatEntryText->setReturnKeyStartsNewLine (false);
  43. chatEntryText->setReadOnly (false);
  44. chatEntryText->setScrollbarsShown (false);
  45. chatEntryText->setCaretVisible (true);
  46. chatEntryText->setPopupMenuEnabled (true);
  47. chatEntryText->setColour (TextEditor::textColourId, Colours::grey);
  48. chatEntryText->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  49. chatEntryText->setColour (TextEditor::highlightColourId, Colour (0x00000000));
  50. chatEntryText->setColour (TextEditor::outlineColourId, Colour (0x00000000));
  51. chatEntryText->setColour (CaretComponent::caretColourId, Colours::white);
  52. chatEntryText->setText (String());
  53. addAndMakeVisible (topicLabel = new Label ("topicLabel",
  54. String()));
  55. topicLabel->setFont (Font (Font::getDefaultMonospacedFontName(), 15.00f, Font::bold));
  56. topicLabel->setJustificationType (Justification::centredTop);
  57. topicLabel->setEditable (true, true, true);
  58. topicLabel->setColour (Label::backgroundColourId, Colour (0x00000000));
  59. topicLabel->setColour (Label::textColourId, Colours::grey);
  60. topicLabel->setColour (TextEditor::textColourId, Colours::grey);
  61. topicLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  62. topicLabel->setColour (TextEditor::highlightColourId, Colour (0xffbbbbff));
  63. topicLabel->addListener (this);
  64. //[UserPreSize]
  65. // set fonts and sizes
  66. Font topic_font = this->topicLabel->getFont().withHeight(GUI::TOPIC_FONT_H) ;
  67. this->prevTopicText = String() ;
  68. this->topicLabel->setFont(topic_font) ;
  69. setFontSize() ;
  70. //[/UserPreSize]
  71. setSize (622, 162);
  72. //[Constructor] You can add your own custom stuff here..
  73. // set text editor colors
  74. this->topicLabel ->setColour(Label::backgroundColourId , GUI::CHAT_TEXT_BG_COLOR ) ;
  75. this->topicLabel ->setColour(Label::textColourId , GUI::TOPIC_TEXT_COLOR ) ;
  76. this->topicLabel ->setColour(Label::outlineColourId , Colour(0xffff0000));//GUI::CHAT_OUTLINE_COLOR ) ;
  77. this->topicLabel ->setColour(CaretComponent::caretColourId , GUI::TEXT_CARET_COLOR ) ;
  78. this->chatEntryText->setColour(CaretComponent::caretColourId , GUI::TEXT_CARET_COLOR ) ;
  79. this->topicLabel ->setColour(TextEditor::textColourId , GUI::CHAT_TEXT_COLOR ) ;
  80. this->chatText ->setColour(TextEditor::textColourId , GUI::CHAT_TEXT_COLOR ) ;
  81. this->chatEntryText->setColour(TextEditor::textColourId , GUI::CHAT_TEXT_COLOR ) ;
  82. this->topicLabel ->setColour(TextEditor::highlightColourId , GUI::TEXT_HILITEBG_COLOR) ;
  83. this->chatText ->setColour(TextEditor::highlightColourId , GUI::TEXT_HILITEBG_COLOR) ;
  84. this->chatEntryText->setColour(TextEditor::highlightColourId , GUI::TEXT_HILITEBG_COLOR) ;
  85. this->topicLabel ->setColour(TextEditor::highlightedTextColourId , GUI::TEXT_HILITE_COLOR ) ;
  86. this->chatText ->setColour(TextEditor::highlightedTextColourId , GUI::TEXT_HILITE_COLOR ) ;
  87. this->chatEntryText->setColour(TextEditor::highlightedTextColourId , GUI::TEXT_HILITE_COLOR ) ;
  88. this->topicLabel ->setColour(TextEditor::outlineColourId , GUI::CHAT_OUTLINE_COLOR ) ;
  89. this->chatText ->setColour(TextEditor::outlineColourId , GUI::CHAT_OUTLINE_COLOR ) ;
  90. this->chatEntryText->setColour(TextEditor::outlineColourId , GUI::CHAT_OUTLINE_COLOR ) ;
  91. this->topicLabel ->setColour(TextEditor::focusedOutlineColourId , GUI::CHAT_FOCUS_COLOR ) ;
  92. this->chatText ->setColour(TextEditor::focusedOutlineColourId , GUI::CHAT_FOCUS_COLOR ) ;
  93. this->chatEntryText->setColour(TextEditor::focusedOutlineColourId , GUI::CHAT_FOCUS_COLOR ) ;
  94. this->topicLabel ->setColour(TextEditor::shadowColourId , GUI::CHAT_SHADOW_COLOR ) ;
  95. this->chatText ->setColour(TextEditor::shadowColourId , GUI::CHAT_SHADOW_COLOR ) ;
  96. this->chatEntryText->setColour(TextEditor::shadowColourId , GUI::CHAT_SHADOW_COLOR ) ;
  97. this->topicLabel ->setColour(TextEditor::backgroundColourId , GUI::CHAT_TEXT_BG_COLOR ) ;
  98. this->chatText ->setColour(TextEditor::backgroundColourId , GUI::CHAT_TEXT_BG_COLOR ) ;
  99. this->chatEntryText->setColour(TextEditor::backgroundColourId , GUI::CHAT_TEXT_BG_COLOR ) ;
  100. this->chatEntryText->setSelectAllWhenFocused(true) ;
  101. this->chatEntryText->setTextToShowWhenEmpty(GUI::CHAT_PROMPT_TEXT , GUI::TEXT_EMPTY_COLOR) ;
  102. this->chatEntryText->setInputRestrictions(1024) ;
  103. // local event handlers
  104. this->fontSize .addListener(this) ;
  105. this->chatEntryText->addListener(this) ;
  106. //[/Constructor]
  107. }
  108. Chat::~Chat()
  109. {
  110. //[Destructor_pre]. You can add your own custom destruction code here..
  111. //[/Destructor_pre]
  112. chatText = nullptr;
  113. chatEntryText = nullptr;
  114. topicLabel = nullptr;
  115. //[Destructor]. You can add your own custom destruction code here..
  116. //[/Destructor]
  117. }
  118. //==============================================================================
  119. void Chat::paint (Graphics& g)
  120. {
  121. //[UserPrePaint] Add your own custom painting code here..
  122. #ifdef DRAW_JUCER_BORDERS
  123. //[/UserPrePaint]
  124. g.setColour (Colour (0xff101010));
  125. g.fillRoundedRectangle (0.0f, 0.0f, static_cast<float> (getWidth() - 0), static_cast<float> (getHeight() - 0), 10.000f);
  126. g.setColour (Colours::white);
  127. g.drawRoundedRectangle (0.0f, 0.0f, static_cast<float> (getWidth() - 0), static_cast<float> (getHeight() - 0), 10.000f, 1.000f);
  128. g.setColour (Colours::black);
  129. g.fillRoundedRectangle (4.0f, 4.0f, static_cast<float> (getWidth() - 8), static_cast<float> (getHeight() - 36), 10.000f);
  130. g.setColour (Colours::grey);
  131. g.drawRoundedRectangle (4.0f, 4.0f, static_cast<float> (getWidth() - 8), static_cast<float> (getHeight() - 36), 10.000f, 1.000f);
  132. g.setColour (Colours::black);
  133. g.fillRoundedRectangle (4.0f, static_cast<float> (getHeight() - 28), static_cast<float> (getWidth() - 8), 24.0f, 10.000f);
  134. g.setColour (Colours::grey);
  135. g.drawRoundedRectangle (4.0f, static_cast<float> (getHeight() - 28), static_cast<float> (getWidth() - 8), 24.0f, 10.000f, 1.000f);
  136. g.setColour (Colour (0xff101010));
  137. g.fillRoundedRectangle (8.0f, 8.0f, static_cast<float> (getWidth() - 16), 20.0f, 10.000f);
  138. g.setColour (Colours::grey);
  139. g.drawRoundedRectangle (8.0f, 8.0f, static_cast<float> (getWidth() - 16), 20.0f, 10.000f, 1.000f);
  140. //[UserPaint] Add your own custom painting code here..
  141. #endif // DRAW_JUCER_BORDERS
  142. bool should_show_topic = shouldShowTopic() ;
  143. float chat_pane_w = static_cast<float>(getWidth()) ;
  144. float chat_pane_h = static_cast<float>(getHeight()) ;
  145. float font_size = static_cast<float>(getFontSize()) ;
  146. float chat_entry_h = font_size + GUI::CHAT_ENTRY_PADH ;
  147. float chat_entry_y = chat_pane_h - chat_entry_h - GUI::PAD2F ;
  148. float chat_entry_border_y = chat_entry_y - GUI::PADF ;
  149. float chat_h = chat_entry_y - GUI::PAD5F ;
  150. float topic_border_w = chat_pane_w - GUI::TOPIC_BORDER_PADW ;
  151. float chat_border_w = chat_pane_w - GUI::CHAT_BORDER_PADW ;
  152. float chat_border_h = chat_h + GUI::PAD2F ;
  153. float chat_entry_border_h = chat_entry_h + GUI::PAD2F ;
  154. Colour panel_background_color = GUI::BACKGROUND_L1_COLOR ;
  155. Colour panel_border_color = GUI::BORDER_L1_COLOR ;
  156. Colour chat_background_color = GUI::BACKGROUND_LTOP_COLOR ;
  157. Colour chat_border_color = GUI::BORDER_L2_COLOR ;
  158. Colour topic_background_color = (should_show_topic) ? GUI::BACKGROUND_L1_COLOR :
  159. chat_background_color ;
  160. Colour topic_border_color = (should_show_topic) ? GUI::BORDER_L3_COLOR :
  161. chat_background_color ;
  162. // chat panel border and fill
  163. g.setColour(panel_background_color) ;
  164. g.fillRoundedRectangle(GUI::CHAT_PANE_BORDER_X , GUI::CHAT_PANE_BORDER_Y , chat_pane_w ,
  165. chat_pane_h , GUI::BORDER_RADIUS ) ;
  166. g.setColour(panel_border_color) ;
  167. g.drawRoundedRectangle(GUI::CHAT_PANE_BORDER_X , GUI::CHAT_PANE_BORDER_Y , chat_pane_w ,
  168. chat_pane_h , GUI::BORDER_RADIUS , GUI::BORDER_PX) ;
  169. // chat border and fill
  170. g.setColour(chat_background_color) ;
  171. g.fillRoundedRectangle(GUI::CHAT_BORDER_X , GUI::CHAT_BORDER_Y , chat_border_w ,
  172. chat_border_h , GUI::BORDER_RADIUS ) ;
  173. g.setColour(chat_border_color) ;
  174. g.drawRoundedRectangle(GUI::CHAT_BORDER_X , GUI::CHAT_BORDER_Y , chat_border_w ,
  175. chat_border_h , GUI::BORDER_RADIUS , GUI::BORDER_PX) ;
  176. // topic border and fill
  177. g.setColour(topic_background_color) ;
  178. g.fillRoundedRectangle(GUI::TOPIC_BORDER_X , GUI::TOPIC_BORDER_Y , topic_border_w ,
  179. GUI::TOPIC_BORDER_H , GUI::BORDER_RADIUS ) ;
  180. g.setColour(topic_border_color) ;
  181. g.drawRoundedRectangle(GUI::TOPIC_BORDER_X , GUI::TOPIC_BORDER_Y , topic_border_w ,
  182. GUI::TOPIC_BORDER_H , GUI::BORDER_RADIUS , GUI::BORDER_PX ) ;
  183. // chat entry border and fill
  184. g.setColour(chat_background_color) ;
  185. g.fillRoundedRectangle(GUI::CHAT_BORDER_X , chat_entry_border_y , chat_border_w ,
  186. chat_entry_border_h , GUI::BORDER_RADIUS ) ;
  187. g.setColour(chat_border_color) ;
  188. g.drawRoundedRectangle(GUI::CHAT_BORDER_X , chat_entry_border_y , chat_border_w ,
  189. chat_entry_border_h , GUI::BORDER_RADIUS , GUI::BORDER_PX) ;
  190. //[/UserPaint]
  191. }
  192. void Chat::resized()
  193. {
  194. //[UserPreResize] Add your own custom resize code here..
  195. #ifdef DRAW_JUCER_CHAT_RESIZE
  196. //[/UserPreResize]
  197. chatText->setBounds (8, 8, getWidth() - 16, getHeight() - 44);
  198. chatEntryText->setBounds (8, getHeight() - 24, getWidth() - 16, 16);
  199. topicLabel->setBounds (12, 10, getWidth() - 24, 16);
  200. //[UserResized] Add your own custom resize handling here..
  201. #endif // DRAW_JUCER_CHAT_RESIZE
  202. bool should_show_topic = shouldShowTopic() ;
  203. int chat_y = ((should_show_topic) ? GUI::CHAT_WITH_TOPIC_Y : GUI::CHAT_Y) ;
  204. int chat_entry_h = getFontSize() + GUI::CHAT_ENTRY_PADH ;
  205. int chat_entry_y = getHeight() - chat_entry_h - GUI::CHAT_ENTRY_PADY ;
  206. int chat_w = getWidth() - GUI::CHAT_PADW ;
  207. int chat_h = chat_entry_y - chat_y - GUI::CHAT_PADH ;
  208. int topic_w = getWidth() - GUI::TOPIC_PADW ;
  209. // resize components according to topic visibility and font size
  210. this->topicLabel ->setVisible(should_show_topic) ;
  211. this->topicLabel ->setBounds(GUI::TOPIC_X , GUI::TOPIC_Y , topic_w , GUI::TOPIC_H);
  212. this->chatText ->setBounds(GUI::CHAT_X , chat_y , chat_w , chat_h ) ;
  213. this->chatEntryText->setBounds(GUI::CHAT_X , chat_entry_y , chat_w , chat_entry_h) ;
  214. repaint() ;
  215. //[/UserResized]
  216. }
  217. void Chat::labelTextChanged (Label* labelThatHasChanged)
  218. {
  219. //[UserlabelTextChanged_Pre]
  220. //[/UserlabelTextChanged_Pre]
  221. if (labelThatHasChanged == topicLabel)
  222. {
  223. //[UserLabelCode_topicLabel] -- add your label text handling code here..
  224. // post topic change message to server but defer updating GUI until ack
  225. LinJam::SendChat(CLIENT::CHATMSG_CMD_TOPIC + " " + this->topicLabel->getText()) ;
  226. this->topicLabel->setText(this->prevTopicText , juce::dontSendNotification) ;
  227. //[/UserLabelCode_topicLabel]
  228. }
  229. //[UserlabelTextChanged_Post]
  230. //[/UserlabelTextChanged_Post]
  231. }
  232. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  233. /* Chat public instance methods */
  234. void Chat::setTopic(String topic_text)
  235. {
  236. this->topicLabel->setText(topic_text , juce::dontSendNotification) ;
  237. this->prevTopicText = topic_text ;
  238. }
  239. void Chat::addChatLine(String chat_user , String chat_text)
  240. {
  241. this->chatText->moveCaretToEnd() ;
  242. this->chatText->insertTextAtCaret(chat_user + ": " + chat_text + "\n") ;
  243. this->chatText->moveCaretToEnd() ;
  244. }
  245. /* Chat private instance methods */
  246. void Chat::valueChanged(Value& a_value)
  247. {
  248. if (a_value.refersToSameSourceAs(this->fontSize)) setFontSize() ;
  249. }
  250. void Chat::textEditorReturnKeyPressed(TextEditor& a_text_editor)
  251. {
  252. if (&a_text_editor != this->chatEntryText) return ;
  253. LinJam::SendChat(this->chatEntryText->getText()) ;
  254. this->chatEntryText->clear() ;
  255. }
  256. void Chat::setFontSize()
  257. {
  258. DEBUG_TRACE_SET_FONTSIZE
  259. Font current_font = this->chatText->getFont() ;
  260. int new_font_size = getFontSize() ;
  261. Font new_font = current_font.withHeight(new_font_size) ;
  262. this->chatText ->applyFontToAllText(new_font) ;
  263. this->chatEntryText->applyFontToAllText(new_font) ;
  264. resized() ;
  265. }
  266. int Chat::getFontSize()
  267. {
  268. int font_size_n = int(this->fontSize.getValue()) ;
  269. int font_size = GUI::FONT_SIZES[font_size_n].getIntValue() ;
  270. return font_size ;
  271. }
  272. bool Chat::shouldShowTopic() { return (getHeight() > GUI::MIN_SHOW_TOPIC_CHAT_H) ; }
  273. //[/MiscUserCode]
  274. //==============================================================================
  275. #if 0
  276. /* -- Introjucer information section --
  277. This is where the Introjucer stores the metadata that describe this GUI layout, so
  278. make changes in here at your peril!
  279. BEGIN_JUCER_METADATA
  280. <JUCER_COMPONENT documentType="Component" className="Chat" componentName="Chat"
  281. parentClasses="public Component, public TextEditor::Listener, public ValueListener"
  282. constructorParams="Value font_size" variableInitialisers="fontSize(font_size)"
  283. snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330"
  284. fixedSize="0" initialWidth="622" initialHeight="162">
  285. <BACKGROUND backgroundColour="0">
  286. <ROUNDRECT pos="0 0 0M 0M" cornerSize="10" fill="solid: ff101010" hasStroke="1"
  287. stroke="1, mitered, butt" strokeColour="solid: ffffffff"/>
  288. <ROUNDRECT pos="4 4 8M 36M" cornerSize="10" fill="solid: ff000000" hasStroke="1"
  289. stroke="1, mitered, butt" strokeColour="solid: ff808080"/>
  290. <ROUNDRECT pos="4 28R 8M 24" cornerSize="10" fill="solid: ff000000" hasStroke="1"
  291. stroke="1, mitered, butt" strokeColour="solid: ff808080"/>
  292. <ROUNDRECT pos="8 8 16M 20" cornerSize="10" fill="solid: ff101010" hasStroke="1"
  293. stroke="1, mitered, butt" strokeColour="solid: ff808080"/>
  294. </BACKGROUND>
  295. <TEXTEDITOR name="chatText" id="ba11ad8bfe4752c1" memberName="chatText" virtualName=""
  296. explicitFocusOrder="0" pos="8 8 16M 44M" textcol="ff808080" bkgcol="0"
  297. hilitecol="0" outlinecol="0" initialText="" multiline="1" retKeyStartsLine="0"
  298. readonly="1" scrollbars="1" caret="0" popupmenu="1"/>
  299. <TEXTEDITOR name="chatEntryText" id="412133d948ede027" memberName="chatEntryText"
  300. virtualName="" explicitFocusOrder="1" pos="8 24R 16M 16" textcol="ff808080"
  301. bkgcol="0" hilitecol="0" outlinecol="0" caretcol="ffffffff" initialText=""
  302. multiline="0" retKeyStartsLine="0" readonly="0" scrollbars="0"
  303. caret="1" popupmenu="1"/>
  304. <LABEL name="topicLabel" id="3544f00ed11410e1" memberName="topicLabel"
  305. virtualName="" explicitFocusOrder="0" pos="12 10 24M 16" bkgCol="0"
  306. textCol="ff808080" edTextCol="ff808080" edBkgCol="0" hiliteCol="ffbbbbff"
  307. labelText="" editableSingleClick="1" editableDoubleClick="1"
  308. focusDiscardsChanges="1" fontname="Default monospaced font" fontsize="15"
  309. bold="1" italic="0" justification="12"/>
  310. </JUCER_COMPONENT>
  311. END_JUCER_METADATA
  312. */
  313. #endif
  314. //[EndFile] You can add extra defines here...
  315. //[/EndFile]