commit fbcf1d1683d0d5a7632ed87f5fe6327592b4e5d7 Author: cancel Date: Wed Jun 1 04:57:45 2016 +0900 Add files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee5aa8f --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ + +#ignore thumbnails created by windows +Thumbs.db +#Ignore files build by Visual Studio +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* + +*.o +packaging/ +build/ diff --git a/benchmark_layout.c b/benchmark_layout.c new file mode 100644 index 0000000..7965e4a --- /dev/null +++ b/benchmark_layout.c @@ -0,0 +1,299 @@ +#include +#include "layout.c" + +#ifdef _WIN32 +#include + +LONG WINAPI LayTestUnhandledExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo) +{ + fputs("Exception", stdout); + fflush(stdout); + // TODO Do useful stuff + return EXCEPTION_EXECUTE_HANDLER; +} + +static inline double get_perf_freq() +{ + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + return (double)freq.QuadPart; +} + +static inline int64_t perf_counter() +{ + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + return (int64_t)c.QuadPart; +} + +static inline double perf_seconds(double perf_freq, int64_t t) +{ + return t / perf_freq; +} + +#endif + +/* +int sprint_vec4(char* str, lay_vec4 v) +{ + return sprintf(str, "%d, %d, %d, %d", v[0], v[1], v[2], v[3]); +} +int print_vec4(lay_vec4 v) +{ + return printf("%d, %d, %d, %d", v[0], v[1], v[2], v[3]); +} +int sprint_vec2(char* str, lay_vec2 v) +{ + return sprintf(str, "%d, %d", v[0], v[1]); +} +int sprint_item_info(char* str, lay_context *ctx, lay_id item) +{ + int offs = 0; + lay_vec4 item_rect = lay_rect_get(ctx, item); + + lay_vec4 item_margins = lay_margins_get(ctx, item); + lay_vec2 item_size = lay_size_get(ctx, item); + + offs += sprintf(str + offs, "rect: "); + offs += sprint_vec4(str + offs, item_rect); + + offs += sprintf(str + offs, " | margins: "); + offs += sprint_vec4(str + offs, item_margins); + + offs += sprintf(str + offs, " | size: "); + offs += sprint_vec2(str + offs, item_size); + + return offs; +} +*/ + +#define LTEST_TRUE(cond) \ + if (cond) {} \ + else { \ + printf("Failed test at line %d in %s", __LINE__, __func__); \ + abort(); \ + } +#define LTEST_FALSE(cond) \ + if (cond) { \ + printf("Failed test at line %d in %s", __LINE__, __func__); \ + abort(); \ + } \ + else {} + +#define LTEST_DECLARE(testname) \ + static void test_##testname(lay_context *ctx, char* strbuf) + +// vec4 equals test +#define LTEST_VEC4EQ(vecvar, x, y, z, w) \ + LTEST_TRUE(vecvar[0] == x && vecvar[1] == y && vecvar[2] == z && vecvar[3] == w) +#define LTEST_VEC4UNEQ(vecvar, x, y, z, w) \ + LTEST_FALSE(vecvar[0] == x && vecvar[1] == y && vecvar[2] == z && vecvar[3] == w) + +static inline void benchmark_nested(lay_context *ctx, char *strbuf) +{ + const size_t num_rows = 5; + // one of the rows is "fake" and will have 0 units tall height + const size_t num_rows_with_height = num_rows - 1; + + lay_id root = lay_item(ctx); + // main_child is a column that contains rows, and those rows + // will contain columns. + lay_id main_child = lay_item(ctx); + lay_size_set_xy(ctx, root, + 70, + // 10 units extra size above and below for main_child + // margin + (num_rows_with_height * 10 + 2 * 10) + ); + lay_margins_set_ltrb(ctx, main_child, 10, 10, 10, 10); + lay_contain_set(ctx, main_child, LAY_COLUMN); + lay_insert(ctx, root, main_child); + lay_behave_set(ctx, main_child, LAY_FILL); + + lay_id *rows = (lay_id*)calloc(num_rows, sizeof(lay_id)); + + // auto-filling columns-in-row, each one should end up being + // 10 units wide + rows[0] = lay_item(ctx); + lay_contain_set(ctx, rows[0], LAY_ROW); + lay_behave_set(ctx, rows[0], LAY_FILL); + lay_id cols1[5]; + // hmm so both the row and its child columns need to be set to + // fill? which means main_child also needs to be set to fill? + for (size_t i = 0; i < 5; ++i) { + lay_id col = lay_item(ctx); + // fill empty space + lay_behave_set(ctx, col, LAY_FILL); + lay_insert(ctx, rows[0], col); + cols1[i] = col; + } + + rows[1] = lay_item(ctx); + lay_contain_set(ctx, rows[1], LAY_ROW); + lay_behave_set(ctx, rows[1], LAY_VFILL); + lay_id cols2[5]; + for (size_t i = 0; i < 5; ++i) { + lay_id col = lay_item(ctx); + // fixed-size horizontally, fill vertically + lay_size_set_xy(ctx, col, 10, 0); + lay_behave_set(ctx, col, LAY_VFILL); + lay_insert(ctx, rows[1], col); + cols2[i] = col; + } + + // these columns have an inner item which sizes them + rows[2] = lay_item(ctx); + lay_contain_set(ctx, rows[2], LAY_ROW); + lay_id cols3[2]; + for (size_t i = 0; i < 2; ++i) { + lay_id col = lay_item(ctx); + lay_id inner_sizer = lay_item(ctx); + // only the second one will have height + lay_size_set_xy(ctx, inner_sizer, 25, 10 * (lay_scalar)i); + // align to bottom, only should make a difference for + // first item + lay_behave_set(ctx, col, LAY_BOTTOM); + lay_insert(ctx, col, inner_sizer); + lay_insert(ctx, rows[2], col); + cols3[i] = col; + } + + // row 4 should end up being 0 units tall after layout + rows[3] = lay_item(ctx); + lay_contain_set(ctx, rows[3], LAY_ROW); + lay_behave_set(ctx, rows[3], LAY_HFILL); + lay_id cols4[99]; + for (size_t i = 0; i < 99; ++i) { + lay_id col = lay_item(ctx); + lay_insert(ctx, rows[3], col); + cols4[i] = col; + } + + // row 5 should be 10 pixels tall after layout, and each of + // its columns should be 1 pixel wide + rows[4] = lay_item(ctx); + lay_contain_set(ctx, rows[4], LAY_ROW); + lay_behave_set(ctx, rows[4], LAY_FILL); + lay_id cols5[50]; + for (size_t i = 0; i < 50; ++i) { + lay_id col = lay_item(ctx); + lay_behave_set(ctx, col, LAY_FILL); + lay_insert(ctx, rows[4], col); + cols5[i] = col; + } + + for (size_t i = 0; i < num_rows; ++i) { + lay_insert(ctx, main_child, rows[i]); + } + + // Repeat the run and tests multiple times to make sure we get the expected + // results each time. The original version of oui would overwrite its input + // state (intentionally) with the output state, so the context's input data + // (margins, size) had to be "rebuilt" by the client code by doing a reset + // and then filling it back up for each run. 'lay' does not have that + // restriction. + // + // This is one of the more complex tests, so it's a good + // choice for testing multiple runs of the same context. + for (uint32_t run_n = 0; run_n < 5; ++run_n) { + //printf(" Iteration #%d\n", run_n + 1); + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, main_child), 10, 10, 50, 40); + // These rows should all be 10 units in height + LTEST_VEC4EQ(lay_rect_get(ctx, rows[0]), 10, 10, 50, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, rows[1]), 10, 20, 50, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, rows[2]), 10, 30, 50, 10); + // this row should have 0 height + LTEST_VEC4EQ(lay_rect_get(ctx, rows[3]), 10, 40, 50, 0); + LTEST_VEC4EQ(lay_rect_get(ctx, rows[4]), 10, 40, 50, 10); + + for (int16_t i = 0; i < 5; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols1[i]); + // each of these should be 10 units wide, and stacked + // horizontally + LTEST_VEC4EQ(r, 10 + 10 * i, 10, 10, 10); + } + + // the cols in the second row are similar to first row + for (int16_t i = 0; i < 5; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols2[i]); + LTEST_VEC4EQ(r, 10 + 10 * i, 20, 10, 10); + } + + // leftmost (first of two items), aligned to bottom of row, 0 + // units tall + LTEST_VEC4EQ(lay_rect_get(ctx, cols3[0]), 10, 40, 25, 0); + // rightmost (second of two items), same height as row, which + // is 10 units tall + LTEST_VEC4EQ(lay_rect_get(ctx, cols3[1]), 35, 30, 25, 10); + + // these should all have size 0 and be in the middle of the + // row + for (int16_t i = 0; i < 99; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols4[i]); + LTEST_VEC4EQ(r, 25 + 10, 40, 0, 0); + } + + // these should all be 1 unit wide and 10 units tall + for (int16_t i = 0; i < 50; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols5[i]); + LTEST_VEC4EQ(r, 10 + i, 40, 1, 10); + } + } + + free(rows); +} + +// Call in main to run a test by name +// +// Resets string buffer and lay context before running test +#define LBENCH_RUN(testname) \ + memset(sbuf, 0, 4096); \ + lay_context_reset(&ctx); \ + printf(" * " #testname "\n"); \ + testname(&ctx, sbuf); + +int main() +{ +#ifdef _WIN32 + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); + SetUnhandledExceptionFilter(LayTestUnhandledExceptionFilter); +#endif + + char *sbuf = (char*)malloc(4096); + + lay_context ctx; + lay_context_init(&ctx); + + double freq = get_perf_freq(); + memset(sbuf, 0, 4096); + + printf("Running benchmarks\n"); + + //LBENCH_RUN(benchmark_nested); + int64_t total_perfc = 0; + const uint32_t num_runs = 100000; + int64_t *run_times = (int64_t*)calloc(num_runs, sizeof(int64_t)); + for (uint32_t run_n = 0; run_n < num_runs; ++run_n) { + lay_context_reset(&ctx); + int64_t t1 = perf_counter(); + //printf(" * " #testname "\n"); + benchmark_nested(&ctx, sbuf); + int64_t t2 = perf_counter(); + int64_t diff = t2 - t1; + total_perfc += diff; + run_times[run_n] = diff; + //double seconds = perf_seconds(freq, diff); + //printf("Run %d: %f microsecs\n", run_n + 1, seconds * 1000000.0); + } + + double avg_seconds = perf_seconds(freq, total_perfc) / (double)num_runs; + printf("Average time: %f", avg_seconds * 1000000.0); + + free(run_times); + + lay_context_destroy(&ctx); + free(sbuf); + return 0; +} diff --git a/genie.lua b/genie.lua new file mode 100644 index 0000000..30015f7 --- /dev/null +++ b/genie.lua @@ -0,0 +1,179 @@ +local action = _ACTION or "" + +newaction { + trigger = "clean", + description = "Clean everything", + execute = function () + os.rmdir("./build") + end +} + +newaction { + trigger = "asm", + description = "Emit GCC assembly output for a file", + -- Hard-coded for windows+gmake, probably has shell quote bugs + execute = function () + os.execute("gcc -ggdb1 -Ofast -S -fno-stack-check -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -masm=intel -march=nehalem -fverbose-asm -DNDEBUG " .. _ARGS[1]) + end +} + +newoption { + trigger = "coords", + value = "coordtype", + description = "Use either integer or floating point for coordinates and sizes", + allowed = { + { "integer", "16-bit signed integer" }, + { "float", "32-bit floating point" }, + } +} + +local targetDir = path.join("./build", _ACTION, "bin") + +function incl_luajit() + configuration {} + includedirs { + "./thirdparty/LuaJIT-2.0.0/src", + } + -- Note: you will need to manually rebuild LuaJIT with the correct + -- compiler (mingw, msvc) first + libdirs { + "./thirdparty/LuaJIT-2.0.0/src", + } + -- lua51.dll + links { "lua51" } +end + +function as_console_app() + kind "ConsoleApp" + -- Needed in mingw when building console app, genie/premake + -- automatically adds this for WindowedApp. + links { "gdi32" } +end +function as_shared_lib() + kind "SharedLib" +end + +function lay_project(project_name, as_build_type, src_file) + project(project_name) + platforms { "x32", "x64" } + -- kind "ConsoleApp" + -- kind "WindowedApp" + language "C" + -- regular + --files { "**.h", "**.c" } + files { "**.h", src_file } + + if _ACTION == "vs2015" then + -- Build as C++ even with .c filename extension, because we + -- need [] operator for our vector types (GCC provides this in + -- C with vector_size special attribute, but need operator + -- overload in C++) + options { "ForceCpp" } + end + + configuration {} + flags { "ExtraWarnings" } + targetdir(targetDir) + as_build_type() + + configuration { "float" } + defines { "LAY_FLOAT=1" } + + configuration { "vs2015", "windows" } + defines { "_CRT_SECURE_NO_WARNINGS" } + buildoptions { + -- Disable "unreferenced formal parameter" warning + "-wd4100" + } + + configuration { "gmake", "windows" } + -- Nehalem chips were released in 2008, and include SSE4. + -- + -- Note that gcc -Q --help=target -march=nehalem (or whatever other + -- arch) will not correctly display the enabled/disabled flags for + -- stuff like SSE, etc. This is a known problem with gcc (it's on + -- their bug tracker). -march=native, however, will show the + -- enabled/disabled flags correctly. + buildoptions { + "-std=c99", + "-Wno-unused-parameter", + -- We want strict overflow on regardless of optimization level, + -- because it will change program semantics. + "-fstrict-overflow", + "-fstrict-aliasing", + "-Wstrict-aliasing=3", + "-march=nehalem" + } + + configuration "Debug or Develop" + flags { "Symbols" } + + configuration { "Debug" } + defines { "DEBUG" } + configuration { "Debug", "gmake" } + buildoptions { + "-ggdb", + "-fstack-protector-strong", + } + -- need to manually link -lssp for stack protector stuff + links { "ssp" } + configuration { "Develop or Release", "gmake" } + -- Remove some extra junk from the asm we don't need + buildoptions { + "-fno-stack-check", + "-fno-dwarf2-cfi-asm", + "-fno-asynchronous-unwind-tables", + } + + configuration "Release" + defines { "NDEBUG", "WIN32_LEAN_AND_MEAN", "VC_EXTRALEAN" } + flags { + "Optimize", + "NoBufferSecurityCheck", + "NoWinRT", + "NoFramePointer", + } + windowstargetplatformversion "7" + + configuration { "Release", "gmake" } + buildoptions { + "-Ofast", + "-fuse-linker-plugin", + "-flto", + "-fno-fat-lto-objects" + } + linkoptions { + "-flto", + "-fuse-linker-plugin", + "-s" + } + -- SSE2 in msvc must be explicitly enabled when building 32-bit. In + -- x64, /arch:SSE2 will produce a compiler warning, because it's + -- redundant (SSE2 always available in x64). + configuration { "Release or Develop", "x32" } + flags { "EnableSSE2" } + -- msvc builds are forced to C++, and we always want to disable + -- exceptions and RTTI. + configuration { "vs*" } + flags { "NoExceptions", "NoRTTI" } + configuration { "Release", "vs*" } + flags { "FloatFast" } +end + +solution "layout" + location ( "build/" .. action ) + configurations { "Debug", "Develop", "Release" } + lay_project("tests", as_console_app, "test_layout.c") + lay_project("benchmark", as_console_app, "benchmark_layout.c") + lay_project("luamodule", as_shared_lib, "luamodule_layout.c") + incl_luajit() + configuration {} + targetname("layout") + +-- Copy glew32.dll file when creating gmake project files +-- Only needed if we have any projects using glew +-- +--if _ACTION == "gmake" then +-- os.mkdir(targetDir) +-- os.copyfile("./thirdparty/glew/bin/mingw/glew32.dll", path.join(targetDir, "glew32.dll")) +--end diff --git a/layout.c b/layout.c new file mode 100644 index 0000000..a34cbfd --- /dev/null +++ b/layout.c @@ -0,0 +1,683 @@ +#include "layout.h" +#include +#include // memset +#include // realloc + +#if defined(__GNUC__) +#define LAY_FORCE_INLINE __attribute__((always_inline)) inline +#define LAY_RESTRICT restrict +#elif defined(_MSC_VER) +#define LAY_FORCE_INLINE __forceinline +#define LAY_RESTRICT __restrict +#else +#define LAY_FORCE_INLINE inline +#define LAY_RESTRICT +#endif + +// Useful math utilities +static LAY_FORCE_INLINE lay_scalar lay_scalar_max(lay_scalar a, lay_scalar b) +{ return a > b ? a : b; } +static LAY_FORCE_INLINE lay_scalar lay_scalar_min(lay_scalar a, lay_scalar b) +{ return a < b ? a : b; } +static LAY_FORCE_INLINE float lay_float_max(float a, float b) +{ return a > b ? a : b; } +static LAY_FORCE_INLINE float lay_float_min(float a, float b) +{ return a < b ? a : b; } + +void lay_context_init(lay_context *ctx) +{ + ctx->capacity = 0; + ctx->count = 0; + ctx->items = NULL; + ctx->rects = NULL; +} + +void lay_context_reserve(lay_context *ctx, lay_id count) +{ + if (count >= ctx->capacity) { + ctx->capacity = count; + const size_t item_size = sizeof(lay_item_t) + sizeof(lay_vec4); + ctx->items = (lay_item_t*)realloc(ctx->items, ctx->capacity * item_size); + const lay_item_t *past_last = ctx->items + ctx->capacity; + ctx->rects = (lay_vec4*)past_last; + } +} + +void lay_context_destroy(lay_context *ctx) +{ + if (ctx->items != NULL) { + free(ctx->items); + ctx->items = NULL; + ctx->rects = NULL; + } +} + +void lay_context_reset(lay_context *ctx) +{ ctx->count = 0; } + +static void lay_calc_size(lay_context *ctx, lay_id item, int dim); +static void lay_arrange(lay_context *ctx, lay_id item, int dim); + +void lay_context_run(lay_context *ctx) +{ + LAY_ASSERT(ctx != NULL); + + // TODO this is bad. We need to reset the 'break' flag, in case the item + // data is being reused across 'run' invocations. Iterating all of the + // items up-front like this and mutating data is bad for performance. But + // if we don't do this, then the leftover LAY_BREAK bits will still be set + // from the previous 'run', which may cause broken wrapping behavior if the + // sizes of the container (or the items) have changed. + // + // Maybe we should instead have a separate 'reset_breaks' procedure the + // user can explicitly run when they know they have 'wrap' items which are + // being resued across frames. + // + // Alternatively, we could use a flag bit to indicate whether an item's + // children have already been wrapped and may need re-wrapping. + for (lay_id i = 0; i < ctx->count; ++i) { + lay_item_t *pitem = lay_item_get(ctx, i); + pitem->flags = pitem->flags & ~LAY_BREAK; + } + + if (ctx->count > 0) { + lay_calc_size(ctx, 0, 0); + lay_arrange(ctx, 0, 0); + lay_calc_size(ctx, 0, 1); + lay_arrange(ctx, 0, 1); + } +} + +lay_id lay_context_item_count(lay_context *ctx) +{ + LAY_ASSERT(ctx != NULL); + return ctx->count; +} + +lay_id lay_context_item_capacity(lay_context *ctx) +{ + LAY_ASSERT(ctx != NULL); + return ctx->capacity; +} + +lay_id lay_item(lay_context *ctx) +{ + lay_id idx = ctx->count++; + + if (idx >= ctx->capacity) { + ctx->capacity = ctx->capacity < 1 ? 32 : (ctx->capacity * 4); + const size_t item_size = sizeof(lay_item_t) + sizeof(lay_vec4); + ctx->items = (lay_item_t*)realloc(ctx->items, ctx->capacity * item_size); + const lay_item_t *past_last = ctx->items + ctx->capacity; + ctx->rects = (lay_vec4*)past_last; + } + + lay_item_t *item = lay_item_get(ctx, idx); + // We can either do this here, or when creating/resetting buffer + memset(item, 0, sizeof(lay_item_t)); + item->first_child = LAY_ID_INVALID; + item->next_sibling = LAY_ID_INVALID; + // hmm + memset(&ctx->rects[idx], 0, sizeof(lay_vec4)); + return idx; +} + +static LAY_FORCE_INLINE +void lay_append_by_ptr( + lay_item_t *LAY_RESTRICT pearlier, + lay_id later, lay_item_t *LAY_RESTRICT plater) +{ + plater->next_sibling = pearlier->next_sibling; + plater->flags |= LAY_ITEM_INSERTED; + pearlier->next_sibling = later; +} + +lay_id lay_last_child(const lay_context *ctx, lay_id parent) +{ + lay_item_t *pparent = lay_item_get(ctx, parent); + lay_id child = pparent->first_child; + if (child == LAY_ID_INVALID) return LAY_ID_INVALID; + lay_item_t *pchild = lay_item_get(ctx, child); + lay_id result = child; + for (;;) { + lay_id next = pchild->next_sibling; + if (next == LAY_ID_INVALID) break; + result = next; + pchild = lay_item_get(ctx, next); + } + return result; +} + +void lay_append(lay_context *ctx, lay_id earlier, lay_id later) +{ + LAY_ASSERT(later != 0); // Must not be root item + LAY_ASSERT(earlier != later); // Must not be same item id + lay_item_t *LAY_RESTRICT pearlier = lay_item_get(ctx, earlier); + lay_item_t *LAY_RESTRICT plater = lay_item_get(ctx, later); + lay_append_by_ptr(pearlier, later, plater); +} + +void lay_insert(lay_context *ctx, lay_id parent, lay_id child) +{ + LAY_ASSERT(child != 0); // Must not be root item + LAY_ASSERT(parent != child); // Must not be same item id + lay_item_t *LAY_RESTRICT pparent = lay_item_get(ctx, parent); + lay_item_t *LAY_RESTRICT pchild = lay_item_get(ctx, child); + LAY_ASSERT(!(pchild->flags & LAY_ITEM_INSERTED)); + // Parent has no existing children, make inserted item the first child. + if (pparent->first_child == LAY_ID_INVALID) { + pparent->first_child = child; + pchild->flags |= LAY_ITEM_INSERTED; + // Parent has existing items, iterate to find the last child and append the + // inserted item after it. + } else { + lay_id next = pparent->first_child; + lay_item_t *LAY_RESTRICT pnext = lay_item_get(ctx, next); + for (;;) { + next = pnext->next_sibling; + if (next == LAY_ID_INVALID) break; + pnext = lay_item_get(ctx, next); + } + lay_append_by_ptr(pnext, child, pchild); + } +} + +void lay_push(lay_context *ctx, lay_id parent, lay_id new_child) +{ + LAY_ASSERT(new_child != 0); // Must not be root item + LAY_ASSERT(parent != new_child); // Must not be same item id + lay_item_t *LAY_RESTRICT pparent = lay_item_get(ctx, parent); + lay_id old_child = pparent->first_child; + lay_item_t *LAY_RESTRICT pchild = lay_item_get(ctx, new_child); + LAY_ASSERT(!(pchild->flags & LAY_ITEM_INSERTED)); + pparent->first_child = new_child; + pchild->flags |= LAY_ITEM_INSERTED; + pchild->next_sibling = old_child; +} + +lay_vec2 lay_size_get(lay_context *ctx, lay_id item) +{ + lay_item_t *pitem = lay_item_get(ctx, item); + return pitem->size; +} + +void lay_size_set_xy( + lay_context *ctx, lay_id item, + lay_scalar width, lay_scalar height) +{ + lay_item_t *pitem = lay_item_get(ctx, item); + pitem->size[0] = width; + pitem->size[1] = height; + uint32_t flags = pitem->flags; + if (width == 0) + flags &= ~LAY_ITEM_HFIXED; + else + flags |= LAY_ITEM_HFIXED; + if (height == 0) + flags &= ~LAY_ITEM_VFIXED; + else + flags |= LAY_ITEM_VFIXED; + pitem->flags = flags; +} + +void lay_behave_set(lay_context *ctx, lay_id item, uint32_t flags) +{ + LAY_ASSERT((flags & LAY_ITEM_LAYOUT_MASK) == flags); + lay_item_t *pitem = lay_item_get(ctx, item); + pitem->flags = (pitem->flags & ~LAY_ITEM_LAYOUT_MASK) | flags; +} + +void lay_contain_set(lay_context *ctx, lay_id item, uint32_t flags) +{ + LAY_ASSERT((flags & LAY_ITEM_BOX_MASK) == flags); + lay_item_t *pitem = lay_item_get(ctx, item); + pitem->flags = (pitem->flags & ~LAY_ITEM_BOX_MASK) | flags; +} +void lay_margins_set(lay_context *ctx, lay_id item, lay_vec4 ltrb) +{ + lay_item_t *pitem = lay_item_get(ctx, item); + pitem->margins = ltrb; +} +void lay_margins_set_ltrb( + lay_context *ctx, lay_id item, + lay_scalar l, lay_scalar t, lay_scalar r, lay_scalar b) +{ + lay_item_t *pitem = lay_item_get(ctx, item); + // Alternative, uses stack and addressed writes + //pitem->margins = lay_vec4_xyzw(l, t, r, b); + // Alternative, uses rax and left-shift + //pitem->margins = (lay_vec4){l, t, r, b}; + // Fewest instructions, but uses more addressed writes? + pitem->margins[0] = l; + pitem->margins[1] = t; + pitem->margins[2] = r; + pitem->margins[3] = b; +} + +lay_vec4 lay_margins_get(lay_context *ctx, lay_id item) +{ return lay_item_get(ctx, item)->margins; } + +// TODO restrict item ptrs correctly +static LAY_FORCE_INLINE +lay_scalar lay_calc_overlayed_size( + lay_context *ctx, lay_id item, int dim) +{ + const int wdim = dim + 2; + lay_item_t *LAY_RESTRICT pitem = lay_item_get(ctx, item); + lay_scalar need_size = 0; + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + lay_vec4 rect = ctx->rects[child]; + // width = start margin + calculated width + end margin + lay_scalar child_size = rect[dim] + rect[2 + dim] + pchild->margins[wdim]; + need_size = lay_scalar_max(need_size, child_size); + child = pchild->next_sibling; + } + return need_size; +} + +static LAY_FORCE_INLINE +lay_scalar lay_calc_stacked_size( + lay_context *ctx, lay_id item, int dim) +{ + const int wdim = dim + 2; + lay_item_t *LAY_RESTRICT pitem = lay_item_get(ctx, item); + lay_scalar need_size = 0; + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + lay_vec4 rect = ctx->rects[child]; + need_size += rect[dim] + rect[2 + dim] + pchild->margins[wdim]; + child = pchild->next_sibling; + } + return need_size; +} + +static LAY_FORCE_INLINE +lay_scalar lay_calc_wrapped_overlayed_size( + lay_context *ctx, lay_id item, int dim) +{ + const int wdim = dim + 2; + lay_item_t *LAY_RESTRICT pitem = lay_item_get(ctx, item); + lay_scalar need_size = 0; + lay_scalar need_size2 = 0; + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + lay_vec4 rect = ctx->rects[child]; + if (pchild->flags & LAY_BREAK) { + need_size2 += need_size; + need_size = 0; + } + lay_scalar child_size = rect[dim] + rect[2 + dim] + pchild->margins[wdim]; + need_size = lay_scalar_max(need_size, child_size); + child = pchild->next_sibling; + } + return need_size2 + need_size; +} + +// Equivalent to uiComputeWrappedStackedSize +static LAY_FORCE_INLINE +lay_scalar lay_calc_wrapped_stacked_size( + lay_context *ctx, lay_id item, int dim) +{ + const int wdim = dim + 2; + lay_item_t *LAY_RESTRICT pitem = lay_item_get(ctx, item); + lay_scalar need_size = 0; + lay_scalar need_size2 = 0; + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + lay_vec4 rect = ctx->rects[child]; + if (pchild->flags & LAY_BREAK) { + need_size2 = lay_scalar_max(need_size2, need_size); + need_size = 0; + } + need_size += rect[dim] + rect[2 + dim] + pchild->margins[wdim]; + child = pchild->next_sibling; + } + return lay_scalar_max(need_size2, need_size); +} + +static void lay_calc_size(lay_context *ctx, lay_id item, int dim) +{ + lay_item_t *pitem = lay_item_get(ctx, item); + + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + // NOTE: this is recursive and will run out of stack space if items are + // nested too deeply. + lay_calc_size(ctx, child, dim); + lay_item_t *pchild = lay_item_get(ctx, child); + child = pchild->next_sibling; + } + + // Set the mutable rect output data to the starting input data + ctx->rects[item][dim] = pitem->margins[dim]; + + // If we have an explicit input size, just set our output size (which other + // calc_size and arrange procedures will use) to it. + if (pitem->size[dim] != 0) { + ctx->rects[item][2 + dim] = pitem->size[dim]; + return; + } + + // Calculate our size based on children items. Note that we've already + // called lay_calc_size on our children at this point. + lay_scalar cal_size; + switch (pitem->flags & LAY_ITEM_BOX_MODEL_MASK) { + case LAY_COLUMN|LAY_WRAP: + // flex model + if (dim) // direction + cal_size = lay_calc_stacked_size(ctx, item, 1); + else + cal_size = lay_calc_overlayed_size(ctx, item, 0); + break; + case LAY_ROW|LAY_WRAP: + // flex model + if (!dim) // direction + cal_size = lay_calc_wrapped_stacked_size(ctx, item, 0); + else + cal_size = lay_calc_wrapped_overlayed_size(ctx, item, 1); + break; + case LAY_COLUMN: + case LAY_ROW: + // flex model + if ((pitem->flags & 1) == (uint32_t)dim) // direction + cal_size = lay_calc_stacked_size(ctx, item, dim); + else + cal_size = lay_calc_overlayed_size(ctx, item, dim); + break; + default: + // layout model + cal_size = lay_calc_overlayed_size(ctx, item, dim); + break; + } + + // Set our output data size. Will be used by parent calc_size procedures., + // and by arrange procedures. + ctx->rects[item][2 + dim] = cal_size; +} + +static LAY_FORCE_INLINE +void lay_arrange_stacked( + lay_context *ctx, lay_id item, int dim, bool wrap) +{ + const int wdim = dim + 2; + lay_item_t *pitem = lay_item_get(ctx, item); + + const uint32_t item_flags = pitem->flags; + lay_vec4 rect = ctx->rects[item]; + lay_scalar space = rect[2 + dim]; + + float max_x2 = (float)(rect[dim] + space); + + lay_id start_child = pitem->first_child; + while (start_child != LAY_ID_INVALID) { + lay_scalar used = 0; + uint32_t count = 0; // count of fillers + uint32_t squeezed_count = 0; // count of squeezable elements + uint32_t total = 0; + bool hardbreak = false; + // first pass: count items that need to be expanded, + // and the space that is used + lay_id child = start_child; + lay_id end_child = LAY_ID_INVALID; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + const uint32_t child_flags = pchild->flags; + const uint32_t flags = (child_flags & LAY_ITEM_LAYOUT_MASK) >> dim; + const uint32_t fflags = (child_flags & LAY_ITEM_FIXED_MASK) >> dim; + const lay_vec4 child_margins = pchild->margins; + lay_vec4 child_rect = ctx->rects[child]; + lay_scalar extend = used; + if ((flags & LAY_HFILL) == LAY_HFILL) { + ++count; + extend += child_rect[dim] + child_margins[wdim]; + } else { + if ((fflags & LAY_ITEM_HFIXED) != LAY_ITEM_HFIXED) + ++squeezed_count; + extend += child_rect[dim] + child_rect[2 + dim] + child_margins[wdim]; + } + // wrap on end of line or manual flag + if (wrap && ( + total && ((extend > space) || + (child_flags & LAY_BREAK)))) { + end_child = child; + hardbreak = (child_flags & LAY_BREAK) == LAY_BREAK; + // add marker for subsequent queries + pchild->flags = child_flags | LAY_BREAK; + break; + } else { + used = extend; + child = pchild->next_sibling; + } + ++total; + } + + lay_scalar extra_space = space - used; + float filler = 0.0f; + float spacer = 0.0f; + float extra_margin = 0.0f; + float eater = 0.0f; + + if (extra_space > 0) { + if (count > 0) + filler = (float)extra_space / (float)count; + else if (total > 0) { + switch (item_flags & LAY_JUSTIFY) { + case LAY_JUSTIFY: + // justify when not wrapping or not in last line, + // or not manually breaking + if (!wrap || ((end_child != LAY_ID_INVALID) && !hardbreak)) + spacer = (float)extra_space / (float)(total - 1); + break; + case LAY_START: + break; + case LAY_END: + extra_margin = extra_space; + break; + default: + extra_margin = extra_space / 2.0f; + break; + } + } + } +#if LAY_FLOAT == 1 + // In floating point, it's possible to end up with some small negative + // value for extra_space, while also have a 0.0 squeezed_count. This + // would cause divide by zero. Instead, we'll check to see if + // squeezed_count is > 0. I believe this produces the same results as + // the original oui int-only code. However, I don't have any tests for + // it, so I'll leave it if-def'd for now. + else if (!wrap && (squeezed_count > 0)) +#else + // This is the original oui code + else if (!wrap && (extra_space < 0)) +#endif + eater = (float)extra_space / (float)squeezed_count; + + // distribute width among items + float x = (float)rect[dim]; + float x1; + // second pass: distribute and rescale + child = start_child; + while (child != end_child) { + lay_scalar ix0, ix1; + lay_item_t *pchild = lay_item_get(ctx, child); + const uint32_t child_flags = pchild->flags; + const uint32_t flags = (child_flags & LAY_ITEM_LAYOUT_MASK) >> dim; + const uint32_t fflags = (child_flags & LAY_ITEM_FIXED_MASK) >> dim; + const lay_vec4 child_margins = pchild->margins; + lay_vec4 child_rect = ctx->rects[child]; + + x += (float)child_rect[dim] + extra_margin; + if ((flags & LAY_HFILL) == LAY_HFILL) // grow + x1 = x + filler; + else if ((fflags & LAY_ITEM_HFIXED) == LAY_ITEM_HFIXED) + x1 = x + (float)child_rect[2 + dim]; + else // squeeze + x1 = x + lay_float_max(0.0f, (float)child_rect[2 + dim] + eater); + + ix0 = (lay_scalar)x; + if (wrap) + ix1 = (lay_scalar)lay_float_min(max_x2 - (float)child_margins[wdim], x1); + else + ix1 = (lay_scalar)x1; + child_rect[dim] = ix0; // pos + child_rect[dim + 2] = ix1 - ix0; // size + ctx->rects[child] = child_rect; + x = x1 + (float)child_margins[wdim]; + child = pchild->next_sibling; + extra_margin = spacer; + } + + start_child = end_child; + } +} + +static LAY_FORCE_INLINE +void lay_arrange_overlay(lay_context *ctx, lay_id item, int dim) +{ + const int wdim = dim + 2; + lay_item_t *pitem = lay_item_get(ctx, item); + const lay_vec4 rect = ctx->rects[item]; + const lay_scalar offset = rect[dim]; + const lay_scalar space = rect[2 + dim]; + + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + const uint32_t b_flags = (pchild->flags & LAY_ITEM_LAYOUT_MASK) >> dim; + const lay_vec4 child_margins = pchild->margins; + lay_vec4 child_rect = ctx->rects[child]; + + switch (b_flags & LAY_HFILL) { + case LAY_HCENTER: + child_rect[dim] += (space - child_rect[2 + dim]) / 2 - child_margins[wdim]; + break; + case LAY_RIGHT: + child_rect[dim] += space - child_rect[2 + dim] - child_margins[wdim]; + break; + case LAY_HFILL: + child_rect[2 + dim] = lay_scalar_max(0, space - child_rect[dim] - child_margins[wdim]); + break; + default: + break; + } + + child_rect[dim] += offset; + ctx->rects[child] = child_rect; + child = pchild->next_sibling; + } +} + +static LAY_FORCE_INLINE +void lay_arrange_overlay_squeezed_range( + lay_context *ctx, int dim, + lay_id start_item, lay_id end_item, + lay_scalar offset, lay_scalar space) +{ + int wdim = dim + 2; + lay_id item = start_item; + while (item != end_item) { + lay_item_t *pitem = lay_item_get(ctx, item); + const uint32_t b_flags = (pitem->flags & LAY_ITEM_LAYOUT_MASK) >> dim; + const lay_vec4 margins = pitem->margins; + lay_vec4 rect = ctx->rects[item]; + lay_scalar min_size = lay_scalar_max(0, space - rect[dim] - margins[wdim]); + switch (b_flags & LAY_HFILL) { + case LAY_HCENTER: + rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size); + rect[dim] += (space - rect[2 + dim]) / 2 - margins[wdim]; + break; + case LAY_RIGHT: + rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size); + rect[dim] = space - rect[2 + dim] - margins[wdim]; + break; + case LAY_HFILL: + rect[2 + dim] = min_size; + break; + default: + rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size); + break; + } + rect[dim] += offset; + ctx->rects[item] = rect; + item = pitem->next_sibling; + } +} + +static LAY_FORCE_INLINE +lay_scalar lay_arrange_wrapped_overlay_squeezed( + lay_context *ctx, lay_id item, int dim) +{ + const int wdim = dim + 2; + lay_item_t *pitem = lay_item_get(ctx, item); + lay_scalar offset = ctx->rects[item][dim]; + lay_scalar need_size = 0; + lay_id child = pitem->first_child; + lay_id start_child = child; + while (child != LAY_ID_INVALID) { + lay_item_t *pchild = lay_item_get(ctx, child); + if (pchild->flags & LAY_BREAK) { + lay_arrange_overlay_squeezed_range(ctx, dim, start_child, child, offset, need_size); + offset += need_size; + start_child = child; + need_size = 0; + } + const lay_vec4 rect = ctx->rects[child]; + lay_scalar child_size = rect[dim] + rect[2 + dim] + pchild->margins[wdim]; + need_size = lay_scalar_max(need_size, child_size); + child = pchild->next_sibling; + } + lay_arrange_overlay_squeezed_range(ctx, dim, start_child, LAY_ID_INVALID, offset, need_size); + offset += need_size; + return offset; +} + +static void lay_arrange(lay_context *ctx, lay_id item, int dim) +{ + lay_item_t *pitem = lay_item_get(ctx, item); + + const uint32_t flags = pitem->flags; + switch (flags & LAY_ITEM_BOX_MODEL_MASK) { + case LAY_COLUMN | LAY_WRAP: + if (dim != 0) { + lay_arrange_stacked(ctx, item, 1, true); + lay_scalar offset = lay_arrange_wrapped_overlay_squeezed(ctx, item, 0); + ctx->rects[item][2 + 0] = offset - ctx->rects[item][0]; + } + break; + case LAY_ROW | LAY_WRAP: + if (dim == 0) + lay_arrange_stacked(ctx, item, 0, true); + else + // discard return value + lay_arrange_wrapped_overlay_squeezed(ctx, item, 1); + break; + case LAY_COLUMN: + case LAY_ROW: + if ((flags & 1) == (uint32_t)dim) { + lay_arrange_stacked(ctx, item, dim, false); + } else { + const lay_vec4 rect = ctx->rects[item]; + lay_arrange_overlay_squeezed_range( + ctx, dim, pitem->first_child, LAY_ID_INVALID, + rect[dim], rect[2 + dim]); + } + break; + default: + lay_arrange_overlay(ctx, item, dim); + break; + } + lay_id child = pitem->first_child; + while (child != LAY_ID_INVALID) { + // NOTE: this is recursive and will run out of stack space if items are + // nested too deeply. + lay_arrange(ctx, child, dim); + lay_item_t *pchild = lay_item_get(ctx, child); + child = pchild->next_sibling; + } +} diff --git a/layout.h b/layout.h new file mode 100644 index 0000000..966bd5a --- /dev/null +++ b/layout.h @@ -0,0 +1,253 @@ +#pragma once + +#include +#include +#include + +#ifndef LAY_EXPORT +#define LAY_EXPORT extern +#endif + +// 'static inline' for things we always want inlined -- the compiler should not +// even have to consider not inlining these. +#if defined(__GNUC__) +#define LAY_STATIC_INLINE __attribute__((always_inline)) static inline +#elif defined(_MSC_VER) +#define LAY_STATIC_INLINE __forceinline static +#else +#define LAY_STATIC_INLINE inline static +#endif + +#define LAY_ASSERT assert + +typedef uint32_t lay_id; +#if LAY_FLOAT == 1 +typedef float lay_scalar; +#else +typedef int16_t lay_scalar; +#endif + +#if defined(__GNUC__) +#if LAY_FLOAT == 1 +typedef float lay_vec4 __attribute__ ((__vector_size__ (16), aligned(16))); +typedef float lay_vec2 __attribute__ ((__vector_size__ (8), aligned(8))); +#else +typedef int16_t lay_vec4 __attribute__ ((__vector_size__ (8), aligned(8))); +typedef int16_t lay_vec2 __attribute__ ((__vector_size__ (4), aligned(4))); +#endif // LAY_FLOAT +#elif defined(_MSC_VER) +struct lay_vec4 { + lay_scalar xyzw[4]; + const lay_scalar& operator[](int index) const + { return xyzw[index]; } + lay_scalar& operator[](int index) + { return xyzw[index]; } +}; +struct lay_vec2 { + lay_scalar xy[2]; + const lay_scalar& operator[](int index) const + { return xy[index]; } + lay_scalar& operator[](int index) + { return xy[index]; } +}; +#endif // __GNUC__ or _MSC_VER + +#ifndef LAY_ID_INVALID +#define LAY_ID_INVALID UINT32_MAX +#endif + +typedef struct lay_item_t { + uint32_t flags; + lay_id first_child; + lay_id next_sibling; + lay_vec4 margins; + lay_vec2 size; +#if defined(__GNUC__) +} __attribute__ ((aligned (16))) lay_item_t; +#else +} lay_item_t; +#endif + +typedef struct lay_context { + lay_item_t *items; + lay_vec4 *rects; + lay_id capacity; + lay_id count; +} lay_context; + +// container flags to pass to uiSetBox() +typedef enum lay_box_flags { + // flex-direction (bit 0+1) + + // left to right + LAY_ROW = 0x002, + // top to bottom + LAY_COLUMN = 0x003, + + // model (bit 1) + + // free layout + LAY_LAYOUT = 0x000, + // flex model + LAY_FLEX = 0x002, + + // flex-wrap (bit 2) + + // single-line + LAY_NOWRAP = 0x000, + // multi-line, wrap left to right + LAY_WRAP = 0x004, + + + // justify-content (start, end, center, space-between) + // at start of row/column + LAY_START = 0x008, + // at center of row/column + LAY_MIDDLE = 0x000, + // at end of row/column + LAY_END = 0x010, + // insert spacing to stretch across whole row/column + LAY_JUSTIFY = 0x018, + + // align-items + // can be implemented by putting a flex container in a layout container, + // then using LAY_TOP, LAY_BOTTOM, LAY_VFILL, LAY_VCENTER, etc. + // FILL is equivalent to stretch/grow + + // align-content (start, end, center, stretch) + // can be implemented by putting a flex container in a layout container, + // then using LAY_TOP, LAY_BOTTOM, LAY_VFILL, LAY_VCENTER, etc. + // FILL is equivalent to stretch; space-between is not supported. +} lay_box_flags; + +// child layout flags to pass to uiSetLayout() +typedef enum lay_layout_flags { + // attachments (bit 5-8) + // fully valid when parent uses LAY_LAYOUT model + // partially valid when in LAY_FLEX model + + // anchor to left item or left side of parent + LAY_LEFT = 0x020, + // anchor to top item or top side of parent + LAY_TOP = 0x040, + // anchor to right item or right side of parent + LAY_RIGHT = 0x080, + // anchor to bottom item or bottom side of parent + LAY_BOTTOM = 0x100, + // anchor to both left and right item or parent borders + LAY_HFILL = 0x0a0, + // anchor to both top and bottom item or parent borders + LAY_VFILL = 0x140, + // center horizontally, with left margin as offset + LAY_HCENTER = 0x000, + // center vertically, with top margin as offset + LAY_VCENTER = 0x000, + // center in both directions, with left/top margin as offset + LAY_CENTER = 0x000, + // anchor to all four directions + LAY_FILL = 0x1e0, + // when wrapping, put this element on a new line + // wrapping layout code auto-inserts LAY_BREAK flags, + // drawing routines can read them with uiGetLayout() + LAY_BREAK = 0x200 +} lay_layout_flags; + +enum { + // these bits, starting at bit 16, can be safely assigned by the + // application, e.g. as item types, other event types, drop targets, etc. + // they can be set and queried using uiSetFlags() and uiGetFlags() + // + // (In reality we have more free bits than this, see what gets used in the + // masks) + LAY_USERMASK = 0xffff0000, + + // a special mask passed to uiFindItem() + LAY_ANY = 0xffffffff, +}; + +enum { + // extra item flags + + // bit 0-2 + LAY_ITEM_BOX_MODEL_MASK = 0x000007, + // bit 0-4 + LAY_ITEM_BOX_MASK = 0x00001F, + // bit 5-9 + LAY_ITEM_LAYOUT_MASK = 0x0003E0, + // item has been inserted (bit 10) + LAY_ITEM_INSERTED = 0x400, + // horizontal size has been explicitly set (bit 11) + LAY_ITEM_HFIXED = 0x800, + // vertical size has been explicitly set (bit 12) + LAY_ITEM_VFIXED = 0x1000, + // bit 11-12 + LAY_ITEM_FIXED_MASK = LAY_ITEM_HFIXED | LAY_ITEM_VFIXED, + + // which flag bits will be compared + LAY_ITEM_COMPARE_MASK = LAY_ITEM_BOX_MODEL_MASK + | (LAY_ITEM_LAYOUT_MASK & ~LAY_BREAK) + | LAY_USERMASK, +}; + +LAY_STATIC_INLINE lay_vec4 lay_vec4_xyzw(lay_scalar x, lay_scalar y, lay_scalar z, lay_scalar w) +{ +#if defined(__GNUC__) + return (lay_vec4){x, y, z, w}; +#elif defined(_MSC_VER) + lay_vec4 result; + result[0] = x; + result[1] = y; + result[2] = z; + result[3] = w; + return result; +#endif +} + +LAY_EXPORT void lay_context_init(lay_context *ctx); +LAY_EXPORT void lay_context_reserve(lay_context *ctx, lay_id count); +LAY_EXPORT void lay_context_destroy(lay_context *ctx); + +LAY_EXPORT void lay_context_reset(lay_context *ctx); +LAY_EXPORT void lay_context_run(lay_context *ctx); + +LAY_EXPORT lay_id lay_context_item_count(lay_context *ctx); +LAY_EXPORT lay_id lay_context_item_capacity(lay_context *ctx); + +LAY_EXPORT lay_id lay_item(lay_context *ctx); +LAY_EXPORT void lay_append(lay_context *ctx, lay_id earlier, lay_id later); +LAY_EXPORT void lay_insert(lay_context *ctx, lay_id parent, lay_id child); +LAY_EXPORT void lay_push(lay_context *ctx, lay_id parent, lay_id child); +LAY_EXPORT lay_vec2 lay_size_get(lay_context *ctx, lay_id item); +LAY_EXPORT void lay_size_set_xy(lay_context *ctx, lay_id item, lay_scalar width, lay_scalar height); +LAY_EXPORT void lay_contain_set(lay_context *ctx, lay_id item, uint32_t flags); +LAY_EXPORT void lay_behave_set(lay_context *ctx, lay_id item, uint32_t flags); +LAY_EXPORT lay_vec4 lay_margins_get(lay_context *ctx, lay_id item); +LAY_EXPORT void lay_margins_set(lay_context *ctx, lay_id item, lay_vec4 ltrb); +LAY_EXPORT void lay_margins_set_ltrb(lay_context *ctx, lay_id item, lay_scalar l, lay_scalar t, lay_scalar r, lay_scalar b); + +LAY_STATIC_INLINE lay_item_t *lay_item_get(const lay_context *ctx, lay_id id) +{ + LAY_ASSERT(id != LAY_ID_INVALID && id < ctx->count); + return ctx->items + id; +} + +LAY_STATIC_INLINE lay_id lay_first_child(const lay_context *ctx, lay_id id) +{ + const lay_item_t *pitem = lay_item_get(ctx, id); + return pitem->first_child; +} + +LAY_STATIC_INLINE lay_id lay_next_sibling(const lay_context *ctx, lay_id id) +{ + const lay_item_t *pitem = lay_item_get(ctx, id); + return pitem->next_sibling; +} + +LAY_STATIC_INLINE lay_vec4 lay_rect_get(const lay_context *ctx, lay_id id) +{ + LAY_ASSERT(id != LAY_ID_INVALID && id < ctx->count); + return ctx->rects[id]; +} + +#undef LAY_EXPORT +#undef LAY_STATIC_INLINE diff --git a/luamodule_layout.c b/luamodule_layout.c new file mode 100644 index 0000000..deb7c15 --- /dev/null +++ b/luamodule_layout.c @@ -0,0 +1,284 @@ +#include "layout.c" +#ifdef __cplusplus +extern "C" { +#endif +#include "lauxlib.h" +#ifdef __cplusplus +} +#endif + +#if LAY_FLOAT == 1 +#define LUALAY_CHECK_SCALAR luaL_checknumber +#define LUALAY_PUSH_SCALAR lua_pushnumber +#else +#define LUALAY_CHECK_SCALAR luaL_checkinteger +#define LUALAY_PUSH_SCALAR lua_pushinteger +#endif + +int lualay_context_new(lua_State* L) +{ + lua_settop(L, 0); + lay_context *ctx = (lay_context*)lua_newuserdata(L, sizeof(lay_context)); + lay_context_init(ctx); + + luaL_getmetatable(L, "Layout"); + lua_setmetatable(L, -2); + return 1; +} + +lay_context *lualay_context_check(lua_State *L) +{ + void *ud = luaL_checkudata(L, 1, "Layout"); + luaL_argcheck(L, ud != NULL, 1, "`Layout` expected"); + return (lay_context*)ud; +} + +lay_id lualay_id_check(lua_State *L, lay_context *ctx, int pos) +{ + int n = (int)luaL_checkinteger(L, pos); + luaL_argcheck(L, n >= 0, pos, "Item id must be non-negative"); + luaL_argcheck(L, (lay_id)n <= ctx->count, pos, "Item id is not in valid range for layout context"); + return (lay_id)n; +} + +int lualay_context_gc(lua_State* L) +{ + lay_context* ctx = lualay_context_check(L); + lay_context_destroy(ctx); + return 0; +} + +int lualay_context_capacity(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lua_settop(L, 0); + lay_id cap = lay_context_item_capacity(ctx); + if (cap > (lay_id)INT_MAX) + lua_pushinteger(L, INT_MAX); + else + lua_pushinteger(L, (int)cap); + return 1; +} + +int lualay_context_reserve(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + int n = (int)luaL_checkinteger(L, 2); + luaL_argcheck(L, n >= 0, 2, "Zero or positive integer capacity expected"); + lay_context_reserve(ctx, (lay_id)n); + return 0; +} + +int lualay_item_new(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lua_pushinteger(L, lay_item(ctx)); + return 1; +} + +int lualay_context_run(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_context_run(ctx); + return 0; +} + +int lualay_context_reset(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_context_reset(ctx); + return 0; +} + +int lualay_item_insert(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id parent = lualay_id_check(L, ctx, 2); + lay_id child = lualay_id_check(L, ctx, 3); + lay_insert(ctx, parent, child); + return 0; +} + +int lualay_item_append(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id earlier = lualay_id_check(L, ctx, 2); + lay_id later = lualay_id_check(L, ctx, 3); + lay_append(ctx, earlier, later); + return 0; +} + +int lualay_item_push(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id parent = lualay_id_check(L, ctx, 2); + lay_id child = lualay_id_check(L, ctx, 3); + lay_push(ctx, parent, child); + return 0; +} + +int lualay_item_size_set(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + lay_scalar w = (lay_scalar)LUALAY_CHECK_SCALAR(L, 3); + lay_scalar h = (lay_scalar)LUALAY_CHECK_SCALAR(L, 4); + lay_size_set_xy(ctx, item, w, h); + return 0; +} + +int lualay_item_margins_set(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + lay_scalar l = (lay_scalar)LUALAY_CHECK_SCALAR(L, 3); + lay_scalar t = (lay_scalar)LUALAY_CHECK_SCALAR(L, 4); + lay_scalar r = (lay_scalar)LUALAY_CHECK_SCALAR(L, 5); + lay_scalar b = (lay_scalar)LUALAY_CHECK_SCALAR(L, 6); + lay_margins_set_ltrb(ctx, item, l, t, r, b); + return 0; +} + +int lualay_rect_get(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + lay_vec4 rect = lay_rect_get(ctx, item); + LUALAY_PUSH_SCALAR(L, rect[0]); + LUALAY_PUSH_SCALAR(L, rect[1]); + LUALAY_PUSH_SCALAR(L, rect[2]); + LUALAY_PUSH_SCALAR(L, rect[3]); + return 4; +} + +// TODO make varargs + +int lualay_item_contain_set(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + uint32_t flags = 0; + const int n = lua_gettop(L); + for (int i = 3; i <= n; ++i) { + flags |= luaL_checkinteger(L, i); + luaL_argcheck(L, (flags & LAY_ITEM_BOX_MASK) == flags, i, "Invalid container flag"); + } + lay_contain_set(ctx, item, flags); + return 0; +} +int lualay_item_behave_set(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + uint32_t flags = 0; + const int n = lua_gettop(L); + for (int i = 3; i <= n; ++i) { + flags |= luaL_checkinteger(L, i); + luaL_argcheck(L, (flags & LAY_ITEM_LAYOUT_MASK) == flags, i, "Invalid behavior flag"); + } + lay_behave_set(ctx, item, flags); + return 0; +} + +int lualay_item_next_sibling(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + lay_id id_next = lay_next_sibling(ctx, item); + lua_settop(L, 0); + // lay_id is uint32_t by default, and LAY_ID_INVALID is uint32_t max, so we + // need to check if it's out of range + if (id_next >= (lay_id)INT_MAX) + lua_pushinteger(L, -1); + else + lua_pushinteger(L, (lay_id)id_next); + return 1; +} + +int lualay_item_first_child(lua_State* L) +{ + lay_context *ctx = lualay_context_check(L); + lay_id item = lualay_id_check(L, ctx, 2); + lay_id id_child = lay_first_child(ctx, item); + lua_settop(L, 0); + if (id_child >= (lay_id)INT_MAX) + lua_pushinteger(L, -1); + else + lua_pushinteger(L, (lay_id)id_child); + return 1; +} + +static const struct luaL_reg laylib[] = { + {"new", lualay_context_new}, + {"run", lualay_context_run}, + {"reset", lualay_context_reset}, + {"capacity", lualay_context_capacity}, + {"reserve", lualay_context_reserve}, + {"item", lualay_item_new}, + {"insert", lualay_item_insert}, + {"append", lualay_item_append}, + {"push", lualay_item_push}, + {"set_size", lualay_item_size_set}, + {"set_margins", lualay_item_margins_set}, + {"set_contain", lualay_item_contain_set}, + {"set_behave", lualay_item_behave_set}, + {"rect", lualay_rect_get}, + {"next_sibling", lualay_item_next_sibling}, + {"first_child", lualay_item_first_child}, + {"__gc", lualay_context_gc}, + {NULL, NULL} +}; + +#ifdef __cplusplus +extern "C" __declspec(dllexport) int luaopen_layout(lua_State* L) +#else +__declspec(dllexport) int luaopen_layout(lua_State* L) +#endif +{ + // TODO update existing if hot reloading + luaL_newmetatable(L, "Layout"); + lua_pushvalue(L, -1); + + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + + luaL_openlib(L, NULL, laylib, 0); + + lua_setglobal(L, "Layout"); + + // Set integer flag constants + +#define LUALAY_VAL(flagsym, fieldname) \ + lua_pushinteger(L, flagsym); \ + lua_setfield(L, -2, "" #fieldname ""); + + LUALAY_VAL(LAY_ROW, ROW); + LUALAY_VAL(LAY_COLUMN, COLUMN); + LUALAY_VAL(LAY_LAYOUT, OVERLAY); + LUALAY_VAL(LAY_FLEX, FLEX); + LUALAY_VAL(LAY_NOWRAP, NOWRAP); + LUALAY_VAL(LAY_WRAP, WRAP); + LUALAY_VAL(LAY_START, START); + LUALAY_VAL(LAY_MIDDLE, MIDDLE); + LUALAY_VAL(LAY_END, END); + LUALAY_VAL(LAY_JUSTIFY, JUSTIFY); + + LUALAY_VAL(LAY_LEFT, LEFT); + LUALAY_VAL(LAY_TOP, TOP); + LUALAY_VAL(LAY_RIGHT, RIGHT); + LUALAY_VAL(LAY_BOTTOM, BOTTOM); + LUALAY_VAL(LAY_HFILL, HFILL); + LUALAY_VAL(LAY_VFILL, VFILL); + LUALAY_VAL(LAY_HCENTER, HCENTER); + LUALAY_VAL(LAY_VCENTER, VCENTER); + LUALAY_VAL(LAY_CENTER, CENTER); + LUALAY_VAL(LAY_FILL, FILL); + LUALAY_VAL(LAY_BREAK, BREAK); + +#undef LUALAY_VAL + + // TODO flags for preventing being squished (HFIXED/VFIXED) + + return 0; +} diff --git a/test_layout.c b/test_layout.c new file mode 100644 index 0000000..3d8a0ff --- /dev/null +++ b/test_layout.c @@ -0,0 +1,910 @@ +#include +#include "layout.c" + +#ifdef _WIN32 +#include + +LONG WINAPI LayTestUnhandledExceptionFilter(EXCEPTION_POINTERS *ExceptionInfo) +{ + fputs("Exception", stdout); + fflush(stdout); + // TODO Do useful stuff + return EXCEPTION_EXECUTE_HANDLER; +} + +#endif + +/* +int sprint_vec4(char* str, lay_vec4 v) +{ + return sprintf(str, "%d, %d, %d, %d", v[0], v[1], v[2], v[3]); +} +int print_vec4(lay_vec4 v) +{ + return printf("%d, %d, %d, %d", v[0], v[1], v[2], v[3]); +} +int sprint_vec2(char* str, lay_vec2 v) +{ + return sprintf(str, "%d, %d", v[0], v[1]); +} +int sprint_item_info(char* str, lay_context *ctx, lay_id item) +{ + int offs = 0; + lay_vec4 item_rect = lay_rect_get(ctx, item); + + lay_vec4 item_margins = lay_margins_get(ctx, item); + lay_vec2 item_size = lay_size_get(ctx, item); + + offs += sprintf(str + offs, "rect: "); + offs += sprint_vec4(str + offs, item_rect); + + offs += sprintf(str + offs, " | margins: "); + offs += sprint_vec4(str + offs, item_margins); + + offs += sprintf(str + offs, " | size: "); + offs += sprint_vec2(str + offs, item_size); + + return offs; +} +*/ + +#define LTEST_TRUE(cond) \ + if (cond) {} \ + else { \ + printf("Failed test at line %d in %s", __LINE__, __func__); \ + abort(); \ + } +#define LTEST_FALSE(cond) \ + if (cond) { \ + printf("Failed test at line %d in %s", __LINE__, __func__); \ + abort(); \ + } \ + else {} + +#define LTEST_DECLARE(testname) \ + static void test_##testname(lay_context *ctx, char *strbuf) + +// vec4 equals test +#define LTEST_VEC4EQ(vecvar, x, y, z, w) \ + LTEST_TRUE(vecvar[0] == x && vecvar[1] == y && vecvar[2] == z && vecvar[3] == w) +#define LTEST_VEC4UNEQ(vecvar, x, y, z, w) \ + LTEST_FALSE(vecvar[0] == x && vecvar[1] == y && vecvar[2] == z && vecvar[3] == w) + +LTEST_DECLARE(testing_sanity) +{ + // MSVC (rightly) warns us that these are constant expressions which are + // always or never true. +#ifndef _MSC_VER + LTEST_TRUE(1); + LTEST_TRUE(-1); + LTEST_TRUE(((bool)(1))); + LTEST_TRUE(!((bool)(0))); + LTEST_TRUE(34518 == 34518); + LTEST_TRUE(1337 != 420); + + LTEST_FALSE(0); + LTEST_FALSE(!((bool)(1))); + LTEST_FALSE(((bool)(0))); + LTEST_FALSE(34518 != 34518); + LTEST_FALSE(1337 == 420); +#endif + + lay_vec4 v1 = lay_vec4_xyzw(56, 57, 58, 59); + LTEST_VEC4EQ(v1, 56, 57, 58, 59); + LTEST_VEC4UNEQ(v1, 55, 57, 58, 59); + LTEST_VEC4UNEQ(v1, 55, 57, 58, 59); + + LTEST_TRUE(ctx != NULL); +} + +LTEST_DECLARE(simple_fill) +{ + lay_id root = lay_item(ctx); + lay_id child = lay_item(ctx); + + lay_size_set_xy(ctx, root, 30, 40); + lay_behave_set(ctx, child, LAY_FILL); + lay_insert(ctx, root, child); + + lay_context_run(ctx); + + lay_vec4 root_r = lay_rect_get(ctx, root); + lay_vec4 child_r = lay_rect_get(ctx, child); + + LTEST_TRUE(root_r[0] == 0 && root_r[1] == 0); + LTEST_TRUE(root_r[2] == 30 && root_r[3] == 40); + + LTEST_TRUE(child_r[0] == 0 && child_r[1] == 0); + LTEST_TRUE(child_r[2] == 30 && child_r[3] == 40); +} + +LTEST_DECLARE(reserve_capacity) +{ + lay_context_reserve(ctx, 512); + LTEST_TRUE(lay_context_item_capacity(ctx) >= 512); + + // Run some simple stuff like above, just to make sure it's still working + + lay_id root = lay_item(ctx); + lay_id child = lay_item(ctx); + + lay_size_set_xy(ctx, root, 30, 40); + lay_behave_set(ctx, child, LAY_FILL); + lay_insert(ctx, root, child); + + lay_context_run(ctx); + + lay_vec4 root_r = lay_rect_get(ctx, root); + lay_vec4 child_r = lay_rect_get(ctx, child); + + LTEST_TRUE(root_r[0] == 0 && root_r[1] == 0); + LTEST_TRUE(root_r[2] == 30 && root_r[3] == 40); + + LTEST_TRUE(child_r[0] == 0 && child_r[1] == 0); + LTEST_TRUE(child_r[2] == 30 && child_r[3] == 40); + + LTEST_TRUE(lay_context_item_capacity(ctx) >= 512); +} + +LTEST_DECLARE(multiple_uninserted) +{ + lay_id root = lay_item(ctx); + lay_id child1 = lay_item(ctx); + lay_id child2 = lay_item(ctx); + + lay_size_set_xy(ctx, root, 155, 177); + lay_size_set_xy(ctx, child2, 1, 1); + + lay_context_run(ctx); + + lay_vec4 root_r, child1_r, child2_r; + root_r = lay_rect_get(ctx, root); + child1_r = lay_rect_get(ctx, child1); + child2_r = lay_rect_get(ctx, child2); + + LTEST_VEC4EQ(root_r, 0, 0, 155, 177); + // This uninserted child should obviously be zero: + LTEST_VEC4EQ(child1_r, 0, 0, 0, 0); + // You might expect this to pass for this child: + // + // LTEST_VEC4EQ(child2_r, 0, 0, 1, 1); + // + // But it won't, because items not inserted into the root will not have + // their output rect set. (Hmm, is this a good API design idea?) + // + // Instead, it will be zero: + LTEST_VEC4EQ(child2_r, 0, 0, 0, 0); +} + +LTEST_DECLARE(column_even_fill) +{ + lay_id root = lay_item(ctx); + lay_id child_a = lay_item(ctx); + lay_id child_b = lay_item(ctx); + lay_id child_c = lay_item(ctx); + + lay_size_set_xy(ctx, root, 50, 60); + lay_contain_set(ctx, root, LAY_COLUMN); + lay_behave_set(ctx, child_a, LAY_FILL); + lay_behave_set(ctx, child_b, LAY_FILL); + lay_behave_set(ctx, child_c, LAY_FILL); + lay_size_set_xy(ctx, child_a, 0, 0); + lay_size_set_xy(ctx, child_b, 0, 0); + lay_size_set_xy(ctx, child_c, 0, 0); + lay_insert(ctx, root, child_a); + lay_insert(ctx, root, child_b); + lay_insert(ctx, root, child_c); + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, root), 0, 0, 50, 60); + LTEST_VEC4EQ(lay_rect_get(ctx, child_a), 0, 0, 50, 20); + LTEST_VEC4EQ(lay_rect_get(ctx, child_b), 0, 20, 50, 20); + LTEST_VEC4EQ(lay_rect_get(ctx, child_c), 0, 40, 50, 20); +} + +LTEST_DECLARE(row_even_fill) +{ + lay_id root = lay_item(ctx); + lay_id child_a = lay_item(ctx); + lay_id child_b = lay_item(ctx); + lay_id child_c = lay_item(ctx); + + lay_size_set_xy(ctx, root, 90, 3); + lay_contain_set(ctx, root, LAY_ROW); + lay_behave_set(ctx, child_a, LAY_HFILL | LAY_TOP); + lay_behave_set(ctx, child_b, LAY_HFILL | LAY_VCENTER); + lay_behave_set(ctx, child_c, LAY_HFILL | LAY_BOTTOM); + lay_size_set_xy(ctx, child_a, 0, 1); + lay_size_set_xy(ctx, child_b, 0, 1); + lay_size_set_xy(ctx, child_c, 0, 1); + lay_insert(ctx, root, child_a); + lay_insert(ctx, root, child_b); + lay_insert(ctx, root, child_c); + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, root), 0, 0, 90, 3); + LTEST_VEC4EQ(lay_rect_get(ctx, child_a), 0, 0, 30, 1); + LTEST_VEC4EQ(lay_rect_get(ctx, child_b), 30, 1, 30, 1); + LTEST_VEC4EQ(lay_rect_get(ctx, child_c), 60, 2, 30, 1); +} + +LTEST_DECLARE(fixed_and_fill) +{ + lay_id root = lay_item(ctx); + lay_id fixed_a = lay_item(ctx); + lay_id fixed_b = lay_item(ctx); + lay_id filler = lay_item(ctx); + + lay_contain_set(ctx, root, LAY_COLUMN); + + lay_size_set_xy(ctx, root, 50, 60); + lay_size_set_xy(ctx, fixed_a, 50, 15); + lay_size_set_xy(ctx, fixed_b, 50, 15); + lay_behave_set(ctx, filler, LAY_FILL); + + lay_insert(ctx, root, fixed_a); + lay_insert(ctx, root, filler); + lay_insert(ctx, root, fixed_b); + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, root), 0, 0, 50, 60); + LTEST_VEC4EQ(lay_rect_get(ctx, fixed_a), 0, 0, 50, 15); + LTEST_VEC4EQ(lay_rect_get(ctx, filler), 0, 15, 50, 30); + LTEST_VEC4EQ(lay_rect_get(ctx, fixed_b), 0, 45, 50, 15); +} + +LTEST_DECLARE(simple_margins_1) +{ + lay_id root = lay_item(ctx); + lay_id child_a = lay_item(ctx); + lay_id child_b = lay_item(ctx); + lay_id child_c = lay_item(ctx); + + lay_contain_set(ctx, root, LAY_COLUMN); + lay_behave_set(ctx, child_a, LAY_HFILL); + lay_behave_set(ctx, child_b, LAY_FILL); + lay_behave_set(ctx, child_c, LAY_HFILL); + + lay_size_set_xy(ctx, root, 100, 90); + + lay_margins_set_ltrb(ctx, child_a, 3, 5, 7, 10); + lay_size_set_xy(ctx, child_a, 0, (30 - (5 + 10))); + lay_size_set_xy(ctx, child_c, 0, 30); + + lay_insert(ctx, root, child_a); + lay_insert(ctx, root, child_b); + lay_insert(ctx, root, child_c); + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, child_a), 3, 5, 90, (5 + 10)); + LTEST_VEC4EQ(lay_rect_get(ctx, child_b), 0, 30, 100, 30); + LTEST_VEC4EQ(lay_rect_get(ctx, child_c), 0, 60, 100, 30); +} + +LTEST_DECLARE(nested_boxes_1) +{ + const size_t num_rows = 5; + // one of the rows is "fake" and will have 0 units tall height + const size_t num_rows_with_height = num_rows - 1; + + lay_id root = lay_item(ctx); + // main_child is a column that contains rows, and those rows + // will contain columns. + lay_id main_child = lay_item(ctx); + lay_size_set_xy(ctx, root, + 70, + // 10 units extra size above and below for main_child + // margin + (num_rows_with_height * 10 + 2 * 10) + ); + lay_margins_set_ltrb(ctx, main_child, 10, 10, 10, 10); + lay_contain_set(ctx, main_child, LAY_COLUMN); + lay_insert(ctx, root, main_child); + lay_behave_set(ctx, main_child, LAY_FILL); + + lay_id *rows = (lay_id*)calloc(num_rows, sizeof(lay_id)); + + // auto-filling columns-in-row, each one should end up being + // 10 units wide + rows[0] = lay_item(ctx); + lay_contain_set(ctx, rows[0], LAY_ROW); + lay_behave_set(ctx, rows[0], LAY_FILL); + lay_id cols1[5]; + // hmm so both the row and its child columns need to be set to + // fill? which means main_child also needs to be set to fill? + for (uint16_t i = 0; i < 5; ++i) { + lay_id col = lay_item(ctx); + // fill empty space + lay_behave_set(ctx, col, LAY_FILL); + lay_insert(ctx, rows[0], col); + cols1[i] = col; + } + + rows[1] = lay_item(ctx); + lay_contain_set(ctx, rows[1], LAY_ROW); + lay_behave_set(ctx, rows[1], LAY_VFILL); + lay_id cols2[5]; + for (uint16_t i = 0; i < 5; ++i) { + lay_id col = lay_item(ctx); + // fixed-size horizontally, fill vertically + lay_size_set_xy(ctx, col, 10, 0); + lay_behave_set(ctx, col, LAY_VFILL); + lay_insert(ctx, rows[1], col); + cols2[i] = col; + } + + // these columns have an inner item which sizes them + rows[2] = lay_item(ctx); + lay_contain_set(ctx, rows[2], LAY_ROW); + lay_id cols3[2]; + for (uint16_t i = 0; i < 2; ++i) { + lay_id col = lay_item(ctx); + lay_id inner_sizer = lay_item(ctx); + // only the second one will have height + lay_size_set_xy(ctx, inner_sizer, 25, (lay_scalar)10 * i); + // align to bottom, only should make a difference for + // first item + lay_behave_set(ctx, col, LAY_BOTTOM); + lay_insert(ctx, col, inner_sizer); + lay_insert(ctx, rows[2], col); + cols3[i] = col; + } + + // row 4 should end up being 0 units tall after layout + rows[3] = lay_item(ctx); + lay_contain_set(ctx, rows[3], LAY_ROW); + lay_behave_set(ctx, rows[3], LAY_HFILL); + lay_id cols4[99]; + for (uint16_t i = 0; i < 99; ++i) { + lay_id col = lay_item(ctx); + lay_insert(ctx, rows[3], col); + cols4[i] = col; + } + + // row 5 should be 10 pixels tall after layout, and each of + // its columns should be 1 pixel wide + rows[4] = lay_item(ctx); + lay_contain_set(ctx, rows[4], LAY_ROW); + lay_behave_set(ctx, rows[4], LAY_FILL); + lay_id cols5[50]; + for (uint16_t i = 0; i < 50; ++i) { + lay_id col = lay_item(ctx); + lay_behave_set(ctx, col, LAY_FILL); + lay_insert(ctx, rows[4], col); + cols5[i] = col; + } + + for (uint16_t i = 0; i < num_rows; ++i) { + lay_insert(ctx, main_child, rows[i]); + } + + // Repeat the run and tests multiple times to make sure we get the expected + // results each time. The original version of oui would overwrite its input + // state (intentionally) with the output state, so the context's input data + // (margins, size) had to be "rebuilt" by the client code by doing a reset + // and then filling it back up for each run. 'lay' does not have that + // restriction. + // + // This is one of the more complex tests, so it's a good + // choice for testing multiple runs of the same context. + for (uint32_t run_n = 0; run_n < 5; ++run_n) { + printf(" Iteration #%d\n", run_n + 1); + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, main_child), 10, 10, 50, 40); + // These rows should all be 10 units in height + LTEST_VEC4EQ(lay_rect_get(ctx, rows[0]), 10, 10, 50, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, rows[1]), 10, 20, 50, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, rows[2]), 10, 30, 50, 10); + // this row should have 0 height + LTEST_VEC4EQ(lay_rect_get(ctx, rows[3]), 10, 40, 50, 0); + LTEST_VEC4EQ(lay_rect_get(ctx, rows[4]), 10, 40, 50, 10); + + for (int16_t i = 0; i < 5; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols1[i]); + // each of these should be 10 units wide, and stacked + // horizontally + LTEST_VEC4EQ(r, 10 + 10 * i, 10, 10, 10); + } + + // the cols in the second row are similar to first row + for (int16_t i = 0; i < 5; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols2[i]); + LTEST_VEC4EQ(r, 10 + 10 * i, 20, 10, 10); + } + + // leftmost (first of two items), aligned to bottom of row, 0 + // units tall + LTEST_VEC4EQ(lay_rect_get(ctx, cols3[0]), 10, 40, 25, 0); + // rightmost (second of two items), same height as row, which + // is 10 units tall + LTEST_VEC4EQ(lay_rect_get(ctx, cols3[1]), 35, 30, 25, 10); + + // these should all have size 0 and be in the middle of the + // row + for (int16_t i = 0; i < 99; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols4[i]); + LTEST_VEC4EQ(r, 25 + 10, 40, 0, 0); + } + + // these should all be 1 unit wide and 10 units tall + for (int16_t i = 0; i < 50; ++i) { + lay_vec4 r = lay_rect_get(ctx, cols5[i]); + LTEST_VEC4EQ(r, 10 + i, 40, 1, 10); + } + } + + free(rows); +} + +LTEST_DECLARE(deep_nest_1) +{ + lay_id root = lay_item(ctx); + + const int16_t num_items = 500; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + lay_id parent = root; + for (int16_t i = 0; i < num_items; ++i) + { + lay_id item = lay_item(ctx); + lay_insert(ctx, parent, item); + parent = item; + } + + lay_size_set_xy(ctx, parent, 77, 99); + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, root), 0, 0, 77, 99) + + free(items); +} + +LTEST_DECLARE(many_children_1) +{ + const int16_t num_items = 20000; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 1, 0); + lay_contain_set(ctx, root, LAY_COLUMN); + + lay_id prev = lay_item(ctx); + lay_size_set_xy(ctx, prev, 1, 1); + lay_insert(ctx, root, prev); + for (int16_t i = 0; i < num_items - 1; ++i) + { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 1, 1); + lay_append(ctx, prev, item); + prev = item; + } + + lay_context_run(ctx); + + // with each child item being 1 unit tall, the total height should be num_items + LTEST_VEC4EQ(lay_rect_get(ctx, root), 0, 0, 1, num_items) + + free(items); +} + +LTEST_DECLARE(child_align_1) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 50, 50); + +#define MK_ALIGN_BOX(n, flags) \ + lay_id aligned_box_##n = lay_item(ctx); \ + lay_size_set_xy(ctx, aligned_box_##n, 10, 10); \ + lay_behave_set(ctx, aligned_box_##n, flags); \ + lay_insert(ctx, root, aligned_box_##n); + + MK_ALIGN_BOX(1, LAY_TOP | LAY_LEFT); + MK_ALIGN_BOX(2, LAY_TOP | LAY_RIGHT); + MK_ALIGN_BOX(3, LAY_TOP | LAY_HCENTER); + + MK_ALIGN_BOX(4, LAY_VCENTER | LAY_LEFT); + MK_ALIGN_BOX(5, LAY_VCENTER | LAY_RIGHT); + MK_ALIGN_BOX(6, LAY_VCENTER | LAY_HCENTER); + + MK_ALIGN_BOX(7, LAY_BOTTOM | LAY_LEFT); + MK_ALIGN_BOX(8, LAY_BOTTOM | LAY_RIGHT); + MK_ALIGN_BOX(9, LAY_BOTTOM | LAY_HCENTER); + +#undef MK_ALIGN_BOX + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_1), 0, 0, 10, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_2), 40, 0, 10, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_3), 20, 0, 10, 10); + + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_4), 0, 20, 10, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_5), 40, 20, 10, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_6), 20, 20, 10, 10); + + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_7), 0, 40, 10, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_8), 40, 40, 10, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_9), 20, 40, 10, 10); +} + +LTEST_DECLARE(child_align_2) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 50, 50); + +#define MK_ALIGN_BOX(n, flags) \ + lay_id aligned_box_##n = lay_item(ctx); \ + lay_size_set_xy(ctx, aligned_box_##n, 10, 10); \ + lay_behave_set(ctx, aligned_box_##n, flags); \ + lay_insert(ctx, root, aligned_box_##n); + + MK_ALIGN_BOX(1, LAY_TOP | LAY_HFILL); + MK_ALIGN_BOX(2, LAY_VCENTER | LAY_HFILL); + MK_ALIGN_BOX(3, LAY_BOTTOM | LAY_HFILL); + + MK_ALIGN_BOX(4, LAY_VFILL | LAY_LEFT); + MK_ALIGN_BOX(5, LAY_VFILL | LAY_RIGHT); + MK_ALIGN_BOX(6, LAY_VFILL | LAY_HCENTER); + +#undef MK_ALIGN_BOX + + lay_context_run(ctx); + + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_1), 0, 0, 50, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_2), 0, 20, 50, 10); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_3), 0, 40, 50, 10); + + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_4), 0, 0, 10, 50); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_5), 40, 0, 10, 50); + LTEST_VEC4EQ(lay_rect_get(ctx, aligned_box_6), 20, 0, 10, 50); +} + +LTEST_DECLARE(wrap_row_1) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 50, 50); + lay_contain_set(ctx, root, LAY_ROW | LAY_WRAP); + + // We will create a 5x5 grid of boxes that are 10x10 units per each box. + // There should be no empty space, gaps, or extra wrapping. + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + x = i % 5; + y = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), x * 10, y * 10, 10, 10); + } + + free(items); +} + +LTEST_DECLARE(wrap_row_2) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 57, 57); + lay_contain_set(ctx, root, LAY_ROW | LAY_WRAP | LAY_START); + + // This one should have extra space on the right edge and bottom (7 units) + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + x = i % 5; + y = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), x * 10, y * 10, 10, 10); + } + + free(items); +} + +LTEST_DECLARE(wrap_row_3) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 57, 57); + lay_contain_set(ctx, root, LAY_ROW | LAY_WRAP | LAY_END); + + // This one should have extra space on the left edge and bottom (7 units) + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + //lay_behave_set(ctx, item, LAY_BOTTOM | LAY_RIGHT); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + x = i % 5; + y = i / 5; + //print_vec4(lay_rect_get(ctx, items[i])); + //printf("\n"); + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), 7 + x * 10, y * 10, 10, 10); + } + + free(items); +} + +LTEST_DECLARE(wrap_row_4) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 58, 57); + lay_contain_set(ctx, root, LAY_ROW | LAY_WRAP | LAY_MIDDLE); + + lay_id spacer = lay_item(ctx); + lay_size_set_xy(ctx, spacer, 58, 7); + lay_insert(ctx, root, spacer); + + // This one should split the horizontal extra space between the left and + // right, and have the vertical extra space at the top (via extra inserted + // spacer item, with explicit size) + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + x = i % 5; + y = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), 4 + x * 10, 7 + y * 10, 10, 10); + } + + free(items); +} + +// bug? the last row of a LAY_JUSTIFY wrapping row container will not have its +// space divided evenly, and will instead behave like LAY_MIDDLE (or LAY_START?) + +LTEST_DECLARE(wrap_row_5) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 54, 50); + lay_contain_set(ctx, root, LAY_ROW | LAY_WRAP | LAY_JUSTIFY); + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + // TODO note we're adding the -5 here so we ignore the last row, which + // seems to be bugged + for (int16_t i = 0; i < num_items - 5; ++i) { + int16_t x, y; + x = i % 5; + y = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), x * 11, y * 10, 10, 10); + } + + free(items); +} + +// Same as wrap_row_1, but for columns +LTEST_DECLARE(wrap_column_1) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 50, 50); + lay_contain_set(ctx, root, LAY_COLUMN | LAY_WRAP); + + // We will create a 5x5 grid of boxes that are 10x10 units per each box. + // There should be no empty space, gaps, or extra wrapping. + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + y = i % 5; + x = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), x * 10, y * 10, 10, 10); + } + + free(items); +} + +LTEST_DECLARE(wrap_column_2) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 57, 57); + lay_contain_set(ctx, root, LAY_COLUMN | LAY_WRAP | LAY_START); + + // This one should have extra space on the right and bottom (7 units) + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + y = i % 5; + x = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), x * 10, y * 10, 10, 10); + } + + free(items); +} + +LTEST_DECLARE(wrap_column_3) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 57, 57); + lay_contain_set(ctx, root, LAY_COLUMN | LAY_WRAP | LAY_END); + + // This one should have extra space on the top and right (7 units) + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + //lay_behave_set(ctx, item, LAY_BOTTOM | LAY_RIGHT); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + y = i % 5; + x = i / 5; + //print_vec4(lay_rect_get(ctx, items[i])); + //printf("\n"); + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), x * 10, 7 + y * 10, 10, 10); + } + + free(items); +} + +// Just like wrap_row_4, but as columns instead of rows +LTEST_DECLARE(wrap_column_4) +{ + lay_id root = lay_item(ctx); + lay_size_set_xy(ctx, root, 57, 58); + lay_contain_set(ctx, root, LAY_COLUMN | LAY_WRAP | LAY_MIDDLE); + + lay_id spacer = lay_item(ctx); + lay_size_set_xy(ctx, spacer, 7, 58); + lay_insert(ctx, root, spacer); + + const int16_t num_items = 5 * 5; + lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id)); + + for (int16_t i = 0; i < num_items; ++i) { + lay_id item = lay_item(ctx); + lay_size_set_xy(ctx, item, 10, 10); + lay_insert(ctx, root, item); + items[i] = item; + } + + lay_context_run(ctx); + + for (int16_t i = 0; i < num_items; ++i) { + int16_t x, y; + y = i % 5; + x = i / 5; + LTEST_VEC4EQ(lay_rect_get(ctx, items[i]), 7 + x * 10, 4 + y * 10, 10, 10); + } + + free(items); +} + +// Call in main to run a test by name +// +// Resets string buffer and lay context before running test +#define LTEST_RUN(testname) \ + memset(sbuf, 0, 4096); \ + lay_context_reset(&ctx); \ + printf(" * " #testname "\n"); \ + test_##testname(&ctx, sbuf); + +int main() +{ +#ifdef _WIN32 + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); + SetUnhandledExceptionFilter(LayTestUnhandledExceptionFilter); +#endif + + char *sbuf = (char*)malloc(4096); + + lay_context ctx; + lay_context_init(&ctx); + + printf("Running tests\n"); + + LTEST_RUN(testing_sanity); + LTEST_RUN(simple_fill); + LTEST_RUN(reserve_capacity); + LTEST_RUN(multiple_uninserted); + LTEST_RUN(column_even_fill); + LTEST_RUN(row_even_fill); + LTEST_RUN(fixed_and_fill); + LTEST_RUN(simple_margins_1); + LTEST_RUN(nested_boxes_1); + LTEST_RUN(deep_nest_1); + LTEST_RUN(many_children_1); + LTEST_RUN(child_align_1); + LTEST_RUN(child_align_2); + LTEST_RUN(wrap_row_1); + LTEST_RUN(wrap_row_2); + LTEST_RUN(wrap_row_3); + LTEST_RUN(wrap_row_4); + LTEST_RUN(wrap_row_5); + LTEST_RUN(wrap_column_1); + LTEST_RUN(wrap_column_2); + LTEST_RUN(wrap_column_3); + LTEST_RUN(wrap_column_4); + + printf("Finished tests\n"); + + lay_context_destroy(&ctx); + free(sbuf); + return 0; +} diff --git a/thirdparty/readme.txt b/thirdparty/readme.txt new file mode 100644 index 0000000..0d7656e --- /dev/null +++ b/thirdparty/readme.txt @@ -0,0 +1,2 @@ +Put LuaJIT-2.0.0 src here (thirdparty/Luajit-2.0.0) if you want to build the +Lua/LuaJIT module. Better solution for this needed.