mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-09-27 21:56:19 -04: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)
|
// UIManager manages a collection of UI elements (buttons, textboxes, labels)
|
||||||
type UIManager struct {
|
type UIManager struct {
|
||||||
asset *d2asset.AssetManager
|
asset *d2asset.AssetManager
|
||||||
renderer d2interface.Renderer
|
renderer d2interface.Renderer
|
||||||
inputManager d2interface.InputManager
|
inputManager d2interface.InputManager
|
||||||
audio d2interface.AudioProvider
|
audio d2interface.AudioProvider
|
||||||
widgets []Widget
|
widgets []Widget
|
||||||
cursorButtons CursorButton
|
clickableWidgets []ClickableWidget
|
||||||
CursorX int
|
cursorButtons CursorButton
|
||||||
CursorY int
|
CursorX int
|
||||||
pressedWidget Widget
|
CursorY int
|
||||||
clickSfx d2interface.SoundEffect
|
pressedWidget ClickableWidget
|
||||||
|
clickSfx d2interface.SoundEffect
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: methods for creating buttons and stuff are in their respective files
|
// 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
|
// Reset resets the state of the UI manager. Typically called for new screens
|
||||||
func (ui *UIManager) Reset() {
|
func (ui *UIManager) Reset() {
|
||||||
ui.widgets = nil
|
ui.widgets = nil
|
||||||
|
ui.clickableWidgets = nil
|
||||||
ui.pressedWidget = nil
|
ui.pressedWidget = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,8 +56,12 @@ func (ui *UIManager) addWidget(widget Widget) {
|
|||||||
log.Print(err)
|
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)
|
widget.bindManager(ui)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,13 +73,12 @@ func (ui *UIManager) OnMouseButtonUp(event d2interface.MouseEvent) bool {
|
|||||||
// activate previously pressed widget if cursor is still hovering
|
// activate previously pressed widget if cursor is still hovering
|
||||||
w := ui.pressedWidget
|
w := ui.pressedWidget
|
||||||
|
|
||||||
if w != nil && ui.contains(w, ui.CursorX, ui.CursorY) && w.GetVisible() && w.
|
if w != nil && ui.contains(w, ui.CursorX, ui.CursorY) && w.GetVisible() && w.GetEnabled() {
|
||||||
GetEnabled() {
|
|
||||||
w.Activate()
|
w.Activate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// unpress all widgets that are pressed
|
// unpress all widgets that are pressed
|
||||||
for _, w := range ui.widgets {
|
for _, w := range ui.clickableWidgets {
|
||||||
w.SetPressed(false)
|
w.SetPressed(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +92,7 @@ func (ui *UIManager) OnMouseButtonDown(event d2interface.MouseEvent) bool {
|
|||||||
if event.Button() == d2enum.MouseButtonLeft {
|
if event.Button() == d2enum.MouseButtonLeft {
|
||||||
// find and press a widget on screen
|
// find and press a widget on screen
|
||||||
ui.pressedWidget = nil
|
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() {
|
if ui.contains(w, ui.CursorX, ui.CursorY) && w.GetVisible() && w.GetEnabled() {
|
||||||
w.SetPressed(true)
|
w.SetPressed(true)
|
||||||
ui.pressedWidget = w
|
ui.pressedWidget = w
|
||||||
|
@ -4,9 +4,14 @@ package d2ui
|
|||||||
type Widget interface {
|
type Widget interface {
|
||||||
Drawable
|
Drawable
|
||||||
bindManager(ui *UIManager)
|
bindManager(ui *UIManager)
|
||||||
GetEnabled() bool
|
}
|
||||||
|
|
||||||
|
// ClickableWidget defines an object that can be clicked
|
||||||
|
type ClickableWidget interface {
|
||||||
|
Widget
|
||||||
SetEnabled(enabled bool)
|
SetEnabled(enabled bool)
|
||||||
SetPressed(pressed bool)
|
SetPressed(pressed bool)
|
||||||
|
GetEnabled() bool
|
||||||
GetPressed() bool
|
GetPressed() bool
|
||||||
OnActivated(callback func())
|
OnActivated(callback func())
|
||||||
Activate()
|
Activate()
|
||||||
|
Loading…
Reference in New Issue
Block a user