按钮悬停换图、按下效果和音效
这一页给按钮加上手感:光标移上去变亮、点下去有声音、点完换成「选中」的样子。
还没写过画面的话,先做完 从零写第一个画面。
第 1 步:新建文件
在 plugins/CloudUI/screens/ 里新建 tut_hover.yml,先放背景、标题和关闭按钮:
portrait:
enabled: false
camera:
animation: false
elements:
backdrop:
type: image
image: main/backdrop.png
x: 0
y: 0
fit: cover
resolution: 0.25
title:
type: text
text: '悬停与音效'
font: title
size: 48
color: '#E6D3A1'
x: 960
y: 280
align: center
close:
type: button
image: main/close.png
hover: main/close_hover.png
x: 842
y: 840
click:
left: ['close']
close-label:
type: text
text: '关闭'
font: text
size: 20
color: '#E2EEF4'
x: 960
y: 857
align: center
关闭按钮已经有悬停效果了:hover 那一行就是。
第 2 步:悬停换图
在 elements 下面加一个大按钮:
big:
type: button
image: main/hero.png
hover: main/hero_hover.png
x: 704
y: 500
hit: [708, 504, 504, 224]
click:
left:
- 'sound: entity.experience_orb.pickup 1 1.2'
- 'notice: <green>你点了大按钮'
big-label:
type: text
text: '点我试试'
font: title
size: 44
color: '#E2EEF4'
x: 960
y: 597
align: center
image是平常的图,hover是光标停在按钮上时叠在上面的图。两张图要一样大。hover是叠上去,不是换掉。所以悬停图可以只画一圈亮边或一层光,其余透明。- 自己做悬停图时,最简单的做法是复制一份平常的图,调亮一点,另存一个文件名。
hit 决定光标停在哪里才算悬停。圆形或不规则的按钮可以加 hit-shape:ellipse 只有范围里的椭圆算,hover 只有悬停图不透明的地方算。
第 3 步:点下去的声音
点击有两层声音。
第一层是全服统一的。plugins/CloudUI/config.yml 里的 sounds:
sounds:
enabled: true
click: 'cloudui:ui.click 1 1'
open: 'cloudui:ui.open 1 1'
close: 'cloudui:ui.open 1 1'
mute-water: true
hover: ''
deny: 'block.note_block.bass 0.5 0.8'
格式是 音效ID 音量 音调,留空就是不播放。
click:点到任何一个有动作的按钮都会响。hover:光标移到按钮上时响,默认留空不播放。想要的话填一个短促的声音,例如'ui.button.click 0.3 1.6'。deny:没有权限、花费不够时响。open/close:打开、关闭画面。
第二层是按钮自己的。第 2 步里的 sound: entity.experience_orb.pickup 1 1.2 就是:点这个按钮时,除了统一的点击声,再多播一个声音。音效 ID 用原版的名字,和 /playsound 指令里的一样。
想把统一的点击声换成自己的,放一个 Ogg Vorbis 文件到 plugins/CloudUI/sounds/ui_click.ogg;打开和关闭的声音放 sounds/ui_open.ogg。
第 4 步:按下后换成选中的样子
按钮没有单独的「按下图」。点下去的反馈靠三样:上面的点击声、动作里的 notice 之类的提示,以及这一步要做的,点完把按钮换成另一张图。
做法是让按钮的图跟着一个值走,点击时改这个值。先在文件最上面(和 portrait 同一层)加:
values:
pick: a
这是给画面一个叫 pick 的值,默认是 a。然后在 elements 下面加两个按钮:
opt-a:
type: button
image: teleport/tab.png
hover: teleport/tab_hover.png
state: '{pick}'
states: {a: teleport/tab_active.png}
x: 682
y: 400
click:
left:
- 'sound: block.note_block.pling 0.6 1.5'
- 'console: cui set {player} pick=a'
opt-a-label:
type: text
text: '选项一'
font: title
size: 24
color: '#E2EEF4'
x: 817
y: 418
align: center
opt-b:
type: button
image: teleport/tab.png
hover: teleport/tab_hover.png
state: '{pick}'
states: {b: teleport/tab_active.png}
x: 967
y: 400
click:
left:
- 'sound: block.note_block.pling 0.6 1.8'
- 'console: cui set {player} pick=b'
opt-b-label:
type: text
text: '选项二'
font: title
size: 24
color: '#E2EEF4'
x: 1102
y: 418
align: center
state: '{pick}':这个按钮看pick的值。states: {a: teleport/tab_active.png}:值是a时改用这张图;是别的值就用image。console: cui set {player} pick=a:由后台执行/cui set,把这位玩家开着的画面的pick改成a,画面马上跟着变。
这个值只在画面开着的时候有效,关掉再打开又回到 values 里的默认值。要长期记住玩家选了什么,得靠别的插件存,再用 PlaceholderAPI 变量当 state。
第 5 步:文字悬停变色
不想做按钮图,也可以让一行字直接能点、移上去变色:
link:
type: text
text: '查看说明'
font: text
size: 20
color: '#96AABA'
hover-color: '#FFE08A'
x: 960
y: 770
align: center
click:
left: ['message: <gray>这是一行可以点的文字。']
点击范围就是这行字本身。hover-color 只对写死的文字有效,带变量或颜色码的文字照样能点,但不会变色。
第 6 步:重载,打开
存档后输入 /cui reload,再输入 /cui open tut_hover。
你应该看到:
- 上面两个并排的选项,「选项一」是亮的。点「选项二」,听到一声提示音,亮的换到「选项二」。
- 中间一个大按钮,光标移上去变亮,点下去听到拾取经验的声音,顶端出现通知。
- 下面一行灰色的「查看说明」,光标移上去变成黄色,点一下聊天栏出现一行字。
完整文件
values:
pick: a
portrait:
enabled: false
camera:
animation: false
elements:
backdrop:
type: image
image: main/backdrop.png
x: 0
y: 0
fit: cover
resolution: 0.25
title:
type: text
text: '悬停与音效'
font: title
size: 48
color: '#E6D3A1'
x: 960
y: 280
align: center
opt-a:
type: button
image: teleport/tab.png
hover: teleport/tab_hover.png
state: '{pick}'
states: {a: teleport/tab_active.png}
x: 682
y: 400
click:
left:
- 'sound: block.note_block.pling 0.6 1.5'
- 'console: cui set {player} pick=a'
opt-a-label:
type: text
text: '选项一'
font: title
size: 24
color: '#E2EEF4'
x: 817
y: 418
align: center
opt-b:
type: button
image: teleport/tab.png
hover: teleport/tab_hover.png
state: '{pick}'
states: {b: teleport/tab_active.png}
x: 967
y: 400
click:
left:
- 'sound: block.note_block.pling 0.6 1.8'
- 'console: cui set {player} pick=b'
opt-b-label:
type: text
text: '选项二'
font: title
size: 24
color: '#E2EEF4'
x: 1102
y: 418
align: center
big:
type: button
image: main/hero.png
hover: main/hero_hover.png
x: 704
y: 500
hit: [708, 504, 504, 224]
click:
left:
- 'sound: entity.experience_orb.pickup 1 1.2'
- 'notice: <green>你点了大按钮'
big-label:
type: text
text: '点我试试'
font: title
size: 44
color: '#E2EEF4'
x: 960
y: 597
align: center
link:
type: text
text: '查看说明'
font: text
size: 20
color: '#96AABA'
hover-color: '#FFE08A'
x: 960
y: 770
align: center
click:
left: ['message: <gray>这是一行可以点的文字。']
close:
type: button
image: main/close.png
hover: main/close_hover.png
x: 842
y: 840
click:
left: ['close']
close-label:
type: text
text: '关闭'
font: text
size: 20
color: '#E2EEF4'
x: 960
y: 857
align: center
常见问题
/cui reload 提示 hit-shape: hover needs a hover image。 hit-shape: hover 是照悬停图的形状判断点击,按钮必须写 hover。
带 states 的按钮悬停变亮慢半拍。 一般按钮的悬停由玩家本机的显卡画,没有延迟;带 states 的按钮默认由服务器画,会慢一点点。这个按钮确定永远不会整个消失时,可以加一行 hover-gpu: true 改由显卡画。会消失的按钮不要加,否则空的位置也会亮。
填了音效但听不到。 先看 config.yml 的 sounds.enabled 是不是 true、音效 ID 有没有拼错,再看玩家客户端的音量设定。改 config.yml 之后也要 /cui reload。