mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-19 10:56:07 -05:00
7e3aff557b
* 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) ```
289 lines
6.6 KiB
Go
289 lines
6.6 KiB
Go
package d2gamescreen
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2screen"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
|
)
|
|
|
|
const (
|
|
creditsX, creditsY = 0, 0
|
|
charSelExitBtnX, charSelExitBtnY = 33, 543
|
|
)
|
|
|
|
const secondsPerCycle float64 = 0.02
|
|
|
|
type labelItem struct {
|
|
Label *d2ui.Label
|
|
IsHeading bool
|
|
Available bool
|
|
}
|
|
|
|
// Credits represents the credits screen
|
|
type Credits struct {
|
|
creditsBackground *d2ui.Sprite
|
|
exitButton *d2ui.Button
|
|
creditsText []string
|
|
labels []*labelItem
|
|
cycleTime float64
|
|
cyclesTillNextLine int
|
|
doneWithCredits bool
|
|
|
|
asset *d2asset.AssetManager
|
|
renderer d2interface.Renderer
|
|
navigator Navigator
|
|
uiManager *d2ui.UIManager
|
|
}
|
|
|
|
// CreateCredits creates an instance of the credits screen
|
|
func CreateCredits(navigator Navigator, asset *d2asset.AssetManager, renderer d2interface.Renderer,
|
|
ui *d2ui.UIManager) *Credits {
|
|
result := &Credits{
|
|
asset: asset,
|
|
labels: make([]*labelItem, 0),
|
|
cycleTime: 0,
|
|
doneWithCredits: false,
|
|
cyclesTillNextLine: 0,
|
|
renderer: renderer,
|
|
navigator: navigator,
|
|
uiManager: ui,
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// LoadContributors loads the contributors data from file
|
|
// TODO: use markdown for file and convert it to the suitable format
|
|
func (v *Credits) LoadContributors() []string {
|
|
file, err := os.Open(path.Join("./", "CONTRIBUTORS"))
|
|
if err != nil || file == nil {
|
|
log.Print("CONTRIBUTORS file is missing")
|
|
return []string{"MISSING CONTRIBUTOR FILES!"}
|
|
}
|
|
|
|
defer func() {
|
|
if err = file.Close(); err != nil {
|
|
fmt.Printf("an error occurred while closing file: %s, err: %q\n", file.Name(), err)
|
|
}
|
|
}()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
var contributors []string
|
|
for scanner.Scan() {
|
|
contributors = append(contributors, strings.Trim(scanner.Text(), " "))
|
|
}
|
|
|
|
return contributors
|
|
}
|
|
|
|
// OnLoad is called to load the resources for the credits screen
|
|
func (v *Credits) OnLoad(loading d2screen.LoadingState) {
|
|
v.creditsBackground, _ = v.uiManager.NewSprite(d2resource.CreditsBackground, d2resource.PaletteSky)
|
|
v.creditsBackground.SetPosition(creditsX, creditsY)
|
|
loading.Progress(twentyPercent)
|
|
|
|
v.exitButton = v.uiManager.NewButton(d2ui.ButtonTypeMedium, "EXIT")
|
|
v.exitButton.SetPosition(charSelExitBtnX, charSelExitBtnY)
|
|
v.exitButton.OnActivated(func() { v.onExitButtonClicked() })
|
|
loading.Progress(fourtyPercent)
|
|
|
|
fileData, err := v.asset.LoadFile(d2resource.CreditsText)
|
|
if err != nil {
|
|
loading.Error(err)
|
|
return
|
|
}
|
|
|
|
loading.Progress(sixtyPercent)
|
|
|
|
creditData, _ := d2util.Utf16BytesToString(fileData[2:])
|
|
v.creditsText = strings.Split(creditData, "\r\n")
|
|
|
|
for i := range v.creditsText {
|
|
v.creditsText[i] = strings.Trim(v.creditsText[i], " ")
|
|
}
|
|
|
|
loading.Progress(eightyPercent)
|
|
|
|
v.creditsText = append(v.LoadContributors(), v.creditsText...)
|
|
}
|
|
|
|
// Render renders the credits screen
|
|
func (v *Credits) Render(screen d2interface.Surface) error {
|
|
err := v.creditsBackground.RenderSegmented(screen, 4, 3, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, label := range v.labels {
|
|
if label.Available {
|
|
continue
|
|
}
|
|
|
|
label.Label.Render(screen)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Advance runs the update logic on the credits screen
|
|
func (v *Credits) Advance(tickTime float64) error {
|
|
v.cycleTime += tickTime
|
|
for v.cycleTime >= secondsPerCycle {
|
|
v.cycleTime -= secondsPerCycle
|
|
v.cyclesTillNextLine--
|
|
|
|
if !v.doneWithCredits && v.cyclesTillNextLine <= 0 {
|
|
v.addNextItem()
|
|
}
|
|
|
|
for _, label := range v.labels {
|
|
if label.Available {
|
|
continue
|
|
}
|
|
|
|
if label.Label.Y-1 < -15 {
|
|
label.Available = true
|
|
continue
|
|
}
|
|
label.Label.Y--
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (v *Credits) onExitButtonClicked() {
|
|
v.navigator.ToMainMenu()
|
|
}
|
|
|
|
func (v *Credits) addNextItem() {
|
|
if len(v.creditsText) == 0 {
|
|
v.doneWithCredits = true
|
|
return
|
|
}
|
|
|
|
text := v.creditsText[0]
|
|
v.creditsText = v.creditsText[1:]
|
|
|
|
if text == "" {
|
|
if v.creditsText[0][0] == '*' {
|
|
v.cyclesTillNextLine = 38
|
|
return
|
|
}
|
|
|
|
v.cyclesTillNextLine = 19
|
|
|
|
return
|
|
}
|
|
|
|
isHeading := text[0] == '*'
|
|
isNextHeading := len(v.creditsText) > 0 && len(v.creditsText[0]) > 0 && v.creditsText[0][0] == '*'
|
|
isNextSpace := len(v.creditsText) > 0 && v.creditsText[0] == ""
|
|
|
|
var label = v.getNewFontLabel(isHeading)
|
|
|
|
if isHeading {
|
|
label.SetText(text[1:])
|
|
} else {
|
|
label.SetText(text)
|
|
}
|
|
|
|
isDoubled, isNextHeading := v.setItemLabelPosition(label, isHeading, isNextHeading, isNextSpace)
|
|
|
|
switch {
|
|
case isHeading && isNextHeading:
|
|
v.cyclesTillNextLine = 38
|
|
case isNextHeading:
|
|
if isDoubled {
|
|
v.cyclesTillNextLine = 38
|
|
} else {
|
|
v.cyclesTillNextLine = 57
|
|
}
|
|
case isHeading:
|
|
v.cyclesTillNextLine = 38
|
|
default:
|
|
v.cyclesTillNextLine = 19
|
|
}
|
|
}
|
|
|
|
const (
|
|
itemLabelY = 605
|
|
itemLabelX = 400
|
|
itemLabel2offsetX = 10
|
|
halfItemLabel2offsetX = itemLabel2offsetX / 2
|
|
)
|
|
|
|
func (v *Credits) setItemLabelPosition(label *d2ui.Label, isHeading, isNextHeading, isNextSpace bool) (isDoubled, nextHeading bool) {
|
|
width, _ := label.GetSize()
|
|
half := 2
|
|
halfWidth := width / half
|
|
|
|
if !isHeading && !isNextHeading && !isNextSpace {
|
|
isDoubled = true
|
|
// Gotta go side by side
|
|
label.SetPosition(itemLabelX-width, itemLabelY)
|
|
|
|
text2 := v.creditsText[0]
|
|
v.creditsText = v.creditsText[1:]
|
|
|
|
nextHeading = len(v.creditsText) > 0 && len(v.creditsText[0]) > 0 && v.creditsText[0][0] == '*'
|
|
label2 := v.getNewFontLabel(isHeading)
|
|
label2.SetText(text2)
|
|
|
|
label2.SetPosition(itemLabelX+itemLabel2offsetX, itemLabelY)
|
|
|
|
return isDoubled, nextHeading
|
|
}
|
|
|
|
label.SetPosition(itemLabelX+halfItemLabel2offsetX-halfWidth, itemLabelY)
|
|
|
|
return isDoubled, isNextHeading
|
|
}
|
|
|
|
const (
|
|
lightRed = 0xff5852ff
|
|
beige = 0xc6b296ff
|
|
)
|
|
|
|
func (v *Credits) getNewFontLabel(isHeading bool) *d2ui.Label {
|
|
for _, label := range v.labels {
|
|
if label.Available {
|
|
label.Available = false
|
|
if isHeading {
|
|
label.Label.Color[0] = rgbaColor(lightRed)
|
|
} else {
|
|
label.Label.Color[0] = rgbaColor(beige)
|
|
}
|
|
|
|
return label.Label
|
|
}
|
|
}
|
|
|
|
newLabelItem := &labelItem{
|
|
Available: false,
|
|
IsHeading: isHeading,
|
|
Label: v.uiManager.NewLabel(d2resource.FontFormal10, d2resource.PaletteSky),
|
|
}
|
|
|
|
if isHeading {
|
|
newLabelItem.Label.Color[0] = rgbaColor(lightRed)
|
|
} else {
|
|
newLabelItem.Label.Color[0] = rgbaColor(beige)
|
|
}
|
|
|
|
v.labels = append(v.labels, newLabelItem)
|
|
|
|
return newLabelItem.Label
|
|
}
|