aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/NameEntryScreen.cpp
blob: f498d4ea3346c35a4cca6546d2fb437694603b29 (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
#include "stdafx.h"
#include "NameEntryScreen.h"
#include "Button.h"
#include "..\Minecraft.World\StringHelpers.h"

const wstring NameEntryScreen::allowedChars = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.:-_'*!\"#%/()=+?[]{}<>";

NameEntryScreen::NameEntryScreen(Screen *lastScreen, const wstring& oldName, int slot)
{
	frame = 0; // 4J added

    this->lastScreen = lastScreen;
    this->slot = slot;
    this->name = oldName;
    if (name==L"-") name = L"";
}

void NameEntryScreen::init()
{
    buttons.clear();
    Keyboard::enableRepeatEvents(true);
    buttons.push_back(new Button(0, width / 2 - 100, height / 4 + 24 * 5, L"Save"));
    buttons.push_back(new Button(1, width / 2 - 100, height / 4 + 24 * 6, L"Cancel"));
    buttons[0]->active = trimString(name).length() > 1;
}

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

void NameEntryScreen::tick()
{
	frame++;
}

void NameEntryScreen::buttonClicked(Button button)
{
    if (!button.active) return;

    if (button.id == 0 && trimString(name).length() > 1)
    {
        minecraft->saveSlot(slot, trimString(name));
        minecraft->setScreen(nullptr);
//        minecraft->grabMouse();	// 4J - removed
    }
    if (button.id == 1)
    {
        minecraft->setScreen(lastScreen);
    }
}

void NameEntryScreen::keyPressed(wchar_t ch, int eventKey)
{
    if (eventKey == Keyboard::KEY_BACK && name.length() > 0) name = name.substr(0, name.length() - 1);
    if (allowedChars.find(ch) != wstring::npos && name.length()<64)
    {
        name += ch;
    }
    buttons[0]->active = trimString(name).length() > 1;
}

void NameEntryScreen::render(int xm, int ym, float a)
{
    renderBackground();

    drawCenteredString(font, title, width / 2, 40, 0xffffff);

    int bx = width / 2 - 100;
    int by = height / 2 - 10;
    int bw = 200;
    int bh = 20;
    fill(bx - 1, by - 1, bx + bw + 1, by + bh + 1, 0xffa0a0a0);
    fill(bx, by, bx + bw, by + bh, 0xff000000);
    drawString(font, name + (frame / 6 % 2 == 0 ? L"_" : L""), bx + 4, by + (bh - 8) / 2, 0xe0e0e0);

    Screen::render(xm, ym, a);
}