1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-26 09:05:24 +00:00

d2core/d2ui: Add checks to all widgets if they implement Widget

this also adds missing methods to elements not implementing widget. Note
here that we do not enable sprite and label, as this would produce a
crazy amount of linter warnings due to render() requiering error
handling then, which non of the callers handle. Since we remove the
render calls later anyways, we can postpone this static check for now.
This commit is contained in:
juander 2020-11-09 15:46:08 +01:00
parent bad07defe8
commit 01927d0f3b
9 changed files with 65 additions and 9 deletions

View File

@ -8,6 +8,9 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
) )
// static check that Checkbox implements Widget
var _ Widget = &Checkbox{}
// Checkbox represents a checkbox UI element // Checkbox represents a checkbox UI element
type Checkbox struct { type Checkbox struct {
*BaseWidget *BaseWidget

View File

@ -8,6 +8,9 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
) )
// static check that UIFrame implements Widget
var _ Widget = &UIFrame{}
type frameOrientation = int type frameOrientation = int
// Frame orientations // Frame orientations
@ -228,3 +231,8 @@ func (u *UIFrame) renderFramePiece(sfc d2interface.Surface, x, y, idx int) error
return nil return nil
} }
// Advance is a no-op
func (u *UIFrame) Advance(elapsed float64) error {
return nil
}

View File

@ -162,6 +162,11 @@ func (v *Label) getAlignOffset(textWidth int) int {
} }
} }
// Advance is a no-op
func (v *Label) Advance(elapsed float64) error {
return nil
}
func getColor(token ColorToken) color.Color { func getColor(token ColorToken) color.Color {
// https://github.com/OpenDiablo2/OpenDiablo2/issues/823 // https://github.com/OpenDiablo2/OpenDiablo2/issues/823
colors := map[ColorToken]color.Color{ colors := map[ColorToken]color.Color{

View File

@ -15,6 +15,9 @@ const (
scrollbarWidth = 10 scrollbarWidth = 10
) )
// static check that Scrollbar implements widget
var _ Widget = &Scrollbar{}
// Scrollbar is a vertical slider ui element // Scrollbar is a vertical slider ui element
type Scrollbar struct { type Scrollbar struct {
*BaseWidget *BaseWidget

View File

@ -10,6 +10,9 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
) )
// static check that TextBox implements widget
var _ Widget = &TextBox{}
// TextBox represents a text input box // TextBox represents a text input box
type TextBox struct { type TextBox struct {
*BaseWidget *BaseWidget

View File

@ -14,6 +14,9 @@ const (
screenHeight = 600 screenHeight = 600
) )
// static check that Tooltip implements widget
var _ Widget = &Tooltip{}
// Tooltip contains a label containing text with a transparent, black background // Tooltip contains a label containing text with a transparent, black background
type Tooltip struct { type Tooltip struct {
*BaseWidget *BaseWidget
@ -137,7 +140,7 @@ func (t *Tooltip) GetSize() (sx, sy int) {
} }
// Render draws the tooltip // Render draws the tooltip
func (t *Tooltip) Render(target d2interface.Surface) { func (t *Tooltip) Render(target d2interface.Surface) error {
maxW, maxH := t.GetSize() maxW, maxH := t.GetSize()
// nolint:gomnd // no magic numbers, their meaning is obvious // nolint:gomnd // no magic numbers, their meaning is obvious
@ -189,4 +192,11 @@ func (t *Tooltip) Render(target d2interface.Surface) {
} }
target.PopN(len(t.lines)) target.PopN(len(t.lines))
return nil
}
// Advance is a no-op
func (t *Tooltip) Advance(elapsed float64) error {
return nil
} }

View File

@ -595,7 +595,10 @@ func (h *HUD) renderMiniPanel(target d2interface.Surface) error {
labelY := centerY - halfButtonHeight - labelHeight labelY := centerY - halfButtonHeight - labelHeight
h.miniPanelTooltip.SetPosition(labelX, labelY) h.miniPanelTooltip.SetPosition(labelX, labelY)
h.miniPanelTooltip.Render(target)
if err := h.miniPanelTooltip.Render(target); err != nil {
return err
}
} }
return nil return nil
@ -627,6 +630,7 @@ func (h *HUD) renderNewSkillsButton(x, _ int, target d2interface.Surface) error
return nil return nil
} }
//nolint:golint,dupl // we clean this up later
func (h *HUD) renderHealthTooltip(target d2interface.Surface) { func (h *HUD) renderHealthTooltip(target d2interface.Surface) {
mx, my := h.lastMouseX, h.lastMouseY mx, my := h.lastMouseX, h.lastMouseY
@ -641,9 +645,13 @@ func (h *HUD) renderHealthTooltip(target d2interface.Surface) {
} }
h.healthTooltip.SetText(strPanelHealth) h.healthTooltip.SetText(strPanelHealth)
h.healthTooltip.Render(target)
if err := h.healthTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }
//nolint:golint,dupl // we clean this up later
func (h *HUD) renderManaTooltip(target d2interface.Surface) { func (h *HUD) renderManaTooltip(target d2interface.Surface) {
mx, my := h.lastMouseX, h.lastMouseY mx, my := h.lastMouseX, h.lastMouseY
@ -657,7 +665,10 @@ func (h *HUD) renderManaTooltip(target d2interface.Surface) {
} }
h.manaTooltip.SetText(strPanelMana) h.manaTooltip.SetText(strPanelMana)
h.manaTooltip.Render(target)
if err := h.manaTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }
func (h *HUD) renderRunWalkTooltip(target d2interface.Surface) { func (h *HUD) renderRunWalkTooltip(target d2interface.Surface) {
@ -679,7 +690,9 @@ func (h *HUD) renderRunWalkTooltip(target d2interface.Surface) {
h.runWalkTooltip.SetText(h.asset.TranslateString(stringTableKey)) h.runWalkTooltip.SetText(h.asset.TranslateString(stringTableKey))
h.runWalkTooltip.Render(target) if err := h.runWalkTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }
func (h *HUD) renderStaminaTooltip(target d2interface.Surface) { func (h *HUD) renderStaminaTooltip(target d2interface.Surface) {
@ -696,7 +709,10 @@ func (h *HUD) renderStaminaTooltip(target d2interface.Surface) {
strPanelStamina := fmt.Sprintf(fmtStamina, staminaCurr, staminaMax) strPanelStamina := fmt.Sprintf(fmtStamina, staminaCurr, staminaMax)
h.staminaTooltip.SetText(strPanelStamina) h.staminaTooltip.SetText(strPanelStamina)
h.staminaTooltip.Render(target)
if err := h.staminaTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }
func (h *HUD) renderExperienceTooltip(target d2interface.Surface) { func (h *HUD) renderExperienceTooltip(target d2interface.Surface) {
@ -720,7 +736,10 @@ func (h *HUD) renderExperienceTooltip(target d2interface.Surface) {
strPanelExp := fmt.Sprintf(fmtExp, expCurr, expMax) strPanelExp := fmt.Sprintf(fmtExp, expCurr, expMax)
h.experienceTooltip.SetText(strPanelExp) h.experienceTooltip.SetText(strPanelExp)
h.experienceTooltip.Render(target)
if err := h.experienceTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }
func (h *HUD) renderForSelectableEntitiesHovered(target d2interface.Surface) { func (h *HUD) renderForSelectableEntitiesHovered(target d2interface.Surface) {

View File

@ -242,5 +242,8 @@ func (g *Inventory) renderItemDescription(target d2interface.Surface, i Inventor
g.itemTooltip.SetTextLines(lines) g.itemTooltip.SetTextLines(lines)
_, y := g.grid.SlotToScreen(i.InventoryGridSlot()) _, y := g.grid.SlotToScreen(i.InventoryGridSlot())
g.itemTooltip.SetPosition(g.hoverX, y) g.itemTooltip.SetPosition(g.hoverX, y)
g.itemTooltip.Render(target)
if err := g.itemTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }

View File

@ -129,7 +129,9 @@ func (s *SkillPanel) Render(target d2interface.Surface) error {
} }
if s.hoveredSkill != nil { if s.hoveredSkill != nil {
s.hoverTooltip.Render(target) if err := s.hoverTooltip.Render(target); err != nil {
log.Printf("Cannot render tooltip, %e", err)
}
} }
return nil return nil