Beispiel #1
0
func (t *TextBox) buildText(
	text, font string,
	fontSize int,
	fontColor sdl.Color,
) {
	for _, line := range t.lines {
		line.Free()
	}
	t.lines = make([]*sdl.Surface, 0, 10)
	lines := strings.Split(strings.Trim(text, "\n"), "\n")
	for _, line := range lines {
		if line == "" {
			// SDL chokes on empty lines, so replace them with nil straight out
			t.lines = append(t.lines, nil)
			continue
		}
		words := strings.Split(line, " ")
		for current, rest := words[:], []string(nil); len(current) != 0; {
			w, _, _ := fonts.Size(strings.Join(current, " "), font, fontSize)
			if w > int(t.W) {
				rest = append(current[len(current)-1:], rest...)
				current = current[:len(current)-1]
			} else {
				t.lines = append(
					t.lines,
					fonts.BlendedText(
						strings.Join(current, " "),
						font,
						fontSize,
						fontColor,
					),
				)
				current = rest
				rest = nil
			}
		}
	}
}
Beispiel #2
0
func (b *Button) refreshText(text string, size int, color sdl.Color) {
	if b.labelText != nil {
		b.labelText.Free()
	}
	b.labelText = fonts.BlendedText(text, "sans_bold", size, color)
}