Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Bevy 2D Template
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Bevy 2D Template
Commits
3ce73ff9
Verified
Commit
3ce73ff9
authored
1 year ago
by
Louis
Browse files
Options
Downloads
Patches
Plain Diff
Update to bevy 0.10
parent
39418ff4
No related branches found
No related tags found
No related merge requests found
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
game_core/src/system/web.rs
+7
-8
7 additions, 8 deletions
game_core/src/system/web.rs
game_core/src/system/window.rs
+75
-19
75 additions, 19 deletions
game_core/src/system/window.rs
with
82 additions
and
27 deletions
game_core/src/system/web.rs
+
7
−
8
View file @
3ce73ff9
#[cfg(target_arch
=
"wasm32"
)]
mod
__plugin
{
use
bevy
::
app
::{
App
,
Plugin
,
CoreStage
}
;
use
bevy
::
prelude
::
*
;
pub
fn
handle_startup_fullscreen
()
{
if
micro_bevy_web_utils
::
bindings
::
is_touch_device
()
{
micro_bevy_web_utils
::
bindings
::
make_selector_fullscreen
(
"canvas"
.to_string
());
...
...
@@ -12,12 +12,11 @@ mod __plugin {
pub
struct
WebPlugin
;
impl
Plugin
for
WebPlugin
{
fn
build
(
&
self
,
app
:
&
mut
App
)
{
app
.add_startup_system
(
handle_startup_fullscreen
)
.add_system_to_stage
(
CoreStage
::
First
,
micro_bevy_web_utils
::
bevy
::
emit_touch_events
,
);
}
app
.add_startup_system
(
handle_startup_fullscreen
)
.add_system
(
micro_bevy_web_utils
::
bevy
::
emit_touch_events
.in_base_set
(
CoreSet
::
First
),
);
}
}
}
...
...
@@ -30,4 +29,4 @@ mod __plugin {
}
}
pub
use
__plugin
::
WebPlugin
;
\ No newline at end of file
pub
use
__plugin
::
WebPlugin
;
This diff is collapsed.
Click to expand it.
game_core/src/system/window.rs
+
75
−
19
View file @
3ce73ff9
use
bevy
::
ecs
::
system
::
SystemParam
;
use
bevy
::
math
::
Vec4Swizzles
;
use
bevy
::
prelude
::
*
;
use
bevy
::
window
::
PrimaryWindow
;
// use advent_ui::prelude::Units;
// use num_traits::AsPrimitive;
use
crate
::
system
::
camera
::
GameCamera
;
#[derive(PartialEq,
Default,
Debug,
Copy,
Clone,
Resource)]
pub
struct
WindowUnits
{
pub
inner_width
:
f32
,
pub
inner_height
:
f32
,
pub
vw
:
f32
,
pub
vh
:
f32
,
}
// TODO: Enable if using advent_ui
// impl WindowUnits {
// pub fn vw_units<T: AsPrimitive<f32>>(&self, units: T) -> Units {
// Units::Pixels(self.vw * units.as_())
// }
// pub fn vh_units<T: AsPrimitive<f32>>(&self, units: T) -> Units {
// Units::Pixels(self.vh * units.as_())
// }
// }
pub
fn
update_window_units
(
window
:
WindowManager
,
mut
units
:
ResMut
<
WindowUnits
>
)
{
let
next_units
=
window
.get_units
();
if
next_units
!=
*
units
{
*
units
=
next_units
;
}
}
pub
struct
WindowSyncPlugin
;
impl
Plugin
for
WindowSyncPlugin
{
fn
build
(
&
self
,
app
:
&
mut
App
)
{
app
.init_resource
::
<
WindowUnits
>
()
.add_system
(
update_window_units
.in_base_set
(
CoreSet
::
First
));
}
}
/// A struct that provides several convenience methods for getting mouse and
/// window related information
#[derive(SystemParam)]
pub
struct
WindowManager
<
'w
,
's
>
{
mouse
:
Res
<
'w
,
Input
<
MouseButton
>>
,
windows
:
Res
<
'w
,
Windows
>
,
cam
_qu
er
y
:
ParamSet
<
'w
,
's
,
(
Query
<
'w
,
's
,
&
'static
Transform
,
With
<
GameCamera
>>
,
)
>
,
windows
:
Query
<
'w
,
's
,
&
'static
Window
,
With
<
PrimaryWindow
>
>
,
camer
a
:
Query
<
'w
,
's
,
(
&
'static
OrthographicProjection
,
&
'static
Transform
)
,
With
<
GameCamera
>>
,
}
impl
<
'w
,
's
>
WindowManager
<
'w
,
's
>
{
/// Conditionally run a function with the primary window. The function will not
/// run if the primary window does not exist - typically this is the desired behaviour.
///
/// ## Arguments
/// - `func`: an `FnOnce` callback that is given a [`bevy::prelude::Window`]
pub
fn
with_primary_window
<
Func
:
FnOnce
(
&
Window
)
>
(
&
self
,
func
:
Func
)
{
match
self
.windows
.get_primary
()
{
Some
(
window
)
=>
func
(
window
),
None
=>
{}
}
}
pub
fn
get_primary_window
(
&
self
)
->
Option
<&
Window
>
{
self
.windows
.get_
primary
()
self
.windows
.get_
single
()
.ok
()
}
pub
fn
get_mouse_press
(
&
mut
self
)
->
Option
<
Vec2
>
{
if
self
.mouse
.just_pressed
(
MouseButton
::
Left
)
{
if
let
Some
(
window
)
=
self
.
windows
.
get_primary
()
{
if
let
Some
(
window
)
=
self
.get_primary
_window
()
{
if
let
Some
(
position
)
=
window
.cursor_position
()
{
let
window_size
=
Vec2
::
new
(
window
.width
()
as
f32
,
window
.height
()
as
f32
);
let
adjusted_position
=
position
-
window_size
/
2.0
;
if
let
Ok
(
camera_transform
)
=
self
.cam_query
.p0
()
.get_single
()
{
if
let
Ok
((
projection
,
camera_transform
))
=
self
.camera
.get_single
()
{
let
window_size
=
Vec2
::
new
(
window
.width
()
as
f32
,
window
.height
()
as
f32
);
let
projection_size
=
Vec2
::
new
(
projection
.area.max.x
-
projection
.area.min.x
,
projection
.area.max.y
-
projection
.area.min.y
,
);
let
adjusted_position
=
position
-
window_size
/
2.0
;
let
scale
=
window_size
/
projection_size
;
let
adjusted_position
=
adjusted_position
/
scale
;
let
world_position
=
camera_transform
.compute_matrix
()
*
adjusted_position
.extend
(
0.0
)
.extend
(
1.0
);
return
Some
(
Vec2
::
new
(
world_position
.x
,
world_position
.
y
));
return
Some
(
world_position
.xy
(
));
}
}
}
}
None
}
pub
fn
get_units
(
&
self
)
->
WindowUnits
{
let
window
=
self
.get_primary_window
();
if
let
Some
(
window
)
=
window
{
let
inner_width
=
window
.width
();
let
real_width
=
window
.physical_width
();
let
inner_height
=
window
.height
();
WindowUnits
{
inner_width
,
inner_height
,
vh
:
inner_height
/
100.0
,
vw
:
inner_width
/
100.0
,
}
}
else
{
WindowUnits
{
inner_height
:
0.0
,
inner_width
:
0.0
,
vw
:
0.0
,
vh
:
0.0
,
}
}
}
}
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment