1// Copyright 2016 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2// Use of this source code is governed by a MIT license that can
3// be found in the LICENSE file.
4
5package termui
6
7import "image"
8
9// Align is the position of the gauge's label.
10type Align uint
11
12// All supported positions.
13const (
14	AlignNone Align = 0
15	AlignLeft Align = 1 << iota
16	AlignRight
17	AlignBottom
18	AlignTop
19	AlignCenterVertical
20	AlignCenterHorizontal
21	AlignCenter = AlignCenterVertical | AlignCenterHorizontal
22)
23
24func AlignArea(parent, child image.Rectangle, a Align) image.Rectangle {
25	w, h := child.Dx(), child.Dy()
26
27	// parent center
28	pcx, pcy := parent.Min.X+parent.Dx()/2, parent.Min.Y+parent.Dy()/2
29	// child center
30	ccx, ccy := child.Min.X+child.Dx()/2, child.Min.Y+child.Dy()/2
31
32	if a&AlignLeft == AlignLeft {
33		child.Min.X = parent.Min.X
34		child.Max.X = child.Min.X + w
35	}
36
37	if a&AlignRight == AlignRight {
38		child.Max.X = parent.Max.X
39		child.Min.X = child.Max.X - w
40	}
41
42	if a&AlignBottom == AlignBottom {
43		child.Max.Y = parent.Max.Y
44		child.Min.Y = child.Max.Y - h
45	}
46
47	if a&AlignTop == AlignRight {
48		child.Min.Y = parent.Min.Y
49		child.Max.Y = child.Min.Y + h
50	}
51
52	if a&AlignCenterHorizontal == AlignCenterHorizontal {
53		child.Min.X += pcx - ccx
54		child.Max.X = child.Min.X + w
55	}
56
57	if a&AlignCenterVertical == AlignCenterVertical {
58		child.Min.Y += pcy - ccy
59		child.Max.Y = child.Min.Y + h
60	}
61
62	return child
63}
64
65func MoveArea(a image.Rectangle, dx, dy int) image.Rectangle {
66	a.Min.X += dx
67	a.Max.X += dx
68	a.Min.Y += dy
69	a.Max.Y += dy
70	return a
71}
72
73var termWidth int
74var termHeight int
75
76func TermRect() image.Rectangle {
77	return image.Rect(0, 0, termWidth, termHeight)
78}
79