Ejemplo n.º 1
0
func (b *Button) HandleEvent(event interface{}) bool {
	switch event.(type) {
	case sdl.MouseMotionEvent:
		m := event.(sdl.MouseMotionEvent)
		if ui.Overlaps(int16(m.X), int16(m.Y), b.X, b.Y, b.W, b.H) {
			b.hovered = true
			return true
		} else {
			b.hovered = false
		}
	case sdl.MouseButtonEvent:
		m := event.(sdl.MouseButtonEvent)
		overlap := ui.Overlaps(int16(m.X), int16(m.Y), b.X, b.Y, b.W, b.H)
		if ui.IsMouseDown(m, 1) && overlap && b.OnClick != nil {
			b.OnClick()
			return true
		}
	}
	return false
}
Ejemplo n.º 2
0
func (m *MainMenu) Draw(screen *sdl.Surface, top bool) {
	w := uint16(m.titleText.W)
	h := uint16(m.titleText.H)
	screen.Blit(
		&sdl.Rect{
			X: int16(screen.W - m.titleText.W - 50),
			Y: 15,
			W: w,
			H: h,
		},
		m.titleText,
		&sdl.Rect{X: 0, Y: 0, W: w, H: h},
	)

	m.menuItemSelected = ""
	y := int16(screen.H - menuItemYOffset)
	for _, label := range m.itemLabels {
		item := m.menuItems[label]
		mx := int16(m.mouseX)
		my := int16(m.mouseY)
		w = uint16(item.W)
		h = uint16(item.H)
		y -= int16(item.H)
		if ui.Overlaps(mx, my, menuItemXOffset, y, w, h) {
			if top {
				item = m.hoverItems[label]
			}
			m.menuItemSelected = label
		}

		screen.Blit(
			&sdl.Rect{
				X: menuItemXOffset,
				Y: int16(y),
				W: w,
				H: h,
			},
			item,
			&sdl.Rect{X: 0, Y: 0, W: w, H: h},
		)
	}
}