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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#include "stdafx.h"
#include "Screen.h"
#include "Button.h"
#include "ChatScreen.h"
#include "GuiParticles.h"
#include "Tesselator.h"
#include "Textures.h"
#include "..\Minecraft.World\SoundTypes.h"
#ifdef _WINDOWS64
#include "Windows64\KeyboardMouseInput.h"
#endif
Screen::Screen() // 4J added
{
minecraft = nullptr;
width = 0;
height = 0;
passEvents = false;
font = nullptr;
particles = nullptr;
clickedButton = nullptr;
}
void Screen::render(int xm, int ym, float a)
{
for (Button* button : buttons)
{
if ( button )
button->render(minecraft, xm, ym);
}
}
void Screen::keyPressed(wchar_t eventCharacter, int eventKey)
{
if (eventKey == Keyboard::KEY_ESCAPE)
{
minecraft->setScreen(nullptr);
// minecraft->grabMouse(); // 4J - removed
}
}
wstring Screen::getClipboard()
{
#ifdef _WINDOWS64
if (!OpenClipboard(nullptr)) return wstring();
HANDLE h = GetClipboardData(CF_UNICODETEXT);
wstring out;
if (h)
{
const wchar_t *p = static_cast<const wchar_t*>(GlobalLock(h));
if (p) { out = p; GlobalUnlock(h); }
}
CloseClipboard();
return out;
#else
return wstring();
#endif
}
void Screen::setClipboard(const wstring& str)
{
#ifdef _WINDOWS64
if (!OpenClipboard(nullptr)) return;
EmptyClipboard();
size_t len = (str.length() + 1) * sizeof(wchar_t);
HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE, len);
if (h) { memcpy(GlobalLock(h), str.c_str(), len); GlobalUnlock(h); SetClipboardData(CF_UNICODETEXT, h); }
CloseClipboard();
#endif
}
void Screen::mouseClicked(int x, int y, int buttonNum)
{
if (buttonNum == 0)
{
for (Button* button : buttons)
{
if ( button && button->clicked(minecraft, x, y) )
{
clickedButton = button;
minecraft->soundEngine->playUI(eSoundType_RANDOM_CLICK, 1, 1);
buttonClicked(button);
}
}
}
}
void Screen::mouseReleased(int x, int y, int buttonNum)
{
if (clickedButton!=nullptr && buttonNum==0)
{
clickedButton->released(x, y);
clickedButton = nullptr;
}
}
void Screen::buttonClicked(Button *button)
{
}
void Screen::init(Minecraft *minecraft, int width, int height)
{
particles = new GuiParticles(minecraft);
this->minecraft = minecraft;
this->font = minecraft->font;
this->width = width;
this->height = height;
buttons.clear();
init();
}
void Screen::setSize(int width, int height)
{
this->width = width;
this->height = height;
}
void Screen::init()
{
}
void Screen::updateEvents()
{
#ifdef _WINDOWS64
// Poll mouse button state and dispatch click/release events
for (int btn = 0; btn < 3; btn++)
{
if (g_KBMInput.IsMouseButtonPressed(btn))
{
int xm = Mouse::getX() * width / minecraft->width;
int ym = height - Mouse::getY() * height / minecraft->height - 1;
mouseClicked(xm, ym, btn);
}
if (g_KBMInput.IsMouseButtonReleased(btn))
{
int xm = Mouse::getX() * width / minecraft->width;
int ym = height - Mouse::getY() * height / minecraft->height - 1;
mouseReleased(xm, ym, btn);
}
}
// Only drain WM_CHAR when this screen wants text input (e.g. ChatScreen); otherwise we'd steal keys from the game
if (dynamic_cast<ChatScreen*>(this) != nullptr)
{
wchar_t ch;
while (g_KBMInput.ConsumeChar(ch))
{
if (ch >= 0x20)
keyPressed(ch, -1);
else if (ch == 0x08)
keyPressed(0, Keyboard::KEY_BACK);
else if (ch == 0x0D)
keyPressed(0, Keyboard::KEY_RETURN);
}
}
// Arrow key repeat: deliver on first press (when key down and last==0) and while held (throttled)
static DWORD s_arrowLastTime[2] = { 0, 0 };
static bool s_arrowFirstRepeat[2] = { false, false };
const DWORD ARROW_REPEAT_DELAY_MS = 250;
const DWORD ARROW_REPEAT_INTERVAL_MS = 50;
DWORD now = GetTickCount();
// Poll keyboard events (special keys that may not come through WM_CHAR, e.g. Escape, arrows)
for (int vk = 0; vk < 256; vk++)
{
bool deliver = g_KBMInput.IsKeyPressed(vk);
if (vk == VK_LEFT || vk == VK_RIGHT)
{
int idx = (vk == VK_LEFT) ? 0 : 1;
if (!g_KBMInput.IsKeyDown(vk))
{
s_arrowLastTime[idx] = 0;
s_arrowFirstRepeat[idx] = false;
}
else
{
DWORD last = s_arrowLastTime[idx];
if (last == 0)
deliver = true;
else if (!deliver)
{
DWORD interval = s_arrowFirstRepeat[idx] ? ARROW_REPEAT_INTERVAL_MS : ARROW_REPEAT_DELAY_MS;
if ((now - last) >= interval)
{
deliver = true;
s_arrowFirstRepeat[idx] = true;
}
}
if (deliver)
s_arrowLastTime[idx] = now;
}
}
// Escape: deliver when key is down so we don't miss it (IsKeyPressed can be one frame late)
if (vk == VK_ESCAPE && g_KBMInput.IsKeyDown(VK_ESCAPE))
deliver = true;
if (!deliver) continue;
if (dynamic_cast<ChatScreen*>(this) != nullptr &&
(vk >= 'A' && vk <= 'Z' || vk >= '0' && vk <= '9' || vk == VK_SPACE || vk == VK_RETURN || vk == VK_BACK))
continue;
// Map to Screen::keyPressed
int mappedKey = -1;
wchar_t ch = 0;
if (vk == VK_ESCAPE) mappedKey = Keyboard::KEY_ESCAPE;
else if (vk == VK_RETURN) mappedKey = Keyboard::KEY_RETURN;
else if (vk == VK_BACK) mappedKey = Keyboard::KEY_BACK;
else if (vk == VK_UP) mappedKey = Keyboard::KEY_UP;
else if (vk == VK_DOWN) mappedKey = Keyboard::KEY_DOWN;
else if (vk == VK_LEFT) mappedKey = Keyboard::KEY_LEFT;
else if (vk == VK_RIGHT) mappedKey = Keyboard::KEY_RIGHT;
else if (vk == VK_LSHIFT || vk == VK_RSHIFT) mappedKey = Keyboard::KEY_LSHIFT;
else if (vk == VK_TAB) mappedKey = Keyboard::KEY_TAB;
else if (vk >= 'A' && vk <= 'Z')
{
ch = static_cast<wchar_t>(vk - 'A' + L'a');
if (g_KBMInput.IsKeyDown(VK_LSHIFT) || g_KBMInput.IsKeyDown(VK_RSHIFT)) ch = static_cast<wchar_t>(vk);
}
else if (vk >= '0' && vk <= '9') ch = static_cast<wchar_t>(vk);
else if (vk == VK_SPACE) ch = L' ';
if (mappedKey != -1) keyPressed(ch, mappedKey);
else if (ch != 0) keyPressed(ch, -1);
}
#else
/* 4J - TODO
while (Mouse.next()) {
mouseEvent();
}
while (Keyboard.next()) {
keyboardEvent();
}
*/
#endif
}
void Screen::mouseEvent()
{
#ifdef _WINDOWS64
// Mouse event dispatching is handled directly in updateEvents() for Windows
#else
/* 4J - TODO
if (Mouse.getEventButtonState()) {
int xm = Mouse.getEventX() * width / minecraft.width;
int ym = height - Mouse.getEventY() * height / minecraft.height - 1;
mouseClicked(xm, ym, Mouse.getEventButton());
} else {
int xm = Mouse.getEventX() * width / minecraft.width;
int ym = height - Mouse.getEventY() * height / minecraft.height - 1;
mouseReleased(xm, ym, Mouse.getEventButton());
}
*/
#endif
}
void Screen::keyboardEvent()
{
/* 4J - TODO
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_F11) {
minecraft.toggleFullScreen();
return;
}
keyPressed(Keyboard.getEventCharacter(), Keyboard.getEventKey());
}
*/
}
void Screen::tick()
{
}
void Screen::removed()
{
}
void Screen::renderBackground()
{
renderBackground(0);
}
void Screen::renderBackground(int vo)
{
if (minecraft->level != nullptr)
{
fillGradient(0, 0, width, height, 0xc0101010, 0xd0101010);
}
else
{
renderDirtBackground(vo);
}
}
void Screen::renderDirtBackground(int vo)
{
// 4J Unused
#if 0
glDisable(GL_LIGHTING);
glDisable(GL_FOG);
Tesselator *t = Tesselator::getInstance();
glBindTexture(GL_TEXTURE_2D, minecraft->textures->loadTexture(L"/gui/background.png"));
glColor4f(1, 1, 1, 1);
float s = 32;
t->begin();
t->color(0x404040);
t->vertexUV(static_cast<float>(0), static_cast<float>(height), static_cast<float>(0), static_cast<float>(0), static_cast<float>(height / s + vo));
t->vertexUV(static_cast<float>(width), static_cast<float>(height), static_cast<float>(0), static_cast<float>(width / s), static_cast<float>(height / s + vo));
t->vertexUV(static_cast<float>(width), static_cast<float>(0), static_cast<float>(0), static_cast<float>(width / s), static_cast<float>(0 + vo));
t->vertexUV(static_cast<float>(0), static_cast<float>(0), static_cast<float>(0), static_cast<float>(0), static_cast<float>(0 + vo));
t->end();
#endif
}
bool Screen::isPauseScreen()
{
return true;
}
void Screen::confirmResult(bool result, int id)
{
}
void Screen::tabPressed()
{
}
|