dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / 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. Hovering a button shows its
## command and shortcut.
from luce_gpu.gpu import Rect
from luce_ui.ui import HStack, Button, ButtonStyle, Font, NumberField, Text, Tooltip, Command
from interop import Connection
from workspace import Workspace
from actions import Actions
from timeline_bar import TimelineBar
import timeline_commands
import time
## Seconds the pointer rests on a button before its tooltip shows.
let tooltip_delay: float = 0.5
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 (hovered: its tooltip).
let buttons: list[Button]
let commands: list[Command]
let play: Button
let play_backwards: Button
let loop: Button
let tooltip: Tooltip
## The button under the pointer (-1 none) and since when (ns).
var hovered: int = -1
## The icons the play buttons show now.
var play_icon: str = "play"
var back_icon: str = "play-backwards"
var hovered_at: int = 0
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 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]
self.tooltip = Tooltip(font)
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], self.tooltip], spacing = 2.0, padding = 4.0)
self.show_state()
func toggle_play(self, backwards: bool = false) -> unit!:
self.bar.toggle(backwards)
self.show_state()
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)
## The tooltip of the button the pointer rests on: its command and
## shortcut, above it, after a moment.
func follow_hover(self) -> unit!:
var over = -1
for at in 0..<self.buttons.length:
if self.buttons[at].layout().contains_pointer(): over = at
let now = time.now()
if over != self.hovered:
self.hovered = over
self.hovered_at = now
self.tooltip.hide()
return
if over < 0 or self.tooltip.is_open() or float(now - self.hovered_at) / 1000000000.0 < tooltip_delay:
return
self.show_tooltip(over)
## Button `index`'s tooltip now: its command and shortcut, above it.
pub func show_tooltip(self, index: int) -> unit!:
let over = index
let command = self.commands[over]
let shortcut = command.shortcut()
# Above the button, in the row's points.
let bounds = self.buttons[over].layout().bounds()
let row = self.body.layout().bounds()
self.tooltip.show(command.text(), shortcut.text() if shortcut.is_bound() else "", bounds.x - row.x, bounds.y - row.y - 44.0)
## Each frame of the app's clock: playback, and the fields kept to the
## timeline (an undo, a project opened).
pub func poll(self) -> unit!:
self.bar.poll()
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.show_state()
self.follow_hover()
## Button `index`'s place in the window (tests).
pub func button_bounds(self, index: int) -> Rect:
return self.buttons[index].layout().bounds()
## The tooltip's text while it shows, else "" (tests).
pub func tooltip_text(self) -> str:
return self.tooltip.text() if self.tooltip.is_open() else ""
pub func close(self):
for connection in self.connections:
connection.disconnect()
self.connections.clear()