Skip to content

Commit

Permalink
#1301 Start with image support
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Nov 4, 2024
1 parent 9cba63c commit a473ed3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
26 changes: 15 additions & 11 deletions api/src/persistence/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,48 +325,52 @@ pub struct StreamDeckButtonDesign {
#[derive(Clone, Eq, PartialEq, Hash, Debug, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum StreamDeckButtonForeground {
Solid(StreamDeckButtonSolidForeground),
Image(StreamDeckButtonImageForeground),
Bar(StreamDeckButtonBarForeground),
FadingColor(StreamDeckButtonFadingColorForeground),
FadingImage(StreamDeckButtonFadingImageForeground),
FullBar(StreamDeckButtonFullBarForeground),
Arc(StreamDeckButtonArcForeground),
}

impl Default for StreamDeckButtonForeground {
fn default() -> Self {
Self::Solid(StreamDeckButtonSolidForeground::default())
Self::FadingColor(StreamDeckButtonFadingColorForeground::default())
}
}

#[derive(Clone, Eq, PartialEq, Hash, Debug, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum StreamDeckButtonBackground {
Solid(StreamDeckButtonSolidBackground),
Color(StreamDeckButtonColorBackground),
Image(StreamDeckButtonImageBackground),
}

impl Default for StreamDeckButtonBackground {
fn default() -> Self {
Self::Solid(StreamDeckButtonSolidBackground::default())
Self::Color(StreamDeckButtonColorBackground::default())
}
}

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, Serialize, Deserialize)]
pub struct StreamDeckButtonImageForeground {}
pub struct StreamDeckButtonFadingImageForeground {
pub path: String,
}

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, Serialize, Deserialize)]
pub struct StreamDeckButtonSolidForeground {}
pub struct StreamDeckButtonFadingColorForeground {}

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, Serialize, Deserialize)]
pub struct StreamDeckButtonBarForeground {}
pub struct StreamDeckButtonFullBarForeground {}

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, Serialize, Deserialize)]
pub struct StreamDeckButtonArcForeground {}

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, Serialize, Deserialize)]
pub struct StreamDeckButtonImageBackground {}
pub struct StreamDeckButtonImageBackground {
pub path: String,
}

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, Serialize, Deserialize)]
pub struct StreamDeckButtonSolidBackground {}
pub struct StreamDeckButtonColorBackground {}

#[derive(Copy, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Keystroke {
Expand Down
16 changes: 8 additions & 8 deletions main/src/application/source_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,21 +744,21 @@ impl SourceModel {
StreamDeckButtonDesign {
background: match self.button_background_type {
StreamDeckButtonBackgroundType::Solid => {
StreamDeckButtonBackground::Solid(Default::default())
StreamDeckButtonBackground::Color(Default::default())
}
StreamDeckButtonBackgroundType::Image => {
StreamDeckButtonBackground::Image(Default::default())
}
},
foreground: match self.button_foreground_type {
StreamDeckButtonForegroundType::Solid => {
StreamDeckButtonForeground::Solid(Default::default())
StreamDeckButtonForeground::FadingColor(Default::default())
}
StreamDeckButtonForegroundType::Image => {
StreamDeckButtonForeground::Image(Default::default())
StreamDeckButtonForeground::FadingImage(Default::default())
}
StreamDeckButtonForegroundType::Bar => {
StreamDeckButtonForeground::Bar(Default::default())
StreamDeckButtonForeground::FullBar(Default::default())
}
StreamDeckButtonForegroundType::Arc => {
StreamDeckButtonForeground::Arc(Default::default())
Expand Down Expand Up @@ -1322,7 +1322,7 @@ pub enum StreamDeckButtonBackgroundType {
impl From<&StreamDeckButtonBackground> for StreamDeckButtonBackgroundType {
fn from(value: &StreamDeckButtonBackground) -> Self {
match value {
StreamDeckButtonBackground::Solid(_) => Self::Solid,
StreamDeckButtonBackground::Color(_) => Self::Solid,
StreamDeckButtonBackground::Image(_) => Self::Image,
}
}
Expand All @@ -1343,10 +1343,10 @@ pub enum StreamDeckButtonForegroundType {
impl From<&StreamDeckButtonForeground> for StreamDeckButtonForegroundType {
fn from(value: &StreamDeckButtonForeground) -> Self {
match value {
StreamDeckButtonForeground::Solid(_) => Self::Solid,
StreamDeckButtonForeground::Image(_) => Self::Image,
StreamDeckButtonForeground::FadingColor(_) => Self::Solid,
StreamDeckButtonForeground::FadingImage(_) => Self::Image,
StreamDeckButtonForeground::Arc(_) => Self::Arc,
StreamDeckButtonForeground::Bar(_) => Self::Bar,
StreamDeckButtonForeground::FullBar(_) => Self::Bar,
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions main/src/domain/backbone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,25 @@ impl Backbone {
// Paint background
let bg_color = value.background_color.unwrap_or(DEFAULT_BG_COLOR);
let mut img: ImageBuffer<Rgba<u8>, _> = match value.button_design.background {
StreamDeckButtonBackground::Solid(_) => {
StreamDeckButtonBackground::Color(_) => {
ImageBuffer::from_pixel(width, height, bg_color.into())
}
StreamDeckButtonBackground::Image(_) => ImageBuffer::default(),
StreamDeckButtonBackground::Image(b) => image::open(b.path).unwrap().into(),
};
// Paint foreground
if value.numeric_value.is_some() || value.text_value.is_some() {
let fg_color = value.foreground_color.unwrap_or(DEFAULT_FG_COLOR);
match value.button_design.foreground {
StreamDeckButtonForeground::Solid(_) => {
StreamDeckButtonForeground::FadingColor(_) => {
let opacity = value.numeric_value.unwrap_or(UnitValue::MAX);
let mut rgba: Rgba<u8> = fg_color.into();
rgba[3] = (opacity.get() * 255.0).round() as u8;
for pixel in img.pixels_mut() {
pixel.blend(&rgba);
}
}
StreamDeckButtonForeground::Image(_) => {}
StreamDeckButtonForeground::Bar(_) => {
StreamDeckButtonForeground::FadingImage(_) => {}
StreamDeckButtonForeground::FullBar(_) => {
let percentage = value.numeric_value.map(|v| v.get()).unwrap_or(0.0);
let rect_height = (height as f64 * percentage) as u32;
// Fill the background
Expand Down

0 comments on commit a473ed3

Please sign in to comment.