Allow using mouse wheel to scroll through hotkeys/texture packs/levels list

This commit is contained in:
UnknownShadow200 2018-07-23 02:32:04 +10:00
parent 6a88fe13d4
commit 90f15bc994
13 changed files with 59 additions and 48 deletions

View File

@ -86,7 +86,7 @@ namespace ClassicalSharp.Gui.Screens {
} }
protected void SetCurrentIndex(int index) { protected void SetCurrentIndex(int index) {
if (index >= entries.Length) index -= items; if (index >= entries.Length) { index = entries.Length - 1; }
if (index < 0) index = 0; if (index < 0) index = 0;
currentIndex = index; currentIndex = index;
@ -130,6 +130,13 @@ namespace ClassicalSharp.Gui.Screens {
return HandleMouseDown(widgets, mouseX, mouseY, button) >= 0; 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() { public override void OnResize() {
RepositionWidgets(widgets); RepositionWidgets(widgets);
} }

View File

@ -59,7 +59,6 @@ namespace ClassicalSharp.Gui.Widgets {
originalText = null; originalText = null;
typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1. typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.
game.Chat.Add(null, MessageType.ClientStatus2);
game.Chat.Add(null, MessageType.ClientStatus3); game.Chat.Add(null, MessageType.ClientStatus3);
base.EnterInput(); base.EnterInput();
} }

View File

@ -413,8 +413,7 @@ namespace ClassicalSharp.Gui.Widgets {
text = game.window.GetClipboardText().Trim(trimChars); text = game.window.GetClipboardText().Trim(trimChars);
} catch (Exception ex) { } catch (Exception ex) {
ErrorHandler.LogError("Paste from clipboard", ex); ErrorHandler.LogError("Paste from clipboard", ex);
const string warning = "&cError while trying to paste from clipboard."; game.Chat.Add("&cError while trying to paste from clipboard.");
game.Chat.Add(warning, MessageType.ClientStatus2);
return true; return true;
} }
@ -427,8 +426,7 @@ namespace ClassicalSharp.Gui.Widgets {
game.window.SetClipboardText(Text.ToString()); game.window.SetClipboardText(Text.ToString());
} catch (Exception ex) { } catch (Exception ex) {
ErrorHandler.LogError("Copy to clipboard", ex); ErrorHandler.LogError("Copy to clipboard", ex);
const string warning = "&cError while trying to copy to clipboard."; game.Chat.Add("&cError while trying to copy to clipboard.");
game.Chat.Add(warning, MessageType.ClientStatus2);
} }
return true; return true;
} }

View File

@ -67,7 +67,7 @@ namespace ClassicalSharp {
// client defined message ids // client defined message ids
ClientStatus1 = 256, // cuboid messages ClientStatus1 = 256, // cuboid messages
ClientStatus2 = 257, // clipboard invalid characters ClientStatus2 = 257,
ClientStatus3 = 258, // tab list matching names ClientStatus3 = 258, // tab list matching names
} }

View File

@ -15,10 +15,8 @@ namespace ClassicalSharp.Textures {
} }
public delegate void ZipEntryProcessor(string filename, byte[] data, ZipEntry entry); public delegate void ZipEntryProcessor(string filename, byte[] data, ZipEntry entry);
public delegate bool ZipEntrySelector(string filename); public delegate bool ZipEntrySelector(string filename);
/// <summary> Extracts files from a stream that represents a .zip file. </summary> /// <summary> Extracts files from a stream that represents a .zip file. </summary>
public sealed class ZipReader { public sealed class ZipReader {

View File

@ -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]); #define KeyBind_MakeName(name) String_Clear(&name); String_AppendConst(&name, "key-"); String_AppendConst(&name, KeyBind_Names[i]);
void KeyBind_Load(void) { void KeyBind_Load(void) {
UInt32 i; Int32 i;
UChar nameBuffer[String_BufferSize(STRING_SIZE)]; UChar nameBuffer[String_BufferSize(STRING_SIZE)];
String name = String_InitAndClearArray(nameBuffer); String name = String_InitAndClearArray(nameBuffer);
@ -152,7 +152,7 @@ void KeyBind_Load(void) {
} }
void KeyBind_Save(void) { void KeyBind_Save(void) {
UInt32 i; Int32 i;
UChar nameBuffer[String_BufferSize(STRING_SIZE)]; UChar nameBuffer[String_BufferSize(STRING_SIZE)];
String name = String_InitAndClearArray(nameBuffer); String name = String_InitAndClearArray(nameBuffer);
@ -169,7 +169,7 @@ void KeyBind_Set(KeyBind binding, Key key) {
} }
void KeyBind_Init(void) { void KeyBind_Init(void) {
UInt32 i; Int32 i;
for (i = 0; i < KeyBind_Count; i++) { for (i = 0; i < KeyBind_Count; i++) {
KeyBind_Keys[i] = KeyBind_Defaults[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) { void Hotkeys_RemoveText(Int32 index) {
UInt32 i; struct HotkeyData* hKey = HotkeysList; Int32 i; struct HotkeyData* hKey = HotkeysList;
for (i = 0; i < HotkeysText.Count; i++, hKey++) { for (i = 0; i < HotkeysText.Count; i++, hKey++) {
if (hKey->TextIndex >= index) hKey->TextIndex--; 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) { 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++) { for (i = 0; i < HotkeysText.Count; i++, hKey++) {
if (hKey->Trigger != trigger || hKey->Flags != flags) continue; 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) { 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++) { for (i = 0; i < HotkeysText.Count; i++, hKey++) {
if (hKey->Trigger != trigger || hKey->Flags != flags) continue; 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); String_Clear(text);
*moreInput = false; *moreInput = false;
UInt32 i; Int32 i;
for (i = 0; i < HotkeysText.Count; i++) { for (i = 0; i < HotkeysText.Count; i++) {
struct HotkeyData hKey = HotkeysList[i]; struct HotkeyData hKey = HotkeysList[i];
if ((hKey.Flags & flags) == hKey.Flags && hKey.Trigger == key) { 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) { void Hotkeys_Init(void) {
StringsBuffer_Init(&HotkeysText); StringsBuffer_Init(&HotkeysText);
String prefix = String_FromConst("hotkey-"); String prefix = String_FromConst("hotkey-");
UInt32 i; Int32 i;
for (i = 0; i < Options_Keys.Count; i++) { for (i = 0; i < Options_Keys.Count; i++) {
String key = StringsBuffer_UNSAFE_Get(&Options_Keys, i); String key = StringsBuffer_UNSAFE_Get(&Options_Keys, i);
if (!String_CaselessStarts(&key, &prefix)) continue; if (!String_CaselessStarts(&key, &prefix)) continue;

View File

@ -119,10 +119,10 @@ void KeyBind_Init(void);
extern UInt8 Hotkeys_LWJGL[256]; extern UInt8 Hotkeys_LWJGL[256];
struct HotkeyData { struct HotkeyData {
UInt32 TextIndex; /* contents to copy directly into the input bar */ Int32 TextIndex; /* contents to copy directly into the input bar */
UInt8 Trigger; /* Member of Key enumeration */ UInt8 Trigger; /* Member of Key enumeration */
UInt8 Flags; /* ctrl 1, shift 2, alt 4 */ UInt8 Flags; /* ctrl 1, shift 2, alt 4 */
bool StaysOpen; /* whether the user is able to enter further input */ bool StaysOpen; /* whether the user is able to enter further input */
}; };
#define HOTKEYS_MAX_COUNT 256 #define HOTKEYS_MAX_COUNT 256

View File

@ -35,6 +35,7 @@
struct ListScreen { struct ListScreen {
Screen_Layout Screen_Layout
struct FontDesc Font; struct FontDesc Font;
Real32 WheelAcc;
Int32 CurrentIndex; Int32 CurrentIndex;
Widget_LeftClick EntryClick; Widget_LeftClick EntryClick;
String TitleText; String TitleText;
@ -300,8 +301,9 @@ static void Menu_SwitchHotkeys(struct GuiElem* a, struct GuiElem* b) {
struct GuiElementVTABLE ListScreen_VTABLE; struct GuiElementVTABLE ListScreen_VTABLE;
struct ListScreen ListScreen_Instance; struct ListScreen ListScreen_Instance;
#define LIST_SCREEN_EMPTY "-----" #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); return StringsBuffer_UNSAFE_Get(&screen->Entries, index);
} else { } else {
String str = String_FromConst(LIST_SCREEN_EMPTY); return str; 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) { 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; if (index < 0) index = 0;
Int32 i; Int32 i;
@ -410,6 +412,7 @@ static String ListScreen_UNSAFE_GetCur(struct ListScreen* screen, struct GuiElem
static void ListScreen_Init(struct GuiElem* elem) { static void ListScreen_Init(struct GuiElem* elem) {
struct ListScreen* screen = (struct ListScreen*)elem; struct ListScreen* screen = (struct ListScreen*)elem;
Platform_FontMake(&screen->Font, &Game_FontName, 16, FONT_STYLE_BOLD); Platform_FontMake(&screen->Font, &Game_FontName, 16, FONT_STYLE_BOLD);
screen->WheelAcc = 0.0f;
ListScreen_ContextRecreated(screen); ListScreen_ContextRecreated(screen);
Event_RegisterVoid(&GfxEvents_ContextLost, screen, ListScreen_ContextLost); Event_RegisterVoid(&GfxEvents_ContextLost, screen, ListScreen_ContextLost);
Event_RegisterVoid(&GfxEvents_ContextRecreated, screen, ListScreen_ContextRecreated); 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; 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) { static void ListScreen_OnResize(struct GuiElem* elem) {
struct ListScreen* screen = (struct ListScreen*)elem; struct ListScreen* screen = (struct ListScreen*)elem;
Menu_RepositionWidgets(screen->Widgets, Array_Elems(screen->Widgets)); Menu_RepositionWidgets(screen->Widgets, Array_Elems(screen->Widgets));
@ -474,6 +484,7 @@ struct ListScreen* ListScreen_MakeInstance(void) {
screen->VTABLE->HandlesKeyDown = ListScreen_HandlesKeyDown; screen->VTABLE->HandlesKeyDown = ListScreen_HandlesKeyDown;
screen->VTABLE->HandlesMouseDown = ListScreen_HandlesMouseDown; screen->VTABLE->HandlesMouseDown = ListScreen_HandlesMouseDown;
screen->VTABLE->HandlesMouseMove = ListScreen_HandlesMouseMove; screen->VTABLE->HandlesMouseMove = ListScreen_HandlesMouseMove;
screen->VTABLE->HandlesMouseScroll = ListScreen_HandlesMouseScroll;
screen->OnResize = ListScreen_OnResize; screen->OnResize = ListScreen_OnResize;
screen->VTABLE->Init = ListScreen_Init; screen->VTABLE->Init = ListScreen_Init;

View File

@ -246,7 +246,7 @@ static bool WoM_ReadLine(STRING_REF String* page, Int32* start, STRING_TRANSIENT
return true; return true;
} }
static void Wom_ParseConfig(STRING_PURE String* page) { static void WoM_ParseConfig(STRING_PURE String* page) {
String line; String line;
Int32 start = 0; Int32 start = 0;
@ -292,7 +292,7 @@ static void WoM_Tick(void) {
if (item.ResultData) { if (item.ResultData) {
String str = String_Init(item.ResultData, item.ResultSize, item.ResultSize); String str = String_Init(item.ResultData, item.ResultSize, item.ResultSize);
Wom_ParseConfig(&str); WoM_ParseConfig(&str);
} }
ASyncRequest_Free(&item); 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) { void Classic_WriteSetBlock(struct Stream* stream, Int32 x, Int32 y, Int32 z, bool place, BlockID block) {
Stream_WriteU8(stream, OPCODE_SET_BLOCK_CLIENT); Stream_WriteU8(stream, OPCODE_SET_BLOCK_CLIENT);
Stream_WriteI16_BE(stream, x); Stream_WriteU16_BE(stream, x);
Stream_WriteI16_BE(stream, y); Stream_WriteU16_BE(stream, y);
Stream_WriteI16_BE(stream, z); Stream_WriteU16_BE(stream, z);
Stream_WriteU8(stream, place ? 1 : 0); Stream_WriteU8(stream, place ? 1 : 0);
Handlers_WriteBlock(stream, block); Handlers_WriteBlock(stream, block);
} }

View File

@ -7,10 +7,7 @@
Copyright 2017 ClassicalSharp | Licensed under BSD-3 Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/ */
enum STREAM_SEEKFROM { enum STREAM_SEEKFROM { STREAM_SEEKFROM_BEGIN, STREAM_SEEKFROM_CURRENT, STREAM_SEEKFROM_END };
STREAM_SEEKFROM_BEGIN, STREAM_SEEKFROM_CURRENT, STREAM_SEEKFROM_END,
};
struct Stream; struct Stream;
/* Represents a stream that can be written to and/or read from. */ /* Represents a stream that can be written to and/or read from. */
struct Stream { struct Stream {

View File

@ -693,14 +693,14 @@ void StringsBuffer_Free(StringsBuffer* buffer) {
StringsBuffer_Init(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 raw = StringsBuffer_UNSAFE_Get(buffer, index);
String_Clear(text); String_Clear(text);
String_AppendString(text, &raw); String_AppendString(text, &raw);
} }
String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, UInt32 index) { String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, Int32 index) {
if (index >= buffer->Count) ErrorHandler_Fail("Tried to get String past StringsBuffer end"); if (index < 0 || index >= buffer->Count) ErrorHandler_Fail("Tried to get String past StringsBuffer end");
UInt32 flags = buffer->FlagsBuffer[index]; UInt32 flags = buffer->FlagsBuffer[index];
UInt32 offset = flags >> STRINGSBUFFER_LEN_SHIFT; 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"); ErrorHandler_Fail("String too big to insert into StringsBuffer");
} }
UInt32 textOffset = buffer->UsedElems; Int32 textOffset = buffer->UsedElems;
if (textOffset + text->length >= buffer->TextBufferElems) { if (textOffset + text->length >= buffer->TextBufferElems) {
StringsBuffer_Resize(&buffer->TextBuffer, &buffer->TextBufferElems, sizeof(UChar), StringsBuffer_Resize(&buffer->TextBuffer, &buffer->TextBufferElems, sizeof(UChar),
STRINGSBUFFER_BUFFER_DEF_SIZE, STRINGSBUFFER_BUFFER_EXPAND_SIZE); 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; buffer->UsedElems += text->length;
} }
void StringsBuffer_Remove(StringsBuffer* buffer, UInt32 index) { void StringsBuffer_Remove(StringsBuffer* buffer, Int32 index) {
if (index >= buffer->Count) ErrorHandler_Fail("Tried to remove String past StringsBuffer end"); if (index < 0 || index >= buffer->Count) ErrorHandler_Fail("Tried to remove String past StringsBuffer end");
UInt32 flags = buffer->FlagsBuffer[index]; UInt32 flags = buffer->FlagsBuffer[index];
UInt32 offset = flags >> STRINGSBUFFER_LEN_SHIFT; UInt32 offset = flags >> STRINGSBUFFER_LEN_SHIFT;

View File

@ -111,19 +111,19 @@ bool Convert_TryParseBool(STRING_PURE String* str, bool* value);
typedef struct StringsBuffer_ { typedef struct StringsBuffer_ {
UChar* TextBuffer; UChar* TextBuffer;
UInt32* FlagsBuffer; UInt32* FlagsBuffer;
UInt32 TextBufferElems, FlagsBufferElems; Int32 TextBufferElems, FlagsBufferElems;
UInt32 Count, UsedElems; Int32 Count, UsedElems;
UChar DefaultBuffer[STRINGSBUFFER_BUFFER_DEF_SIZE]; UChar DefaultBuffer[STRINGSBUFFER_BUFFER_DEF_SIZE];
UInt32 DefaultFlags[STRINGSBUFFER_FLAGS_DEF_ELEMS]; UInt32 DefaultFlags[STRINGSBUFFER_FLAGS_DEF_ELEMS];
} StringsBuffer; } StringsBuffer;
void StringsBuffer_Init(StringsBuffer* buffer); void StringsBuffer_Init(StringsBuffer* buffer);
void StringsBuffer_Free(StringsBuffer* buffer); void StringsBuffer_Free(StringsBuffer* buffer);
void StringsBuffer_Get(StringsBuffer* buffer, UInt32 index, STRING_TRANSIENT String* text); void StringsBuffer_Get(StringsBuffer* buffer, Int32 index, STRING_TRANSIENT String* text);
STRING_REF String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, UInt32 index); 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_Resize(void** buffer, UInt32* elems, UInt32 elemSize, UInt32 defElems, UInt32 expandElems);
void StringsBuffer_Add(StringsBuffer* buffer, STRING_PURE String* text); 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_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); void WordWrap_GetCoords(Int32 index, STRING_PURE String* lines, Int32 numLines, Int32* coordX, Int32* coordY);