mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-01-13 12:56:35 -05:00
d2core/ui: Introduce clickable widgets
not all widgets need to be clickable, so let's make it it's own interface.
This commit is contained in:
parent
e99fbf5c4b
commit
aa1fca84d5
@ -12,16 +12,17 @@ import (
|
||||
|
||||
// UIManager manages a collection of UI elements (buttons, textboxes, labels)
|
||||
type UIManager struct {
|
||||
asset *d2asset.AssetManager
|
||||
renderer d2interface.Renderer
|
||||
inputManager d2interface.InputManager
|
||||
audio d2interface.AudioProvider
|
||||
widgets []Widget
|
||||
cursorButtons CursorButton
|
||||
CursorX int
|
||||
CursorY int
|
||||
pressedWidget Widget
|
||||
clickSfx d2interface.SoundEffect
|
||||
asset *d2asset.AssetManager
|
||||
renderer d2interface.Renderer
|
||||
inputManager d2interface.InputManager
|
||||
audio d2interface.AudioProvider
|
||||
widgets []Widget
|
||||
clickableWidgets []ClickableWidget
|
||||
cursorButtons CursorButton
|
||||
CursorX int
|
||||
CursorY int
|
||||
pressedWidget ClickableWidget
|
||||
clickSfx d2interface.SoundEffect
|
||||
}
|
||||
|
||||
// Note: methods for creating buttons and stuff are in their respective files
|
||||
@ -44,6 +45,7 @@ func (ui *UIManager) Initialize() {
|
||||
// Reset resets the state of the UI manager. Typically called for new screens
|
||||
func (ui *UIManager) Reset() {
|
||||
ui.widgets = nil
|
||||
ui.clickableWidgets = nil
|
||||
ui.pressedWidget = nil
|
||||
}
|
||||
|
||||
@ -54,8 +56,12 @@ func (ui *UIManager) addWidget(widget Widget) {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
ui.widgets = append(ui.widgets, widget)
|
||||
clickable, ok := widget.(ClickableWidget)
|
||||
if ok {
|
||||
ui.clickableWidgets = append(ui.clickableWidgets, clickable)
|
||||
}
|
||||
|
||||
ui.widgets = append(ui.widgets, widget)
|
||||
widget.bindManager(ui)
|
||||
}
|
||||
|
||||
@ -67,13 +73,12 @@ func (ui *UIManager) OnMouseButtonUp(event d2interface.MouseEvent) bool {
|
||||
// activate previously pressed widget if cursor is still hovering
|
||||
w := ui.pressedWidget
|
||||
|
||||
if w != nil && ui.contains(w, ui.CursorX, ui.CursorY) && w.GetVisible() && w.
|
||||
GetEnabled() {
|
||||
if w != nil && ui.contains(w, ui.CursorX, ui.CursorY) && w.GetVisible() && w.GetEnabled() {
|
||||
w.Activate()
|
||||
}
|
||||
|
||||
// unpress all widgets that are pressed
|
||||
for _, w := range ui.widgets {
|
||||
for _, w := range ui.clickableWidgets {
|
||||
w.SetPressed(false)
|
||||
}
|
||||
}
|
||||
@ -87,7 +92,7 @@ func (ui *UIManager) OnMouseButtonDown(event d2interface.MouseEvent) bool {
|
||||
if event.Button() == d2enum.MouseButtonLeft {
|
||||
// find and press a widget on screen
|
||||
ui.pressedWidget = nil
|
||||
for _, w := range ui.widgets {
|
||||
for _, w := range ui.clickableWidgets {
|
||||
if ui.contains(w, ui.CursorX, ui.CursorY) && w.GetVisible() && w.GetEnabled() {
|
||||
w.SetPressed(true)
|
||||
ui.pressedWidget = w
|
||||
|
@ -4,9 +4,14 @@ package d2ui
|
||||
type Widget interface {
|
||||
Drawable
|
||||
bindManager(ui *UIManager)
|
||||
GetEnabled() bool
|
||||
}
|
||||
|
||||
// ClickableWidget defines an object that can be clicked
|
||||
type ClickableWidget interface {
|
||||
Widget
|
||||
SetEnabled(enabled bool)
|
||||
SetPressed(pressed bool)
|
||||
GetEnabled() bool
|
||||
GetPressed() bool
|
||||
OnActivated(callback func())
|
||||
Activate()
|
||||
|
Loading…
Reference in New Issue
Block a user