-------------------------------------------------------------------------------- --! @title Lottie Controls --! @brief Dress knobs, buttons and a meter with vector animations --! @category UIHelpers --! --! A Lottie animation is a vector movie: one file holds every frame of a control, --! and the widget picks the frame that matches its state. Where a strip image --! needs one bitmap per state and one per screen density, a Lottie is resolution --! independent and stays sharp at any size. --! --! Which frame shows depends on the widget: --! - a knob or a slider maps its value across the whole animation, so frame 0 --! is the minimum and the last frame the maximum; --! - a button runs the animation on click -- "toggle" walks to the last frame --! when switched on and back down when switched off, "trigger" replays from --! the first frame every time; --! - a meter maps the level it displays, in dB, over the frames. --! --! A `.lottie` file is a package -- one or more reels and a state machine zipped --! together. When one declares a machine it runs on its own, and that is what --! gives the widget its hovered and pressed look: the two below recolour --! themselves as the pointer arrives and presses, while the click or the value --! still drives the frame. Nothing in the script asks for it, and nothing in the --! script can steer it either. --! --! An animation is fitted inside the widget with its own proportions kept, so a --! widget shaped unlike its asset leaves empty room: `button_sm.lottie` is square, --! and in a wide short button it would only be as tall as the button. Size the --! widget like the file. --! --! Paths resolve where images do: next to the script, then next to the Program. --! An animation always wins over a strip image or the separate state images on --! the same widget, so give a widget one or the other. --! --! @demonstrates Knob, Slider, OnOffButton, Button, AudioMeter, Panel, Label --! @asset knob.json Knob face, frame 0 at the minimum --! @asset slider.json Horizontal slider: groove, fill and handle --! @asset toggle.json Two-state switch --! @asset trigger.json One-shot flash for a push button --! @asset vu.json VU needle driven by the meter level --! @asset button_sm.lottie Packaged button whose state machine adds hover and press --! @asset knob_sm.lottie Packaged knob whose state machine highlights on hover -------------------------------------------------------------------------------- setBackgroundColour("14141a") local function caption(parent, text, x, y, w) local label = parent:Label{"cap_" .. text, bounds = {x, y, w, 14}} label.text = text label.fontSize = 10 label.textColour = "7f8c8d" label.align = "centred" return label end local panel = Panel{"lottie", bounds = {10, 10, 560, 305}, backgroundColour = "1e1e28"} -------------------------------------------------------------------------------- -- Knob: the value picks the frame, so no mode and no frame count to declare. -- The property form is enough wherever the default behaviour is wanted. -------------------------------------------------------------------------------- local cutoff = panel:Knob{"Cutoff", 0.5, 0, 1, bounds = {20, 24, 90, 90}, unit = Unit.PercentNormalized, lottieAnimation = "knob.json" } caption(panel, "Knob", 20, 116, 90) -------------------------------------------------------------------------------- -- Slider: same rule, and the animation replaces handle, groove and fill at once. -- The asset is drawn wide, so give the widget bounds of the same proportion. -------------------------------------------------------------------------------- local depth = panel:Slider{"Depth", 0.3, 0, 1, bounds = {130, 46, 180, 48}, unit = Unit.PercentNormalized, showLabel = false } depth:setLottieAnimation("slider.json") caption(panel, "Slider", 130, 116, 180) -------------------------------------------------------------------------------- -- Toggle button: "toggle" is the default, so the property form says it all. -------------------------------------------------------------------------------- local active = panel:OnOffButton{"Active", true, bounds = {330, 34, 70, 70}, lottieAnimation = "toggle.json" } caption(panel, "OnOffButton", 330, 116, 70) active.changed = function(self) print("Active: " .. tostring(self.value)) end -------------------------------------------------------------------------------- -- Push button: a one-shot needs the mode, which only the call form takes. -------------------------------------------------------------------------------- local strike = panel:Button{"Strike", bounds = {420, 34, 70, 70}} strike:setLottieAnimation("trigger.json", "trigger") caption(panel, "Button", 420, 116, 70) strike.changed = function() print("Strike") end -------------------------------------------------------------------------------- -- Meter: the frame follows the level in dB, not a parameter. -------------------------------------------------------------------------------- local meter = panel:AudioMeter{"Out", Program, true, 0, true, bounds = {508, 20, 26, 170}, -- vu.json is 30 x 200: keep the widget as narrow displayAsMono = true, lottieAnimation = "vu.json" } caption(panel, "Meter", 491, 194, 60) -------------------------------------------------------------------------------- -- Packages: same call, but these two declare a state machine, so they answer the -- mouse -- hover and press -- with no script and no extra property. A plain -- .json cannot: pointer events only reach an animation while its machine runs. -------------------------------------------------------------------------------- local packaged = panel:OnOffButton{"Packaged", false, bounds = {40, 140, 80, 80}, lottieAnimation = "button_sm.lottie" } caption(panel, "hover + press", 20, 224, 120) local tone = panel:Knob{"Tone", 0.6, 0, 1, bounds = {180, 140, 80, 80}, unit = Unit.PercentNormalized, showValue = false, lottieAnimation = "knob_sm.lottie" } caption(panel, "hover", 180, 224, 80) setSize(580, 330) makePerformanceView()