diff --git a/src/Chat.c b/src/Chat.c index 82babfae1..294054962 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -414,7 +414,7 @@ static struct ChatCommand ModelCommand = { *#########################################################################################################################*/ static int cuboid_block = -1; static Vector3I cuboid_mark1, cuboid_mark2; -static bool cuboid_persist, cuboid_hooked; +static bool cuboid_persist, cuboid_hooked, cuboid_hasMark1; const static String cuboid_msg = String_FromConst("&eCuboid: &fPlace or delete a block."); static bool CuboidCommand_ParseBlock(const String* args, int argsCount) { @@ -461,8 +461,9 @@ static void CuboidCommand_BlockChanged(void* obj, Vector3I coords, BlockID old, String msg; char msgBuffer[STRING_SIZE]; String_InitArray(msg, msgBuffer); - if (cuboid_mark1.X == Int32_MaxValue) { - cuboid_mark1 = coords; + if (!cuboid_hasMark1) { + cuboid_mark1 = coords; + cuboid_hasMark1 = true; Game_UpdateBlock(coords.X, coords.Y, coords.Z, old); String_Format3(&msg, "&eCuboid: &fMark 1 placed at (%i, %i, %i), place mark 2.", &coords.X, &coords.Y, &coords.Z); @@ -476,7 +477,7 @@ static void CuboidCommand_BlockChanged(void* obj, Vector3I coords, BlockID old, cuboid_hooked = false; Chat_AddOf(&String_Empty, MSG_TYPE_CLIENTSTATUS_1); } else { - cuboid_mark1 = Vector3I_MaxValue(); + cuboid_hasMark1 = false; Chat_AddOf(&cuboid_msg, MSG_TYPE_CLIENTSTATUS_1); } } @@ -488,10 +489,9 @@ static void CuboidCommand_Execute(const String* args, int argsCount) { cuboid_hooked = false; } - cuboid_block = -1; - cuboid_mark1 = Vector3I_MaxValue(); - cuboid_mark2 = Vector3I_MaxValue(); - cuboid_persist = false; + cuboid_block = -1; + cuboid_hasMark1 = false; + cuboid_persist = false; if (!CuboidCommand_ParseBlock(args, argsCount)) return; if (argsCount > 1 && String_CaselessEqualsConst(&args[0], "yes")) { diff --git a/src/HeldBlockRenderer.c b/src/HeldBlockRenderer.c index 0bde8e622..34b59a771 100644 --- a/src/HeldBlockRenderer.c +++ b/src/HeldBlockRenderer.c @@ -229,7 +229,7 @@ void HeldBlockRenderer_Render(double delta) { Gfx_LoadMatrix(MATRIX_PROJECTION, &Gfx.Projection); } -struct EntityVTABLE heldEntity_VTABLE = { +static struct EntityVTABLE heldEntity_VTABLE = { NULL, NULL, NULL, HeldBlockRenderer_GetCol, NULL, NULL, NULL, NULL, }; diff --git a/src/Inventory.c b/src/Inventory.c index 052ca5b66..78872790d 100644 --- a/src/Inventory.c +++ b/src/Inventory.c @@ -43,14 +43,14 @@ void Inventory_SetSelectedBlock(BlockID block) { Event_RaiseVoid(&UserEvents.HeldBlockChanged); } -const static uint8_t inv_classicTable[42] = { +const static uint8_t classicInventory[42] = { BLOCK_STONE, BLOCK_COBBLE, BLOCK_BRICK, BLOCK_DIRT, BLOCK_WOOD, BLOCK_LOG, BLOCK_LEAVES, BLOCK_GLASS, BLOCK_SLAB, BLOCK_MOSSY_ROCKS, BLOCK_SAPLING, BLOCK_DANDELION, BLOCK_ROSE, BLOCK_BROWN_SHROOM, BLOCK_RED_SHROOM, BLOCK_SAND, BLOCK_GRAVEL, BLOCK_SPONGE, BLOCK_RED, BLOCK_ORANGE, BLOCK_YELLOW, BLOCK_LIME, BLOCK_GREEN, BLOCK_TEAL, BLOCK_AQUA, BLOCK_CYAN, BLOCK_BLUE, BLOCK_INDIGO, BLOCK_VIOLET, BLOCK_MAGENTA, BLOCK_PINK, BLOCK_BLACK, BLOCK_GRAY, BLOCK_WHITE, BLOCK_COAL_ORE, BLOCK_IRON_ORE, BLOCK_GOLD_ORE, BLOCK_IRON, BLOCK_GOLD, BLOCK_BOOKSHELF, BLOCK_TNT, BLOCK_OBSIDIAN, }; -const static uint8_t inv_classicHacksTable[49] = { +const static uint8_t classicHacksInventory[49] = { BLOCK_STONE, BLOCK_GRASS, BLOCK_COBBLE, BLOCK_BRICK, BLOCK_DIRT, BLOCK_WOOD, BLOCK_BEDROCK, BLOCK_WATER, BLOCK_STILL_WATER, BLOCK_LAVA, BLOCK_STILL_LAVA, BLOCK_LOG, BLOCK_LEAVES, BLOCK_GLASS, BLOCK_SLAB, BLOCK_MOSSY_ROCKS, BLOCK_SAPLING, BLOCK_DANDELION, BLOCK_ROSE, BLOCK_BROWN_SHROOM, BLOCK_RED_SHROOM, BLOCK_SAND, BLOCK_GRAVEL, BLOCK_SPONGE, BLOCK_RED, BLOCK_ORANGE, BLOCK_YELLOW, BLOCK_LIME, BLOCK_GREEN, BLOCK_TEAL, @@ -60,9 +60,9 @@ const static uint8_t inv_classicHacksTable[49] = { static BlockID Inventory_DefaultMapping(int slot) { if (Game_PureClassic) { - if (slot < 9 * 4 + 6) return inv_classicTable[slot]; + if (slot < 9 * 4 + 6) return classicInventory[slot]; } else if (Game_ClassicMode) { - if (slot < 10 * 4 + 9) return inv_classicHacksTable[slot]; + if (slot < 10 * 4 + 9) return classicHacksInventory[slot]; } else if (slot < BLOCK_MAX_CPE) { return (BlockID)(slot + 1); } diff --git a/src/Logger.c b/src/Logger.c index bddf25bcf..b4fd735e7 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -508,7 +508,7 @@ static void Logger_DumpRegisters(void* ctx) { #if defined CC_BUILD_LINUX || defined CC_BUILD_SOLARIS static void Logger_DumpMisc(void* ctx) { const static String memMap = String_FromConst("-- memory map --\n"); - String str; char strBuffer[STRING_SIZE * 5]; + String str; char strBuffer[320]; int n, fd; Logger_Log(&memMap); @@ -541,7 +541,7 @@ void Logger_Abort2(ReturnCode result, const char* raw_msg) { } #elif defined CC_BUILD_WIN static LONG WINAPI Logger_UnhandledFilter(struct _EXCEPTION_POINTERS* pInfo) { - String msg; char msgBuffer[STRING_SIZE * 2 + 1]; + String msg; char msgBuffer[128 + 1]; uint32_t code; uintptr_t addr; @@ -582,8 +582,6 @@ void Logger_Abort2(ReturnCode result, const char* raw_msg) { mov [ctx.ContextFlags], CONTEXT_CONTROL } #else - int32_t _ebp, _eip, _esp; - /* TODO: I think this is right, not sure.. */ __asm__( "mov 0(%%ebp), %%eax \n\t" "mov %%eax, %0 \n\t" @@ -591,13 +589,10 @@ void Logger_Abort2(ReturnCode result, const char* raw_msg) { "mov %%eax, %1 \n\t" "lea 8(%%ebp), %%eax \n\t" "mov %%eax, %2" - : "=m" (_ebp), "=m" (_eip), "=m" (_esp) + : "=m" (ctx.Ebp), "=m" (ctx.Eip), "=m" (ctx.Esp) : - : "eax", "memory"); - - ctx.Ebp = _ebp; - ctx.Eip = _eip; - ctx.Esp = _esp; + : "eax", "memory" + ); ctx.ContextFlags = CONTEXT_CONTROL; #endif @@ -608,7 +603,7 @@ void Logger_Abort2(ReturnCode result, const char* raw_msg) { #endif #elif defined CC_BUILD_POSIX static void Logger_SignalHandler(int sig, siginfo_t* info, void* ctx) { - String msg; char msgBuffer[STRING_SIZE * 2 + 1]; + String msg; char msgBuffer[128 + 1]; int type, code; uintptr_t addr; diff --git a/src/Menus.c b/src/Menus.c index 7d60df0ac..c0b183d85 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -2126,7 +2126,7 @@ static void MenuOptionsScreen_Input(void* screen, void* widget) { String value; char valueBuffer[STRING_SIZE]; struct MenuOptionsScreen* s = screen; - struct ButtonWidget* btn = widget; + struct ButtonWidget* btn = widget; int i; s->ActiveI = Menu_Index(s, btn); diff --git a/src/PickedPosRenderer.c b/src/PickedPosRenderer.c index 554ab3294..ec865e212 100644 --- a/src/PickedPosRenderer.c +++ b/src/PickedPosRenderer.c @@ -81,7 +81,7 @@ void PickedPosRenderer_Update(struct PickedPos* selected) { (0) OUTER_MIN ==>#--#-------#--# - these are used to fake thick lines, by making the lines appear slightly inset - - note: actual difference between inner and outer is much smaller then the diagram + - note: actual difference between inner and outer is much smaller than the diagram */ Vector3_Add1(&coords[0], &selected->Min, -offset); Vector3_Add1(&coords[1], &coords[0], size); diff --git a/src/Platform.c b/src/Platform.c index 962f4a434..90179bd26 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -13,7 +13,7 @@ #include "freetype/ftmodapi.h" #include "freetype/ftglyph.h" -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN #define WIN32_LEAN_AND_MEAN #define NOSERVICE #define NOMCX @@ -49,9 +49,8 @@ const ReturnCode ReturnCode_NotSupported = ERROR_NOT_SUPPORTED; const ReturnCode ReturnCode_InvalidArg = ERROR_INVALID_PARAMETER; const ReturnCode ReturnCode_SocketInProgess = WSAEINPROGRESS; const ReturnCode ReturnCode_SocketWouldBlock = WSAEWOULDBLOCK; -#endif +#elif defined CC_BUILD_POSIX /* POSIX can be shared between Linux/BSD/OSX */ -#ifdef CC_BUILD_POSIX #include #include #include @@ -129,7 +128,7 @@ CC_NOINLINE static void Platform_AllocFailed(const char* place) { Logger_Abort(log.buffer); } -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN void* Mem_Alloc(uint32_t numElems, uint32_t elemsSize, const char* place) { uint32_t numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ void* ptr = HeapAlloc(heap, 0, numBytes); @@ -154,8 +153,7 @@ void* Mem_Realloc(void* mem, uint32_t numElems, uint32_t elemsSize, const char* void Mem_Free(void* mem) { if (mem) HeapFree(heap, 0, mem); } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_POSIX void* Mem_Alloc(uint32_t numElems, uint32_t elemsSize, const char* place) { void* ptr = malloc(numElems * elemsSize); /* TODO: avoid overflow here */ if (!ptr) Platform_AllocFailed(place); @@ -213,7 +211,7 @@ uint64_t Stopwatch_ElapsedMicroseconds(uint64_t beg, uint64_t end) { return ((end - beg) * sw_freqMul) / sw_freqDiv; } -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN static HANDLE conHandle; static BOOL hasDebugger; @@ -274,8 +272,7 @@ uint64_t Stopwatch_Measure(void) { return (uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32); } } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_POSIX void Platform_Log(const String* message) { write(STDOUT_FILENO, message->buffer, message->length); write(STDOUT_FILENO, "\n", 1); @@ -345,7 +342,7 @@ uint64_t Stopwatch_Measure(void) { /*########################################################################################################################* *-----------------------------------------------------Directory/File------------------------------------------------------* *#########################################################################################################################*/ -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN bool Directory_Exists(const String* path) { TCHAR str[300]; DWORD attribs; @@ -495,8 +492,7 @@ ReturnCode File_Length(FileHandle file, uint32_t* len) { *len = GetFileSize(file, NULL); return *len != INVALID_FILE_SIZE ? 0 : GetLastError(); } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_POSIX bool Directory_Exists(const String* path) { char str[600]; struct stat sb; @@ -636,7 +632,7 @@ ReturnCode File_Length(FileHandle file, uint32_t* len) { /*########################################################################################################################* *--------------------------------------------------------Threading--------------------------------------------------------* *#########################################################################################################################*/ -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN void Thread_Sleep(uint32_t milliseconds) { Sleep(milliseconds); } DWORD WINAPI Thread_StartCallback(void* param) { Thread_StartFunc* func = (Thread_StartFunc*)param; @@ -701,8 +697,24 @@ void Waitable_Wait(void* handle) { void Waitable_WaitFor(void* handle, uint32_t milliseconds) { WaitForSingleObject((HANDLE)handle, milliseconds); } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_WEB +/* No true multithreading support with emscripten backend */ +void Thread_Sleep(uint32_t milliseconds) { usleep(milliseconds * 1000); } +void* Thread_Start(Thread_StartFunc* func, bool detach) { (*func)(); return NULL; } +void Thread_Detach(void* handle) { } +void Thread_Join(void* handle) { } + +void* Mutex_Create(void) { } +void Mutex_Free(void* handle) { } +void Mutex_Lock(void* handle) { } +void Mutex_Unlock(void* handle) { } + +void* Waitable_Create(void) { } +void Waitable_Free(void* handle) { } +void Waitable_Signal(void* handle) { } +void Waitable_Wait(void* handle) { } +void Waitable_WaitFor(void* handle, uint32_t milliseconds) { } +#elif defined CC_BUILD_POSIX void Thread_Sleep(uint32_t milliseconds) { usleep(milliseconds * 1000); } void* Thread_StartCallback(void* lpParam) { Thread_StartFunc* func = (Thread_StartFunc*)lpParam; @@ -1298,7 +1310,7 @@ void Socket_Create(SocketHandle* socketResult) { } static ReturnCode Socket_ioctl(SocketHandle socket, uint32_t cmd, int* data) { -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN return ioctlsocket(socket, cmd, data); #else return ioctl(socket, cmd, data); @@ -1309,7 +1321,7 @@ ReturnCode Socket_Available(SocketHandle socket, uint32_t* available) { return Socket_ioctl(socket, FIONREAD, available); } ReturnCode Socket_SetBlocking(SocketHandle socket, bool blocking) { -#ifdef CC_BUILD_WEB +#if defined CC_BUILD_WEB return ReturnCode_NotSupported; /* sockets always async */ #else int blocking_raw = blocking ? 0 : -1; @@ -1360,7 +1372,7 @@ ReturnCode Socket_Close(SocketHandle socket) { #endif if (res1 == -1) res = Socket__Error(); -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN res2 = closesocket(socket); #else res2 = close(socket); @@ -1370,7 +1382,7 @@ ReturnCode Socket_Close(SocketHandle socket) { } /* Alas, a simple cross-platform select() is not good enough */ -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN ReturnCode Socket_Poll(SocketHandle socket, int mode, bool* success) { fd_set set; struct timeval time = { 0 }; @@ -1389,8 +1401,7 @@ ReturnCode Socket_Poll(SocketHandle socket, int mode, bool* success) { *success = set.fd_count != 0; return 0; } -#else -#ifdef CC_BUILD_OSX +#elif defined CC_BUILD_OSX /* poll is broken on old OSX apparently https://daniel.haxx.se/docs/poll-vs-select.html */ ReturnCode Socket_Poll(SocketHandle socket, int mode, bool* success) { fd_set set; @@ -1424,14 +1435,13 @@ ReturnCode Socket_Poll(SocketHandle socket, int mode, bool* success) { return 0; } #endif -#endif /*########################################################################################################################* *----------------------------------------------------------Audio----------------------------------------------------------* *#########################################################################################################################*/ static ReturnCode Audio_AllCompleted(AudioHandle handle, bool* finished); -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN struct AudioContext { HWAVEOUT Handle; WAVEHDR Headers[AUDIO_MAX_BUFFERS]; @@ -1534,8 +1544,7 @@ ReturnCode Audio_IsCompleted(AudioHandle handle, int idx, bool* completed) { } ReturnCode Audio_IsFinished(AudioHandle handle, bool* finished) { return Audio_AllCompleted(handle, finished); } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_POSIX struct AudioContext { ALuint Source; ALuint Buffers[AUDIO_MAX_BUFFERS]; @@ -1767,7 +1776,7 @@ ReturnCode Audio_StopAndFree(AudioHandle handle) { /*########################################################################################################################* *-----------------------------------------------------Process/Module------------------------------------------------------* *#########################################################################################################################*/ -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN ReturnCode Process_GetExePath(String* path) { TCHAR chars[FILENAME_SIZE + 1]; DWORD len = GetModuleFileName(NULL, chars, FILENAME_SIZE); @@ -1825,8 +1834,7 @@ ReturnCode DynamicLib_Get(void* lib, const char* name, void** symbol) { bool DynamicLib_DescribeError(ReturnCode res, String* dst) { return Platform_DescribeError(res, dst); } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_POSIX ReturnCode Process_Start(const String* path, const String* args) { char str[600], raw[600]; pid_t pid; @@ -1881,14 +1889,34 @@ bool DynamicLib_DescribeError(ReturnCode res, String* dst) { return err && err[0]; } #endif -#ifdef CC_BUILD_UNIX +/* Opening browser and retrieving exe path is not standardised at all */ +#if defined CC_BUILD_WEB +ReturnCode Process_StartOpen(const String* args) { + char str[600]; + Platform_ConvertString(str, args); + EM_ASM_({ window.open(UTF8ToString($0)); }, str); +} +ReturnCode Process_GetExePath(String* path) { return ReturnCode_NotSupported; } +#elif defined CC_BUILD_OSX +ReturnCode Process_StartOpen(const String* args) { + const static String path = String_FromConst("/usr/bin/open"); + return Process_Start(&path, args); +} +ReturnCode Process_GetExePath(String* path) { + char str[600]; + int len = 600; + + if (_NSGetExecutablePath(str, &len) != 0) return ReturnCode_InvalidArg; + Convert_DecodeUtf8(path, str, len); + return 0; +} +#elif defined CC_BUILD_UNIX ReturnCode Process_StartOpen(const String* args) { /* TODO: Can this be used on original Solaris, or is it just an OpenIndiana thing */ const static String path = String_FromConst("xdg-open"); return Process_Start(&path, args); } -#endif -#ifdef CC_BUILD_LINUX +#if defined CC_BUILD_LINUX ReturnCode Process_GetExePath(String* path) { char str[600]; int len = readlink("/proc/self/exe", str, 600); @@ -1897,8 +1925,7 @@ ReturnCode Process_GetExePath(String* path) { Convert_DecodeUtf8(path, str, len); return 0; } -#endif -#ifdef CC_BUILD_FREEBSD +#elif defined CC_BUILD_FREEBSD ReturnCode Process_GetExePath(String* path) { static int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; char str[600]; @@ -1909,8 +1936,7 @@ ReturnCode Process_GetExePath(String* path) { Convert_DecodeUtf8(path, str, size); return 0; } -#endif -#ifdef CC_BUILD_OPENBSD +#elif defined CC_BUILD_OPENBSD ReturnCode Process_GetExePath(String* path) { static int mib[4] = { CTL_KERN, KERN_PROC_ARGS, 0, KERN_PROC_ARGV }; char tmp[600]; @@ -1936,8 +1962,7 @@ ReturnCode Process_GetExePath(String* path) { Convert_DecodeUtf8(path, str, size); return 0; } -#endif -#ifdef CC_BUILD_NETBSD +#elif defined CC_BUILD_NETBSD ReturnCode Process_GetExePath(String* path) { static int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; char str[600]; @@ -1948,8 +1973,7 @@ ReturnCode Process_GetExePath(String* path) { Convert_DecodeUtf8(path, str, size); return 0; } -#endif -#ifdef CC_BUILD_SOLARIS +#elif defined CC_BUILD_SOLARIS ReturnCode Process_GetExePath(String* path) { char str[600]; int len = readlink("/proc/self/path/a.out", str, 600); @@ -1959,27 +1983,6 @@ ReturnCode Process_GetExePath(String* path) { return 0; } #endif -#ifdef CC_BUILD_OSX -ReturnCode Process_StartOpen(const String* args) { - const static String path = String_FromConst("/usr/bin/open"); - return Process_Start(&path, args); -} -ReturnCode Process_GetExePath(String* path) { - char str[600]; - int len = 600; - - if (_NSGetExecutablePath(str, &len) != 0) return ReturnCode_InvalidArg; - Convert_DecodeUtf8(path, str, len); - return 0; -} -#endif -#ifdef CC_BUILD_WEB -ReturnCode Process_StartOpen(const String* args) { - char str[600]; - Platform_ConvertString(str, args); - EM_ASM_({ window.open(UTF8ToString($0)); }, str); -} -ReturnCode Process_GetExePath(String* path) { return ReturnCode_NotSupported; } #endif void* DynamicLib_GetFrom(const char* filename, const char* name) { @@ -2000,7 +2003,7 @@ void* DynamicLib_GetFrom(const char* filename, const char* name) { /*########################################################################################################################* *--------------------------------------------------------Platform---------------------------------------------------------* *#########################################################################################################################*/ -#ifdef CC_BUILD_WIN +#if defined CC_BUILD_WIN int Platform_ConvertString(void* data, const String* src) { TCHAR* dst = data; int i; @@ -2133,8 +2136,7 @@ bool Platform_DescribeError(ReturnCode res, String* dst) { Platform_DecodeString(dst, chars, res); return true; } -#endif -#ifdef CC_BUILD_POSIX +#elif defined CC_BUILD_POSIX int Platform_ConvertString(void* data, const String* src) { uint8_t* dst = data; uint8_t* cur; @@ -2209,14 +2211,13 @@ bool Platform_DescribeError(ReturnCode res, String* dst) { return true; } #endif -#ifdef CC_BUILD_UNIX +#if defined CC_BUILD_UNIX void Platform_Init(void) { Platform_InitCommon(); /* stopwatch always in nanoseconds */ sw_freqDiv = 1000; } -#endif -#ifdef CC_BUILD_OSX +#elif defined CC_BUILD_OSX static void Platform_InitStopwatch(void) { mach_timebase_info_data_t tb = { 0 }; mach_timebase_info(&tb); @@ -2235,7 +2236,6 @@ void Platform_Init(void) { /* NOTE: TransformProcessType is OSX 10.3 or later */ TransformProcessType(&psn, kProcessTransformToForegroundApplication); } -#endif -#ifdef CC_BUILD_WEB +#elif defined CC_BUILD_WEB void Platform_Init(void) { } #endif diff --git a/src/Widgets.c b/src/Widgets.c index dca68c0f8..7bdb59542 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -200,10 +200,10 @@ static PackedCol Scroll_BackCol = PACKEDCOL_CONST( 10, 10, 10, 220); static PackedCol Scroll_BarCol = PACKEDCOL_CONST(100, 100, 100, 220); static PackedCol Scroll_HoverCol = PACKEDCOL_CONST(122, 122, 122, 220); -static void ScrollbarWidget_ClampScrollY(struct ScrollbarWidget* w) { - int maxRows = w->TotalRows - TABLE_MAX_ROWS_DISPLAYED; - if (w->ScrollY >= maxRows) w->ScrollY = maxRows; - if (w->ScrollY < 0) w->ScrollY = 0; +static void ScrollbarWidget_ClampTopRow(struct ScrollbarWidget* w) { + int maxTop = w->TotalRows - TABLE_MAX_ROWS_DISPLAYED; + if (w->TopRow >= maxTop) w->TopRow = maxTop; + if (w->TopRow < 0) w->TopRow = 0; } static float ScrollbarWidget_GetScale(struct ScrollbarWidget* w) { @@ -213,7 +213,7 @@ static float ScrollbarWidget_GetScale(struct ScrollbarWidget* w) { static void ScrollbarWidget_GetScrollbarCoords(struct ScrollbarWidget* w, int* y, int* height) { float scale = ScrollbarWidget_GetScale(w); - *y = Math_Ceil(w->ScrollY * scale) + SCROLL_BORDER; + *y = Math_Ceil(w->TopRow * scale) + SCROLL_BORDER; *height = Math_Ceil(TABLE_MAX_ROWS_DISPLAYED * scale); *height = min(*y + *height, w->Height - SCROLL_BORDER) - *y; } @@ -256,14 +256,14 @@ static bool ScrollbarWidget_MouseDown(void* widget, int x, int y, MouseButton bt ScrollbarWidget_GetScrollbarCoords(w, &posY, &height); if (y < posY) { - w->ScrollY -= TABLE_MAX_ROWS_DISPLAYED; + w->TopRow -= TABLE_MAX_ROWS_DISPLAYED; } else if (y >= posY + height) { - w->ScrollY += TABLE_MAX_ROWS_DISPLAYED; + w->TopRow += TABLE_MAX_ROWS_DISPLAYED; } else { w->DraggingMouse = true; w->MouseOffset = y - posY; } - ScrollbarWidget_ClampScrollY(w); + ScrollbarWidget_ClampTopRow(w); return true; } @@ -278,8 +278,8 @@ static bool ScrollbarWidget_MouseScroll(void* widget, float delta) { struct ScrollbarWidget* w = widget; int steps = Utils_AccumulateWheelDelta(&w->ScrollingAcc, delta); - w->ScrollY -= steps; - ScrollbarWidget_ClampScrollY(w); + w->TopRow -= steps; + ScrollbarWidget_ClampTopRow(w); return true; } @@ -290,8 +290,8 @@ static bool ScrollbarWidget_MouseMove(void* widget, int x, int y) { if (w->DraggingMouse) { y -= w->Y; scale = ScrollbarWidget_GetScale(w); - w->ScrollY = (int)((y - w->MouseOffset) / scale); - ScrollbarWidget_ClampScrollY(w); + w->TopRow = (int)((y - w->MouseOffset) / scale); + ScrollbarWidget_ClampTopRow(w); return true; } return false; @@ -308,7 +308,7 @@ void ScrollbarWidget_Create(struct ScrollbarWidget* w) { w->VTABLE = &ScrollbarWidget_VTABLE; w->Width = SCROLL_WIDTH; w->TotalRows = 0; - w->ScrollY = 0; + w->TopRow = 0; w->ScrollingAcc = 0.0f; w->DraggingMouse = false; w->MouseOffset = 0; @@ -520,7 +520,7 @@ static int Table_Height(struct TableWidget* w) { static bool TableWidget_GetCoords(struct TableWidget* w, int i, int* cellX, int* cellY) { int x, y; x = i % w->ElementsPerRow; - y = i / w->ElementsPerRow - w->Scroll.ScrollY; + y = i / w->ElementsPerRow - w->Scroll.TopRow; *cellX = w->X + w->CellSize * x; *cellY = w->Y + w->CellSize * y + 3; @@ -729,8 +729,8 @@ static void TableWidget_ScrollRelative(struct TableWidget* w, int delta) { w->SelectedIndex = index; /* adjust scrollbar by number of rows moved up/down */ - w->Scroll.ScrollY += (index / w->ElementsPerRow) - (start / w->ElementsPerRow); - ScrollbarWidget_ClampScrollY(&w->Scroll); + w->Scroll.TopRow += (index / w->ElementsPerRow) - (start / w->ElementsPerRow); + ScrollbarWidget_ClampTopRow(&w->Scroll); TableWidget_RecreateDescTex(w); TableWidget_MoveCursorToSelected(w); @@ -760,18 +760,18 @@ static bool TableWidget_MouseUp(void* widget, int x, int y, MouseButton btn) { static bool TableWidget_MouseScroll(void* widget, float delta) { struct TableWidget* w = widget; - int startScrollY, index; + int origTopRow, index; bool bounds = Gui_Contains(Table_X(w), Table_Y(w), Table_Width(w) + w->Scroll.Width, Table_Height(w), Mouse_X, Mouse_Y); if (!bounds) return false; - startScrollY = w->Scroll.ScrollY; + origTopRow = w->Scroll.TopRow; Elem_HandlesMouseScroll(&w->Scroll, delta); if (w->SelectedIndex == -1) return true; index = w->SelectedIndex; - index += (w->Scroll.ScrollY - startScrollY) * w->ElementsPerRow; + index += (w->Scroll.TopRow - origTopRow) * w->ElementsPerRow; if (index >= w->ElementsCount) index = -1; w->SelectedIndex = index; @@ -846,9 +846,9 @@ void TableWidget_SetBlockTo(struct TableWidget* w, BlockID block) { /* When holding air, inventory should open at middle */ if (block == BLOCK_AIR) w->SelectedIndex = -1; - w->Scroll.ScrollY = w->SelectedIndex / w->ElementsPerRow; - w->Scroll.ScrollY -= (TABLE_MAX_ROWS_DISPLAYED - 1); - ScrollbarWidget_ClampScrollY(&w->Scroll); + w->Scroll.TopRow = w->SelectedIndex / w->ElementsPerRow; + w->Scroll.TopRow -= (TABLE_MAX_ROWS_DISPLAYED - 1); + ScrollbarWidget_ClampTopRow(&w->Scroll); TableWidget_MoveCursorToSelected(w); TableWidget_RecreateDescTex(w); } @@ -860,8 +860,8 @@ void TableWidget_OnInventoryChanged(struct TableWidget* w) { } w->LastX = -1; w->LastY = -1; - w->Scroll.ScrollY = w->SelectedIndex / w->ElementsPerRow; - ScrollbarWidget_ClampScrollY(&w->Scroll); + w->Scroll.TopRow = w->SelectedIndex / w->ElementsPerRow; + ScrollbarWidget_ClampTopRow(&w->Scroll); TableWidget_RecreateDescTex(w); } diff --git a/src/Widgets.h b/src/Widgets.h index 7f4f3d9c0..3dbae0ca9 100644 --- a/src/Widgets.h +++ b/src/Widgets.h @@ -37,7 +37,7 @@ CC_NOINLINE void ButtonWidget_Set(struct ButtonWidget* w, const String* text, co struct ScrollbarWidget { Widget_Layout - int TotalRows, ScrollY; + int TotalRows, TopRow; float ScrollingAcc; int MouseOffset; bool DraggingMouse;