Skip to content

Commit

Permalink
feat: add mp.input menu support
Browse files Browse the repository at this point in the history
  • Loading branch information
dyphire committed Oct 16, 2024
1 parent 4107b94 commit f04d552
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# uosc_danmaku
在MPV播放器中加载弹弹play弹幕,基于 uosc UI框架和弹弹play API的mpv弹幕扩展插件
> [!NOTE]
>
> 已添加对mpv内部`mp.input`的支持,在uosc不可用时通过键绑定调用此方式渲染菜单
>
> mpv最低版本要求:0.39.0
## 项目简介

Expand Down Expand Up @@ -253,6 +258,7 @@ autoload_for_url=yes
#### 功能说明

保存哈希匹配的弹幕关联结果。启用时可以避免同番剧不同剧集的反复哈希匹配;禁用时对同目录文件始终进行哈希匹配(仅当同目录从未执行过手动搜索)

禁用时可以应对边缘案例:同目录存在同一番剧的 OVA 和 MOVIE;同一番剧的剧集文件命名格式不同;同目录存在多个不同番剧。在这些情况下这些命名特殊无法获取到正确剧集数的文件可以通过不保存哈希匹配的弹幕关联以始终对同目录文件执行哈希匹配以尝试加载其对应的弹幕。

#### 使用方法
Expand Down
116 changes: 105 additions & 11 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local msg = require 'mp.msg'
local options = require("options")
require("api")

local input_loaded, input = pcall(require, "mp.input")
local uosc_available = false

function get_animes(query)
local encoded_query = url_encode(query)

Expand Down Expand Up @@ -31,21 +34,33 @@ function get_animes(query)
local items = {}

local message = "加载数据中..."
update_menu(menu_item(message), query)
if uosc_available then
update_menu(menu_item(message), query)
else
mp.osd_message(message, 30)
end

local res = mp.command_native(cmd)

if res.status ~= 0 then
local message = "获取数据失败"
update_menu(menu_item(message), query)
if uosc_available then
update_menu(menu_item(message), query)
else
mp.osd_message(message, 3)
end
msg.error("HTTP Request failed: " .. res.stderr)
end

local response = utils.parse_json(res.stdout)

if not response or not response.animes then
local message = "无结果"
update_menu(menu_item(message), query)
if uosc_available then
update_menu(menu_item(message), query)
else
mp.osd_message(message, 3)
end
msg.verbose("无结果")
return
end
Expand All @@ -63,8 +78,14 @@ function get_animes(query)
})
end

update_menu(items, query)

if uosc_available then
update_menu(items, query)
elseif input_loaded then
mp.osd_message("", 0)
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end

function get_episodes(episodes)
Expand All @@ -85,7 +106,13 @@ function get_episodes(episodes)
items = items,
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
if uosc_available then
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
elseif input_loaded then
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end

function menu_item(input)
Expand All @@ -109,8 +136,37 @@ function update_menu(items, query)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end

function open_menu_select(menu_items)
local item_titles, item_values = {}, {}
for i, v in ipairs(menu_items) do
item_titles[i] = v.hint and v.title .. " (" .. v.hint .. ")" or v.title
item_values[i] = v.value
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = '筛选:',
items = item_titles,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
end

-- 打开输入菜单
function open_input_menu()
function open_input_menu_get()
mp.commandv('script-message-to', 'console', 'disable')
input.get({
prompt = '番剧名称:',
default_text = get_title(true),
cursor_position = get_title(true) and #get_title(true) + 1,
submit = function(text)
input.terminate()
mp.commandv("script-message-to", mp.get_script_name(), "search-anime-event", text)
end
})
end

function open_input_menu_uosc()
local menu_props = {
type = "menu_danmaku",
title = "在此处输入动画名称",
Expand All @@ -133,7 +189,18 @@ function open_input_menu()
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end

function open_add_menu()
function open_add_menu_get()
mp.commandv('script-message-to', 'console', 'disable')
input.get({
prompt = 'Input url:',
submit = function(text)
input.terminate()
mp.commandv("script-message-to", mp.get_script_name(), "add-source-event", text)
end
})
end

function open_add_menu_uosc()
local menu_props = {
type = "menu_source",
title = "在此输入源地址url",
Expand All @@ -155,6 +222,22 @@ function open_add_menu()
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end

function open_input_menu()
if uosc_available then
open_input_menu_uosc()
elseif input_loaded then
open_input_menu_get()
end
end

function open_add_menu()
if uosc_available then
open_add_menu_uosc()
elseif input_loaded then
open_add_menu_get()
end
end

mp.commandv(
"script-message-to",
"uosc",
Expand All @@ -179,14 +262,23 @@ mp.commandv(
})
)


mp.register_script_message('uosc-version', function()
uosc_available = true
end)

-- 注册函数给 uosc 按钮使用
mp.register_script_message("open_search_danmaku_menu", open_input_menu)
mp.register_script_message("search-anime-event", function(query)
mp.commandv("script-message-to", "uosc", "update-menu", "menu_danmaku")
if uosc_available then
mp.commandv("script-message-to", "uosc", "update-menu", "menu_danmaku")
end
get_animes(query)
end)
mp.register_script_message("search-episodes-event", function(episodes)
mp.commandv("script-message-to", "uosc", "close-menu", "menu_anime")
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_anime")
end
get_episodes(utils.parse_json(episodes))
end)

Expand All @@ -197,7 +289,9 @@ end)

mp.register_script_message("open_add_source_menu", open_add_menu)
mp.register_script_message("add-source-event", function(query)
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
end
add_danmaku_source(query)
end)

Expand Down

0 comments on commit f04d552

Please sign in to comment.