summaryrefslogtreecommitdiff
path: root/gui.go
blob: fff4fd61d3268728507a3dbfd8d41936dce0bee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main

import (
	"bytes"
	_ "embed"
	"image"
	_ "image/png"
	"os"
	"strconv"
	"time"

	g "github.com/AllenDang/giu"
	"github.com/AllenDang/imgui-go"
	"github.com/ncruces/zenity"
)

//go:generate goversioninfo -64 -o resource_amd64.syso res/versioninfo.json

//go:embed res/monitor_48.png
var icon []byte

//go:embed res/icomoon.ttf
var icomoon []byte
var icomoonFI *g.FontInfo

const comboSize = 120

type entryCfg struct {
	inputFile string
	outputDir string
	gender    Gender
	ageGroup  AgeGroup
	weapon    Weapon
}

var (
	header struct {
		name        string
		description string
		date        time.Time
		targetDir   string
	}
	entries []entryCfg
)

type GridLayout struct {
	widgets []g.Widget
	labels  []*g.LabelWidget
}

type GridLine struct {
	label  string
	widget g.Widget
}

func Line(label string, widget g.Widget) GridLine {
	return GridLine{label, widget}
}

const gridPadding = 10

var gridSize float32 = gridPadding

func Grid(lines ...GridLine) *GridLayout {
	widgets := make([]g.Widget, len(lines))
	labels := make([]*g.LabelWidget, len(lines))

	for i, line := range lines {
		labelSize, _ := g.CalcTextSize(line.label)
		if labelSize+gridPadding > gridSize {
			gridSize = labelSize + gridPadding
		}

		labels[i] = g.Label(line.label)
		widgets[i] = line.widget
	}

	return &GridLayout{widgets, labels}
}

func (grid *GridLayout) Build() {
	for i := range grid.labels {
		g.AlignTextToFramePadding()
		grid.labels[i].Build()
		imgui.SameLineV(gridSize, 0)
		grid.widgets[i].Build()
	}
}

func Layout(widgets ...g.Widget) g.Layout {
	return widgets
}

func PushID(id string) g.Widget {
	return g.Custom(func() { imgui.PushID(id) })
}

func PopID() g.Widget {
	return g.Custom(imgui.PopID)
}

func buildEntry(idx int) g.Widget {
	entry := &entries[idx]

	return Layout(
		PushID(strconv.Itoa(idx)),
		g.Spacing(),
		g.Separator(),
		g.Spacing(),
		Grid(
			Line("Waffe", g.Combo("",
				entry.weapon.String(), WeaponStrings, (*int32)(&entry.weapon)).
				Size(comboSize)),
			Line("Geschlecht", g.Combo("",
				entry.gender.String(), GenderStrings, (*int32)(&entry.gender)).
				Size(comboSize)),
			Line("Altersklasse", g.Combo("",
				entry.ageGroup.String(), AgeGroupStrings, (*int32)(&entry.ageGroup)).
				Size(comboSize)),
			Line("Ophardt-Export", g.Row(
				g.InputText(&entry.inputFile),
				g.Button("Wähle...").OnClick(func() {
					file, err := zenity.SelectFile(zenity.FileFilters{
						{Name: "CSV Files", Patterns: []string{"*.csv"}},
					})
					if err == nil && file != "" {
						entry.inputFile = file
					}
				}))),
		),
		PopID())
}

func entryBuilder() g.Widget {
	const id = "entries"

	return g.Custom(func() {
		imgui.PushID(id)
		defer imgui.PopID()

		for i := range entries {
			buildEntry(i).Build()
		}
	})
}

func shouldQuit() {
	g.Context.GetPlatform().SetShouldStop(true)
}

func loop() {
	g.SingleWindow().Layout(
		g.Align(g.AlignCenter).To(g.Label("Engarde Importer")),
		g.Spacing(),
		Grid(
			Line("Name", g.InputText(&header.name)),
			Line("Beschreibung", g.InputText(&header.description)),
			Line("Wettkampftag", g.DatePicker("##date", &header.date).
				Format("02.01.2006").StartOfWeek(time.Monday).
				Size(comboSize)),
			Line("Zielverzeichnis", g.Row(
				g.InputText(&header.targetDir),
				g.Button("Wähle...").OnClick(func() {
					dir, err := zenity.SelectFile(zenity.Directory(), zenity.Filename(header.targetDir+"/"))
					if err == nil && dir != "" {
						header.targetDir = dir
					}
				}))),
		),
		entryBuilder(),
		g.Style().SetFont(icomoonFI).To(
			g.Button("\ue900").OnClick(func() {
				entries = append(entries, entryCfg{})
			})),
		g.Align(g.AlignCenter).To(g.Button("Quit").OnClick(shouldQuit)),
	)
}

func gui() {
	header.date = time.Now()
	entries = make([]entryCfg, 1)
	header.targetDir, _ = os.UserHomeDir()

	icomoonFI = g.AddFontFromBytes("icomoon", icomoon, 16)
	w := g.NewMasterWindow("Engarde Importer", 500, 400, 0)

	if img, _, err := image.Decode(bytes.NewReader(icon)); err == nil {
		w.SetIcon([]image.Image{img})
	}

	w.Run(loop)
}