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

Added methods to get/set/move the cursor in Editbox and InputText #789

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions src/ui/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use button::Button;
pub use checkbox::Checkbox;
pub use combobox::ComboBox;
pub use editbox::Editbox;
pub(crate) use editbox::EditboxState;
pub use group::{Group, GroupToken};
pub use input::InputText;
pub use label::Label;
Expand Down
30 changes: 29 additions & 1 deletion src/ui/widgets/editbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Editbox<'a> {

mod text_editor;

use text_editor::EditboxState;
pub(crate) use text_editor::EditboxState;

const LEFT_MARGIN: f32 = 2.;

Expand Down Expand Up @@ -78,6 +78,34 @@ impl<'a> Editbox<'a> {
}
}

pub fn get_cursor(&self, ui: &mut Ui) -> u32 {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
state.cursor
}

pub fn set_cursor(&self, ui: &mut Ui, cursor: u32) {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
state.cursor = cursor
}

pub fn move_cursor(&self, ui: &mut Ui, amount: i32) {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
if amount > 0 {
state.cursor = state.cursor.saturating_add(amount as u32);
} else if amount < 0 {
state.cursor = state.cursor.saturating_sub(-amount as u32);
}
}

fn apply_keyboard_input(
&self,
input_buffer: &mut Vec<InputCharacter>,
Expand Down
33 changes: 32 additions & 1 deletion src/ui/widgets/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
math::{vec2, Vec2},
ui::{widgets::Editbox, ElementState, Id, Layout, Ui, UiContent},
ui::{
widgets::{Editbox, EditboxState},
ElementState, Id, Layout, Ui, UiContent,
},
};

pub struct InputText<'a> {
Expand Down Expand Up @@ -70,6 +73,34 @@ impl<'a> InputText<'a> {
}
}

pub fn get_cursor(&self, ui: &mut Ui) -> u32 {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
state.cursor
}

pub fn set_cursor(&self, ui: &mut Ui, cursor: u32) {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
state.cursor = cursor
}

pub fn move_cursor(&self, ui: &mut Ui, amount: i32) {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
if amount > 0 {
state.cursor = state.cursor.saturating_add(amount as u32);
} else if amount < 0 {
state.cursor = state.cursor.saturating_sub(-amount as u32);
}
}

pub fn margin(self, margin: Vec2) -> Self {
Self {
margin: Some(margin),
Expand Down
Loading