1 #include "chatDialog.h"
2 #include "gameGlobalInfo.h"
3 #include "spaceObjects/playerSpaceship.h"
4
5 #include "screenComponents/radarView.h"
6
7 #include "gui/gui2_textentry.h"
8 #include "gui/gui2_scrolltext.h"
9
GameMasterChatDialog(GuiContainer * owner,GuiRadarView * radar,int index)10 GameMasterChatDialog::GameMasterChatDialog(GuiContainer* owner, GuiRadarView* radar, int index)
11 : GuiResizableDialog(owner, "", "")
12 {
13 this->player_index = index;
14 this->radar = radar;
15
16 text_entry = new GuiTextEntry(contents, "", "");
17 text_entry->setTextSize(23)->setPosition(0, 0, ABottomLeft)->setSize(GuiElement::GuiSizeMax, 30);
18 text_entry->enterCallback([this](string text){
19 if (this->player)
20 {
21 if (this->player->isCommsChatOpenToGM())
22 this->player->addCommsIncommingMessage(text_entry->getText());
23 else
24 this->player->hailCommsByGM(text_entry->getText());
25 }
26 text_entry->setText("");
27 });
28
29 chat_text = new GuiScrollText(contents, "", "");
30 chat_text->setTextSize(20)->setPosition(0, -30, ABottomLeft)->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
31 chat_text->enableAutoScrollDown()->setScrollbarWidth(30);
32
33 min_size.y += 100;
34
35 notification = false;
36 }
37
onDraw(sf::RenderTarget & window)38 void GameMasterChatDialog::onDraw(sf::RenderTarget& window)
39 {
40 GuiResizableDialog::onDraw(window);
41 if (!player)
42 player = gameGlobalInfo->getPlayerShip(player_index);
43
44 if (!player)
45 {
46 disableComms(tr("chatGM", "Target - Destroyed"));
47 return;
48 }
49
50 if (!isMinimized())
51 notification = false;
52
53 switch(player->getCommsState())
54 {
55 case CS_Inactive:
56 case CS_ChannelFailed:
57 case CS_ChannelBroken:
58 case CS_ChannelClosed:
59 chat_text->setText(tr("chatGM", "Channel not open, enter name to hail as to hail target."));
60 disableComms(tr("chatGM", "{callsign} - Inactive").format({{"callsign", player->getCallSign()}}));
61 break;
62 case CS_OpeningChannel:
63 case CS_BeingHailed:
64 disableComms(tr("chatGM", "{callsign} - Opening communications with {target}").format({{"callsign", player->getCallSign()}, {"target", player->getCommsTargetName()}}));
65 break;
66 case CS_BeingHailedByGM:
67 disableComms(tr("chatGM", "{callsign} - Hailing as {target}").format({{"callsign", player->getCallSign()}, {"target", player->getCommsTargetName()}}));
68 break;
69 case CS_ChannelOpen:
70 case CS_ChannelOpenPlayer:
71 disableComms(tr("chatGM", "{callsign} - Communicating with {target}").format({{"callsign", player->getCallSign()}, {"target", player->getCommsTargetName()}}));
72 break;
73 case CS_ChannelOpenGM:
74 if (notification)
75 setTitle(tr("chatGM", "**{callsign} - Communicating as {target}**").format({{"callsign", player->getCallSign()}, {"target", player->getCommsTargetName()}}));
76 else
77 setTitle(tr("chatGM", "{callsign} - Communicating as {target}").format({{"callsign", player->getCallSign()}, {"target", player->getCommsTargetName()}}));
78 chat_text->enable();
79 text_entry->enable();
80 if (chat_text->getText() != player->getCommsIncommingMessage())
81 {
82 chat_text->setText(player->getCommsIncommingMessage());
83 notification = true;
84 }
85 if (player->getCommsTarget())
86 drawLine(window, radar->worldToScreen(player->getCommsTarget()->getPosition()));
87 break;
88 }
89 drawLine(window, radar->worldToScreen(player->getPosition()));
90 }
91
disableComms(string title)92 void GameMasterChatDialog::disableComms(string title)
93 {
94 if (notification)
95 title = "**" + title + "**";
96 setTitle(title);
97 chat_text->disable();
98 text_entry->enable();
99 }
100
onClose()101 void GameMasterChatDialog::onClose()
102 {
103 if (player && (player->isCommsChatOpenToGM() || player->isCommsBeingHailedByGM()))
104 {
105 player->closeComms();
106 }
107 hide();
108 }
109
drawLine(sf::RenderTarget & window,sf::Vector2f target)110 void GameMasterChatDialog::drawLine(sf::RenderTarget& window, sf::Vector2f target)
111 {
112 sf::VertexArray a(sf::LinesStrip, 2);
113 a[0].position = getCenterPoint();
114 a[1].position = target;
115 a[0].color = sf::Color(128,255,128,128);
116 a[1].color = a[0].color;
117 window.draw(a);
118 }
119