Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / timeline_pane.luc

## The timeline along the window's bottom: icon buttons for the first frame,
## a step back, play backwards, play (pause while playing), a step on and
## the last frame, the current frame, the playbar (timeline_bar.luc), the
## range and the frames per second, and looping. A button's tooltip shows
## its command and shortcut.
from luce_gpu.gpu import Rect
from luce_ui.ui import HStack, Button, ButtonStyle, Font, NumberField, Text, Command
from interop import Connection
from workspace import Workspace
from actions import Actions
from timeline_bar import TimelineBar
import timeline_commands

pub class TimelinePane:
    pub let body: HStack
    pub let bar: TimelineBar
    let w: Workspace
    let actions: Actions
    let frame: NumberField
    let start: NumberField
    let end: NumberField
    let fps: NumberField
    ## The icon buttons and the command each triggers.
    let buttons: list[Button]
    let commands: list[Command]
    let play: Button
    let play_backwards: Button
    let loop: Button
    ## The icons the play buttons show now.
    var play_icon: str = "play"
    var back_icon: str = "play-backwards"
    ## Told when playing starts (the editor draws frames while it plays).
    pub var on_play: func() -> unit!? = none
    let connections: list[Connection]

    pub func init(self, w: Workspace, actions: Actions, font: Font) -> unit!:
        self.w = w
        self.actions = actions
        self.connections = []
        self.bar = TimelineBar(w)
        self.frame = NumberField(1.0, -100000.0, 100000.0, 1.0, 0, font = font, cells = 5.0)
        self.start = NumberField(1.0, -100000.0, 100000.0, 1.0, 0, font = font, cells = 5.0)
        self.end = NumberField(240.0, -100000.0, 100000.0, 1.0, 0, font = font, cells = 5.0)
        self.fps = NumberField(24.0, 1.0, 1000.0, 1.0, 2, suffix = " fps", font = font, cells = 6.0)
        self.connections = [actions.play.on_trigger(func () -> unit!: self.toggle_play()),
            actions.play_backwards.on_trigger(func () -> unit!: self.toggle_play(true)),
            actions.previous_frame.on_trigger(func () -> unit!: timeline_commands.step(w, -1.0)),
            actions.next_frame.on_trigger(func () -> unit!: timeline_commands.step(w, 1.0)),
            actions.first_frame.on_trigger(func () -> unit!: timeline_commands.set_frame(w, w.network.start)),
            actions.last_frame.on_trigger(func () -> unit!: timeline_commands.set_frame(w, w.network.end)),
            actions.looping.on_trigger(self.toggle_loop),
            self.frame.on_change(func (value: float) -> unit!: timeline_commands.set_frame(w, value)),
            self.start.on_change(func (value: float) -> unit!: timeline_commands.set_range(w, value, w.network.end)),
            self.end.on_change(func (value: float) -> unit!: timeline_commands.set_range(w, w.network.start, value)),
            self.fps.on_change(func (value: float) -> unit!: timeline_commands.set_fps(w, value))]
        actions.looping.set_checked(true)
        # Icon buttons that trigger the commands (luce-ui's playback icons).
        self.buttons = []
        self.commands = []
        for (icon, command) in [("go-to-start", actions.first_frame), ("step-back", actions.previous_frame), ("play-backwards", actions.play_backwards),
                                ("play", actions.play), ("step-forward", actions.next_frame), ("go-to-end", actions.last_frame), ("repeat", actions.looping)]:
            let button = Button(icon = icon, font = font)
            button.set_style(ButtonStyle.plain)
            let shortcut = command.shortcut()
            button.set_tooltip(command.text(), shortcut.text() if shortcut.is_bound() else "")
            let chosen = command
            self.connections.append(button.on_click(func () -> unit!: chosen.trigger()))
            self.buttons.append(button)
            self.commands.append(command)
        self.play_backwards = self.buttons[2]
        self.play = self.buttons[3]
        self.loop = self.buttons[6]
        let b = self.buttons
        self.body = HStack([b[0], b[1], b[2], b[3], b[4], b[5], self.frame, self.bar, Text("Start", font = font, muted = true), self.start, Text("End", font = font, muted = true), self.end, self.fps, b[6]], spacing = 2.0, padding = 4.0)
        self.show_state()

    func toggle_play(self, backwards: bool = false) -> unit!:
        self.bar.toggle(backwards)
        self.show_state()
        if self.bar.playing:
            if let play = self.on_play:
                play()

    func toggle_loop(self) -> unit!:
        self.bar.looping = not self.bar.looping
        self.show_state()

    ## The play buttons show pause while playing their way; Loop is lit
    ## while on.
    func show_state(self) -> unit!:
        let forward = self.bar.playing and not self.bar.backwards
        let backward = self.bar.playing and self.bar.backwards
        self.actions.play.set_checked(forward)
        self.actions.play_backwards.set_checked(backward)
        self.actions.looping.set_checked(self.bar.looping)
        let play_icon = "pause" if forward else "play"
        let back_icon = "pause" if backward else "play-backwards"
        if self.play_icon != play_icon:
            self.play_icon = play_icon
            self.play.set_icon(play_icon)
        if self.back_icon != back_icon:
            self.back_icon = back_icon
            self.play_backwards.set_icon(back_icon)
        self.loop.set_style(ButtonStyle.selected if self.bar.looping else ButtonStyle.plain)

    ## One of the clock's frames while playing: the timeline moves on (it
    ## stops at the end unless it loops, and the play buttons say so).
    pub func play_frame(self) -> unit!:
        self.bar.poll()
        if not self.bar.playing:
            self.show_state()

    ## The fields kept to the timeline (after an undo, a project opened, a
    ## frame stepped) and the buttons to playing and looping.
    pub func sync(self) -> unit!:
        let g = self.w.network
        for (field, value) in [(self.frame, g.frame), (self.start, g.start), (self.end, g.end), (self.fps, g.fps)]:
            if field.value() != value and not field.layout().is_focused():
                field.set_value(value)
        self.bar.show_frame()
        self.show_state()

    ## Button `index`'s place in the window (tests).
    pub func button_bounds(self, index: int) -> Rect:
        return self.buttons[index].layout().bounds()

    ## Button `index`'s tooltip text while it shows, else "" (tests).
    pub func tooltip_text(self, index: int) -> str:
        return self.buttons[index].shown_tooltip()

    pub func close(self):
        self.on_play = none
        for connection in self.connections:
            connection.disconnect()
        self.connections.clear()