Skip to content

Commit

Permalink
Add image support to file viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
dextercd committed Dec 29, 2023
1 parent c7a41e8 commit a687ad4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
88 changes: 85 additions & 3 deletions component-explorer/file_viewer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---@module 'component-explorer.utils.lua_appends'
local lua_appends = dofile_once("mods/component-explorer/utils/lua_appends.lua")

---@module 'component-explorer.style'
local style = dofile_once("mods/component-explorer/style.lua")

local ModTextFileGetContent = ModTextFileGetContent
Expand Down Expand Up @@ -30,6 +33,11 @@ local function is_supported_file_type(path)
if l4 == ".txt" then return true end
if l4 == ".csv" then return true end

if imgui.LoadImage then
if l4 == ".png" then return true end
if l4 == ".bmp" then return true end
end

local l5 = path:sub(-5)
if #l5 < 5 then return false end

Expand Down Expand Up @@ -70,14 +78,24 @@ end

---@param path string
function file_viewer.show_file(path)
local content = get_content(path)
local who_set_content = ModTextFileWhoSetContent(path)

imgui.Text("File: " .. path)
imgui.SameLine()
if imgui.SmallButton("Copy path") then
imgui.SetClipboardText(path)
end

local l4 = path:sub(-4)
if l4 == ".bmp" or l4 == ".png" then
return file_viewer.show_image_file(path)
else
return file_viewer.show_text_file(path)
end
end

function file_viewer.show_text_file(path)
local content = get_content(path)
local who_set_content = ModTextFileWhoSetContent(path)

imgui.SameLine()
if imgui.SmallButton("Copy content") then
imgui.SetClipboardText(content or "Hi :)")
Expand Down Expand Up @@ -145,6 +163,67 @@ function file_viewer.show_file(path)
end
end

local hover_zoom = 4

function file_viewer.show_image_file(path)
local img = imgui.LoadImage(path)
if not img then
imgui.PushStyleColor(imgui.Col.Text, unpack(style.colour_fail))
imgui.Text("Couldn't load image")
imgui.PopStyleColor()
return
end

imgui.Text("Size: " .. img.width .. " x " .. img.height)

imgui.SetNextItemWidth(200)
local _
_, hover_zoom = imgui.SliderFloat("Hover zoom", hover_zoom, 1, 10)

local window_width = imgui.GetWindowWidth()

local ideal_scale_factor = math.max(1, window_width / img.width)
local scale_factor = math.floor(ideal_scale_factor)

local pos_x, pos_y = imgui.GetCursorScreenPos()
imgui.Image(img, img.width * scale_factor, img.height * scale_factor)

if imgui.IsItemHovered() then
imgui.BeginTooltip()

local region_sz = 300

-- local zoomed_sz_x = img.width / (img.width * scale_factor / region_sz) / hover_zoom
local zoomed_sz_x = math.min(region_sz / scale_factor / hover_zoom, img.width)
local zoomed_sz_y = math.min(zoomed_sz_x, img.height)

local mouse_pos_x, mouse_pos_y = imgui.GetMousePos()
local tex_x = (mouse_pos_x - pos_x) / scale_factor - zoomed_sz_x * 0.5
local tex_y = (mouse_pos_y - pos_y) / scale_factor - zoomed_sz_y * 0.5

if tex_x < 0 then tex_x = 0 end
if tex_y < 0 then tex_y = 0 end
if tex_x > img.width - zoomed_sz_x then tex_x = img.width - zoomed_sz_x end
if tex_y > img.height - zoomed_sz_y then tex_y = img.height - zoomed_sz_y end

imgui.Text(("Min: %.2f %.2f"):format(tex_x, tex_y))
imgui.Text(("Max: %.2f %.2f"):format(tex_x + zoomed_sz_x, tex_y + zoomed_sz_y))

local uv_0_x = tex_x / img.width
local uv_1_x = (tex_x + zoomed_sz_x) / img.width
local uv_0_y = tex_y / img.height
local uv_1_y = (tex_y + zoomed_sz_y) / img.height

imgui.Image(img,
region_sz, region_sz * (zoomed_sz_y / zoomed_sz_x),
uv_0_x, uv_0_y,
uv_1_x, uv_1_y
)

imgui.EndTooltip()
end
end


---@type string?
local set_this_selected = nil
Expand All @@ -156,6 +235,9 @@ local open_files_lookup = {}
local open_files = {}

function file_viewer.open_file(to_open)
if not is_supported_file_type(to_open) then
error("Unsupported file type")
end
if not open_files_lookup[to_open] then
table.insert(open_files, to_open)
open_files_lookup[to_open] = true
Expand Down
8 changes: 8 additions & 0 deletions component-explorer/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ function show_about_window()
imgui.Text("Made by dextercd")
link_ui.button("Homepage", version.homepage)

local wx, wy = imgui.GetWindowSize()
if imgui.LoadImage and wy > 300 then
local img = imgui.LoadImage("mods/component-explorer/ui/james.png")
if img then
imgui.Image(img, img.width, img.height)
end
end

imgui.End()
end
end
Expand Down
Binary file added component-explorer/ui/james.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a687ad4

Please sign in to comment.