vendetta / examples / host_proto / main.v
117 lines · 112 sloc · 3.42 KB · e566477c350a80f9666b93ab0d991ad14f06df91
Raw
1// host_proto is a dummy host app: its own header and sidebar, with the Vendetta
2// widget in a frame inside the window. The sidebar checkbox switches the
3// widget's input scope between its frame and the whole window.
4//
5// v examples/host_proto/main.v [docked]
6//
7// It plays assets/video/test.mp4 from the working directory. The repository
8// ships no clip, so put one there first.
9import libvendetta
10import os
11import ui2
12
13const header_h = 40.0
14const sidebar_w = 220.0
15
16struct Host {
17mut:
18 player &libvendetta.Player
19 localised bool = true
20}
21
22fn (mut h Host) view() ui2.Element {
23 b := ui2.bounds()
24 widget := h.player.build(ui2.rect(sidebar_w, header_h, b.width - sidebar_w, b.height - header_h))
25 text := ui2.TextStyle{
26 color: 0xe5e7eb
27 size: 13
28 valign: .middle
29 }
30 scope := if h.localised { 'frame' } else { 'window' }
31 mut children := []ui2.Element{}
32 children << ui2.view('host_header', ui2.rect(0, 0, b.width, header_h), ui2.BoxStyle{
33 bg: 0x1f2937
34 }, [])
35 children << ui2.label('host_title', 'dummy host app', ui2.rect(12, 0, 300, header_h), ui2.TextStyle{
36 color: 0xf9fafb
37 size: 15
38 valign: .middle
39 })
40 children << ui2.view('host_sidebar', ui2.rect(0, header_h, sidebar_w, b.height - header_h), ui2.BoxStyle{
41 bg: 0x111827
42 }, [])
43 children << ui2.label('host_input', 'input scope: ${scope}', ui2.rect(12, header_h + 12, sidebar_w - 24, 18), text)
44 children << ui2.checkbox('host_scope', 'localise input', h.localised, ui2.rect(12, header_h + 40, sidebar_w - 24, 24), text)
45 children << ui2.button('host_pause', 'pause (host)', ui2.rect(12, header_h + 76, sidebar_w - 24, 28), ui2.BoxStyle{
46 bg: 0x3b82f6
47 radius: 6
48 }, ui2.TextStyle{
49 color: 0xffffff
50 size: 13
51 align: .center
52 valign: .middle
53 })
54 children << ui2.checkbox('host_overlay', 'floating controls', h.player.cfg.overlay, ui2.rect(12, header_h + 116, sidebar_w - 24, 24), text)
55 children << widget
56 return ui2.screen(0x0b1120, children)
57}
58
59fn (mut h Host) handle(event string) {
60 match event {
61 'host_scope' {
62 h.localised = !h.localised
63 h.player.set_input_scope(if h.localised {
64 libvendetta.InputScope.frame
65 } else {
66 libvendetta.InputScope.window
67 })
68 }
69 'host_overlay' {
70 h.player.set_overlay(!h.player.cfg.overlay)
71 }
72 'host_pause' {
73 h.player.toggle_play()
74 }
75 else {
76 h.player.handle(event)
77 }
78 }
79}
80
81fn main() {
82 if !os.exists('assets/video/test.mp4') {
83 eprintln('no test video found (expected assets/video/test.mp4 in the working directory)')
84 exit(1)
85 }
86 mut player := libvendetta.new(libvendetta.Config{})
87 player.load('assets/video/test.mp4')
88 if os.args.contains('docked') {
89 player.set_overlay(false)
90 }
91 // Every forwarded event goes to the player. It decides, per its input
92 // scope, whether the event is for the widget.
93 ui2.set_gl_surface_renderer(fn [mut player] (id string, frame ui2.Rect, scale f64, height f64) {
94 player.render(id, frame, scale, height)
95 })
96 ui2.on_pointer_move(fn [mut player] (x f64, y f64) {
97 player.pointer_moved(x, y)
98 })
99 ui2.on_mouse_button(fn [mut player] (button int, down bool, x f64, y f64) {
100 player.mouse_button(button, down, x, y)
101 })
102 ui2.on_wheel(fn [mut player] (dx f64, dy f64) {
103 player.wheel(dx, dy)
104 })
105 ui2.on_key(fn [mut player] (key string) {
106 player.key(key)
107 })
108 mut host := &Host{
109 player: player
110 }
111 ui2.run_window('vendetta in a dummy host', 960, 600, fn [mut host] () ui2.Element {
112 return host.view()
113 }, fn [mut host] (event string) {
114 host.handle(event)
115 })
116 player.destroy()
117}
118