Skip to content

Commit

Permalink
window dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 3, 2024
1 parent 4a64d10 commit 6dbdc9f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 31 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions assets_gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct State {

impl engine::framework::State for State {
fn new(ctx: &mut Context) -> Self {
goryak::set_blur_texture(ctx.yakui.blur_bg_texture);

let gfx = &mut ctx.gfx;

gfx.render_params.value_mut().shadow_mapping_resolution = 2048;
Expand Down
3 changes: 1 addition & 2 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ crate-type = ["dylib"]
[dependencies]
geom = { path = "../geom" }
common = { path = "../common" }
goryak = { path = "../goryak", optional = true }
ordered-float = { workspace = true }
egui = { workspace = true }
oddio = { workspace = true }
Expand Down Expand Up @@ -42,4 +41,4 @@ yakui-winit = { workspace = true, optional = true }
lazy_static = "1.4.0"

[features]
yakui = ["dep:yakui", "dep:yakui-wgpu", "dep:yakui-winit", "dep:goryak"]
yakui = ["dep:yakui", "dep:yakui-wgpu", "dep:yakui-winit"]
4 changes: 1 addition & 3 deletions engine/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,11 @@ async fn run<S: State>(el: EventLoop<()>, window: Arc<Window>) {
});
ctx.gui_time = gui_start.elapsed().as_secs_f32();
ctx.gfx.finish_frame(enc);
sco.present();

ctx.gfx.window.set_cursor_icon(get_cursor_icon());

ctx.input.end_frame();
ctx.total_cpu_time = last_update.elapsed().as_secs_f32();

sco.present();
ctx.gfx.window.request_redraw();
}
_ => (),
Expand Down
2 changes: 0 additions & 2 deletions engine/src/yakui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl YakuiWrapper {
wgpu::FilterMode::Linear,
);

goryak::set_blur_texture(texture_id);

Self {
blur_bg_texture: texture_id,
yakui,
Expand Down
108 changes: 85 additions & 23 deletions goryak/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::rc::Rc;

use yakui_core::geometry::{Dim2, Vec2};
use yakui_core::{Alignment, MainAxisSize};
use yakui_core::geometry::{Constraints, Dim2, Vec2};
use yakui_core::widget::{LayoutContext, Widget};
use yakui_core::{context, Alignment, Flow, MainAxisSize};
use yakui_widgets::widgets::{List, Pad};
use yakui_widgets::{draggable, offset, reflow, row, use_state};
use yakui_widgets::{draggable, row};

use crate::{blur_bg, divider, on_secondary_container, outline, secondary_container, textc};

Expand All @@ -21,29 +23,89 @@ pub struct Window {

impl Window {
pub fn show(self, children: impl FnOnce()) {
reflow(Alignment::TOP_LEFT, Dim2::ZERO, || {
let off = use_state(|| Vec2::new(300.0, 300.0));

offset(off.get(), || {
let r = draggable(|| {
blur_bg(secondary_container().with_alpha(0.7), 5.0, || {
self.pad.show(|| {
let mut l = List::column();
l.main_axis_size = MainAxisSize::Min;
l.show(|| {
row(|| {
textc(on_secondary_container(), Cow::Borrowed(self.title));
});
divider(outline(), 10.0, 1.0);
children();
});
let dom = context::dom();
let response = dom.begin_widget::<WindowBase>(());

let off = draggable(|| {
blur_bg(secondary_container().with_alpha(0.7), 5.0, || {
self.pad.show(|| {
let mut l = List::column();
l.main_axis_size = MainAxisSize::Min;
l.show(|| {
row(|| {
textc(on_secondary_container(), Cow::Borrowed(self.title));
});
divider(outline(), 10.0, 1.0);
children();
});
});
if let Some(v) = r.dragging {
off.set(v.current);
}
});
});

response.confirm.set(off.dragging.is_none());
if let Some(drag) = off.dragging {
response.off.set(drag.current - drag.start);
}

dom.end_widget::<WindowBase>(response.id);
}
}

#[derive(Default, Debug)]
struct WindowBase {
props: (),
off: Vec2,
resp: Rc<WindowResp>,
}

#[derive(Default, Debug)]
struct WindowResp {
off: Cell<Vec2>,
confirm: Cell<bool>,
}

impl Widget for WindowBase {
type Props<'a> = ();
type Response = Rc<WindowResp>;

fn new() -> Self {
Self::default()
}

fn update(&mut self, props: Self::Props<'_>) -> Self::Response {
self.props = props;
if self.resp.confirm.get() {
self.off += self.resp.off.get();
self.resp.off.set(Vec2::ZERO);
}
self.resp.clone()
}

fn flow(&self) -> Flow {
Flow::Relative {
anchor: Alignment::TOP_LEFT,
offset: Dim2::ZERO,
}
}

fn layout(&self, mut ctx: LayoutContext<'_>, constraints: Constraints) -> Vec2 {
let node = ctx.dom.get_current();
if node.children.len() > 1 {
panic!("Window can only have one child");
}

let child = *node.children.first().unwrap();
let size = ctx.calculate_layout(child, constraints);

let vp = ctx.layout.viewport().size();

let mut pos = vp * 0.5 - size * 0.5 + self.off + self.resp.off.get();
let overflow = (pos + size - vp).max(Vec2::ZERO);
pos = pos - overflow;
pos = pos.max(Vec2::ZERO);

ctx.layout.set_pos(child, pos);

Vec2::ZERO
}
}
2 changes: 2 additions & 0 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct State {

impl engine::framework::State for State {
fn new(ctx: &mut Context) -> Self {
goryak::set_blur_texture(ctx.yakui.blur_bg_texture);

let camera = OrbitCamera::load((ctx.gfx.size.0, ctx.gfx.size.1));

Gui::set_style(ctx.egui.platform.egui_ctx());
Expand Down

0 comments on commit 6dbdc9f

Please sign in to comment.