Skip to content

Commit

Permalink
Added methods to get/set/move the cursor in Editbox and InputText
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-915 committed Sep 4, 2024
1 parent e21e5e7 commit 402b07e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
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 @@ -17,7 +17,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 @@ -68,6 +68,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 @@ -67,6 +70,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 ui(self, ui: &mut Ui, data: &mut String) {
let context = ui.get_active_window_context();

Expand Down

0 comments on commit 402b07e

Please sign in to comment.