honeylisp/presentation/engine.fnl

108 lines
3.8 KiB
Plaintext
Raw Normal View History

2021-06-19 01:31:21 +00:00
(local lume (require :lib.lume))
(local style (require :core.style))
(local common (require :core.common))
(local View (require :core.view))
(local SlideshowView (View:extend))
(fn SlideshowView.parse [slides]
(var style nil)
(icollect [_ slide (ipairs slides)]
(icollect [_ elem (ipairs slide)]
(match (type elem)
2021-06-22 02:09:09 +00:00
(where :table elem.style) (do (set style elem) nil)
:table elem
2021-06-19 01:31:21 +00:00
:string (lume.merge style {:text elem})))))
(fn SlideshowView.new [self slides]
(SlideshowView.super.new self)
(set self.slides slides)
2021-06-22 02:09:09 +00:00
(set self.imagecache {})
2021-06-19 01:31:21 +00:00
(set self.islide 1)
(set self.ielem 0)
(self:advance))
(fn SlideshowView.next-slide [self]
2021-06-22 02:09:09 +00:00
(set self.islide (if (>= self.islide (length self.slides)) 1 (+ self.islide 1)))
2021-06-19 01:31:21 +00:00
(set self.ielem 0)
(self:advance))
(fn SlideshowView.prev-slide [self]
2021-06-22 02:09:09 +00:00
(set self.islide (if (<= self.islide 1) (length self.slides) (- self.islide 1)))
2021-06-19 01:31:21 +00:00
(set self.ielem (+ 1 (length (. self.slides self.islide))))
(self:back))
(fn SlideshowView.ielemNext [self ielem di]
(let [slide (. self.slides self.islide)
elem (. slide ielem)]
(when elem
(if elem.pause-after ielem
(self:ielemNext (+ ielem di) di)))))
(fn SlideshowView.advance [self]
(let [ielemNext (self:ielemNext (+ self.ielem 1) 1)]
(if ielemNext (set self.ielem ielemNext)
(self:next-slide))))
(fn SlideshowView.back [self]
(let [ielemNext (self:ielemNext (- self.ielem 1) -1)]
(if ielemNext (set self.ielem ielemNext)
(self:prev-slide))))
2021-06-22 02:09:09 +00:00
(fn SlideshowView.load-image [self {:image filename}]
(when (= (. self.imagecache filename) nil)
(tset self.imagecache filename (love.graphics.newImage filename)))
(. self.imagecache filename))
(fn SlideshowView.justify [self element width]
(match element.justify
:center (/ (- self.size.x width) 2)
:right (- self.size.x width style.padding.x)
_ style.padding.x))
(fn SlideshowView.this-y [self element y]
(if element.topPadding (+ y element.topPadding)
(+ y style.padding.y)))
(fn SlideshowView.next-y [self element height y]
(if element.lowerPadding (+ y height element.lowerPadding)
element.overlay y
(+ y height style.padding.y)))
(fn SlideshowView.word-wrap [self element]
(let [letter-width (element.font:get_width "m")
screen-width (- self.size.x style.padding.x style.padding.x)
max-letters (math.floor (/ screen-width letter-width))
wrapped (lume.wordwrap element.text max-letters)
lines (icollect [line (string.gmatch wrapped "([^\n]+)")] line)]
lines))
2021-06-19 01:31:21 +00:00
(fn SlideshowView.render-element [self element y]
2021-06-22 02:09:09 +00:00
(if element.text
(let [lines (self:word-wrap element)
line-height (element.font:get_height)
full-height (+ (* line-height (length lines)) (* style.padding.y (- (length lines) 1)))]
(each [iline line (ipairs lines)]
(let [width (element.font:get_width line) ;; todo: word-wrapping
x (+ self.position.x (self:justify element width))
yline (+ y (* (+ (element.font:get_height) style.padding.y) (- iline 1)))]
(renderer.draw_text element.font line x yline element.color)))
(self:next-y element full-height y))
element.image
(let [image (self:load-image element)
x (+ self.position.x (self:justify element (image:getWidth)))]
(love.graphics.setColor 1 1 1 element.alpha)
(love.graphics.draw image x y)
(self:next-y element (image:getHeight) y))
y))
2021-06-19 01:31:21 +00:00
(fn SlideshowView.draw [self]
(self:draw_background style.background)
2021-06-22 02:09:09 +00:00
(var y self.position.y)
2021-06-19 01:31:21 +00:00
(each [ielem element (ipairs (. self.slides self.islide)) :until (> ielem self.ielem)]
2021-06-22 02:09:09 +00:00
(set y (self:render-element element (self:this-y element y)))))
(fn SlideshowView.get_name [self] "] KFest 2021")
2021-06-19 01:31:21 +00:00
SlideshowView