1
0
Fork 0
forked from AbleOS/ableos

multi-drag

This commit is contained in:
koniifer 2025-02-12 00:59:42 +00:00
parent abaac886ef
commit 19715ef6fd
4 changed files with 60 additions and 29 deletions
rust-toolchain.toml
sysdata
programs/render_example/src
system_config.toml

View file

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

View file

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

View file

@ -1 +1 @@
.{example: main} := @use("./examples/intouch.hb")
.{example: main} := @use("./examples/drag-and-drop.hb")

View file

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