2020-01-31 23:18:11 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2audio"
|
2020-02-01 20:39:28 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CursorButton represents a mouse button
|
|
|
|
type CursorButton uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CursorButtonLeft represents the left mouse button
|
|
|
|
CursorButtonLeft CursorButton = 1
|
|
|
|
// CursorButtonRight represents the right mouse button
|
|
|
|
CursorButtonRight CursorButton = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
var widgets []Widget
|
|
|
|
var cursorButtons CursorButton
|
|
|
|
var pressedIndex int
|
|
|
|
var CursorX int
|
|
|
|
var CursorY int
|
2020-02-01 20:39:28 -05:00
|
|
|
var clickSfx d2audio.SoundEffect
|
2020-01-31 23:18:11 -05:00
|
|
|
var waitForLeftMouseUp bool
|
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
func Initialize() {
|
2020-01-31 23:18:11 -05:00
|
|
|
pressedIndex = -1
|
|
|
|
clickSfx, _ = d2audio.LoadSoundEffect(d2resource.SFXButtonClick)
|
|
|
|
waitForLeftMouseUp = false
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:36:20 -04:00
|
|
|
// Reset resets the state of the UI manager. Typically called for new screens
|
2020-01-31 23:18:11 -05:00
|
|
|
func Reset() {
|
|
|
|
widgets = make([]Widget, 0)
|
|
|
|
pressedIndex = -1
|
|
|
|
waitForLeftMouseUp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWidget adds a widget to the UI manager
|
|
|
|
func AddWidget(widget Widget) {
|
|
|
|
widgets = append(widgets, widget)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WaitForMouseRelease() {
|
|
|
|
waitForLeftMouseUp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders all of the UI elements
|
2020-02-01 20:39:28 -05:00
|
|
|
func Render(target d2render.Surface) {
|
2020-01-31 23:18:11 -05:00
|
|
|
for _, widget := range widgets {
|
|
|
|
if widget.GetVisible() {
|
|
|
|
widget.Render(target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates all of the UI elements
|
|
|
|
func Advance(elapsed float64) {
|
|
|
|
for _, widget := range widgets {
|
|
|
|
if widget.GetVisible() {
|
|
|
|
widget.Advance(elapsed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cursorButtons = 0
|
|
|
|
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
|
|
|
if !waitForLeftMouseUp {
|
|
|
|
cursorButtons |= CursorButtonLeft
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if waitForLeftMouseUp {
|
|
|
|
waitForLeftMouseUp = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
|
|
|
|
cursorButtons |= CursorButtonRight
|
|
|
|
}
|
|
|
|
CursorX, CursorY = ebiten.CursorPosition()
|
|
|
|
if CursorButtonPressed(CursorButtonLeft) {
|
|
|
|
found := false
|
|
|
|
for i, widget := range widgets {
|
|
|
|
if !widget.GetVisible() || !widget.GetEnabled() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wx, wy := widget.GetPosition()
|
|
|
|
ww, wh := widget.GetSize()
|
2020-02-01 21:51:49 -05:00
|
|
|
if CursorX >= wx && CursorX <= wx+ww && CursorY >= wy && CursorY <= wy+wh {
|
2020-01-31 23:18:11 -05:00
|
|
|
widget.SetPressed(true)
|
|
|
|
if pressedIndex == -1 {
|
|
|
|
found = true
|
|
|
|
pressedIndex = i
|
|
|
|
clickSfx.Play()
|
|
|
|
} else if pressedIndex > -1 && pressedIndex != i {
|
|
|
|
widgets[i].SetPressed(false)
|
|
|
|
} else {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
widget.SetPressed(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
if pressedIndex > -1 {
|
|
|
|
widgets[pressedIndex].SetPressed(false)
|
|
|
|
} else {
|
|
|
|
pressedIndex = -2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if pressedIndex > -1 {
|
|
|
|
widget := widgets[pressedIndex]
|
|
|
|
wx, wy := widget.GetPosition()
|
|
|
|
ww, wh := widget.GetSize()
|
2020-02-01 21:51:49 -05:00
|
|
|
if CursorX >= wx && CursorX <= wx+ww && CursorY >= wy && CursorY <= wy+wh {
|
2020-01-31 23:18:11 -05:00
|
|
|
widget.Activate()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, widget := range widgets {
|
|
|
|
if !widget.GetVisible() || !widget.GetEnabled() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
widget.SetPressed(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pressedIndex = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CursorButtonPressed determines if the specified button has been pressed
|
|
|
|
func CursorButtonPressed(button CursorButton) bool {
|
|
|
|
return cursorButtons&button > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func KeyPressed(key ebiten.Key) bool {
|
|
|
|
return ebiten.IsKeyPressed(key)
|
|
|
|
}
|