diff --git a/demo/main.fnl b/demo/main.fnl index 6dc7541..10116c3 100644 --- a/demo/main.fnl +++ b/demo/main.fnl @@ -16,8 +16,8 @@ (var logo-sprite nil) (global init (fn [] - (pxl8.load_palette "sprites/pxl8_logo.ase") - (set logo-sprite (pxl8.load_sprite "sprites/pxl8_logo.ase")) + (pxl8.load_palette "res/sprites/pxl8_logo.ase") + (set logo-sprite (pxl8.load_sprite "res/sprites/pxl8_logo.ase")) (set particles (pxl8.particles_new 1000)))) (global update (fn [dt] @@ -44,7 +44,7 @@ (set current-effect 8)) (when (pxl8.key_pressed "9") (set use-nes-palette (not use-nes-palette)) - (local palette-path (if use-nes-palette "palettes/nes.ase" "sprites/pxl8_logo.ase")) + (local palette-path (if use-nes-palette "res/palettes/nes.ase" "res/sprites/pxl8_logo.ase")) (pxl8.load_palette palette-path)) (case current-effect diff --git a/demo/mod/cube3d.fnl b/demo/mod/cube3d.fnl index 606e29b..094f6d3 100644 --- a/demo/mod/cube3d.fnl +++ b/demo/mod/cube3d.fnl @@ -26,8 +26,8 @@ (fn init-texture [] (when (not texture-initialized) - (pxl8.load_palette "sprites/pxl8_logo.ase") - (set texture-id (pxl8.load_sprite "sprites/pxl8_logo.ase")) + (pxl8.load_palette "res/sprites/pxl8_logo.ase") + (set texture-id (pxl8.load_sprite "res/sprites/pxl8_logo.ase")) (pxl8.upload_atlas) (set texture-initialized true))) @@ -71,37 +71,57 @@ (set fps-accumulator 0) (set fps-frame-count 0)) - (let [wheel-y (pxl8.mouse_wheel_y) - zoom-speed 0.5] - (when (not= wheel-y 0) - (let [forward-x (* (math.sin cam-yaw) wheel-y zoom-speed) - forward-z (* (math.cos cam-yaw) wheel-y zoom-speed)] - (set cam-x (+ cam-x forward-x)) - (set cam-z (- cam-z forward-z))))) + (let [wheel-y (pxl8.mouse_wheel_y)] + (when (and (not= wheel-y 0) (not (pxl8.ui_has_mouse_focus))) + (if orthographic + (set zoom (math.max 0.5 (math.min (- zoom (* wheel-y 0.2)) 10.0))) + (let [zoom-speed 0.5 + forward-x (* (math.sin cam-yaw) wheel-y zoom-speed) + forward-z (* (math.cos cam-yaw) wheel-y zoom-speed)] + (set cam-x (+ cam-x forward-x)) + (set cam-z (- cam-z forward-z)))))) (let [move-speed 5.0 rot-speed 2.0 + zoom-speed 2.0 forward-x (* (math.sin cam-yaw) move-speed dt) forward-z (* (math.cos cam-yaw) move-speed dt) right-x (* (math.cos cam-yaw) move-speed dt) right-z (* (- (math.sin cam-yaw)) move-speed dt)] - (when (pxl8.key_down "w") - (set cam-x (+ cam-x forward-x)) - (set cam-z (- cam-z forward-z))) - (when (pxl8.key_down "s") - (set cam-x (- cam-x forward-x)) - (set cam-z (+ cam-z forward-z))) - (when (pxl8.key_down "a") - (set cam-x (- cam-x right-x)) - (set cam-z (- cam-z right-z))) - (when (pxl8.key_down "d") - (set cam-x (+ cam-x right-x)) - (set cam-z (+ cam-z right-z))) - (when (pxl8.key_down "q") - (set cam-y (- cam-y (* move-speed dt)))) - (when (pxl8.key_down "e") - (set cam-y (+ cam-y (* move-speed dt)))) + (if orthographic + (do + (when (pxl8.key_down "w") + (set zoom (math.max 0.5 (- zoom (* zoom-speed dt))))) + (when (pxl8.key_down "s") + (set zoom (math.min 10.0 (+ zoom (* zoom-speed dt))))) + (when (pxl8.key_down "a") + (set cam-x (- cam-x right-x)) + (set cam-z (- cam-z right-z))) + (when (pxl8.key_down "d") + (set cam-x (+ cam-x right-x)) + (set cam-z (+ cam-z right-z))) + (when (pxl8.key_down "q") + (set cam-y (- cam-y (* move-speed dt)))) + (when (pxl8.key_down "e") + (set cam-y (+ cam-y (* move-speed dt))))) + (do + (when (pxl8.key_down "w") + (set cam-x (+ cam-x forward-x)) + (set cam-z (- cam-z forward-z))) + (when (pxl8.key_down "s") + (set cam-x (- cam-x forward-x)) + (set cam-z (+ cam-z forward-z))) + (when (pxl8.key_down "a") + (set cam-x (- cam-x right-x)) + (set cam-z (- cam-z right-z))) + (when (pxl8.key_down "d") + (set cam-x (+ cam-x right-x)) + (set cam-z (+ cam-z right-z))) + (when (pxl8.key_down "q") + (set cam-y (- cam-y (* move-speed dt)))) + (when (pxl8.key_down "e") + (set cam-y (+ cam-y (* move-speed dt)))))) (when (pxl8.key_down "left") (set cam-yaw (- cam-yaw (* rot-speed dt)))) @@ -144,7 +164,7 @@ (pxl8.set_wireframe wireframe) (if orthographic - (let [size 2.5 + (let [size zoom aspect (/ (pxl8.get_width) (pxl8.get_height)) w (* size aspect) h size] diff --git a/demo/mod/debug_ui.fnl b/demo/mod/debug_ui.fnl index eadc6f8..cfa43b3 100644 --- a/demo/mod/debug_ui.fnl +++ b/demo/mod/debug_ui.fnl @@ -3,8 +3,7 @@ (fn render [state] (var new-state {}) (when state.show-debug-ui - (let [window-h (if state.use-texture 210 180) - window-open (pxl8.ui_window_begin "Debug Menu (F8)" 10 10 250 window-h)] + (let [window-open (pxl8.ui_window_begin "Debug Menu (F8)" 10 10 256 128)] (when window-open (pxl8.ui_layout_row 1 0 0) (pxl8.ui_label (string.format "FPS: %.0f" (or state.fps 0))) diff --git a/demo/palettes/gruvbox.ase b/demo/palettes/gruvbox.ase deleted file mode 100644 index d667cb4..0000000 Binary files a/demo/palettes/gruvbox.ase and /dev/null differ diff --git a/demo/palettes/nes.ase b/demo/res/palettes/nes.ase similarity index 100% rename from demo/palettes/nes.ase rename to demo/res/palettes/nes.ase diff --git a/demo/sprites/pxl8_icon.ase b/demo/res/sprites/pxl8_icon.ase similarity index 100% rename from demo/sprites/pxl8_icon.ase rename to demo/res/sprites/pxl8_icon.ase diff --git a/demo/sprites/pxl8_logo.ase b/demo/res/sprites/pxl8_logo.ase similarity index 100% rename from demo/sprites/pxl8_logo.ase rename to demo/res/sprites/pxl8_logo.ase diff --git a/src/lua/pxl8.lua b/src/lua/pxl8.lua index 92d043f..4bb956e 100644 --- a/src/lua/pxl8.lua +++ b/src/lua/pxl8.lua @@ -2,6 +2,7 @@ local ffi = require("ffi") local C = ffi.C local gfx = _pxl8_gfx local input = _pxl8_input +local ui = _pxl8_ui -- pxl8 lua api -- @@ -369,30 +370,30 @@ function pxl8.mat4_lookat(eye, center, up) return C.pxl8_mat4_lookat(eye_vec, center_vec, up_vec) end -pxl8.gfx = gfx -pxl8.input = input -pxl8.ui = _pxl8_ui - function pxl8.bounds(x, y, w, h) return ffi.new("pxl8_bounds", {x = x, y = y, w = w, h = h}) end function pxl8.ui_button(label) - return C.pxl8_ui_button(_pxl8_ui, label) + return C.pxl8_ui_button(ui, label) end function pxl8.ui_checkbox(label, state) local state_ptr = ffi.new("bool[1]", state) - local changed = C.pxl8_ui_checkbox(_pxl8_ui, label, state_ptr) + local changed = C.pxl8_ui_checkbox(ui, label, state_ptr) return changed, state_ptr[0] end +function pxl8.ui_has_mouse_focus() + return C.pxl8_ui_has_mouse_focus(ui) +end + function pxl8.ui_indent(amount) - C.pxl8_ui_indent(_pxl8_ui, amount) + C.pxl8_ui_indent(ui, amount) end function pxl8.ui_label(text) - C.pxl8_ui_label(_pxl8_ui, text) + C.pxl8_ui_label(ui, text) end function pxl8.ui_layout_row(item_count, widths, height) @@ -402,20 +403,20 @@ function pxl8.ui_layout_row(item_count, widths, height) elseif type(widths) == "number" then widths_array = ffi.new("int[1]", widths) end - C.pxl8_ui_layout_row(_pxl8_ui, item_count, widths_array, height) + C.pxl8_ui_layout_row(ui, item_count, widths_array, height) end function pxl8.ui_window_begin(title, x, y, w, h, options) local rect = ffi.new("pxl8_bounds", {x = x, y = y, w = w, h = h}) - return C.pxl8_ui_window_begin(_pxl8_ui, title, rect, options or 0) + return C.pxl8_ui_window_begin(ui, title, rect, options or 0) end function pxl8.ui_window_end() - C.pxl8_ui_window_end(_pxl8_ui) + C.pxl8_ui_window_end(ui) end function pxl8.ui_window_set_open(title, open) - C.pxl8_ui_window_set_open(_pxl8_ui, title, open) + C.pxl8_ui_window_set_open(ui, title, open) end return pxl8 diff --git a/src/pxl8.c b/src/pxl8.c index 47ce57c..dd34d04 100644 --- a/src/pxl8.c +++ b/src/pxl8.c @@ -334,6 +334,9 @@ SDL_AppResult SDL_AppIterate(void* appstate) { if (app->ui) { pxl8_ui_input_mousemove(app->ui, app->input.mouse_x, app->input.mouse_y); + if (app->input.mouse_wheel_x != 0 || app->input.mouse_wheel_y != 0) { + pxl8_ui_input_scroll(app->ui, app->input.mouse_wheel_x * 10, -app->input.mouse_wheel_y * 10); + } pxl8_ui_frame_begin(app->ui); diff --git a/src/pxl8_script.c b/src/pxl8_script.c index 91f684f..2045d36 100644 --- a/src/pxl8_script.c +++ b/src/pxl8_script.c @@ -174,6 +174,7 @@ static const char* pxl8_ffi_cdefs = "void pxl8_ui_input_text(pxl8_ui* ui, const char* text);\n" "bool pxl8_ui_button(pxl8_ui* ui, const char* label);\n" "bool pxl8_ui_checkbox(pxl8_ui* ui, const char* label, bool* state);\n" +"bool pxl8_ui_has_mouse_focus(pxl8_ui* ui);\n" "void pxl8_ui_indent(pxl8_ui* ui, int amount);\n" "void pxl8_ui_label(pxl8_ui* ui, const char* text);\n" "void pxl8_ui_layout_row(pxl8_ui* ui, int item_count, const int* widths, int height);\n" diff --git a/src/pxl8_ui.c b/src/pxl8_ui.c index 0813a17..77d35d6 100644 --- a/src/pxl8_ui.c +++ b/src/pxl8_ui.c @@ -137,8 +137,8 @@ pxl8_ui* pxl8_ui_create(pxl8_gfx* gfx) { ui->mu_ctx.style->colors[9] = (mu_Color){2, 0, 0, 255}; ui->mu_ctx.style->colors[10] = (mu_Color){3, 0, 0, 255}; ui->mu_ctx.style->colors[11] = (mu_Color){10, 0, 0, 255}; - ui->mu_ctx.style->colors[12] = (mu_Color){7, 0, 0, 255}; - ui->mu_ctx.style->colors[13] = (mu_Color){8, 0, 0, 255}; + ui->mu_ctx.style->colors[12] = (mu_Color){1, 0, 0, 255}; + ui->mu_ctx.style->colors[13] = (mu_Color){3, 0, 0, 255}; return ui; } @@ -194,6 +194,11 @@ void pxl8_ui_input_text(pxl8_ui* ui, const char* text) { mu_input_text(&ui->mu_ctx, text); } +bool pxl8_ui_has_mouse_focus(pxl8_ui* ui) { + if (!ui) return false; + return ui->mu_ctx.hover_root != NULL; +} + bool pxl8_ui_button(pxl8_ui* ui, const char* label) { if (!ui) return false; return mu_button(&ui->mu_ctx, label) & MU_RES_SUBMIT; diff --git a/src/pxl8_ui.h b/src/pxl8_ui.h index 51acc52..144c0ac 100644 --- a/src/pxl8_ui.h +++ b/src/pxl8_ui.h @@ -52,6 +52,10 @@ void pxl8_ui_destroy(pxl8_ui* ui); void pxl8_ui_frame_begin(pxl8_ui* ui); void pxl8_ui_frame_end(pxl8_ui* ui); +bool pxl8_ui_button(pxl8_ui* ui, const char* label); +bool pxl8_ui_checkbox(pxl8_ui* ui, const char* label, bool* state); +bool pxl8_ui_has_mouse_focus(pxl8_ui* ui); +void pxl8_ui_indent(pxl8_ui* ui, i32 amount); void pxl8_ui_input_keydown(pxl8_ui* ui, i32 key); void pxl8_ui_input_keyup(pxl8_ui* ui, i32 key); void pxl8_ui_input_mousedown(pxl8_ui* ui, i32 x, i32 y, i32 button); @@ -59,10 +63,6 @@ void pxl8_ui_input_mousemove(pxl8_ui* ui, i32 x, i32 y); void pxl8_ui_input_mouseup(pxl8_ui* ui, i32 x, i32 y, i32 button); void pxl8_ui_input_scroll(pxl8_ui* ui, i32 x, i32 y); void pxl8_ui_input_text(pxl8_ui* ui, const char* text); - -bool pxl8_ui_button(pxl8_ui* ui, const char* label); -bool pxl8_ui_checkbox(pxl8_ui* ui, const char* label, bool* state); -void pxl8_ui_indent(pxl8_ui* ui, i32 amount); void pxl8_ui_label(pxl8_ui* ui, const char* text); void pxl8_ui_layout_row(pxl8_ui* ui, i32 item_count, const i32* widths, i32 height); i32 pxl8_ui_menu(pxl8_ui* ui, pxl8_menu_item* items, i32 item_count);