2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local config = require "core.config"
|
|
|
|
local common = require "core.common"
|
|
|
|
local Object = require "core.object"
|
2022-10-16 02:12:15 +02:00
|
|
|
local Scrollbar = require "core.scrollbar"
|
2021-10-06 01:42:04 +02:00
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@class core.view.position
|
|
|
|
---@field x number
|
|
|
|
---@field y number
|
|
|
|
|
|
|
|
---@class core.view.scroll
|
|
|
|
---@field x number
|
|
|
|
---@field y number
|
|
|
|
---@field to core.view.position
|
|
|
|
|
|
|
|
---@class core.view.thumbtrack
|
|
|
|
---@field thumb number
|
|
|
|
---@field track number
|
|
|
|
|
|
|
|
---@class core.view.thumbtrackwidth
|
|
|
|
---@field thumb number
|
|
|
|
---@field track number
|
|
|
|
---@field to core.view.thumbtrack
|
|
|
|
|
|
|
|
---@class core.view.scrollbar
|
|
|
|
---@field x core.view.thumbtrack
|
|
|
|
---@field y core.view.thumbtrack
|
|
|
|
---@field w core.view.thumbtrackwidth
|
|
|
|
---@field h core.view.thumbtrack
|
|
|
|
|
|
|
|
---@alias core.view.cursor "'arrow'" | "'ibeam'" | "'sizeh'" | "'sizev'" | "'hand'"
|
|
|
|
|
|
|
|
---@alias core.view.mousebutton "'left'" | "'right'"
|
|
|
|
|
|
|
|
---@alias core.view.context "'application'" | "'session'"
|
|
|
|
|
|
|
|
---Base view.
|
|
|
|
---@class core.view : core.object
|
|
|
|
---@field context core.view.context
|
|
|
|
---@field super core.object
|
|
|
|
---@field position core.view.position
|
|
|
|
---@field size core.view.position
|
|
|
|
---@field scroll core.view.scroll
|
|
|
|
---@field cursor core.view.cursor
|
|
|
|
---@field scrollable boolean
|
2022-10-16 02:12:15 +02:00
|
|
|
---@field v_scrollbar core.scrollbar
|
|
|
|
---@field h_scrollbar core.scrollbar
|
2022-10-11 20:44:32 +02:00
|
|
|
---@field current_scale number
|
2019-12-28 12:16:32 +01:00
|
|
|
local View = Object:extend()
|
|
|
|
|
2021-08-27 23:55:17 +02:00
|
|
|
-- context can be "application" or "session". The instance of objects
|
|
|
|
-- with context "session" will be closed when a project session is
|
|
|
|
-- terminated. The context "application" is for functional UI elements.
|
|
|
|
View.context = "application"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
function View:new()
|
|
|
|
self.position = { x = 0, y = 0 }
|
|
|
|
self.size = { x = 0, y = 0 }
|
|
|
|
self.scroll = { x = 0, y = 0, to = { x = 0, y = 0 } }
|
|
|
|
self.cursor = "arrow"
|
|
|
|
self.scrollable = false
|
2022-11-01 23:38:50 +01:00
|
|
|
self.v_scrollbar = Scrollbar({direction = "v", alignment = "e"})
|
|
|
|
self.h_scrollbar = Scrollbar({direction = "h", alignment = "e"})
|
2022-10-11 20:44:32 +02:00
|
|
|
self.current_scale = SCALE
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-04-26 02:35:35 +02:00
|
|
|
function View:move_towards(t, k, dest, rate, name)
|
2019-12-28 12:16:32 +01:00
|
|
|
if type(t) ~= "table" then
|
2022-04-26 02:35:35 +02:00
|
|
|
return self:move_towards(self, t, k, dest, rate, name)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
local val = t[k]
|
2022-03-01 22:41:54 +01:00
|
|
|
local diff = math.abs(val - dest)
|
2022-04-26 02:35:35 +02:00
|
|
|
if not config.transitions or diff < 0.5 or config.disabled_transitions[name] then
|
2019-12-28 12:16:32 +01:00
|
|
|
t[k] = dest
|
|
|
|
else
|
2021-03-18 16:20:21 +01:00
|
|
|
rate = rate or 0.5
|
2021-03-20 17:00:43 +01:00
|
|
|
if config.fps ~= 60 or config.animation_rate ~= 1 then
|
2021-03-18 16:20:21 +01:00
|
|
|
local dt = 60 / config.fps
|
2021-03-20 17:00:43 +01:00
|
|
|
rate = 1 - common.clamp(1 - rate, 1e-8, 1 - 1e-8)^(config.animation_rate * dt)
|
2021-03-18 16:20:21 +01:00
|
|
|
end
|
|
|
|
t[k] = common.lerp(val, dest, rate)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-03-01 22:41:54 +01:00
|
|
|
if diff > 1e-8 then
|
2019-12-28 12:16:32 +01:00
|
|
|
core.redraw = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:try_close(do_close)
|
|
|
|
do_close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@return string
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:get_name()
|
|
|
|
return "---"
|
|
|
|
end
|
|
|
|
|
2021-08-30 00:50:46 +02:00
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@return number
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:get_scrollable_size()
|
2021-08-30 00:50:46 +02:00
|
|
|
return math.huge
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
---@return number
|
|
|
|
function View:get_h_scrollable_size()
|
|
|
|
return 0
|
2021-08-30 00:50:46 +02:00
|
|
|
end
|
2021-08-28 19:59:09 +02:00
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param x number
|
|
|
|
---@param y number
|
|
|
|
---@return boolean
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:scrollbar_overlaps_point(x, y)
|
2022-10-16 02:12:15 +02:00
|
|
|
return not (not (self.v_scrollbar:overlaps(x, y) or self.h_scrollbar:overlaps(x, y)))
|
2022-04-12 02:49:11 +02:00
|
|
|
end
|
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
|
|
|
|
---@return boolean
|
|
|
|
function View:scrollbar_dragging()
|
|
|
|
return self.v_scrollbar.dragging or self.h_scrollbar.dragging
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@return boolean
|
2022-10-16 02:12:15 +02:00
|
|
|
function View:scrollbar_hovering()
|
|
|
|
return self.v_scrollbar.hovering.track or self.h_scrollbar.hovering.track
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param button core.view.mousebutton
|
|
|
|
---@param x number
|
|
|
|
---@param y number
|
|
|
|
---@param clicks integer
|
|
|
|
---return boolean
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:on_mouse_pressed(button, x, y, clicks)
|
2022-10-16 02:12:15 +02:00
|
|
|
if not self.scrollable then return end
|
|
|
|
local result = self.v_scrollbar:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
if result then
|
|
|
|
if result ~= true then
|
2024-02-11 18:51:12 +01:00
|
|
|
self.scroll.to.y = result * self:get_scrollable_size()
|
2022-10-16 02:12:15 +02:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
result = self.h_scrollbar:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
if result then
|
|
|
|
if result ~= true then
|
2024-02-11 18:51:12 +01:00
|
|
|
self.scroll.to.x = result * self:get_h_scrollable_size()
|
2022-04-12 02:49:11 +02:00
|
|
|
end
|
2021-08-30 00:50:46 +02:00
|
|
|
return true
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param button core.view.mousebutton
|
|
|
|
---@param x number
|
|
|
|
---@param y number
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:on_mouse_released(button, x, y)
|
2022-10-16 02:12:15 +02:00
|
|
|
if not self.scrollable then return end
|
|
|
|
self.v_scrollbar:on_mouse_released(button, x, y)
|
|
|
|
self.h_scrollbar:on_mouse_released(button, x, y)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param x number
|
|
|
|
---@param y number
|
|
|
|
---@param dx number
|
|
|
|
---@param dy number
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:on_mouse_moved(x, y, dx, dy)
|
2022-10-16 02:12:15 +02:00
|
|
|
if not self.scrollable then return end
|
|
|
|
local result
|
|
|
|
if self.h_scrollbar.dragging then goto skip_v_scrollbar end
|
|
|
|
result = self.v_scrollbar:on_mouse_moved(x, y, dx, dy)
|
|
|
|
if result then
|
|
|
|
if result ~= true then
|
2024-02-11 18:51:12 +01:00
|
|
|
self.scroll.to.y = result * self:get_scrollable_size()
|
2022-10-16 02:12:15 +02:00
|
|
|
if not config.animate_drag_scroll then
|
|
|
|
self:clamp_scroll_position()
|
|
|
|
self.scroll.y = self.scroll.to.y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- hide horizontal scrollbar
|
|
|
|
self.h_scrollbar:on_mouse_left()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
::skip_v_scrollbar::
|
|
|
|
result = self.h_scrollbar:on_mouse_moved(x, y, dx, dy)
|
|
|
|
if result then
|
|
|
|
if result ~= true then
|
2024-02-11 18:51:12 +01:00
|
|
|
self.scroll.to.x = result * self:get_h_scrollable_size()
|
2022-10-16 02:12:15 +02:00
|
|
|
if not config.animate_drag_scroll then
|
|
|
|
self:clamp_scroll_position()
|
|
|
|
self.scroll.x = self.scroll.to.x
|
|
|
|
end
|
2022-04-26 15:54:11 +02:00
|
|
|
end
|
2022-10-16 02:12:15 +02:00
|
|
|
return true
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-04-29 03:50:34 +02:00
|
|
|
function View:on_mouse_left()
|
2022-10-16 02:12:15 +02:00
|
|
|
if not self.scrollable then return end
|
|
|
|
self.v_scrollbar:on_mouse_left()
|
|
|
|
self.h_scrollbar:on_mouse_left()
|
2022-04-29 03:50:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param filename string
|
|
|
|
---@param x number
|
|
|
|
---@param y number
|
|
|
|
---@return boolean
|
2022-02-15 00:45:59 +01:00
|
|
|
function View:on_file_dropped(filename, x, y)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param text string
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:on_text_input(text)
|
|
|
|
-- no-op
|
|
|
|
end
|
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
|
2022-10-16 01:58:51 +02:00
|
|
|
function View:on_ime_text_editing(text, start, length)
|
|
|
|
-- no-op
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
---@param y number @Vertical scroll delta; positive is "up"
|
|
|
|
---@param x number @Horizontal scroll delta; positive is "left"
|
|
|
|
---@return boolean @Capture event
|
|
|
|
function View:on_mouse_wheel(y, x)
|
|
|
|
-- no-op
|
2021-11-07 21:42:03 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-10-11 20:44:32 +02:00
|
|
|
---Can be overriden to listen for scale change events to apply
|
|
|
|
---any neccesary changes in sizes, padding, etc...
|
|
|
|
---@param new_scale number
|
|
|
|
---@param prev_scale number
|
|
|
|
function View:on_scale_change(new_scale, prev_scale) end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:get_content_bounds()
|
|
|
|
local x = self.scroll.x
|
|
|
|
local y = self.scroll.y
|
|
|
|
return x, y, x + self.size.x, y + self.size.y
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@return number x
|
|
|
|
---@return number y
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:get_content_offset()
|
2020-04-30 15:40:26 +02:00
|
|
|
local x = common.round(self.position.x - self.scroll.x)
|
|
|
|
local y = common.round(self.position.y - self.scroll.y)
|
|
|
|
return x, y
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-02 00:54:53 +02:00
|
|
|
function View:clamp_scroll_position()
|
2021-09-08 03:59:12 +02:00
|
|
|
local max = self:get_scrollable_size() - self.size.y
|
|
|
|
self.scroll.to.y = common.clamp(self.scroll.to.y, 0, max)
|
2022-10-16 02:12:15 +02:00
|
|
|
|
|
|
|
max = self:get_h_scrollable_size() - self.size.x
|
|
|
|
self.scroll.to.x = common.clamp(self.scroll.to.x, 0, max)
|
2020-05-02 00:54:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-04-12 02:49:11 +02:00
|
|
|
function View:update_scrollbar()
|
2022-10-16 02:12:15 +02:00
|
|
|
local v_scrollable = self:get_scrollable_size()
|
|
|
|
self.v_scrollbar:set_size(self.position.x, self.position.y, self.size.x, self.size.y, v_scrollable)
|
2024-02-11 18:51:12 +01:00
|
|
|
self.v_scrollbar:set_percent(self.scroll.y/v_scrollable)
|
2022-10-16 02:12:15 +02:00
|
|
|
self.v_scrollbar:update()
|
|
|
|
|
|
|
|
local h_scrollable = self:get_h_scrollable_size()
|
|
|
|
self.h_scrollbar:set_size(self.position.x, self.position.y, self.size.x, self.size.y, h_scrollable)
|
2024-02-11 18:51:12 +01:00
|
|
|
self.h_scrollbar:set_percent(self.scroll.x/h_scrollable)
|
2022-10-16 02:12:15 +02:00
|
|
|
self.h_scrollbar:update()
|
2022-04-12 02:49:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:update()
|
2022-10-11 20:44:32 +02:00
|
|
|
if self.current_scale ~= SCALE then
|
|
|
|
self:on_scale_change(SCALE, self.current_scale)
|
|
|
|
self.current_scale = SCALE
|
|
|
|
end
|
|
|
|
|
2020-05-02 00:54:53 +02:00
|
|
|
self:clamp_scroll_position()
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards(self.scroll, "x", self.scroll.to.x, 0.3, "scroll")
|
|
|
|
self:move_towards(self.scroll, "y", self.scroll.to.y, 0.3, "scroll")
|
2022-10-16 02:12:15 +02:00
|
|
|
if not self.scrollable then return end
|
2022-04-12 02:49:11 +02:00
|
|
|
self:update_scrollbar()
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param color renderer.color
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:draw_background(color)
|
|
|
|
local x, y = self.position.x, self.position.y
|
|
|
|
local w, h = self.size.x, self.size.y
|
2021-10-10 14:52:55 +02:00
|
|
|
renderer.draw_rect(x, y, w, h, color)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-04-12 02:49:11 +02:00
|
|
|
function View:draw_scrollbar()
|
2022-10-18 22:01:32 +02:00
|
|
|
self.v_scrollbar:draw()
|
|
|
|
self.h_scrollbar:draw()
|
2021-08-30 00:50:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:draw()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return View
|