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

Simple shadows for FlatTheme #239

Merged
merged 16 commits into from
Aug 17, 2021
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
47 changes: 39 additions & 8 deletions crates/kas-core/src/draw/draw_rounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ pub trait DrawRounded: Draw {
/// painted, while `1.0` will result in a zero-width line on the outer edge.
fn circle(&mut self, rect: Quad, inner_radius: f32, col: Rgba);

/// Draw a circle or oval with two colours
///
/// More generally, this shape is an axis-aligned oval which may be hollow.
///
/// Colour `col1` is used at the centre and `col2` at the edge with linear
/// blending. The edge is not anti-aliased.
///
/// Note: this is drawn *before* other drawables, allowing it to be used
/// for shadows without masking.
fn circle_2col(&mut self, rect: Quad, col1: Rgba, col2: Rgba);

/// Draw a frame with rounded corners and uniform colour
///
/// All drawing occurs within the `outer` rect and outside of the `inner`
Expand All @@ -44,22 +55,43 @@ pub trait DrawRounded: Draw {
/// When `inner_radius > 0`, the frame will be visually thinner than the
/// allocated area.
fn rounded_frame(&mut self, outer: Quad, inner: Quad, inner_radius: f32, col: Rgba);

/// Draw a frame with rounded corners with two colours
///
/// This is a variant of `rounded_frame` which blends between two colours,
/// `c1` at the inner edge and `c2` at the outer edge.
///
/// Note: this is drawn *before* other drawables, allowing it to be used
/// for shadows without masking.
fn rounded_frame_2col(&mut self, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba);
}

impl<'a, DS: DrawSharedImpl> DrawRounded for DrawIface<'a, DS>
where
DS::Draw: DrawRoundedImpl,
{
#[inline]
fn rounded_line(&mut self, p1: Vec2, p2: Vec2, radius: f32, col: Rgba) {
self.draw.rounded_line(self.pass, p1, p2, radius, col);
}
#[inline]
fn circle(&mut self, rect: Quad, inner_radius: f32, col: Rgba) {
self.draw.circle(self.pass, rect, inner_radius, col);
}
#[inline]
fn circle_2col(&mut self, rect: Quad, col1: Rgba, col2: Rgba) {
self.draw.circle_2col(self.pass, rect, col1, col2);
}
#[inline]
fn rounded_frame(&mut self, outer: Quad, inner: Quad, inner_radius: f32, col: Rgba) {
self.draw
.rounded_frame(self.pass, outer, inner, inner_radius, col);
}
#[inline]
fn rounded_frame_2col(&mut self, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba) {
self.draw
.rounded_frame_2col(self.pass, outer, inner, c1, c2);
}
}

/// Drawing commands for rounded shapes
Expand All @@ -78,13 +110,12 @@ pub trait DrawRoundedImpl: DrawImpl {
/// Draw a circle or oval of uniform colour
fn circle(&mut self, pass: PassId, rect: Quad, inner_radius: f32, col: Rgba);

/// Draw a circle or oval with two colours
fn circle_2col(&mut self, pass: PassId, rect: Quad, col1: Rgba, col2: Rgba);

/// Draw a frame with rounded corners and uniform colour
fn rounded_frame(
&mut self,
pass: PassId,
outer: Quad,
inner: Quad,
inner_radius: f32,
col: Rgba,
);
fn rounded_frame(&mut self, pass: PassId, outer: Quad, inner: Quad, r1: f32, col: Rgba);

/// Draw a frame with rounded corners with two colours
fn rounded_frame_2col(&mut self, pass: PassId, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba);
}
26 changes: 25 additions & 1 deletion crates/kas-core/src/geom/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use crate::cast::{CastFloat, Conv};
use crate::geom::{Coord, Offset, Rect, Size};
use std::ops::{Add, Div, Mul, Neg, Sub};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub};

/// Axis-aligned 2D cuboid, specified via two corners `a` and `b`
///
Expand Down Expand Up @@ -103,6 +103,14 @@ impl Quad {
}
}

impl AddAssign<Vec2> for Quad {
#[inline]
fn add_assign(&mut self, rhs: Vec2) {
self.a += rhs;
self.b += rhs;
}
}

impl From<Rect> for Quad {
#[inline]
fn from(rect: Rect) -> Quad {
Expand Down Expand Up @@ -290,6 +298,22 @@ macro_rules! impl_vec2 {
}
}

impl AddAssign<$T> for $T {
#[inline]
fn add_assign(&mut self, rhs: $T) {
self.0 += rhs.0;
self.1 += rhs.1;
}
}

impl AddAssign<$f> for $T {
#[inline]
fn add_assign(&mut self, rhs: $f) {
self.0 += rhs;
self.1 += rhs;
}
}

impl Sub<$T> for $T {
type Output = $T;
#[inline]
Expand Down
7 changes: 7 additions & 0 deletions crates/kas-theme/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const MIN_HIGHLIGHT: f32 = 0.2;
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "config", derive(serde::Serialize, serde::Deserialize))]
pub struct Colors<C> {
/// True if this is a dark theme
pub is_dark: bool,
/// Background colour
pub background: C,
/// Colour for frames (not always used)
Expand Down Expand Up @@ -60,6 +62,7 @@ pub type ColorsLinear = Colors<Rgba>;
impl From<ColorsSrgb> for ColorsLinear {
fn from(col: ColorsSrgb) -> Self {
Colors {
is_dark: col.is_dark,
background: col.background.into(),
frame: col.frame.into(),
accent: col.accent.into(),
Expand All @@ -78,6 +81,7 @@ impl From<ColorsSrgb> for ColorsLinear {
impl From<ColorsLinear> for ColorsSrgb {
fn from(col: ColorsLinear) -> Self {
Colors {
is_dark: col.is_dark,
background: col.background.into(),
frame: col.frame.into(),
accent: col.accent.into(),
Expand Down Expand Up @@ -111,6 +115,7 @@ impl ColorsSrgb {
/// Default "light" scheme
pub fn light() -> Self {
Colors {
is_dark: false,
background: Rgba8Srgb::from_str("#FAFAFA").unwrap(),
frame: Rgba8Srgb::from_str("#BCBCBC").unwrap(),
accent: Rgba8Srgb::from_str("#7E3FF2").unwrap(),
Expand All @@ -128,6 +133,7 @@ impl ColorsSrgb {
/// Dark scheme
pub fn dark() -> Self {
Colors {
is_dark: true,
background: Rgba8Srgb::from_str("#404040").unwrap(),
frame: Rgba8Srgb::from_str("#AAAAAA").unwrap(),
accent: Rgba8Srgb::from_str("#F74C00").unwrap(),
Expand All @@ -145,6 +151,7 @@ impl ColorsSrgb {
/// Blue scheme
pub fn blue() -> Self {
Colors {
is_dark: false,
background: Rgba8Srgb::from_str("#FFFFFF").unwrap(),
frame: Rgba8Srgb::from_str("#DADADA").unwrap(),
accent: Rgba8Srgb::from_str("#7CDAFF").unwrap(),
Expand Down
18 changes: 15 additions & 3 deletions crates/kas-theme/src/dim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub struct Parameters {
pub slider_size: Vec2,
/// Progress bar size (horizontal)
pub progress_bar: Vec2,
/// Shadow size (average)
pub shadow_size: Vec2,
/// Proportional offset of shadow (range: -1..=1)
pub shadow_rel_offset: Vec2,
}

/// Dimensions available within [`Window`]
Expand All @@ -63,10 +67,12 @@ pub struct Dimensions {
pub scrollbar: Size,
pub slider: Size,
pub progress_bar: Size,
pub shadow_a: Vec2,
pub shadow_b: Vec2,
}

impl Dimensions {
pub fn new(params: Parameters, pt_size: f32, scale_factor: f32) -> Self {
pub fn new(params: &Parameters, pt_size: f32, scale_factor: f32) -> Self {
let font_id = Default::default();
let dpp = scale_factor * (96.0 / 72.0);
let dpem = dpp * pt_size;
Expand All @@ -81,6 +87,10 @@ impl Dimensions {
let frame_margin = (params.frame_margin * scale_factor).cast_nearest();
let text_margin = (params.text_margin * scale_factor).cast_nearest();
let frame = (params.frame_size * scale_factor).cast_nearest();

let shadow_size = params.shadow_size * scale_factor;
let shadow_offset = shadow_size * params.shadow_rel_offset;

Dimensions {
scale_factor,
dpp,
Expand All @@ -99,6 +109,8 @@ impl Dimensions {
scrollbar: Size::from(params.scrollbar_size * scale_factor),
slider: Size::from(params.slider_size * scale_factor),
progress_bar: Size::from(params.progress_bar * scale_factor),
shadow_a: shadow_offset - shadow_size,
shadow_b: shadow_offset + shadow_size,
}
}
}
Expand All @@ -111,7 +123,7 @@ pub struct Window {

impl Window {
pub fn new(
dims: Parameters,
dims: &Parameters,
pt_size: f32,
scale_factor: f32,
fonts: Rc<LinearMap<TextClass, FontId>>,
Expand All @@ -122,7 +134,7 @@ impl Window {
}
}

pub fn update(&mut self, dims: Parameters, pt_size: f32, scale_factor: f32) {
pub fn update(&mut self, dims: &Parameters, pt_size: f32, scale_factor: f32) {
self.dims = Dimensions::new(dims, pt_size, scale_factor);
}
}
Expand Down
Loading