Generalize `draw_image_switch` to multiple segments

This commit is contained in:
Patrick Walton 2019-05-08 10:38:12 -07:00
parent 0d25fd0eab
commit 940605fbb5
2 changed files with 14 additions and 24 deletions

View File

@ -490,7 +490,6 @@ where
debug_ui_presenter.ui_presenter.draw_solid_rect(device, slider_knob_rect, TEXT_COLOR); debug_ui_presenter.ui_presenter.draw_solid_rect(device, slider_knob_rect, TEXT_COLOR);
} }
fn draw_screenshot_menu_item<W>( fn draw_screenshot_menu_item<W>(
&mut self, &mut self,
device: &D, device: &D,

View File

@ -449,35 +449,26 @@ impl<D> UIPresenter<D> where D: Device {
value value
} }
/// TODO(pcwalton): Support switches with more than two segments.
pub fn draw_image_switch(&mut self, pub fn draw_image_switch(&mut self,
device: &D, device: &D,
origin: Point2DI32, mut origin: Point2DI32,
off_texture: &D::Texture, segment_textures: &[&D::Texture],
on_texture: &D::Texture, mut value: u8)
mut value: bool) -> u8 {
-> bool {
if let Some(new_value) = self.draw_segmented_control(device, if let Some(new_value) = self.draw_segmented_control(device,
origin, origin,
Some(value as u8), Some(value),
2) { segment_textures.len() as u8) {
value = new_value != 0; value = new_value;
} }
let off_offset = SEGMENT_SIZE / 2 - device.texture_size(off_texture).x() / 2; for (segment_index, segment_texture) in segment_textures.iter().enumerate() {
let on_offset = SEGMENT_SIZE + SEGMENT_SIZE / 2 - device.texture_size(on_texture).x() / 2; let texture_width = device.texture_size(segment_texture).x();
let offset = SEGMENT_SIZE / 2 - texture_width / 2;
let off_color = if !value { WINDOW_COLOR } else { TEXT_COLOR }; let color = if segment_index as u8 == value { TEXT_COLOR } else { WINDOW_COLOR };
let on_color = if value { WINDOW_COLOR } else { TEXT_COLOR }; self.draw_texture(device, origin + Point2DI32::new(offset, 0), segment_texture, color);
origin += Point2DI32::new(SEGMENT_SIZE + 1, 0);
self.draw_texture(device, }
origin + Point2DI32::new(off_offset, PADDING),
off_texture,
off_color);
self.draw_texture(device,
origin + Point2DI32::new(on_offset, PADDING),
on_texture,
on_color);
value value
} }