Skip to content

Commit

Permalink
Merge pull request #152 from kas-gui/work
Browse files Browse the repository at this point in the history
Conv and ConvFloat conversion traits, i32 in Size, new Offset type, fn extract
  • Loading branch information
dhardy authored Feb 2, 2021
2 parents bdcc79e + 5a6243f commit 882e9b2
Show file tree
Hide file tree
Showing 55 changed files with 1,218 additions and 757 deletions.
22 changes: 11 additions & 11 deletions kas-macros/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ pub(crate) fn data_type(children: &Vec<Child>, layout: &LayoutArgs) -> Result<To
}

let col_temp = if cols > 16 {
quote! { Vec<u32> }
quote! { Vec<i32> }
} else {
quote! { [u32; #cols] }
quote! { [i32; #cols] }
};
let row_temp = if rows > 16 {
quote! { Vec<u32> }
quote! { Vec<i32> }
} else {
quote! { [u32; #rows] }
quote! { [i32; #rows] }
};

Ok(match layout.layout {
Expand All @@ -76,7 +76,7 @@ pub(crate) fn data_type(children: &Vec<Child>, layout: &LayoutArgs) -> Result<To
l @ LayoutType::Right | l @ LayoutType::Left => quote! {
type Data = kas::layout::FixedRowStorage::<
[kas::layout::SizeRules; #cols + 1],
[u32; #cols],
[i32; #cols],
>;
type Solver = kas::layout::RowSolver::<
Self::Data,
Expand All @@ -90,7 +90,7 @@ pub(crate) fn data_type(children: &Vec<Child>, layout: &LayoutArgs) -> Result<To
l @ LayoutType::Down | l @ LayoutType::Up => quote! {
type Data = kas::layout::FixedRowStorage::<
[kas::layout::SizeRules; #rows + 1],
[u32; #rows],
[i32; #rows],
>;
type Solver = kas::layout::RowSolver::<
Self::Data,
Expand Down Expand Up @@ -211,9 +211,9 @@ pub(crate) fn derive(
});

draw.append_all(quote! {
let c0 = self.#ident.rect().pos;
let c1 = c0 + Coord::from(self.#ident.rect().size);
if c0.0 <= pos1.0 && c1.0 >= pos0.0 && c0.1 <= pos1.1 && c1.1 >= pos0.1 {
let c1 = self.#ident.rect().pos;
let c2 = self.#ident.rect().pos2();
if c1.0 <= pos2.0 && c2.0 >= pos1.0 && c1.1 <= pos2.1 && c2.1 >= pos1.1 {
self.#ident.draw(draw_handle, mgr, disabled);
}
});
Expand Down Expand Up @@ -295,8 +295,8 @@ pub(crate) fn derive(
use kas::{geom::Coord, WidgetCore};

let rect = draw_handle.target_rect();
let pos0 = rect.pos;
let pos1 = rect.pos + Coord::from(rect.size);
let pos1 = rect.pos;
let pos2 = rect.pos2();
let disabled = disabled || self.is_disabled();
#draw
}
Expand Down
69 changes: 35 additions & 34 deletions kas-theme/src/dim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use std::any::Any;
use std::f32;

use kas::conv::{Conv, ConvFloat};
use kas::draw::{self, TextClass};
use kas::geom::{Size, Vec2};
use kas::layout::{AxisInfo, Margins, SizeRules, StretchPolicy};
Expand Down Expand Up @@ -44,14 +45,14 @@ pub struct Dimensions {
pub dpp: f32,
pub pt_size: f32,
pub font_marker_width: f32,
pub line_height: u32,
pub min_line_length: u32,
pub ideal_line_length: u32,
pub outer_margin: u32,
pub inner_margin: u32,
pub frame: u32,
pub button_frame: u32,
pub checkbox: u32,
pub line_height: i32,
pub min_line_length: i32,
pub ideal_line_length: i32,
pub outer_margin: u16,
pub inner_margin: u16,
pub frame: i32,
pub button_frame: i32,
pub checkbox: i32,
pub scrollbar: Size,
pub slider: Size,
pub progress_bar: Size,
Expand All @@ -62,24 +63,24 @@ impl Dimensions {
let font_id = Default::default();
let dpp = scale_factor * (96.0 / 72.0);
let dpem = dpp * pt_size;
let line_height = kas::text::fonts::fonts().get(font_id).height(dpem).ceil() as u32;
let line_height = i32::conv_ceil(kas::text::fonts::fonts().get(font_id).height(dpem));

let outer_margin = (params.outer_margin * scale_factor).round() as u32;
let inner_margin = (params.inner_margin * scale_factor).round() as u32;
let frame = (params.frame_size * scale_factor).round() as u32;
let outer_margin = u16::conv_nearest(params.outer_margin * scale_factor);
let inner_margin = u16::conv_nearest(params.inner_margin * scale_factor);
let frame = i32::conv_nearest(params.frame_size * scale_factor);
Dimensions {
scale_factor,
dpp,
pt_size,
font_marker_width: (1.6 * scale_factor).round().max(1.0),
line_height,
min_line_length: (8.0 * dpem).round() as u32,
ideal_line_length: (24.0 * dpem).round() as u32,
min_line_length: i32::conv_nearest(8.0 * dpem),
ideal_line_length: i32::conv_nearest(24.0 * dpem),
outer_margin,
inner_margin,
frame,
button_frame: (params.button_frame * scale_factor).round() as u32,
checkbox: (9.0 * dpp).round() as u32 + 2 * (inner_margin + frame),
button_frame: i32::conv_nearest(params.button_frame * scale_factor),
checkbox: i32::conv_nearest(9.0 * dpp) + 2 * (i32::from(inner_margin) + frame),
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),
Expand Down Expand Up @@ -138,23 +139,23 @@ impl<'a> draw::SizeHandle for SizeHandle<'a> {
}

fn frame(&self) -> Size {
let f = self.dims.frame as u32;
Size::uniform(f)
let f = self.dims.frame;
Size::splat(f)
}
fn menu_frame(&self) -> Size {
let f = self.dims.frame as u32;
Size(f, f / 2)
let f = self.dims.frame;
Size::new(f, f / 2)
}

fn inner_margin(&self) -> Size {
Size::uniform(self.dims.inner_margin as u32)
Size::splat(self.dims.inner_margin.into())
}

fn outer_margins(&self) -> Margins {
Margins::uniform(self.dims.outer_margin as u16)
Margins::splat(self.dims.outer_margin)
}

fn line_height(&self, _: TextClass) -> u32 {
fn line_height(&self, _: TextClass) -> i32 {
self.dims.line_height
}

Expand All @@ -170,9 +171,9 @@ impl<'a> draw::SizeHandle for SizeHandle<'a> {

let mut bounds = kas::text::Vec2::INFINITY;
if let Some(size) = axis.size_other_if_fixed(false) {
bounds.1 = size as f32;
bounds.1 = f32::conv(size);
} else if let Some(size) = axis.size_other_if_fixed(true) {
bounds.0 = size as f32;
bounds.0 = f32::conv(size);
}
env.set_bounds(bounds);

Expand All @@ -185,10 +186,10 @@ impl<'a> draw::SizeHandle for SizeHandle<'a> {
let margin = match class {
TextClass::Label | TextClass::LabelSingle => self.dims.outer_margin,
TextClass::Button | TextClass::Edit | TextClass::EditMulti => self.dims.inner_margin,
} as u16;
};
let margins = (margin, margin);
if axis.is_horizontal() {
let bound = (required.0).ceil() as u32;
let bound = i32::conv_ceil(required.0);
let min = self.dims.min_line_length;
let ideal = self.dims.ideal_line_length;
// NOTE: using different variable-width stretch policies here can
Expand All @@ -201,13 +202,13 @@ impl<'a> draw::SizeHandle for SizeHandle<'a> {
SizeRules::new(min, ideal, margins, policy)
} else {
let min = match class {
TextClass::Label => (required.1).ceil() as u32,
TextClass::Label => i32::conv_ceil(required.1),
TextClass::LabelSingle | TextClass::Button | TextClass::Edit => {
self.dims.line_height
}
TextClass::EditMulti => self.dims.line_height * 3,
};
let ideal = ((required.1).ceil() as u32).max(min);
let ideal = i32::conv_ceil(required.1).max(min);
let stretch = match class {
TextClass::Button | TextClass::Edit | TextClass::LabelSingle => {
StretchPolicy::Fixed
Expand All @@ -224,30 +225,30 @@ impl<'a> draw::SizeHandle for SizeHandle<'a> {
}

fn button_surround(&self) -> (Size, Size) {
let s = Size::uniform(self.dims.button_frame);
let s = Size::splat(self.dims.button_frame);
(s, s)
}

fn edit_surround(&self) -> (Size, Size) {
let s = Size::uniform(self.dims.frame as u32 + self.dims.inner_margin as u32);
let s = Size::splat(self.dims.frame + i32::from(self.dims.inner_margin));
(s, s)
}

fn checkbox(&self) -> Size {
Size::uniform(self.dims.checkbox)
Size::splat(self.dims.checkbox)
}

#[inline]
fn radiobox(&self) -> Size {
self.checkbox()
}

fn scrollbar(&self) -> (Size, u32) {
fn scrollbar(&self) -> (Size, i32) {
let size = self.dims.scrollbar;
(size, 2 * size.0)
}

fn slider(&self) -> (Size, u32) {
fn slider(&self) -> (Size, i32) {
let size = self.dims.slider;
(size, 2 * size.0)
}
Expand Down
24 changes: 12 additions & 12 deletions kas-theme/src/flat_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::f32;
use std::ops::Range;

use crate::{Dimensions, DimensionsParams, DimensionsWindow, Theme, ThemeColours, Window};
use kas::conv::Conv;
use kas::draw::{
self, ClipRegion, Colour, Draw, DrawRounded, DrawShared, DrawText, InputState, Pass,
SizeHandle, TextClass,
Expand Down Expand Up @@ -70,7 +71,7 @@ pub struct DrawHandle<'a, D: Draw> {
pub(crate) window: &'a mut DimensionsWindow,
pub(crate) cols: &'a ThemeColours,
pub(crate) rect: Rect,
pub(crate) offset: Coord,
pub(crate) offset: Offset,
pub(crate) pass: Pass,
}

Expand Down Expand Up @@ -115,7 +116,7 @@ where
window: transmute::<&'a mut Self::Window, &'static mut Self::Window>(window),
cols: transmute::<&'a ThemeColours, &'static ThemeColours>(&self.cols),
rect,
offset: Coord::ZERO,
offset: Offset::ZERO,
pass: super::START_PASS,
}
}
Expand All @@ -133,7 +134,7 @@ where
window,
cols: &self.cols,
rect,
offset: Coord::ZERO,
offset: Offset::ZERO,
pass: super::START_PASS,
}
}
Expand Down Expand Up @@ -209,14 +210,14 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
}
}

fn draw_device(&mut self) -> (kas::draw::Pass, Coord, &mut dyn kas::draw::Draw) {
fn draw_device(&mut self) -> (kas::draw::Pass, Offset, &mut dyn kas::draw::Draw) {
(self.pass, self.offset, self.draw)
}

fn clip_region(
&mut self,
rect: Rect,
offset: Coord,
offset: Offset,
class: ClipRegion,
f: &mut dyn FnMut(&mut dyn draw::DrawHandle),
) {
Expand Down Expand Up @@ -271,7 +272,7 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
&mut self,
pos: Coord,
bounds: Vec2,
offset: Coord,
offset: Offset,
text: &TextDisplay,
class: TextClass,
) {
Expand All @@ -281,7 +282,7 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
.text(self.pass, pos.into(), bounds, offset.into(), text, col);
}

fn text_effects(&mut self, pos: Coord, offset: Coord, text: &dyn TextApi, class: TextClass) {
fn text_effects(&mut self, pos: Coord, offset: Offset, text: &dyn TextApi, class: TextClass) {
self.draw.text_col_effects(
self.pass,
(pos + self.offset).into(),
Expand Down Expand Up @@ -312,7 +313,7 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
&mut self,
pos: Coord,
bounds: Vec2,
offset: Coord,
offset: Offset,
text: &TextDisplay,
range: Range<usize>,
class: TextClass,
Expand Down Expand Up @@ -342,12 +343,12 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
aux: col,
},
Effect {
start: range.start as u32,
start: u32::conv(range.start),
flags: Default::default(),
aux: self.cols.text_sel,
},
Effect {
start: range.end as u32,
start: u32::conv(range.end),
flags: Default::default(),
aux: col,
},
Expand All @@ -360,7 +361,7 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
&mut self,
pos: Coord,
mut bounds: Vec2,
offset: Coord,
offset: Offset,
text: &TextDisplay,
class: TextClass,
byte: usize,
Expand Down Expand Up @@ -434,7 +435,6 @@ impl<'a, D: Draw + DrawRounded + DrawText> draw::DrawHandle for DrawHandle<'a, D
if let Some(col) = self.cols.check_mark_state(state, checked) {
let radius = inner.size().sum() * (1.0 / 16.0);
let inner = inner.shrink(self.window.dims.inner_margin as f32 + radius);
let radius = radius as f32;
self.draw
.rounded_line(self.pass, inner.a, inner.b, radius, col);
self.draw
Expand Down
Loading

0 comments on commit 882e9b2

Please sign in to comment.