Beispiel #1
0
// NewActivityBar returns an ActivityBar widget.
// maxValue: helps determines drawn data height; must
//           be greater than 0.0, otherwise set to 1.0
// data:     data slice to use to render
func NewActivityBar(parent ui.Drawer, name string, maxValue float64, data []float64) *ActivityBar {
	self := &ActivityBar{
		ui.NewElement(parent, name),
		maxValue,
		data,
		color.NewTween(color.Gray13, color.Red1, 250*time.Millisecond),
	}

	self.AddMouseEnterCB(func(b bool) {
		if b {
			self.tween = color.NewTween(color.Gray13, color.Red1, 250*time.Millisecond)
			self.tween.Start()
		} else {
			c := self.tween.Color()
			self.tween = color.NewTween(c, color.Gray13, 250*time.Millisecond)
			self.tween.Start()
		}
	})

	self.DrawCB = func(ctx vg.Context) {
		x, y, w, h := self.Bounds()
		barw := 5.0 // width of the bar
		sepw := 1.0
		totalw := (barw + sepw)
		leftover := w - (math.Floor(w/totalw) * totalw)
		x += leftover / 2
		w -= 2 + totalw
		c := self.tween.Color()

		l := float64(len(self.Data))
		if l == 0 {
			return
		}

		maxcount := w / totalw
		s := 0
		if l > maxcount {
			s = int(l - 1 - maxcount)
		}

		iy := y + h

		for i := s; i < len(self.Data); i++ {
			v := self.Data[i]
			ctx.BeginPath()
			ix := math.Floor(x + (float64(i-s) * totalw))
			ih := math.Floor(iy - (h * (v / self.MaxValue)))
			ctx.FillColor(c)
			ax := ix + sepw
			bx := ix + barw
			ctx.MoveTo(ax, iy)
			ctx.LineTo(ax, ih)
			ctx.LineTo(bx, ih)
			ctx.LineTo(bx, iy)
			ctx.Fill()
		}
	}

	return self
}
Beispiel #2
0
func NewLineChart(parent ui.Drawer, name string, mdl LineChartModeler) *LineChart {
	self := &LineChart{
		ui.NewElement(parent, name),
		mdl,
		Title{Model: mdl, FontSize: 17},
		Palette[CHART_BACKGROUND],
		Palette[CHART_BACKGROUND],
		true,
	}

	return self
}
Beispiel #3
0
func New(parent ui.Drawer, name, text string) *Text {
	tokens := strings.Fields(text)
	self := &Text{
		ui.NewElement(parent, name),
		ui.NewScroll(),
		text,
		tokens,
		vg.FONT_DEFAULT,
		21,
		LEFT,
		nil,
		make([]*ui.Rectangle, len(tokens)),
		make([]*ui.Rectangle, len(tokens)),
	}

	self.CornerRadius = 0

	self.AddScrollCB(func(xoff, yoff float64) {
		vertical := self.YOffset()
		vertical -= yoff * self.Increment()
		if vertical < 0 {
			vertical = 0
		}
		self.SetYOffset(vertical)
	})

	X, Y := 0.0, 0.0
	self.AddMousePositionCB(func(x, y float64) {
		X, Y = x, y
	})

	self.AddMouseClickCB(func(state ui.MouseButtonState) {
		fmt.Println("CLICK!")
		xx, yy, w, h := self.Bounds()

		self.forEachDrawnToken(xx, yy, w, h,
			func(i int, x, y, lineHeight float64, bounds *ui.Rectangle, ctx *vg.Context) {
				r := ui.Rectangle{ui.Position{x, y - lineHeight/2}, ui.Size{bounds.Size.W, bounds.Size.H + lineHeight}}

				if r.Contains(X, Y) {
					fmt.Println(tokens[i])
				}
			},
		)
	})

	self.Background = White
	self.Foreground = Gray10
	return self
}
Beispiel #4
0
func NewProgressBar(parent ui.Drawer, name string, max float64) *ProgressBar {
	self := &ProgressBar{
		ui.NewElement(parent, name),
		0,
		max,
	}

	self.Element.ClickBackground = Blue4

	self.DrawCB = func(ctx vg.Context) {
		x, y, w, h := self.Bounds()
		fg := ctx.BoxGradient(x, y, w, h/3, h/2, h, self.ClickBackground, self.ClickBackground)
		ctx.BeginPath()
		ctx.RoundedRect(x+1, y+1, (w-2)*(self.Value/self.Max), h-2, self.CornerRadius)
		ctx.FillPaint(fg)
		ctx.Fill()
	}
	return self
}
Beispiel #5
0
func NewButton(parent ui.Drawer, name, text string) *Button {
	self := &Button{
		ui.NewElement(parent, name),
		text,
	}

	self.DrawCB = func(ctx vg.Context) {
		x, y, w, h := self.Bounds()
		ctx.Scissor(x, y, w, h)
		ctx.FillColor(self.Foreground)
		ctx.TextAlign(vg.ALIGN_CENTER | vg.ALIGN_MIDDLE)

		ctx.SetFontSize(35)
		ctx.FindFont(vg.FONT_DEFAULT)
		ctx.WrappedText(x, y+h/2, w, self.Text)
		ctx.ResetScissor()
	}

	return self
}
Beispiel #6
0
func NewBartext(parent ui.Drawer, name, text string) *bartext {
	self := &bartext{
		ui.NewElement(parent, name),
		text,
	}

	self.SetCommonBackground(color.Purple2)

	self.DrawCB = func(ctx vg.Context) {
		x, y, w, h := self.Bounds()
		ctx.Scissor(x, y, w, h)
		ctx.FillColor(self.Foreground)
		ctx.SetFontSize(40)
		ctx.FindFont(vg.FONT_DEFAULT)
		ctx.TextAlign(vg.ALIGN_CENTER | vg.ALIGN_RIGHT)
		ctx.Text(x+w, y+h/1.5, self.Text)
		ctx.ResetScissor()
	}

	return self
}