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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
//! Settings methods for the [`Engine`].
//!
//! Methods for reading and setting various engine configuration values.
//!
//! Provided types:
//!
//! - [`DrawMode`]: Determines how `(x, y)` coordinates are used for rendering.
//! - [`RectMode`]: Alias for `DrawMode`.
//! - [`EllipseMode`]: Alias for `DrawMode`.
//! - [`ImageMode`]: Alias for `DrawMode`.
//! - [`ArcMode`]: Determines how arcs are rendered.
//! - [`BlendMode`]: Determines how images and textures are blended.
//! - [`AngleMode`]: Determines how angles are interpreted.
//! - [`FontStyle`]: Determines how text is rendered.
//!
//! Provided [`PixState`] methods:
//!
//! - [`PixState::background`]: Sets the [Color] used by [`PixState::clear`] to clear the canvas.
//! - [`PixState::fill`]: Sets the [Color] used to fill shapes.
//! - [`PixState::stroke`]: Sets the [Color] used to stroke shapes and text.
//! - [`PixState::stroke_weight`]: Sets the stroke line thickness for lines and text.
//! - [`PixState::text_shadow`]: Sets the shadow distance for drawing text.
//! - [`PixState::smooth`]: Enables the anti-alias smoothing option for drawing shapes.
//! - [`PixState::bezier_detail`]: Set the resolution at which Bezier curves are dispalyed.
//! - [`PixState::wrap`]: Sets the wrap width for rendering text.
//! - [`PixState::clip`]: Sets a clip rectangle for rendering.
//! - [`PixState::fullscreen`]: Sets fullscreen mode to enabled or disabled.
//! - [`PixState::toggle_fullscreen`]: Toggles fullscreen.
//! - [`PixState::vsync`]: Sets vertical sync mode to enabled or disabled.
//! - [`PixState::toggle_vsync`]: Toggles vertical sync.
//! - [`PixState::cursor`]: Set a custom window cursor or hide the cursor.
//! - [`PixState::disable`]: Disable UI elements from being interactive.
//! - [`PixState::running`]: Whether the render loop is running (calling [`PixEngine::on_update`]).
//! - [`PixState::run`]: Enable or disable the render loop.
//! - [`PixState::show_frame_rate`]: Display the average frame rate in the title bar.
//! - [`PixState::target_frame_rate`]: Return the current targeted frame rate.
//! - [`PixState::frame_rate`]: Set or clear a targeted frame rate.
//! - [`PixState::scale`]: Set the rendering scale of the current canvas.
//! - [`PixState::rect_mode`]: Change the [`RectMode`] for rendering rectangles.
//! - [`PixState::ellipse_mode`]: Change the [`EllipseMode`] for rendering ellipses.
//! - [`PixState::image_mode`]: Change the [`ImageMode`] for rendering images.
//! - [`PixState::image_tint`]: Set or clear a [Color] used to tint [Image]s.
//! - [`PixState::arc_mode`]: Change the [`ArcMode`] for rendering arcs.
//! - [`PixState::angle_mode`]: Change the [`AngleMode`] for angle interpretation.
//! - [`PixState::blend_mode`]: Change the [`BlendMode`] for rendering images and textures.
//! - [`PixState::push`]: Push a copy of all the current settings to a stack.
//! - [`PixState::pop`]: Pop the previously pushed settings off the stack, restoring them.
use crate::{
prelude::*,
renderer::{Rendering, WindowRenderer},
};
use bitflags::bitflags;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::time::Duration;
/// Drawing mode which changes how `(x, y)` coordinates are interpreted.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DrawMode {
/// Use `(x, y)` as the top-left corner. Default.
Corner,
/// Use `(x, y)` as the center.
Center,
}
/// Drawing mode which determines how `(x, y)` coordinates are interpreted when drawing [Rect]s.
pub type RectMode = DrawMode;
/// Drawing mode which determines how `(x, y)` coordinates are interpreted when drawing [Ellipse]s.
pub type EllipseMode = DrawMode;
/// Drawing mode which determines how `(x, y)` coordinates are interpreted when drawing [Image]s.
pub type ImageMode = DrawMode;
/// Drawing mode which determines how arcs are drawn.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ArcMode {
/// Draws arc as an open, unfilled pie segment using `stroke` color.
Default,
/// Draws arc as a closed pie segment using `stroke` and `fill` colors.
Pie,
}
/// Drawing mode which determines how textures are blended together.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BlendMode {
/// Disable blending.
None,
/// Alpha blending.
Blend,
/// Additive blending.
Add,
/// Color modulate.
Mod,
}
/// Determines how angles are interpreted.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AngleMode {
/// Radians.
Radians,
/// Degrees.
Degrees,
}
bitflags! {
/// Font style for drawing text.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct FontStyle: i32 {
/// Normal.
const NORMAL = 0x00;
/// Bold.
const BOLD = 0x01;
/// Italic.
const ITALIC = 0x02;
/// Underline
const UNDERLINE = 0x04;
/// Strike-through
const STRIKETHROUGH = 0x08;
}
}
/// Several settings used to change various functionality of the engine.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub(crate) struct Settings {
pub(crate) background: Color,
pub(crate) fill: Option<Color>,
pub(crate) stroke: Option<Color>,
pub(crate) stroke_weight: u16,
pub(crate) font_size: u32,
pub(crate) font_style: FontStyle,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) font_family: Font,
pub(crate) text_shadow: Option<u16>,
pub(crate) smooth: bool,
pub(crate) bezier_detail: i32,
pub(crate) wrap_width: Option<u32>,
pub(crate) clip: Option<Rect<i32>>,
pub(crate) running: bool,
pub(crate) show_frame_rate: bool,
pub(crate) target_frame_rate: Option<usize>,
pub(crate) target_delta_time: Option<Duration>,
pub(crate) scale_x: f32,
pub(crate) scale_y: f32,
pub(crate) rect_mode: RectMode,
pub(crate) ellipse_mode: EllipseMode,
pub(crate) image_mode: ImageMode,
pub(crate) image_tint: Option<Color>,
pub(crate) arc_mode: ArcMode,
pub(crate) angle_mode: AngleMode,
pub(crate) blend_mode: BlendMode,
pub(crate) cursor: Option<Cursor>,
pub(crate) disabled: bool,
}
impl Default for Settings {
fn default() -> Self {
Self {
background: Color::BLACK,
fill: Some(Color::WHITE),
stroke: None,
stroke_weight: 1,
font_size: 14,
font_style: FontStyle::NORMAL,
font_family: Font::default(),
text_shadow: None,
smooth: true,
bezier_detail: 20,
wrap_width: None,
clip: None,
running: true,
show_frame_rate: false,
target_frame_rate: None,
target_delta_time: None,
scale_x: 1.0,
scale_y: 1.0,
rect_mode: RectMode::Corner,
ellipse_mode: EllipseMode::Center,
image_mode: ImageMode::Corner,
image_tint: None,
arc_mode: ArcMode::Default,
angle_mode: AngleMode::Radians,
blend_mode: BlendMode::None,
cursor: Some(Cursor::default()),
disabled: false,
}
}
}
impl PixState {
/// Sets the [Color] value used to clear the canvas.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.background(Color::ALICE_BLUE);
/// s.clear();
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn background<C>(&mut self, color: C)
where
C: Into<Color>,
{
self.settings.background = color.into();
let _result = self.clear(); // If this errors, something is very wrong
}
/// Sets the [Color] value used to fill shapes drawn on the canvas. `None` disables fill
/// entirely.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.fill(Color::ALICE_BLUE);
/// s.rect([0, 0, 100, 100])?;
/// s.fill((None));
/// s.rect([25, 25, 75, 75])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn fill<C>(&mut self, color: C)
where
C: Into<Option<Color>>,
{
self.settings.fill = color.into();
}
/// Sets the [Color] value used to outline shapes drawn on the canvas. `None` disables stroke
/// entirely.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.stroke(Color::BLACK);
/// s.rect([0, 0, 100, 100])?;
/// s.stroke((None));
/// s.rect([25, 25, 75, 75])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn stroke<C>(&mut self, color: C)
where
C: Into<Option<Color>>,
{
self.settings.stroke = color.into();
}
/// Sets the width used to draw lines on the canvas.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.stroke(Color::BLUE);
/// s.stroke_weight(2);
/// // Draws a 2-pixel wide diagonal line
/// s.line(line_![0, 0, 100, 100])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn stroke_weight(&mut self, weight: u16) {
self.settings.stroke_weight = weight;
}
/// Set the font size for drawing to the current canvas.
///
/// # Errors
///
/// If the renderer fails to load the given font size from the currently loaded font data, then
/// an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.font_size(22);
/// s.text("Some big text")?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn font_size(&mut self, size: u32) -> PixResult<()> {
self.settings.font_size = size;
self.theme.font_size = size;
self.renderer.font_size(size)
}
/// Set the font style for drawing to the current canvas.
///
/// # Errors
///
/// If the renderer fails to load the current font, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.font_style(FontStyle::BOLD);
/// s.text("Some bold text")?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn font_style(&mut self, style: FontStyle) {
self.settings.font_style = style;
self.renderer.font_style(style);
}
/// Set the font family for drawing to the current canvas.
///
/// # Errors
///
/// If the renderer fails to load the given font size from the currently loaded font data, then
/// an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.font_family(Font::NOTO)?;
/// s.text("Some NOTO family text")?;
/// s.font_family(Font::from_file("Custom font", "./custom_font.ttf"))?;
/// s.text("Some custom family text")?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn font_family(&mut self, font: Font) -> PixResult<()> {
self.settings.font_family = font;
self.renderer.font_family(&self.settings.font_family)
}
/// Sets the text shadow distance used to draw text on the canvas.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.text_shadow(2);
/// // Draws a 2-pixel offset shhadow
/// s.text("Shadowed")?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn text_shadow<D>(&mut self, distance: D)
where
D: Into<Option<u16>>,
{
self.settings.text_shadow = distance.into();
}
/// Enable or disable the anti-alias option used for drawing shapes on the canvas. `smooth` is
/// enabled by default.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// // Draws a anti-aliased diagonal line
/// s.smooth(true);
/// s.line(line_![0, 0, 100, 100])?;
/// // Disables anti-aliasing
/// s.smooth(false);
/// s.line(line_![0, 0, 100, 100])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn smooth(&mut self, val: bool) {
self.settings.smooth = val;
}
/// Set the resolution at which [`PixState::bezier`] curves are displayed. The default is `20`.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.bezier_detail(5);
/// s.stroke(Color::RED);
/// s.bezier([[85, 20], [10, 10], [90, 90], [15, 80]])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn bezier_detail(&mut self, detail: i32) {
self.settings.bezier_detail = detail;
}
/// Sets the wrap width used to draw text on the canvas. `None` disables text wrap.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// // Renders as (depending on font width):
/// //
/// // Lorem ipsum
/// // dollor sit amet,
/// // consetetur
/// // sadipscing
/// // elitr, sed diam
/// s.wrap(100);
/// s.text("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam")?;
///
/// // Disable wrapping
/// s.wrap((None));
/// s.text("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam")?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn wrap<W>(&mut self, width: W)
where
W: Into<Option<u32>>,
{
self.settings.wrap_width = width.into();
}
/// Sets the clip [Rect] used by the renderer to draw to the current canvas. `None` disables
/// clipping.
///
/// # Errors
///
/// If the current render target is closed or dropped, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.clip(rect![0, 0, 100, 100])?;
/// // Renders a quarter pie-slice with radius 100
/// s.circle([100, 100, 200, 200])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn clip<R>(&mut self, rect: R) -> PixResult<()>
where
R: Into<Option<Rect<i32>>>,
{
self.settings.clip = rect.into();
self.renderer.clip(self.settings.clip)
}
/// Set the application to fullscreen or not.
///
/// # Errors
///
/// If the current render target is closed or dropped or the renderer fails to set
/// fullscreen, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// s.fullscreen(true)?;
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn fullscreen(&mut self, val: bool) -> PixResult<()> {
self.renderer.set_fullscreen(val)
}
/// Toggle fullscreen.
///
/// # Errors
///
/// If the current render target is closed or dropped or the renderer fails to toggle
/// fullscreen, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// s.toggle_fullscreen()?;
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn toggle_fullscreen(&mut self) -> PixResult<()> {
let is_fullscreen = self.renderer.fullscreen()?;
self.renderer.set_fullscreen(!is_fullscreen)
}
/// Set the window to synchronize frame rate to the screens refresh rate ([`VSync`]).
///
/// # Note
///
/// Due to the current limitation with changing `VSync` at runtime, this method creates a new
/// window using the properties of the current window and returns the new `WindowId`.
///
/// If you are storing and interacting with this window using the `WindowId`, make sure to
/// use the newly returned `WindowId`.
///
/// [`VSync`]: https://en.wikipedia.org/wiki/Screen_tearing#Vertical_synchronization
///
/// # Errors
///
/// If the current render target is closed or dropped or the renderer fails to set
/// vsync, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// s.vsync(true)?;
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn vsync(&mut self, val: bool) -> PixResult<WindowId> {
self.renderer.set_vsync(val)
}
/// Toggle synchronizing frame rate to the screens refresh rate ([`VSync`]).
///
/// # Note
///
/// Due to the current limitation with changing `VSync` at runtime, this method creates a new
/// window using the properties of the current window and returns the new `WindowId`.
///
/// If you are storing and interacting with this window using the `WindowId`, make sure to
/// use the newly returned `WindowId`.
///
/// [`VSync`]: https://en.wikipedia.org/wiki/Screen_tearing#Vertical_synchronization
///
/// # Errors
///
/// If the current render target is closed or dropped or the renderer fails to toggle
/// vsync, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// s.toggle_vsync()?;
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn toggle_vsync(&mut self) -> PixResult<WindowId> {
let vsync_enabled = self.renderer.vsync();
self.renderer.set_vsync(vsync_enabled)
}
/// Set the mouse cursor to a predefined symbol or image. `None` hides the cursor.
///
/// # Errors
///
/// If the renderer fails to set the cursor or load it from an image file, then an error is
/// returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.text("Hover me")?;
/// if s.hovered() {
/// s.cursor(Cursor::hand())?;
/// } else {
/// s.cursor(Cursor::arrow())?;
/// }
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn cursor<C>(&mut self, cursor: C) -> PixResult<()>
where
C: Into<Option<Cursor>>,
{
self.settings.cursor = cursor.into();
self.renderer.cursor(self.settings.cursor.as_ref())
}
/// Disables any UI elements drawn after this is called, preventing them from being interacted
/// with.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App { checkbox: bool };
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// if s.button("Disable UI")? {
/// s.disable(true);
/// }
/// s.checkbox("Disabled checkbox", &mut self.checkbox)?;
/// Ok(())
/// }
/// # }
/// ```
pub fn disable(&mut self, disabled: bool) {
self.settings.disabled = disabled;
self.ui.disabled = disabled;
}
/// Whether the render loop is running or not.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// // Toggle pausing rendering
/// let running = s.running();
/// s.run(!running);
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
#[must_use]
pub fn running(&mut self) -> bool {
self.settings.running
}
/// Pause or resume the render loop called by [`PixEngine::on_update`].
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// // Toggle rendering
/// let running = s.running();
/// s.run(running);
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn run(&mut self, val: bool) {
self.settings.running = val;
}
/// Set whether to show the current frame rate per second in the title or not.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Return = event.key {
/// s.show_frame_rate(true);
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn show_frame_rate(&mut self, show: bool) {
self.settings.show_frame_rate = show;
}
/// Get the target frame rate to render at.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Down = event.key {
/// let target = s.target_frame_rate().unwrap_or(60);
/// s.frame_rate(target - 10);
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
#[must_use]
pub fn target_frame_rate(&mut self) -> Option<usize> {
self.settings.target_frame_rate
}
/// Set a target frame rate to render at, controls how often [`PixEngine::on_update`] is
/// called. `None` clears the target frame rate.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_start(&mut self, s: &mut PixState) -> PixResult<()> {
/// // Target a lower FPS than natively possible
/// s.frame_rate(30);
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn frame_rate<R>(&mut self, frame_rate: R)
where
R: Into<Option<usize>>,
{
let frame_rate = frame_rate.into();
self.settings.target_frame_rate = frame_rate;
self.settings.target_delta_time =
frame_rate.map(|frame_rate| Duration::from_secs(1) / frame_rate as u32);
}
/// Set the rendering scale of the current canvas. Drawing coordinates are scaled by x/y
/// factors before being drawn to the canvas.
///
/// # Errors
///
/// If the current render target is closed or dropped, or `(x, y`) contain invalid values,
/// then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// # fn on_update(&mut self, s: &mut PixState) -> PixResult<()> { Ok(()) }
/// fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
/// if let Key::Plus = event.key {
/// s.scale(2.0, 2.0)?;
/// return Ok(true);
/// }
/// Ok(false)
/// }
/// # }
/// ```
#[inline]
pub fn scale(&mut self, x: f32, y: f32) -> PixResult<()> {
let s = &mut self.settings;
s.scale_x = x;
s.scale_y = y;
self.renderer.scale(s.scale_x, s.scale_y)
}
/// Change the way parameters are interpreted for drawing [Square](Rect)s and
/// [Rectangle](Rect)s.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.rect_mode(RectMode::Center);
/// // Draw rect with center at `(100, 100)`
/// s.rect([100, 100, 50, 50])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn rect_mode(&mut self, mode: RectMode) {
self.settings.rect_mode = mode;
}
/// Change the way parameters are interpreted for drawing [Ellipse]s.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.ellipse_mode(EllipseMode::Center);
/// // Draw ellipse with center at `(100, 100)`
/// s.ellipse([100, 100, 50, 50])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn ellipse_mode(&mut self, mode: EllipseMode) {
self.settings.ellipse_mode = mode;
}
/// Change the way parameters are interpreted for drawing [Image]s.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.image_mode(ImageMode::Center);
/// // Draw image with center at `(100, 100)`
/// s.image(&Image::from_file("./some_image.png")?, [100, 100])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn image_mode(&mut self, mode: ImageMode) {
self.settings.image_mode = mode;
}
/// Add a color tint to [Image]s when drawing.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.image_tint(Color::RED);
/// // Draw image tinted red
/// s.image(&Image::from_file("./some_image.png")?, [0, 0])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn image_tint<C>(&mut self, tint: C)
where
C: Into<Option<Color>>,
{
self.settings.image_tint = tint.into();
}
/// Change the way arcs are drawn.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// // Draw arc as a open, unfilled pie segment using only the `stroke` (The default)
/// s.arc_mode(ArcMode::Default);
/// s.arc([100, 100], 20, 0, 180)?;
/// s.arc_mode(ArcMode::Pie);
/// // Draw arc as a closed pie segment using both `fill` and `stroke`
/// s.arc([200, 200], 20, 0, 180)?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn arc_mode(&mut self, mode: ArcMode) {
self.settings.arc_mode = mode;
}
/// Change the way angles are interpreted for rotation and matrix transformations.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.angle_mode(AngleMode::Degrees);
/// let angle = 10.0;
/// let center = point!(10, 10);
/// s.text_transformed("Rotated text", angle, center, None)?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn angle_mode(&mut self, mode: AngleMode) {
self.settings.angle_mode = mode;
}
/// Change the way textures are blended together.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.blend_mode(BlendMode::Blend);
/// // Draw image with alpha blended with background
/// s.image(&Image::from_file("./some_image.png")?, [0, 0])?;
/// Ok(())
/// }
/// # }
/// ```
#[inline]
pub fn blend_mode(&mut self, mode: BlendMode) {
self.settings.blend_mode = mode;
self.renderer.blend_mode(mode);
}
/// Saves the current draw settings and transforms.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.fill(Color::BLUE);
/// s.stroke(Color::WHITE);
///
/// s.push(); // Save settings
///
/// s.fill(Color::RED);
/// s.stroke(Color::BLACK);
/// s.rect([0, 0, 100, 100])?;
///
/// s.pop(); // Restore settings
///
/// // Rectangle is blue with a white outline
/// s.rect([0, 0, 100, 100])?;
/// Ok(())
/// }
/// # }
#[inline]
pub fn push(&mut self) {
self.setting_stack.push(self.settings.clone());
}
/// Restores the previous draw settings and transforms, if present. If the settings stack is
/// empty, the settings will remain unchanged.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App;
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.fill(Color::BLUE);
/// s.stroke(Color::WHITE);
///
/// s.push(); // Save settings
///
/// s.fill(Color::RED);
/// s.stroke(Color::BLACK);
/// s.rect([0, 0, 100, 100])?;
///
/// s.pop(); // Restore settings
///
/// // Rectangle is blue with a white outline
/// s.rect([0, 0, 100, 100])?;
/// Ok(())
/// }
/// # }
#[inline]
pub fn pop(&mut self) {
if let Some(settings) = self.setting_stack.pop() {
self.settings = settings;
self.ui.disabled = self.settings.disabled;
}
let s = &self.settings;
// All of these settings should be valid since they were set prior to `pop()` being
// called.
let _ = self.renderer.clip(s.clip);
// Excluding restoring cursor - as it's used for mouse hover.
let _ = self.renderer.font_size(s.font_size);
self.renderer.font_style(s.font_style);
let _ = self.renderer.font_family(&s.font_family);
self.renderer.blend_mode(s.blend_mode);
}
}
impl PixState {
/// Set the mouse cursor to a predefined symbol or image for a single frame.
///
/// Cursor will get reset to the current setting next frame.
#[inline]
pub(crate) fn frame_cursor(&mut self, cursor: &Cursor) -> PixResult<()> {
self.renderer.cursor(Some(cursor))
}
/// Get the target delta time between frames.
#[inline]
pub(crate) fn target_delta_time(&self) -> Option<Duration> {
self.settings.target_delta_time
}
/// Get whether `VSync` is enabled.
#[inline]
pub(crate) fn vsync_enabled(&self) -> bool {
self.renderer.vsync()
}
}