Skip to content
Snippets Groups Projects
Commit c686c966 authored by MrGVSV's avatar MrGVSV
Browse files

Formatting

parent 0a3ab688
No related branches found
No related tags found
No related merge requests found
use bevy::{
input::{keyboard::KeyboardInput, mouse::{MouseButtonInput, MouseScrollUnit, MouseWheel}, ElementState},
input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseScrollUnit, MouseWheel},
ElementState,
},
math::Vec2,
prelude::{EventReader, IntoExclusiveSystem, MouseButton, Plugin, Res, World},
render::color::Color,
......@@ -79,12 +83,12 @@ pub fn process_events(world: &mut World) {
EventReader<KeyboardInput>,
), _, _>(
|(
mut cursor_moved_events,
mut mouse_button_input_events,
mut mouse_wheel_events,
mut char_input_events,
mut keyboard_input_events,
)| {
mut cursor_moved_events,
mut mouse_button_input_events,
mut mouse_wheel_events,
mut char_input_events,
mut keyboard_input_events,
)| {
if let Some(event) = cursor_moved_events.iter().last() {
// Currently, we can only handle a single MouseMoved event at a time so everything but the last needs to be skipped
input_events.push(InputEvent::MouseMoved((
......
......@@ -45,7 +45,6 @@ Vivamus pulvinar dui et elit volutpat hendrerit. Praesent luctus dolor ut rutrum
Vestibulum rutrum imperdiet nisl, et consequat massa porttitor vel. Ut velit justo, vehicula a nulla eu, auctor eleifend metus. Ut egestas malesuada metus, sit amet pretium nunc commodo ac. Pellentesque gravida, nisl in faucibus volutpat, libero turpis mattis orci, vitae tincidunt ligula ligula ut tortor. Maecenas vehicula lobortis odio in molestie. Curabitur dictum elit sed arcu dictum, ut semper nunc cursus. Donec semper felis non nisl tincidunt elementum.
"#.to_string();
render! {
<App>
<NinePatch
......
......@@ -44,4 +44,4 @@ impl Default for ScrollUnit {
fn default() -> Self {
ScrollUnit::Pixel { x: 0.0, y: 0.0 }
}
}
\ No newline at end of file
}
......@@ -80,7 +80,9 @@ pub enum EventType {
Scroll(ScrollEvent),
Focus,
Blur,
CharInput { c: char },
CharInput {
c: char,
},
KeyUp(KeyboardEvent),
KeyDown(KeyboardEvent),
}
......
......@@ -226,10 +226,10 @@ impl EventDispatcher {
// Mouse is currently within this node
if events.contains(&EventType::MouseIn(Default::default()))
&& !Self::contains_event(
&next_events,
index,
&EventType::MouseOut(Default::default()),
)
&next_events,
index,
&EventType::MouseOut(Default::default()),
)
{
// Make sure this event isn't removed while mouse is still within node
Self::insert_event(
......@@ -544,7 +544,7 @@ impl EventDispatcher {
ScrollUnit::Line { x: *dx, y: *dy }
} else {
ScrollUnit::Pixel { x: *dx, y: *dy }
}
},
}),
);
}
......
......@@ -6,9 +6,18 @@ pub enum InputEvent {
MouseLeftPress,
MouseLeftRelease,
/// An event that occurs when the user scrolls
Scroll { dx: f32, dy: f32, is_line: bool },
CharEvent { c: char },
Keyboard { key: KeyCode, is_pressed: bool },
Scroll {
dx: f32,
dy: f32,
is_line: bool,
},
CharEvent {
c: char,
},
Keyboard {
key: KeyCode,
is_pressed: bool,
},
}
pub enum InputEventCategory {
......
......@@ -7,7 +7,6 @@ pub use scroll_bar::*;
pub use scroll_box::*;
pub use scroll_context::*;
/// Maps a value from one range to another range
fn map_range(value: f32, from_range: (f32, f32), to_range: (f32, f32)) -> f32 {
let from_diff = from_range.1 - from_range.0;
......
......@@ -6,12 +6,11 @@ use crate::core::{
};
use kayak_core::layout_cache::Rect;
use kayak_core::styles::{Corner, Edge};
use kayak_core::{Color};
use kayak_core::Color;
use crate::widgets::Background;
use crate::widgets::{Background};
use super::{ScrollContext, map_range};
use super::{map_range, ScrollContext};
/// Props used by the [`ScrollBar`] widget
#[derive(WidgetProps, Default, Debug, PartialEq, Clone)]
......@@ -88,22 +87,28 @@ pub fn ScrollBar(props: ScrollBarProps) {
let horizontal = props.horizontal;
let _thickness = props.thickness;
let thickness = props.thickness;
let thumb_color = props.thumb_color.unwrap_or_else(|| Color::new(0.2981, 0.3098, 0.321, 0.95));
let thumb_color = props
.thumb_color
.unwrap_or_else(|| Color::new(0.2981, 0.3098, 0.321, 0.95));
let thumb_styles = props.thumb_styles.clone();
let track_color = props.track_color.unwrap_or_else(|| Color::new(0.1581, 0.1758, 0.191, 0.15));
let track_color = props
.track_color
.unwrap_or_else(|| Color::new(0.1581, 0.1758, 0.191, 0.15));
let track_styles = props.track_styles.clone();
/// The size of the thumb as a percentage
let thumb_size_percent = (if props.horizontal {
layout.width / (content_width - thickness).max(1.0)
} else {
layout.height / (content_height - thickness).max(1.0)
}).clamp(0.1, 1.0);
})
.clamp(0.1, 1.0);
/// The size of the thumb in pixels
let thumb_size_pixels = thumb_size_percent * if props.horizontal {
layout.width
} else {
layout.height
};
let thumb_size_pixels = thumb_size_percent
* if props.horizontal {
layout.width
} else {
layout.height
};
let thumb_extents = thumb_size_pixels / 2.0;
let percent_scrolled = if props.horizontal {
scroll.percent_x()
......@@ -111,32 +116,39 @@ pub fn ScrollBar(props: ScrollBarProps) {
scroll.percent_y()
};
// The thumb's offset as a percentage
let thumb_offset = map_range(percent_scrolled * 100.0, (0.0, 100.0), (0.0, 100.0 - thumb_size_percent * 100.0));
let thumb_offset = map_range(
percent_scrolled * 100.0,
(0.0, 100.0),
(0.0, 100.0 - thumb_size_percent * 100.0),
);
// === Styles === //
props.styles = Some(Style::default().with_style(Style {
render_command: RenderCommand::Layout.into(),
width: if horizontal {
Units::Stretch(1.0)
} else {
Units::Pixels(thickness)
}.into(),
height: if horizontal {
Units::Pixels(thickness)
} else {
Units::Stretch(1.0)
}.into(),
..Default::default()
}));
props.styles = Some(
Style::default().with_style(Style {
render_command: RenderCommand::Layout.into(),
width: if horizontal {
Units::Stretch(1.0)
} else {
Units::Pixels(thickness)
}
.into(),
height: if horizontal {
Units::Pixels(thickness)
} else {
Units::Stretch(1.0)
}
.into(),
..Default::default()
}),
);
let mut track_style =
Style::default()
.with_style(&track_styles)
.with_style(Style {
background_color: track_color.into(),
border_radius: Corner::all(thickness / 2.0).into(),
..Default::default()
});
let mut track_style = Style::default()
.with_style(&track_styles)
.with_style(Style {
background_color: track_color.into(),
border_radius: Corner::all(thickness / 2.0).into(),
..Default::default()
});
let mut border_color = thumb_color;
border_color.a = (border_color.a - 0.2).max(0.0);
......@@ -262,7 +274,7 @@ pub fn ScrollBar(props: ScrollBarProps) {
// Positional difference (scaled by thumb size)
let pos_diff = (
(start_pos.0 - data.position.0) / thumb_size_percent,
(start_pos.1 - data.position.1) / thumb_size_percent
(start_pos.1 - data.position.1) / thumb_size_percent,
);
let mut old = scroll_ctx.get();
if horizontal {
......@@ -280,11 +292,7 @@ pub fn ScrollBar(props: ScrollBarProps) {
_ => {}
});
let on_track_event = if disabled {
None
} else {
Some(on_track_event)
};
let on_track_event = if disabled { None } else { Some(on_track_event) };
// === Render === //
rsx! {
......@@ -292,4 +300,4 @@ pub fn ScrollBar(props: ScrollBarProps) {
<Background styles={Some(thumb_style)} />
</Background>
}
}
\ No newline at end of file
}
......@@ -5,13 +5,12 @@ use crate::core::{
use_state, widget, Bound, Children, EventType, MutableBound, OnEvent, ScrollUnit, WidgetProps,
};
use kayak_core::styles::{LayoutType};
use kayak_core::{Color};
use kayak_core::styles::LayoutType;
use kayak_core::Color;
use crate::widgets::{Clip, Element, If};
use super::{ScrollContext, ScrollBar, ScrollContent, ScrollMode};
use super::{ScrollBar, ScrollContent, ScrollContext, ScrollMode};
/// Props used by the [`ScrollBox`] widget
#[derive(WidgetProps, Default, Debug, PartialEq, Clone)]
......@@ -133,7 +132,7 @@ pub fn ScrollBox(props: ScrollBoxProps) {
layout_type: LayoutType::Row.into(),
..Default::default()
})
.with_style(&props.styles)
.with_style(&props.styles),
);
let hbox_styles = Style::default().with_style(Style {
......@@ -224,4 +223,4 @@ pub fn ScrollBox(props: ScrollBoxProps) {
</Element>
</Element>
}
}
\ No newline at end of file
}
use crate::core::{
rsx,
styles::{Style}, widget, Bound, Children, MutableBound, WidgetProps,
};
use crate::core::{rsx, styles::Style, widget, Bound, Children, MutableBound, WidgetProps};
use super::ScrollContext;
......@@ -63,4 +54,4 @@ pub(super) fn ScrollContent(props: ScrollContentProps) {
{children}
</>
}
}
\ No newline at end of file
}
/// Context data provided by a [`ScrollBox`](crate::ScrollBox) widget
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct ScrollContext {
......@@ -86,7 +78,7 @@ impl ScrollContext {
let min = -self.scrollable_width();
self.scroll_x = match self.mode {
ScrollMode::Clamped => ScrollContext::clamped(x, min, 0.0),
ScrollMode::Infinite => x
ScrollMode::Infinite => x,
}
}
......@@ -97,7 +89,7 @@ impl ScrollContext {
let min = -self.scrollable_height();
self.scroll_y = match self.mode {
ScrollMode::Clamped => ScrollContext::clamped(y, min, 0.0),
ScrollMode::Infinite => y
ScrollMode::Infinite => y,
};
}
......@@ -127,4 +119,4 @@ impl ScrollContext {
fn clamped(value: f32, min: f32, max: f32) -> f32 {
value.clamp(min, max)
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment