Skip to content

Commit

Permalink
Update buffer lib
Browse files Browse the repository at this point in the history
  • Loading branch information
emmabritton committed Dec 24, 2023
1 parent 79da7b2 commit b5eaefb
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 62 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### Version 0.14.1
- Add `images` feature
- Update buffer lib
- Remove shapes lib dep

### Version 0.14.0
- Add `controller_xinput` feature
- Remove `controller` from default feature
Expand Down
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pixels-graphics-lib"
version = "0.14.0"
version = "0.14.1"
edition = "2021"
authors = ["Emma Britton <emmabritton@pm.me>"]
description = "Simple wrapper library around Pixels/Buffer Graphics"
Expand All @@ -17,6 +17,7 @@ controller = ["serde", "simple-game-utils/controller"]
sound = ["simple-game-utils/sound"]
file_dialogs = ["directories"]
controller_xinput = ["serde", "simple-game-utils/controller_xinput"]
images = ["buffer-graphics-lib/image_loading"]

[dependencies]
pixels = "0.13.0"
Expand All @@ -25,12 +26,19 @@ winit_input_helper = "0.15.1"
thiserror = "1.0.51"
serde = { version = "1.0.193", features = ["derive"], optional = true }
directories = { version = "5.0.1", optional = true }
graphics-shapes = "0.2.4"
buffer-graphics-lib = { version = "0.12.4", features = ["image_loading", "ici", "serde_derive"] }
buffer-graphics-lib = { version ="0.12.5", default-features = false, features = ["image_loading", "ici", "serde_derive"] }
rustc-hash = "1.1.0"
simple-game-utils = { version = "0.4.0", default-features = false }
fnv = "1.0.7"

[dev-dependencies]
fastrand = "2.0.0"
anyhow = "1.0"

[[example]]
name = "test_dialogs"
required-features = ["file_dialogs"]

[[example]]
name = "test_dialogs_w_controllers"
required-features = ["file_dialogs", "controller"]
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is a simple wrapper around [Pixels](https://github.com/parasyte/pixels), de

In your `Cargo.toml` file add
```toml
pixels-graphics-lib = "0.14.0"
pixels-graphics-lib = "0.14.1"
winit_input_helper = "0.15.1" #only needed if you're not using `run()`
```

Expand Down Expand Up @@ -61,22 +61,22 @@ impl ExampleScene {
}

impl Scene<SceneResult, SceneName> for ExampleScene {
fn render(&mut self, graphics: &mut Graphics, mouse_xy: Coord) {
fn render(
&mut self,
graphics: &mut Graphics,
mouse_xy: Coord,
held_keys: &[VirtualKeyCode]) {
todo!()
}

fn update(
&mut self,
timing: &Timing,
mouse_xy: Coord,
held_keys: &Vec<&VirtualKeyCode>,
held_keys: &[VirtualKeyCode],
) -> SceneUpdateResult<SceneResult, SceneName> {
todo!()
}

fn resuming(&mut self, result: Option<SceneResult>) {
todo!()
}
}
```

Expand Down Expand Up @@ -105,6 +105,27 @@ Save and restore window position and size

To use this the `impl System` must override `System::window_prefs()`

### `controller`

* Adds gamepad support
* Adds gamepad state to `Scene::update`, `Scene::render`

### `controller_xinput`

As above but using xinput, windows only

### `sound`

Play music or sound effects

### `images`

Loading and displaying of PNGs, JPEGs, BMPs

### `file_dialogs`

Built in file selection dialogs, not recommended, use `rfd`

## Projects

### [Retro Games](https://github.com/emmabritton/retro-games)
Expand Down
25 changes: 25 additions & 0 deletions examples/test_dialogs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use pixels_graphics_lib::dialogs::save_file_dialog::SaveFileDialog;
use pixels_graphics_lib::prelude::FileDialogResults;
use pixels_graphics_lib::ui::styles::UiStyle;

// This example is used a test to make the dialogs have been correctly written

fn main() {
let _ = SaveFileDialog::<SR, SN>::new(None, None, 10, 10, &UiStyle::default().dialog);
}

#[derive(Clone, Debug, PartialEq)]
enum SR {}

#[derive(Clone, Debug, PartialEq)]
enum SN {}

impl FileDialogResults<SR> for SR {
fn save_file_result(_: String) -> SR {
todo!()
}

fn load_file_result(_: String) -> SR {
todo!()
}
}
25 changes: 25 additions & 0 deletions examples/test_dialogs_w_controllers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use pixels_graphics_lib::dialogs::save_file_dialog::SaveFileDialog;
use pixels_graphics_lib::dialogs::FileDialogResults;
use pixels_graphics_lib::ui::styles::UiStyle;

// This example is used a test to make the dialogs with controllers have been correctly written

fn main() {
let _ = SaveFileDialog::<SR, SN>::new(None, None, 10, 10, &UiStyle::default().dialog);
}

#[derive(Clone, Debug, PartialEq)]
enum SR {}

#[derive(Clone, Debug, PartialEq)]
enum SN {}

impl FileDialogResults<SR> for SR {
fn save_file_result(_: String) -> SR {
todo!()
}

fn load_file_result(_: String) -> SR {
todo!()
}
}
12 changes: 4 additions & 8 deletions examples/ui_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use anyhow::Result;
use buffer_graphics_lib::prelude::DrawType::Stroke;
use buffer_graphics_lib::prelude::Positioning::*;
use buffer_graphics_lib::prelude::*;
use pixels_graphics_lib::prelude::SceneUpdateResult::*;
use pixels_graphics_lib::prelude::SceneUpdateResult::Nothing;
use pixels_graphics_lib::prelude::*;
use pixels_graphics_lib::ui::prelude::TextFilter::{Raw, Sentence};
use pixels_graphics_lib::ui::prelude::TextFilter::*;
use pixels_graphics_lib::ui::prelude::*;
use pixels_graphics_lib::ui::text_field::TextFilter::All;
use pixels_graphics_lib::window_prefs::WindowPreferences;
use winit::keyboard::KeyCode;
use pixels_graphics_lib::*;

#[allow(clippy::upper_case_acronyms)]
type SUR = SceneUpdateResult<SceneResult, SceneName>;
Expand Down Expand Up @@ -63,7 +59,7 @@ impl Menu {
let (large_icon, _) =
IndexedImage::from_file_contents(include_bytes!("resources/large_icon.ici")).unwrap();
let title = Text::new("UI Tester", TextPos::Px(8, 8), style.title_text.clone());
let tooltip_rect = Drawable::from_obj(Rect::new_with_size((8, 40), 10, 10), Stroke(WHITE));
let tooltip_rect = Drawable::from_obj(Rect::new_with_size((8, 40), 10, 10), stroke(WHITE));
let tooltip = Tooltip::new((8, 40), "This is a test tooltip", LeftTop, &style.tooltip);
let mut button1 = Button::new((0, 0), "Test Button", None, &style.button);
let mut button2 = Button::new((0, 0), "Test Button", Some(100), &style.button);
Expand Down
43 changes: 35 additions & 8 deletions src/dialogs/load_file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::ui::prelude::TextFilter::*;
use crate::ui::prelude::*;
use crate::*;
use buffer_graphics_lib::prelude::*;
use directories::UserDirs;
use std::fmt::Debug;

/// You should use something like `rfd` instead of this
Expand Down Expand Up @@ -110,8 +111,7 @@ where
}
}

impl<SR: Clone + Debug + PartialEq, SN: Clone + Debug + PartialEq> Scene<SR, SN>
for LoadFileDialog<SR, SN>
impl<SR: Clone + Debug + PartialEq, SN: Clone + Debug + PartialEq> LoadFileDialog<SR, SN>
where
SR: FileDialogResults<SR>,
{
Expand All @@ -127,7 +127,28 @@ where
self.home.render(graphics, mouse_xy);
}

fn on_key_up(&mut self, key: KeyCode, _: Coord, held_keys: &Vec<&KeyCode>) {
fn update(&mut self, timing: &Timing) -> SceneUpdateResult<SR, SN> {
self.current_dir_field.update(timing);
self.result.clone()
}
}

impl<SR: Clone + Debug + PartialEq, SN: Clone + Debug + PartialEq> Scene<SR, SN>
for LoadFileDialog<SR, SN>
where
SR: FileDialogResults<SR>,
{
#[cfg(any(feature = "controller", feature = "controller_xinput"))]
fn render(&self, graphics: &mut Graphics, mouse_xy: Coord, _: &[KeyCode], _: &GameController) {
self.render(graphics, mouse_xy)
}

#[cfg(not(any(feature = "controller", feature = "controller_xinput")))]
fn render(&self, graphics: &mut Graphics, mouse_xy: Coord, _: &[KeyCode]) {
self.render(graphics, mouse_xy)
}

fn on_key_up(&mut self, key: KeyCode, _: Coord, held_keys: &[KeyCode]) {
if self.current_dir_field.is_focused() {
if key == KeyCode::KeyV {
if held_keys.contains(&&KeyCode::ControlRight) {}
Expand All @@ -138,7 +159,7 @@ where
}
}

fn on_mouse_up(&mut self, xy: Coord, button: MouseButton, _: &Vec<&KeyCode>) {
fn on_mouse_up(&mut self, xy: Coord, button: MouseButton, _: &[KeyCode]) {
if button != MouseButton::Left {
return;
}
Expand Down Expand Up @@ -189,18 +210,24 @@ where
}
}

fn on_scroll(&mut self, xy: Coord, _: isize, y_diff: isize, _: &Vec<&KeyCode>) {
fn on_scroll(&mut self, xy: Coord, _: isize, y_diff: isize, _: &[KeyCode]) {
self.dir_panel.on_scroll(xy, y_diff);
}

#[cfg(any(feature = "controller", feature = "controller_xinput"))]
fn update(
&mut self,
timing: &Timing,
_: Coord,
_: &Vec<&KeyCode>,
_: &[KeyCode],
_: &GameController,
) -> SceneUpdateResult<SR, SN> {
self.current_dir_field.update(timing);
self.result.clone()
self.update(timing)
}

#[cfg(not(any(feature = "controller", feature = "controller_xinput")))]
fn update(&mut self, timing: &Timing, _: Coord, _: &[KeyCode]) -> SceneUpdateResult<SR, SN> {
self.update(timing)
}

fn resuming(&mut self, _: Option<SR>) {}
Expand Down
1 change: 0 additions & 1 deletion src/dialogs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::ui::prelude::*;
use buffer_graphics_lib::prelude::*;
use graphics_shapes::coord;
use std::fmt::Debug;

#[cfg(feature = "directories")]
Expand Down
44 changes: 35 additions & 9 deletions src/dialogs/save_file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ where
}
}

impl<SR: Clone + Debug + PartialEq, SN: Clone + Debug + PartialEq> Scene<SR, SN>
for SaveFileDialog<SR, SN>
impl<SR: Clone + Debug + PartialEq, SN: Clone + Debug + PartialEq> SaveFileDialog<SR, SN>
where
SR: FileDialogResults<SR>,
{
Expand All @@ -151,12 +150,34 @@ where
self.cancel.render(graphics, mouse_xy);
}

fn on_key_up(&mut self, key: KeyCode, _: Coord, held_keys: &Vec<&KeyCode>) {
fn update(&mut self, timing: &Timing) -> SceneUpdateResult<SR, SN> {
self.name_field.update(timing);
self.current_dir_field.update(timing);
self.result.clone()
}
}

impl<SR: Clone + Debug + PartialEq, SN: Clone + Debug + PartialEq> Scene<SR, SN>
for SaveFileDialog<SR, SN>
where
SR: FileDialogResults<SR>,
{
#[cfg(any(feature = "controller", feature = "controller_xinput"))]
fn render(&self, graphics: &mut Graphics, mouse_xy: Coord, _: &[KeyCode], _: &GameController) {
self.render(graphics, mouse_xy)
}

#[cfg(not(any(feature = "controller", feature = "controller_xinput")))]
fn render(&self, graphics: &mut Graphics, mouse_xy: Coord, _: &[KeyCode]) {
self.render(graphics, mouse_xy)
}

fn on_key_up(&mut self, key: KeyCode, _: Coord, held_keys: &[KeyCode]) {
self.name_field.on_key_press(key, held_keys);
self.current_dir_field.on_key_press(key, held_keys);
}

fn on_mouse_up(&mut self, xy: Coord, button: MouseButton, _: &Vec<&KeyCode>) {
fn on_mouse_up(&mut self, xy: Coord, button: MouseButton, _: &[KeyCode]) {
if button != MouseButton::Left {
return;
}
Expand Down Expand Up @@ -217,19 +238,24 @@ where
}
}

fn on_scroll(&mut self, xy: Coord, _: isize, y_diff: isize, _: &Vec<&KeyCode>) {
fn on_scroll(&mut self, xy: Coord, _: isize, y_diff: isize, _: &[KeyCode]) {
self.dir_panel.on_scroll(xy, y_diff);
}

#[cfg(any(feature = "controller", feature = "controller_xinput"))]
fn update(
&mut self,
timing: &Timing,
_: Coord,
_: &Vec<&KeyCode>,
_: &[KeyCode],
_: &GameController,
) -> SceneUpdateResult<SR, SN> {
self.name_field.update(timing);
self.current_dir_field.update(timing);
self.result.clone()
self.update(timing)
}

#[cfg(not(any(feature = "controller", feature = "controller_xinput")))]
fn update(&mut self, timing: &Timing, _: Coord, _: &[KeyCode]) -> SceneUpdateResult<SR, SN> {
self.update(timing)
}

fn resuming(&mut self, _: Option<SR>) {}
Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use crate::window_prefs::WindowPreferences;
use crate::GraphicsError::LoadingWindowPref;
pub use buffer_graphics_lib;
use buffer_graphics_lib::Graphics;
pub use graphics_shapes;
use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
use simple_game_utils::prelude::*;
use thiserror::Error;
Expand All @@ -59,10 +58,17 @@ use winit_input_helper::WinitInputHelper;

pub mod prelude {
pub use crate::dialogs::*;
pub use crate::run;
pub use crate::scenes::*;
pub use crate::setup;
pub use crate::utilities::virtual_key_codes::*;
pub use crate::window_prefs::*;
pub use crate::*;
pub use crate::GraphicsError;
pub use crate::MouseButton;
pub use crate::Options;
pub use crate::System;
pub use crate::WindowScaling;
pub use buffer_graphics_lib::prelude::*;
pub use simple_game_utils::prelude::*;
pub use winit::keyboard::KeyCode;
}
Expand Down
1 change: 0 additions & 1 deletion src/ui/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::ui::prelude::*;
use crate::ui::styles::AlertStyle;
use buffer_graphics_lib::prelude::Positioning::Center;
use buffer_graphics_lib::prelude::*;
use graphics_shapes::coord;

const BUTTON_Y: isize = 28;
const ALERT_SIZE: (usize, usize) = (200, 50);
Expand Down
Loading

0 comments on commit b5eaefb

Please sign in to comment.