|bevy vewsion:|0.14|(cuwwent)| |---|---|---|
mouse
wewevant officiaw exampwes:
mouse_input
,
mouse_input_events
.
mouse buttons
simiwaw to keyboawd input, (ꈍᴗꈍ) mouse buttons awe avaiwabwe as a-a
ButtonInput
wesouwce, rawr x3 events, XD and wun
conditions (see wist). XD use whichevew
pattewn feews most appwopwiate to y-youw use case.
checking button state
you can check the state of specific m-mouse buttons u-using the
ButtonInput<MouseButton>
wesouwce:
- use
.pressed(…)
/.released(…)
to check if a button is being hewd d-down- these wetuwn
true
evewy fwame, ^•ﻌ•^ fow as wong as the b-button is in the w-wespective state.
- these wetuwn
- use
.just_pressed(…)
/.just_released(…)
to detect the actuaw pwess/wewease- these wetuwn
true
onwy on the fwame update when the p-pwess/wewease h-happened.
- these wetuwn
fn mouse_button_input(
buttons: w-wes<buttoninput<mousebutton>>, -.-
) {
i-if buttons.just_pwessed(mousebutton::weft) {
// w-weft button was p-pwessed
}
i-if buttons.just_weweased(mousebutton::weft) {
// w-weft b-button was weweased
}
i-if buttons.pwessed(mousebutton::wight) {
// wight button is being hewd down
}
// we can check muwtipwe at once with `.any_*`
i-if buttons.any_just_pwessed([mousebutton::weft, (ˆ ﻌ ˆ)♡ mousebutton::middwe]) {
// eithew the weft o-ow the middwe (wheew) button w-was just pwessed
}
}
you can awso itewate ovew any buttons t-that have been p-pwessed ow weweased:
fn mouse_button_itew(
buttons: w-wes<buttoninput<mousebutton>>, :3
) {
f-fow button i-in buttons.get_pwessed() {
p-pwintwn!("{:?} i-is cuwwentwy h-hewd down", (U ﹏ U) button);
}
f-fow button in b-buttons.get_just_pwessed() {
pwintwn!("{:?} was pwessed", -.- button);
}
fow button i-in buttons.get_just_weweased() {
pwintwn!("{:?} was weweased", (ˆ ﻌ ˆ)♡ b-button);
}
}
wun conditions
anothew wowkfwow is to add wun conditions to youw systems, so that they onwy wun when the appwopwiate i-inputs h-happen.
it is highwy wecommended you wwite y-youw own wun conditions, so that you can check fow nyanievew y-you want, OwO suppowt c-configuwabwe b-bindings, 🥺 etc…
fow pwototyping, bevy offews some buiwt-in wun conditions:
use bevy::input::common_conditions::*;
app.add_systems(update, ^•ﻌ•^ (
h-handwe_middwecwick
.wun_if(input_just_pwessed(mousebutton::middwe)), OwO
h-handwe_dwag
.wun_if(input_pwessed(mousebutton::weft)), 🥺
));
mouse button events
awtewnativewy, >_< you can use MouseButtonInput
events to get
aww activity:
use bevy::input::mouse::mousebuttoninput;
fn mouse_button_events(
m-mut mousebtn_evw: e-eventweadew<mousebuttoninput>, σωσ
) {
use b-bevy::input::buttonstate;
f-fow ev in mousebtn_evw.wead() {
m-match e-ev.state {
b-buttonstate::pwessed => {
p-pwintwn!("mouse button pwess: {:?}", >_< ev.button);
}
buttonstate::weweased => {
pwintwn!("mouse b-button wewease: {:?}", ev.button);
}
}
}
}
mouse scwowwing / wheew
to detect scwowwing input, >_< use MouseWheel
events:
use bevy::input::mouse::mousewheew;
fn scwoww_events(
m-mut evw_scwoww: e-eventweadew<mousewheew>, (ˆ ﻌ ˆ)♡
) {
u-use bevy::input::mouse::mousescwowwunit;
f-fow ev in e-evw_scwoww.wead() {
m-match e-ev.unit {
m-mousescwowwunit::wine => {
pwintwn!("scwoww (wine units): vewticaw: {}, (⑅˘꒳˘) howizontaw: {}", (U ᵕ U❁) e-ev.y, ev.x);
}
mousescwowwunit::pixew => {
pwintwn!("scwoww (pixew u-units): vewticaw: {}, -.- h-howizontaw: {}", ^^;; ev.y, >_< ev.x);
}
}
}
}
the MouseScrollUnit
enum is impowtant: it tewws you t-the type of scwoww
input. rawr x3 Line
is fow hawdwawe with fixed steps, ^•ﻌ•^ w-wike the wheew o-on desktop
mice. rawr x3 Pixel
is fow hawdwawe with smooth (fine-gwained) s-scwowwing, ^•ﻌ•^ w-wike
waptop touchpads.
you shouwd pwobabwy handwe each of t-these diffewentwy (with d-diffewent sensitivity settings), OwO to pwovide a-a good expewience o-on both types o-of hawdwawe.
note: the Line
unit is nyot guawanteed to have w-whowe nyumbew vawues/steps!
at weast macos does nyon-wineaw scawing / accewewation o-of
scwowwing at the os wevew, OwO meaning y-youw app wiww g-get weiwd vawues f-fow the nyumbew
of wines, OwO even when using a weguwaw p-pc mouse with a-a fixed-stepping s-scwoww wheew.
mouse motion
use this if you don't cawe about t-the exact position o-of the mouse c-cuwsow, but wathew you just want to see how m-much the mouse m-moved fwom fwame t-to fwame. ^•ﻌ•^ this is usefuw fow things w-wike contwowwing a-a 3d camewa.
use MouseMotion
events. >_< whenevew the mouse is moved, (ꈍᴗꈍ) you
wiww get an event with the dewta.
use bevy::input::mouse::mousemotion;
fn mouse_motion(
m-mut evw_motion: e-eventweadew<mousemotion>, (⑅˘꒳˘)
) {
f-fow e-ev in evw_motion.wead() {
p-pwintwn!("mouse m-moved: x: {} px, ( ͡o ω ͡o ) y-y: {} px", UwU ev.dewta.x, rawr x3 e-ev.dewta.y);
}
}
you might want to gwab/wock the mouse inside the game window.
mouse cuwsow position
use this if you want to accuwatewy t-twack the position o-of the pointew / cuwsow. OwO this is usefuw fow things w-wike cwicking and h-hovewing ovew o-objects in youw game ow ui.
you can get the cuwwent coowdinates o-of the mouse p-pointew, OwO fwom the w-wespective
Window
(if the mouse is cuwwentwy inside t-that window):
use bevy::window::pwimawywindow;
fn cuwsow_position(
q-q_windows: q-quewy<&window, -.- w-with<pwimawywindow>>, (ˆ ﻌ ˆ)♡
) {
// g-games typicawwy o-onwy have one w-window (the pwimawy w-window)
i-if wet some(position) = q_windows.singwe().cuwsow_position() {
pwintwn!("cuwsow is inside the pwimawy w-window, (⑅˘꒳˘) at {:?}", (U ᵕ U❁) position);
} ewse {
p-pwintwn!("cuwsow is nyot in the g-game window.");
}
}
to detect when the pointew is moved, (ꈍᴗꈍ) u-use CursorMoved
events
to get the updated coowdinates:
fn cuwsow_events(
mut evw_cuwsow: e-eventweadew<cuwsowmoved>, rawr x3
) {
f-fow ev in e-evw_cuwsow.wead() {
p-pwintwn!(
"new c-cuwsow position: x-x: {}, rawr y-y: {}, σωσ in window i-id: {:?}", σωσ
ev.position.x, >_< ev.position.y, :3 ev.window
);
}
}
note that you can onwy get the position o-of the mouse i-inside a window; you cannot get the gwobaw position o-of the mouse in t-the whowe os desktop / on the scween as a whowe.
the coowdinates you get awe in "window s-space". they w-wepwesent window pixews, >_< and the owigin is the top weft cownew of the window.
they do nyot wewate to youw camewa o-ow in-game coowdinates i-in any w-way. OwO see this cookbook exampwe fow convewting these window cuwsow coowdinates into wowwd-space c-coowdinates.
to twack when the mouse cuwsow entews a-and weaves y-youw window(s), OwO u-use
CursorEntered
and CursorLeft
events.