#enum with const values

1 messages · Page 1 of 1 (latest)

fleet tartan
#

are there better ways to do this?

#[derive(EnumIter, Display, Default, PartialEq, Eq, Debug, Clone, Copy, Deserialize, Serialize)]
pub enum Resolution {
  R640_360,
  R1280_720,
  R1360_768,
  R1536_864,
  R1600_900,
  #[default]
  R1920_1080,
  R2048_1152,
  R2560_1080,
  R2560_1440,
  R3840_2160,
}

impl Into<Vec2> for Resolution {
  fn into(self) -> Vec2 {
    match self {
      Resolution::R640_360 => Vec2::new(640., 360.),
      Resolution::R1280_720 => Vec2::new(1280., 720.),
      Resolution::R1360_768 => Vec2::new(1360., 768.),
      Resolution::R1536_864 => Vec2::new(1536., 864.),
      Resolution::R1600_900 => Vec2::new(1600., 900.),
      Resolution::R1920_1080 => Vec2::new(1920., 1080.),
      Resolution::R2048_1152 => Vec2::new(2048., 1152.),
      Resolution::R2560_1080 => Vec2::new(2560., 1080.),
      Resolution::R2560_1440 => Vec2::new(2560., 1440.),
      Resolution::R3840_2160 => Vec2::new(3840., 2160.),
    }
  }
}
vale crescent
#

Why do you need an enum in the first place ?

fleet tartan
#

currenlty changed it from Into to From implementation

vale crescent
fleet tartan
#

currenlty, in config ron file I have this resolution: R2560_1080

#

so that I can only select one of supported resolutions

#

with array it will be possible to set any resolution

#

also I have a select box here in editor UI to change resolution and see how game looks under different resolutions and so I need this enum to render that select box

vale crescent
vale crescent
fleet tartan
vale crescent
fleet tartan
#

he will need some UI for that

vale crescent
#

So in the end it's the same problem if you use the values directly or with an enum

fleet tartan
vale crescent
fleet tartan
#

yes, but with enums I do not have to )

#

also with enums, in any part of code I know that resolution is somethig withing enum variants. It can't be just anythings

#

I think this is better approach

#

also, with help of strum I can do this:

#[derive(EnumIter, Display, Default, PartialEq, Eq, Debug, Clone, Copy, Deserialize, Serialize)]
pub enum Resolution {
  #[strum(serialize = "nHD 640x360 (16:9)")]
  R640_360,
  #[strum(serialize = "WXGA 1280x720 (16:9)")]
  R1280_720,
  #[strum(serialize = "HD 1360x768 (16:9)")]
  R1360_768,
  #[strum(serialize = "HD+ 1600x900 (16:9)")]
  R1600_900,
  #[default]
  #[strum(serialize = "FHD 1920x1080 (16:9)")]
  R1920_1080,
  #[strum(serialize = "UWFHD 2560x1080 (21:9")]
  R2560_1080,
  #[strum(serialize = "QWXGA 2048x1152 (16:9)")]
  R2048_1152,
  #[strum(serialize = "QHD 2560x1440 (16:9)")]
  R2560_1440,
  #[strum(serialize = "4K UHD 3840x2160 (16:9)")]
  R3840_2160,
}

impl From<Resolution> for Vec2 {
  fn from(value: Resolution) -> Self {
    match value {
      Resolution::R640_360 => Vec2::new(640., 360.),
      Resolution::R1280_720 => Vec2::new(1280., 720.),
      Resolution::R1360_768 => Vec2::new(1360., 768.),
      Resolution::R1600_900 => Vec2::new(1600., 900.),
      Resolution::R1920_1080 => Vec2::new(1920., 1080.),
      Resolution::R2048_1152 => Vec2::new(2048., 1152.),
      Resolution::R2560_1080 => Vec2::new(2560., 1080.),
      Resolution::R2560_1440 => Vec2::new(2560., 1440.),
      Resolution::R3840_2160 => Vec2::new(3840., 2160.),
    }
  }
}
#

and now I have nice captions for resolution select box

#

while with array I would have to switch to something like {resolution: Vec2, caption: String}

#

@vale crescent from the other side, you made me think about what might be rule to choose where to use arrays and when enums

vale crescent
#

Yeah, I think enums and values or both a good idea to use depending on your use case
If you want to have captions like this enums are better

fleet tartan
#

also with enums I have some kind of a guarantees that resolution value is something within enum variants. It is impossible to set up resolution to Vec2::new(1., 1.) by some kind of mistake

ashen python
#

if what you care about is validation then you don't need an enum for that, you can use a struct that has private constructor

pub struct Resolution(f32, f32);

impl TryFrom<(f32, f32)> for Resolution {
  type Error = ()

  fn try_from(value: (f32, f32)) -> Result<Self, Self::Error>
    match value {
      (640, 360.)
      | (1280., 720.)
      | (1360., 768.) => Ok(Resolution(value.0, value.1)),

      _ => Err(())
    }
  }
}

impl From<Resolution> for Vec2<f32> {
  fn from(value: Resolution) -> Self {
    Vec2::new(value.0, value.1)
  }
}
#

similarly you could define a static map of Resolution objects with string names for a selection dropdown

#

not saying that you should, just that you don't have to use an enum to be sure that a particular value has been validated