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