2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local config = require "core.config"
|
|
|
|
local style = require "core.style"
|
|
|
|
local common = require "core.common"
|
|
|
|
local Object = require "core.object"
|
|
|
|
|
|
|
|
|
|
|
|
local View = Object:extend()
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
function View:move_towards(t, k, dest, rate)
|
|
|
|
if type(t) ~= "table" then
|
2021-07-06 11:28:38 +02:00
|
|
|
return self:move_towards(t, k, dest, rate)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
local val = t[k]
|
2021-02-21 11:08:25 +01:00
|
|
|
if not config.transitions or math.abs(val - dest) < 0.5 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
|
|
|
|
if val ~= dest then
|
|
|
|
core.redraw = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:try_close(do_close)
|
|
|
|
do_close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:get_name()
|
|
|
|
return "---"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:get_scrollable_size()
|
2020-05-02 00:54:53 +02:00
|
|
|
return math.huge
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:get_scrollbar_rect()
|
|
|
|
local sz = self:get_scrollable_size()
|
2020-05-02 00:54:53 +02:00
|
|
|
if sz <= self.size.y or sz == math.huge then
|
2019-12-28 12:16:32 +01:00
|
|
|
return 0, 0, 0, 0
|
|
|
|
end
|
|
|
|
local h = math.max(20, self.size.y * self.size.y / sz)
|
|
|
|
return
|
|
|
|
self.position.x + self.size.x - style.scrollbar_size,
|
|
|
|
self.position.y + self.scroll.y * (self.size.y - h) / (sz - self.size.y),
|
|
|
|
style.scrollbar_size,
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:scrollbar_overlaps_point(x, y)
|
|
|
|
local sx, sy, sw, sh = self:get_scrollbar_rect()
|
|
|
|
return x >= sx - sw * 3 and x < sx + sw and y >= sy and y < sy + sh
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
if self:scrollbar_overlaps_point(x, y) then
|
|
|
|
self.dragging_scrollbar = true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:on_mouse_released(button, x, y)
|
|
|
|
self.dragging_scrollbar = false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:on_mouse_moved(x, y, dx, dy)
|
|
|
|
if self.dragging_scrollbar then
|
|
|
|
local delta = self:get_scrollable_size() / self.size.y * dy
|
|
|
|
self.scroll.to.y = self.scroll.to.y + delta
|
|
|
|
end
|
|
|
|
self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:on_text_input(text)
|
|
|
|
-- no-op
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:on_mouse_wheel(y)
|
|
|
|
if self.scrollable then
|
|
|
|
self.scroll.to.y = self.scroll.to.y + y * -config.mouse_wheel_scroll
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
local max = self:get_scrollable_size() - self.size.y
|
|
|
|
self.scroll.to.y = common.clamp(self.scroll.to.y, 0, max)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function View:update()
|
2020-05-02 00:54:53 +02:00
|
|
|
self:clamp_scroll_position()
|
2019-12-28 12:16:32 +01:00
|
|
|
self:move_towards(self.scroll, "x", self.scroll.to.x, 0.3)
|
|
|
|
self:move_towards(self.scroll, "y", self.scroll.to.y, 0.3)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:draw_background(color)
|
|
|
|
local x, y = self.position.x, self.position.y
|
|
|
|
local w, h = self.size.x, self.size.y
|
2020-05-01 17:15:28 +02:00
|
|
|
renderer.draw_rect(x, y, w + x % 1, h + y % 1, color)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:draw_scrollbar()
|
|
|
|
local x, y, w, h = self:get_scrollbar_rect()
|
|
|
|
local highlight = self.hovered_scrollbar or self.dragging_scrollbar
|
|
|
|
local color = highlight and style.scrollbar2 or style.scrollbar
|
|
|
|
renderer.draw_rect(x, y, w, h, color)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function View:draw()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return View
|