1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-04 23:10:42 +00:00

Escape menu - Keyboard support and minor improvements (#374)

* Handle up/down/enter keys and reset menu on toggle

Signed-off-by: William Claude <w.claude@thebeat.co>

* Add sound when selecting an item

Signed-off-by: William Claude <w.claude@thebeat.co>

* Reorganise code and remove unused things (YAGNI)

Signed-off-by: William Claude <w.claude@thebeat.co>

* Group properties

Signed-off-by: William Claude <w.claude@thebeat.co>

* Use switch statements instead of IFs

Signed-off-by: William Claude <w.claude@thebeat.co>

* Prevent opening the escape menu over hero stats or inventory

Signed-off-by: William Claude <w.claude@thebeat.co>
This commit is contained in:
William 2020-06-22 00:44:33 +02:00 committed by GitHub
parent ec17fcf3e6
commit 485e60683b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 26 deletions

View File

@ -254,6 +254,7 @@ const (
// --- Sound Effects ---
SFXCursorSelect = "cursor_select"
SFXButtonClick = "cursor_button_click"
SFXAmazonDeselect = "cursor_amazon_deselect"
SFXAmazonSelect = "cursor_amazon_select"

View File

@ -5,6 +5,7 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2audio"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
@ -28,11 +29,12 @@ const (
// EscapeMenu is the overlay menu shown in-game when pressing Escape
type EscapeMenu struct {
current EscapeOption
isOpen bool
labels []d2ui.Label
pentLeft *d2ui.Sprite
pentRight *d2ui.Sprite
current EscapeOption
isOpen bool
labels []d2ui.Label
pentLeft *d2ui.Sprite
pentRight *d2ui.Sprite
selectSound d2audio.SoundEffect
// pre-computations
pentWidth int
@ -49,7 +51,6 @@ func NewEscapeMenu() *EscapeMenu {
// ScreenLoadHandler
func (m *EscapeMenu) OnLoad() error {
m.labels = []d2ui.Label{
d2ui.CreateLabel(d2resource.Font42, d2resource.PaletteSky),
d2ui.CreateLabel(d2resource.Font42, d2resource.PaletteSky),
@ -76,11 +77,8 @@ func (m *EscapeMenu) OnLoad() error {
m.pentWidth, m.pentHeight = m.pentLeft.GetFrameBounds()
_, m.textHeight = m.labels[EscapeOptions].GetSize()
return nil
}
m.selectSound, _ = d2audio.LoadSoundEffect(d2resource.SFXCursorSelect)
// ScreenUnloadHandler
func (m *EscapeMenu) OnUnload() error {
return nil
}
@ -131,15 +129,36 @@ func (m *EscapeMenu) IsOpen() bool {
}
func (m *EscapeMenu) Toggle() {
if !m.isOpen {
m.reset()
}
m.isOpen = !m.isOpen
}
func (m *EscapeMenu) Open() {
m.isOpen = true
func (m *EscapeMenu) reset() {
m.current = EscapeOptions
}
func (m *EscapeMenu) Close() {
m.isOpen = false
func (m *EscapeMenu) OnUpKey() {
switch m.current {
case EscapeSaveExit:
m.current = EscapeOptions
case EscapeReturn:
m.current = EscapeSaveExit
}
}
func (m *EscapeMenu) OnDownKey() {
switch m.current {
case EscapeOptions:
m.current = EscapeSaveExit
case EscapeSaveExit:
m.current = EscapeReturn
}
}
func (m *EscapeMenu) OnEnterKey() {
m.selectCurrent()
}
// Moves current selection marker to closes option to mouse.
@ -170,25 +189,42 @@ func (m *EscapeMenu) OnMouseButtonDown(event d2input.MouseEvent) bool {
lbl := &m.labels[EscapeOptions]
if m.toMouseRegion(event.HandlerEvent, lbl) == regIn {
m.onOptions()
m.current = EscapeOptions
m.selectCurrent()
return false
}
lbl = &m.labels[EscapeSaveExit]
if m.toMouseRegion(event.HandlerEvent, lbl) == regIn {
m.onSaveAndExit()
m.current = EscapeSaveExit
m.selectCurrent()
return false
}
lbl = &m.labels[EscapeReturn]
if m.toMouseRegion(event.HandlerEvent, lbl) == regIn {
m.onReturnToGame()
m.current = EscapeReturn
m.selectCurrent()
return false
}
return false
}
func (m *EscapeMenu) selectCurrent() {
switch m.current {
case EscapeOptions:
m.onOptions()
m.selectSound.Play()
case EscapeSaveExit:
m.onSaveAndExit()
m.selectSound.Play()
case EscapeReturn:
m.onReturnToGame()
m.selectSound.Play()
}
}
// User clicked on "OPTIONS"
func (m *EscapeMenu) onOptions() error {
log.Println("OPTIONS Clicked from Escape Menu")

View File

@ -57,20 +57,29 @@ func NewGameControls(hero *d2mapentity.Player, mapEngine *d2mapengine.MapEngine,
}
func (g *GameControls) OnKeyDown(event d2input.KeyEvent) bool {
if event.Key == d2input.KeyEscape {
switch event.Key {
case d2input.KeyEscape:
if g.inventory.IsOpen() || g.heroStats.IsOpen() {
g.inventory.Close()
g.heroStats.Close()
break
}
g.escapeMenu.Toggle()
return true
}
if event.Key == d2input.KeyI {
case d2input.KeyUp:
g.escapeMenu.OnUpKey()
case d2input.KeyDown:
g.escapeMenu.OnDownKey()
case d2input.KeyEnter:
g.escapeMenu.OnEnterKey()
case d2input.KeyI:
g.inventory.Toggle()
return true
}
if event.Key == d2input.KeyC {
case d2input.KeyC:
g.heroStats.Toggle()
return true
default:
return false
}
return false
return true
}
func (g *GameControls) OnMouseMove(event d2input.MouseMoveEvent) bool {