dragonruby-game-toolkit - 如何在 DragonRuby 游戏工具包中创建按钮
问题描述
DragonRuby Game Toolkit 中似乎没有按钮的概念。如何创建按钮等 UI 组件?
解决方案
按钮(和任何其他 UI 组件)可以解构为primitives
:
- 按钮有一个点击空间(通常用边框表示)。
- 有描述按钮功能的文本。
- 响应被点击。
将按钮分解为上述部分会产生以下结果:
def tick args
# create an entity in state that represents the button
args.state.click_me_button.border ||= { x: 10, y: 10, w: 100, h: 50 }
args.state.click_me_button.label ||= { x: 10, y: 10, text: "click me" }
# render the button
args.outputs.borders << args.state.click_me_button.border
args.outputs.labels << args.state.click_me_button.label
# determine if the button has been clicked
if (args.inputs.mouse.click) &&
(args.inputs.mouse.point.inside_rect? args.state.click_me_button.border)
args.gtk.notify! "button was clicked"
end
end
如果你想花哨,可以将上面的按钮代码泛化并放入一个函数中:
# helper method to create a button
def new_button id, x, y, text
# create a hash ("entity") that has some metadata
# about what it represents
entity = {
id: id,
rect: { x: x, y: y, w: 100, h: 50 }
}
# for that entity, define the primitives
# that form it
entity[:primitives] = [
{ x: x, y: y, w: 100, h: 50 }.border,
{ x: x, y: y, text: text }.label
]
entity
end
# helper method for determining if a button was clicked
def button_clicked? args, button
return false unless args.inputs.mouse.click
return args.inputs.mouse.point.inside_rect? button[:rect]
end
def tick args
# use helper method to create a button
args.state.click_me_button ||= new_button :click_me, 10, 10, "click me"
# render button generically using `args.outputs.primitives`
args.outputs.primitives << args.state.click_me_button[:primitives]
# check if the click occurred using the button_clicked? helper method
if button_clicked? args, args.state.click_me_button
args.gtk.notify! "click me button was clicked!"
end
end
推荐阅读
- keras - 我们可以将在一台 PC 上训练的模型转移到另一台 PC
- javascript - 来自 textarea 的计数结果未显示在 HTML 页面上
- java - tomcat 是否也会与 J2EE 一起运行所有 J2SE 代码?
- python - python中嵌套if语句的增量
- java - 在这种情况下,如何将我的 maxDiff 值添加到我的 ArrayList 中?
- javascript - 如何在 Material UI 中使用 Autosuggest
- asp.net - Windows 10 未将变量传递给 default.aspx
- angular - 使用“finalize,catcherror”运算符时出错
- android - 为什么在 Android 9 上不显示通知
- r - R:如何删除饼图中的帮助文本?