2017-12-26 22:19:42 -07:00
|
|
|
package exhibit
|
|
|
|
|
2017-12-28 19:21:01 -07:00
|
|
|
import (
|
|
|
|
"image"
|
2017-12-30 14:42:13 -07:00
|
|
|
"sync"
|
2017-12-28 19:21:01 -07:00
|
|
|
)
|
|
|
|
|
2017-12-26 22:19:42 -07:00
|
|
|
type WindowWidget struct {
|
2017-12-30 14:42:13 -07:00
|
|
|
blockLock sync.Mutex
|
|
|
|
block Block
|
|
|
|
|
|
|
|
borderLock sync.Mutex
|
2017-12-30 03:23:20 -07:00
|
|
|
border Border
|
2017-12-30 14:42:13 -07:00
|
|
|
|
|
|
|
widgetLock sync.Mutex
|
2017-12-30 03:23:20 -07:00
|
|
|
widgets []Widget
|
2017-12-26 22:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WindowWidget) AddWidget(widget Widget) {
|
2017-12-30 14:42:13 -07:00
|
|
|
w.widgetLock.Lock()
|
|
|
|
defer w.widgetLock.Unlock()
|
|
|
|
|
2017-12-26 22:19:42 -07:00
|
|
|
if w.widgets == nil {
|
|
|
|
w.widgets = make([]Widget, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.widgets = append(w.widgets, widget)
|
|
|
|
}
|
|
|
|
|
2017-12-30 03:23:20 -07:00
|
|
|
func (w WindowWidget) Size() image.Point {
|
2018-01-02 00:11:56 -07:00
|
|
|
w.blockLock.Lock()
|
|
|
|
defer w.blockLock.Unlock()
|
|
|
|
|
|
|
|
return w.block.Size()
|
2017-12-27 17:03:14 -07:00
|
|
|
}
|
|
|
|
|
2017-12-30 03:23:20 -07:00
|
|
|
func (w *WindowWidget) SetSize(p image.Point) {
|
2018-01-02 00:11:56 -07:00
|
|
|
w.blockLock.Lock()
|
|
|
|
defer w.blockLock.Unlock()
|
|
|
|
|
2017-12-30 03:23:20 -07:00
|
|
|
w.block.SetSize(p)
|
2017-12-27 17:03:14 -07:00
|
|
|
}
|
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
func (w *WindowWidget) Origin() image.Point {
|
|
|
|
w.blockLock.Lock()
|
|
|
|
defer w.blockLock.Unlock()
|
2017-12-28 01:10:37 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
return w.block.Rect.Min
|
|
|
|
}
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
func (w *WindowWidget) SetOrigin(p image.Point) {
|
|
|
|
w.blockLock.Lock()
|
|
|
|
defer w.blockLock.Unlock()
|
2017-12-28 01:10:37 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
w.block.SetOrigin(p)
|
|
|
|
}
|
2017-12-28 01:10:37 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
func (w *WindowWidget) Border() Border {
|
|
|
|
w.borderLock.Lock()
|
|
|
|
defer w.borderLock.Unlock()
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
return w.border
|
|
|
|
}
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
func (w *WindowWidget) SetBorder(b Border) {
|
|
|
|
w.borderLock.Lock()
|
|
|
|
defer w.borderLock.Unlock()
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
w.border = b
|
2017-12-26 22:19:42 -07:00
|
|
|
}
|
2017-12-28 01:10:37 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
func (w *WindowWidget) Render(origin image.Point) Block {
|
2018-01-02 00:11:56 -07:00
|
|
|
w.blockLock.Lock()
|
|
|
|
defer w.blockLock.Unlock()
|
2017-12-30 14:42:13 -07:00
|
|
|
|
|
|
|
w.widgetLock.Lock()
|
|
|
|
defer w.widgetLock.Unlock()
|
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
if w.block.Rect.Size().X == 0 || w.block.Rect.Size().Y == 0 {
|
|
|
|
return NewBlock(0, 0, 0, 0)
|
|
|
|
}
|
2017-12-30 14:42:13 -07:00
|
|
|
|
|
|
|
var borderAdj image.Point
|
|
|
|
border := w.Border()
|
|
|
|
if border.Visible {
|
|
|
|
borderAdj = image.Pt(1, 1)
|
|
|
|
}
|
2017-12-28 01:10:37 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
block := NewBlock(0, 0, 0, 0)
|
|
|
|
block.SetSize(w.block.Rect.Size())
|
|
|
|
block.SetOrigin(origin.Add(w.block.Origin()))
|
|
|
|
|
|
|
|
for _, widget := range w.widgets {
|
|
|
|
cells := widget.Render(block.Origin().Add(borderAdj)).Cells
|
|
|
|
|
|
|
|
for k, v := range cells {
|
|
|
|
if !k.In(block.Rect) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
block.Cells[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if border.Visible {
|
|
|
|
for x := 0; x < block.Size().X; x++ {
|
|
|
|
for y := 0; y < block.Size().Y; y++ {
|
|
|
|
point := image.Pt(x, y).Add(block.Origin())
|
|
|
|
c, ok := border.Cell(point, block.Rect)
|
|
|
|
if ok {
|
|
|
|
block.Cells[point] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 01:10:37 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
return block
|
2017-12-28 01:10:37 -07:00
|
|
|
}
|