showimage/showimage.odin

82 lines
1.7 KiB
Odin

package showimage
import "core:os"
import "core:fmt"
import "core:strings"
import rl "vendor:raylib"
tex: ^rl.Texture
zoom: f32 = 1
offset: [2]f32
main :: proc() {
rl.InitWindow(800, 600, "showimage")
rl.SetWindowState({ .WINDOW_RESIZABLE })
rl.SetTargetFPS(60)
if len(os.args) > 1 {
path := os.args[1]
if strings.has_prefix(path, "file://") {
path, _ = strings.substring_from(path, 7)
}
rl.SetWindowTitle(strings.clone_to_cstring(path));
tex = new_clone(rl.LoadTexture(strings.clone_to_cstring(path)))
if tex.id <= 0 { return }
reset_zoom()
}
for !rl.WindowShouldClose() {
scroll := rl.GetMouseWheelMove()
if scroll > 0 {
// zoom in
if zoom > 1 {
change_zoom(1)
} else {
change_zoom(zoom)
}
} else if scroll < 0 {
if zoom > 1 {
change_zoom(-1)
} else {
change_zoom(-zoom / 2)
}
}
if rl.IsMouseButtonDown(rl.MouseButton.LEFT) {
offset += rl.GetMouseDelta()
}
if rl.IsMouseButtonPressed(rl.MouseButton.RIGHT) {
offset = 0
reset_zoom()
}
rl.BeginDrawing()
rl.ClearBackground({50, 50, 50, 255})
if tex != nil {
rl.DrawTextureEx(tex^, offset, 0, zoom, rl.WHITE)
}
rl.EndDrawing()
}
rl.CloseWindow()
}
change_zoom :: proc(delta: f32) {
mouse_pos := rl.GetMousePosition()
zoom_pos := (mouse_pos - offset) / zoom
zoom += delta
fmt.printf("zoomed to %f\n", zoom)
offset = mouse_pos - zoom_pos * zoom
}
zoom_to_fit :: proc(size: i32, max: i32) -> f32 {
if size > max {
return 1
}
return f32(max / size)
}
reset_zoom :: proc() {
zoom = f32(min(
zoom_to_fit(tex.width, rl.GetRenderWidth()),
zoom_to_fit(tex.height, rl.GetRenderHeight()),
))
fmt.printf("zoomed to %f\n",zoom)
}