2019-01-02 22:08:13 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
2019-01-30 12:11:34 +00:00
|
|
|
use image;
|
2019-02-11 06:12:46 +00:00
|
|
|
use conrod_core::Ui;
|
|
|
|
use conrod_core::UiBuilder;
|
|
|
|
use conrod_core::widget::image::Image as ImageWidget;
|
|
|
|
use conrod_core::image::Map as ImageMap;
|
|
|
|
use conrod_core::{Positionable, Sizeable};
|
|
|
|
use conrod_core::Widget;
|
2019-01-02 22:08:13 +00:00
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
// Crate
|
|
|
|
use crate::{
|
|
|
|
PlayState,
|
2019-01-07 21:10:31 +00:00
|
|
|
PlayStateResult,
|
2019-01-02 21:25:01 +00:00
|
|
|
GlobalState,
|
|
|
|
window::Event,
|
2019-01-11 23:18:34 +00:00
|
|
|
session::SessionState,
|
2019-02-11 06:12:46 +00:00
|
|
|
render::{
|
|
|
|
Consts,
|
|
|
|
UiLocals,
|
|
|
|
Renderer,
|
|
|
|
Texture,
|
|
|
|
UiPipeline,
|
|
|
|
create_ui_quad_mesh,
|
2019-01-30 12:11:34 +00:00
|
|
|
},
|
2019-01-02 21:25:01 +00:00
|
|
|
};
|
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
pub struct TitleState {
|
|
|
|
ui: Ui,
|
2019-02-11 06:12:46 +00:00
|
|
|
image_map: ImageMap<Texture<UiPipeline>>,
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
|
|
|
impl TitleState {
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Create a new `TitleState`
|
2019-01-30 12:11:34 +00:00
|
|
|
pub fn new(renderer: &mut Renderer) -> Self {
|
2019-02-11 06:12:46 +00:00
|
|
|
let mut ui = UiBuilder::new([500.0, 500.0]).build();
|
|
|
|
let widget_id = ui.widget_id_generator().next();
|
|
|
|
let mut image_map = ImageMap::new();
|
|
|
|
let img = image::open(concat!(env!("CARGO_MANIFEST_DIR"), "/test_assets/test.png")).unwrap();
|
|
|
|
let img = renderer.create_texture(&img).unwrap();
|
|
|
|
let img_id = image_map.insert(img);
|
|
|
|
ImageWidget::new(img_id)
|
|
|
|
.x_y(0.0, 0.0)
|
|
|
|
.w_h(500.0,500.0)
|
|
|
|
.set(widget_id, &mut ui.set_widgets());
|
2019-01-30 12:11:34 +00:00
|
|
|
Self {
|
2019-02-11 06:12:46 +00:00
|
|
|
ui,
|
|
|
|
image_map
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// The background colour
|
2019-01-12 14:01:01 +00:00
|
|
|
const BG_COLOR: Rgba<f32> = Rgba { r: 0.0, g: 0.3, b: 1.0, a: 1.0 };
|
2019-01-11 20:14:37 +00:00
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
impl PlayState for TitleState {
|
2019-01-07 21:10:31 +00:00
|
|
|
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
2019-01-11 23:18:34 +00:00
|
|
|
loop {
|
|
|
|
// Handle window events
|
2019-01-07 21:10:31 +00:00
|
|
|
for event in global_state.window.fetch_events() {
|
|
|
|
match event {
|
2019-01-11 23:18:34 +00:00
|
|
|
Event::Close => return PlayStateResult::Shutdown,
|
|
|
|
// When space is pressed, start a session
|
|
|
|
Event::Char(' ') => return PlayStateResult::Push(
|
2019-03-03 22:11:38 +00:00
|
|
|
Box::new(SessionState::new(global_state.window.renderer_mut()).unwrap()), // TODO: Handle this error
|
2019-01-11 23:18:34 +00:00
|
|
|
),
|
|
|
|
// Ignore all other events
|
|
|
|
_ => {},
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
// Maintain the UI
|
2019-02-11 06:12:46 +00:00
|
|
|
//self.ui.maintain(global_state.window.renderer_mut());
|
2019-01-30 12:11:34 +00:00
|
|
|
|
|
|
|
// Draw the UI to the screen
|
2019-02-11 06:12:46 +00:00
|
|
|
//self.ui.render(global_state.window.renderer_mut());
|
|
|
|
if let Some(mut primitives) = Some(self.ui.draw()){//_if_changed() {
|
|
|
|
// Clear the screen
|
|
|
|
global_state.window.renderer_mut().clear(BG_COLOR);
|
|
|
|
|
|
|
|
//render the primatives one at a time
|
|
|
|
while let Some(prim) = primitives.next() {
|
|
|
|
let mut renderer = global_state.window.renderer_mut();
|
|
|
|
use conrod_core::render::{Primitive, PrimitiveKind};
|
|
|
|
let Primitive {kind, scizzor, rect, ..} = prim;
|
|
|
|
match kind {
|
|
|
|
PrimitiveKind::Image { image_id, color, source_rect } => {
|
|
|
|
|
|
|
|
let mut locals = renderer.create_consts(&[UiLocals::default()]).unwrap();
|
|
|
|
renderer.update_consts(&mut locals, &[UiLocals::new(
|
|
|
|
[0.0, 0.0, 1.0, 1.0],
|
|
|
|
)]);
|
|
|
|
let model = renderer.create_model(&create_ui_quad_mesh()).unwrap();
|
|
|
|
global_state.window.renderer_mut().render_ui_element(&model, &locals, self.image_map.get(&image_id).unwrap())
|
|
|
|
}
|
|
|
|
_ => {println!("should not reach here");}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Finish the frame
|
|
|
|
global_state.window.renderer_mut().flush();
|
|
|
|
global_state.window
|
|
|
|
.swap_buffers()
|
|
|
|
.expect("Failed to swap window buffers");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str { "Title" }
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|