diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index f857e200c..6bd249aa4 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -186,7 +186,7 @@ namespace ClassicalSharp.Gui.Screens { int x = normalChat.X; int y = normalChat.Y + normalChat.Height - chatHeight; - int width = Math.Max(clientStatus.Width, normalChat.Width); + int width = Math.Max(clientStatus.Width, normalChat.Width); int height = chatHeight + clientStatus.GetUsedHeight(); if (height > 0) { diff --git a/ClassicalSharp/2D/Screens/Menu/ListScreen.cs b/ClassicalSharp/2D/Screens/Menu/ListScreen.cs index 6b4bfe86b..c7f2c5f5f 100644 --- a/ClassicalSharp/2D/Screens/Menu/ListScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/ListScreen.cs @@ -86,7 +86,7 @@ namespace ClassicalSharp.Gui.Screens { } protected void SetCurrentIndex(int index) { - if (index >= entries.Length) index -= items; + if (index >= entries.Length) { index = entries.Length - 1; } if (index < 0) index = 0; currentIndex = index; @@ -130,6 +130,13 @@ namespace ClassicalSharp.Gui.Screens { return HandleMouseDown(widgets, mouseX, mouseY, button) >= 0; } + float wheelAcc; + public override bool HandlesMouseScroll(float delta) { + int steps = Utils.AccumulateWheelDelta(ref wheelAcc, delta); + if (steps != 0) SetCurrentIndex(currentIndex + steps); + return true; + } + public override void OnResize() { RepositionWidgets(widgets); } diff --git a/ClassicalSharp/2D/Widgets/Chat/ChatInputWidget.cs b/ClassicalSharp/2D/Widgets/Chat/ChatInputWidget.cs index 97382021e..c0815cca2 100644 --- a/ClassicalSharp/2D/Widgets/Chat/ChatInputWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/ChatInputWidget.cs @@ -59,7 +59,6 @@ namespace ClassicalSharp.Gui.Widgets { originalText = null; typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1. - game.Chat.Add(null, MessageType.ClientStatus2); game.Chat.Add(null, MessageType.ClientStatus3); base.EnterInput(); } diff --git a/ClassicalSharp/2D/Widgets/InputWidget.cs b/ClassicalSharp/2D/Widgets/InputWidget.cs index 61264af61..628810824 100644 --- a/ClassicalSharp/2D/Widgets/InputWidget.cs +++ b/ClassicalSharp/2D/Widgets/InputWidget.cs @@ -413,8 +413,7 @@ namespace ClassicalSharp.Gui.Widgets { text = game.window.GetClipboardText().Trim(trimChars); } catch (Exception ex) { ErrorHandler.LogError("Paste from clipboard", ex); - const string warning = "&cError while trying to paste from clipboard."; - game.Chat.Add(warning, MessageType.ClientStatus2); + game.Chat.Add("&cError while trying to paste from clipboard."); return true; } @@ -427,8 +426,7 @@ namespace ClassicalSharp.Gui.Widgets { game.window.SetClipboardText(Text.ToString()); } catch (Exception ex) { ErrorHandler.LogError("Copy to clipboard", ex); - const string warning = "&cError while trying to copy to clipboard."; - game.Chat.Add(warning, MessageType.ClientStatus2); + game.Chat.Add("&cError while trying to copy to clipboard."); } return true; } diff --git a/ClassicalSharp/Network/Enums.cs b/ClassicalSharp/Network/Enums.cs index edf39fe5b..f7b64bc83 100644 --- a/ClassicalSharp/Network/Enums.cs +++ b/ClassicalSharp/Network/Enums.cs @@ -67,7 +67,7 @@ namespace ClassicalSharp { // client defined message ids ClientStatus1 = 256, // cuboid messages - ClientStatus2 = 257, // clipboard invalid characters + ClientStatus2 = 257, ClientStatus3 = 258, // tab list matching names } diff --git a/ClassicalSharp/TexturePack/ZipReader.cs b/ClassicalSharp/TexturePack/ZipReader.cs index 562bd5744..5508f2f7e 100644 --- a/ClassicalSharp/TexturePack/ZipReader.cs +++ b/ClassicalSharp/TexturePack/ZipReader.cs @@ -14,10 +14,8 @@ namespace ClassicalSharp.Textures { public string Filename; } - public delegate void ZipEntryProcessor(string filename, byte[] data, ZipEntry entry); - - public delegate bool ZipEntrySelector(string filename); - + public delegate void ZipEntryProcessor(string filename, byte[] data, ZipEntry entry); + public delegate bool ZipEntrySelector(string filename); /// Extracts files from a stream that represents a .zip file. public sealed class ZipReader { diff --git a/src/Client/Input.c b/src/Client/Input.c index 9c41ce717..e69f3e1a5 100644 --- a/src/Client/Input.c +++ b/src/Client/Input.c @@ -139,7 +139,7 @@ bool KeyBind_IsPressed(KeyBind binding) { return Key_States[KeyBind_Keys[binding #define KeyBind_MakeName(name) String_Clear(&name); String_AppendConst(&name, "key-"); String_AppendConst(&name, KeyBind_Names[i]); void KeyBind_Load(void) { - UInt32 i; + Int32 i; UChar nameBuffer[String_BufferSize(STRING_SIZE)]; String name = String_InitAndClearArray(nameBuffer); @@ -152,7 +152,7 @@ void KeyBind_Load(void) { } void KeyBind_Save(void) { - UInt32 i; + Int32 i; UChar nameBuffer[String_BufferSize(STRING_SIZE)]; String name = String_InitAndClearArray(nameBuffer); @@ -169,7 +169,7 @@ void KeyBind_Set(KeyBind binding, Key key) { } void KeyBind_Init(void) { - UInt32 i; + Int32 i; for (i = 0; i < KeyBind_Count; i++) { KeyBind_Keys[i] = KeyBind_Defaults[i]; } @@ -232,8 +232,8 @@ void Hotkeys_AddNewHotkey(Key trigger, UInt8 flags, STRING_PURE String* text, bo } -void Hotkeys_RemoveText(UInt32 index) { - UInt32 i; struct HotkeyData* hKey = HotkeysList; +void Hotkeys_RemoveText(Int32 index) { + Int32 i; struct HotkeyData* hKey = HotkeysList; for (i = 0; i < HotkeysText.Count; i++, hKey++) { if (hKey->TextIndex >= index) hKey->TextIndex--; @@ -242,7 +242,7 @@ void Hotkeys_RemoveText(UInt32 index) { } void Hotkeys_Add(Key trigger, UInt8 flags, STRING_PURE String* text, bool more) { - UInt32 i; struct HotkeyData* hKey = HotkeysList; + Int32 i; struct HotkeyData* hKey = HotkeysList; for (i = 0; i < HotkeysText.Count; i++, hKey++) { if (hKey->Trigger != trigger || hKey->Flags != flags) continue; @@ -257,7 +257,7 @@ void Hotkeys_Add(Key trigger, UInt8 flags, STRING_PURE String* text, bool more) } bool Hotkeys_Remove(Key trigger, UInt8 flags) { - UInt32 i, j; struct HotkeyData* hKey = HotkeysList; + Int32 i, j; struct HotkeyData* hKey = HotkeysList; for (i = 0; i < HotkeysText.Count; i++, hKey++) { if (hKey->Trigger != trigger || hKey->Flags != flags) continue; @@ -280,7 +280,7 @@ bool Hotkeys_IsHotkey(Key key, STRING_TRANSIENT String* text, bool* moreInput) { String_Clear(text); *moreInput = false; - UInt32 i; + Int32 i; for (i = 0; i < HotkeysText.Count; i++) { struct HotkeyData hKey = HotkeysList[i]; if ((hKey.Flags & flags) == hKey.Flags && hKey.Trigger == key) { @@ -296,7 +296,8 @@ bool Hotkeys_IsHotkey(Key key, STRING_TRANSIENT String* text, bool* moreInput) { void Hotkeys_Init(void) { StringsBuffer_Init(&HotkeysText); String prefix = String_FromConst("hotkey-"); - UInt32 i; + Int32 i; + for (i = 0; i < Options_Keys.Count; i++) { String key = StringsBuffer_UNSAFE_Get(&Options_Keys, i); if (!String_CaselessStarts(&key, &prefix)) continue; diff --git a/src/Client/Input.h b/src/Client/Input.h index 8c4e2ae46..56432caad 100644 --- a/src/Client/Input.h +++ b/src/Client/Input.h @@ -119,10 +119,10 @@ void KeyBind_Init(void); extern UInt8 Hotkeys_LWJGL[256]; struct HotkeyData { - UInt32 TextIndex; /* contents to copy directly into the input bar */ - UInt8 Trigger; /* Member of Key enumeration */ - UInt8 Flags; /* ctrl 1, shift 2, alt 4 */ - bool StaysOpen; /* whether the user is able to enter further input */ + Int32 TextIndex; /* contents to copy directly into the input bar */ + UInt8 Trigger; /* Member of Key enumeration */ + UInt8 Flags; /* ctrl 1, shift 2, alt 4 */ + bool StaysOpen; /* whether the user is able to enter further input */ }; #define HOTKEYS_MAX_COUNT 256 diff --git a/src/Client/Menus.c b/src/Client/Menus.c index bb379bd2d..7ebfe56ab 100644 --- a/src/Client/Menus.c +++ b/src/Client/Menus.c @@ -35,6 +35,7 @@ struct ListScreen { Screen_Layout struct FontDesc Font; + Real32 WheelAcc; Int32 CurrentIndex; Widget_LeftClick EntryClick; String TitleText; @@ -300,8 +301,9 @@ static void Menu_SwitchHotkeys(struct GuiElem* a, struct GuiElem* b) { struct GuiElementVTABLE ListScreen_VTABLE; struct ListScreen ListScreen_Instance; #define LIST_SCREEN_EMPTY "-----" -STRING_REF String ListScreen_UNSAFE_Get(struct ListScreen* screen, UInt32 index) { - if (index < screen->Entries.Count) { + +STRING_REF String ListScreen_UNSAFE_Get(struct ListScreen* screen, Int32 index) { + if (index >= 0 && index < screen->Entries.Count) { return StringsBuffer_UNSAFE_Get(&screen->Entries, index); } else { String str = String_FromConst(LIST_SCREEN_EMPTY); return str; @@ -332,7 +334,7 @@ static void ListScreen_UpdateArrows(struct ListScreen* screen) { } static void ListScreen_SetCurrentIndex(struct ListScreen* screen, Int32 index) { - if (index >= screen->Entries.Count) index -= LIST_SCREEN_ITEMS; + if (index >= screen->Entries.Count) { index = screen->Entries.Count - 1; } if (index < 0) index = 0; Int32 i; @@ -410,6 +412,7 @@ static String ListScreen_UNSAFE_GetCur(struct ListScreen* screen, struct GuiElem static void ListScreen_Init(struct GuiElem* elem) { struct ListScreen* screen = (struct ListScreen*)elem; Platform_FontMake(&screen->Font, &Game_FontName, 16, FONT_STYLE_BOLD); + screen->WheelAcc = 0.0f; ListScreen_ContextRecreated(screen); Event_RegisterVoid(&GfxEvents_ContextLost, screen, ListScreen_ContextLost); Event_RegisterVoid(&GfxEvents_ContextRecreated, screen, ListScreen_ContextRecreated); @@ -455,6 +458,13 @@ static bool ListScreen_HandlesMouseDown(struct GuiElem* elem, Int32 x, Int32 y, return Menu_HandleMouseDown(elem, screen->Widgets, Array_Elems(screen->Widgets), x, y, btn) >= 0; } +static bool ListScreen_HandlesMouseScroll(struct GuiElem* elem, Real32 delta) { + struct ListScreen* screen = (struct ListScreen*)elem; + Int32 steps = Utils_AccumulateWheelDelta(&screen->WheelAcc, delta); + if (steps) ListScreen_SetCurrentIndex(screen, screen->CurrentIndex + steps); + return true; +} + static void ListScreen_OnResize(struct GuiElem* elem) { struct ListScreen* screen = (struct ListScreen*)elem; Menu_RepositionWidgets(screen->Widgets, Array_Elems(screen->Widgets)); @@ -474,6 +484,7 @@ struct ListScreen* ListScreen_MakeInstance(void) { screen->VTABLE->HandlesKeyDown = ListScreen_HandlesKeyDown; screen->VTABLE->HandlesMouseDown = ListScreen_HandlesMouseDown; screen->VTABLE->HandlesMouseMove = ListScreen_HandlesMouseMove; + screen->VTABLE->HandlesMouseScroll = ListScreen_HandlesMouseScroll; screen->OnResize = ListScreen_OnResize; screen->VTABLE->Init = ListScreen_Init; diff --git a/src/Client/PacketHandlers.c b/src/Client/PacketHandlers.c index 23ee393fd..395cc8dd4 100644 --- a/src/Client/PacketHandlers.c +++ b/src/Client/PacketHandlers.c @@ -246,7 +246,7 @@ static bool WoM_ReadLine(STRING_REF String* page, Int32* start, STRING_TRANSIENT return true; } -static void Wom_ParseConfig(STRING_PURE String* page) { +static void WoM_ParseConfig(STRING_PURE String* page) { String line; Int32 start = 0; @@ -292,7 +292,7 @@ static void WoM_Tick(void) { if (item.ResultData) { String str = String_Init(item.ResultData, item.ResultSize, item.ResultSize); - Wom_ParseConfig(&str); + WoM_ParseConfig(&str); } ASyncRequest_Free(&item); } @@ -340,9 +340,9 @@ void Classic_WritePosition(struct Stream* stream, Vector3 pos, Real32 rotY, Real void Classic_WriteSetBlock(struct Stream* stream, Int32 x, Int32 y, Int32 z, bool place, BlockID block) { Stream_WriteU8(stream, OPCODE_SET_BLOCK_CLIENT); - Stream_WriteI16_BE(stream, x); - Stream_WriteI16_BE(stream, y); - Stream_WriteI16_BE(stream, z); + Stream_WriteU16_BE(stream, x); + Stream_WriteU16_BE(stream, y); + Stream_WriteU16_BE(stream, z); Stream_WriteU8(stream, place ? 1 : 0); Handlers_WriteBlock(stream, block); } diff --git a/src/Client/Stream.h b/src/Client/Stream.h index ab3575b5a..4b5235ff8 100644 --- a/src/Client/Stream.h +++ b/src/Client/Stream.h @@ -7,10 +7,7 @@ Copyright 2017 ClassicalSharp | Licensed under BSD-3 */ -enum STREAM_SEEKFROM { - STREAM_SEEKFROM_BEGIN, STREAM_SEEKFROM_CURRENT, STREAM_SEEKFROM_END, -}; - +enum STREAM_SEEKFROM { STREAM_SEEKFROM_BEGIN, STREAM_SEEKFROM_CURRENT, STREAM_SEEKFROM_END }; struct Stream; /* Represents a stream that can be written to and/or read from. */ struct Stream { diff --git a/src/Client/String.c b/src/Client/String.c index 6045e7f73..4a2908cd2 100644 --- a/src/Client/String.c +++ b/src/Client/String.c @@ -693,14 +693,14 @@ void StringsBuffer_Free(StringsBuffer* buffer) { StringsBuffer_Init(buffer); } -void StringsBuffer_Get(StringsBuffer* buffer, UInt32 index, STRING_TRANSIENT String* text) { +void StringsBuffer_Get(StringsBuffer* buffer, Int32 index, STRING_TRANSIENT String* text) { String raw = StringsBuffer_UNSAFE_Get(buffer, index); String_Clear(text); String_AppendString(text, &raw); } -String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, UInt32 index) { - if (index >= buffer->Count) ErrorHandler_Fail("Tried to get String past StringsBuffer end"); +String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, Int32 index) { + if (index < 0 || index >= buffer->Count) ErrorHandler_Fail("Tried to get String past StringsBuffer end"); UInt32 flags = buffer->FlagsBuffer[index]; UInt32 offset = flags >> STRINGSBUFFER_LEN_SHIFT; @@ -741,7 +741,7 @@ void StringsBuffer_Add(StringsBuffer* buffer, STRING_PURE String* text) { ErrorHandler_Fail("String too big to insert into StringsBuffer"); } - UInt32 textOffset = buffer->UsedElems; + Int32 textOffset = buffer->UsedElems; if (textOffset + text->length >= buffer->TextBufferElems) { StringsBuffer_Resize(&buffer->TextBuffer, &buffer->TextBufferElems, sizeof(UChar), STRINGSBUFFER_BUFFER_DEF_SIZE, STRINGSBUFFER_BUFFER_EXPAND_SIZE); @@ -756,8 +756,8 @@ void StringsBuffer_Add(StringsBuffer* buffer, STRING_PURE String* text) { buffer->UsedElems += text->length; } -void StringsBuffer_Remove(StringsBuffer* buffer, UInt32 index) { - if (index >= buffer->Count) ErrorHandler_Fail("Tried to remove String past StringsBuffer end"); +void StringsBuffer_Remove(StringsBuffer* buffer, Int32 index) { + if (index < 0 || index >= buffer->Count) ErrorHandler_Fail("Tried to remove String past StringsBuffer end"); UInt32 flags = buffer->FlagsBuffer[index]; UInt32 offset = flags >> STRINGSBUFFER_LEN_SHIFT; diff --git a/src/Client/String.h b/src/Client/String.h index 3f8e7e88d..c117bcfc1 100644 --- a/src/Client/String.h +++ b/src/Client/String.h @@ -111,19 +111,19 @@ bool Convert_TryParseBool(STRING_PURE String* str, bool* value); typedef struct StringsBuffer_ { UChar* TextBuffer; UInt32* FlagsBuffer; - UInt32 TextBufferElems, FlagsBufferElems; - UInt32 Count, UsedElems; + Int32 TextBufferElems, FlagsBufferElems; + Int32 Count, UsedElems; UChar DefaultBuffer[STRINGSBUFFER_BUFFER_DEF_SIZE]; UInt32 DefaultFlags[STRINGSBUFFER_FLAGS_DEF_ELEMS]; } StringsBuffer; void StringsBuffer_Init(StringsBuffer* buffer); void StringsBuffer_Free(StringsBuffer* buffer); -void StringsBuffer_Get(StringsBuffer* buffer, UInt32 index, STRING_TRANSIENT String* text); -STRING_REF String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, UInt32 index); +void StringsBuffer_Get(StringsBuffer* buffer, Int32 index, STRING_TRANSIENT String* text); +STRING_REF String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, Int32 index); void StringsBuffer_Resize(void** buffer, UInt32* elems, UInt32 elemSize, UInt32 defElems, UInt32 expandElems); void StringsBuffer_Add(StringsBuffer* buffer, STRING_PURE String* text); -void StringsBuffer_Remove(StringsBuffer* buffer, UInt32 index); +void StringsBuffer_Remove(StringsBuffer* buffer, Int32 index); void WordWrap_Do(STRING_REF String* text, STRING_TRANSIENT String* lines, Int32 numLines, Int32 lineLen); void WordWrap_GetCoords(Int32 index, STRING_PURE String* lines, Int32 numLines, Int32* coordX, Int32* coordY);