在 Rebol3 Saphir 中,我正在编写一个游戏,我一直在研究事件处理程序功能以及演员,我想知道使用事件处理程序来为游戏进行键盘控制是否是一个更好的主意或将参与者添加到 GUI 元素之一中。
如果我使用演员,在什么级别?我目前正在image!
为屏幕使用一种类型。我可以将演员添加到 root( layout
) 面,这样即使我在 GUI 上单击(给予焦点)按钮,焦点也会离开图像并且不会使用键盘控制。
在 Rebol3 Saphir 中,我正在编写一个游戏,我一直在研究事件处理程序功能以及演员,我想知道使用事件处理程序来为游戏进行键盘控制是否是一个更好的主意或将参与者添加到 GUI 元素之一中。
如果我使用演员,在什么级别?我目前正在image!
为屏幕使用一种类型。我可以将演员添加到 root( layout
) 面,这样即使我在 GUI 上单击(给予焦点)按钮,焦点也会离开图像并且不会使用键盘控制。
下面是一个如何做到这一点的示例......如果您不需要按键事件,您也可以只使用内置的键盘快捷键功能。在这种情况下,请参见此处的快捷键示例:https ://github.com/saphirion/documentation/blob/master/r3/r3-gui/examples/layouts/layout-15.r3
此版本已修改为与 R3-GUI 的当前 (09/26/13) 版本一起使用
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
]
]