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) {
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);
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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
}

View File

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

View File

@ -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

View File

@ -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;

View File

@ -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);
}

View File

@ -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 {

View File

@ -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;

View File

@ -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);