/* 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 widgetRelations map[Widget]widgetRelation active bool visible bool x, y int w, h int } var _ Widget = (*RelativeLayout)(nil) type widgetRelation struct { relation RelativeRelation relTo Widget } type RelativeRelation int const ( RRAbove = iota // Above Widget RRToRightOf // To Right of Widget RRBelow // Below Widget RRToLeftOf // To Left of Widget RATop // Anchored to parent Top RARight // Anchored to parent Right RABottom // Anchored to parent Bottom RALeft // Anchored to parent Left ) 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 } func (w *RelativeLayout) Id() string { return w.id } func (w *RelativeLayout) HandleResize(ev *tcell.EventResize) {} func (w *RelativeLayout) HandleKey(ev *tcell.EventKey) bool { return false } func (w *RelativeLayout) HandleTime(ev *tcell.EventTime) {} func (w *RelativeLayout) Draw(screen tcell.Screen) { if !w.visible { return } 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 true } 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) 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) SetSize(c Coord) { w.w, w.h = c.X, c.Y } func (w *RelativeLayout) Add(n Widget, relTo Widget, relation RelativeRelation) { w.widgetRelations[n] = widgetRelation{ relation: relation, relTo: relTo, } }