2019-02-24 04:49:09 +00:00
|
|
|
package component
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-14 03:01:44 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2019-02-17 01:54:48 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
2019-03-16 04:35:00 +00:00
|
|
|
"github.com/sqshq/sampler/data"
|
2019-01-31 00:02:38 +00:00
|
|
|
)
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
type Component struct {
|
2019-03-16 23:59:28 +00:00
|
|
|
ui.Drawable
|
|
|
|
*data.Consumer
|
2019-02-24 04:42:52 +00:00
|
|
|
Type config.ComponentType
|
|
|
|
Title string
|
2019-03-18 02:41:23 +00:00
|
|
|
Location config.Location
|
2019-02-24 04:42:52 +00:00
|
|
|
Size config.Size
|
|
|
|
RefreshRateMs int
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:55:24 +00:00
|
|
|
func NewComponent(dbl ui.Drawable, cmr *data.Consumer, cfg config.ComponentConfig) *Component {
|
2019-03-16 04:35:00 +00:00
|
|
|
return &Component{
|
2019-03-16 23:59:28 +00:00
|
|
|
Drawable: dbl,
|
|
|
|
Consumer: cmr,
|
2019-03-17 23:55:24 +00:00
|
|
|
Type: cfg.Type,
|
2019-03-16 23:59:28 +00:00
|
|
|
Title: cfg.Title,
|
2019-03-18 02:41:23 +00:00
|
|
|
Location: cfg.GetLocation(),
|
|
|
|
Size: cfg.GetSize(),
|
2019-03-16 23:59:28 +00:00
|
|
|
RefreshRateMs: *cfg.RefreshRateMs,
|
2019-03-16 04:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 03:26:51 +00:00
|
|
|
func (c *Component) Move(x, y int) {
|
2019-03-18 02:41:23 +00:00
|
|
|
c.Location.X += x
|
|
|
|
c.Location.Y += y
|
2019-02-11 03:26:51 +00:00
|
|
|
c.normalize()
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 03:26:51 +00:00
|
|
|
func (c *Component) Resize(x, y int) {
|
|
|
|
c.Size.X += x
|
|
|
|
c.Size.Y += y
|
|
|
|
c.normalize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Component) normalize() {
|
2019-03-18 02:41:23 +00:00
|
|
|
if c.Location.X < 0 {
|
|
|
|
c.Location.X = 0
|
2019-02-11 03:26:51 +00:00
|
|
|
}
|
2019-03-18 02:41:23 +00:00
|
|
|
if c.Location.Y < 0 {
|
|
|
|
c.Location.Y = 0
|
2019-02-11 03:26:51 +00:00
|
|
|
}
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|