summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-05-30 00:13:18 +0200
committernatrixaeria <janng@gmx.de>2019-05-30 00:13:18 +0200
commit43f14444fa6f4756aaac255e9c1b47b756d0ecd5 (patch)
treea2b7da5bb2f261ac43f69ca5f4241f43235cabe4
parentbf0137c3fc6093df085b9d95cd251b7f67d94815 (diff)
Initialise WebGl2 context
-rw-r--r--WebInterface/wasm/webhogg/src/app.rs11
-rw-r--r--WebInterface/wasm/webhogg/src/canvas.rs42
-rw-r--r--WebInterface/wasm/webhogg/src/lib.rs4
-rw-r--r--WebInterface/wasm/webhogg/src/page.rs26
-rw-r--r--WebInterface/wasm/webhogg/src/webhogg_exception.rs18
5 files changed, 96 insertions, 5 deletions
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<Self, WebhoggException> {
+ 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<Self, WebhoggException> {
+ 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::<web_sys::HtmlCanvasElement>()
+ .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::<WebGl2>()
+ .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<Self, WebhoggException> {
+ 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<web_sys::Element> {
+ 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())
}
}