From 19715ef6fd8d589600eec0e1ccb75ba164dd6f5d Mon Sep 17 00:00:00 2001 From: koniifer <koniifer@proton.me> Date: Wed, 12 Feb 2025 00:59:42 +0000 Subject: [PATCH] multi-drag --- rust-toolchain.toml | 3 +- .../src/examples/drag-and-drop.hb | 80 +++++++++++++------ sysdata/programs/render_example/src/main.hb | 2 +- sysdata/system_config.toml | 4 +- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index b37e5aa..aadc00b 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -3,5 +3,6 @@ # channel = "nightly-2024-07-27" # last stable # channel = "nightly-2024-11-20" -channel = "nightly" +# locking here to prevent compile error in holey-bytes crate +channel = "nightly-2025-01-25" components = ["rust-src", "llvm-tools"] diff --git a/sysdata/programs/render_example/src/examples/drag-and-drop.hb b/sysdata/programs/render_example/src/examples/drag-and-drop.hb index 6628d9a..b7214f4 100644 --- a/sysdata/programs/render_example/src/examples/drag-and-drop.hb +++ b/sysdata/programs/render_example/src/examples/drag-and-drop.hb @@ -2,42 +2,72 @@ intouch := @use("lib:intouch") render := @use("lib:render") stn := @use("stn") +mouse_pos := stn.math.Vec2(int).(0, 0) +mouse_event := @as(?intouch.MouseEvent, null) +global_lock := false + +DraggableSurface := struct { + pos: stn.math.Vec2(int), + surface: render.Surface, + lock: bool, + process := fn(self: ^Self, screen: ^render.Surface): void { + screen.put_surface(self.surface, @bitcast(self.pos), false) + // funny hblang bug means need to check for boolean equality + if global_lock | @unwrap(mouse_event).left { + if self.lock == true return + } + if mouse_pos.x >= self.pos.x & mouse_pos.x <= self.pos.x + @bitcast(self.surface.width) & mouse_pos.y >= self.pos.y & mouse_pos.y <= self.pos.y + @bitcast(self.surface.height) { + global_lock = true + self.lock = false + if @unwrap(mouse_event).left { + self.pos = .( + stn.math.clamp(int, self.pos.x + @unwrap(mouse_event).x_change, 1, @bitcast(screen.width) - @bitcast(self.surface.width) - 1), + stn.math.clamp(int, self.pos.y - @unwrap(mouse_event).y_change, 1, @bitcast(screen.height) - @bitcast(self.surface.height) - 1), + ) + screen.put_rect(@bitcast(self.pos), .(self.surface.width, self.surface.height), render.RED) + } else { + screen.put_rect(@bitcast(self.pos), .(self.surface.width, self.surface.height), render.CYAN) + } + } else { + global_lock = false + self.lock = true + } + } +} + example := fn(): void { screen := render.init(true) - mouse_pos := stn.math.Vec2(int).(0, 0) image := render.image.from(@bitcast(&@embed("sysdata:assets/mini.qoi"))) - if image == null { + image2 := render.image.from(@bitcast(&@embed("sysdata:assets/able.bmp"))) + if image == null | image2 == null { stn.log.error("failed to load images for whatever reason") return } - image_pos := stn.math.Vec2(int).(@bitcast(screen.width - image.width) / 2, @bitcast(screen.height - image.height) / 2) - image_dim: stn.math.Vec2(int) = .(@bitcast(image.width), @bitcast(image.height)) + + thing := DraggableSurface.( + .(400, 400), + image, + false, + ) + + thing2 := DraggableSurface.( + .(0, 0), + image2, + false, + ) loop { - mouse := intouch.recieve_mouse_event() - if mouse == null { - continue - } - screen.put_surface(image, @bitcast(image_pos), false) + mouse_event = intouch.recieve_mouse_event() + if mouse_event == null continue + mouse_pos.x = stn.math.clamp(int, mouse_pos.x + mouse_event.x_change, 6, @bitcast(screen.width) - 6) + mouse_pos.y = stn.math.clamp(int, mouse_pos.y - @unwrap(mouse_event).y_change, 6, @bitcast(screen.height) - 6) - mouse_pos.x = stn.math.clamp(int, mouse_pos.x + mouse.x_change, 5, @bitcast(screen.width) - 5) - mouse_pos.y = stn.math.clamp(int, mouse_pos.y - mouse.y_change, 5, @bitcast(screen.height) - 5) + thing.process(&screen) + thing2.process(&screen) - // :( - if mouse_pos.x >= image_pos.x & mouse_pos.x <= image_pos.x + image_dim.x & mouse_pos.y >= image_pos.y & mouse_pos.y <= image_pos.y + image_dim.y { - if mouse.left { - screen.put_rect(@bitcast(image_pos), @bitcast(image_dim), render.RED) - - image_pos.x = stn.math.clamp(int, image_pos.x + mouse.x_change, 0, @bitcast(screen.width) - image_dim.x) - image_pos.y = stn.math.clamp(int, image_pos.y - mouse.y_change, 0, @bitcast(screen.height) - image_dim.y) - } else { - screen.put_rect(@bitcast(image_pos), @bitcast(image_dim), render.CYAN) - screen.put_filled_circle(@bitcast(mouse_pos), 5, render.GRAY) - screen.put_circle(@bitcast(mouse_pos), 5, render.BLACK) - } - } + screen.put_filled_circle(@bitcast(mouse_pos), 5, render.BLACK) screen.sync() - screen.clear(render.BLACK) + screen.clear(render.GRAY) } } \ No newline at end of file diff --git a/sysdata/programs/render_example/src/main.hb b/sysdata/programs/render_example/src/main.hb index c3e299f..651f2de 100644 --- a/sysdata/programs/render_example/src/main.hb +++ b/sysdata/programs/render_example/src/main.hb @@ -1 +1 @@ -.{example: main} := @use("./examples/intouch.hb") \ No newline at end of file +.{example: main} := @use("./examples/drag-and-drop.hb") \ No newline at end of file diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index 31c3f63..d8319b4 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -29,8 +29,8 @@ path = "boot:///render_example.hbf" # [boot.limine.ableos.modules.sunset_server] # path = "boot:///sunset_server.hbf" -# [boot.limine.ableos.modules.ps2_mouse_driver] -# path = "boot:///ps2_mouse_driver.hbf" +[boot.limine.ableos.modules.ps2_mouse_driver] +path = "boot:///ps2_mouse_driver.hbf" # [boot.limine.ableos.modules.ps2_keyboard_driver] # path = "boot:///ps2_keyboard_driver.hbf"