2021-03-13 12:57:52 +01:00
|
|
|
local core = require "core"
|
2021-04-04 16:11:47 +02:00
|
|
|
local command = require "core.command"
|
2021-03-13 12:57:52 +01:00
|
|
|
local common = require "core.common"
|
2021-04-07 23:46:45 +02:00
|
|
|
local config = require "core.config"
|
2021-03-13 12:57:52 +01:00
|
|
|
local View = require "core.view"
|
|
|
|
local style = require "core.style"
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
local BORDER_WIDTH = common.round(1 * SCALE)
|
|
|
|
local UNDERLINE_WIDTH = common.round(2 * SCALE)
|
|
|
|
local UNDERLINE_MARGIN = common.round(1 * SCALE)
|
2021-03-13 12:57:52 +01:00
|
|
|
|
|
|
|
local noop = function() end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@class core.nagview : core.view
|
|
|
|
---@field super core.view
|
2021-03-13 12:57:52 +01:00
|
|
|
local NagView = View:extend()
|
|
|
|
|
|
|
|
function NagView:new()
|
|
|
|
NagView.super.new(self)
|
|
|
|
self.size.y = 0
|
2021-12-28 16:39:40 +01:00
|
|
|
self.show_height = 0
|
2021-03-13 12:57:52 +01:00
|
|
|
self.force_focus = false
|
2021-04-04 16:11:47 +02:00
|
|
|
self.queue = {}
|
2022-03-10 09:27:04 +01:00
|
|
|
self.scrollable = true
|
|
|
|
self.target_height = 0
|
|
|
|
self.on_mouse_pressed_root = nil
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function NagView:get_title()
|
|
|
|
return self.title
|
|
|
|
end
|
|
|
|
|
2021-04-07 23:46:45 +02:00
|
|
|
-- The two methods below are duplicated from DocView
|
|
|
|
function NagView:get_line_height()
|
|
|
|
return math.floor(style.font:get_height() * config.line_height)
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2021-04-07 23:46:45 +02:00
|
|
|
function NagView:get_line_text_y_offset()
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
local th = style.font:get_height()
|
|
|
|
return (lh - th) / 2
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Buttons height without padding
|
|
|
|
function NagView:get_buttons_height()
|
|
|
|
local lh = style.font:get_height()
|
|
|
|
local bt_padding = lh / 2
|
|
|
|
return lh + 2 * BORDER_WIDTH + 2 * bt_padding
|
|
|
|
end
|
|
|
|
|
|
|
|
function NagView:get_target_height()
|
|
|
|
return self.target_height + 2 * style.padding.y
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
function NagView:get_scrollable_size()
|
|
|
|
local w, h = system.get_window_size()
|
|
|
|
if self.visible and self:get_target_height() > h then
|
|
|
|
self.size.y = h
|
|
|
|
return self:get_target_height()
|
2021-04-04 16:11:47 +02:00
|
|
|
else
|
2022-03-10 09:27:04 +01:00
|
|
|
self.size.y = 0
|
2021-04-04 16:11:47 +02:00
|
|
|
end
|
2022-03-10 09:27:04 +01:00
|
|
|
return 0
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2021-12-28 16:39:40 +01:00
|
|
|
function NagView:dim_window_content()
|
2021-03-13 12:57:52 +01:00
|
|
|
local ox, oy = self:get_content_offset()
|
2021-12-28 16:39:40 +01:00
|
|
|
oy = oy + self.show_height
|
2021-03-13 12:57:52 +01:00
|
|
|
local w, h = core.root_view.size.x, core.root_view.size.y - oy
|
|
|
|
core.root_view:defer_draw(function()
|
2024-02-11 18:51:12 +01:00
|
|
|
renderer.draw_rect(ox, oy, w, h, style.nagbar_dim)
|
2021-03-13 12:57:52 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
function NagView:change_hovered(i)
|
|
|
|
if i ~= self.hovered_item then
|
|
|
|
self.hovered_item = i
|
|
|
|
self.underline_progress = 0
|
|
|
|
core.redraw = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function NagView:each_option()
|
2021-03-13 12:57:52 +01:00
|
|
|
return coroutine.wrap(function()
|
2021-04-04 16:11:47 +02:00
|
|
|
if not self.options then return end
|
|
|
|
local opt, bw,bh,ox,oy
|
2021-04-07 23:46:45 +02:00
|
|
|
bh = self:get_buttons_height()
|
2021-04-04 16:11:47 +02:00
|
|
|
ox,oy = self:get_content_offset()
|
|
|
|
ox = ox + self.size.x
|
2021-12-28 16:39:40 +01:00
|
|
|
oy = oy + self.show_height - bh - style.padding.y
|
2021-04-04 16:11:47 +02:00
|
|
|
|
|
|
|
for i = #self.options, 1, -1 do
|
|
|
|
opt = self.options[i]
|
2023-02-06 18:38:00 +01:00
|
|
|
bw = style.font:get_width(opt.text) + 2 * BORDER_WIDTH + style.padding.x
|
2021-04-04 16:11:47 +02:00
|
|
|
|
2021-03-13 12:57:52 +01:00
|
|
|
ox = ox - bw - style.padding.x
|
2021-04-04 16:11:47 +02:00
|
|
|
coroutine.yield(i, opt, ox,oy,bw,bh)
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function NagView:on_mouse_moved(mx, my, ...)
|
2022-03-10 09:27:04 +01:00
|
|
|
if not self.visible then return end
|
|
|
|
core.set_active_view(self)
|
2021-03-13 12:57:52 +01:00
|
|
|
NagView.super.on_mouse_moved(self, mx, my, ...)
|
2021-04-04 16:11:47 +02:00
|
|
|
for i, _, x,y,w,h in self:each_option() do
|
2021-03-13 12:57:52 +01:00
|
|
|
if mx >= x and my >= y and mx < x + w and my < y + h then
|
2021-04-04 16:11:47 +02:00
|
|
|
self:change_hovered(i)
|
2021-03-13 12:57:52 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2021-04-04 16:11:47 +02:00
|
|
|
end
|
2021-03-13 12:57:52 +01:00
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
local function register_mouse_pressed(self)
|
|
|
|
if self.on_mouse_pressed_root then return end
|
2021-12-29 16:00:53 +01:00
|
|
|
-- RootView is loaded locally to avoid NagView and RootView being
|
|
|
|
-- mutually recursive
|
|
|
|
local RootView = require "core.rootview"
|
2022-03-10 09:27:04 +01:00
|
|
|
self.on_mouse_pressed_root = RootView.on_mouse_pressed
|
|
|
|
local this = self
|
|
|
|
function RootView:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
if
|
|
|
|
not this:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
then
|
|
|
|
return this.on_mouse_pressed_root(self, button, x, y, clicks)
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
2021-12-29 16:00:53 +01:00
|
|
|
end
|
2022-03-10 09:27:04 +01:00
|
|
|
self.new_on_mouse_pressed_root = RootView.on_mouse_pressed
|
2021-12-29 16:00:53 +01:00
|
|
|
end
|
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
local function unregister_mouse_pressed(self)
|
2021-12-29 16:00:53 +01:00
|
|
|
local RootView = require "core.rootview"
|
2022-03-10 09:27:04 +01:00
|
|
|
if
|
|
|
|
self.on_mouse_pressed_root
|
|
|
|
and
|
|
|
|
-- just in case prevent overwriting what something else may
|
|
|
|
-- have overwrote after us, but after testing with various
|
|
|
|
-- plugins this doesn't seems to happen, but just in case
|
|
|
|
self.new_on_mouse_pressed_root == RootView.on_mouse_pressed
|
|
|
|
then
|
|
|
|
RootView.on_mouse_pressed = self.on_mouse_pressed_root
|
|
|
|
self.on_mouse_pressed_root = nil
|
|
|
|
self.new_on_mouse_pressed_root = nil
|
2021-12-29 16:00:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
function NagView:on_mouse_pressed(button, mx, my, clicks)
|
2022-03-10 09:27:04 +01:00
|
|
|
if not self.visible then return false end
|
2021-12-29 16:00:53 +01:00
|
|
|
if NagView.super.on_mouse_pressed(self, button, mx, my, clicks) then return true end
|
2021-04-04 16:11:47 +02:00
|
|
|
for i, _, x,y,w,h in self:each_option() do
|
|
|
|
if mx >= x and my >= y and mx < x + w and my < y + h then
|
|
|
|
self:change_hovered(i)
|
|
|
|
command.perform "dialog:select"
|
|
|
|
end
|
|
|
|
end
|
2022-03-10 09:27:04 +01:00
|
|
|
return true
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
function NagView:on_text_input(text)
|
2022-03-10 09:27:04 +01:00
|
|
|
if not self.visible then return end
|
2021-04-04 16:11:47 +02:00
|
|
|
if text:lower() == "y" then
|
|
|
|
command.perform "dialog:select-yes"
|
|
|
|
elseif text:lower() == "n" then
|
|
|
|
command.perform "dialog:select-no"
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
function NagView:update()
|
|
|
|
if not self.visible and self.show_height <= 0 then return end
|
|
|
|
NagView.super.update(self)
|
2021-04-07 23:46:45 +02:00
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
if self.visible and core.active_view == self and self.title then
|
2024-02-11 18:51:12 +01:00
|
|
|
self:move_towards(self, "show_height", self:get_target_height(), nil, "nagbar")
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards(self, "underline_progress", 1, nil, "nagbar")
|
2022-03-10 09:27:04 +01:00
|
|
|
else
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards(self, "show_height", 0, nil, "nagbar")
|
2022-03-10 09:27:04 +01:00
|
|
|
if self.show_height <= 0 then
|
|
|
|
self.title = nil
|
|
|
|
self.message = nil
|
|
|
|
self.options = nil
|
|
|
|
self.on_selected = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-13 12:57:52 +01:00
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
local function draw_nagview_message(self)
|
2021-12-28 16:39:40 +01:00
|
|
|
self:dim_window_content()
|
2021-03-13 12:57:52 +01:00
|
|
|
|
2021-12-28 16:39:40 +01:00
|
|
|
-- draw message's background
|
2021-03-13 12:57:52 +01:00
|
|
|
local ox, oy = self:get_content_offset()
|
2021-12-28 16:39:40 +01:00
|
|
|
renderer.draw_rect(ox, oy, self.size.x, self.show_height, style.nagbar)
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
ox = ox + style.padding.x
|
|
|
|
|
2022-03-10 09:27:04 +01:00
|
|
|
core.push_clip_rect(ox, oy, self.size.x, self.show_height)
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
-- if there are other items, show it
|
|
|
|
if #self.queue > 0 then
|
|
|
|
local str = string.format("[%d]", #self.queue)
|
2021-12-28 16:39:40 +01:00
|
|
|
ox = common.draw_text(style.font, style.nagbar_text, str, "left", ox, oy, self.size.x, self.show_height)
|
2021-04-04 16:11:47 +02:00
|
|
|
ox = ox + style.padding.x
|
|
|
|
end
|
|
|
|
|
|
|
|
-- draw message
|
2021-04-07 23:46:45 +02:00
|
|
|
local lh = style.font:get_height() * config.line_height
|
|
|
|
oy = oy + style.padding.y + (self.target_height - self:get_message_height()) / 2
|
|
|
|
for msg_line in self.message:gmatch("(.-)\n") do
|
|
|
|
local ty = oy + self:get_line_text_y_offset()
|
|
|
|
renderer.draw_text(style.font, msg_line, ox, ty, style.nagbar_text)
|
|
|
|
oy = oy + lh
|
|
|
|
end
|
2021-03-13 12:57:52 +01:00
|
|
|
|
|
|
|
-- draw buttons
|
2021-04-04 16:11:47 +02:00
|
|
|
for i, opt, bx,by,bw,bh in self:each_option() do
|
|
|
|
local fw,fh = bw - 2 * BORDER_WIDTH, bh - 2 * BORDER_WIDTH
|
|
|
|
local fx,fy = bx + BORDER_WIDTH, by + BORDER_WIDTH
|
2021-03-13 12:57:52 +01:00
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
-- draw the button
|
2021-03-13 12:57:52 +01:00
|
|
|
renderer.draw_rect(bx,by,bw,bh, style.nagbar_text)
|
2021-04-04 16:11:47 +02:00
|
|
|
renderer.draw_rect(fx,fy,fw,fh, style.nagbar)
|
|
|
|
|
|
|
|
if i == self.hovered_item then -- draw underline
|
|
|
|
local uw = fw - 2 * UNDERLINE_MARGIN
|
|
|
|
local halfuw = uw / 2
|
|
|
|
local lx = fx + UNDERLINE_MARGIN + halfuw - (halfuw * self.underline_progress)
|
|
|
|
local ly = fy + fh - UNDERLINE_MARGIN - UNDERLINE_WIDTH
|
|
|
|
uw = uw * self.underline_progress
|
|
|
|
renderer.draw_rect(lx,ly,uw,UNDERLINE_WIDTH, style.nagbar_text)
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2023-02-06 18:38:00 +01:00
|
|
|
common.draw_text(style.font, style.nagbar_text, opt.text, "center", fx,fy,fw,fh)
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
2022-03-10 09:27:04 +01:00
|
|
|
|
|
|
|
self:draw_scrollbar()
|
|
|
|
|
|
|
|
core.pop_clip_rect()
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2021-12-28 16:39:40 +01:00
|
|
|
function NagView:draw()
|
2022-03-10 09:27:04 +01:00
|
|
|
if (not self.visible and self.show_height <= 0) or not self.title then
|
|
|
|
return
|
|
|
|
end
|
2021-12-28 16:39:40 +01:00
|
|
|
core.root_view:defer_draw(draw_nagview_message, self)
|
|
|
|
end
|
|
|
|
|
2023-02-06 18:38:00 +01:00
|
|
|
function NagView:on_scale_change(new_scale, old_scale)
|
|
|
|
BORDER_WIDTH = common.round(1 * new_scale)
|
|
|
|
UNDERLINE_WIDTH = common.round(2 * new_scale)
|
|
|
|
UNDERLINE_MARGIN = common.round(1 * new_scale)
|
|
|
|
self.target_height = math.max(
|
|
|
|
self:get_message_height(),
|
|
|
|
self:get_buttons_height()
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-07 23:46:45 +02:00
|
|
|
function NagView:get_message_height()
|
|
|
|
local h = 0
|
|
|
|
for str in string.gmatch(self.message, "(.-)\n") do
|
|
|
|
h = h + style.font:get_height() * config.line_height
|
|
|
|
end
|
|
|
|
return h
|
|
|
|
end
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
function NagView:next()
|
|
|
|
local opts = table.remove(self.queue, 1) or {}
|
2022-03-10 09:27:04 +01:00
|
|
|
if opts.title and opts.message and opts.options then
|
|
|
|
self.visible = true
|
|
|
|
self.title = opts.title
|
|
|
|
self.message = opts.message and opts.message .. "\n"
|
|
|
|
self.options = opts.options
|
|
|
|
self.on_selected = opts.on_selected
|
|
|
|
|
2021-04-07 23:46:45 +02:00
|
|
|
local message_height = self:get_message_height()
|
|
|
|
-- self.target_height is the nagview height needed to display the message and
|
|
|
|
-- the buttons, excluding the top and bottom padding space.
|
|
|
|
self.target_height = math.max(message_height, self:get_buttons_height())
|
2021-06-14 03:28:29 +02:00
|
|
|
self:change_hovered(common.find_index(self.options, "default_yes"))
|
2022-03-10 09:27:04 +01:00
|
|
|
|
|
|
|
self.force_focus = true
|
|
|
|
core.set_active_view(self)
|
2021-12-29 16:00:53 +01:00
|
|
|
-- We add a hook to manage all the mouse_pressed events.
|
2022-03-10 09:27:04 +01:00
|
|
|
register_mouse_pressed(self)
|
2021-12-29 16:00:53 +01:00
|
|
|
else
|
2022-03-10 09:27:04 +01:00
|
|
|
self.force_focus = false
|
|
|
|
core.set_active_view(core.next_active_view or core.last_active_view)
|
|
|
|
self.visible = false
|
|
|
|
unregister_mouse_pressed(self)
|
2021-12-29 16:00:53 +01:00
|
|
|
end
|
2021-03-13 12:57:52 +01:00
|
|
|
end
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
function NagView:show(title, message, options, on_select)
|
|
|
|
local opts = {}
|
|
|
|
opts.title = assert(title, "No title")
|
|
|
|
opts.message = assert(message, "No message")
|
|
|
|
opts.options = assert(options, "No options")
|
|
|
|
opts.on_selected = on_select or noop
|
|
|
|
table.insert(self.queue, opts)
|
2022-03-10 09:27:04 +01:00
|
|
|
self:next()
|
2021-04-04 16:11:47 +02:00
|
|
|
end
|
|
|
|
|
2021-06-07 23:00:29 +02:00
|
|
|
return NagView
|