在 Rebol3 Saphir 中,我正在编写一个游戏,我一直在研究事件处理程序功能和 actor,我想知道使用事件处理程序来获取游戏的键盘控制或将 actor 添加到其中一个 GUI 元素中是否是一个更好的主意。
如果我使用演员,在什么层面上? 我目前正在为屏幕使用image!
类型。 我可以在根( layout
)脸上添加一个演员吗,这样即使我在 GUI 上单击(焦点)按钮,焦点也会离开图像,并且不会进行键盘控制。
下面是一个如何做到这一点的例子......如果您不需要键控事件,也可以仅使用内置键盘快捷键功能。在这种情况下,请参阅此处使用快捷键的示例:https://github.com/saphirion/documentation/blob/master/r3/r3-gui/examples/layouts/layout-15.r3
此版本经过修改,可与当前 (09/26/13) 版本的 R3-GUI 配合使用
REBOL [
author: "cyphre@seznam.cz"
]
load-gui
;image for game screen
img: make image! 400x400
;just some example game object
game-object: context [
offset: 0x0
draw-block: [
pen red
fill-pen white
line-width 5
circle offset 20
]
move: func [
delta [pair!]
][
offset: offset + delta
img/rgb: black
;note: this is not optimal way how to render DRAW objects
;but I use image! here because the asking SO user uses image! as well
;better way is to just use DRAWING style for DRAW based graphics
draw img to-draw draw-block copy []
draw-face game-screen
;signal true move has been executed
return true
]
]
stylize [
;backup the original window style to be able call the original key actor
window-orig: window []
;override window style with our key actor
window: window [
actors: [
on-key: [
;execute our key controls prior to 'system' key handling
switch arg/type [
key [
;here you can handle key-down events
moved?: switch/default arg/key [
up [
game-object/move 0x-5
]
down [
game-object/move 0x5
]
left [
game-object/move -5x0
]
right [
game-object/move 5x0
]
][
false
]
]
key-up [
;here you can handle key-up events
]
]
;for example filter out faces that shouldn't get the system key events (for example editable styles)
unless all [
moved?
guie/focal-face
tag-face? guie/focal-face 'edit
][
;handle the system key handling
do-actor/style face 'on-key arg 'window-orig
]
]
]
]
]
view [
title "Custom keyboard handling (whole window)"
text "press cursor keys to move the box"
game-screen: image options [min-size: 400x400 max-size: 400x400]
text 400 "focus the field below and press to see these keys are filtered out, but other keys works normally"
field "lorem ipsum"
when [enter] on-action [
;initialize game object
game-object/move 200x200
set-face game-screen img
]
]
此版本使用尚未发布的 R3-GUI 版本:
REBOL [
author: "cyphre@seznam.cz"
]
;image for game screen
img: make image! 400x400
;just some example game object
game-object: context [
offset: 0x0
draw-block: [
pen red
fill-pen white
line-width 5
circle offset 20
]
move: func [
delta [pair!]
][
offset: offset + delta
img/rgb: black
;note: this is not optimal way how to render DRAW objects
;but I use image! here because the asking SO user uses image! as well
;better way is to just use DRAWING style for DRAW based graphics
draw img to-draw draw-block copy []
draw-face game-screen
;signal true move has been executed
return true
]
]
stylize [
;backup the original window style to be able call the original key actor
window-orig: window []
;override window style with our key actor
window: window [
actors: [
on-key: [
;execute our key controls prior to 'system' key handling
switch arg/type [
key [
;here you can handle key-down events
moved?: switch/default arg/key [
up [
game-object/move 0x-5
]
down [
game-object/move 0x5
]
left [
game-object/move -5x0
]
right [
game-object/move 5x0
]
][
false
]
]
key-up [
;here you can handle key-up events
]
]
;for example filter out faces that shouldn't get the system key events (for example editable styles)
unless all [
moved?
guie/focal-face
tag-face? guie/focal-face 'edit
][
;handle the system key handling
do-actor/style face 'on-key arg 'window-orig
]
]
]
]
]
view [
title "Custom keyboard handling (whole window)"
text "press cursor keys to move the box"
game-screen: image img options [min-size: 400x400 max-size: 400x400]
text 400 "focus the field below and press to see these keys are filtered out, but other keys works normally"
field "lorem ipsum"
when [enter] on-action [
;initialize game object
game-object/move 200x200
]
]