added dropping files to load prez; added rectangles
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 18 Feb 2021 23:08:20 +0000 (17:08 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 18 Feb 2021 23:08:20 +0000 (17:08 -0600)
dist/index.html
dist/index.js
initial.prez [new file with mode: 0644]
src/events.onyx
src/prez.onyx
src/show_parser.onyx
src/slides.onyx
test.prez [deleted file]

index fd3b3538e877dd2e4733a65258a8d6968d54983b..ef8a6ea6a60e8526b98157374747dbed5c95719f 100644 (file)
@@ -17,6 +17,6 @@
     </head>
 
     <body>
-        <canvas id="prez_canvas">This browser does not support the HTML5 Canvas.</canvas>
+        <canvas id="prez_canvas" ondragover="prez_dragover_handler(event);">This browser does not support the HTML5 Canvas.</canvas>
     </body>
 </html>
index a6d998968606fd3805b246dff13e54b5fad27538..3c1f889f91691017c89655bfc2334c0164bf07d3 100644 (file)
@@ -3,9 +3,15 @@ let wasm_instance;
 let canvasElem;
 let canvasCtx;
 let __loaded_images = [];
+let presentation_source;
 
 const MAGIC_CANVAS_NUMBER = 0x5052455A;
 
+function prez_dragover_handler(ev) {
+    ev.preventDefault();
+    return false;
+}
+
 function push_event_to_buffer(esp, event_size, event_kind, data) {
     let WASM_U32 = new Uint32Array(wasm_instance.exports.memory.buffer);
 
@@ -62,12 +68,30 @@ let event_import_obj = {
             push_event_to_buffer(esp, event_size, 0x06, [ window.innerWidth, window.innerHeight ]);
         });
 
+        document.getElementById("prez_canvas").addEventListener("drop", async (ev) => {
+            ev.preventDefault();
+
+            presentation_source = await ev.dataTransfer.items[0].getAsFile().arrayBuffer();
+            push_event_to_buffer(esp, event_size, 0x08, [ presentation_source.byteLength ]);
+
+            return false;
+        });
+
         push_event_to_buffer(esp, event_size, 0x06, [ window.innerWidth, window.innerHeight ]);
 
         document.oncontextmenu = (e) => {
             e.preventDefault = true;
             return false;
         };
+    },
+
+    copy_presentation_source(dest_ptr, dest_len) {
+        if (dest_len < presentation_source.byteLength) return;
+
+        let wasm_u8 = new Uint8Array(wasm_instance.exports.memory.buffer);
+        let source  = new Uint8Array(presentation_source);
+
+        wasm_u8.set(source, dest_ptr);
     }
 }
 
diff --git a/initial.prez b/initial.prez
new file mode 100644 (file)
index 0000000..8fa408a
--- /dev/null
@@ -0,0 +1,3 @@
+[slide] [background 60 60 60]
+[text_style undefind font_name "monospace" font_size 48 font_attr bold centered]
+[y 50] Drop a presentation file here!
index 543486f361c83f3f0a527ce33fb1af3006891268..820076da34a3ec65c986549079e9aaf73e60b408 100644 (file)
@@ -15,6 +15,8 @@ DomEventKind :: enum {
     KeyUp      :: 0x05;
 
     Resize     :: 0x06;
+
+    FileDropped :: 0x08;
 }
 
 DomEvent :: struct {
@@ -52,12 +54,19 @@ ResizeEvent :: struct {
     height : u32;
 }
 
+FileDroppedEvent :: struct {
+    use event : DomEvent;
+
+    file_size : u32;
+}
+
 Event :: struct #union {
     use dom : DomEvent;
 
     keyboard : KeyboardEvent;
     mouse    : MouseEvent;
     resize   : ResizeEvent;
+    file     : FileDroppedEvent;
 }
 
 clear_event :: (ev: ^Event) {
index 63533570f296121056ddc0480d35bb8f1b773e98..a2db31cced1445a6e7601d6d4189fafb056ce130 100644 (file)
@@ -10,40 +10,44 @@ redraw := 0
 poll_events :: () {
     use event.DomEventKind
 
-    redraw = 2;
-
     ev: event.Event;
-    while event.poll(^ev) do switch ev.kind {
-        case Resize {
-            printf("New window size: %i, %i\n", ev.resize.width, ev.resize.height);
+    while event.poll(^ev) {
+        redraw = 1;
 
-            use Canvas
-            set_size(canvas, ev.resize.width, ev.resize.height);
-        }
+        switch ev.kind {
+            case Resize {
+                use Canvas
+                set_size(canvas, ev.resize.width, ev.resize.height);
+            }
 
-        case MouseDown {
-            printf("Mouse down: %i, %i\n", ev.mouse.pos_x, ev.mouse.pos_y);
+            case MouseDown {
+                use event.MouseButton
+                switch ev.mouse.button {
+                    case Right    do slideshow_advance_slide(^the_slideshow, -1);
+                    case #default do slideshow_advance_slide(^the_slideshow, 1);
+                }
+            }
 
-            use event.MouseButton
-            switch ev.mouse.button {
-                case Right    do slideshow_advance_slide(^the_slideshow, -1);
-                case #default do slideshow_advance_slide(^the_slideshow, 1);
+            case KeyDown {
+                switch ev.keyboard.keycode {
+                    case 0x25 do slideshow_advance_slide(^the_slideshow, -1);
+                    case 0x27 do slideshow_advance_slide(^the_slideshow, 1);
+                }
             }
-        }
 
-        case KeyDown {
-            printf("Keydown: %i\n", ev.keyboard.keycode);
+            case FileDropped {
+                printf("New slideshow was dropped! %i bytes in size\n", ev.file.file_size);
 
-            switch ev.keyboard.keycode {
-                case 0x25 do slideshow_advance_slide(^the_slideshow, -1);
-                case 0x27 do slideshow_advance_slide(^the_slideshow, 1);
-            }
-        }
+                source := memory.make_slice(u8, ev.file.file_size);
+                defer cfree(source.data);
 
-        case #default do redraw = 0;
+                copy_presentation_source :: (source: [] u8) -> void #foreign "event" "copy_presentation_source" ---
+                copy_presentation_source(source);
 
-    } else {
-        redraw = 0;
+                slideshow_reset(^the_slideshow);
+                parse_slideshow(source, ^the_slideshow);
+            }
+        }
     }
 }
 
@@ -65,7 +69,7 @@ main :: (args: [] cstr) {
     event.init();
 
     the_slideshow = slideshow_make();
-    parse_slideshow(#file_contents "test.prez", ^the_slideshow);
+    parse_slideshow(#file_contents "initial.prez", ^the_slideshow);
 
     start_loop :: () -> void #foreign "host" "start_loop" ---
     start_loop();
index b83e8ce0f99e37572d3b73974a9362db92bf01a7..38fb95cf32344c8e35e057885d20c8d3d20d9dc0 100644 (file)
@@ -36,7 +36,6 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                 read_byte(^show_reader);
 
                 command_name := read_word(^show_reader, allocator=parse_alloc);
-                printf("Parsing command: %s\n", command_name);
 
                 if command_name == "slide" {
                     prev_slide := ^slideshow.slides[slideshow.slides.count - 1];
@@ -88,6 +87,14 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                     current_slide.items[current_item_idx] = new_slide_image;
                     current_item_idx += 1;
                 }
+                elseif command_name == "rect" {
+                    new_slide_rect := slideshow_make_item(slideshow);
+                    new_slide_rect.kind = Slide_Item.Kind.Rect;
+                    parse_rect_style(^show_reader, ^new_slide_rect.rect, parse_alloc);
+
+                    current_slide.items[current_item_idx] = new_slide_rect;
+                    current_item_idx += 1;
+                }
                 else {
                     printf("******** Unknown command: '%s'.\n", command_name);
                 }
@@ -128,7 +135,6 @@ parse_text_style :: (reader: ^io.Reader, text_style: ^Slide_Item_Text, parse_all
         if style_name == "color" {
             r, g, b := parse_color(reader);
             text_style.color = Color.{ r, g, b };
-            printf("Parsed color: %f %f %f\n", r, g, b);
         }
         elseif style_name == "font_size" {
             text_style.font_size = read_u32(reader);
@@ -150,8 +156,7 @@ parse_text_style :: (reader: ^io.Reader, text_style: ^Slide_Item_Text, parse_all
         elseif style_name == "centered" do text_style.justify = Slide_Item_Text.Justify.Center;
         elseif style_name == "right"    do text_style.justify = Slide_Item_Text.Justify.Right;
         else {
-            use package core { printf as pf }
-            pf("Unknown style option: '%s'\n", style_name);
+            printf("Unknown style option: '%s'\n", style_name);
             advance_line(reader);
             break;
         }
@@ -183,8 +188,44 @@ parse_image_style :: (reader: ^io.Reader, image_style: ^Slide_Item_Image, parse_
             image_style.width = cast(f32) width / 100;
         }
         else {
-            use package core { printf as pf }
-            pf("Unknown style option: '%s'\n", style_name);
+            printf("Unknown style option: '%s'\n", style_name);
+            advance_line(reader);
+            break;
+        }
+    }
+}
+
+#private_file
+parse_rect_style :: (reader: ^io.Reader, rect_style: ^Slide_Item_Rect, parse_alloc := context.allocator) {
+    use io
+
+    while !stream_end_of_file(reader.stream) && peek_byte(reader) != #char "]" {
+        skip_whitespace(reader);
+        defer skip_whitespace(reader);
+        style_name := read_word(reader, allocator=parse_alloc);
+        
+        if style_name == "x" {
+            x := read_u32(reader);
+            rect_style.x = cast(f32) x / 100;
+        }
+        elseif style_name == "y" {
+            y := read_u32(reader);
+            rect_style.y = cast(f32) y / 100;
+        }
+        elseif style_name == "w" {
+            w := read_u32(reader);
+            rect_style.w = cast(f32) w / 100;
+        }
+        elseif style_name == "h" {
+            h := read_u32(reader);
+            rect_style.h = cast(f32) h / 100;
+        }
+        elseif style_name == "color" {
+            r, g, b := parse_color(reader);
+            rect_style.color = Color.{ r, g, b };
+        }
+        else {
+            printf("Unknown style option: '%s'\n", style_name);
             advance_line(reader);
             break;
         }
index 122247dd7782bbc4e71217714ac1433103ecaff7..44f1cf5a3156895f4e96e4e8822fbbd15c0dc22c 100644 (file)
@@ -35,11 +35,13 @@ Slide_Item :: struct #union {
         Undefined;
         Text;
         Image;
+        Rect;
     }
 
     use base: Slide_Item_Base;
     text  : Slide_Item_Text;
     image : Slide_Item_Image; 
+    rect  : Slide_Item_Rect;
 }
 
 Slide_Item_Base :: struct {
@@ -76,6 +78,15 @@ Slide_Item_Image :: struct {
     // so the height of the image is automatically determined.
 }
 
+Slide_Item_Rect :: struct {
+    use base := Slide_Item_Base.{ Slide_Item.Kind.Rect };
+
+    color : Color;
+
+    x, y : f32; // Between 0 and 1
+    w, h : f32; // Between 0 and 1
+}
+
 
 
 slideshow_make :: (allocator := context.allocator) -> Slideshow {
@@ -223,6 +234,19 @@ slide_item_render :: (use slide_item: ^Slide_Item, slide: ^Slide) {
                 draw_image(canvas, html_image.handle, x, y, w, h);
             }
         }
+
+        case Rect {
+            use Canvas
+
+            width, height := cast(f32) get_width(canvas), cast(f32) get_height(canvas);
+
+            x := rect.x * width;
+            y := rect.y * height;
+            w := rect.w * width;
+            h := rect.h * height;
+
+            fill_rect(canvas, x, y, w, h, rect.color.r, rect.color.g, rect.color.b, rect.color.a);
+        }
     }
 
 
diff --git a/test.prez b/test.prez
deleted file mode 100644 (file)
index 05936fc..0000000
--- a/test.prez
+++ /dev/null
@@ -1,39 +0,0 @@
-
-[define_text_style normal_text color 255 255 255 font_size 36 font_name "Arial" left padding 10]
-[define_text_style bold_title color 255 255 255 font_attr bold font_size 72 font_name "Arial" centered]
-
-[load_image first_image "images/first.jpg"]
-
-# ------------------------------------------------------------------
-[slide]
-[background 25 25 40]
-[text_style bold_title]
-[y 50]
-First slide title!
-
-# ------------------------------------------------------------------
-[slide]
-[background 40 25 25]
-[text_style bold_title]
-[y 10]
-Second slide title!
-
-[text_style normal_text]
-[y 20]
-Some normal looking text is here.
-
-# Robustness: This shouldn't be necessary to declare.
-
-[text_style normal_text font_attr bold]
-[y 25]
-Some more normal looking text is here.
-
-[image first_image x 30 y 30 width 40]
-
-# ------------------------------------------------------------------
-[slide]
-[text_style bold_title color 255 50 50 font_size 96 font_attr italic]
-[y 10]
-Bigger image!
-
-[image first_image x 25 width 50 y 15]