Skip to content

Commit a152330

Browse files
authored
Merge pull request #127 from malhwiu/toggle-2d-border
[FEAT] Toggle black border in 2D mode #120
2 parents 1083bc2 + 89d8b7c commit a152330

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The app has some simple hotkeys:
7373
| <kbd>x</kbd> | WINDOWS ONLY Toggle Window Fallthrough 3D |
7474
| <kbd>l</kbd> | Window-Level |
7575
| <kbd>d</kbd> | Toggle Decorations (not all OSes) |
76+
| <kbd>b</kbd> | Toggle Border (2d only) |
7677
| <kbd>t</kbd> | Toggle Transparency (returning to fully transparent is not supported) |
7778
| <kbd>r</kbd> | Toggle Rotating shape (3d only) |
7879
| <kbd>spacebar</kbd> | Takes a screenshot && versions the current `.wgsl` |

src/plugin.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::camera::PanOrbitCameraPlugin;
1+
use crate::{camera::PanOrbitCameraPlugin, utils::{ShadplayWindowBorder, toggle_border}};
22
use bevy::{
33
input::keyboard::KeyboardInput, log::tracing_subscriber::util::SubscriberInitExt, prelude::*,
44
sprite_render::Material2dPlugin, window::WindowResized,
@@ -22,13 +22,15 @@ impl Plugin for ShadPlayPlugin {
2222
.insert_resource(MonitorsSpecs::default())
2323
.insert_resource(TexHandleQueue::default())
2424
.insert_resource(ShadplayWindowDims::default())
25+
.insert_resource(ShadplayWindowBorder::default())
2526
.insert_resource(ShapeOptions::default())
2627
.insert_resource(TransparencySet(true))
2728
.insert_resource(Rotating(false))
2829
.add_plugins(PanOrbitCameraPlugin)
2930
//events:
3031
.add_message::<UserAddedTexture>()
3132
.add_message::<DragNDropShader>()
33+
.add_message::<ShadplayWindowBorder>()
3234
// 3D
3335
.add_systems(OnEnter(AppState::ThreeD), setup_3d)
3436
.add_systems(OnExit(AppState::ThreeD), cleanup_3d)
@@ -72,9 +74,12 @@ impl Plugin for ShadPlayPlugin {
7274
(
7375
// utils::max_mon_res, // We're currently not using the maximum resolution of the primary monitor.
7476
update_mouse_pos,
77+
toggle_border
78+
.run_if(in_state(AppState::TwoD))
79+
.run_if(on_message::<KeyboardInput>),
7580
size_quad
7681
.run_if(in_state(AppState::TwoD))
77-
.run_if(on_message::<WindowResized>),
82+
.run_if(on_message::<WindowResized>.or(on_message::<ShadplayWindowBorder>)),
7883
swap_2d_tex_from_idx.run_if(on_message::<KeyboardInput>),
7984
),
8085
);

src/utils.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ impl ShadplayWindowDims {
100100
}
101101
}
102102

103+
/// Resource and Message: Used for toggling on/off border for the 2d shader.
104+
#[derive(Resource, Message, Debug, Clone)]
105+
pub struct ShadplayWindowBorder {
106+
pub enabled: bool,
107+
pub thickness: Vec2
108+
}
109+
110+
impl ShadplayWindowBorder {
111+
/// Get border thickness in __%__
112+
///
113+
/// If `enabled` is `false` will return __x__ = `1.00`, __y__ = `1.00`
114+
pub fn thickness(&self) -> Vec2 {
115+
if !self.enabled {
116+
return Vec2::new(1.00, 1.00);
117+
}
118+
119+
1.00 - self.thickness
120+
}
121+
}
122+
123+
impl Default for ShadplayWindowBorder {
124+
fn default() -> Self {
125+
Self {
126+
enabled: true,
127+
thickness: Vec2::new(0.05, 0.05)
128+
}
129+
}
130+
}
131+
103132
/// Resource: All the shapes we have the option of displaying. 3d Only.
104133
#[allow(clippy::type_complexity)]
105134
#[derive(Resource, Default)]
@@ -429,25 +458,44 @@ pub fn size_quad(
429458
windows: Query<&Window>,
430459
mut query: Query<&mut Transform, With<BillBoardQuad>>,
431460
mut msd: ResMut<ShadplayWindowDims>,
461+
border: Res<ShadplayWindowBorder>,
432462
// monitors: Res<MonitorsSpecs>,
433463
) {
434464
let win = windows
435465
.single()
436466
.expect("Should be impossible to NOT get a window");
437467

438468
let (width, height) = (win.width(), win.height());
469+
let (border_w, border_h) = (border.thickness().x, border.thickness().y);
439470

440471
query.iter_mut().for_each(|mut transform| {
441472
*msd = ShadplayWindowDims(Vec2 {
442473
x: width,
443474
y: height,
444475
});
445476

446-
transform.scale = Vec3::new(width * 0.95, height * 0.95, 1.0);
477+
transform.scale = Vec3::new(width * border_w, height * border_h, 1.0);
447478
info!("Window Resized, resizing quad");
448479
});
449480
}
450481

482+
/// System: Runs only when in [`AppState::TwoD`]
483+
///
484+
/// Used for toggling on/off the window border.
485+
///
486+
/// Press `b` when in 2D mode to toggle the window border.
487+
pub fn toggle_border(
488+
mut border: ResMut<ShadplayWindowBorder>,
489+
input: Res<ButtonInput<KeyCode>>,
490+
mut fire_event: MessageWriter<ShadplayWindowBorder>
491+
) {
492+
if input.just_pressed(KeyCode::KeyB) {
493+
info!("Toggling window border");
494+
border.enabled = !border.enabled;
495+
fire_event.write(border.clone());
496+
}
497+
}
498+
451499
// Monitor Maximum Res.
452500
pub fn max_mon_res(
453501
window_query: Query<Entity, With<Window>>,

0 commit comments

Comments
 (0)