-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathExEditBox.cpp
161 lines (148 loc) · 4.99 KB
/
ExEditBox.cpp
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
/*==========================================================
* D2Ex2
* https://github.com/lolet/D2Ex2
* ==========================================================
* Copyright (c) 2011-2014 Bartosz Jankowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==========================================================
*/
#include "stdafx.h"
#include "ExEditBox.h"
#include "ExScreen.h"
ExEditBox::ExEditBox(int X, int Y, int TextXOffset, int TextYOffset, int TextLen, int TextFont, wstring szLabel, string szFile) : ExControl(X, Y, -1, -1, 0)
{
try
{
aCellFile = unique_ptr<ExCellFile>(new ExCellFile(szFile));
}
catch (const CellLoadError&)
{
D2EXERROR("Cannot create edit box because of missing or corrupted DC6 file!")
}
aTextBox = 0;
aCellFile->SetFrame(0);
cWidth = aCellFile->Get()->pCellFile->cells[0]->width;
cHeight = aCellFile->Get()->pCellFile->cells[0]->height;
Text = L"";
TextMax = TextLen;
isFocused = false;
isHashed = false;
TextX = TextXOffset;
TextY = TextYOffset;
aFont = TextFont;
event_onChange = NULL;
CursorPos = Text.length();
Color = COL_WHITE;
TextPos = 0;
if (!szLabel.empty()) {
aTextBox = new ExTextBox(X, Y - cHeight, COL_WHITE, 0, szLabel, 0);
//TODO Set this as a child
}
}
void ExEditBox::SetHashed(bool How)
{
if (How) isHashed = true;
else isHashed = false;
}
void ExEditBox::SetChangeEvent(void(*event_Change)(ExEditBox*))
{
event_onChange = event_Change;
}
void ExEditBox::Draw()
{
if (cState == INVISIBLE) return;
D2Funcs.D2WIN_SetTextSize(aFont);
D2Funcs.D2GFX_DrawCellContextEx(aCellFile->Get(), cX, cY, -1, 5, cState == VISIBLE ? 0 : 1);
int TextWid = 0;
if (!Text.empty())
{
if ((unsigned)TextPos > Text.length()) TextPos = 0;
wstring Buffer(Text.substr(TextPos));
TextWid = ExScreen::GetTextWidthEx(Buffer.c_str(), aFont);
while (TextWid > (cWidth - (TextX * 2) - 5))
{
TextWid = ExScreen::GetTextWidthEx(Buffer.c_str(), aFont);
if (Buffer.empty()) break;
Buffer.erase(Buffer.length());
}
if (isHashed) Buffer.assign(Buffer.length(), L'*');
D2Funcs.D2WIN_DrawText(Buffer.c_str(), cX + TextX, cY - TextY, Color, 0);
TextWid = ExScreen::GetTextWidthEx(Buffer.substr(0, CursorPos).c_str(), aFont);
}
if (!isFocused) return;
if (GetTickCount() % 50 == 0) D2Funcs.D2GFX_DrawLine(TextWid + cX + TextX, cY - TextY, TextWid + cX + TextX + 5, cY - TextY, 255, 0);
aTextBox->Draw();
}
bool ExEditBox::isPressed(DWORD Sender, WPARAM wParam)
{
if (cState == INVISIBLE) return false;
switch (Sender)
{
case WM_LBUTTONDOWN:
if (cState == DISABLED) return false;
if (*D2Vars.D2CLIENT_MouseX >= cX && *D2Vars.D2CLIENT_MouseX <= cX + cWidth && *D2Vars.D2CLIENT_MouseY <= cY && *D2Vars.D2CLIENT_MouseY >= cY - cHeight)
{
bBeingPressed = true;
return true;
}
break;
case WM_LBUTTONUP:
if (cState == DISABLED) return false;
if (*D2Vars.D2CLIENT_MouseX >= cX && *D2Vars.D2CLIENT_MouseX <= cX + cWidth && *D2Vars.D2CLIENT_MouseY <= cY && *D2Vars.D2CLIENT_MouseY >= cY - cHeight)
{
if (cState == VISIBLE) {
D2ASMFuncs::D2CLIENT_PlaySound(ExSounds::STAND_CLICK); isFocused = true;
}
bBeingPressed = false;
return true;
}
else
{
if (cState == VISIBLE) isFocused = false;
}
break;
case WM_KEYDOWN:
{
if (!isFocused) return false;
bool shiftState = ((GetKeyState(VK_LSHIFT) & 0x80) || (GetKeyState(VK_RSHIFT) & 0x80));
wstring OldText(Text);
switch (wParam)
{
case VK_BACK: { if (CursorPos > 0) { Text.erase(CursorPos - 1, 1); CursorPos--; if (ExScreen::GetTextWidthEx(Text.c_str(), 6) > (cWidth - (TextX * 2) - 5)) TextPos--; else TextPos = 0; } break; }
case VK_DELETE: { if (Text.length()) Text.erase(CursorPos, 1); break; }
case VK_LEFT: { if (CursorPos > 0) CursorPos--; if (ExScreen::GetTextWidthEx(Text.c_str(),6) > (cWidth - (TextX * 2) - 5)) TextPos--; else TextPos = 0; break; }
case VK_RIGHT: { if ((unsigned)CursorPos<Text.length()) CursorPos++; if (ExScreen::GetTextWidthEx(Text.substr(TextPos).c_str(), aFont)>(cWidth - (TextX * 2) - 5) && TextPos < Text.length()) TextPos++; break; }
case 0x13: {isFocused = false; break; }
default:
{
if (Text.length() == TextMax) break;
wchar_t key = (wchar_t)wParam;
if (!iswalnum(key) && !iswspace(key)) break;
if (!shiftState) key = towlower(key);
Text.insert(CursorPos, 1, key); CursorPos++;
if (ExScreen::GetTextWidthEx(Text.c_str(), aFont) > (cWidth - (TextX * 2) - 5)) TextPos++;
break;
}
}
if (OldText != Text && event_onChange) event_onChange(this);
return true;
}
break;
}
return false;
}
ExEditBox::~ExEditBox(void)
{
delete aTextBox;
}