Esempio n. 1
0
func NewGUIListBox(id int, x, y, w, h float64, fnt *font.Font, color, highlightColor, spriteHighlightColor hge.Dword) *GUIListBox {
	l := new(GUIListBox)

	l.GUIObject.Initialize()

	l.GUIObject.Id = id
	l.GUIObject.Visible = true
	l.GUIObject.Enabled = true
	l.GUIObject.Rect.Set(x, y, x+w, y+h)
	l.Font = fnt
	l.highlight = sprite.New(nil, 0, 0, w, fnt.GetHeight())
	l.highlight.SetColor(highlightColor)
	l.color = color
	l.highlightColor = highlightColor
	l.List = list.New()

	l.GUIObject.Render = func() {
		item := l.List.Front()

		for i := 0; i < l.topItem; i++ {
			if item == nil {
				return
			}

			item = item.Next()
		}

		for i := 0; i < l.NumRows(); i++ {
			if i >= l.items {
				return
			}
			if l.topItem+i == l.selectedItem {
				l.highlight.Render(l.GUIObject.Rect.X1, l.GUIObject.Rect.Y1+float64(i)*l.Font.GetHeight())
				l.Font.SetColor(l.highlightColor)
			} else {
				l.Font.SetColor(l.color)
			}

			l.Font.Render(l.GUIObject.Rect.X1+3, l.GUIObject.Rect.Y1+float64(i)*l.Font.GetHeight(), font.TEXT_LEFT, item.Value.(*guiListboxItem).text)

			item = item.Next()
		}
	}

	l.GUIObject.MouseMove = func(x, y float64) bool {
		l.mx, l.my = x, y
		return false
	}

	l.GUIObject.MouseLButton = func(down bool) bool {
		if down {
			item := l.topItem + int(l.my)/int(l.Font.GetHeight())
			if item < l.Len() {
				l.selectedItem = item
				return true
			}
		}
		return false
	}

	l.GUIObject.MouseWheel = func(notches int) bool {
		l.topItem -= notches
		if l.topItem < 0 {
			l.topItem = 0
		}
		if l.topItem > l.Len()-l.NumRows() {
			l.topItem = l.Len() - l.NumRows()
		}

		return true
	}

	l.GUIObject.KeyClick = func(key input.Key, chr int) bool {
		switch key {
		case input.K_DOWN:
			if l.selectedItem < l.Len()-1 {
				l.selectedItem++
				if l.selectedItem > l.topItem+l.NumRows()-1 {
					l.topItem = l.selectedItem - l.NumRows() + 1
				}
				return true
			}

		case input.K_UP:
			if l.selectedItem > 0 {
				l.selectedItem--
				if l.selectedItem < l.topItem {
					l.topItem = l.selectedItem
				}
				return true
			}

		}
		return false
	}

	return l
}