Use in-place mutation in builders

Since we consume self anyway, this style is much more readable
and might have better performance in non-optimized builds.
This commit is contained in:
Joonas Javanainen 2016-08-07 19:53:31 +03:00
parent 8c64697adc
commit 9c56e48140
8 changed files with 234 additions and 191 deletions

View File

@ -191,6 +191,14 @@ impl ImGuiWindowFlags {
self - mask
}
}
#[inline]
pub fn set(&mut self, mask: ImGuiWindowFlags, value: bool) {
if value {
*self |= mask;
} else {
*self -= mask;
}
}
}
bitflags!(
@ -236,6 +244,14 @@ impl ImGuiInputTextFlags {
self - mask
}
}
#[inline]
pub fn set(&mut self, mask: ImGuiInputTextFlags, value: bool) {
if value {
*self |= mask;
} else {
*self -= mask;
}
}
}
@ -275,6 +291,14 @@ impl ImGuiTreeNodeFlags {
self - mask
}
}
#[inline]
pub fn set(&mut self, mask: ImGuiTreeNodeFlags, value: bool) {
if value {
*self |= mask;
} else {
*self -= mask;
}
}
}
pub type ImGuiTextEditCallback =

View File

@ -16,136 +16,103 @@ use super::{ImGuiInputTextFlags,
macro_rules! impl_text_flags {
($InputType:ident) => {
#[inline]
pub fn flags(self, flags: ImGuiInputTextFlags) -> Self {
$InputType {
flags: flags,
.. self
}
pub fn flags(mut self, flags: ImGuiInputTextFlags) -> Self {
self.flags = flags;
self
}
#[inline]
pub fn chars_decimal(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsDecimal, value),
.. self
}
pub fn chars_decimal(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CharsDecimal, value);
self
}
#[inline]
pub fn chars_hexadecimal(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsHexadecimal, value),
.. self
}
pub fn chars_hexadecimal(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CharsHexadecimal, value);
self
}
#[inline]
pub fn chars_uppercase(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsUppercase, value),
.. self
}
pub fn chars_uppercase(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CharsUppercase, value);
self
}
#[inline]
pub fn chars_noblank(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsNoBlank, value),
.. self
}
pub fn chars_noblank(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CharsNoBlank, value);
self
}
#[inline]
pub fn auto_select_all(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_AutoSelectAll, value),
.. self
}
pub fn auto_select_all(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_AutoSelectAll, value);
self
}
#[inline]
pub fn enter_returns_true(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_EnterReturnsTrue, value),
.. self
}
pub fn enter_returns_true(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_EnterReturnsTrue, value);
self
}
#[inline]
pub fn callback_completion(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCompletion, value),
.. self
}
pub fn callback_completion(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CallbackCompletion, value);
self
}
#[inline]
pub fn callback_history(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackHistory, value),
.. self
}
pub fn callback_history(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CallbackHistory, value);
self
}
#[inline]
pub fn callback_always(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackAlways, value),
.. self
}
pub fn callback_always(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CallbackAlways, value);
self
}
#[inline]
pub fn callback_char_filter(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCharFilter, value),
.. self
}
pub fn callback_char_filter(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_CallbackCharFilter, value);
self
}
#[inline]
pub fn allow_tab_input(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_AllowTabInput, value),
.. self
}
pub fn allow_tab_input(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_AllowTabInput, value);
self
}
#[inline]
pub fn no_horizontal_scroll(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_NoHorizontalScroll, value),
.. self
}
pub fn no_horizontal_scroll(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_NoHorizontalScroll, value);
self
}
#[inline]
pub fn always_insert_mode(self, value: bool) -> Self {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_AlwaysInsertMode, value),
.. self
}
pub fn always_insert_mode(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags_AlwaysInsertMode, value);
self
}
}
}
macro_rules! impl_step_params {
($InputType:ident, $Value:ty) => {
#[inline]
pub fn step(self, value: $Value) -> Self {
$InputType {
step: value,
.. self
}
pub fn step(mut self, value: $Value) -> Self {
self.step = value;
self
}
#[inline]
pub fn step_fast(self, value: $Value) -> Self {
$InputType {
step_fast: value,
.. self
}
pub fn step_fast(mut self, value: $Value) -> Self {
self.step_fast = value;
self
}
}
}
@ -153,11 +120,9 @@ macro_rules! impl_step_params {
macro_rules! impl_precision_params {
($InputType:ident) => {
#[inline]
pub fn decimal_precision(self, value: i32) -> Self {
$InputType {
decimal_precision: value,
.. self
}
pub fn decimal_precision(mut self, value: i32) -> Self {
self.decimal_precision = value;
self
}
}
}

View File

@ -20,7 +20,10 @@ impl<'ui, 'p> Menu<'ui, 'p> {
}
}
#[inline]
pub fn enabled(self, enabled: bool) -> Self { Menu { enabled: enabled, ..self } }
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) };
if render {
@ -50,15 +53,20 @@ impl<'ui, 'p> MenuItem<'ui, 'p> {
}
}
#[inline]
pub fn shortcut(self, shortcut: ImStr<'p>) -> Self {
MenuItem { shortcut: Some(shortcut), ..self }
pub fn shortcut(mut self, shortcut: ImStr<'p>) -> Self {
self.shortcut = Some(shortcut);
self
}
#[inline]
pub fn selected(self, selected: &'p mut bool) -> Self {
MenuItem { selected: Some(selected), ..self }
pub fn selected(mut self, selected: &'p mut bool) -> Self {
self.selected = Some(selected);
self
}
#[inline]
pub fn enabled(self, enabled: bool) -> Self { MenuItem { enabled: enabled, ..self } }
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn build(self) -> bool {
let label = self.label.as_ptr();
let shortcut = self.shortcut.map(|x| x.as_ptr()).unwrap_or(ptr::null());

View File

@ -28,28 +28,33 @@ impl<'p> PlotHistogram<'p> {
}
#[inline]
pub fn values_offset(self, values_offset: usize) -> Self {
PlotHistogram { values_offset: values_offset, ..self }
pub fn values_offset(mut self, values_offset: usize) -> Self {
self.values_offset = values_offset;
self
}
#[inline]
pub fn overlay_text(self, overlay_text: ImStr<'p>) -> Self {
PlotHistogram { overlay_text: Some(overlay_text), ..self }
pub fn overlay_text(mut self, overlay_text: ImStr<'p>) -> Self {
self.overlay_text = Some(overlay_text);
self
}
#[inline]
pub fn scale_min(self, scale_min: f32) -> Self {
PlotHistogram { scale_min: scale_min, ..self }
pub fn scale_min(mut self, scale_min: f32) -> Self {
self.scale_min = scale_min;
self
}
#[inline]
pub fn scale_max(self, scale_max: f32) -> Self {
PlotHistogram { scale_max: scale_max, ..self }
pub fn scale_max(mut self, scale_max: f32) -> Self {
self.scale_max = scale_max;
self
}
#[inline]
pub fn graph_size(self, graph_size: ImVec2) -> Self {
PlotHistogram { graph_size: graph_size, ..self }
pub fn graph_size(mut self, graph_size: ImVec2) -> Self {
self.graph_size = graph_size;
self
}
pub fn build(self) {

View File

@ -28,24 +28,33 @@ impl<'p> PlotLines<'p> {
}
#[inline]
pub fn values_offset(self, values_offset: usize) -> Self {
PlotLines { values_offset: values_offset, ..self }
pub fn values_offset(mut self, values_offset: usize) -> Self {
self.values_offset = values_offset;
self
}
#[inline]
pub fn overlay_text(self, overlay_text: ImStr<'p>) -> Self {
PlotLines { overlay_text: Some(overlay_text), ..self }
pub fn overlay_text(mut self, overlay_text: ImStr<'p>) -> Self {
self.overlay_text = Some(overlay_text);
self
}
#[inline]
pub fn scale_min(self, scale_min: f32) -> Self { PlotLines { scale_min: scale_min, ..self } }
pub fn scale_min(mut self, scale_min: f32) -> Self {
self.scale_min = scale_min;
self
}
#[inline]
pub fn scale_max(self, scale_max: f32) -> Self { PlotLines { scale_max: scale_max, ..self } }
pub fn scale_max(mut self, scale_max: f32) -> Self {
self.scale_max = scale_max;
self
}
#[inline]
pub fn graph_size(self, graph_size: ImVec2) -> Self {
PlotLines { graph_size: graph_size, ..self }
pub fn graph_size(mut self, graph_size: ImVec2) -> Self {
self.graph_size = graph_size;
self
}
pub fn build(self) {

View File

@ -27,8 +27,9 @@ impl<'ui, 'p> SliderInt<'ui, 'p> {
}
}
#[inline]
pub fn display_format(self, display_format: ImStr<'p>) -> Self {
SliderInt { display_format: display_format, ..self }
pub fn display_format(mut self, display_format: ImStr<'p>) -> Self {
self.display_format = display_format;
self
}
pub fn build(self) -> bool {
unsafe {
@ -65,11 +66,15 @@ impl<'ui, 'p> SliderFloat<'ui, 'p> {
}
}
#[inline]
pub fn display_format(self, display_format: ImStr<'p>) -> Self {
SliderFloat { display_format: display_format, ..self }
pub fn display_format(mut self, display_format: ImStr<'p>) -> Self {
self.display_format = display_format;
self
}
#[inline]
pub fn power(self, power: f32) -> Self { SliderFloat { power: power, ..self } }
pub fn power(mut self, power: f32) -> Self {
self.power = power;
self
}
pub fn build(self) -> bool {
unsafe {
imgui_sys::igSliderFloat(self.label.as_ptr(),

View File

@ -26,14 +26,15 @@ impl<'ui, 'p> TreeNode<'ui, 'p> {
}
}
#[inline]
pub fn label(self, label: ImStr<'p>) -> Self { TreeNode { label: Some(label), ..self } }
pub fn label(mut self, label: ImStr<'p>) -> Self {
self.label = Some(label);
self
}
#[inline]
pub fn opened(self, opened: bool, cond: ImGuiSetCond) -> Self {
TreeNode {
opened: opened,
opened_cond: cond,
..self
}
pub fn opened(mut self, opened: bool, cond: ImGuiSetCond) -> Self {
self.opened = opened;
self.opened_cond = cond;
self
}
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe {
@ -69,35 +70,39 @@ impl<'ui, 'p> CollapsingHeader<'ui, 'p> {
}
}
#[inline]
pub fn flags(self, flags: ImGuiTreeNodeFlags) -> Self {
CollapsingHeader { flags: flags, ..self }
pub fn flags(mut self, flags: ImGuiTreeNodeFlags) -> Self {
self.flags = flags;
self
}
#[inline]
pub fn selected(self, value: bool) -> Self {
CollapsingHeader { flags: self.flags.with(ImGuiTreeNodeFlags_Selected, value), ..self }
pub fn selected(mut self, value: bool) -> Self {
self.flags.set(ImGuiTreeNodeFlags_Selected, value);
self
}
#[inline]
pub fn default_open(self, value: bool) -> Self {
CollapsingHeader { flags: self.flags.with(ImGuiTreeNodeFlags_DefaultOpen, value), ..self }
pub fn default_open(mut self, value: bool) -> Self {
self.flags.set(ImGuiTreeNodeFlags_DefaultOpen, value);
self
}
#[inline]
pub fn open_on_double_click(self, value: bool) -> Self {
CollapsingHeader {
flags: self.flags.with(ImGuiTreeNodeFlags_OpenOnDoubleClick, value),
..self
}
pub fn open_on_double_click(mut self, value: bool) -> Self {
self.flags.set(ImGuiTreeNodeFlags_OpenOnDoubleClick, value);
self
}
#[inline]
pub fn open_on_arrow(self, value: bool) -> Self {
CollapsingHeader { flags: self.flags.with(ImGuiTreeNodeFlags_OpenOnArrow, value), ..self }
pub fn open_on_arrow(mut self, value: bool) -> Self {
self.flags.set(ImGuiTreeNodeFlags_OpenOnArrow, value);
self
}
#[inline]
pub fn leaf(self, value: bool) -> Self {
CollapsingHeader { flags: self.flags.with(ImGuiTreeNodeFlags_Leaf, value), ..self }
pub fn leaf(mut self, value: bool) -> Self {
self.flags.set(ImGuiTreeNodeFlags_Leaf, value);
self
}
#[inline]
pub fn bullet(self, value: bool) -> Self {
CollapsingHeader { flags: self.flags.with(ImGuiTreeNodeFlags_Bullet, value), ..self }
pub fn bullet(mut self, value: bool) -> Self {
self.flags.set(ImGuiTreeNodeFlags_Bullet, value);
self
}
pub fn build(self) -> bool {
unsafe { imgui_sys::igCollapsingHeader(self.label.as_ptr(), self.flags) }

View File

@ -40,94 +40,116 @@ impl<'ui, 'p> Window<'ui, 'p> {
}
}
#[inline]
pub fn position(self, pos: (f32, f32), cond: ImGuiSetCond) -> Self {
Window {
pos: pos,
pos_cond: cond,
..self
}
pub fn position(mut self, pos: (f32, f32), cond: ImGuiSetCond) -> Self {
self.pos = pos;
self.pos_cond = cond;
self
}
#[inline]
pub fn size(self, size: (f32, f32), cond: ImGuiSetCond) -> Self {
Window {
size: size,
size_cond: cond,
..self
}
pub fn size(mut self, size: (f32, f32), cond: ImGuiSetCond) -> Self {
self.size = size;
self.size_cond = cond;
self
}
#[inline]
pub fn opened(self, opened: &'p mut bool) -> Self { Window { opened: Some(opened), ..self } }
#[inline]
pub fn bg_alpha(self, bg_alpha: f32) -> Self { Window { bg_alpha: bg_alpha, ..self } }
#[inline]
pub fn flags(self, flags: ImGuiWindowFlags) -> Self { Window { flags: flags, ..self } }
#[inline]
pub fn title_bar(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoTitleBar, !value), ..self }
pub fn opened(mut self, opened: &'p mut bool) -> Self {
self.opened = Some(opened);
self
}
#[inline]
pub fn resizable(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoResize, !value), ..self }
pub fn bg_alpha(mut self, bg_alpha: f32) -> Self {
self.bg_alpha = bg_alpha;
self
}
#[inline]
pub fn movable(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoMove, !value), ..self }
pub fn flags(mut self, flags: ImGuiWindowFlags) -> Self {
self.flags = flags;
self
}
#[inline]
pub fn scroll_bar(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoScrollbar, !value), ..self }
pub fn title_bar(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoTitleBar, !value);
self
}
#[inline]
pub fn scrollable(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoScrollWithMouse, !value), ..self }
pub fn resizable(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoResize, !value);
self
}
#[inline]
pub fn collapsible(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoCollapse, !value), ..self }
pub fn movable(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoMove, !value);
self
}
#[inline]
pub fn always_auto_resize(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_AlwaysAutoResize, value), ..self }
pub fn scroll_bar(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoScrollbar, !value);
self
}
#[inline]
pub fn show_borders(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_ShowBorders, value), ..self }
pub fn scrollable(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoScrollWithMouse, !value);
self
}
#[inline]
pub fn save_settings(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoSavedSettings, !value), ..self }
pub fn collapsible(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoCollapse, !value);
self
}
#[inline]
pub fn inputs(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoInputs, !value), ..self }
pub fn always_auto_resize(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_AlwaysAutoResize, value);
self
}
#[inline]
pub fn menu_bar(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_MenuBar, value), ..self }
pub fn show_borders(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_ShowBorders, value);
self
}
#[inline]
pub fn horizontal_scrollbar(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_HorizontalScrollbar, value), ..self }
pub fn save_settings(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoSavedSettings, !value);
self
}
#[inline]
pub fn no_focus_on_appearing(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoFocusOnAppearing, value), ..self }
pub fn inputs(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoInputs, !value);
self
}
#[inline]
pub fn no_bring_to_front_on_focus(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_NoBringToFrontOnFocus, value), ..self }
pub fn menu_bar(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_MenuBar, value);
self
}
#[inline]
pub fn always_vertical_scollbar(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_AlwaysVerticalScrollbar, value), ..self }
pub fn horizontal_scrollbar(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_HorizontalScrollbar, value);
self
}
#[inline]
pub fn always_horizontal_scrollbar(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_AlwaysHorizontalScrollbar, value), ..self }
pub fn no_focus_on_appearing(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoFocusOnAppearing, value);
self
}
#[inline]
pub fn always_use_window_padding(self, value: bool) -> Self {
Window { flags: self.flags.with(ImGuiWindowFlags_AlwaysUseWindowPadding, value), ..self }
pub fn no_bring_to_front_on_focus(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_NoBringToFrontOnFocus, value);
self
}
#[inline]
pub fn always_vertical_scollbar(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_AlwaysVerticalScrollbar, value);
self
}
#[inline]
pub fn always_horizontal_scrollbar(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_AlwaysHorizontalScrollbar, value);
self
}
#[inline]
pub fn always_use_window_padding(mut self, value: bool) -> Self {
self.flags.set(ImGuiWindowFlags_AlwaysUseWindowPadding, value);
self
}
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe {