From 43f14444fa6f4756aaac255e9c1b47b756d0ecd5 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Thu, 30 May 2019 00:13:18 +0200 Subject: Initialise WebGl2 context --- WebInterface/wasm/webhogg/src/app.rs | 11 ++++-- WebInterface/wasm/webhogg/src/canvas.rs | 42 ++++++++++++++++++++++ WebInterface/wasm/webhogg/src/lib.rs | 4 ++- WebInterface/wasm/webhogg/src/page.rs | 26 ++++++++++++++ WebInterface/wasm/webhogg/src/webhogg_exception.rs | 18 ++++++++-- 5 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 WebInterface/wasm/webhogg/src/canvas.rs create mode 100644 WebInterface/wasm/webhogg/src/page.rs diff --git a/WebInterface/wasm/webhogg/src/app.rs b/WebInterface/wasm/webhogg/src/app.rs index bee7c08..7931418 100644 --- a/WebInterface/wasm/webhogg/src/app.rs +++ b/WebInterface/wasm/webhogg/src/app.rs @@ -1,15 +1,22 @@ use crate::webhogg_exception::WebhoggException; +use crate::page::Page; +use crate::canvas::Canvas; -pub struct WebhoggApplication { +pub(crate) struct WebhoggApplication { + page: Page, + canvas: Canvas, } impl WebhoggApplication { pub fn new() -> Result { + let page = Page::obtain()?; + let canvas = Canvas::from_existing("canvas", &page)?; Ok(Self { + page, canvas, }) } - pub fn run(&mut self) -> Result<(), WebhoggException> { + pub fn run(self) -> Result<(), WebhoggException> { Ok(()) } } diff --git a/WebInterface/wasm/webhogg/src/canvas.rs b/WebInterface/wasm/webhogg/src/canvas.rs new file mode 100644 index 0000000..a0ef7d1 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/canvas.rs @@ -0,0 +1,42 @@ +use web_sys::WebGl2RenderingContext as WebGl2; +use wasm_bindgen::JsCast; +use crate::webhogg_exception::WebhoggException; +use crate::page::Page; + +pub struct Canvas { + ctx: WebGl2, +} + +impl Canvas { + pub fn from_existing(id: &str, page: &Page) -> Result { + let canvas_element = page.get_element(id) + .ok_or(WebhoggException::DomError( + "could not obtain canvas element (id=canvas)" + .to_string()))?; + let canvas_element = canvas_element + .dyn_into::() + .map_err(|_| WebhoggException::DomError( + "id=canvas is not a canvas element".to_string()))?; + debug!("successfully obtained canvas element"); + let ctx = canvas_element.get_context("webgl2") + .map_err(|_| WebhoggException::WebGlContextError( + "obtained invalid webgl2 context js value".to_string()))? + .ok_or(WebhoggException::WebGlContextError( + "could not obtaine webgl2 context".to_string()))? + .dyn_into::() + .map_err(|_| WebhoggException::WebGlContextError( + "obtained invalid webgl2 context js object".to_string()))?; + debug!("successfully obtained webgl2 context"); + Ok(Self { + ctx, + }) + } + + pub fn gl<'a>(&'a self) -> &'a WebGl2 { + &self.ctx + } + + pub fn gl_mut<'a>(&'a mut self) -> &'a mut WebGl2 { + &mut self.ctx + } +} diff --git a/WebInterface/wasm/webhogg/src/lib.rs b/WebInterface/wasm/webhogg/src/lib.rs index 8b75ee1..81a3237 100644 --- a/WebInterface/wasm/webhogg/src/lib.rs +++ b/WebInterface/wasm/webhogg/src/lib.rs @@ -1,5 +1,7 @@ mod client_logger; mod webhogg_exception; +mod page; +mod canvas; mod app; use wasm_bindgen::prelude::*; @@ -9,7 +11,7 @@ use app::WebhoggApplication as App; extern crate log; fn run_application() { - match App::new().and_then(|mut app| app.run()) { + match App::new().and_then(|app| app.run()) { Ok(_) => info!("program terminated successfully"), Err(e) => error!("program terminated with failure > {}", e) } diff --git a/WebInterface/wasm/webhogg/src/page.rs b/WebInterface/wasm/webhogg/src/page.rs new file mode 100644 index 0000000..cb9ee3f --- /dev/null +++ b/WebInterface/wasm/webhogg/src/page.rs @@ -0,0 +1,26 @@ +use web_sys; + +use crate::webhogg_exception::WebhoggException; + +pub struct Page { + window: web_sys::Window, + document: web_sys::Document, +} + +impl Page { + pub fn obtain() -> Result { + let window = web_sys::window() + .ok_or(WebhoggException::DomError("could not obtain window".to_string()))?; + let document = window.document() + .ok_or(WebhoggException::DomError("could not obtain document".to_string()))?; + debug!("initialised page"); + Ok(Self { + window, + document, + }) + } + + pub fn get_element(&self, id: &str) -> Option { + self.document.get_element_by_id(id) + } +} diff --git a/WebInterface/wasm/webhogg/src/webhogg_exception.rs b/WebInterface/wasm/webhogg/src/webhogg_exception.rs index eac67c7..46eedd6 100644 --- a/WebInterface/wasm/webhogg/src/webhogg_exception.rs +++ b/WebInterface/wasm/webhogg/src/webhogg_exception.rs @@ -2,11 +2,25 @@ use std::error::Error; #[derive(Debug)] pub enum WebhoggException { + DomError(String), + WebGlContextError(String), +} + +impl WebhoggException { + fn err_name(&self) -> &str { + match self { + WebhoggException::DomError(_) => "DomError", + WebhoggException::WebGlContextError(_) => "WebGlContextError", + } + } } impl Error for WebhoggException { fn description(&self) -> &str { - "webhogg exception" + match self { + WebhoggException::DomError(desc) => desc, + WebhoggException::WebGlContextError(desc) => desc, + } } fn cause(&self) -> Option<&dyn Error> { @@ -20,6 +34,6 @@ impl Error for WebhoggException { impl std::fmt::Display for WebhoggException { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "WebhoggException: {}", self.description()) + write!(f, "WebhoggException::{} {}", self.err_name(), self.description()) } } -- cgit v1.2.3-54-g00ecf