Skip to content

Commit

Permalink
Improve tilt and add option to change it
Browse files Browse the repository at this point in the history
  • Loading branch information
Woyten committed Jun 21, 2024
1 parent 53f007b commit c1c7394
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
1 change: 1 addition & 0 deletions microwave/src/app/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ fn handle_key_event(
"K" => virtual_keyboard.on_screen_keyboard.toggle_next(),
"L" => virtual_keyboard.layout.toggle_next(),
"S" => virtual_keyboard.scale.toggle_next(),
"T" => virtual_keyboard.tilt.toggle_next(),
_ => {}
};
}
Expand Down
46 changes: 31 additions & 15 deletions microwave/src/app/resources/virtual_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub struct VirtualKeyboardResource {
pub scale: Toggle<VirtualKeyboardScale>,
pub layout: Toggle<Option<Arc<VirtualKeyboardLayout>>>,
pub compression: Toggle<Compression>,
avg_step_size: Ratio,
pub tilt: Toggle<Tilt>,
pub avg_step_size: Ratio,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -54,7 +55,7 @@ pub struct VirtualKeyboardLayout {
scale_name: String,
keyboard: IsomorphicKeyboard,
orig_keyboard: IsomorphicKeyboard,
steps: (u16, u16),
num_steps: (u16, u16),
}

#[derive(Debug)]
Expand All @@ -64,6 +65,13 @@ pub enum Compression {
Expanded,
}

#[derive(Debug)]
pub enum Tilt {
Automatic,
Lumatone,
None,
}

impl VirtualKeyboardResource {
pub fn new(scl: &Scl, options: CustomKeyboardOptions) -> VirtualKeyboardResource {
let on_screen_keyboards = vec![
Expand Down Expand Up @@ -109,7 +117,7 @@ impl VirtualKeyboardResource {
scale_name,
keyboard: orig_keyboard.clone().coprime(),
orig_keyboard,
steps: (mos.num_primary_steps(), mos.num_secondary_steps()),
num_steps: (mos.num_primary_steps(), mos.num_secondary_steps()),
},
generate_colors(&isomorphic_layout),
)
Expand All @@ -125,7 +133,7 @@ impl VirtualKeyboardResource {
scale_name: options.layout_name,
keyboard: keyboard.clone(),
orig_keyboard: keyboard,
steps: (options.num_primary_steps, options.num_secondary_steps),
num_steps: (options.num_primary_steps, options.num_secondary_steps),
},
options.colors.0,
)]
Expand All @@ -146,11 +154,14 @@ impl VirtualKeyboardResource {
Compression::Expanded,
];

let tilts = vec![Tilt::Automatic, Tilt::Lumatone, Tilt::None];

VirtualKeyboardResource {
on_screen_keyboard: on_screen_keyboards.into(),
scale: scales.into(),
layout: layouts.into(),
compression: compressions.into(),
tilt: tilts.into(),
avg_step_size,
}
}
Expand Down Expand Up @@ -178,8 +189,22 @@ impl VirtualKeyboardResource {
.unwrap_or("Automatic")
}

pub fn layout_step_counts(&self) -> (u16, u16) {
self.curr_layout().steps
pub fn layout_step_counts(&self) -> (i32, i32) {
match self.tilt.curr_option() {
Tilt::Automatic => {
let num_steps = self.curr_layout().num_steps;
let num_primary_steps = i32::from(num_steps.0);
let num_secondary_steps = i32::from(num_steps.1);
let num_primary_steps = match self.compression.curr_option() {
Compression::None => num_primary_steps,
Compression::Compressed => num_primary_steps - num_secondary_steps,
Compression::Expanded => num_primary_steps + num_secondary_steps,
};
(num_primary_steps, num_secondary_steps)
}
Tilt::Lumatone => (5, 2),
Tilt::None => (1, 0),
}
}

pub fn layout_step_sizes(&self) -> (i32, i32, i32) {
Expand Down Expand Up @@ -212,15 +237,6 @@ impl VirtualKeyboardResource {
.keyboard
.get_key(num_primary_steps, num_secondary_steps)
}

pub fn period(&self) -> Ratio {
let (num_primary_steps, num_secondary_steps) = self.layout_step_counts();
let (primary_step, secondary_step, ..) = self.layout_step_sizes();
self.avg_step_size.repeated(
i32::from(num_primary_steps) * primary_step
+ i32::from(num_secondary_steps) * secondary_step,
)
}
}

fn generate_colors(layout: &IsomorphicLayout) -> Vec<Color> {
Expand Down
4 changes: 3 additions & 1 deletion microwave/src/app/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ fn create_hud_text(
[Alt+S] Scale: {}\n\
[Alt+L] Layout: {}\n\
[Alt+C] Compression: {:?}\n\
[Alt+T] Tilt: {:?}\n\
\n\
[Esc] Back",
scale_steps.0,
Expand All @@ -659,7 +660,8 @@ fn create_hud_text(
virtual_keyboard.on_screen_keyboard.curr_option(),
virtual_keyboard.scale_name(),
virtual_keyboard.layout_name(),
virtual_keyboard.compression.curr_option()
virtual_keyboard.compression.curr_option(),
virtual_keyboard.tilt.curr_option()
)
.unwrap();
}
Expand Down
24 changes: 14 additions & 10 deletions microwave/src/app/view/on_screen_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,26 @@ impl KeyboardCreator<'_, '_, '_> {
const INCLINATION: f32 = 15.0;
const ROTATION_POINT_FACTOR: f32 = 10.0;

let primary_step = Vec2::new(1.0, 0.0); // Hexagonal east direction
let secondary_step = Vec2::new(0.5, -0.5 * 3f32.sqrt()); // Hexagonal south-east direction
let (num_primary_steps, num_secondary_steps) = virtual_keyboard.layout_step_counts();
let geometric_period = f32::from(num_primary_steps) * primary_step
+ f32::from(num_secondary_steps) * secondary_step;
let (primary_step, secondary_step, ..) = virtual_keyboard.layout_step_sizes();
let geom_primary_step = Vec2::new(1.0, 0.0); // Hexagonal east direction
let geom_secondary_step = Vec2::new(0.5, -0.5 * 3f32.sqrt()); // Hexagonal south-east direction

let board_angle = geometric_period.angle_between(Vec2::X);
let period = virtual_keyboard
.avg_step_size
.repeated(num_primary_steps * primary_step + num_secondary_steps * secondary_step);
let geom_period = num_primary_steps as f32 * geom_primary_step
+ num_secondary_steps as f32 * geom_secondary_step;

let board_angle = geom_period.angle_between(Vec2::X);
let board_rotation = Mat2::from_angle(board_angle);

let key_stride = virtual_keyboard
.period()
.divided_into_equal_steps(geometric_period.length())
let key_stride = period
.divided_into_equal_steps(geom_period.length())
.num_equal_steps_of_size(self.main_view.pitch_range()) as f32;

let primary_stride_2d = key_stride * (board_rotation * primary_step);
let secondary_stride_2d = key_stride * (board_rotation * secondary_step);
let primary_stride_2d = key_stride * (board_rotation * geom_primary_step);
let secondary_stride_2d = key_stride * (board_rotation * geom_secondary_step);

let (x_range, y_range) = self.get_bounding_box();
let offset = self.main_view.hor_world_coord(tuning.1.ref_pitch) as f32;
Expand Down

0 comments on commit c1c7394

Please sign in to comment.