Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use easy-cast traits for geometry types #284

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/kas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ron = ["config", "dep_ron"]
macros_log = ["kas-macros/log"]

[dependencies]
easy-cast = "0.4.2"
easy-cast = { git = "https://github.com/kas-gui/easy-cast.git", rev = "6bf6084bb78f6bd1e781158016916ef103db0b19" }
log = "0.4"
smallvec = "1.6.1"
stack_dst = { version = "0.6", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-core/src/draw/draw_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use super::color::Rgba;
use super::{images, DrawImpl, ImageError, ImageFormat, ImageId, PassId};
use crate::cast::Cast;
use crate::geom::{Quad, Size, Vec2};
use crate::text::{Effect, TextDisplay};
use std::any::Any;
Expand Down Expand Up @@ -103,7 +104,7 @@ impl<DS: DrawSharedImpl> DrawShared for SharedState<DS> {

#[inline]
fn image_size(&self, id: ImageId) -> Option<Size> {
self.draw.image_size(id).map(|size| size.into())
self.draw.image_size(id).map(|size| size.cast())
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/kas-core/src/event/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use super::ScrollDelta::{LineDelta, PixelDelta};
use super::{Command, CursorIcon, Event, EventMgr, PressSource, Response, VoidMsg};
use crate::cast::CastFloat;
use crate::cast::traits::*;
use crate::geom::{Coord, Offset, Rect, Size, Vec2};
#[allow(unused)]
use crate::text::SelectionHelper;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Glide {
}
}
let dur = now - t0;
let v = Vec2::from(delta) / dur.as_secs_f32();
let v = Vec2::conv(delta) / dur.as_secs_f32();
if dur >= Duration::from_millis(1) && v != Vec2::ZERO {
*self = Glide::Glide(now, v, Vec2::ZERO);
true
Expand All @@ -76,8 +76,8 @@ impl Glide {
let now = Instant::now();
let dur = (now - *start).as_secs_f32();
let d = *v * dur + *rest;
let rest = d.fract();
let delta = Offset::from(d.trunc());
let delta = Offset::conv_approx(d);
let rest = d - Vec2::conv(delta);

if v.max_abs_comp() >= 1.0 {
let mut v = *v * decay_mul.powf(dur);
Expand Down Expand Up @@ -144,7 +144,7 @@ impl ScrollComponent {
/// change in offset. In practice the caller will likely be performing all
/// required updates regardless and the return value can be safely ignored.
pub fn set_sizes(&mut self, window_size: Size, content_size: Size) -> TkAction {
self.max_offset = Offset::from(content_size) - Offset::from(window_size);
self.max_offset = Offset::conv(content_size) - Offset::conv(window_size);
self.set_offset(self.offset)
}

Expand Down Expand Up @@ -203,7 +203,7 @@ impl ScrollComponent {
#[inline]
pub fn focus_rect(&mut self, rect: Rect, window_rect: Rect) -> (Rect, TkAction) {
let v = rect.pos - window_rect.pos;
let off = Offset::from(rect.size) - Offset::from(window_rect.size);
let off = Offset::conv(rect.size) - Offset::conv(window_rect.size);
let offset = self.offset.max(v + off).min(v);
let action = self.set_offset(offset);
(rect - self.offset, action)
Expand Down
6 changes: 3 additions & 3 deletions crates/kas-core/src/event/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ pub enum Event {
///
/// In general, a point `p` on the screen should be transformed as follows:
/// ```
/// # use kas_core::cast::{Cast, CastFloat};
/// # use kas_core::geom::{Coord, DVec2};
/// # let (alpha, delta) = (DVec2::ZERO, DVec2::ZERO);
/// # let mut p = Coord::ZERO;
/// // Works for Coord type; for DVec2 type-conversions are unnecessary:
/// p = (alpha.complex_mul(p.into()) + delta).into();
/// let mut p = Coord::ZERO; // or whatever
/// p = (alpha.complex_mul(p.cast()) + delta).cast_nearest();
/// ```
///
/// When it is known that there is no rotational component, one can use a
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-core/src/event/manager/mgr_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::time::{Duration, Instant};
use std::u16;

use super::*;
use crate::cast::Conv;
use crate::draw::DrawShared;
use crate::geom::{Coord, Offset, Vec2};
use crate::layout::SetRectMgr;
Expand Down Expand Up @@ -154,7 +155,7 @@ impl EventState {
#[inline]
pub fn config_test_pan_thresh(&self, dist: Offset) -> bool {
let thresh = self.config.pan_dist_thresh();
Vec2::from(dist).sum_square() >= thresh * thresh
Vec2::conv(dist).sum_square() >= thresh * thresh
}

/// Access the screen's scale factor
Expand Down
16 changes: 8 additions & 8 deletions crates/kas-core/src/event/manager/mgr_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::collections::HashMap;
use std::time::{Duration, Instant};

use super::*;
use crate::cast::Conv;
use crate::geom::{Coord, DVec2, Offset};
use crate::cast::traits::*;
use crate::geom::{Coord, DVec2};
use crate::layout::SetRectMgr;
#[allow(unused)]
use crate::WidgetConfig; // for doc-links
Expand Down Expand Up @@ -216,7 +216,7 @@ impl EventState {
assert!(grab.n > 0);

// Terminology: pi are old coordinates, qi are new coords
let (p1, q1) = (DVec2::from(grab.coords[0].0), DVec2::from(grab.coords[0].1));
let (p1, q1) = (DVec2::conv(grab.coords[0].0), DVec2::conv(grab.coords[0].1));
grab.coords[0].0 = grab.coords[0].1;

let alpha;
Expand All @@ -228,7 +228,7 @@ impl EventState {
} else {
// We don't use more than two touches: information would be
// redundant (although it could be averaged).
let (p2, q2) = (DVec2::from(grab.coords[1].0), DVec2::from(grab.coords[1].1));
let (p2, q2) = (DVec2::conv(grab.coords[1].0), DVec2::conv(grab.coords[1].1));
grab.coords[1].0 = grab.coords[1].1;
let (pd, qd) = (p2 - p1, q2 - q1);

Expand Down Expand Up @@ -371,7 +371,7 @@ impl<'a> EventMgr<'a> {
}
CursorMoved { position, .. } => {
self.state.last_click_button = FAKE_MOUSE_BUTTON;
let coord = position.into();
let coord = position.cast_approx();

// Update hovered widget
let cur_id = widget.find_id(coord);
Expand Down Expand Up @@ -427,8 +427,8 @@ impl<'a> EventMgr<'a> {
MouseScrollDelta::PixelDelta(pos) => {
// The delta is given as a PhysicalPosition, so we need
// to convert to our vector type (Offset) here.
let coord = Coord::from(pos);
ScrollDelta::PixelDelta(Offset(coord.0, coord.1))
let coord = Coord::conv_approx(pos);
ScrollDelta::PixelDelta(coord.cast())
}
});
if let Some(id) = self.state.hover.clone() {
Expand Down Expand Up @@ -493,7 +493,7 @@ impl<'a> EventMgr<'a> {
// AxisMotion { axis: AxisId, value: f64, },
Touch(touch) => {
let source = PressSource::Touch(touch.id);
let coord = touch.location.into();
let coord = touch.location.cast_approx();
match touch.phase {
TouchPhase::Started => {
let start_id = widget.find_id(coord);
Expand Down
Loading