2020-01-31 23:18:11 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
2020-07-06 21:26:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-23 18:12:08 -04:00
|
|
|
"log"
|
|
|
|
|
2020-06-28 19:31:10 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-06-23 18:12:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
type UI struct {
|
|
|
|
widgets []Widget
|
|
|
|
cursorButtons CursorButton // TODO (carrelld) convert dependent code and remove
|
|
|
|
CursorX int // TODO (carrelld) convert dependent code and remove
|
|
|
|
CursorY int // TODO (carrelld) convert dependent code and remove
|
|
|
|
pressedWidget Widget
|
|
|
|
}
|
|
|
|
|
|
|
|
var singleton UI
|
2020-06-28 19:31:10 -04:00
|
|
|
var clickSfx d2interface.SoundEffect
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-06-28 19:31:10 -04:00
|
|
|
func Initialize(audioProvider d2interface.AudioProvider) {
|
|
|
|
sfx, err := audioProvider.LoadSoundEffect(d2resource.SFXButtonClick)
|
2020-06-23 18:12:08 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to initialize ui: %v", err)
|
|
|
|
}
|
|
|
|
clickSfx = sfx
|
|
|
|
|
|
|
|
d2input.BindHandler(&singleton)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-06-23 18:12:08 -04:00
|
|
|
singleton.widgets = nil
|
|
|
|
singleton.pressedWidget = nil
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddWidget adds a widget to the UI manager
|
|
|
|
func AddWidget(widget Widget) {
|
2020-06-23 18:12:08 -04:00
|
|
|
d2input.BindHandler(widget)
|
|
|
|
singleton.widgets = append(singleton.widgets, widget)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (u *UI) OnMouseButtonUp(event d2interface.MouseEvent) bool {
|
|
|
|
singleton.CursorX, singleton.CursorY = event.X(), event.Y()
|
2020-07-06 21:26:08 -04:00
|
|
|
if event.Button() == d2enum.MouseButtonLeft {
|
2020-06-23 18:12:08 -04:00
|
|
|
singleton.cursorButtons |= CursorButtonLeft
|
|
|
|
// activate previously pressed widget if cursor is still hovering
|
|
|
|
w := singleton.pressedWidget
|
|
|
|
if w != nil && contains(w, singleton.CursorX, singleton.CursorY) && w.GetVisible() && w.GetEnabled() {
|
|
|
|
w.Activate()
|
|
|
|
}
|
|
|
|
// unpress all widgets that are pressed
|
|
|
|
for _, w := range singleton.widgets {
|
|
|
|
w.SetPressed(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (u *UI) OnMouseButtonDown(event d2interface.MouseEvent) bool {
|
|
|
|
singleton.CursorX, singleton.CursorY = event.X(), event.Y()
|
2020-07-06 21:26:08 -04:00
|
|
|
if event.Button() == d2enum.MouseButtonLeft {
|
2020-06-23 18:12:08 -04:00
|
|
|
// find and press a widget on screen
|
|
|
|
singleton.pressedWidget = nil
|
|
|
|
for _, w := range singleton.widgets {
|
|
|
|
if contains(w, singleton.CursorX, singleton.CursorY) && w.GetVisible() && w.GetEnabled() {
|
|
|
|
w.SetPressed(true)
|
|
|
|
singleton.pressedWidget = w
|
|
|
|
clickSfx.Play()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 21:26:08 -04:00
|
|
|
if event.Button() == d2enum.MouseButtonRight {
|
2020-06-23 18:12:08 -04:00
|
|
|
singleton.cursorButtons |= CursorButtonRight
|
|
|
|
}
|
|
|
|
return false
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders all of the UI elements
|
2020-06-29 00:41:58 -04:00
|
|
|
func Render(target d2interface.Surface) {
|
2020-06-23 18:12:08 -04:00
|
|
|
for _, widget := range singleton.widgets {
|
2020-01-31 23:18:11 -05:00
|
|
|
if widget.GetVisible() {
|
|
|
|
widget.Render(target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
// contains determines whether a given x,y coordinate lands within a Widget
|
|
|
|
func contains(w Widget, x, y int) bool {
|
|
|
|
wx, wy := w.GetPosition()
|
|
|
|
ww, wh := w.GetSize()
|
|
|
|
return x >= wx && x <= wx+ww && y >= wy && y <= wy+wh
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
// Update updates all of the UI elements
|
|
|
|
func Advance(elapsed float64) {
|
2020-06-23 18:12:08 -04:00
|
|
|
for _, widget := range singleton.widgets {
|
2020-01-31 23:18:11 -05:00
|
|
|
if widget.GetVisible() {
|
|
|
|
widget.Advance(elapsed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CursorButtonPressed determines if the specified button has been pressed
|
|
|
|
func CursorButtonPressed(button CursorButton) bool {
|
2020-06-23 18:12:08 -04:00
|
|
|
return singleton.cursorButtons&button > 0
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
func CursorPosition() (x, y int) {
|
|
|
|
return singleton.CursorX, singleton.CursorY
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|