1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
use std::mem;
use std::os::raw::{c_char, c_void};
use std::ptr;
use std::thread;
use crate::context::Context;
use crate::fonts::atlas::FontId;
use crate::internal::RawCast;
use crate::style::{StyleColor, StyleVar};
use crate::sys;
use crate::{Id, Ui};
/// # Parameter stacks (shared)
impl<'ui> Ui<'ui> {
/// Switches to the given font by pushing it to the font stack.
///
/// Returns a `FontStackToken` that must be popped by calling `.pop()`
///
/// # Panics
///
/// Panics if the font atlas does not contain the given font
///
/// # Examples
///
/// ```no_run
/// # use arcdps_imgui::*;
/// # let mut ctx = Context::create();
/// # let font_data_sources = [];
/// // At initialization time
/// let my_custom_font = ctx.fonts().add_font(&font_data_sources);
/// # let ui = ctx.frame();
/// // During UI construction
/// let font = ui.push_font(my_custom_font);
/// ui.text("I use the custom font!");
/// font.pop();
/// ```
#[doc(alias = "PushFont")]
pub fn push_font(&self, id: FontId) -> FontStackToken<'_> {
let fonts = self.fonts();
let font = fonts
.get_font(id)
.expect("Font atlas did not contain the given font");
unsafe { sys::igPushFont(font.raw() as *const _ as *mut _) };
FontStackToken::new(self)
}
/// Changes a style color by pushing a change to the color stack.
///
/// Returns a `ColorStackToken` that must be popped by calling `.pop()`
///
/// # Examples
///
/// ```no_run
/// # use arcdps_imgui::*;
/// # let mut ctx = Context::create();
/// # let ui = ctx.frame();
/// const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
/// let color = ui.push_style_color(StyleColor::Text, RED);
/// ui.text("I'm red!");
/// color.pop();
/// ```
#[doc(alias = "PushStyleColorVec4")]
pub fn push_style_color(
&self,
style_color: StyleColor,
color: [f32; 4],
) -> ColorStackToken<'_> {
unsafe { sys::igPushStyleColor_Vec4(style_color as i32, color.into()) };
ColorStackToken::new(self)
}
/// Changes style colors by pushing several changes to the color stack.
///
/// Returns a `ColorStackToken` that must be popped by calling `.pop()`
///
/// # Examples
///
/// ```no_run
/// # use arcdps_imgui::*;
/// # let mut ctx = Context::create();
/// # let ui = ctx.frame();
/// const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
/// const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
/// let colors = ui.push_style_colors(&[
/// (StyleColor::Text, RED),
/// (StyleColor::TextDisabled, GREEN),
/// ]);
/// ui.text("I'm red!");
/// ui.text_disabled("I'm green!");
/// colors.pop(&ui);
/// ```
#[deprecated = "deprecated in 0.7.0. Use `push_style_color` multiple times for similar effect."]
pub fn push_style_colors<'a, I>(&self, style_colors: I) -> MultiColorStackToken
where
I: IntoIterator<Item = &'a (StyleColor, [f32; 4])>,
{
let mut count = 0;
for &(style_color, color) in style_colors {
unsafe { sys::igPushStyleColor_Vec4(style_color as i32, color.into()) };
count += 1;
}
MultiColorStackToken {
count,
ctx: self.ctx,
}
}
/// Changes a style variable by pushing a change to the style stack.
///
/// Returns a `StyleStackToken` that can be popped by calling `.end()`
/// or by allowing to drop.
///
/// # Examples
///
/// ```no_run
/// # use arcdps_imgui::*;
/// # let mut ctx = Context::create();
/// # let ui = ctx.frame();
/// let style = ui.push_style_var(StyleVar::Alpha(0.2));
/// ui.text("I'm transparent!");
/// style.pop();
/// ```
#[doc(alias = "PushStyleVar")]
pub fn push_style_var(&self, style_var: StyleVar) -> StyleStackToken<'_> {
unsafe { push_style_var(style_var) };
StyleStackToken::new(self)
}
/// Changes style variables by pushing several changes to the style stack.
///
/// Returns a `StyleStackToken` that must be popped by calling `.pop()`
///
/// # Examples
///
/// ```no_run
/// # use arcdps_imgui::*;
/// # let mut ctx = Context::create();
/// # let ui = ctx.frame();
/// let styles = ui.push_style_vars(&[
/// StyleVar::Alpha(0.2),
/// StyleVar::ItemSpacing([50.0, 50.0])
/// ]);
/// ui.text("We're transparent...");
/// ui.text("...with large spacing as well");
/// styles.pop(&ui);
/// ```
#[deprecated = "deprecated in 0.7.0. Use `push_style_var` multiple times for similar effect."]
pub fn push_style_vars<'a, I>(&self, style_vars: I) -> MultiStyleStackToken
where
I: IntoIterator<Item = &'a StyleVar>,
{
let mut count = 0;
for &style_var in style_vars {
unsafe { push_style_var(style_var) };
count += 1;
}
MultiStyleStackToken {
count,
ctx: self.ctx,
}
}
}
create_token!(
/// Tracks a font pushed to the font stack that can be popped by calling `.end()`
/// or by dropping.
pub struct FontStackToken<'ui>;
/// Pops a change from the font stack.
drop { sys::igPopFont() }
);
impl FontStackToken<'_> {
/// Pops a change from the font stack.
pub fn pop(self) {
self.end()
}
}
create_token!(
/// Tracks a color pushed to the color stack that can be popped by calling `.end()`
/// or by dropping.
pub struct ColorStackToken<'ui>;
/// Pops a change from the color stack.
drop { sys::igPopStyleColor(1) }
);
impl ColorStackToken<'_> {
/// Pops a change from the color stack.
pub fn pop(self) {
self.end()
}
}
/// Tracks one or more changes pushed to the color stack that must be popped by calling `.pop()`
#[must_use]
pub struct MultiColorStackToken {
count: usize,
ctx: *const Context,
}
impl MultiColorStackToken {
/// Pops changes from the color stack
#[doc(alias = "PopStyleColor")]
pub fn pop(mut self, _: &Ui<'_>) {
self.ctx = ptr::null();
unsafe { sys::igPopStyleColor(self.count as i32) };
}
}
impl Drop for MultiColorStackToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A ColorStackToken was leaked. Did you call .pop()?");
}
}
}
create_token!(
/// Tracks a style pushed to the style stack that can be popped by calling `.end()`
/// or by dropping.
pub struct StyleStackToken<'ui>;
/// Pops a change from the style stack.
drop { sys::igPopStyleVar(1) }
);
impl StyleStackToken<'_> {
/// Pops a change from the style stack.
pub fn pop(self) {
self.end()
}
}
/// Tracks one or more changes pushed to the style stack that must be popped by calling `.pop()`
#[must_use]
pub struct MultiStyleStackToken {
count: usize,
ctx: *const Context,
}
impl MultiStyleStackToken {
/// Pops changes from the style stack
#[doc(alias = "PopStyleVar")]
pub fn pop(mut self, _: &Ui<'_>) {
self.ctx = ptr::null();
unsafe { sys::igPopStyleVar(self.count as i32) };
}
}
impl Drop for MultiStyleStackToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A StyleStackToken was leaked. Did you call .pop()?");
}
}
}
#[inline]
unsafe fn push_style_var(style_var: StyleVar) {
use crate::style::StyleVar::*;
use crate::sys::{igPushStyleVar_Float, igPushStyleVar_Vec2};
match style_var {
Alpha(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_Alpha as i32, v),
WindowPadding(v) => igPushStyleVar_Vec2(sys::ImGuiStyleVar_WindowPadding as i32, v.into()),
WindowRounding(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_WindowRounding as i32, v),
WindowBorderSize(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_WindowBorderSize as i32, v),
WindowMinSize(v) => igPushStyleVar_Vec2(sys::ImGuiStyleVar_WindowMinSize as i32, v.into()),
WindowTitleAlign(v) => {
igPushStyleVar_Vec2(sys::ImGuiStyleVar_WindowTitleAlign as i32, v.into())
}
ChildRounding(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_ChildRounding as i32, v),
ChildBorderSize(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_ChildBorderSize as i32, v),
PopupRounding(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_PopupRounding as i32, v),
PopupBorderSize(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_PopupBorderSize as i32, v),
FramePadding(v) => igPushStyleVar_Vec2(sys::ImGuiStyleVar_FramePadding as i32, v.into()),
FrameRounding(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_FrameRounding as i32, v),
FrameBorderSize(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_FrameBorderSize as i32, v),
ItemSpacing(v) => igPushStyleVar_Vec2(sys::ImGuiStyleVar_ItemSpacing as i32, v.into()),
ItemInnerSpacing(v) => {
igPushStyleVar_Vec2(sys::ImGuiStyleVar_ItemInnerSpacing as i32, v.into())
}
IndentSpacing(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_IndentSpacing as i32, v),
ScrollbarSize(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_ScrollbarSize as i32, v),
ScrollbarRounding(v) => {
igPushStyleVar_Float(sys::ImGuiStyleVar_ScrollbarRounding as i32, v)
}
GrabMinSize(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_GrabMinSize as i32, v),
GrabRounding(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_GrabRounding as i32, v),
TabRounding(v) => igPushStyleVar_Float(sys::ImGuiStyleVar_TabRounding as i32, v),
ButtonTextAlign(v) => {
igPushStyleVar_Vec2(sys::ImGuiStyleVar_ButtonTextAlign as i32, v.into())
}
SelectableTextAlign(v) => {
igPushStyleVar_Vec2(sys::ImGuiStyleVar_SelectableTextAlign as i32, v.into())
}
CellPadding(v) => igPushStyleVar_Vec2(sys::ImGuiStyleVar_CellPadding as i32, v.into())
}
}
/// # Parameter stacks (current window)
impl<'ui> Ui<'ui> {
/// Changes the item width by pushing a change to the item width stack.
///
/// Returns an `ItemWidthStackToken` that may be popped by calling `.pop()`
///
/// - `> 0.0`: width is `item_width` pixels
/// - `= 0.0`: default to ~2/3 of window width
/// - `< 0.0`: `item_width` pixels relative to the right of window (-1.0 always aligns width to
/// the right side)
#[doc(alias = "PushItemWith")]
pub fn push_item_width(&self, item_width: f32) -> ItemWidthStackToken {
unsafe { sys::igPushItemWidth(item_width) };
ItemWidthStackToken { _ctx: self.ctx }
}
/// Sets the width of the next item.
///
/// - `> 0.0`: width is `item_width` pixels
/// - `= 0.0`: default to ~2/3 of window width
/// - `< 0.0`: `item_width` pixels relative to the right of window (-1.0 always aligns width to
/// the right side)
#[doc(alias = "SetNextItemWidth")]
pub fn set_next_item_width(&self, item_width: f32) {
unsafe { sys::igSetNextItemWidth(item_width) };
}
/// Returns the width of the item given the pushed settings and the current cursor position.
///
/// This is NOT necessarily the width of last item.
#[doc(alias = "CalcItemWidth")]
pub fn calc_item_width(&self) -> f32 {
unsafe { sys::igCalcItemWidth() }
}
/// Changes the text wrapping position to the end of window (or column), which
/// is generally the default.
///
/// This is the same as calling [push_text_wrap_pos_with_pos](Self::push_text_wrap_pos_with_pos)
/// with `wrap_pos_x` set to 0.0.
///
/// Returns a `TextWrapPosStackToken` that may be popped by calling `.pop()`
#[doc(alias = "PushTextWrapPos")]
pub fn push_text_wrap_pos(&self) -> TextWrapPosStackToken {
self.push_text_wrap_pos_with_pos(0.0)
}
/// Changes the text wrapping position by pushing a change to the text wrapping position stack.
///
/// Returns a `TextWrapPosStackToken` that may be popped by calling `.pop()`
///
/// - `> 0.0`: wrap at `wrap_pos_x` position in window local space
/// - `= 0.0`: wrap to end of window (or column)
/// - `< 0.0`: no wrapping
#[doc(alias = "PushTextWrapPos")]
pub fn push_text_wrap_pos_with_pos(&self, wrap_pos_x: f32) -> TextWrapPosStackToken {
unsafe { sys::igPushTextWrapPos(wrap_pos_x) };
TextWrapPosStackToken { _ctx: self.ctx }
}
/// Changes an item flag by pushing a change to the item flag stack.
///
/// Returns a `ItemFlagsStackToken` that may be popped by calling `.pop()`
#[doc(alias = "PushItemFlag")]
pub fn push_item_flag(&self, item_flag: ItemFlag) -> ItemFlagsStackToken {
use self::ItemFlag::*;
match item_flag {
AllowKeyboardFocus(v) => unsafe { sys::igPushAllowKeyboardFocus(v) },
ButtonRepeat(v) => unsafe { sys::igPushButtonRepeat(v) },
}
ItemFlagsStackToken {
discriminant: mem::discriminant(&item_flag),
_ctx: self.ctx,
}
}
}
/// A temporary change in item flags
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ItemFlag {
AllowKeyboardFocus(bool),
ButtonRepeat(bool),
}
pub struct ItemWidthStackToken {
_ctx: *const Context,
}
impl ItemWidthStackToken {
/// Pops a change from the item width stack
#[doc(alias = "PopItemWidth")]
pub fn pop(mut self, _: &Ui<'_>) {
self._ctx = ptr::null();
unsafe { sys::igPopItemWidth() };
}
}
/// Tracks a change pushed to the text wrap position stack
pub struct TextWrapPosStackToken {
_ctx: *const Context,
}
impl TextWrapPosStackToken {
/// Pops a change from the text wrap position stack
#[doc(alias = "PopTextWrapPos")]
pub fn pop(mut self, _: &Ui<'_>) {
self._ctx = ptr::null();
unsafe { sys::igPopTextWrapPos() };
}
}
/// Tracks a change pushed to the item flags stack
pub struct ItemFlagsStackToken {
discriminant: mem::Discriminant<ItemFlag>,
_ctx: *const Context,
}
impl ItemFlagsStackToken {
/// Pops a change from the item flags stack
#[doc(alias = "PopAllowKeyboardFocus", alias = "PopButtonRepeat")]
pub fn pop(mut self, _: &Ui<'_>) {
self._ctx = ptr::null();
const ALLOW_KEYBOARD_FOCUS: ItemFlag = ItemFlag::AllowKeyboardFocus(true);
const BUTTON_REPEAT: ItemFlag = ItemFlag::ButtonRepeat(true);
if self.discriminant == mem::discriminant(&ALLOW_KEYBOARD_FOCUS) {
unsafe { sys::igPopAllowKeyboardFocus() };
} else if self.discriminant == mem::discriminant(&BUTTON_REPEAT) {
unsafe { sys::igPopButtonRepeat() };
} else {
unreachable!();
}
}
}
create_token!(
/// Tracks an ID pushed to the ID stack that can be popped by calling `.pop()`
/// or by dropping.
pub struct IdStackToken<'ui>;
/// Pops a change from the ID stack
drop { sys::igPopID() }
);
impl IdStackToken<'_> {
/// Pops a change from the ID stack
pub fn pop(self) {
self.end()
}
}
/// # ID stack
impl<'ui> Ui<'ui> {
/// Pushes an identifier to the ID stack.
///
/// Returns an `IdStackToken` that can be popped by calling `.end()`
/// or by dropping manually.
#[doc(alias = "PushId")]
pub fn push_id<'a, I: Into<Id<'a>>>(&self, id: I) -> IdStackToken<'ui> {
let id = id.into();
unsafe {
match id {
Id::Int(i) => sys::igPushID_Int(i),
Id::Str(s) => {
let start = s.as_ptr() as *const c_char;
let end = start.add(s.len());
sys::igPushID_StrStr(start, end)
}
Id::Ptr(p) => sys::igPushID_Ptr(p as *const c_void),
}
}
IdStackToken::new(self)
}
}