2020-02-17 22:11:52 -05:00
|
|
|
package d2gui
|
|
|
|
|
|
|
|
import (
|
2020-11-13 15:03:30 -05:00
|
|
|
"image/color"
|
2020-09-23 13:30:54 -04:00
|
|
|
"log"
|
2020-11-13 15:03:30 -05:00
|
|
|
"time"
|
2020-09-23 13:30:54 -04:00
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 17:31:45 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-02-17 22:11:52 -05:00
|
|
|
)
|
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
// Constants defining the main shades of basic colors
|
|
|
|
// found in the game
|
|
|
|
const (
|
|
|
|
ColorWhite = 0xffffffff
|
|
|
|
ColorRed = 0xdb3f3dff
|
|
|
|
ColorGreen = 0x00d000ff
|
|
|
|
ColorBlue = 0x5450d1ff
|
|
|
|
ColorBrown = 0xa1925dff
|
|
|
|
ColorGrey = 0x555555ff
|
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Label is renderable text
|
2020-02-17 22:11:52 -05:00
|
|
|
type Label struct {
|
|
|
|
widgetBase
|
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
renderer d2interface.Renderer
|
|
|
|
text string
|
|
|
|
font *d2asset.Font
|
|
|
|
surface d2interface.Surface
|
|
|
|
color color.RGBA
|
|
|
|
hoverColor color.RGBA
|
|
|
|
isHovered bool
|
|
|
|
isBlinking bool
|
|
|
|
isDisplayed bool
|
|
|
|
blinkTimer time.Time
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
func createLabel(renderer d2interface.Renderer, text string, font *d2asset.Font, col color.RGBA) *Label {
|
2020-07-03 14:00:56 -04:00
|
|
|
label := &Label{
|
2020-11-13 15:03:30 -05:00
|
|
|
font: font,
|
|
|
|
renderer: renderer,
|
|
|
|
color: col,
|
|
|
|
hoverColor: col,
|
2020-07-03 14:00:56 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
err := label.setText(text)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
label.SetVisible(true)
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
return label
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
// SetHoverColor will set the value of hoverColor
|
|
|
|
func (l *Label) SetHoverColor(col color.RGBA) {
|
|
|
|
l.hoverColor = col
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIsBlinking will set the isBlinking value
|
|
|
|
func (l *Label) SetIsBlinking(isBlinking bool) {
|
|
|
|
l.isBlinking = isBlinking
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIsHovered will set the isHovered value
|
|
|
|
func (l *Label) SetIsHovered(isHovered bool) error {
|
|
|
|
l.isHovered = isHovered
|
|
|
|
|
|
|
|
return l.setText(l.text)
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
func (l *Label) render(target d2interface.Surface) {
|
2020-11-13 15:03:30 -05:00
|
|
|
if l.isBlinking && time.Since(l.blinkTimer) >= 200*time.Millisecond {
|
|
|
|
l.isDisplayed = !l.isDisplayed
|
|
|
|
l.blinkTimer = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.isBlinking && !l.isDisplayed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
target.Render(l.surface)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (l *Label) getSize() (width, height int) {
|
2020-02-17 22:11:52 -05:00
|
|
|
return l.surface.GetSize()
|
|
|
|
}
|
2020-06-25 16:27:16 -04:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// GetText returns the label text
|
2020-06-25 16:27:16 -04:00
|
|
|
func (l *Label) GetText() string {
|
|
|
|
return l.text
|
|
|
|
}
|
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
// SetColor sets the label text
|
|
|
|
func (l *Label) SetColor(col color.RGBA) error {
|
|
|
|
l.color = col
|
|
|
|
return l.setText(l.text)
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetText sets the label text
|
2020-06-25 16:27:16 -04:00
|
|
|
func (l *Label) SetText(text string) error {
|
|
|
|
if text == l.text {
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
return l.setText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) setText(text string) error {
|
|
|
|
width, height := l.font.GetTextMetrics(text)
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
surface := l.renderer.NewSurface(width, height)
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
col := l.color
|
|
|
|
if l.isHovered {
|
|
|
|
col = l.hoverColor
|
|
|
|
}
|
|
|
|
|
|
|
|
l.font.SetColor(col)
|
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
if err := l.font.RenderText(text, surface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
l.surface = surface
|
|
|
|
l.text = text
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
return nil
|
|
|
|
}
|