Skip to content

Update BTerm mouse handling #305

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 19 additions & 8 deletions bracket-terminal/src/bterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ impl BTerm {
self.width_pixels as f32 / max_sizes.0 as f32,
self.height_pixels as f32 / max_sizes.1 as f32,
);
let global_offset = console.get_offset();
let mut offsets = (
center_x as f32 * font_size.0 * (scale - 1.0),
center_y as f32 * font_size.1 * (scale - 1.0),
(center_x as f32 + global_offset.0) * font_size.0 * (scale - 1.0),
(center_y as f32 + global_offset.1) * font_size.1 * (scale - 1.0),
);

let w: f32;
Expand All @@ -261,12 +262,19 @@ impl BTerm {
offsets.1 -= be.screen_scaler.gutter_top as f32;
}

let extent_x = (pos.0 as f32 + offsets.0) / w;
let extent_y = (pos.1 as f32 + offsets.1) / h;
let mut global_offset = console.get_offset();
global_offset.0 *= font_size.0 * scale;
global_offset.1 *= font_size.1 * scale;

let extent_x = (pos.0 as f32 + offsets.0 - global_offset.0) / w;
let extent_y = (pos.1 as f32 + offsets.1 + global_offset.1) / h;
let mouse_x = f32::min(extent_x * max_sizes.0 as f32, max_sizes.0 as f32 - 1.0);
let mouse_y = f32::min(extent_y * max_sizes.1 as f32, max_sizes.1 as f32 - 1.0);

(i32::max(0, mouse_x as i32), i32::max(0, mouse_y as i32))
(
i32::max(-1, mouse_x.floor() as i32),
i32::max(-1, mouse_y.floor() as i32),
)
}

/// Applies the current physical mouse position to the active console, and translates the coordinates into that console's coordinate space.
Expand Down Expand Up @@ -354,13 +362,16 @@ impl BTerm {

/// Internal: mark a mouse press
pub(crate) fn on_mouse_button(&mut self, button_num: usize, pressed: bool) {
if button_num == 0 {
self.left_click = true;
}
let mut input = INPUT.lock();
if pressed {
if button_num == 0 {
self.left_click = true;
}
input.on_mouse_button_down(button_num);
} else {
if button_num == 0 {
self.left_click = false;
}
input.on_mouse_button_up(button_num);
}
input.push_event(BEvent::MouseClick {
Expand Down
4 changes: 4 additions & 0 deletions bracket-terminal/src/consoles/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ pub trait Console {
/// drawing walls between tiles.
fn set_offset(&mut self, x: f32, y: f32);

/// Return the current global offset (by character count, so 0.5 is half a character). Useful for
/// calculating mouse position.
fn get_offset(&self) -> (f32, f32);

/// Specify a scale and center of the console.
/// A scale above 1.0 will make the text larger.
/// The center of the scale is at character position (center_x, center_y).
Expand Down
9 changes: 7 additions & 2 deletions bracket-terminal/src/consoles/flexible_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use crate::prelude::{
use bracket_color::prelude::RGBA;
use bracket_geometry::prelude::{PointF, Rect};
use bracket_rex::prelude::XpColor;
use ultraviolet::Vec2;

use std::any::Any;
use ultraviolet::Vec2;

/// Internal storage structure for sparse tiles.
pub struct FlexiTile {
Expand Down Expand Up @@ -396,6 +395,12 @@ impl Console for FlexiConsole {
self.offset_y = y * (2.0 / self.height as f32);
}

fn get_offset(&self) -> (f32, f32) {
let x = self.offset_x / (2.0 / self.width as f32);
let y = self.offset_y / (2.0 / self.height as f32);
(x, y)
}

fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) {
self.is_dirty = true;
self.scale = scale;
Expand Down
6 changes: 6 additions & 0 deletions bracket-terminal/src/consoles/simple_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ impl Console for SimpleConsole {
self.offset_y = y * (2.0 / self.height as f32);
}

fn get_offset(&self) -> (f32, f32) {
let x = self.offset_x / (2.0 / self.width as f32);
let y = self.offset_y / (2.0 / self.height as f32);
(x, y)
}

fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) {
self.is_dirty = true;
self.scale = scale;
Expand Down
6 changes: 6 additions & 0 deletions bracket-terminal/src/consoles/sparse_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ impl Console for SparseConsole {
self.offset_y = y * (2.0 / self.height as f32);
}

fn get_offset(&self) -> (f32, f32) {
let x = self.offset_x / (2.0 / self.width as f32);
let y = self.offset_y / (2.0 / self.height as f32);
(x, y)
}

fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) {
self.is_dirty = true;
self.scale = scale;
Expand Down
4 changes: 4 additions & 0 deletions bracket-terminal/src/consoles/sprite_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ impl Console for SpriteConsole {
/// character size; so -0.5 will offset half a character to the left/top.
fn set_offset(&mut self, _x: f32, _y: f32) {}

fn get_offset(&self) -> (f32, f32) {
(0.0, 0.0)
}

fn set_scale(&mut self, _scale: f32, _center_x: i32, _center_y: i32) {}

fn get_scale(&self) -> (f32, i32, i32) {
Expand Down
4 changes: 4 additions & 0 deletions bracket-terminal/src/consoles/virtual_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ impl Console for VirtualConsole {
panic!("Unsupported on virtual consoles.");
}

fn get_offset(&self) -> (f32, f32) {
(0.0, 0.0)
}

fn set_scale(&mut self, _scale: f32, _center_x: i32, _center_y: i32) {
panic!("Unsupported on virtual consoles.");
}
Expand Down