Skip to content

Commit

Permalink
Fix input camera
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rianb committed Jan 23, 2022
1 parent 60b5be0 commit 45fda3a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
- preferences
- rework font
- submenu/inputs
- x camera movements
- relative path in build
2 changes: 1 addition & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Camera {
Vector2::new(self.computed_x(), self.computed_y())
}

pub fn get_cursor_real_x(&self, cursor: &Cursor) -> f32 {
pub fn get_cursor_x_with_offset(&self, cursor: &Cursor) -> f32 {
cursor.real_x() + self.initial_x - cursor.font.borrow().char_width
}

Expand Down
6 changes: 3 additions & 3 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ impl Editor {
self.update_camera();
}

fn update_camera(&mut self) {
pub(crate) fn update_camera(&mut self) {
// Horizontal Scroll
if self.camera.get_cursor_real_x(&self.cursor) < self.camera.computed_x() + self.camera.safe_zone_size {
self.camera.move_x(self.camera.get_cursor_real_x(&self.cursor) - self.camera.computed_x() - self.camera.safe_zone_size)
if self.camera.get_cursor_x_with_offset(&self.cursor) < self.camera.computed_x() + self.camera.safe_zone_size {
self.camera.move_x(self.camera.get_cursor_x_with_offset(&self.cursor) - self.camera.computed_x() - self.camera.safe_zone_size)
} else if self.padding + self.cursor.real_x() - self.camera.computed_x() > self.camera.width - self.camera.safe_zone_size {
self.camera.move_x(self.padding + self.cursor.real_x() - self.camera.computed_x() - self.camera.width + self.camera.safe_zone_size)
}
Expand Down
17 changes: 14 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use regex::Regex;
use speedy2d::color::Color;
use speedy2d::dimen::Vector2;
use speedy2d::Graphics2D;
use speedy2d::shape::Rectangle;
use speedy2d::window::{UserEventSender, VirtualKeyCode};

use crate::{Animation, Editable, Editor, EditorEvent, FocusElement, MenuAction, MenuActionFn, MenuId};
Expand All @@ -13,7 +14,7 @@ use crate::camera::Camera;
use crate::render_helper::draw_rounded_rectangle_with_border;

pub const MIN_INPUT_WIDTH: f32 = 200.;
pub const MAX_INPUT_WIDTH: f32 = 1000.;
pub const MAX_INPUT_WIDTH: f32 = 500.;

const ANIMATION_DURATION: f32 = 100.;

Expand Down Expand Up @@ -104,9 +105,10 @@ impl Input {
pub fn new(menu_id: MenuId, action_fn: MenuActionFn, es: UserEventSender<EditorEvent>) -> Self {
let mut editor = Editor::new(MIN_INPUT_WIDTH, 50., Vector2::ZERO, 10.); // arbitrary
editor.font.borrow_mut().change_font_size(-6); // Set font size to 10
editor.set_event_sender(Some(es));
editor.camera.safe_zone_size = 0.;
let offset = Vector2::new(0., (50. - editor.font.borrow().char_height) / 2. - 10.);
editor.set_offset(offset);
editor.set_event_sender(Some(es));
Self {
editor,
is_focus: false,
Expand All @@ -122,6 +124,7 @@ impl Input {
pub fn focus(&mut self) {
if self.width == 0. { self.set_width(MIN_INPUT_WIDTH); }
self.is_focus = true;
self.editor.update_camera();
self.editor.event_sender.as_ref().unwrap().send_event(
EditorEvent::Focus(FocusElement::MenuInput(self.menu_id))
).unwrap()
Expand All @@ -139,6 +142,7 @@ impl Input {
let animation_width = Animation::new(self.computed_width(), width, ANIMATION_DURATION, EasingFunction::SmootherStep, self.editor.event_sender.clone().unwrap());
self.animation_width = Some(animation_width);
self.width = width;
self.editor.camera.width = width;
}

pub fn set_placeholder(&mut self, text: &str) {
Expand Down Expand Up @@ -187,7 +191,7 @@ impl Input {
pub fn update_text_layout(&mut self) {
self.editor.update_text_layout();
let width_left = self.width - self.editor.lines.first().unwrap().formatted_text_block.width();
const WIDTH_OFFSET: f32 = 25.;
const WIDTH_OFFSET: f32 = 0.;
if width_left < WIDTH_OFFSET {
self.set_width((self.width + WIDTH_OFFSET - width_left).clamp(MIN_INPUT_WIDTH, MAX_INPUT_WIDTH));
} else if width_left >= WIDTH_OFFSET {
Expand All @@ -201,7 +205,14 @@ impl Input {
draw_rounded_rectangle_with_border(x, y, self.computed_width(), self.height, 8., 0.5, Color::from_int_rgba(250, 250, 250, 255), graphics);
// Draw text
let line = self.editor.lines.first().unwrap();
graphics.set_clip(Some(
Rectangle::new(
Vector2::new(x as i32, y as i32),
Vector2::new((x + self.width) as i32, (y + self.height) as i32)
)
));
line.render(x - self.editor.camera.computed_x(), y - self.editor.camera.computed_y(), graphics);
graphics.set_clip(Option::None);

let input_camera = Camera::from_with_offset(&self.editor.camera, Vector2::new(-x, -y));
self.editor.cursor.render(&input_camera, graphics);
Expand Down

0 comments on commit 45fda3a

Please sign in to comment.