Example #1
0
func (cb *CheckBox) Draw() {
	var img image.Image

	if cb.checked && cb.mouseIn {
		img = tk.cbCheckedHover
	} else if cb.checked && !cb.mouseIn {
		img = tk.cbChecked
	} else if !cb.checked && cb.mouseIn {
		img = tk.cbUncheckedHover
	} else if !cb.checked && !cb.mouseIn {
		img = tk.cbUnchecked
	}

	gfx.DrawOver(&cb.parent.Element, img, cb.Element.X, cb.Element.Y, cb.Element.Width, cb.Element.Height)

	if cb.txt != "" {
		w, h, _ := fonts.ExpectedSize(cb.Element.Font, cb.txt)
		voffsetTxt := cb.Element.Height/2 - int(h)/2 + 1

		// FIXME: temp solution. instead, parent element should expose region redraw method
		bg := cb.parent.GetBG()
		gfx.RectFilled(cb.parent.Element.Buffer,
			cb.Element.X+cb.Element.Width+ICON_TEXT_SPACE,
			cb.Element.Y+voffsetTxt,
			cb.Element.X+cb.Element.Width+ICON_TEXT_SPACE+int(w),
			cb.Element.Y+voffsetTxt+int(h),
			cb.parent.Element.Width,
			bg.R, bg.G, bg.B, bg.A)

		fonts.Render(&cb.parent.Element, cb.txt,
			cb.Element.X+cb.Element.Width+ICON_TEXT_SPACE, cb.Element.Y+voffsetTxt,
			cb.parent.Element.Width-cb.Element.X-ICON_TEXT_SPACE-cb.Element.Width, cb.Element.Height,
			cb.Element.Font)
	}
}
Example #2
0
func (but *Button) Draw() {
	var r, g, b byte

	if but.pushed {
		r, g, b = 49, 80, 0
	} else {
		r, g, b = 80, 130, 0
	}

	gfx.RectFilled(but.parent.Element.Buffer, but.Element.X, but.Element.Y, but.Element.X+but.Element.Width, but.Element.Y+but.Element.Height, but.parent.Element.Width, r, g, b, gfx.A_OPAQUE)
	gfx.Rect(but.parent.Element.Buffer, but.Element.X, but.Element.Y, but.Element.X+but.Element.Width-1, but.Element.Y+but.Element.Height-1, but.parent.Element.Width, 0, 0, 0, gfx.A_OPAQUE)

	if but.style&BS_TEXT != 0 {
		w, h, _ := fonts.ExpectedSize(but.Element.Font, but.txt)
		hoffsetTxt := but.Element.Width/2 - int(w)/2
		voffsetTxt := but.Element.Height/2 - int(h)/2 + 1
		fonts.Render(&but.parent.Element, but.txt,
			but.Element.X+hoffsetTxt, but.Element.Y+voffsetTxt,
			but.Element.Width, but.Element.Height, but.Element.Font)
	} else if but.style&BS_ICON_TEXT != 0 {
		iw := but.icon.Bounds().Dx()
		ih := but.icon.Bounds().Dy()
		w, h, _ := fonts.ExpectedSize(but.Element.Font, but.txt)
		hoffsetIcon := but.Element.Width/2 - int(int(w)+iw+ICON_TEXT_SPACE)/2
		voffsetTxt := but.Element.Height/2 - int(h)/2 + 1
		voffsetIcon := but.Element.Height/2 - int(ih)/2
		gfx.DrawOver(&but.parent.Element, but.icon, but.Element.X+hoffsetIcon, but.Element.Y+voffsetIcon, iw, ih)
		fonts.Render(&but.parent.Element, but.txt,
			but.Element.X+hoffsetIcon+iw+ICON_TEXT_SPACE, but.Element.Y+voffsetTxt,
			but.Element.Width, but.Element.Height, but.Element.Font)
	} else if but.style&BS_ICON != 0 {
		iw := but.icon.Bounds().Dx()
		ih := but.icon.Bounds().Dy()
		hoffsetIcon := but.Element.Width/2 - int(iw)/2
		voffsetIcon := but.Element.Height/2 - int(ih)/2
		gfx.DrawOver(&but.parent.Element, but.icon, but.Element.X+hoffsetIcon, but.Element.Y+voffsetIcon, iw, ih)
	}
}
Example #3
0
func (win *Window) CheckBox(style byte, ms *mouse.Mouse, txt string, fnClick clickHandler, x, y int) *CheckBox {

	cb := &CheckBox{
		parent:    win,
		fb:        win.fb,
		clickHndr: fnClick,
		txt:       txt,
		style:     style,
		checked:   false,
		mouseIn:   false,
	}

	win.Children.PushFront(cb)

	cb.Element = base.Element{
		Id:         base.GenerateId(),
		X:          x,
		Y:          y,
		ScreenX:    win.Element.X + x,
		ScreenY:    win.Element.Y + y,
		Width:      tk.cbChecked.Bounds().Dx(),
		Height:     tk.cbChecked.Bounds().Dy(),
		InvMsgPipe: win.InvMsgPipe,
		Font:       fonts.Default(),
	}

	var totalWidth, totalHeight int

	if txt != "" {
		w, h, _ := fonts.ExpectedSize(cb.Element.Font, txt)
		totalWidth = cb.Element.Width + ICON_TEXT_SPACE + int(w)

		if int(h) > cb.Element.Height {
			totalHeight = int(h)
		} else {
			totalHeight = cb.Element.Height
		}
	} else {
		totalWidth = cb.Element.Width
		totalHeight = cb.Element.Height
	}

	ms.RegisterMouse(cb.Element.Id, cb.Mouse, nil, &cb.Element.ScreenX, &cb.Element.ScreenY, totalWidth, totalHeight)

	cb.Draw()

	return cb
}