Skip to content

Commit

Permalink
Preliminary text input stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Oct 18, 2015
1 parent 8ec34e0 commit 4d37860
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
8 changes: 8 additions & 0 deletions imgui-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ bitflags!(
}
);

impl ImGuiInputTextFlags {
#[inline]
pub fn with(self, mask: ImGuiInputTextFlags, value: bool) -> ImGuiInputTextFlags {
if value { self | mask } else { self - mask }
}
}


bitflags!(
#[repr(C)]
flags ImGuiSelectableFlags: c_int {
Expand Down
163 changes: 163 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use imgui_sys;
use libc::size_t;
use std::marker::PhantomData;
use std::ptr;

use super::{
Ui,
ImGuiInputTextFlags, ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsUppercase, ImGuiInputTextFlags_CharsNoBlank,
ImGuiInputTextFlags_AutoSelectAll, ImGuiInputTextFlags_EnterReturnsTrue,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_AllowTabInput, //ImGuiInputTextFlags_CtrlEnterForNewLine,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_AlwaysInsertMode,
ImStr
};

#[must_use]
pub struct InputText<'ui, 'p> {
label: ImStr<'p>,
buf: &'p mut str,
flags: ImGuiInputTextFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
}

impl<'ui, 'p> InputText<'ui, 'p> {
pub fn new(label: ImStr<'p>, buf: &'p mut str) -> Self {
InputText {
label: label,
buf: buf,
flags: ImGuiInputTextFlags::empty(),
_phantom: PhantomData
}
}

#[inline]
pub fn flags(self, flags: ImGuiInputTextFlags) -> Self {
InputText {
flags: flags,
.. self
}
}

#[inline]
pub fn chars_decimal(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CharsDecimal, value),
.. self
}
}

#[inline]
pub fn chars_hexadecimal(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CharsHexadecimal, value),
.. self
}
}

#[inline]
pub fn chars_uppercase(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CharsUppercase, value),
.. self
}
}

#[inline]
pub fn chars_noblank(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CharsNoBlank, value),
.. self
}
}

#[inline]
pub fn auto_select_all(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_AutoSelectAll, value),
.. self
}
}

#[inline]
pub fn enter_returns_true(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_EnterReturnsTrue, value),
.. self
}
}

#[inline]
pub fn callback_completion(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCompletion, value),
.. self
}
}

#[inline]
pub fn callback_history(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CallbackHistory, value),
.. self
}
}

#[inline]
pub fn callback_always(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CallbackAlways, value),
.. self
}
}

#[inline]
pub fn callback_char_filter(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCharFilter, value),
.. self
}
}

#[inline]
pub fn allow_tab_input(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_AllowTabInput, value),
.. self
}
}

#[inline]
pub fn no_horizontal_scroll(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_NoHorizontalScroll, value),
.. self
}
}

#[inline]
pub fn always_insert_mode(self, value: bool) -> Self {
InputText {
flags: self.flags.with(ImGuiInputTextFlags_AlwaysInsertMode, value),
.. self
}
}

// TODO: boxed closure...?
// pub fn callback(self) -> Self { }

pub fn build(self) -> bool {
unsafe {
imgui_sys::igInputText(
self.label.as_ptr(),
// TODO: this is evil. Perhaps something else than &mut str is better
self.buf.as_ptr() as *mut i8,
self.buf.len() as size_t,
self.flags,
None,
ptr::null_mut())
}
}
}
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ use std::str;

pub use imgui_sys::{
ImDrawIdx, ImDrawVert,
ImGuiInputTextFlags, ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsUppercase, ImGuiInputTextFlags_CharsNoBlank,
ImGuiInputTextFlags_AutoSelectAll, ImGuiInputTextFlags_EnterReturnsTrue,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_AllowTabInput, ImGuiInputTextFlags_CtrlEnterForNewLine,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_AlwaysInsertMode,
ImGuiSetCond,
ImGuiSetCond_Always, ImGuiSetCond_Once,
ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing,
Expand All @@ -29,12 +36,14 @@ pub use imgui_sys::{
ImVec2, ImVec4,
ImGuiKey
};
pub use input::{InputText};
pub use menus::{Menu, MenuItem};
pub use sliders::{SliderFloat, SliderInt};
pub use trees::{TreeNode};
pub use widgets::{CollapsingHeader};
pub use window::{Window};

mod input;
mod menus;
mod sliders;
mod trees;
Expand Down Expand Up @@ -438,6 +447,13 @@ impl<'ui> Ui<'ui> {
}
}

// Widgets: Input
impl<'ui> Ui<'ui> {
pub fn input_text<'p>(&self, label: ImStr<'p>, buf: &'p mut str) -> InputText<'ui, 'p> {
InputText::new(label, buf)
}
}

// Widgets: Sliders
impl<'ui> Ui<'ui> {
pub fn slider_f32<'p>(&self, label: ImStr<'p>,
Expand Down

0 comments on commit 4d37860

Please sign in to comment.