From aef7dd135379231b308f5d4165db6a4c588bbf99 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 22 Feb 2019 18:21:03 -0800 Subject: [PATCH] Allow the background color to be changed in the demo --- demo/common/src/lib.rs | 15 ++-- demo/common/src/ui.rs | 105 +++++++++++++++++++++------ resources/textures/demo-bg-dark.png | Bin 0 -> 1356 bytes resources/textures/demo-bg-light.png | Bin 0 -> 926 bytes 4 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 resources/textures/demo-bg-dark.png create mode 100644 resources/textures/demo-bg-light.png diff --git a/demo/common/src/lib.rs b/demo/common/src/lib.rs index f4919b30..bd7ad471 100644 --- a/demo/common/src/lib.rs +++ b/demo/common/src/lib.rs @@ -61,7 +61,8 @@ const CAMERA_ZOOM_AMOUNT_2D: f32 = 0.1; const NEAR_CLIP_PLANE: f32 = 0.01; const FAR_CLIP_PLANE: f32 = 10.0; -const BACKGROUND_COLOR: ColorU = ColorU { r: 32, g: 32, b: 32, a: 255 }; +const LIGHT_BG_COLOR: ColorU = ColorU { r: 192, g: 192, b: 192, a: 255 }; +const DARK_BG_COLOR: ColorU = ColorU { r: 32, g: 32, b: 32, a: 255 }; const GROUND_SOLID_COLOR: ColorU = ColorU { r: 80, g: 80, b: 80, a: 255 }; const GROUND_LINE_COLOR: ColorU = ColorU { r: 127, g: 127, b: 127, a: 255 }; @@ -337,7 +338,7 @@ impl DemoApp { tile_time, } = render_msg; - self.device.clear(); + self.device.clear(self.background_color()); self.draw_environment(&render_transform); self.render_vector_scene(&built_scene); @@ -457,7 +458,7 @@ impl DemoApp { fn render_vector_scene(&mut self, built_scene: &BuiltScene) { if self.ui.gamma_correction_effect_enabled { - self.renderer.enable_gamma_correction(BACKGROUND_COLOR); + self.renderer.enable_gamma_correction(self.background_color()); } else { self.renderer.disable_gamma_correction(); } @@ -529,6 +530,10 @@ impl DemoApp { } } } + + fn background_color(&self) -> ColorU { + if self.ui.dark_background_enabled { DARK_BG_COLOR } else { LIGHT_BG_COLOR } + } } struct SceneThreadProxy { @@ -800,8 +805,8 @@ struct DemoDevice { } impl DemoDevice { - fn clear(&self) { - let color = BACKGROUND_COLOR.to_f32(); + fn clear(&self, color: ColorU) { + let color = color.to_f32(); unsafe { gl::BindFramebuffer(gl::FRAMEBUFFER, 0); gl::ClearColor(color.r(), color.g(), color.b(), color.a()); diff --git a/demo/common/src/ui.rs b/demo/common/src/ui.rs index 566e6be4..d223cb99 100644 --- a/demo/common/src/ui.rs +++ b/demo/common/src/ui.rs @@ -30,7 +30,9 @@ const SLIDER_KNOB_HEIGHT: i32 = 48; const EFFECTS_PANEL_WIDTH: i32 = 550; const EFFECTS_PANEL_HEIGHT: i32 = BUTTON_HEIGHT * 3 + PADDING * 4; -const ROTATE_PANEL_X: i32 = PADDING + (BUTTON_WIDTH + PADDING) * 2 + PADDING + SWITCH_SIZE; +const BACKGROUND_SWITCH_X: i32 = PADDING + (BUTTON_WIDTH + PADDING) * 2 + PADDING + SWITCH_SIZE; + +const ROTATE_PANEL_X: i32 = PADDING + (BUTTON_WIDTH + PADDING) * 2 + (PADDING + SWITCH_SIZE) * 2; const ROTATE_PANEL_WIDTH: i32 = SLIDER_WIDTH + PADDING * 2; const ROTATE_PANEL_HEIGHT: i32 = PADDING * 2 + SLIDER_HEIGHT; @@ -39,6 +41,8 @@ static OPEN_PNG_NAME: &'static str = "demo-open"; static ROTATE_PNG_NAME: &'static str = "demo-rotate"; static ZOOM_IN_PNG_NAME: &'static str = "demo-zoom-in"; static ZOOM_OUT_PNG_NAME: &'static str = "demo-zoom-out"; +static BG_LIGHT_PNG_NAME: &'static str = "demo-bg-light"; +static BG_DARK_PNG_NAME: &'static str = "demo-bg-dark"; pub struct DemoUI { effects_texture: Texture, @@ -46,11 +50,14 @@ pub struct DemoUI { rotate_texture: Texture, zoom_in_texture: Texture, zoom_out_texture: Texture, + bg_light_texture: Texture, + bg_dark_texture: Texture, effects_panel_visible: bool, rotate_panel_visible: bool, pub three_d_enabled: bool, + pub dark_background_enabled: bool, pub gamma_correction_effect_enabled: bool, pub stem_darkening_effect_enabled: bool, pub subpixel_aa_effect_enabled: bool, @@ -64,6 +71,8 @@ impl DemoUI { let rotate_texture = device.create_texture_from_png(ROTATE_PNG_NAME); let zoom_in_texture = device.create_texture_from_png(ZOOM_IN_PNG_NAME); let zoom_out_texture = device.create_texture_from_png(ZOOM_OUT_PNG_NAME); + let bg_light_texture = device.create_texture_from_png(BG_LIGHT_PNG_NAME); + let bg_dark_texture = device.create_texture_from_png(BG_DARK_PNG_NAME); DemoUI { effects_texture, @@ -71,7 +80,10 @@ impl DemoUI { rotate_texture, zoom_in_texture, zoom_out_texture, + bg_light_texture, + bg_dark_texture, three_d_enabled: options.three_d, + dark_background_enabled: true, effects_panel_visible: false, rotate_panel_visible: false, gamma_correction_effect_enabled: false, @@ -107,15 +119,21 @@ impl DemoUI { // Draw 3D switch. let threed_switch_x = PADDING + (BUTTON_WIDTH + PADDING) * 2; let threed_switch_origin = Point2DI32::new(threed_switch_x, open_button_y); - debug_ui.draw_solid_rect(RectI32::new(threed_switch_origin, - Point2DI32::new(SWITCH_SIZE, BUTTON_HEIGHT)), - WINDOW_COLOR); - self.three_d_enabled = self.draw_switch(debug_ui, - event, - threed_switch_origin, - "2D", - "3D", - self.three_d_enabled); + self.three_d_enabled = self.draw_text_switch(debug_ui, + event, + threed_switch_origin, + "2D", + "3D", + self.three_d_enabled); + + // Draw background switch. + let background_switch_origin = Point2DI32::new(BACKGROUND_SWITCH_X, open_button_y); + self.dark_background_enabled = self.draw_image_switch(debug_ui, + event, + background_switch_origin, + &self.bg_light_texture, + &self.bg_dark_texture, + self.dark_background_enabled); // Draw rotate and zoom buttons, if applicable. if !self.three_d_enabled { @@ -248,15 +266,66 @@ impl DemoUI { let switch_x = PADDING + EFFECTS_PANEL_WIDTH - (SWITCH_SIZE + PADDING); let switch_y = window_y + PADDING + (BUTTON_HEIGHT + PADDING) * index; - self.draw_switch(debug_ui, event, Point2DI32::new(switch_x, switch_y), "Off", "On", value) + self.draw_text_switch(debug_ui, + event, + Point2DI32::new(switch_x, switch_y), + "Off", + "On", + value) + } + + fn draw_text_switch(&self, + debug_ui: &mut DebugUI, + event: &mut UIEvent, + origin: Point2DI32, + off_text: &str, + on_text: &str, + mut value: bool) + -> bool { + value = self.draw_switch(debug_ui, event, origin, value); + + let off_size = debug_ui.measure_text(off_text); + let on_size = debug_ui.measure_text(on_text); + let off_offset = SWITCH_HALF_SIZE / 2 - off_size / 2; + let on_offset = SWITCH_HALF_SIZE + SWITCH_HALF_SIZE / 2 - on_size / 2; + let text_top = BUTTON_TEXT_OFFSET; + + debug_ui.draw_text(off_text, origin + Point2DI32::new(off_offset, text_top), !value); + debug_ui.draw_text(on_text, origin + Point2DI32::new(on_offset, text_top), value); + + value + } + + fn draw_image_switch(&self, + debug_ui: &mut DebugUI, + event: &mut UIEvent, + origin: Point2DI32, + off_texture: &Texture, + on_texture: &Texture, + mut value: bool) + -> bool { + value = self.draw_switch(debug_ui, event, origin, value); + + let off_offset = SWITCH_HALF_SIZE / 2 - off_texture.size.x() / 2; + let on_offset = SWITCH_HALF_SIZE + SWITCH_HALF_SIZE / 2 - on_texture.size.x() / 2; + + let off_color = if !value { WINDOW_COLOR } else { TEXT_COLOR }; + let on_color = if value { WINDOW_COLOR } else { TEXT_COLOR }; + + debug_ui.draw_texture(origin + Point2DI32::new(off_offset, PADDING), + off_texture, + off_color); + debug_ui.draw_texture(origin + Point2DI32::new(on_offset, PADDING), + on_texture, + on_color); + + value } fn draw_switch(&self, debug_ui: &mut DebugUI, event: &mut UIEvent, origin: Point2DI32, - off_text: &str, - on_text: &str, mut value: bool) -> bool { let widget_rect = RectI32::new(origin, Point2DI32::new(SWITCH_SIZE, BUTTON_HEIGHT)); @@ -264,6 +333,7 @@ impl DemoUI { value = !value; } + debug_ui.draw_solid_rect(widget_rect, WINDOW_COLOR); debug_ui.draw_rect_outline(widget_rect, TEXT_COLOR); let highlight_size = Point2DI32::new(SWITCH_HALF_SIZE, BUTTON_HEIGHT); @@ -276,15 +346,6 @@ impl DemoUI { TEXT_COLOR); } - let off_size = debug_ui.measure_text(off_text); - let on_size = debug_ui.measure_text(on_text); - let off_offset = SWITCH_HALF_SIZE / 2 - off_size / 2; - let on_offset = SWITCH_HALF_SIZE + SWITCH_HALF_SIZE / 2 - on_size / 2; - let text_top = BUTTON_TEXT_OFFSET; - - debug_ui.draw_text(off_text, origin + Point2DI32::new(off_offset, text_top), !value); - debug_ui.draw_text(on_text, origin + Point2DI32::new(on_offset, text_top), value); - value } } diff --git a/resources/textures/demo-bg-dark.png b/resources/textures/demo-bg-dark.png new file mode 100644 index 0000000000000000000000000000000000000000..3425837d3b65b59be5e9c87828bd3b7e702e1efb GIT binary patch literal 1356 zcmZ{kdo&XY9LIm#u!eaZN_3DCHDk6eS>9~sifXn>UGL^`%q+8M+_IIf=}vCxL3tJ@ z?^h2(S}D^NU9Sk$Bq0?$#cmKHGb3T85&iB7h+Ad#Dtg3-30ARgn9&`X8tguQ2 z0QMrkqym6agsYD$Ky8NF_Yh?OK=A0EZcx>=ZVCWQ!f13KfW%Dzq7;Cy03ezK;1dAe z1Ot$>0M>AG8h1MYfJ&&hrw9Dp7x$)Q0Kjl*{yYGn{e=23+6h1j>*e9fhLz$ zmGw<;t0VBN)79vfQ&Y{YBzmei2_t#IYaC~}D;Rn@ortDNPGU+Cl8sMq*iegi)x97e z%$L?~_qYAB`gMe+$b0a_nsZ(H(s_M!TZh19XKqx1;b;GTXTzGH>IJI?opT?B!bCgb z%)ODyvJ8VTD&>_X)7W(WZ_6Bsx`bAbLseOo`5j)!D5d!TwrUpDFEAAyTHT<&kT{eW zF#MF*Zm3A0ybmr~V)ieVqr>zFv>lv;Xtvfq=5>_gNrkfq-K+-LP#)i?OMQ9e*b}qy z+jr|#0(UJZZ*3dNSqhWhT5sySIafISK%Zx~8Ii$hNkYvPpXA)}jHvG^g^cjG@frgS zj3a3kO(bsM>EV~%{u(3dm6>Ok`VqK*sdU9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