aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/ChatScreen.cpp
blob: 53c90722426bdc851b3ae3f4e69e11592fa38bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "stdafx.h"
#include "ChatScreen.h"
#include "ClientConnection.h"
#include "Font.h"
#include "MultiplayerLocalPlayer.h"
#include "..\Minecraft.World\SharedConstants.h"
#include "..\Minecraft.World\StringHelpers.h"
#include "..\Minecraft.World\ChatPacket.h"

const wstring ChatScreen::allowedChars = SharedConstants::acceptableLetters;
vector<wstring> ChatScreen::s_chatHistory;
int ChatScreen::s_historyIndex = -1;
wstring ChatScreen::s_historyDraft;

bool ChatScreen::isAllowedChatChar(wchar_t c)
{
	return c >= 0x20 && (c == L'\u00A7' || allowedChars.empty() || allowedChars.find(c) != wstring::npos);
}

ChatScreen::ChatScreen()
{
	frame = 0;
	cursorIndex = 0;
	s_historyIndex = -1;
}

void ChatScreen::init()
{
	Keyboard::enableRepeatEvents(true);
}

void ChatScreen::removed()
{
	Keyboard::enableRepeatEvents(false);
}

void ChatScreen::tick()
{
	frame++;
	if (cursorIndex > static_cast<int>(message.length()))
		cursorIndex = static_cast<int>(message.length());
}

void ChatScreen::handlePasteRequest()
{
	wstring pasted = Screen::getClipboard();
	for (size_t i = 0; i < pasted.length() && static_cast<int>(message.length()) < SharedConstants::maxChatLength; i++)
	{
		if (isAllowedChatChar(pasted[i]))
		{
			message.insert(cursorIndex, 1, pasted[i]);
			cursorIndex++;
		}
	}
}

void ChatScreen::applyHistoryMessage()
{
	message = s_historyIndex >= 0 ? s_chatHistory[s_historyIndex] : s_historyDraft;
	cursorIndex = static_cast<int>(message.length());
}

void ChatScreen::handleHistoryUp()
{
	if (s_chatHistory.empty()) return;
	if (s_historyIndex == -1)
	{
		s_historyDraft = message;
		s_historyIndex = static_cast<int>(s_chatHistory.size()) - 1;
	}
	else if (s_historyIndex > 0)
		s_historyIndex--;
	applyHistoryMessage();
}

void ChatScreen::handleHistoryDown()
{
	if (s_chatHistory.empty()) return;
	if (s_historyIndex < static_cast<int>(s_chatHistory.size()) - 1)
		s_historyIndex++;
	else
		s_historyIndex = -1;
	applyHistoryMessage();
}

void ChatScreen::keyPressed(wchar_t ch, int eventKey)
{
    if (eventKey == Keyboard::KEY_ESCAPE)
	{
        minecraft->setScreen(nullptr);
        return;
    }
    if (eventKey == Keyboard::KEY_RETURN)
	{
        wstring trim = trimString(message);
        if (trim.length() > 0)
		{
            if (!minecraft->handleClientSideCommand(trim))
			{
                MultiplayerLocalPlayer* mplp = dynamic_cast<MultiplayerLocalPlayer*>(minecraft->player.get());
                if (mplp && mplp->connection)
                    mplp->connection->send(shared_ptr<ChatPacket>(new ChatPacket(trim)));
            }
            if (s_chatHistory.empty() || s_chatHistory.back() != trim)
			{
                s_chatHistory.push_back(trim);
                if (s_chatHistory.size() > CHAT_HISTORY_MAX)
                    s_chatHistory.erase(s_chatHistory.begin());
            }
        }
        minecraft->setScreen(nullptr);
        return;
    }
    if (eventKey == Keyboard::KEY_UP)   { handleHistoryUp();   return; }
    if (eventKey == Keyboard::KEY_DOWN) { handleHistoryDown(); return; }
    if (eventKey == Keyboard::KEY_LEFT)
	{
        if (cursorIndex > 0)
            cursorIndex--;
        return;
    }
    if (eventKey == Keyboard::KEY_RIGHT)
	{
        if (cursorIndex < static_cast<int>(message.length()))
            cursorIndex++;
        return;
    }
    if (eventKey == Keyboard::KEY_BACK && cursorIndex > 0)
	{
        message.erase(cursorIndex - 1, 1);
        cursorIndex--;
        return;
    }
    if (isAllowedChatChar(ch) && static_cast<int>(message.length()) < SharedConstants::maxChatLength)
	{
        message.insert(cursorIndex, 1, ch);
        cursorIndex++;
    }
}

void ChatScreen::render(int xm, int ym, float a)
{
    fill(2, height - 14, width - 2, height - 2, 0x80000000);
    const wstring prefix = L"> ";
    int x = 4;
    drawString(font, prefix, x, height - 12, 0xe0e0e0);
    x += font->width(prefix);
    wstring beforeCursor = message.substr(0, cursorIndex);
    wstring afterCursor = message.substr(cursorIndex);
    drawStringLiteral(font, beforeCursor, x, height - 12, 0xe0e0e0);
    x += font->widthLiteral(beforeCursor);
    if (frame / 6 % 2 == 0)
        drawString(font, L"_", x, height - 12, 0xe0e0e0);
    x += font->width(L"_");
    drawStringLiteral(font, afterCursor, x, height - 12, 0xe0e0e0);
    Screen::render(xm, ym, a);
}

void ChatScreen::mouseClicked(int x, int y, int buttonNum)
{
    if (buttonNum == 0)
	{
        if (minecraft->gui->selectedName != L"")	// 4J - was nullptr comparison
		{
			if (message.length() > 0 && message[message.length()-1]!=L' ')
			{
                message = message.substr(0, cursorIndex) + L" " + message.substr(cursorIndex);
                cursorIndex++;
            }
            size_t nameLen = minecraft->gui->selectedName.length();
            size_t insertLen = (message.length() + nameLen <= SharedConstants::maxChatLength) ? nameLen : (SharedConstants::maxChatLength - message.length());
            if (insertLen > 0)
			{
                message = message.substr(0, cursorIndex) + minecraft->gui->selectedName.substr(0, insertLen) + message.substr(cursorIndex);
                cursorIndex += static_cast<int>(insertLen);
            }
        }
		else
		{
            Screen::mouseClicked(x, y, buttonNum);
        }
    }
}