Example #1
0
func (c *ColorPicker) SetColor(color nanovgo.Color) {
	if !c.pushed {
		fgColor := color.ContrastingColor()
		c.SetBackgroundColor(color)
		c.SetTextColor(fgColor)
		c.colorWheel.SetColor(color)
		c.pickButton.SetBackgroundColor(color)
		c.pickButton.SetTextColor(fgColor)
	}
}
Example #2
0
func NewColorPicker(parent Widget, colors ...nanovgo.Color) *ColorPicker {
	var color nanovgo.Color
	switch len(colors) {
	case 0:
		color = nanovgo.RGBAf(1.0, 0.0, 0.0, 1.0)
	case 1:
		color = colors[0]
	default:
		panic("NewColorPicker can accept only one extra parameter (color)")
	}

	colorPicker := &ColorPicker{}

	// init PopupButton member
	colorPicker.chevronIcon = IconRightOpen
	colorPicker.SetIconPosition(ButtonIconLeftCentered)
	colorPicker.SetFlags(ToggleButtonType | PopupButtonType)
	parentWindow := parent.FindWindow()

	colorPicker.popup = NewPopup(parentWindow.Parent(), parentWindow)
	colorPicker.popup.panel.SetLayout(NewGroupLayout())

	colorPicker.colorWheel = NewColorWheel(colorPicker.popup.panel)

	colorPicker.pickButton = NewButton(colorPicker.popup.panel, "Pick")
	colorPicker.pickButton.SetFixedSize(100, 25)

	InitWidget(colorPicker, parent)

	colorPicker.SetColor(color)

	colorPicker.PopupButton.SetChangeCallback(func(flag bool) {
		colorPicker.SetColor(colorPicker.BackgroundColor())
		if colorPicker.callback != nil {
			colorPicker.callback(colorPicker.BackgroundColor())
		}
	})

	colorPicker.colorWheel.SetCallback(func(color nanovgo.Color) {
		colorPicker.pickButton.SetBackgroundColor(color)
		colorPicker.pickButton.SetTextColor(color.ContrastingColor())
	})

	colorPicker.pickButton.SetCallback(func() {
		color := colorPicker.colorWheel.Color()
		colorPicker.SetPushed(false)
		colorPicker.SetColor(color)
		if colorPicker.callback != nil {
			colorPicker.callback(colorPicker.BackgroundColor())
		}
	})

	return colorPicker
}
Example #3
0
func (c *ColorWheel) SetColor(color nanovgo.Color) {
	c.hue, c.saturation, c.lightness, _ = color.HSLA()
	c.calculatePosition()
}
Example #4
0
func (b *Button) Draw(self Widget, ctx *nanovgo.Context) {
	b.WidgetImplement.Draw(self, ctx)

	bx := float32(b.x)
	by := float32(b.y)
	bw := float32(b.w)
	bh := float32(b.h)

	var gradTop nanovgo.Color
	var gradBot nanovgo.Color

	if b.pushed {
		gradTop = b.theme.ButtonGradientTopPushed
		gradBot = b.theme.ButtonGradientBotPushed
	} else if b.mouseFocus && b.enabled {
		gradTop = b.theme.ButtonGradientTopFocused
		gradBot = b.theme.ButtonGradientBotFocused
	} else {
		gradTop = b.theme.ButtonGradientTopUnfocused
		gradBot = b.theme.ButtonGradientBotUnfocused
	}
	ctx.BeginPath()
	ctx.RoundedRect(bx+1.0, by+1.0, bw-2.0, bh-2.0, float32(b.theme.ButtonCornerRadius-1))

	if b.backgroundColor.A != 0.0 {
		bgColor := b.backgroundColor
		bgColor.A = 1.0
		ctx.SetFillColor(bgColor)
		ctx.Fill()
		if b.pushed {
			gradTop.A = 0.8
			gradBot.A = 0.8
		} else {
			a := 1 - b.backgroundColor.A
			if !b.enabled {
				a = a*0.5 + 0.5
			}
			gradTop.A = a
			gradBot.A = a
		}
	}

	bg := nanovgo.LinearGradient(bx, by, bx, by+bh, gradTop, gradBot)
	ctx.SetFillPaint(bg)
	ctx.Fill()

	ctx.BeginPath()
	var pOff float32 = 0.0
	if b.pushed {
		pOff = 1.0
	}
	ctx.RoundedRect(bx+0.5, by+1.5-pOff, bw-1.0, bh-2+pOff, float32(b.theme.ButtonCornerRadius))
	ctx.SetStrokeColor(b.theme.BorderLight)
	ctx.Stroke()

	ctx.BeginPath()
	ctx.RoundedRect(bx+0.5, by+0.5, bw-1.0, bh-2, float32(b.theme.ButtonCornerRadius))
	ctx.SetStrokeColor(b.theme.BorderDark)
	ctx.Stroke()

	fontSize := float32(b.FontSize())
	ctx.SetFontSize(fontSize)
	ctx.SetFontFace(b.theme.FontBold)
	caption := b.caption
	tw, _ := ctx.TextBounds(0, 0, caption)

	centerX := bx + bw*0.5
	centerY := by + bh*0.5
	textPosX := centerX - tw*0.5
	textPosY := centerY - 1.0

	textColor := b.TextColor()
	if b.icon > 0 || b.imageIcon > 0 {
		var iw, ih float32
		if b.icon > 0 {
			ih = fontSize * 1.5 / 2
			ctx.SetFontSize(ih)
			ctx.SetFontFace(b.theme.FontIcons)
			iw, _ = ctx.TextBounds(0, 0, string([]rune{rune(b.icon)}))
		} else if b.imageIcon > 0 {
			ih = fontSize * 0.9
			w, h, _ := ctx.ImageSize(b.imageIcon)
			iw = float32(w) * ih / float32(h)
		}
		if b.caption != "" {
			iw += float32(b.h) * 0.15
		}
		ctx.SetFillColor(textColor)
		ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignMiddle)
		iconPosX := centerX
		iconPosY := centerY - 1

		switch b.iconPosition {
		case ButtonIconLeftCentered:
			iconPosX -= (tw + iw) * 0.5
			textPosX += iw * 0.5
		case ButtonIconRightCentered:
			iconPosX -= iw * 0.5
			textPosX += tw * 0.5
		case ButtonIconLeft:
			iconPosX = bx + 8.0
		case ButtonIconRight:
			iconPosX = bx + bw - iw - 8
		}
		if b.icon > 0 {
			ctx.TextRune(iconPosX, iconPosY, []rune{rune(b.icon)})
		} else {
			var eOff float32 = 0.25
			if b.enabled {
				eOff = 0.5
			}
			imgPaint := nanovgo.ImagePattern(iconPosX, iconPosY-ih*0.5, iw, ih, 0, b.imageIcon, eOff)
			ctx.SetFillPaint(imgPaint)
			ctx.Fill()
		}
	}
	ctx.SetFontSize(fontSize)
	ctx.SetFontFace(b.theme.FontBold)
	ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignMiddle)
	ctx.SetFillColor(b.theme.TextColorShadow)
	ctx.Text(textPosX, textPosY, caption)
	ctx.SetFillColor(textColor)
	ctx.Text(textPosX, textPosY+1.0, caption)
}