File: | Screens.c |
Warning: | line 189, column 21 Value stored to 'line1' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | #include "Screens.h" |
2 | #include "Widgets.h" |
3 | #include "Game.h" |
4 | #include "Event.h" |
5 | #include "Platform.h" |
6 | #include "Inventory.h" |
7 | #include "Drawer2D.h" |
8 | #include "Graphics.h" |
9 | #include "Funcs.h" |
10 | #include "TexturePack.h" |
11 | #include "Model.h" |
12 | #include "Generator.h" |
13 | #include "Server.h" |
14 | #include "Chat.h" |
15 | #include "ExtMath.h" |
16 | #include "Window.h" |
17 | #include "Camera.h" |
18 | #include "Http.h" |
19 | #include "Block.h" |
20 | #include "Menus.h" |
21 | #include "World.h" |
22 | #include "Input.h" |
23 | #include "Utils.h" |
24 | |
25 | #define CHAT_MAX_STATUS(sizeof(Chat_Status) / sizeof(Chat_Status[0])) Array_Elems(Chat_Status)(sizeof(Chat_Status) / sizeof(Chat_Status[0])) |
26 | #define CHAT_MAX_BOTTOMRIGHT(sizeof(Chat_BottomRight) / sizeof(Chat_BottomRight[0])) Array_Elems(Chat_BottomRight)(sizeof(Chat_BottomRight) / sizeof(Chat_BottomRight[0])) |
27 | #define CHAT_MAX_CLIENTSTATUS(sizeof(Chat_ClientStatus) / sizeof(Chat_ClientStatus[0])) Array_Elems(Chat_ClientStatus)(sizeof(Chat_ClientStatus) / sizeof(Chat_ClientStatus[0])) |
28 | |
29 | int Screen_FInput(void* s, int key) { return false0; } |
30 | int Screen_FKeyPress(void* s, char keyChar) { return false0; } |
31 | int Screen_FText(void* s, const cc_string* str) { return false0; } |
32 | int Screen_FMouseScroll(void* s, float delta) { return false0; } |
33 | int Screen_FPointer(void* s, int id, int x, int y) { return false0; } |
34 | |
35 | int Screen_TInput(void* s, int key) { return true1; } |
36 | int Screen_TKeyPress(void* s, char keyChar) { return true1; } |
37 | int Screen_TText(void* s, const cc_string* str) { return true1; } |
38 | int Screen_TMouseScroll(void* s, float delta) { return true1; } |
39 | int Screen_TPointer(void* s, int id, int x, int y) { return true1; } |
40 | |
41 | void Screen_NullFunc(void* screen) { } |
42 | void Screen_NullUpdate(void* screen, double delta) { } |
43 | |
44 | /* TODO: Remove these */ |
45 | struct HUDScreen; |
46 | struct ChatScreen; |
47 | static struct HUDScreen* Gui_HUD; |
48 | static struct ChatScreen* Gui_Chat; |
49 | |
50 | static cc_bool InventoryScreen_IsHotbarActive(void); |
51 | CC_NOINLINE__attribute__((noinline)) static cc_bool IsOnlyChatActive(void) { |
52 | struct Screen* s; |
53 | int i; |
54 | |
55 | for (i = 0; i < Gui.ScreensCount; i++) { |
56 | s = Gui_Screens[i]; |
57 | if (s->grabsInput && s != (struct Screen*)Gui_Chat) return false0; |
58 | } |
59 | return true1; |
60 | } |
61 | |
62 | |
63 | /*########################################################################################################################* |
64 | *--------------------------------------------------------HUDScreen--------------------------------------------------------* |
65 | *#########################################################################################################################*/ |
66 | static struct HUDScreen { |
67 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
68 | struct FontDesc font; |
69 | struct TextWidget line1, line2; |
70 | struct TextAtlas posAtlas; |
71 | double accumulator; |
72 | int frames, fps; |
73 | cc_bool hacksChanged; |
74 | float lastSpeed; |
75 | int lastFov; |
76 | struct HotbarWidget hotbar; |
77 | } HUDScreen_Instance; |
78 | |
79 | static void HUDScreen_MakeText(struct HUDScreen* s, cc_string* status) { |
80 | int indices, ping; |
81 | s->fps = (int)(s->frames / s->accumulator); |
82 | String_Format1(status, "%i fps, ", &s->fps); |
83 | |
84 | if (Game_ClassicMode) { |
85 | String_Format1(status, "%i chunk updates", &Game.ChunkUpdates); |
86 | } else { |
87 | if (Game.ChunkUpdates) { |
88 | String_Format1(status, "%i chunks/s, ", &Game.ChunkUpdates); |
89 | } |
90 | |
91 | indices = ICOUNT(Game_Vertices)(((Game_Vertices) >> 2) * 6); |
92 | String_Format1(status, "%i vertices", &indices); |
93 | |
94 | ping = Ping_AveragePingMS(); |
95 | if (ping) String_Format1(status, ", ping %i ms", &ping); |
96 | } |
97 | } |
98 | |
99 | static void HUDScreen_DrawPosition(struct HUDScreen* s) { |
100 | struct VertexTextured vertices[4 * 64]; |
101 | struct VertexTextured* ptr = vertices; |
102 | PackedCol col = PACKEDCOL_WHITE(((cc_uint8)(255) << 0) | ((cc_uint8)(255) << 8) | ((cc_uint8)(255) << 16) | ((cc_uint8)(255) << 24 )); |
103 | |
104 | struct TextAtlas* atlas = &s->posAtlas; |
105 | struct Texture tex; |
106 | IVec3 pos; |
107 | int count; |
108 | |
109 | /* Make "Position: " prefix */ |
110 | tex = atlas->tex; |
111 | tex.X = 2; tex.Width = atlas->offset; |
112 | Gfx_Make2DQuad(&tex, col, &ptr); |
113 | |
114 | IVec3_Floor(&pos, &LocalPlayer_Instance.Base.Position); |
115 | atlas->curX = atlas->offset + 2; |
116 | |
117 | /* Make (X, Y, Z) suffix */ |
118 | TextAtlas_Add(atlas, 13, &ptr); |
119 | TextAtlas_AddInt(atlas, pos.X, &ptr); |
120 | TextAtlas_Add(atlas, 11, &ptr); |
121 | TextAtlas_AddInt(atlas, pos.Y, &ptr); |
122 | TextAtlas_Add(atlas, 11, &ptr); |
123 | TextAtlas_AddInt(atlas, pos.Z, &ptr); |
124 | TextAtlas_Add(atlas, 14, &ptr); |
125 | |
126 | Gfx_BindTexture(atlas->tex.ID); |
127 | /* TODO: Do we need to use a separate VB here? */ |
128 | count = (int)(ptr - vertices); |
129 | Gfx_UpdateDynamicVb_IndexedTris(Models.Vb, vertices, count); |
130 | } |
131 | |
132 | static cc_bool HUDScreen_HasHacksChanged(struct HUDScreen* s) { |
133 | struct HacksComp* hacks = &LocalPlayer_Instance.Hacks; |
134 | float speed = HacksComp_CalcSpeedFactor(hacks, hacks->CanSpeed); |
135 | return speed != s->lastSpeed || Camera.Fov != s->lastFov || s->hacksChanged; |
136 | } |
137 | |
138 | static void HUDScreen_UpdateHackState(struct HUDScreen* s) { |
139 | cc_string status; char statusBuffer[STRING_SIZE64 * 2]; |
140 | struct HacksComp* hacks = &LocalPlayer_Instance.Hacks; |
141 | float speed = HacksComp_CalcSpeedFactor(hacks, hacks->CanSpeed); |
142 | |
143 | s->lastSpeed = speed; s->lastFov = Camera.Fov; |
144 | s->hacksChanged = false0; |
145 | |
146 | String_InitArray(status, statusBuffer)status.buffer = statusBuffer; status.length = 0; status.capacity = sizeof(statusBuffer);; |
147 | if (Camera.Fov != Camera.DefaultFov) { |
148 | String_Format1(&status, "Zoom fov %i ", &Camera.Fov); |
149 | } |
150 | |
151 | if (hacks->Flying) String_AppendConst(&status, "Fly ON "); |
152 | if (speed) String_Format1(&status, "Speed %f1x ", &speed); |
153 | if (hacks->Noclip) String_AppendConst(&status, "Noclip ON "); |
154 | |
155 | TextWidget_Set(&s->line2, &status, &s->font); |
156 | } |
157 | |
158 | static void HUDScreen_Update(void* screen, double delta) { |
159 | struct HUDScreen* s = (struct HUDScreen*)screen; |
160 | cc_string status; char statusBuffer[STRING_SIZE64 * 2]; |
161 | |
162 | s->frames++; |
163 | s->accumulator += delta; |
164 | if (s->accumulator < 1.0) return; |
165 | |
166 | String_InitArray(status, statusBuffer)status.buffer = statusBuffer; status.length = 0; status.capacity = sizeof(statusBuffer);; |
167 | HUDScreen_MakeText(s, &status); |
168 | |
169 | TextWidget_Set(&s->line1, &status, &s->font); |
170 | s->accumulator = 0.0; |
171 | s->frames = 0; |
172 | Game.ChunkUpdates = 0; |
173 | } |
174 | |
175 | static void HUDScreen_ContextLost(void* screen) { |
176 | struct HUDScreen* s = (struct HUDScreen*)screen; |
177 | Font_Free(&s->font); |
178 | TextAtlas_Free(&s->posAtlas); |
179 | Elem_Free(&s->hotbar)(&s->hotbar)->VTABLE->Free(&s->hotbar); |
180 | Elem_Free(&s->line1)(&s->line1)->VTABLE->Free(&s->line1); |
181 | Elem_Free(&s->line2)(&s->line2)->VTABLE->Free(&s->line2); |
182 | } |
183 | |
184 | static void HUDScreen_ContextRecreated(void* screen) { |
185 | static const cc_string chars = String_FromConst("0123456789-, ()"){ "0123456789-, ()", (sizeof("0123456789-, ()") - 1), (sizeof ("0123456789-, ()") - 1)}; |
186 | static const cc_string prefix = String_FromConst("Position: "){ "Position: ", (sizeof("Position: ") - 1), (sizeof("Position: " ) - 1)}; |
187 | |
188 | struct HUDScreen* s = (struct HUDScreen*)screen; |
189 | struct TextWidget* line1 = &s->line1; |
Value stored to 'line1' during its initialization is never read | |
190 | struct TextWidget* line2 = &s->line2; |
191 | |
192 | Drawer2D_MakeFont(&s->font, 16, FONT_FLAGS_PADDING); |
193 | Font_SetPadding(&s->font, 2); |
194 | HotbarWidget_SetFont(&s->hotbar, &s->font); |
195 | |
196 | HUDScreen_Update(s, 1.0); |
197 | TextAtlas_Make(&s->posAtlas, &chars, &s->font, &prefix); |
198 | |
199 | if (Game_ClassicMode) { |
200 | TextWidget_SetConst(line2, "0.30", &s->font); |
201 | } else { |
202 | HUDScreen_UpdateHackState(s); |
203 | } |
204 | } |
205 | |
206 | static void HUDScreen_BuildMesh(void* screen) { } |
207 | |
208 | static void HUDScreen_Layout(void* screen) { |
209 | struct HUDScreen* s = (struct HUDScreen*)screen; |
210 | struct TextWidget* line1 = &s->line1; |
211 | struct TextWidget* line2 = &s->line2; |
212 | int posY; |
213 | |
214 | Widget_SetLocation(line1, ANCHOR_MIN, ANCHOR_MIN, 2, 2); |
215 | posY = line1->y + line1->height; |
216 | s->posAtlas.tex.Y = posY; |
217 | Widget_SetLocation(line2, ANCHOR_MIN, ANCHOR_MIN, 2, 0); |
218 | |
219 | if (Game_ClassicMode) { |
220 | /* Swap around so 0.30 version is at top */ |
221 | line2->yOffset = line1->yOffset; |
222 | line1->yOffset = posY; |
223 | Widget_Layout(line1)(line1)->VTABLE->Reposition(line1); |
224 | } else { |
225 | /* We can't use y in TextWidget_Make because that DPI scales it */ |
226 | line2->yOffset = posY + s->posAtlas.tex.Height; |
227 | } |
228 | |
229 | s->hotbar.scale = Gui_GetHotbarScale(); |
230 | Widget_Layout(&s->hotbar)(&s->hotbar)->VTABLE->Reposition(&s->hotbar ); |
231 | Widget_Layout(line2)(line2)->VTABLE->Reposition(line2); |
232 | } |
233 | |
234 | static int HUDScreen_KeyDown(void* screen, int key) { |
235 | struct HUDScreen* s = (struct HUDScreen*)screen; |
236 | return Elem_HandlesKeyDown(&s->hotbar, key)(&s->hotbar)->VTABLE->HandlesKeyDown(&s-> hotbar, key); |
237 | } |
238 | |
239 | static void HUDScreen_InputUp(void* screen, int key) { |
240 | struct HUDScreen* s = (struct HUDScreen*)screen; |
241 | if (!InventoryScreen_IsHotbarActive()) return; |
242 | Elem_OnInputUp(&s->hotbar, key)(&s->hotbar)->VTABLE->OnInputUp(&s->hotbar , key); |
243 | } |
244 | |
245 | static int HUDscreen_PointerDown(void* screen, int id, int x, int y) { |
246 | struct HUDScreen* s = (struct HUDScreen*)screen; |
247 | if (Input_TouchMode0 || Gui.InputGrab) { |
248 | return Elem_HandlesPointerDown(&s->hotbar, id, x, y)(&s->hotbar)->VTABLE->HandlesPointerDown(&s-> hotbar, id, x, y); |
249 | } |
250 | return false0; |
251 | } |
252 | |
253 | static int HUDscreen_MouseScroll(void* screen, float delta) { |
254 | struct HUDScreen* s = (struct HUDScreen*)screen; |
255 | /* The default scrolling behaviour (e.g. camera, zoom) needs to be checked */ |
256 | /* BEFORE the hotbar is scrolled, but AFTER chat (maybe) handles scrolling. */ |
257 | /* Therefore need to check the default behaviour here, hacky as that may be. */ |
258 | if (Input_HandleMouseWheel(delta)) return false0; |
259 | |
260 | if (!Inventory.CanChangeSelected) return false0; |
261 | return Elem_HandlesMouseScroll(&s->hotbar, delta)(&s->hotbar)->VTABLE->HandlesMouseScroll(&s-> hotbar, delta); |
262 | } |
263 | |
264 | static void HUDScreen_HacksChanged(void* obj) { |
265 | ((struct HUDScreen*)obj)->hacksChanged = true1; |
266 | } |
267 | |
268 | static void HUDScreen_Init(void* screen) { |
269 | struct HUDScreen* s = (struct HUDScreen*)screen; |
270 | HotbarWidget_Create(&s->hotbar); |
271 | TextWidget_Init(&s->line1); |
272 | TextWidget_Init(&s->line2); |
273 | Event_Register_(&UserEvents.HacksStateChanged, screen, HUDScreen_HacksChanged)Event_Register((struct Event_Void*)(&UserEvents.HacksStateChanged ), screen, (Event_Void_Callback)(HUDScreen_HacksChanged)); |
274 | } |
275 | |
276 | static void HUDScreen_Render(void* screen, double delta) { |
277 | struct HUDScreen* s = (struct HUDScreen*)screen; |
278 | if (Game_HideGui) return; |
279 | |
280 | /* TODO: If Game_ShowFps is off and not classic mode, we should just return here */ |
281 | Gfx_SetTexturing(true1); |
282 | if (Gui.ShowFPS) Elem_Render(&s->line1, delta)(&s->line1)->VTABLE->Render(&s->line1, delta ); |
283 | |
284 | if (Game_ClassicMode) { |
285 | Elem_Render(&s->line2, delta)(&s->line2)->VTABLE->Render(&s->line2, delta ); |
286 | } else if (IsOnlyChatActive() && Gui.ShowFPS) { |
287 | if (HUDScreen_HasHacksChanged(s)) HUDScreen_UpdateHackState(s); |
288 | HUDScreen_DrawPosition(s); |
289 | Elem_Render(&s->line2, delta)(&s->line2)->VTABLE->Render(&s->line2, delta ); |
290 | } |
291 | |
292 | if (!Gui_GetBlocksWorld()) Elem_Render(&s->hotbar, delta)(&s->hotbar)->VTABLE->Render(&s->hotbar, delta ); |
293 | Gfx_SetTexturing(false0); |
294 | } |
295 | |
296 | static void HUDScreen_Free(void* screen) { |
297 | Event_Unregister_(&UserEvents.HacksStateChanged, screen, HUDScreen_HacksChanged)Event_Unregister((struct Event_Void*)(&UserEvents.HacksStateChanged ), screen, (Event_Void_Callback)(HUDScreen_HacksChanged)); |
298 | } |
299 | |
300 | static const struct ScreenVTABLE HUDScreen_VTABLE = { |
301 | HUDScreen_Init, HUDScreen_Update, HUDScreen_Free, |
302 | HUDScreen_Render, HUDScreen_BuildMesh, |
303 | HUDScreen_KeyDown, HUDScreen_InputUp, Screen_FKeyPress, Screen_FText, |
304 | HUDscreen_PointerDown, Screen_PointerUp, Screen_FPointer, HUDscreen_MouseScroll, |
305 | HUDScreen_Layout, HUDScreen_ContextLost, HUDScreen_ContextRecreated |
306 | }; |
307 | void HUDScreen_Show(void) { |
308 | struct HUDScreen* s = &HUDScreen_Instance; |
309 | s->VTABLE = &HUDScreen_VTABLE; |
310 | Gui_HUD = s; |
311 | Gui_Add((struct Screen*)s, GUI_PRIORITY_HUD); |
312 | } |
313 | |
314 | |
315 | /*########################################################################################################################* |
316 | *----------------------------------------------------TabListOverlay-----------------------------------------------------* |
317 | *#########################################################################################################################*/ |
318 | #define GROUP_NAME_ID((cc_uint16)65535) UInt16_MaxValue((cc_uint16)65535) |
319 | #define LIST_COLUMN_PADDING5 5 |
320 | #define LIST_NAMES_PER_COLUMN16 16 |
321 | #define TABLIST_MAX_ENTRIES(256 * 2) (TABLIST_MAX_NAMES256 * 2) |
322 | typedef int (*TabListEntryCompare)(int x, int y); |
323 | |
324 | static struct TabListOverlay { |
325 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
326 | int x, y, width, height; |
327 | cc_bool active, classic; |
328 | int namesCount, elementOffset; |
329 | struct TextWidget title; |
330 | struct FontDesc font; |
331 | TabListEntryCompare compare; |
332 | cc_uint16 ids[TABLIST_MAX_ENTRIES(256 * 2)]; |
333 | struct Texture textures[TABLIST_MAX_ENTRIES(256 * 2)]; |
334 | } TabListOverlay_Instance; |
335 | #define TABLIST_MAX_VERTICES(4 + 4 * (256 * 2)) (TEXTWIDGET_MAX4 + 4 * TABLIST_MAX_ENTRIES(256 * 2)) |
336 | |
337 | static void TabListOverlay_DrawText(struct Texture* tex, struct TabListOverlay* s, const cc_string* name) { |
338 | cc_string tmp; char tmpBuffer[STRING_SIZE64]; |
339 | struct DrawTextArgs args; |
340 | |
341 | if (Game_PureClassic(Game_ClassicMode && !Game_ClassicHacks)) { |
342 | String_InitArray(tmp, tmpBuffer)tmp.buffer = tmpBuffer; tmp.length = 0; tmp.capacity = sizeof (tmpBuffer);; |
343 | String_AppendColorless(&tmp, name); |
344 | } else { |
345 | tmp = *name; |
346 | } |
347 | |
348 | DrawTextArgs_Make(&args, &tmp, &s->font, !s->classic); |
349 | Drawer2D_MakeTextTexture(tex, &args); |
350 | } |
351 | |
352 | static int TabListOverlay_GetColumnWidth(struct TabListOverlay* s, int column) { |
353 | int i = column * LIST_NAMES_PER_COLUMN16; |
354 | int end = min(s->namesCount, i + LIST_NAMES_PER_COLUMN)((s->namesCount) < (i + 16) ? (s->namesCount) : (i + 16)); |
355 | int maxWidth = 0; |
356 | |
357 | for (; i < end; i++) { |
358 | maxWidth = max(maxWidth, s->textures[i].Width)((maxWidth) > (s->textures[i].Width) ? (maxWidth) : (s-> textures[i].Width)); |
359 | } |
360 | return maxWidth + LIST_COLUMN_PADDING5 + s->elementOffset; |
361 | } |
362 | |
363 | static int TabListOverlay_GetColumnHeight(struct TabListOverlay* s, int column) { |
364 | int i = column * LIST_NAMES_PER_COLUMN16; |
365 | int end = min(s->namesCount, i + LIST_NAMES_PER_COLUMN)((s->namesCount) < (i + 16) ? (s->namesCount) : (i + 16)); |
366 | int height = 0; |
367 | |
368 | for (; i < end; i++) { |
369 | height += s->textures[i].Height + 1; |
370 | } |
371 | return height; |
372 | } |
373 | |
374 | static void TabListOverlay_SetColumnPos(struct TabListOverlay* s, int column, int x, int y) { |
375 | struct Texture tex; |
376 | int i = column * LIST_NAMES_PER_COLUMN16; |
377 | int end = min(s->namesCount, i + LIST_NAMES_PER_COLUMN)((s->namesCount) < (i + 16) ? (s->namesCount) : (i + 16)); |
378 | |
379 | for (; i < end; i++) { |
380 | tex = s->textures[i]; |
381 | tex.X = x; tex.Y = y - 10; |
382 | |
383 | y += tex.Height + 1; |
384 | /* offset player names a bit, compared to group name */ |
385 | if (!s->classic && s->ids[i] != GROUP_NAME_ID((cc_uint16)65535)) { |
386 | tex.X += s->elementOffset; |
387 | } |
388 | s->textures[i] = tex; |
389 | } |
390 | } |
391 | |
392 | static void TabListOverlay_Layout(void* screen) { |
393 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
394 | int minWidth, minHeight, paddingX, paddingY; |
395 | int i, x, y, width = 0, height = 0; |
396 | int columns = Math_CeilDiv(s->namesCount, LIST_NAMES_PER_COLUMN16); |
397 | |
398 | for (i = 0; i < columns; i++) { |
399 | width += TabListOverlay_GetColumnWidth(s, i); |
400 | y = TabListOverlay_GetColumnHeight(s, i); |
401 | height = max(height, y)((height) > (y) ? (height) : (y)); |
402 | } |
403 | |
404 | minWidth = Display_ScaleX(480); |
405 | width = max(width, minWidth)((width) > (minWidth) ? (width) : (minWidth)); |
406 | paddingX = Display_ScaleX(10); |
407 | paddingY = Display_ScaleY(10); |
408 | |
409 | width += paddingX * 2; |
410 | height += paddingY * 2; |
411 | |
412 | y = WindowInfo.Height / 4 - height / 2; |
413 | s->x = Gui_CalcPos(ANCHOR_CENTRE, 0, width , WindowInfo.Width ); |
414 | s->y = Gui_CalcPos(ANCHOR_CENTRE, -max(0, y)((0) > (y) ? (0) : (y)), height, WindowInfo.Height); |
415 | |
416 | x = s->x + paddingX; |
417 | y = s->y + paddingY; |
418 | |
419 | for (i = 0; i < columns; i++) { |
420 | TabListOverlay_SetColumnPos(s, i, x, y); |
421 | x += TabListOverlay_GetColumnWidth(s, i); |
422 | } |
423 | |
424 | s->y -= (s->title.height + paddingY); |
425 | s->width = width; |
426 | minHeight = Display_ScaleY(300); |
427 | s->height = max(minHeight, height + s->title.height)((minHeight) > (height + s->title.height) ? (minHeight) : (height + s->title.height)); |
428 | |
429 | s->title.horAnchor = ANCHOR_CENTRE; |
430 | s->title.yOffset = s->y + paddingY / 2; |
431 | Widget_Layout(&s->title)(&s->title)->VTABLE->Reposition(&s->title ); |
432 | } |
433 | |
434 | static void TabListOverlay_AddName(struct TabListOverlay* s, EntityID id, int index) { |
435 | cc_string name; |
436 | /* insert at end of list */ |
437 | if (index == -1) { index = s->namesCount; s->namesCount++; } |
438 | |
439 | name = TabList_UNSAFE_GetList(id)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [id] - 2);; |
440 | s->ids[index] = id; |
441 | TabListOverlay_DrawText(&s->textures[index], s, &name); |
442 | } |
443 | |
444 | static void TabListOverlay_DeleteAt(struct TabListOverlay* s, int i) { |
445 | Gfx_DeleteTexture(&s->textures[i].ID); |
446 | |
447 | for (; i < s->namesCount - 1; i++) { |
448 | s->ids[i] = s->ids[i + 1]; |
449 | s->textures[i] = s->textures[i + 1]; |
450 | } |
451 | |
452 | s->namesCount--; |
453 | s->ids[s->namesCount] = 0; |
454 | s->textures[s->namesCount].ID = 0; |
455 | } |
456 | |
457 | static void TabListOverlay_AddGroup(struct TabListOverlay* s, int id, int* index) { |
458 | cc_string group; |
459 | int i; |
460 | group = TabList_UNSAFE_GetGroup(id)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [id] - 1);; |
461 | |
462 | for (i = Array_Elems(s->ids)(sizeof(s->ids) / sizeof(s->ids[0])) - 1; i > (*index); i--) { |
463 | s->ids[i] = s->ids[i - 1]; |
464 | s->textures[i] = s->textures[i - 1]; |
465 | } |
466 | |
467 | s->ids[*index] = GROUP_NAME_ID((cc_uint16)65535); |
468 | s->textures[*index].ID = 0; /* TODO: TEMP HACK! */ |
469 | TabListOverlay_DrawText(&s->textures[*index], s, &group); |
470 | |
471 | (*index)++; |
472 | s->namesCount++; |
473 | } |
474 | |
475 | static int TabListOverlay_GetGroupCount(struct TabListOverlay* s, int id, int i) { |
476 | cc_string group, curGroup; |
477 | int count; |
478 | group = TabList_UNSAFE_GetGroup(id)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [id] - 1);; |
479 | |
480 | for (count = 0; i < s->namesCount; i++, count++) { |
481 | curGroup = TabList_UNSAFE_GetGroup(s->ids[i])StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [s->ids[i]] - 1);; |
482 | if (!String_CaselessEquals(&group, &curGroup)) break; |
483 | } |
484 | return count; |
485 | } |
486 | |
487 | static int TabListOverlay_PlayerCompare(int x, int y) { |
488 | cc_string xName; char xNameBuffer[STRING_SIZE64]; |
489 | cc_string yName; char yNameBuffer[STRING_SIZE64]; |
490 | cc_uint8 xRank, yRank; |
491 | cc_string xNameRaw, yNameRaw; |
492 | |
493 | xRank = TabList.GroupRanks[x]; |
494 | yRank = TabList.GroupRanks[y]; |
495 | if (xRank != yRank) return (xRank < yRank ? -1 : 1); |
496 | |
497 | String_InitArray(xName, xNameBuffer)xName.buffer = xNameBuffer; xName.length = 0; xName.capacity = sizeof(xNameBuffer);; |
498 | xNameRaw = TabList_UNSAFE_GetList(x)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [x] - 2);; |
499 | String_AppendColorless(&xName, &xNameRaw); |
500 | |
501 | String_InitArray(yName, yNameBuffer)yName.buffer = yNameBuffer; yName.length = 0; yName.capacity = sizeof(yNameBuffer);; |
502 | yNameRaw = TabList_UNSAFE_GetList(y)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [y] - 2);; |
503 | String_AppendColorless(&yName, &yNameRaw); |
504 | |
505 | return String_Compare(&xName, &yName); |
506 | } |
507 | |
508 | static int TabListOverlay_GroupCompare(int x, int y) { |
509 | cc_string xGroup, yGroup; |
510 | /* TODO: should we use colourless comparison? ClassicalSharp sorts groups with colours */ |
511 | xGroup = TabList_UNSAFE_GetGroup(x)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [x] - 1);; |
512 | yGroup = TabList_UNSAFE_GetGroup(y)StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [y] - 1);; |
513 | return String_Compare(&xGroup, &yGroup); |
514 | } |
515 | |
516 | static void TabListOverlay_QuickSort(int left, int right) { |
517 | struct Texture* values = TabListOverlay_Instance.textures; struct Texture value; |
518 | cc_uint16* keys = TabListOverlay_Instance.ids; cc_uint16 key; |
519 | TabListEntryCompare compareEntries = TabListOverlay_Instance.compare; |
520 | |
521 | while (left < right) { |
522 | int i = left, j = right; |
523 | int pivot = keys[(i + j) / 2]; |
524 | |
525 | /* partition the list */ |
526 | while (i <= j) { |
527 | while (compareEntries(pivot, keys[i]) > 0) i++; |
528 | while (compareEntries(pivot, keys[j]) < 0) j--; |
529 | QuickSort_Swap_KV_Maybe()if (i <= j) { key = keys[i]; keys[i] = keys[j]; keys[j] = key ; value = values[i]; values[i] = values[j]; values[j] = value ; i++; j--;}; |
530 | } |
531 | /* recurse into the smaller subset */ |
532 | QuickSort_Recurse(TabListOverlay_QuickSort)if (j - left <= right - i) { if (left < j) { TabListOverlay_QuickSort (left, j); } left = i;} else { if (i < right) { TabListOverlay_QuickSort (i, right); } right = j;} |
533 | } |
534 | } |
535 | |
536 | static void TabListOverlay_SortEntries(struct TabListOverlay* s) { |
537 | int i, id, count; |
538 | if (!s->namesCount) return; |
539 | |
540 | if (s->classic) { |
541 | TabListOverlay_Instance.compare = TabListOverlay_PlayerCompare; |
542 | TabListOverlay_QuickSort(0, s->namesCount - 1); |
543 | return; |
544 | } |
545 | |
546 | /* Sort the list by group */ |
547 | /* Loop backwards, since DeleteAt() reduces NamesCount */ |
548 | for (i = s->namesCount - 1; i >= 0; i--) { |
549 | if (s->ids[i] != GROUP_NAME_ID((cc_uint16)65535)) continue; |
550 | TabListOverlay_DeleteAt(s, i); |
551 | } |
552 | TabListOverlay_Instance.compare = TabListOverlay_GroupCompare; |
553 | TabListOverlay_QuickSort(0, s->namesCount - 1); |
554 | |
555 | /* Sort the entries in each group */ |
556 | TabListOverlay_Instance.compare = TabListOverlay_PlayerCompare; |
557 | for (i = 0; i < s->namesCount; ) { |
558 | id = s->ids[i]; |
559 | TabListOverlay_AddGroup(s, id, &i); |
560 | |
561 | count = TabListOverlay_GetGroupCount(s, id, i); |
562 | TabListOverlay_QuickSort(i, i + (count - 1)); |
563 | i += count; |
564 | } |
565 | } |
566 | |
567 | static void TabListOverlay_SortAndLayout(struct TabListOverlay* s) { |
568 | TabListOverlay_SortEntries(s); |
569 | TabListOverlay_Layout(s); |
570 | } |
571 | |
572 | static void TabListOverlay_Add(void* obj, int id) { |
573 | struct TabListOverlay* s = (struct TabListOverlay*)obj; |
574 | TabListOverlay_AddName(s, id, -1); |
575 | TabListOverlay_SortAndLayout(s); |
576 | } |
577 | |
578 | static void TabListOverlay_Update(void* obj, int id) { |
579 | struct TabListOverlay* s = (struct TabListOverlay*)obj; |
580 | int i; |
581 | |
582 | for (i = 0; i < s->namesCount; i++) { |
583 | if (s->ids[i] != id) continue; |
584 | Gfx_DeleteTexture(&s->textures[i].ID); |
585 | |
586 | TabListOverlay_AddName(s, id, i); |
587 | TabListOverlay_SortAndLayout(s); |
588 | return; |
589 | } |
590 | } |
591 | |
592 | static void TabListOverlay_Remove(void* obj, int id) { |
593 | struct TabListOverlay* s = (struct TabListOverlay*)obj; |
594 | int i; |
595 | for (i = 0; i < s->namesCount; i++) { |
596 | if (s->ids[i] != id) continue; |
597 | |
598 | TabListOverlay_DeleteAt(s, i); |
599 | TabListOverlay_SortAndLayout(s); |
600 | return; |
601 | } |
602 | } |
603 | |
604 | static int TabListOverlay_PointerDown(void* screen, int id, int x, int y) { |
605 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
606 | cc_string text; char textBuffer[STRING_SIZE64 * 4]; |
607 | struct Texture tex; |
608 | cc_string player; |
609 | int i; |
610 | |
611 | if (!((struct Screen*)Gui_Chat)->grabsInput) return false0; |
612 | String_InitArray(text, textBuffer)text.buffer = textBuffer; text.length = 0; text.capacity = sizeof (textBuffer);; |
613 | |
614 | for (i = 0; i < s->namesCount; i++) { |
615 | if (!s->textures[i].ID || s->ids[i] == GROUP_NAME_ID((cc_uint16)65535)) continue; |
616 | tex = s->textures[i]; |
617 | if (!Gui_Contains(tex.X, tex.Y, tex.Width, tex.Height, x, y)) continue; |
618 | |
619 | player = TabList_UNSAFE_GetPlayer(s->ids[i])StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets [s->ids[i]] - 3);; |
620 | String_Format1(&text, "%s ", &player); |
621 | ChatScreen_AppendInput(&text); |
622 | return TOUCH_TYPE_GUI1; |
623 | } |
624 | return false0; |
625 | } |
626 | |
627 | static void TabListOverlay_KeyUp(void* screen, int key) { |
628 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
629 | if (key != KeyBinds[KEYBIND_TABLIST] || Input_TouchMode0) return; |
630 | Gui_Remove((struct Screen*)s); |
631 | } |
632 | |
633 | static void TabListOverlay_ContextLost(void* screen) { |
634 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
635 | int i; |
636 | for (i = 0; i < s->namesCount; i++) { |
637 | Gfx_DeleteTexture(&s->textures[i].ID); |
638 | } |
639 | |
640 | Elem_Free(&s->title)(&s->title)->VTABLE->Free(&s->title); |
641 | Font_Free(&s->font); |
642 | } |
643 | |
644 | static void TabListOverlay_ContextRecreated(void* screen) { |
645 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
646 | int size, id; |
647 | |
648 | size = Drawer2D_BitmappedText ? 16 : 11; |
649 | Drawer2D_MakeFont(&s->font, size, FONT_FLAGS_PADDING); |
650 | s->namesCount = 0; |
651 | |
652 | TextWidget_SetConst(&s->title, "Connected players:", &s->font); |
653 | Font_SetPadding(&s->font, 1); |
654 | |
655 | /* TODO: Just recreate instead of this? maybe */ |
656 | for (id = 0; id < TABLIST_MAX_NAMES256; id++) { |
657 | if (!TabList.NameOffsets[id]) continue; |
658 | TabListOverlay_AddName(s, (EntityID)id, -1); |
659 | } |
660 | TabListOverlay_SortAndLayout(s); /* TODO: Not do layout here too */ |
661 | } |
662 | |
663 | static void TabListOverlay_BuildMesh(void* screen) { } |
664 | |
665 | static void TabListOverlay_Render(void* screen, double delta) { |
666 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
667 | struct TextWidget* title = &s->title; |
668 | struct Screen* grabbed; |
669 | struct Texture tex; |
670 | int i; |
671 | PackedCol topCol = PackedCol_Make( 0, 0, 0, 180)(((cc_uint8)(0) << 0) | ((cc_uint8)(0) << 8) | (( cc_uint8)(0) << 16) | ((cc_uint8)(180) << 24)); |
672 | PackedCol bottomCol = PackedCol_Make(50, 50, 50, 205)(((cc_uint8)(50) << 0) | ((cc_uint8)(50) << 8) | ( (cc_uint8)(50) << 16) | ((cc_uint8)(205) << 24)); |
673 | |
674 | if (Game_HideGui || !IsOnlyChatActive()) return; |
675 | Gfx_SetTexturing(false0); |
676 | Gfx_Draw2DGradient(s->x, s->y, s->width, s->height, topCol, bottomCol); |
677 | |
678 | Gfx_SetTexturing(true1); |
679 | Elem_Render(title, delta)(title)->VTABLE->Render(title, delta); |
680 | grabbed = Gui.InputGrab; |
681 | |
682 | for (i = 0; i < s->namesCount; i++) { |
683 | if (!s->textures[i].ID) continue; |
684 | tex = s->textures[i]; |
685 | |
686 | if (grabbed && s->ids[i] != GROUP_NAME_ID((cc_uint16)65535)) { |
687 | if (Gui_ContainsPointers(tex.X, tex.Y, tex.Width, tex.Height)) tex.X += 4; |
688 | } |
689 | Texture_Render(&tex); |
690 | } |
691 | Gfx_SetTexturing(false0); |
692 | } |
693 | |
694 | static void TabListOverlay_Free(void* screen) { |
695 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
696 | s->active = false0; |
697 | Event_Unregister_(&TabListEvents.Added, s, TabListOverlay_Add)Event_Unregister((struct Event_Void*)(&TabListEvents.Added ), s, (Event_Void_Callback)(TabListOverlay_Add)); |
698 | Event_Unregister_(&TabListEvents.Changed, s, TabListOverlay_Update)Event_Unregister((struct Event_Void*)(&TabListEvents.Changed ), s, (Event_Void_Callback)(TabListOverlay_Update)); |
699 | Event_Unregister_(&TabListEvents.Removed, s, TabListOverlay_Remove)Event_Unregister((struct Event_Void*)(&TabListEvents.Removed ), s, (Event_Void_Callback)(TabListOverlay_Remove)); |
700 | } |
701 | |
702 | static void TabListOverlay_Init(void* screen) { |
703 | struct TabListOverlay* s = (struct TabListOverlay*)screen; |
704 | s->active = true1; |
705 | s->classic = Gui.ClassicTabList || !Server.SupportsExtPlayerList; |
706 | s->elementOffset = s->classic ? 0 : 10; |
707 | TextWidget_Init(&s->title); |
708 | |
709 | Event_Register_(&TabListEvents.Added, s, TabListOverlay_Add)Event_Register((struct Event_Void*)(&TabListEvents.Added) , s, (Event_Void_Callback)(TabListOverlay_Add)); |
710 | Event_Register_(&TabListEvents.Changed, s, TabListOverlay_Update)Event_Register((struct Event_Void*)(&TabListEvents.Changed ), s, (Event_Void_Callback)(TabListOverlay_Update)); |
711 | Event_Register_(&TabListEvents.Removed, s, TabListOverlay_Remove)Event_Register((struct Event_Void*)(&TabListEvents.Removed ), s, (Event_Void_Callback)(TabListOverlay_Remove)); |
712 | } |
713 | |
714 | static const struct ScreenVTABLE TabListOverlay_VTABLE = { |
715 | TabListOverlay_Init, Screen_NullUpdate, TabListOverlay_Free, |
716 | TabListOverlay_Render, TabListOverlay_BuildMesh, |
717 | Screen_FInput, TabListOverlay_KeyUp, Screen_FKeyPress, Screen_FText, |
718 | TabListOverlay_PointerDown, Screen_PointerUp, Screen_FPointer, Screen_FMouseScroll, |
719 | TabListOverlay_Layout, TabListOverlay_ContextLost, TabListOverlay_ContextRecreated |
720 | }; |
721 | void TabListOverlay_Show(void) { |
722 | struct TabListOverlay* s = &TabListOverlay_Instance; |
723 | s->VTABLE = &TabListOverlay_VTABLE; |
724 | Gui_Add((struct Screen*)s, GUI_PRIORITY_TABLIST); |
725 | } |
726 | |
727 | |
728 | |
729 | /*########################################################################################################################* |
730 | *--------------------------------------------------------ChatScreen-------------------------------------------------------* |
731 | *#########################################################################################################################*/ |
732 | static struct ChatScreen { |
733 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
734 | float chatAcc; |
735 | cc_bool suppressNextPress; |
736 | int chatIndex, paddingX, paddingY; |
737 | int lastDownloadStatus; |
738 | struct FontDesc chatFont, announcementFont; |
739 | struct TextWidget announcement; |
740 | struct ChatInputWidget input; |
741 | struct TextGroupWidget status, bottomRight, chat, clientStatus; |
742 | struct SpecialInputWidget altText; |
743 | #ifdef CC_BUILD_TOUCH |
744 | struct ButtonWidget send, cancel; |
745 | #endif |
746 | |
747 | struct Texture statusTextures[CHAT_MAX_STATUS(sizeof(Chat_Status) / sizeof(Chat_Status[0]))]; |
748 | struct Texture bottomRightTextures[CHAT_MAX_BOTTOMRIGHT(sizeof(Chat_BottomRight) / sizeof(Chat_BottomRight[0]))]; |
749 | struct Texture clientStatusTextures[CHAT_MAX_CLIENTSTATUS(sizeof(Chat_ClientStatus) / sizeof(Chat_ClientStatus[0]))]; |
750 | struct Texture chatTextures[TEXTGROUPWIDGET_MAX_LINES30]; |
751 | } ChatScreen_Instance; |
752 | #define CH_EXTENT16 16 |
753 | |
754 | static void ChatScreen_UpdateChatYOffsets(struct ChatScreen* s) { |
755 | int pad, y; |
756 | |
757 | /* Determining chat Y requires us to know hotbar's position */ |
758 | /* But HUD is lower priority, so it gets laid out AFTER chat */ |
759 | /* Hence use this hack to resize HUD first */ |
760 | HUDScreen_Layout(Gui_HUD); |
761 | |
762 | y = min(s->input.base.y, Gui_HUD->hotbar.y)((s->input.base.y) < (Gui_HUD->hotbar.y) ? (s->input .base.y) : (Gui_HUD->hotbar.y)); |
763 | y -= s->input.base.yOffset; /* add some padding */ |
764 | s->altText.yOffset = WindowInfo.Height - y; |
765 | Widget_Layout(&s->altText)(&s->altText)->VTABLE->Reposition(&s->altText ); |
766 | |
767 | pad = s->altText.active ? 5 : 10; |
768 | s->clientStatus.yOffset = WindowInfo.Height - s->altText.y + pad; |
769 | Widget_Layout(&s->clientStatus)(&s->clientStatus)->VTABLE->Reposition(&s-> clientStatus); |
770 | s->chat.yOffset = s->clientStatus.yOffset + s->clientStatus.height; |
771 | Widget_Layout(&s->chat)(&s->chat)->VTABLE->Reposition(&s->chat); |
772 | } |
773 | |
774 | static void ChatScreen_OnInputTextChanged(void* elem) { |
775 | ChatScreen_UpdateChatYOffsets(Gui_Chat); |
776 | } |
777 | |
778 | static cc_string ChatScreen_GetChat(int i) { |
779 | i += ChatScreen_Instance.chatIndex; |
780 | |
781 | if (i >= 0 && i < Chat_Log.count) { |
782 | return StringsBuffer_UNSAFE_Get(&Chat_Log, i); |
783 | } |
784 | return String_Empty; |
785 | } |
786 | |
787 | static cc_string ChatScreen_GetStatus(int i) { return Chat_Status[i]; } |
788 | static cc_string ChatScreen_GetBottomRight(int i) { return Chat_BottomRight[2 - i]; } |
789 | static cc_string ChatScreen_GetClientStatus(int i) { return Chat_ClientStatus[i]; } |
790 | |
791 | static void ChatScreen_FreeChatFonts(struct ChatScreen* s) { |
792 | Font_Free(&s->chatFont); |
793 | Font_Free(&s->announcementFont); |
794 | } |
795 | |
796 | static cc_bool ChatScreen_ChatUpdateFont(struct ChatScreen* s) { |
797 | int size = (int)(8 * Gui_GetChatScale()); |
798 | Math_Clamp(size, 8, 60)size = size < (8) ? (8) : size; size = size > (60) ? (60 ) : size;; |
799 | |
800 | /* don't recreate font if possible */ |
801 | /* TODO: Add function for this, don't use Display_ScaleY (Drawer2D_SameFontSize ??) */ |
802 | if (Display_ScaleY(size) == s->chatFont.size) return false0; |
803 | ChatScreen_FreeChatFonts(s); |
804 | Drawer2D_MakeFont(&s->chatFont, size, FONT_FLAGS_PADDING); |
805 | |
806 | size = (int)(16 * Gui_GetChatScale()); |
807 | Math_Clamp(size, 8, 60)size = size < (8) ? (8) : size; size = size > (60) ? (60 ) : size;; |
808 | Drawer2D_MakeFont(&s->announcementFont, size, FONT_FLAGS_NONE); |
809 | |
810 | ChatInputWidget_SetFont(&s->input, &s->chatFont); |
811 | TextGroupWidget_SetFont(&s->status, &s->chatFont); |
812 | TextGroupWidget_SetFont(&s->bottomRight, &s->chatFont); |
813 | TextGroupWidget_SetFont(&s->chat, &s->chatFont); |
814 | TextGroupWidget_SetFont(&s->clientStatus, &s->chatFont); |
815 | return true1; |
816 | } |
817 | |
818 | static void ChatScreen_Redraw(struct ChatScreen* s) { |
819 | TextGroupWidget_RedrawAll(&s->chat); |
820 | TextWidget_Set(&s->announcement, &Chat_Announcement, &s->announcementFont); |
821 | TextGroupWidget_RedrawAll(&s->status); |
822 | TextGroupWidget_RedrawAll(&s->bottomRight); |
823 | TextGroupWidget_RedrawAll(&s->clientStatus); |
824 | |
825 | if (s->grabsInput) InputWidget_UpdateText(&s->input.base); |
826 | SpecialInputWidget_Redraw(&s->altText); |
827 | } |
828 | |
829 | static int ChatScreen_ClampChatIndex(int index) { |
830 | int maxIndex = Chat_Log.count - Gui.Chatlines; |
831 | int minIndex = min(0, maxIndex)((0) < (maxIndex) ? (0) : (maxIndex)); |
832 | Math_Clamp(index, minIndex, maxIndex)index = index < (minIndex) ? (minIndex) : index; index = index > (maxIndex) ? (maxIndex) : index;; |
833 | return index; |
834 | } |
835 | |
836 | static void ChatScreen_ScrollChatBy(struct ChatScreen* s, int delta) { |
837 | int newIndex = ChatScreen_ClampChatIndex(s->chatIndex + delta); |
838 | delta = newIndex - s->chatIndex; |
839 | |
840 | while (delta) { |
841 | if (delta < 0) { |
842 | /* scrolling up to oldest */ |
843 | s->chatIndex--; delta++; |
844 | TextGroupWidget_ShiftDown(&s->chat); |
845 | } else { |
846 | /* scrolling down to newest */ |
847 | s->chatIndex++; delta--; |
848 | TextGroupWidget_ShiftUp(&s->chat); |
849 | } |
850 | } |
851 | } |
852 | |
853 | static void ChatScreen_EnterChatInput(struct ChatScreen* s, cc_bool close) { |
854 | struct InputWidget* input; |
855 | int defaultIndex; |
856 | |
857 | s->grabsInput = false0; |
858 | Gui_UpdateInputGrab(); |
859 | Window_CloseKeyboard(); |
860 | if (close) InputWidget_Clear(&s->input.base); |
861 | |
862 | input = &s->input.base; |
863 | input->OnPressedEnter(input); |
864 | SpecialInputWidget_SetActive(&s->altText, false0); |
865 | ChatScreen_UpdateChatYOffsets(s); |
866 | |
867 | /* Reset chat when user has scrolled up in chat history */ |
868 | defaultIndex = Chat_Log.count - Gui.Chatlines; |
869 | if (s->chatIndex != defaultIndex) { |
870 | s->chatIndex = defaultIndex; |
871 | TextGroupWidget_RedrawAll(&s->chat); |
872 | } |
873 | } |
874 | |
875 | static void ChatScreen_UpdateTexpackStatus(struct ChatScreen* s) { |
876 | int progress = Http_CheckProgress(TexturePack_ReqID); |
877 | if (progress == s->lastDownloadStatus) return; |
878 | |
879 | s->lastDownloadStatus = progress; |
880 | Chat_Status[0].length = 0; |
881 | |
882 | if (progress == HTTP_PROGRESS_MAKING_REQUEST) { |
883 | String_AppendConst(&Chat_Status[0], "&eRetrieving texture pack.."); |
884 | } else if (progress == HTTP_PROGRESS_FETCHING_DATA) { |
885 | String_AppendConst(&Chat_Status[0], "&eDownloading texture pack"); |
886 | } else if (progress >= 0 && progress <= 100) { |
887 | String_Format1(&Chat_Status[0], "&eDownloading texture pack (&7%i&e%%)", &progress); |
888 | } |
889 | TextGroupWidget_Redraw(&s->status, 0); |
890 | } |
891 | |
892 | static void ChatScreen_ColCodeChanged(void* screen, int code) { |
893 | struct ChatScreen* s = (struct ChatScreen*)screen; |
894 | double caretAcc; |
895 | if (Gfx.LostContext) return; |
896 | |
897 | SpecialInputWidget_UpdateCols(&s->altText); |
898 | TextGroupWidget_RedrawAllWithCol(&s->chat, code); |
899 | TextGroupWidget_RedrawAllWithCol(&s->status, code); |
900 | TextGroupWidget_RedrawAllWithCol(&s->bottomRight, code); |
901 | TextGroupWidget_RedrawAllWithCol(&s->clientStatus, code); |
902 | |
903 | /* Some servers have plugins that redefine colours constantly */ |
904 | /* Preserve caret accumulator so caret blinking stays consistent */ |
905 | caretAcc = s->input.base.caretAccumulator; |
906 | InputWidget_UpdateText(&s->input.base); |
907 | s->input.base.caretAccumulator = caretAcc; |
908 | } |
909 | |
910 | static void ChatScreen_ChatReceived(void* screen, const cc_string* msg, int type) { |
911 | struct ChatScreen* s = (struct ChatScreen*)screen; |
912 | if (Gfx.LostContext) return; |
913 | |
914 | if (type == MSG_TYPE_NORMAL) { |
915 | s->chatIndex++; |
916 | if (!Gui.Chatlines) return; |
917 | TextGroupWidget_ShiftUp(&s->chat); |
918 | } else if (type >= MSG_TYPE_STATUS_1 && type <= MSG_TYPE_STATUS_3) { |
919 | /* Status[0] is for texture pack downloading message */ |
920 | TextGroupWidget_Redraw(&s->status, 1 + (type - MSG_TYPE_STATUS_1)); |
921 | } else if (type >= MSG_TYPE_BOTTOMRIGHT_1 && type <= MSG_TYPE_BOTTOMRIGHT_3) { |
922 | /* Bottom3 is top most line, so need to redraw index 0 */ |
923 | TextGroupWidget_Redraw(&s->bottomRight, 2 - (type - MSG_TYPE_BOTTOMRIGHT_1)); |
924 | } else if (type == MSG_TYPE_ANNOUNCEMENT) { |
925 | TextWidget_Set(&s->announcement, msg, &s->announcementFont); |
926 | } else if (type >= MSG_TYPE_CLIENTSTATUS_1 && type <= MSG_TYPE_CLIENTSTATUS_2) { |
927 | TextGroupWidget_Redraw(&s->clientStatus, type - MSG_TYPE_CLIENTSTATUS_1); |
928 | ChatScreen_UpdateChatYOffsets(s); |
929 | } |
930 | } |
931 | |
932 | static void ChatScreen_DrawCrosshairs(void) { |
933 | static struct Texture tex = { 0, Tex_Rect(0,0,0,0)0,0,0,0, Tex_UV(0.0f,0.0f, 15/256.0f,15/256.0f)0.0f,0.0f,15/256.0f,15/256.0f }; |
934 | int extent; |
935 | if (!Gui.IconsTex) return; |
936 | |
937 | extent = (int)(CH_EXTENT16 * Gui_Scale(WindowInfo.Height / 480.0f)); |
938 | tex.ID = Gui.IconsTex; |
939 | tex.X = (WindowInfo.Width / 2) - extent; |
940 | tex.Y = (WindowInfo.Height / 2) - extent; |
941 | |
942 | tex.Width = extent * 2; |
943 | tex.Height = extent * 2; |
944 | Texture_Render(&tex); |
945 | } |
946 | |
947 | static void ChatScreen_DrawChatBackground(struct ChatScreen* s) { |
948 | int usedHeight = TextGroupWidget_UsedHeight(&s->chat); |
949 | int x = s->chat.x; |
950 | int y = s->chat.y + s->chat.height - usedHeight; |
951 | |
952 | int width = max(s->clientStatus.width, s->chat.width)((s->clientStatus.width) > (s->chat.width) ? (s-> clientStatus.width) : (s->chat.width)); |
953 | int height = usedHeight + s->clientStatus.height; |
954 | |
955 | if (height > 0) { |
956 | PackedCol backCol = PackedCol_Make(0, 0, 0, 127)(((cc_uint8)(0) << 0) | ((cc_uint8)(0) << 8) | (( cc_uint8)(0) << 16) | ((cc_uint8)(127) << 24)); |
957 | Gfx_Draw2DFlat( x - s->paddingX, y - s->paddingY, |
958 | width + s->paddingX * 2, height + s->paddingY * 2, backCol); |
959 | } |
960 | } |
961 | |
962 | static void ChatScreen_DrawChat(struct ChatScreen* s, double delta) { |
963 | struct Texture tex; |
964 | double now; |
965 | int i, logIdx; |
966 | |
967 | ChatScreen_UpdateTexpackStatus(s); |
968 | if (!Game_PureClassic(Game_ClassicMode && !Game_ClassicHacks)) { Elem_Render(&s->status, delta)(&s->status)->VTABLE->Render(&s->status, delta ); } |
969 | Elem_Render(&s->bottomRight, delta)(&s->bottomRight)->VTABLE->Render(&s->bottomRight , delta); |
970 | Elem_Render(&s->clientStatus, delta)(&s->clientStatus)->VTABLE->Render(&s->clientStatus , delta); |
971 | |
972 | now = Game.Time; |
973 | if (s->grabsInput) { |
974 | Elem_Render(&s->chat, delta)(&s->chat)->VTABLE->Render(&s->chat, delta ); |
975 | } else { |
976 | /* Only render recent chat */ |
977 | for (i = 0; i < s->chat.lines; i++) { |
978 | tex = s->chat.textures[i]; |
979 | logIdx = s->chatIndex + i; |
980 | if (!tex.ID) continue; |
981 | |
982 | if (logIdx < 0 || logIdx >= Chat_Log.count) continue; |
983 | if (Chat_LogTime[logIdx] + 10 >= now) Texture_Render(&tex); |
984 | } |
985 | } |
986 | |
987 | /* Destroy announcement texture before even rendering it at all, */ |
988 | /* otherwise changing texture pack shows announcement for one frame */ |
989 | if (s->announcement.tex.ID && now > Chat_AnnouncementReceived + 5) { |
990 | Elem_Free(&s->announcement)(&s->announcement)->VTABLE->Free(&s->announcement ); |
991 | } |
992 | Elem_Render(&s->announcement, delta)(&s->announcement)->VTABLE->Render(&s->announcement , delta); |
993 | |
994 | if (s->grabsInput) { |
995 | Elem_Render(&s->input.base, delta)(&s->input.base)->VTABLE->Render(&s->input .base, delta); |
996 | if (s->altText.active) { |
997 | Elem_Render(&s->altText, delta)(&s->altText)->VTABLE->Render(&s->altText , delta); |
998 | } |
999 | |
1000 | #ifdef CC_BUILD_TOUCH |
1001 | if (!Input_TouchMode0) return; |
1002 | Elem_Render(&s->send, delta)(&s->send)->VTABLE->Render(&s->send, delta ); |
1003 | Elem_Render(&s->cancel, delta)(&s->cancel)->VTABLE->Render(&s->cancel, delta ); |
1004 | #endif |
1005 | } |
1006 | } |
1007 | |
1008 | static void ChatScreen_ContextLost(void* screen) { |
1009 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1010 | ChatScreen_FreeChatFonts(s); |
1011 | |
1012 | Elem_Free(&s->chat)(&s->chat)->VTABLE->Free(&s->chat); |
1013 | Elem_Free(&s->input.base)(&s->input.base)->VTABLE->Free(&s->input. base); |
1014 | Elem_Free(&s->altText)(&s->altText)->VTABLE->Free(&s->altText); |
1015 | Elem_Free(&s->status)(&s->status)->VTABLE->Free(&s->status); |
1016 | Elem_Free(&s->bottomRight)(&s->bottomRight)->VTABLE->Free(&s->bottomRight ); |
1017 | Elem_Free(&s->clientStatus)(&s->clientStatus)->VTABLE->Free(&s->clientStatus ); |
1018 | Elem_Free(&s->announcement)(&s->announcement)->VTABLE->Free(&s->announcement ); |
1019 | |
1020 | #ifdef CC_BUILD_TOUCH |
1021 | if (!Input_TouchMode0) return; |
1022 | Elem_Free(&s->send)(&s->send)->VTABLE->Free(&s->send); |
1023 | Elem_Free(&s->cancel)(&s->cancel)->VTABLE->Free(&s->cancel); |
1024 | #endif |
1025 | } |
1026 | |
1027 | static void ChatScreen_ContextRecreated(void* screen) { |
1028 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1029 | struct FontDesc font; |
1030 | ChatScreen_ChatUpdateFont(s); |
1031 | ChatScreen_Redraw(s); |
1032 | |
1033 | #ifdef CC_BUILD_TOUCH |
1034 | if (!Input_TouchMode0) return; |
1035 | Gui_MakeTitleFont(&font); |
1036 | ButtonWidget_SetConst(&s->send, "Send", &font); |
1037 | ButtonWidget_SetConst(&s->cancel, "Cancel", &font); |
1038 | Font_Free(&font); |
1039 | #endif |
1040 | } |
1041 | |
1042 | static void ChatScreen_BuildMesh(void* screen) { } |
1043 | |
1044 | static void ChatScreen_Layout(void* screen) { |
1045 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1046 | /* See comment in ChatScreen_UpdateChatYOffsets */ |
1047 | HUDScreen_Layout(Gui_HUD); |
1048 | if (ChatScreen_ChatUpdateFont(s)) ChatScreen_Redraw(s); |
1049 | |
1050 | s->paddingX = Display_ScaleX(5); |
1051 | s->paddingY = Display_ScaleY(5); |
1052 | |
1053 | Widget_SetLocation(&s->input.base, ANCHOR_MIN, ANCHOR_MAX, 5, 5); |
1054 | Widget_SetLocation(&s->altText, ANCHOR_MIN, ANCHOR_MAX, 5, 5); |
1055 | Widget_SetLocation(&s->status, ANCHOR_MAX, ANCHOR_MIN, 0, 0); |
1056 | Widget_SetLocation(&s->bottomRight, ANCHOR_MAX, ANCHOR_MAX, 0, 0); |
1057 | Widget_SetLocation(&s->chat, ANCHOR_MIN, ANCHOR_MAX, 10, 0); |
1058 | Widget_SetLocation(&s->clientStatus, ANCHOR_MIN, ANCHOR_MAX, 10, 0); |
1059 | ChatScreen_UpdateChatYOffsets(s); |
1060 | |
1061 | /* Can't use Widget_SetLocation because it DPI scales input */ |
1062 | s->bottomRight.yOffset = Gui_HUD->hotbar.height + Display_ScaleY(15); |
1063 | Widget_Layout(&s->bottomRight)(&s->bottomRight)->VTABLE->Reposition(&s-> bottomRight); |
1064 | |
1065 | Widget_SetLocation(&s->announcement, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 0); |
1066 | s->announcement.yOffset = -WindowInfo.Height / 4; |
1067 | Widget_Layout(&s->announcement)(&s->announcement)->VTABLE->Reposition(&s-> announcement); |
1068 | |
1069 | #ifdef CC_BUILD_TOUCH |
1070 | if (!Input_TouchMode0) return; |
1071 | if (WindowInfo.SoftKeyboard == SOFT_KEYBOARD_SHIFT) { |
1072 | Widget_SetLocation(&s->send, ANCHOR_MAX, ANCHOR_MAX, 10, 60); |
1073 | Widget_SetLocation(&s->cancel, ANCHOR_MAX, ANCHOR_MAX, 10, 10); |
1074 | } else { |
1075 | Widget_SetLocation(&s->send, ANCHOR_MAX, ANCHOR_MIN, 10, 10); |
1076 | Widget_SetLocation(&s->cancel, ANCHOR_MAX, ANCHOR_MIN, 10, 60); |
1077 | } |
1078 | #endif |
1079 | } |
1080 | |
1081 | static int ChatScreen_KeyPress(void* screen, char keyChar) { |
1082 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1083 | if (!s->grabsInput) return false0; |
1084 | |
1085 | if (s->suppressNextPress) { |
1086 | s->suppressNextPress = false0; |
1087 | return false0; |
1088 | } |
1089 | |
1090 | InputWidget_Append(&s->input.base, keyChar); |
1091 | return true1; |
1092 | } |
1093 | |
1094 | static int ChatScreen_TextChanged(void* screen, const cc_string* str) { |
1095 | #ifdef CC_BUILD_TOUCH |
1096 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1097 | if (!s->grabsInput) return false0; |
1098 | |
1099 | InputWidget_SetText(&s->input.base, str); |
1100 | #endif |
1101 | return true1; |
1102 | } |
1103 | |
1104 | static int ChatScreen_KeyDown(void* screen, int key) { |
1105 | static const cc_string slash = String_FromConst("/"){ "/", (sizeof("/") - 1), (sizeof("/") - 1)}; |
1106 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1107 | int playerListKey = KeyBinds[KEYBIND_TABLIST]; |
1108 | cc_bool handlesList = playerListKey != KEY_TAB || !Gui.TabAutocomplete || !s->grabsInput; |
1109 | |
1110 | if (key == playerListKey && handlesList) { |
1111 | if (!TabListOverlay_Instance.active && !Server.IsSinglePlayer) { |
1112 | TabListOverlay_Show(); |
1113 | } |
1114 | return true1; |
1115 | } |
1116 | |
1117 | s->suppressNextPress = false0; |
1118 | /* Handle chat text input */ |
1119 | if (s->grabsInput) { |
1120 | #ifdef CC_BUILD_WEB |
1121 | /* See reason for this in HandleInputUp */ |
1122 | if (key == KeyBinds[KEYBIND_SEND_CHAT] || key == KEY_KP_ENTER) { |
1123 | ChatScreen_EnterChatInput(s, false0); |
1124 | #else |
1125 | if (key == KeyBinds[KEYBIND_SEND_CHAT] || key == KEY_KP_ENTER || key == KEY_ESCAPE) { |
1126 | ChatScreen_EnterChatInput(s, key == KEY_ESCAPE); |
1127 | #endif |
1128 | } else if (key == KEY_PAGEUP) { |
1129 | ChatScreen_ScrollChatBy(s, -Gui.Chatlines); |
1130 | } else if (key == KEY_PAGEDOWN) { |
1131 | ChatScreen_ScrollChatBy(s, +Gui.Chatlines); |
1132 | } else { |
1133 | Elem_HandlesKeyDown(&s->input.base, key)(&s->input.base)->VTABLE->HandlesKeyDown(&s-> input.base, key); |
1134 | } |
1135 | return key < KEY_F1 || key > KEY_F24; |
1136 | } |
1137 | |
1138 | if (key == KeyBinds[KEYBIND_CHAT]) { |
1139 | ChatScreen_OpenInput(&String_Empty); |
1140 | } else if (key == KEY_SLASH) { |
1141 | ChatScreen_OpenInput(&slash); |
1142 | } else if (key == KeyBinds[KEYBIND_INVENTORY]) { |
1143 | InventoryScreen_Show(); |
1144 | } else { |
1145 | return false0; |
1146 | } |
1147 | return true1; |
1148 | } |
1149 | |
1150 | static void ChatScreen_KeyUp(void* screen, int key) { |
1151 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1152 | if (!s->grabsInput || (struct Screen*)s != Gui.InputGrab) return; |
1153 | |
1154 | #ifdef CC_BUILD_WEB |
1155 | /* See reason for this in HandleInputUp */ |
1156 | if (key == KEY_ESCAPE) ChatScreen_EnterChatInput(s, true1); |
1157 | #endif |
1158 | |
1159 | if (Server.SupportsFullCP437 && key == KeyBinds[KEYBIND_EXT_INPUT]) { |
1160 | if (!WindowInfo.Focused) return; |
1161 | SpecialInputWidget_SetActive(&s->altText, !s->altText.active); |
1162 | ChatScreen_UpdateChatYOffsets(s); |
1163 | } |
1164 | } |
1165 | |
1166 | static int ChatScreen_MouseScroll(void* screen, float delta) { |
1167 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1168 | int steps; |
1169 | if (!s->grabsInput) return false0; |
1170 | |
1171 | steps = Utils_AccumulateWheelDelta(&s->chatAcc, delta); |
1172 | ChatScreen_ScrollChatBy(s, -steps); |
1173 | return true1; |
1174 | } |
1175 | |
1176 | static int ChatScreen_PointerDown(void* screen, int id, int x, int y) { |
1177 | cc_string text; char textBuffer[STRING_SIZE64 * 4]; |
1178 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1179 | int height, chatY, i; |
1180 | if (Game_HideGui) return false0; |
1181 | |
1182 | if (!s->grabsInput) { |
1183 | if (!Input_TouchMode0) return false0; |
1184 | String_InitArray(text, textBuffer)text.buffer = textBuffer; text.length = 0; text.capacity = sizeof (textBuffer);; |
1185 | |
1186 | /* Should be able to click on links with touch */ |
1187 | i = TextGroupWidget_GetSelected(&s->chat, &text, x, y); |
1188 | if (!Utils_IsUrlPrefix(&text)) return false0; |
1189 | |
1190 | if (Chat_LogTime[s->chatIndex + i] + 10 < Game.Time) return false0; |
1191 | UrlWarningOverlay_Show(&text); return TOUCH_TYPE_GUI1; |
1192 | } |
1193 | |
1194 | #ifdef CC_BUILD_TOUCH |
1195 | if (Widget_Contains(&s->send, x, y)) { |
1196 | ChatScreen_EnterChatInput(s, false0); return TOUCH_TYPE_GUI1; |
1197 | } |
1198 | if (Widget_Contains(&s->cancel, x, y)) { |
1199 | ChatScreen_EnterChatInput(s, true1); return TOUCH_TYPE_GUI1; |
1200 | } |
1201 | #endif |
1202 | |
1203 | if (!Widget_Contains(&s->chat, x, y)) { |
1204 | if (s->altText.active && Widget_Contains(&s->altText, x, y)) { |
1205 | Elem_HandlesPointerDown(&s->altText, id, x, y)(&s->altText)->VTABLE->HandlesPointerDown(&s ->altText, id, x, y); |
1206 | ChatScreen_UpdateChatYOffsets(s); |
1207 | return TOUCH_TYPE_GUI1; |
1208 | } |
1209 | Elem_HandlesPointerDown(&s->input.base, id, x, y)(&s->input.base)->VTABLE->HandlesPointerDown(& s->input.base, id, x, y); |
1210 | return TOUCH_TYPE_GUI1; |
1211 | } |
1212 | |
1213 | height = TextGroupWidget_UsedHeight(&s->chat); |
1214 | chatY = s->chat.y + s->chat.height - height; |
1215 | if (!Gui_Contains(s->chat.x, chatY, s->chat.width, height, x, y)) return false0; |
1216 | |
1217 | String_InitArray(text, textBuffer)text.buffer = textBuffer; text.length = 0; text.capacity = sizeof (textBuffer);; |
1218 | TextGroupWidget_GetSelected(&s->chat, &text, x, y); |
1219 | if (!text.length) return false0; |
1220 | |
1221 | if (Utils_IsUrlPrefix(&text)) { |
1222 | UrlWarningOverlay_Show(&text); |
1223 | } else if (Gui.ClickableChat) { |
1224 | ChatScreen_AppendInput(&text); |
1225 | } |
1226 | return TOUCH_TYPE_GUI1; |
1227 | } |
1228 | |
1229 | static void ChatScreen_Init(void* screen) { |
1230 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1231 | ChatInputWidget_Create(&s->input); |
1232 | s->input.base.OnTextChanged = ChatScreen_OnInputTextChanged; |
1233 | SpecialInputWidget_Create(&s->altText, &s->chatFont, &s->input.base); |
1234 | |
1235 | TextGroupWidget_Create(&s->status, CHAT_MAX_STATUS(sizeof(Chat_Status) / sizeof(Chat_Status[0])), |
1236 | s->statusTextures, ChatScreen_GetStatus); |
1237 | TextGroupWidget_Create(&s->bottomRight, CHAT_MAX_BOTTOMRIGHT(sizeof(Chat_BottomRight) / sizeof(Chat_BottomRight[0])), |
1238 | s->bottomRightTextures, ChatScreen_GetBottomRight); |
1239 | TextGroupWidget_Create(&s->chat, Gui.Chatlines, |
1240 | s->chatTextures, ChatScreen_GetChat); |
1241 | TextGroupWidget_Create(&s->clientStatus, CHAT_MAX_CLIENTSTATUS(sizeof(Chat_ClientStatus) / sizeof(Chat_ClientStatus[0])), |
1242 | s->clientStatusTextures, ChatScreen_GetClientStatus); |
1243 | TextWidget_Init(&s->announcement); |
1244 | |
1245 | s->status.collapsible[0] = true1; /* Texture pack download status */ |
1246 | s->clientStatus.collapsible[0] = true1; |
1247 | s->clientStatus.collapsible[1] = true1; |
1248 | |
1249 | s->chat.underlineUrls = !Game_ClassicMode; |
1250 | s->chatIndex = Chat_Log.count - Gui.Chatlines; |
1251 | |
1252 | Event_Register_(&ChatEvents.ChatReceived, s, ChatScreen_ChatReceived)Event_Register((struct Event_Void*)(&ChatEvents.ChatReceived ), s, (Event_Void_Callback)(ChatScreen_ChatReceived)); |
1253 | Event_Register_(&ChatEvents.ColCodeChanged, s, ChatScreen_ColCodeChanged)Event_Register((struct Event_Void*)(&ChatEvents.ColCodeChanged ), s, (Event_Void_Callback)(ChatScreen_ColCodeChanged)); |
1254 | |
1255 | #ifdef CC_BUILD_TOUCH |
1256 | if (!Input_TouchMode0) return; |
1257 | ButtonWidget_Init(&s->send, 100, NULL((void*)0)); |
1258 | ButtonWidget_Init(&s->cancel, 100, NULL((void*)0)); |
1259 | #endif |
1260 | } |
1261 | |
1262 | static void ChatScreen_Render(void* screen, double delta) { |
1263 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1264 | |
1265 | if (Game_HideGui && s->grabsInput) { |
1266 | Gfx_SetTexturing(true1); |
1267 | Elem_Render(&s->input.base, delta)(&s->input.base)->VTABLE->Render(&s->input .base, delta); |
1268 | Gfx_SetTexturing(false0); |
1269 | } |
1270 | if (Game_HideGui) return; |
1271 | |
1272 | if (!TabListOverlay_Instance.active && !Gui_GetBlocksWorld()) { |
1273 | Gfx_SetTexturing(true1); |
1274 | ChatScreen_DrawCrosshairs(); |
1275 | Gfx_SetTexturing(false0); |
1276 | } |
1277 | if (s->grabsInput && !Gui.ClassicChat) { |
1278 | ChatScreen_DrawChatBackground(s); |
1279 | } |
1280 | |
1281 | Gfx_SetTexturing(true1); |
1282 | ChatScreen_DrawChat(s, delta); |
1283 | Gfx_SetTexturing(false0); |
1284 | } |
1285 | |
1286 | static void ChatScreen_Free(void* screen) { |
1287 | struct ChatScreen* s = (struct ChatScreen*)screen; |
1288 | Event_Unregister_(&ChatEvents.ChatReceived, s, ChatScreen_ChatReceived)Event_Unregister((struct Event_Void*)(&ChatEvents.ChatReceived ), s, (Event_Void_Callback)(ChatScreen_ChatReceived)); |
1289 | Event_Unregister_(&ChatEvents.ColCodeChanged, s, ChatScreen_ColCodeChanged)Event_Unregister((struct Event_Void*)(&ChatEvents.ColCodeChanged ), s, (Event_Void_Callback)(ChatScreen_ColCodeChanged)); |
1290 | } |
1291 | |
1292 | static const struct ScreenVTABLE ChatScreen_VTABLE = { |
1293 | ChatScreen_Init, Screen_NullUpdate, ChatScreen_Free, |
1294 | ChatScreen_Render, ChatScreen_BuildMesh, |
1295 | ChatScreen_KeyDown, ChatScreen_KeyUp, ChatScreen_KeyPress, ChatScreen_TextChanged, |
1296 | ChatScreen_PointerDown, Screen_PointerUp, Screen_FPointer, ChatScreen_MouseScroll, |
1297 | ChatScreen_Layout, ChatScreen_ContextLost, ChatScreen_ContextRecreated |
1298 | }; |
1299 | void ChatScreen_Show(void) { |
1300 | struct ChatScreen* s = &ChatScreen_Instance; |
1301 | s->lastDownloadStatus = HTTP_PROGRESS_NOT_WORKING_ON; |
1302 | |
1303 | s->VTABLE = &ChatScreen_VTABLE; |
1304 | Gui_Chat = s; |
1305 | Gui_Add((struct Screen*)s, GUI_PRIORITY_CHAT); |
1306 | } |
1307 | |
1308 | void ChatScreen_OpenInput(const cc_string* text) { |
1309 | struct ChatScreen* s = &ChatScreen_Instance; |
1310 | struct OpenKeyboardArgs args; |
1311 | s->suppressNextPress = true1; |
1312 | s->grabsInput = true1; |
1313 | |
1314 | Gui_UpdateInputGrab(); |
1315 | OpenKeyboardArgs_Init(&args, text, KEYBOARD_TYPE_TEXT); |
1316 | args.placeholder = "Enter chat"; |
1317 | Window_OpenKeyboard(&args); |
1318 | |
1319 | String_Copy(&s->input.base.text, text); |
1320 | InputWidget_UpdateText(&s->input.base); |
1321 | } |
1322 | |
1323 | void ChatScreen_AppendInput(const cc_string* text) { |
1324 | struct ChatScreen* s = &ChatScreen_Instance; |
1325 | InputWidget_AppendText(&s->input.base, text); |
1326 | } |
1327 | |
1328 | void ChatScreen_SetChatlines(int lines) { |
1329 | struct ChatScreen* s = &ChatScreen_Instance; |
1330 | Elem_Free(&s->chat)(&s->chat)->VTABLE->Free(&s->chat); |
1331 | s->chatIndex += s->chat.lines - lines; |
1332 | s->chat.lines = lines; |
1333 | TextGroupWidget_RedrawAll(&s->chat); |
1334 | } |
1335 | |
1336 | |
1337 | /*########################################################################################################################* |
1338 | *-----------------------------------------------------InventoryScreen-----------------------------------------------------* |
1339 | *#########################################################################################################################*/ |
1340 | static struct InventoryScreen { |
1341 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
1342 | struct FontDesc font; |
1343 | struct TableWidget table; |
1344 | cc_bool releasedInv, deferredSelect; |
1345 | } InventoryScreen_Instance; |
1346 | |
1347 | static void InventoryScreen_OnBlockChanged(void* screen) { |
1348 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1349 | TableWidget_OnInventoryChanged(&s->table); |
1350 | } |
1351 | |
1352 | static void InventoryScreen_ContextLost(void* screen) { |
1353 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1354 | Font_Free(&s->font); |
1355 | Elem_Free(&s->table)(&s->table)->VTABLE->Free(&s->table); |
1356 | } |
1357 | |
1358 | static void InventoryScreen_ContextRecreated(void* screen) { |
1359 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1360 | Gui_MakeBodyFont(&s->font); |
1361 | TableWidget_Recreate(&s->table); |
1362 | } |
1363 | |
1364 | static void InventoryScreen_BuildMesh(void* screen) { } |
1365 | |
1366 | static void InventoryScreen_MoveToSelected(struct InventoryScreen* s) { |
1367 | struct TableWidget* table = &s->table; |
1368 | TableWidget_SetBlockTo(table, Inventory_SelectedBlock(Inventory.Table[Inventory.Offset + (Inventory.SelectedIndex) ])); |
1369 | TableWidget_Recreate(table); |
1370 | |
1371 | s->deferredSelect = false0; |
1372 | /* User is holding invalid block */ |
1373 | if (table->selectedIndex == -1) { |
1374 | TableWidget_MakeDescTex(table, Inventory_SelectedBlock(Inventory.Table[Inventory.Offset + (Inventory.SelectedIndex) ])); |
1375 | } |
1376 | } |
1377 | |
1378 | static void InventoryScreen_Init(void* screen) { |
1379 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1380 | |
1381 | TableWidget_Create(&s->table); |
1382 | s->table.font = &s->font; |
1383 | s->table.blocksPerRow = Inventory.BlocksPerRow; |
1384 | TableWidget_RecreateBlocks(&s->table); |
1385 | |
1386 | /* Can't immediately move to selected here, because cursor grabbed */ |
1387 | /* status might be toggled after InventoryScreen_Init() is called. */ |
1388 | /* That causes the cursor to be moved back to the middle of the window. */ |
1389 | s->deferredSelect = true1; |
1390 | |
1391 | Event_Register_(&BlockEvents.PermissionsChanged, s, InventoryScreen_OnBlockChanged)Event_Register((struct Event_Void*)(&BlockEvents.PermissionsChanged ), s, (Event_Void_Callback)(InventoryScreen_OnBlockChanged)); |
1392 | Event_Register_(&BlockEvents.BlockDefChanged, s, InventoryScreen_OnBlockChanged)Event_Register((struct Event_Void*)(&BlockEvents.BlockDefChanged ), s, (Event_Void_Callback)(InventoryScreen_OnBlockChanged)); |
1393 | } |
1394 | |
1395 | static void InventoryScreen_Render(void* screen, double delta) { |
1396 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1397 | if (s->deferredSelect) InventoryScreen_MoveToSelected(s); |
1398 | Elem_Render(&s->table, delta)(&s->table)->VTABLE->Render(&s->table, delta ); |
1399 | } |
1400 | |
1401 | static void InventoryScreen_Layout(void* screen) { |
1402 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1403 | s->table.scale = Gui_GetInventoryScale(); |
1404 | Widget_Layout(&s->table)(&s->table)->VTABLE->Reposition(&s->table ); |
1405 | } |
1406 | |
1407 | static void InventoryScreen_Free(void* screen) { |
1408 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1409 | Event_Unregister_(&BlockEvents.PermissionsChanged, s, InventoryScreen_OnBlockChanged)Event_Unregister((struct Event_Void*)(&BlockEvents.PermissionsChanged ), s, (Event_Void_Callback)(InventoryScreen_OnBlockChanged)); |
1410 | Event_Unregister_(&BlockEvents.BlockDefChanged, s, InventoryScreen_OnBlockChanged)Event_Unregister((struct Event_Void*)(&BlockEvents.BlockDefChanged ), s, (Event_Void_Callback)(InventoryScreen_OnBlockChanged)); |
1411 | } |
1412 | |
1413 | static int InventoryScreen_KeyDown(void* screen, int key) { |
1414 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1415 | struct TableWidget* table = &s->table; |
1416 | |
1417 | if (key == KeyBinds[KEYBIND_INVENTORY] && s->releasedInv) { |
1418 | Gui_Remove((struct Screen*)s); |
1419 | } else if (key == KEY_ENTER && table->selectedIndex != -1) { |
1420 | Inventory_SetSelectedBlock(table->blocks[table->selectedIndex]); |
1421 | Gui_Remove((struct Screen*)s); |
1422 | } else if (Elem_HandlesKeyDown(table, key)(table)->VTABLE->HandlesKeyDown(table, key)) { |
1423 | } else { |
1424 | return Elem_HandlesKeyDown(&Gui_HUD->hotbar, key)(&Gui_HUD->hotbar)->VTABLE->HandlesKeyDown(& Gui_HUD->hotbar, key); |
1425 | } |
1426 | return true1; |
1427 | } |
1428 | |
1429 | static cc_bool InventoryScreen_IsHotbarActive(void) { |
1430 | struct Screen* grabbed = Gui.InputGrab; |
1431 | /* Only toggle hotbar when inventory or no grab screen is open */ |
1432 | return !grabbed || grabbed == (struct Screen*)&InventoryScreen_Instance; |
1433 | } |
1434 | |
1435 | static void InventoryScreen_KeyUp(void* screen, int key) { |
1436 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1437 | if (key == KeyBinds[KEYBIND_INVENTORY]) s->releasedInv = true1; |
1438 | } |
1439 | |
1440 | static int InventoryScreen_PointerDown(void* screen, int id, int x, int y) { |
1441 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1442 | struct TableWidget* table = &s->table; |
1443 | cc_bool handled, hotbar; |
1444 | |
1445 | if (table->scroll.draggingId == id) return TOUCH_TYPE_GUI1; |
1446 | if (HUDscreen_PointerDown(Gui_HUD, id, x, y)) return TOUCH_TYPE_GUI1; |
1447 | handled = Elem_HandlesPointerDown(table, id, x, y)(table)->VTABLE->HandlesPointerDown(table, id, x, y); |
1448 | |
1449 | if (!handled || table->pendingClose) { |
1450 | hotbar = Key_IsControlPressed()(Input_Pressed[KEY_LCTRL] || Input_Pressed[KEY_RCTRL]) || Key_IsShiftPressed()(Input_Pressed[KEY_LSHIFT] || Input_Pressed[KEY_RSHIFT]); |
1451 | if (!hotbar) Gui_Remove((struct Screen*)s); |
1452 | } |
1453 | return TOUCH_TYPE_GUI1; |
1454 | } |
1455 | |
1456 | static void InventoryScreen_PointerUp(void* screen, int id, int x, int y) { |
1457 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1458 | Elem_OnPointerUp(&s->table, id, x, y)(&s->table)->VTABLE->OnPointerUp(&s->table , id, x, y); |
1459 | } |
1460 | |
1461 | static int InventoryScreen_PointerMove(void* screen, int id, int x, int y) { |
1462 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1463 | return Elem_HandlesPointerMove(&s->table, id, x, y)(&s->table)->VTABLE->HandlesPointerMove(&s-> table, id, x, y); |
1464 | } |
1465 | |
1466 | static int InventoryScreen_MouseScroll(void* screen, float delta) { |
1467 | struct InventoryScreen* s = (struct InventoryScreen*)screen; |
1468 | |
1469 | cc_bool hotbar = Key_IsAltPressed()(Input_Pressed[KEY_LALT] || Input_Pressed[KEY_RALT]) || Key_IsControlPressed()(Input_Pressed[KEY_LCTRL] || Input_Pressed[KEY_RCTRL]) || Key_IsShiftPressed()(Input_Pressed[KEY_LSHIFT] || Input_Pressed[KEY_RSHIFT]); |
1470 | if (hotbar) return false0; |
1471 | return Elem_HandlesMouseScroll(&s->table, delta)(&s->table)->VTABLE->HandlesMouseScroll(&s-> table, delta); |
1472 | } |
1473 | |
1474 | static const struct ScreenVTABLE InventoryScreen_VTABLE = { |
1475 | InventoryScreen_Init, Screen_NullUpdate, InventoryScreen_Free, |
1476 | InventoryScreen_Render, InventoryScreen_BuildMesh, |
1477 | InventoryScreen_KeyDown, InventoryScreen_KeyUp, Screen_TKeyPress, Screen_TText, |
1478 | InventoryScreen_PointerDown, InventoryScreen_PointerUp, InventoryScreen_PointerMove, InventoryScreen_MouseScroll, |
1479 | InventoryScreen_Layout, InventoryScreen_ContextLost, InventoryScreen_ContextRecreated |
1480 | }; |
1481 | void InventoryScreen_Show(void) { |
1482 | struct InventoryScreen* s = &InventoryScreen_Instance; |
1483 | s->grabsInput = true1; |
1484 | s->closable = true1; |
1485 | |
1486 | s->VTABLE = &InventoryScreen_VTABLE; |
1487 | Gui_Add((struct Screen*)s, GUI_PRIORITY_INVENTORY); |
1488 | } |
1489 | |
1490 | |
1491 | /*########################################################################################################################* |
1492 | *------------------------------------------------------LoadingScreen------------------------------------------------------* |
1493 | *#########################################################################################################################*/ |
1494 | static struct LoadingScreen { |
1495 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
1496 | struct FontDesc font; |
1497 | float progress; |
1498 | int rows; |
1499 | |
1500 | int progX, progY, progWidth, progHeight; |
1501 | struct TextWidget title, message; |
1502 | cc_string titleStr, messageStr; |
1503 | const char* lastState; |
1504 | |
1505 | char _titleBuffer[STRING_SIZE64]; |
1506 | char _messageBuffer[STRING_SIZE64]; |
1507 | } LoadingScreen; |
1508 | #define LOADING_MAX_VERTICES(2 * 4) (2 * TEXTWIDGET_MAX4) |
1509 | #define LOADING_TILE_SIZE64 64 |
1510 | |
1511 | static struct Widget* loading_widgets[2] = { |
1512 | (struct Widget*)&LoadingScreen.title, (struct Widget*)&LoadingScreen.message |
1513 | }; |
1514 | |
1515 | static void LoadingScreen_SetTitle(struct LoadingScreen* s) { |
1516 | TextWidget_Set(&s->title, &s->titleStr, &s->font); |
1517 | s->dirty = true1; |
1518 | } |
1519 | static void LoadingScreen_SetMessage(struct LoadingScreen* s) { |
1520 | TextWidget_Set(&s->message, &s->messageStr, &s->font); |
1521 | s->dirty = true1; |
1522 | } |
1523 | |
1524 | static void LoadingScreen_CalcMaxVertices(struct LoadingScreen* s) { |
1525 | s->rows = Math_CeilDiv(WindowInfo.Height, LOADING_TILE_SIZE64); |
1526 | s->maxVertices = LOADING_MAX_VERTICES(2 * 4) + s->rows * 4; |
1527 | } |
1528 | |
1529 | static void LoadingScreen_Layout(void* screen) { |
1530 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1531 | int oldRows, y; |
1532 | Widget_SetLocation(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -31); |
1533 | Widget_SetLocation(&s->message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 17); |
1534 | y = Display_ScaleY(34); |
1535 | |
1536 | s->progWidth = Display_ScaleX(200); |
1537 | s->progX = Gui_CalcPos(ANCHOR_CENTRE, 0, s->progWidth, WindowInfo.Width); |
1538 | s->progHeight = Display_ScaleY(4); |
1539 | s->progY = Gui_CalcPos(ANCHOR_CENTRE, y, s->progHeight, WindowInfo.Height); |
1540 | |
1541 | oldRows = s->rows; |
1542 | LoadingScreen_CalcMaxVertices(s); |
1543 | if (oldRows == s->rows) return; |
1544 | Screen_UpdateVb(s); |
1545 | } |
1546 | |
1547 | static void LoadingScreen_ContextLost(void* screen) { |
1548 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1549 | Font_Free(&s->font); |
1550 | Screen_ContextLost(screen); |
1551 | } |
1552 | |
1553 | static void LoadingScreen_ContextRecreated(void* screen) { |
1554 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1555 | Gui_MakeBodyFont(&s->font); |
1556 | LoadingScreen_SetTitle(s); |
1557 | LoadingScreen_SetMessage(s); |
1558 | Screen_UpdateVb(s); |
1559 | } |
1560 | |
1561 | static void LoadingScreen_BuildMesh(void* screen) { |
1562 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1563 | struct VertexTextured* data; |
1564 | struct VertexTextured** ptr; |
1565 | struct Texture tex; |
1566 | TextureLoc loc; |
1567 | int atlasIndex, i; |
1568 | |
1569 | data = Screen_LockVb(s); |
1570 | ptr = &data; |
1571 | |
1572 | loc = Block_Tex(BLOCK_DIRT, FACE_YMAX)Blocks.Textures[(BLOCK_DIRT) * FACE_COUNT + (FACE_YMAX)]; |
1573 | Tex_SetRect(tex, 0,0, WindowInfo.Width,LOADING_TILE_SIZE)tex.X = 0; tex.Y = 0; tex.Width = WindowInfo.Width; tex.Height = 64;; |
1574 | tex.uv = Atlas1D_TexRec(loc, 1, &atlasIndex); |
1575 | tex.uv.U2 = (float)WindowInfo.Width / LOADING_TILE_SIZE64; |
1576 | |
1577 | for (i = 0; i < s->rows; i++) { |
1578 | tex.Y = i * LOADING_TILE_SIZE64; |
1579 | Gfx_Make2DQuad(&tex, PackedCol_Make(64, 64, 64, 255)(((cc_uint8)(64) << 0) | ((cc_uint8)(64) << 8) | ( (cc_uint8)(64) << 16) | ((cc_uint8)(255) << 24)), ptr); |
1580 | } |
1581 | |
1582 | Widget_BuildMesh(&s->title, ptr)(&s->title)->VTABLE->BuildMesh(&s->title, ptr); |
1583 | Widget_BuildMesh(&s->message, ptr)(&s->message)->VTABLE->BuildMesh(&s->message , ptr); |
1584 | Gfx_UnlockDynamicVb(s->vb); |
1585 | } |
1586 | |
1587 | static void LoadingScreen_MapLoading(void* screen, float progress) { |
1588 | ((struct LoadingScreen*)screen)->progress = progress; |
1589 | } |
1590 | |
1591 | static void LoadingScreen_MapLoaded(void* screen) { |
1592 | Gui_Remove((struct Screen*)screen); |
1593 | } |
1594 | |
1595 | static void LoadingScreen_Init(void* screen) { |
1596 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1597 | TextWidget_Init(&s->title); |
1598 | TextWidget_Init(&s->message); |
1599 | s->widgets = loading_widgets; |
1600 | s->numWidgets = Array_Elems(loading_widgets)(sizeof(loading_widgets) / sizeof(loading_widgets[0])); |
1601 | |
1602 | LoadingScreen_CalcMaxVertices(s); |
1603 | Gfx_SetFog(false0); |
1604 | Event_Register_(&WorldEvents.Loading, s, LoadingScreen_MapLoading)Event_Register((struct Event_Void*)(&WorldEvents.Loading) , s, (Event_Void_Callback)(LoadingScreen_MapLoading)); |
1605 | Event_Register_(&WorldEvents.MapLoaded, s, LoadingScreen_MapLoaded)Event_Register((struct Event_Void*)(&WorldEvents.MapLoaded ), s, (Event_Void_Callback)(LoadingScreen_MapLoaded)); |
1606 | } |
1607 | |
1608 | static void LoadingScreen_Render(void* screen, double delta) { |
1609 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1610 | int offset, filledWidth; |
1611 | TextureLoc loc; |
1612 | |
1613 | Gfx_SetTexturing(true1); |
1614 | Gfx_SetVertexFormat(VERTEX_FORMAT_TEXTURED); |
1615 | Gfx_BindDynamicVbGfx_BindVb(s->vb); |
1616 | |
1617 | /* Draw background dirt */ |
1618 | offset = 0; |
1619 | if (s->rows) { |
1620 | loc = Block_Tex(BLOCK_DIRT, FACE_YMAX)Blocks.Textures[(BLOCK_DIRT) * FACE_COUNT + (FACE_YMAX)]; |
1621 | Gfx_BindTexture(Atlas1D.TexIds[Atlas1D_Index(loc)((loc) >> Atlas1D.Shift)]); |
1622 | Gfx_DrawVb_IndexedTris(s->rows * 4); |
1623 | offset = s->rows * 4; |
1624 | } |
1625 | |
1626 | offset = Widget_Render2(&s->title, offset)(&s->title)->VTABLE->Render2(&s->title, offset ); |
1627 | offset = Widget_Render2(&s->message, offset)(&s->message)->VTABLE->Render2(&s->message , offset); |
1628 | Gfx_SetTexturing(false0); |
1629 | |
1630 | filledWidth = (int)(s->progWidth * s->progress); |
1631 | Gfx_Draw2DFlat(s->progX, s->progY, s->progWidth, |
1632 | s->progHeight, PackedCol_Make(128, 128, 128, 255)(((cc_uint8)(128) << 0) | ((cc_uint8)(128) << 8) | ((cc_uint8)(128) << 16) | ((cc_uint8)(255) << 24 ))); |
1633 | Gfx_Draw2DFlat(s->progX, s->progY, filledWidth, |
1634 | s->progHeight, PackedCol_Make(128, 255, 128, 255)(((cc_uint8)(128) << 0) | ((cc_uint8)(255) << 8) | ((cc_uint8)(128) << 16) | ((cc_uint8)(255) << 24 ))); |
1635 | } |
1636 | |
1637 | static void LoadingScreen_Free(void* screen) { |
1638 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1639 | Event_Unregister_(&WorldEvents.Loading, s, LoadingScreen_MapLoading)Event_Unregister((struct Event_Void*)(&WorldEvents.Loading ), s, (Event_Void_Callback)(LoadingScreen_MapLoading)); |
1640 | Event_Unregister_(&WorldEvents.MapLoaded, s, LoadingScreen_MapLoaded)Event_Unregister((struct Event_Void*)(&WorldEvents.MapLoaded ), s, (Event_Void_Callback)(LoadingScreen_MapLoaded)); |
1641 | } |
1642 | |
1643 | CC_NOINLINE__attribute__((noinline)) static void LoadingScreen_ShowCommon(const cc_string* title, const cc_string* message) { |
1644 | struct LoadingScreen* s = &LoadingScreen; |
1645 | s->lastState = NULL((void*)0); |
1646 | s->progress = 0.0f; |
1647 | |
1648 | String_InitArray(s->titleStr, s->_titleBuffer)s->titleStr.buffer = s->_titleBuffer; s->titleStr.length = 0; s->titleStr.capacity = sizeof(s->_titleBuffer);; |
1649 | String_AppendString(&s->titleStr, title); |
1650 | String_InitArray(s->messageStr, s->_messageBuffer)s->messageStr.buffer = s->_messageBuffer; s->messageStr .length = 0; s->messageStr.capacity = sizeof(s->_messageBuffer );; |
1651 | String_AppendString(&s->messageStr, message); |
1652 | |
1653 | s->grabsInput = true1; |
1654 | s->blocksWorld = true1; |
1655 | Gui_Add((struct Screen*)s, |
1656 | Game_ClassicMode ? GUI_PRIORITY_OLDLOADING : GUI_PRIORITY_LOADING); |
1657 | } |
1658 | |
1659 | static const struct ScreenVTABLE LoadingScreen_VTABLE = { |
1660 | LoadingScreen_Init, Screen_NullUpdate, LoadingScreen_Free, |
1661 | LoadingScreen_Render, LoadingScreen_BuildMesh, |
1662 | Screen_TInput, Screen_InputUp, Screen_TKeyPress, Screen_TText, |
1663 | Screen_TPointer, Screen_PointerUp, Screen_TPointer, Screen_TMouseScroll, |
1664 | LoadingScreen_Layout, LoadingScreen_ContextLost, LoadingScreen_ContextRecreated |
1665 | }; |
1666 | void LoadingScreen_Show(const cc_string* title, const cc_string* message) { |
1667 | LoadingScreen.VTABLE = &LoadingScreen_VTABLE; |
1668 | LoadingScreen_ShowCommon(title, message); |
1669 | } |
1670 | |
1671 | |
1672 | /*########################################################################################################################* |
1673 | *--------------------------------------------------GeneratingMapScreen----------------------------------------------------* |
1674 | *#########################################################################################################################*/ |
1675 | static void GeneratingScreen_AtlasChanged(void* obj) { |
1676 | LoadingScreen.dirty = true1; /* Dirt texture may have changed */ |
1677 | } |
1678 | |
1679 | static void GeneratingScreen_Init(void* screen) { |
1680 | void* thread; |
1681 | Gen_Done = false0; |
1682 | LoadingScreen_Init(screen); |
1683 | |
1684 | Gen_Blocks = (BlockRaw*)Mem_TryAlloc(World.Volume, 1); |
1685 | if (!Gen_Blocks) { |
1686 | Window_ShowDialog("Out of memory", "Not enough free memory to generate a map that large.\nTry a smaller size."); |
1687 | Gen_Done = true1; |
1688 | } else if (Gen_Vanilla) { |
1689 | thread = Thread_Start(NotchyGen_Generate); |
1690 | Thread_Detach(thread); |
1691 | } else { |
1692 | thread = Thread_Start(FlatgrassGen_Generate); |
1693 | Thread_Detach(thread); |
1694 | } |
1695 | Event_Register_(&TextureEvents.AtlasChanged, NULL, GeneratingScreen_AtlasChanged)Event_Register((struct Event_Void*)(&TextureEvents.AtlasChanged ), ((void*)0), (Event_Void_Callback)(GeneratingScreen_AtlasChanged )); |
1696 | } |
1697 | static void GeneratingScreen_Free(void* screen) { |
1698 | LoadingScreen_Free(screen); |
1699 | Event_Unregister_(&TextureEvents.AtlasChanged, NULL, GeneratingScreen_AtlasChanged)Event_Unregister((struct Event_Void*)(&TextureEvents.AtlasChanged ), ((void*)0), (Event_Void_Callback)(GeneratingScreen_AtlasChanged )); |
1700 | } |
1701 | |
1702 | static void GeneratingScreen_EndGeneration(void) { |
1703 | struct LocalPlayer* p = &LocalPlayer_Instance; |
1704 | float x, z; |
1705 | |
1706 | Gen_Done = false0; |
1707 | World_SetNewMap(Gen_Blocks, World.Width, World.Height, World.Length); |
1708 | if (!Gen_Blocks) { Chat_AddRaw("&cFailed to generate the map."); return; } |
1709 | Gen_Blocks = NULL((void*)0); |
1710 | |
1711 | x = (World.Width / 2) + 0.5f; z = (World.Length / 2) + 0.5f; |
1712 | p->Spawn = Respawn_FindSpawnPosition(x, z, p->Base.Size); |
1713 | |
1714 | p->SpawnYaw = 0.0f; |
1715 | p->SpawnPitch = 0.0f; |
1716 | LocalPlayer_MoveToSpawn(); |
1717 | } |
1718 | |
1719 | static void GeneratingScreen_Update(void* screen, double delta) { |
1720 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1721 | const char* state = (const char*)Gen_CurrentState; |
1722 | if (state == s->lastState) return; |
1723 | s->lastState = state; |
1724 | |
1725 | s->messageStr.length = 0; |
1726 | String_AppendConst(&s->messageStr, state); |
1727 | LoadingScreen_SetMessage(s); |
1728 | } |
1729 | |
1730 | static void GeneratingScreen_Render(void* screen, double delta) { |
1731 | struct LoadingScreen* s = (struct LoadingScreen*)screen; |
1732 | s->progress = Gen_CurrentProgress; |
1733 | LoadingScreen_Render(s, delta); |
1734 | if (Gen_Done) GeneratingScreen_EndGeneration(); |
1735 | } |
1736 | |
1737 | static const struct ScreenVTABLE GeneratingScreen_VTABLE = { |
1738 | GeneratingScreen_Init, GeneratingScreen_Update, GeneratingScreen_Free, |
1739 | GeneratingScreen_Render, LoadingScreen_BuildMesh, |
1740 | Screen_TInput, Screen_InputUp, Screen_TKeyPress, Screen_TText, |
1741 | Screen_TPointer, Screen_PointerUp, Screen_FPointer, Screen_TMouseScroll, |
1742 | LoadingScreen_Layout, LoadingScreen_ContextLost, LoadingScreen_ContextRecreated |
1743 | }; |
1744 | void GeneratingScreen_Show(void) { |
1745 | static const cc_string title = String_FromConst("Generating level"){ "Generating level", (sizeof("Generating level") - 1), (sizeof ("Generating level") - 1)}; |
1746 | static const cc_string message = String_FromConst("Generating.."){ "Generating..", (sizeof("Generating..") - 1), (sizeof("Generating.." ) - 1)}; |
1747 | |
1748 | LoadingScreen.VTABLE = &GeneratingScreen_VTABLE; |
1749 | LoadingScreen_ShowCommon(&title, &message); |
1750 | } |
1751 | |
1752 | |
1753 | /*########################################################################################################################* |
1754 | *----------------------------------------------------DisconnectScreen-----------------------------------------------------* |
1755 | *#########################################################################################################################*/ |
1756 | static struct DisconnectScreen { |
1757 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
1758 | double initTime; |
1759 | cc_bool canReconnect, lastActive; |
1760 | int lastSecsLeft; |
1761 | struct ButtonWidget reconnect, quit; |
1762 | |
1763 | struct FontDesc titleFont, messageFont; |
1764 | struct TextWidget title, message; |
1765 | char _titleBuffer[STRING_SIZE64]; |
1766 | char _messageBuffer[STRING_SIZE64]; |
1767 | cc_string titleStr, messageStr; |
1768 | } DisconnectScreen; |
1769 | |
1770 | static struct Widget* disconnect_widgets[4] = { |
1771 | (struct Widget*)&DisconnectScreen.title, |
1772 | (struct Widget*)&DisconnectScreen.message, |
1773 | (struct Widget*)&DisconnectScreen.reconnect, |
1774 | (struct Widget*)&DisconnectScreen.quit |
1775 | }; |
1776 | #define DISCONNECT_MAX_VERTICES(2 * 4 + 2 * 12) (2 * TEXTWIDGET_MAX4 + 2 * BUTTONWIDGET_MAX12) |
1777 | #define DISCONNECT_DELAY_SECS5 5 |
1778 | |
1779 | static void DisconnectScreen_Layout(void* screen) { |
1780 | struct DisconnectScreen* s = (struct DisconnectScreen*)screen; |
1781 | Widget_SetLocation(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -30); |
1782 | Widget_SetLocation(&s->message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 10); |
1783 | Widget_SetLocation(&s->reconnect, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 80); |
1784 | Widget_SetLocation(&s->quit, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 130); |
1785 | } |
1786 | |
1787 | static void DisconnectScreen_UpdateReconnect(struct DisconnectScreen* s) { |
1788 | cc_string msg; char msgBuffer[STRING_SIZE64]; |
1789 | int elapsed, secsLeft; |
1790 | String_InitArray(msg, msgBuffer)msg.buffer = msgBuffer; msg.length = 0; msg.capacity = sizeof (msgBuffer);; |
1791 | |
1792 | if (s->canReconnect) { |
1793 | elapsed = (int)(Game.Time - s->initTime); |
1794 | secsLeft = DISCONNECT_DELAY_SECS5 - elapsed; |
1795 | |
1796 | if (secsLeft > 0) { |
1797 | String_Format1(&msg, "Reconnect in %i", &secsLeft); |
1798 | } |
1799 | s->reconnect.disabled = secsLeft > 0; |
1800 | } |
1801 | |
1802 | if (!msg.length) String_AppendConst(&msg, "Reconnect"); |
1803 | ButtonWidget_Set(&s->reconnect, &msg, &s->titleFont); |
1804 | } |
1805 | |
1806 | static void DisconnectScreen_ContextLost(void* screen) { |
1807 | struct DisconnectScreen* s = (struct DisconnectScreen*)screen; |
1808 | Font_Free(&s->titleFont); |
1809 | Font_Free(&s->messageFont); |
1810 | Screen_ContextLost(screen); |
1811 | } |
1812 | |
1813 | static void DisconnectScreen_ContextRecreated(void* screen) { |
1814 | struct DisconnectScreen* s = (struct DisconnectScreen*)screen; |
1815 | Screen_UpdateVb(screen); |
1816 | |
1817 | Gui_MakeTitleFont(&s->titleFont); |
1818 | Gui_MakeBodyFont(&s->messageFont); |
1819 | TextWidget_Set(&s->title, &s->titleStr, &s->titleFont); |
1820 | TextWidget_Set(&s->message, &s->messageStr, &s->messageFont); |
1821 | |
1822 | DisconnectScreen_UpdateReconnect(s); |
1823 | ButtonWidget_SetConst(&s->quit, "Quit game", &s->titleFont); |
1824 | } |
1825 | |
1826 | static void DisconnectScreen_OnReconnect(void* s, void* w) { |
1827 | Gui_Remove((struct Screen*)s); |
1828 | Gui_ShowDefault(); |
1829 | Server.BeginConnect(); |
1830 | } |
1831 | static void DisconnectScreen_OnQuit(void* s, void* w) { Window_Close(); } |
1832 | |
1833 | static void DisconnectScreen_Init(void* screen) { |
1834 | struct DisconnectScreen* s = (struct DisconnectScreen*)screen; |
1835 | TextWidget_Init(&s->title); |
1836 | TextWidget_Init(&s->message); |
1837 | |
1838 | ButtonWidget_Init(&s->reconnect, 300, DisconnectScreen_OnReconnect); |
1839 | ButtonWidget_Init(&s->quit, 300, DisconnectScreen_OnQuit); |
1840 | s->reconnect.disabled = !s->canReconnect; |
1841 | s->maxVertices = DISCONNECT_MAX_VERTICES(2 * 4 + 2 * 12); |
1842 | |
1843 | /* NOTE: changing VSync can't be done within frame, causes crash on some GPUs */ |
1844 | Gfx_SetFpsLimit(Game_FpsLimit == FPS_LIMIT_VSYNC, 1000 / 5.0f); |
1845 | |
1846 | s->initTime = Game.Time; |
1847 | s->lastSecsLeft = DISCONNECT_DELAY_SECS5; |
1848 | s->widgets = disconnect_widgets; |
1849 | s->numWidgets = Array_Elems(disconnect_widgets)(sizeof(disconnect_widgets) / sizeof(disconnect_widgets[0])); |
1850 | } |
1851 | |
1852 | static void DisconnectScreen_Update(void* screen, double delta) { |
1853 | struct DisconnectScreen* s = (struct DisconnectScreen*)screen; |
1854 | int elapsed, secsLeft; |
1855 | |
1856 | if (!s->canReconnect) return; |
1857 | elapsed = (int)(Game.Time - s->initTime); |
1858 | secsLeft = DISCONNECT_DELAY_SECS5 - elapsed; |
1859 | |
1860 | if (secsLeft < 0) secsLeft = 0; |
1861 | if (s->lastSecsLeft == secsLeft && s->reconnect.active == s->lastActive) return; |
1862 | DisconnectScreen_UpdateReconnect(s); |
1863 | |
1864 | s->lastSecsLeft = secsLeft; |
1865 | s->lastActive = s->reconnect.active; |
1866 | s->dirty = true1; |
1867 | } |
1868 | |
1869 | static void DisconnectScreen_Render(void* screen, double delta) { |
1870 | PackedCol top = PackedCol_Make(64, 32, 32, 255)(((cc_uint8)(64) << 0) | ((cc_uint8)(32) << 8) | ( (cc_uint8)(32) << 16) | ((cc_uint8)(255) << 24)); |
1871 | PackedCol bottom = PackedCol_Make(80, 16, 16, 255)(((cc_uint8)(80) << 0) | ((cc_uint8)(16) << 8) | ( (cc_uint8)(16) << 16) | ((cc_uint8)(255) << 24)); |
1872 | Gfx_Draw2DGradient(0, 0, WindowInfo.Width, WindowInfo.Height, top, bottom); |
1873 | |
1874 | Gfx_SetTexturing(true1); |
1875 | Screen_Render2Widgets(screen, delta); |
1876 | Gfx_SetTexturing(false0); |
1877 | } |
1878 | |
1879 | static void DisconnectScreen_Free(void* screen) { Game_SetFpsLimit(Game_FpsLimit); } |
1880 | |
1881 | static const struct ScreenVTABLE DisconnectScreen_VTABLE = { |
1882 | DisconnectScreen_Init, DisconnectScreen_Update, DisconnectScreen_Free, |
1883 | DisconnectScreen_Render, Screen_BuildMesh, |
1884 | Screen_InputDown, Screen_InputUp, Screen_TKeyPress, Screen_TText, |
1885 | Menu_PointerDown, Screen_PointerUp, Menu_PointerMove, Screen_TMouseScroll, |
1886 | DisconnectScreen_Layout, DisconnectScreen_ContextLost, DisconnectScreen_ContextRecreated |
1887 | }; |
1888 | void DisconnectScreen_Show(const cc_string* title, const cc_string* message) { |
1889 | static const cc_string kick = String_FromConst("Kicked "){ "Kicked ", (sizeof("Kicked ") - 1), (sizeof("Kicked ") - 1) }; |
1890 | static const cc_string ban = String_FromConst("Banned "){ "Banned ", (sizeof("Banned ") - 1), (sizeof("Banned ") - 1) }; |
1891 | cc_string why; char whyBuffer[STRING_SIZE64]; |
1892 | struct DisconnectScreen* s = &DisconnectScreen; |
1893 | |
1894 | s->grabsInput = true1; |
1895 | s->blocksWorld = true1; |
1896 | |
1897 | String_InitArray(s->titleStr, s->_titleBuffer)s->titleStr.buffer = s->_titleBuffer; s->titleStr.length = 0; s->titleStr.capacity = sizeof(s->_titleBuffer);; |
1898 | String_AppendString(&s->titleStr, title); |
1899 | String_InitArray(s->messageStr, s->_messageBuffer)s->messageStr.buffer = s->_messageBuffer; s->messageStr .length = 0; s->messageStr.capacity = sizeof(s->_messageBuffer );; |
1900 | String_AppendString(&s->messageStr, message); |
1901 | |
1902 | String_InitArray(why, whyBuffer)why.buffer = whyBuffer; why.length = 0; why.capacity = sizeof (whyBuffer);; |
1903 | String_AppendColorless(&why, message); |
1904 | |
1905 | s->canReconnect = !(String_CaselessStarts(&why, &kick) || String_CaselessStarts(&why, &ban)); |
1906 | s->VTABLE = &DisconnectScreen_VTABLE; |
1907 | |
1908 | /* Remove all screens instead of just drawing over them to reduce GPU usage */ |
1909 | Gui_RemoveAll(); |
1910 | Gui_Add((struct Screen*)s, GUI_PRIORITY_DISCONNECT); |
1911 | } |
1912 | |
1913 | |
1914 | /*########################################################################################################################* |
1915 | *--------------------------------------------------------TouchScreen------------------------------------------------------* |
1916 | *#########################################################################################################################*/ |
1917 | #ifdef CC_BUILD_TOUCH |
1918 | #define TOUCH_EXTRA_BTNS 2 |
1919 | #define TOUCH_MAX_BTNS (ONSCREEN_MAX_BTNS13 + TOUCH_EXTRA_BTNS + 1) |
1920 | struct TouchButtonDesc { |
1921 | const char* text; |
1922 | cc_uint8 bind, x, y; |
1923 | Widget_LeftClick OnClick; |
1924 | cc_bool* enabled; |
1925 | }; |
1926 | |
1927 | static struct TouchScreen { |
1928 | Screen_Bodyconst struct ScreenVTABLE* VTABLE; cc_bool grabsInput; cc_bool blocksWorld; cc_bool closable; cc_bool dirty; int maxVertices ; GfxResourceID vb; struct Widget** widgets; int numWidgets; |
1929 | const struct TouchButtonDesc* descs; |
1930 | int numOnscreen, numBtns; |
1931 | struct FontDesc font; |
1932 | struct ThumbstickWidget thumbstick; |
1933 | const struct TouchButtonDesc* onscreenDescs[ONSCREEN_MAX_BTNS13]; |
1934 | struct ButtonWidget onscreen[ONSCREEN_MAX_BTNS13]; |
1935 | struct ButtonWidget btns[TOUCH_EXTRA_BTNS], more; |
1936 | } TouchScreen; |
1937 | |
1938 | static struct Widget* touch_widgets[ONSCREEN_MAX_BTNS13 + TOUCH_EXTRA_BTNS + 2] = { |
1939 | NULL((void*)0),NULL((void*)0),NULL((void*)0),NULL((void*)0), NULL((void*)0),NULL((void*)0),NULL((void*)0),NULL((void*)0), NULL((void*)0),NULL((void*)0),NULL((void*)0),NULL((void*)0), NULL((void*)0), |
1940 | NULL((void*)0),NULL((void*)0), (struct Widget*)&TouchScreen.thumbstick, (struct Widget*)&TouchScreen.more |
1941 | }; |
1942 | #define TOUCH_MAX_VERTICES (THUMBSTICKWIDGET_MAX + TOUCH_MAX_BTNS * BUTTONWIDGET_MAX12) |
1943 | |
1944 | static void TouchScreen_OnscreenClick(void* screen, void* widget) { |
1945 | struct TouchScreen* s = (struct TouchScreen*)screen; |
1946 | int i = Screen_Index(screen, widget); |
1947 | int key = KeyBinds[s->onscreenDescs[i]->bind]; |
1948 | Input_Set(key, !Input_Pressed[key]); |
1949 | } |
1950 | |
1951 | static void TouchScreen_ChatClick(void* s, void* w) { ChatScreen_OpenInput(&String_Empty); } |
1952 | static void TouchScreen_RespawnClick(void* s, void* w) { LocalPlayer_HandleRespawn(); } |
1953 | static void TouchScreen_SetSpawnClick(void* s, void* w) { LocalPlayer_HandleSetSpawn(); } |
1954 | static void TouchScreen_FlyClick(void* s, void* w) { LocalPlayer_HandleFly(); } |
1955 | static void TouchScreen_NoclipClick(void* s, void* w) { LocalPlayer_HandleNoclip(); } |
1956 | static void TouchScreen_CameraClick(void* s, void* w) { Camera_CycleActive(); } |
1957 | static void TouchScreen_MoreClick(void* s, void* w) { TouchMoreScreen_Show(); } |
1958 | static void TouchScreen_SwitchClick(void* s, void* w) { Inventory_SwitchHotbar(); } |
1959 | |
1960 | static void TouchScreen_TabClick(void* s, void* w) { |
1961 | if (TabListOverlay_Instance.active) { |
1962 | Gui_Remove((struct Screen*)&TabListOverlay_Instance); |
1963 | } else { |
1964 | TabListOverlay_Show(); |
1965 | } |
1966 | } |
1967 | |
1968 | static void TouchScreen_SpeedClick(void* s, void* w) { |
1969 | struct HacksComp* hacks = &LocalPlayer_Instance.Hacks; |
1970 | if (hacks->Enabled) hacks->Speeding = !hacks->Speeding; |
1971 | } |
1972 | static void TouchScreen_HalfClick(void* s, void* w) { |
1973 | struct HacksComp* hacks = &LocalPlayer_Instance.Hacks; |
1974 | if (hacks->Enabled) hacks->HalfSpeeding = !hacks->HalfSpeeding; |
1975 | } |
1976 | |
1977 | static void TouchScreen_BindClick(void* screen, void* widget) { |
1978 | struct TouchScreen* s = (struct TouchScreen*)screen; |
1979 | int i = Screen_Index(screen, widget) - ONSCREEN_MAX_BTNS13; |
1980 | Input_Set(KeyBinds[s->descs[i].bind], true1); |
1981 | } |
1982 | |
1983 | static const struct TouchButtonDesc onscreenDescs[ONSCREEN_MAX_BTNS13] = { |
1984 | { "Chat", 0,0,0, TouchScreen_ChatClick }, |
1985 | { "Tablist", 0,0,0, TouchScreen_TabClick }, |
1986 | { "Respawn", 0,0,0, TouchScreen_RespawnClick, &LocalPlayer_Instance.Hacks.CanRespawn }, |
1987 | { "Set spawn", 0,0,0, TouchScreen_SetSpawnClick, &LocalPlayer_Instance.Hacks.CanRespawn }, |
1988 | { "Fly", 0,0,0, TouchScreen_FlyClick, &LocalPlayer_Instance.Hacks.CanFly }, |
1989 | { "Noclip", 0,0,0, TouchScreen_NoclipClick, &LocalPlayer_Instance.Hacks.CanNoclip }, |
1990 | { "Speed", 0,0,0, TouchScreen_SpeedClick, &LocalPlayer_Instance.Hacks.CanSpeed }, |
1991 | { "\xabSpeed", 0,0,0, TouchScreen_HalfClick, &LocalPlayer_Instance.Hacks.CanSpeed }, |
1992 | { "Camera", 0,0,0, TouchScreen_CameraClick, &LocalPlayer_Instance.Hacks.CanUseThirdPerson }, |
1993 | { "Delete", KEYBIND_DELETE_BLOCK, 0,0, TouchScreen_OnscreenClick }, |
1994 | { "Pick", KEYBIND_PICK_BLOCK, 0,0, TouchScreen_OnscreenClick }, |
1995 | { "Place", KEYBIND_PLACE_BLOCK, 0,0, TouchScreen_OnscreenClick }, |
1996 | { "Hotbar", 0,0,0, TouchScreen_SwitchClick } |
1997 | }; |
1998 | static const struct TouchButtonDesc normDescs[1] = { |
1999 | { "\x1E", KEYBIND_JUMP, 50, 10, TouchScreen_BindClick } |
2000 | }; |
2001 | static const struct TouchButtonDesc hackDescs[2] = { |
2002 | { "\x1E", KEYBIND_FLY_UP, 50, 70, TouchScreen_BindClick }, |
2003 | { "\x1F", KEYBIND_FLY_DOWN, 50, 10, TouchScreen_BindClick } |
2004 | }; |
2005 | |
2006 | #define TOUCHSCREEN_BTN_COL PackedCol_Make(255, 255, 255, 220)(((cc_uint8)(255) << 0) | ((cc_uint8)(255) << 8) | ((cc_uint8)(255) << 16) | ((cc_uint8)(220) << 24 )) |
2007 | static void TouchScreen_InitButtons(struct TouchScreen* s) { |
2008 | struct HacksComp* hacks = &LocalPlayer_Instance.Hacks; |
2009 | const struct TouchButtonDesc* desc; |
2010 | int i, j; |
2011 | for (i = 0; i < ONSCREEN_MAX_BTNS13 + TOUCH_EXTRA_BTNS; i++) s->widgets[i] = NULL((void*)0); |
2012 | |
2013 | for (i = 0, j = 0; i < ONSCREEN_MAX_BTNS13; i++) { |
2014 | if (!(Gui._onscreenButtons & (1 << i))) continue; |
2015 | desc = &onscreenDescs[i]; |
2016 | |
2017 | ButtonWidget_Init(&s->onscreen[j], 100, desc->OnClick); |
2018 | if (desc->enabled) s->onscreen[j].disabled = !(*desc->enabled); |
2019 | s->onscreenDescs[j] = desc; |
2020 | s->widgets[j] = (struct Widget*)&s->onscreen[j]; |
2021 | j++; |
2022 | } |
2023 | |
2024 | s->numOnscreen = j; |
2025 | if (hacks->Flying || hacks->Noclip) { |
2026 | s->descs = hackDescs; |
2027 | s->numBtns = Array_Elems(hackDescs)(sizeof(hackDescs) / sizeof(hackDescs[0])); |
2028 | } else { |
2029 | s->descs = normDescs; |
2030 | s->numBtns = Array_Elems(normDescs)(sizeof(normDescs) / sizeof(normDescs[0])); |
2031 | } |
2032 | |
2033 | for (i = 0; i < s->numBtns; i++) { |
2034 | s->widgets[i + ONSCREEN_MAX_BTNS13] = (struct Widget*)&s->btns[i]; |
2035 | ButtonWidget_Init(&s->btns[i], 60, s->descs[i].OnClick); |
2036 | s->btns[i].col = TOUCHSCREEN_BTN_COL; |
2037 | } |
2038 | } |
2039 | |
2040 | void TouchScreen_Refresh(void) { |
2041 | struct TouchScreen* s = &TouchScreen; |
2042 | /* InitButtons changes number of widgets, hence */ |
2043 | /* must destroy graphics resources BEFORE that */ |
2044 | Screen_ContextLost(s); |
2045 | TouchScreen_InitButtons(s); |
2046 | Gui_Refresh((struct Screen*)s); |
2047 | } |
2048 | static void TouchScreen_HacksChanged(void* s) { TouchScreen_Refresh(); } |
2049 | |
2050 | static void TouchScreen_ContextLost(void* screen) { |
2051 | struct TouchScreen* s = (struct TouchScreen*)screen; |
2052 | Font_Free(&s->font); |
2053 | Screen_ContextLost(screen); |
2054 | } |
2055 | |
2056 | static void TouchScreen_ContextRecreated(void* screen) { |
2057 | struct TouchScreen* s = (struct TouchScreen*)screen; |
2058 | const struct TouchButtonDesc* desc; |
2059 | int i; |
2060 | Screen_UpdateVb(screen); |
2061 | Gui_MakeTitleFont(&s->font); |
2062 | |
2063 | for (i = 0; i < s->numOnscreen; i++) { |
2064 | desc = s->onscreenDescs[i]; |
2065 | ButtonWidget_SetConst(&s->onscreen[i], desc->text, &s->font); |
2066 | } |
2067 | for (i = 0; i < s->numBtns; i++) { |
2068 | desc = &s->descs[i]; |
2069 | ButtonWidget_SetConst(&s->btns[i], desc->text, &s->font); |
2070 | } |
2071 | ButtonWidget_SetConst(&s->more, "...", &s->font); |
2072 | } |
2073 | |
2074 | static void TouchScreen_Render(void* screen, double delta) { |
2075 | if (Gui.InputGrab) return; |
2076 | Gfx_SetTexturing(true1); |
2077 | Screen_Render2Widgets(screen, delta); |
2078 | Gfx_SetTexturing(false0); |
2079 | } |
2080 | |
2081 | static int TouchScreen_PointerDown(void* screen, int id, int x, int y) { |
2082 | struct TouchScreen* s = (struct TouchScreen*)screen; |
2083 | struct Widget* w; |
2084 | int i; |
2085 | //Chat_Add1("POINTER DOWN: %i", &id); |
2086 | if (Gui.InputGrab) return false0; |
2087 | |
2088 | i = Screen_DoPointerDown(screen, id, x, y); |
2089 | if (i < ONSCREEN_MAX_BTNS13) return i >= 0; |
2090 | |
2091 | /* Clicking on other buttons then */ |
2092 | w = s->widgets[i]; |
2093 | w->active |= id; |
2094 | |
2095 | /* Clicking on jump or fly buttons should still move camera */ |
2096 | for (i = 0; i < s->numBtns; i++) { |
2097 | if (w == (struct Widget*)&s->btns[i]) return TOUCH_TYPE_GUI1 | TOUCH_TYPE_CAMERA2; |
2098 | } |
2099 | return TOUCH_TYPE_GUI1; |
2100 | } |
2101 | |
2102 | static void TouchScreen_PointerUp(void* screen, int id, int x, int y) { |
2103 | struct TouchScreen* s = (struct TouchScreen*)screen; |
2104 | int i; |
2105 | //Chat_Add1("POINTER UP: %i", &id); |
2106 | s->thumbstick.active &= ~id; |
2107 | s->more.active &= ~id; |
2108 | |
2109 | for (i = 0; i < s->numBtns; i++) { |
2110 | if (!(s->btns[i].active & id)) continue; |
2111 | |
2112 | if (s->descs[i].bind < KEYBIND_COUNT) { |
2113 | Input_Set(KeyBinds[s->descs[i].bind], false0); |
2114 | } |
2115 | s->btns[i].active &= ~id; |
2116 | return; |
2117 | } |
2118 | } |
2119 | |
2120 | static void TouchScreen_Layout(void* screen) { |
2121 | struct TouchScreen* s = (struct TouchScreen*)screen; |
2122 | const struct TouchButtonDesc* desc; |
2123 | float scale = Gui.RawTouchScale; |
2124 | int i, height; |
2125 | |
2126 | for (i = 0; i < s->numOnscreen; i++) { |
2127 | Widget_SetLocation(&s->onscreen[i], ANCHOR_MAX, ANCHOR_MIN, 10, 10 + i * 40); |
2128 | } |
2129 | Widget_SetLocation(&s->more, ANCHOR_CENTRE, ANCHOR_MIN, 0, 10); |
2130 | |
2131 | /* Need to align these relative to the hotbar */ |
2132 | HUDScreen_Layout(Gui_HUD); |
2133 | height = Gui_HUD->hotbar.height; |
2134 | |
2135 | for (i = 0; i < s->numBtns; i++) { |
2136 | desc = &s->descs[i]; |
2137 | Widget_SetLocation(&s->btns[i], ANCHOR_MAX, ANCHOR_MAX, desc->x, desc->y); |
2138 | s->btns[i].yOffset += height; |
2139 | |
2140 | /* TODO: Maybe move scaling to be part of button instead */ |
2141 | s->btns[i].minWidth = Display_ScaleX(60 * scale); |
2142 | s->btns[i].minHeight = Display_ScaleY(60 * scale); |
2143 | Widget_Layout(&s->btns[i])(&s->btns[i])->VTABLE->Reposition(&s->btns [i]); |
2144 | } |
2145 | |
2146 | Widget_SetLocation(&s->thumbstick, ANCHOR_MIN, ANCHOR_MAX, 30, 5); |
2147 | s->thumbstick.yOffset += height; |
2148 | s->thumbstick.scale = scale; |
2149 | Widget_Layout(&s->thumbstick)(&s->thumbstick)->VTABLE->Reposition(&s-> thumbstick); |
2150 | } |
2151 | |
2152 | struct LocalPlayerInput touchInput; |
2153 | static void TouchScreen_GetMovement(float* xMoving, float* zMoving) { |
2154 | ThumbstickWidget_GetMovement(&TouchScreen.thumbstick, xMoving, zMoving); |
2155 | } |
2156 | |
2157 | static void TouchScreen_Init(void* screen) { |
2158 | struct TouchScreen* s = (struct TouchScreen*)screen; |
2159 | |
2160 | s->widgets = touch_widgets; |
2161 | s->numWidgets = Array_Elems(touch_widgets)(sizeof(touch_widgets) / sizeof(touch_widgets[0])); |
2162 | s->maxVertices = TOUCH_MAX_VERTICES; |
2163 | Event_Register_(&UserEvents.HacksStateChanged, screen, TouchScreen_HacksChanged)Event_Register((struct Event_Void*)(&UserEvents.HacksStateChanged ), screen, (Event_Void_Callback)(TouchScreen_HacksChanged)); |
2164 | Event_Register_(&UserEvents.HackPermsChanged, screen, TouchScreen_HacksChanged)Event_Register((struct Event_Void*)(&UserEvents.HackPermsChanged ), screen, (Event_Void_Callback)(TouchScreen_HacksChanged)); |
2165 | |
2166 | TouchScreen_InitButtons(s); |
2167 | ButtonWidget_Init(&s->more, 40, TouchScreen_MoreClick); |
2168 | s->more.col = TOUCHSCREEN_BTN_COL; |
2169 | |
2170 | ThumbstickWidget_Init(&s->thumbstick); |
2171 | touchInput.GetMovement = TouchScreen_GetMovement; |
2172 | LocalPlayer_Instance.input.next = &touchInput; |
2173 | } |
2174 | |
2175 | static void TouchScreen_Free(void* s) { |
2176 | Event_Unregister_(&UserEvents.HacksStateChanged, s, TouchScreen_HacksChanged)Event_Unregister((struct Event_Void*)(&UserEvents.HacksStateChanged ), s, (Event_Void_Callback)(TouchScreen_HacksChanged)); |
2177 | Event_Unregister_(&UserEvents.HackPermsChanged, s, TouchScreen_HacksChanged)Event_Unregister((struct Event_Void*)(&UserEvents.HackPermsChanged ), s, (Event_Void_Callback)(TouchScreen_HacksChanged)); |
2178 | } |
2179 | |
2180 | static const struct ScreenVTABLE TouchScreen_VTABLE = { |
2181 | TouchScreen_Init, Screen_NullUpdate, TouchScreen_Free, |
2182 | TouchScreen_Render, Screen_BuildMesh, |
2183 | Screen_FInput, Screen_InputUp, Screen_FKeyPress, Screen_FText, |
2184 | TouchScreen_PointerDown, TouchScreen_PointerUp, Screen_FPointer, Screen_FMouseScroll, |
2185 | TouchScreen_Layout, TouchScreen_ContextLost, TouchScreen_ContextRecreated |
2186 | }; |
2187 | void TouchScreen_Show(void) { |
2188 | struct TouchScreen* s = &TouchScreen; |
2189 | s->VTABLE = &TouchScreen_VTABLE; |
2190 | |
2191 | if (!Input_TouchMode0) return; |
2192 | Gui_Add((struct Screen*)s, GUI_PRIORITY_TOUCH); |
2193 | } |
2194 | #endif |