From: Brendan Hansen Date: Sat, 20 Feb 2021 17:07:18 +0000 (-0600) Subject: added configurable slide animation in text format X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=5ad4db7fe2fb839f3def49cc974965b2546ce8c0;p=onyx-prez.git added configurable slide animation in text format --- diff --git a/dist/prez.wasm b/dist/prez.wasm index 4ec8ace..dd262a3 100644 Binary files a/dist/prez.wasm and b/dist/prez.wasm differ diff --git a/onyx.prez b/onyx.prez index 676a9ca..4901fba 100644 --- a/onyx.prez +++ b/onyx.prez @@ -16,6 +16,7 @@ # --------------------------------------------- [slide] [background $bbright $bbright $bbright] +[animation fade] [rect color 40 40 40 x 0 w 100 y 35 h 30] [rect color 0 0 0 x 0 w 100 y 40 h 20] @@ -32,6 +33,7 @@ # Introduce what Onyx is, the design goals, the improvements over C, etc. [slide] +[animation swipe] [rect color 40 40 40 x 8 w 84 y 5 h 95] [rect color 0 0 0 x 0 w 100 y 5 h 10] [text_style header] @@ -55,6 +57,7 @@ # BRIEFLY introduce WASM and its capabilities [slide] +[animation swipe] [rect color 40 40 40 x 8 w 84 y 5 h 95] [rect color 0 0 0 x 0 w 100 y 5 h 10] [text_style header] @@ -89,6 +92,7 @@ https://webassembly.org/ # High-level language features and design that make it super cozy to program in. [slide] +[animation swipe] [rect color 40 40 40 x 8 w 84 y 5 h 95] [rect color 0 0 0 x 0 w 100 y 5 h 10] [text_style header] @@ -119,6 +123,7 @@ https://webassembly.org/ # Hello, World! [slide] +[animation swipe] [rect color 40 40 40 x 8 w 84 y 5 h 95] [rect color 0 0 0 x 0 w 100 y 5 h 10] [text_style header] @@ -143,6 +148,7 @@ https://webassembly.org/ # * Running engine [slide] +[animation swipe] [rect color 40 40 40 x 8 w 84 y 5 h 95] [rect color 0 0 0 x 0 w 100 y 5 h 10] [text_style header] @@ -174,6 +180,7 @@ https://webassembly.org/ # Projects done in Onyx [slide] +[animation swipe] [rect color 40 40 40 x 8 w 84 y 5 h 95] [rect color 0 0 0 x 0 w 100 y 5 h 10] [text_style header] diff --git a/src/show_parser.onyx b/src/show_parser.onyx index d67f484..5939e72 100644 --- a/src/show_parser.onyx +++ b/src/show_parser.onyx @@ -30,6 +30,11 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { variables := map.make(str, u32); defer map.free(^variables); + animations := map.make(str, #type (Allocator) -> ^Slide_Animation, default=null_proc, hash_count=2); + defer map.free(^animations); + map.put(^animations, "swipe", Slide_Animation_Swipe.make); + map.put(^animations, "fade", Slide_Animation_Fade.make); + parse_context := ParseContext.{ ^show_reader, ^variables }; current_slide: ^Slide = null; @@ -58,7 +63,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { if current_slide != null { background_color = current_slide.background; - flush_items(current_slide, ^current_slide_items); + flush_items(current_slide, ^current_slide_items, slideshow); } current_slide = slideshow_insert_slide(slideshow); @@ -137,6 +142,18 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { map.put(^variables, var_name, var_value); } + elseif command_name == "animation" { + anim_name := read_word(^show_reader, numeric_allowed=true); + + if map.has(^animations, anim_name) { + allocator := alloc.arena.make_allocator(^slideshow.arena); + current_slide.animation = (map.get(^animations, anim_name))(allocator); + } else { + printf("Unknown animation: '%s'\n", anim_name); + + current_slide.animation = null; + } + } else { printf("******** Unknown command: '%s'.\n", command_name); } @@ -164,12 +181,14 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { } } - flush_items(current_slide, ^current_slide_items); + flush_items(current_slide, ^current_slide_items, slideshow); printf("Total slide count: %i\n", slideshow.slides.count); - flush_items :: (slide: ^Slide, items: ^[..] ^Slide_Item) { - slide.items = memory.make_slice(#type ^Slide_Item, items.count); + flush_items :: (slide: ^Slide, items: ^[..] ^Slide_Item, slideshow: ^Slideshow) { + allocator := alloc.arena.make_allocator(^slideshow.arena); + slide.items = memory.make_slice(#type ^Slide_Item, items.count, allocator = allocator); + for idx: items.count { slide.items[idx] = items.data[idx]; } diff --git a/src/slides.onyx b/src/slides.onyx index 799cd92..ea6717b 100644 --- a/src/slides.onyx +++ b/src/slides.onyx @@ -29,7 +29,7 @@ Slide :: struct { background : Color; aspect_ratio : f32 = 1; - animation_generator : (i32, i32) -> ^Slide_Animation; + animation: ^Slide_Animation; items : [] ^Slide_Item; } @@ -142,9 +142,11 @@ slideshow_advance_slide :: (use s: ^Slideshow, count := 1) { current_slide = math.clamp(current_slide, 0, slides.count - 1); if old_slide != current_slide { - if slides[current_slide].animation_generator == null_proc do return; + current_animation = slides[old_slide].animation; - current_animation = slides[current_slide].animation_generator(old_slide, current_slide); + if current_animation != null { + current_animation->init(old_slide, current_slide); + } } } @@ -186,7 +188,6 @@ slideshow_update_animation :: (use s: ^Slideshow) { if current_animation == null do return; if current_animation->update() { - cfree(current_animation); current_animation = null; } } @@ -194,14 +195,19 @@ slideshow_update_animation :: (use s: ^Slideshow) { -slide_init :: (use slide: ^Slide, background_color := Color.{0, 0, 0, 1}, item_count := 4, aspect := 1.0f) { +slide_init :: (use slide: ^Slide, + background_color := Color.{0, 0, 0, 1}, + item_count := 0, + aspect := 1.0f) { + background = background_color; aspect_ratio = aspect; - - animation_generator = fade_animation_make; + animation = null; if item_count > 0 { - items = memory.make_slice(#type ^Slide_Item, item_count); + // @GlobalVariable + allocator := alloc.arena.make_allocator(^the_slideshow.arena); + items = memory.make_slice(#type ^Slide_Item, item_count, allocator=allocator); memory.set(items.data, 0, items.count * sizeof ^Slide_Item); } else { @@ -356,8 +362,9 @@ aprintf :: (allocator: Allocator, format: str, va: ...) -> str { // Slide_Animation :: struct { - render : (anim: ^Slide_Animation, slideshow: ^Slideshow) -> void; + init : (anim: ^Slide_Animation, source: i32, target: i32) -> void; update : (anim: ^Slide_Animation) -> bool; + render : (anim: ^Slide_Animation, slideshow: ^Slideshow) -> void; source_slide : i32 = -1; target_slide : i32 = -1; @@ -365,22 +372,29 @@ Slide_Animation :: struct { Slide_Animation_Swipe :: struct { use base := Slide_Animation.{ + init = swipe_animation_init, update = swipe_animation_update, render = swipe_animation_render }; t : f32 = 0; dt : f32 = 0.03; -} -swipe_animation_make :: (source := -1, target := -1) -> ^Slide_Animation { - anim := new(Slide_Animation_Swipe); - *anim = Slide_Animation_Swipe.{}; - anim.source_slide = source; - anim.target_slide = target; + make :: (allocator := context.allocator) -> ^Slide_Animation_Swipe { + anim := new(Slide_Animation_Swipe, allocator=allocator); + *anim = Slide_Animation_Swipe.{}; + + return anim; + } +} - return anim; +#private_file +swipe_animation_init :: (use anim: ^Slide_Animation_Swipe, source := -1, target := -1) { + source_slide = source; + target_slide = target; + + t = 0; } #private_file @@ -411,22 +425,29 @@ swipe_animation_render :: (use anim: ^Slide_Animation_Swipe, slideshow: ^Slidesh Slide_Animation_Fade :: struct { use base := Slide_Animation.{ + init = fade_animation_init, update = fade_animation_update, render = fade_animation_render }; t : f32 = 0; dt : f32 = 0.03; -} -fade_animation_make :: (source := -1, target := -1) -> ^Slide_Animation { - anim := new(Slide_Animation_Fade); - *anim = Slide_Animation_Fade.{}; - anim.source_slide = source; - anim.target_slide = target; + make :: (allocator := context.allocator) -> ^Slide_Animation_Fade { + anim := new(Slide_Animation_Fade, allocator=allocator); + *anim = Slide_Animation_Fade.{}; + + return anim; + } +} + +#private_file +fade_animation_init :: (use anim: ^Slide_Animation_Fade, source := -1, target := -1) { + source_slide = source; + target_slide = target; - return anim; + t = 0; } #private_file diff --git a/todo b/todo index 1248765..b37fc6b 100644 --- a/todo +++ b/todo @@ -4,4 +4,4 @@ [X] Slide animation: Slide from Right [X] Slide animation: Fade -[ ] Slide animation config in text format +[X] Slide animation config in text format