v2 / examples / gg / drag_n_drop.v
61 lines · 52 sloc · 1.2 KB · bbb61ab3687afe512a1fa12492c876d011626107
Raw
1module main
2
3import gg
4import sokol.sapp
5
6const max_files = 12
7const text_size = 16
8
9struct App {
10mut:
11 gg &gg.Context = unsafe { nil }
12 dropped_file_list []string
13}
14
15fn main() {
16 mut app := &App{}
17 app.gg = gg.new_context(
18 bg_color: gg.rgb(174, 198, 255)
19 width: 600
20 height: 400
21 window_title: 'Drag and drop'
22 frame_fn: frame
23 user_data: app
24 event_fn: my_event_manager
25 // drag & drop
26 enable_dragndrop: true
27 max_dropped_files: max_files
28 max_dropped_file_path_length: 2048
29 )
30 app.gg.run()
31}
32
33fn my_event_manager(mut ev gg.Event, mut app App) {
34 // drag&drop event
35 if ev.typ == .files_dropped {
36 num_dropped := sapp.get_num_dropped_files()
37 app.dropped_file_list.clear()
38 for i in 0 .. num_dropped {
39 app.dropped_file_list << sapp.get_dropped_file_path(i)
40 }
41 }
42}
43
44fn frame(mut app App) {
45 app.gg.begin()
46
47 mut txt_conf := gg.TextCfg{
48 color: gg.black
49 align: .left
50 size: int(text_size * app.gg.scale + 0.5)
51 }
52 app.gg.draw_text(12, 12, 'Drag&Drop here max ${max_files} files.', txt_conf)
53
54 mut y := 40
55 for c, f in app.dropped_file_list {
56 app.gg.draw_text(12, y, '[${c}] ${f}', txt_conf)
57 y += text_size
58 }
59
60 app.gg.end()
61}
62