/* Copyright © Brian Buller Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package widgets import ( "github.com/gdamore/tcell" ) type RelativeLayout struct { id string style tcell.Style x, y int w, h int widgetRelations map[Widget][]widgetRelation widgets []Widget active bool visible bool focusable bool keyMap KeyMap } var _ Widget = (*RelativeLayout)(nil) type widgetRelation struct { relation RelativeRelation relTo Widget } type RelativeRelation int const ( RelRelAbove = iota // Above Widget RelRelToRightOf // To Right of Widget RelRelBelow // Below Widget RelRelToLeftOf // To Left of Widget RelAncTL // Anchored to parent Top-Left RelAncT // Anchored to parent Top RelAncTR // Anchored to parent Top-Right RelAncL // Anchored to parent Left RelAncC // Anchored to parent Center RelAncR // Anchored to parent Right RelAncBL // Anchored to parent Bottom-Left RelAncB // Anchored to parent Bottom RelAncBR // Anchored to parent Bottom-Right ) func NewRelativeLayout(id string, s tcell.Style) *RelativeLayout { ret := &RelativeLayout{} ret.Init(id, s) return ret } func (w *RelativeLayout) Init(id string, style tcell.Style) { w.id = id w.style = style w.visible = true w.focusable = true w.widgetRelations = make(map[Widget][]widgetRelation) w.keyMap = BlankKeyMap() } func (w *RelativeLayout) Id() string { return w.id } func (w *RelativeLayout) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() // TODO: Trickle-down HandleResize } func (w *RelativeLayout) SetKeyMap(km KeyMap, def bool) { w.keyMap = km } func (w *RelativeLayout) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) } func (w *RelativeLayout) RemoveFromKeyMap(km KeyMap) { for k := range km.Keys { w.keyMap.Remove(k) } for r := range km.Runes { w.keyMap.RemoveRune(r) } } func (w *RelativeLayout) HandleKey(ev *tcell.EventKey) bool { return w.keyMap.Handle(ev) } func (w *RelativeLayout) HandleTime(ev *tcell.EventTime) { for i := range w.widgets { w.widgets[i].HandleTime(ev) } } func (w *RelativeLayout) Draw(screen tcell.Screen) { if !w.visible { return } // All widgets should have correct (relative) positions /* done := make(map[Widget]widgetRelation) rem := make(map[Widget]widgetRelation) for k, v := range w.widgetRelations { if v.relTo == w { done[k] = v } else { rem[k] = v } } */ } func (w *RelativeLayout) Active() bool { return w.active } func (w *RelativeLayout) SetActive(a bool) { w.active = a } func (w *RelativeLayout) Visible() bool { return w.visible } func (w *RelativeLayout) SetVisible(a bool) { w.visible = a } func (w *RelativeLayout) Focusable() bool { return w.focusable } func (w *RelativeLayout) SetFocusable(b bool) { w.focusable = b } func (w *RelativeLayout) SetX(x int) { w.x = x } func (w *RelativeLayout) SetY(y int) { w.y = y } func (w *RelativeLayout) GetX() int { return w.x } func (w *RelativeLayout) GetY() int { return w.y } func (w *RelativeLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y } func (w *RelativeLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} } func (w *RelativeLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y } func (w *RelativeLayout) SetW(wd int) { w.w = wd } func (w *RelativeLayout) SetH(h int) { w.h = h } func (w *RelativeLayout) GetW() int { return w.w } func (w *RelativeLayout) GetH() int { return w.h } func (w *RelativeLayout) WantW() int { return 1 } func (w *RelativeLayout) WantH() int { return 1 } func (w *RelativeLayout) MinW() int { // Find the highest value for x in all widgets GetX() + MinW() var minW int for _, wd := range w.widgets { wrk := wd.GetX() + wd.MinW() if wrk > minW { minW = wrk } } return minW } func (w *RelativeLayout) MinH() int { // Find the highest value for y in all widgets GetY() + MinH() var minH int for _, wd := range w.widgets { wrk := wd.GetY() + wd.MinH() if wrk > minH { minH = wrk } } return minH } func (w *RelativeLayout) Add(n Widget, relTo Widget, relation RelativeRelation) { w.widgetRelations[n] = append(w.widgetRelations[n], widgetRelation{ relation: relation, relTo: relTo, }) } func (w *RelativeLayout) AddRelation(n Widget, relation RelativeRelation, relTo Widget) { }