Skip to content

Commit

Permalink
Merge pull request #256 from gucio321/textureloader
Browse files Browse the repository at this point in the history
textureLoader: move texture loading from hseditors into hswidget
  • Loading branch information
essial authored Apr 1, 2021
2 parents c333f1c + dac8d55 commit e2f7fab
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 117 deletions.
71 changes: 71 additions & 0 deletions hswidget/cofwidget/state.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package cofwidget

import (
"fmt"
"log"

"github.com/ianling/giu"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2cof"

"github.com/OpenDiablo2/HellSpawner/hsassets"
"github.com/OpenDiablo2/HellSpawner/hswidget"
)

Expand All @@ -21,6 +26,14 @@ type widgetState struct {
*viewerState
*newLayerFields
mode
textures
}

type textures struct {
up *giu.Texture
down *giu.Texture
left *giu.Texture
right *giu.Texture
}

// Dispose clear widget's state
Expand Down Expand Up @@ -172,3 +185,61 @@ func (s *newLayerFields) Dispose() {
s.drawEffect = 0
s.weaponClass = 0
}

func (p *widget) getStateID() string {
return fmt.Sprintf("widget_%s", p.id)
}

func (p *widget) getState() *widgetState {
var state *widgetState

s := giu.Context.GetState(p.getStateID())

if s != nil {
state = s.(*widgetState)
} else {
p.initState()
state = p.getState()
}

return state
}

func (p *widget) setState(s giu.Disposable) {
giu.Context.SetState(p.getStateID(), s)
}

func (p *widget) initState() {
state := &widgetState{
mode: modeViewer,
viewerState: &viewerState{
confirmDialog: &hswidget.PopUpConfirmDialog{},
},
newLayerFields: &newLayerFields{
selectable: true,
drawEffect: int32(d2enum.DrawEffectNone),
},
}

if len(p.cof.CofLayers) > 0 {
state.viewerState.layer = &p.cof.CofLayers[0]
}

p.textureLoader.CreateTextureFromFile(hsassets.UpArrowIcon, func(texture *giu.Texture) {
state.textures.up = texture
})

p.textureLoader.CreateTextureFromFile(hsassets.DownArrowIcon, func(texture *giu.Texture) {
state.textures.down = texture
})

p.textureLoader.CreateTextureFromFile(hsassets.LeftArrowIcon, func(texture *giu.Texture) {
state.textures.left = texture
})

p.textureLoader.CreateTextureFromFile(hsassets.RightArrowIcon, func(texture *giu.Texture) {
state.textures.right = texture
})

p.setState(state)
}
84 changes: 16 additions & 68 deletions hswidget/cofwidget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2cof"

"github.com/OpenDiablo2/HellSpawner/hscommon"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsenum"
"github.com/OpenDiablo2/HellSpawner/hswidget"
)
Expand All @@ -22,35 +23,24 @@ const (
speedInputW = 40
)

type textures struct {
up *giu.Texture
down *giu.Texture
left *giu.Texture
right *giu.Texture
}

type widget struct {
id string
cof *d2cof.COF
textures
id string
cof *d2cof.COF
textureLoader hscommon.TextureLoader
}

// Create a new COF widget
func Create(
state []byte,
up, down, right, left *giu.Texture,
textureLoader hscommon.TextureLoader,
id string, cof *d2cof.COF,
) giu.Widget {
result := &widget{
id: id,
cof: cof,
id: id,
cof: cof,
textureLoader: textureLoader,
}

result.textures.up = up
result.textures.down = down
result.textures.left = left
result.textures.right = right

if giu.Context.GetState(result.getStateID()) == nil && state != nil {
s := &widgetState{
viewerState: &viewerState{},
Expand Down Expand Up @@ -81,61 +71,19 @@ func (p *widget) Build() {
}
}

func (p *widget) getStateID() string {
return fmt.Sprintf("widget_%s", p.id)
}

func (p *widget) getState() *widgetState {
var state *widgetState

s := giu.Context.GetState(p.getStateID())

if s != nil {
state = s.(*widgetState)
} else {
p.initState()
state = p.getState()
}

return state
}

func (p *widget) setState(s giu.Disposable) {
giu.Context.SetState(p.getStateID(), s)
}

func (p *widget) initState() {
state := &widgetState{
mode: modeViewer,
viewerState: &viewerState{
confirmDialog: &hswidget.PopUpConfirmDialog{},
},
newLayerFields: &newLayerFields{
selectable: true,
drawEffect: int32(d2enum.DrawEffectNone),
},
}

if len(p.cof.CofLayers) > 0 {
state.viewerState.layer = &p.cof.CofLayers[0]
}

p.setState(state)
}

func (p *widget) makeViewerLayout() giu.Layout {
state := p.getState()

return giu.Layout{
giu.TabBar("COFViewerTabs").Layout(giu.Layout{
giu.TabItem("Animation").Layout(p.makeAnimationTab()),
giu.TabItem("Animation").Layout(p.makeAnimationTab(state)),
giu.TabItem("Layer").Layout(p.makeLayerTab(state)),
giu.TabItem("Priority").Layout(p.makePriorityTab(state)),
}),
}
}

func (p *widget) makeAnimationTab() giu.Layout {
func (p *widget) makeAnimationTab(state *widgetState) giu.Layout {
const (
fmtFPS = "FPS: %.1f"
fmtDuration = "Duration: %.2fms"
Expand Down Expand Up @@ -168,7 +116,7 @@ func (p *widget) makeAnimationTab() giu.Layout {

return giu.Layout{
giu.Label(strLabelDirections),
p.layoutAnimFrames(),
p.layoutAnimFrames(state),
giu.Line(speedLabel, speedInput),
giu.Label(strLabelFPS),
giu.Label(strLabelDuration),
Expand Down Expand Up @@ -305,7 +253,7 @@ func (p *widget) makePriorityTab(state *widgetState) giu.Layout {
// the layout ends up looking like this:
// Frames (x6): <- 10 ->
// you use the arrows to set the number of frames per direction
func (p *widget) layoutAnimFrames() *giu.LineWidget {
func (p *widget) layoutAnimFrames(state *widgetState) *giu.LineWidget {
numFrames := p.cof.FramesPerDirection
numDirs := p.cof.NumberOfDirections

Expand All @@ -327,9 +275,9 @@ func (p *widget) layoutAnimFrames() *giu.LineWidget {
leftButtonID := fmt.Sprintf("##%sDecreaseFramesPerDirection", p.id)
rightButtonID := fmt.Sprintf("##%sIncreaseFramesPerDirection", p.id)

left := hswidget.MakeImageButton(leftButtonID, buttonWidthHeight, buttonWidthHeight, p.textures.left, fnDecrease)
left := hswidget.MakeImageButton(leftButtonID, buttonWidthHeight, buttonWidthHeight, state.textures.left, fnDecrease)
frameCount := giu.Label(fmt.Sprintf("%d", numFrames))
right := hswidget.MakeImageButton(rightButtonID, buttonWidthHeight, buttonWidthHeight, p.textures.right, fnIncrease)
right := hswidget.MakeImageButton(rightButtonID, buttonWidthHeight, buttonWidthHeight, state.textures.right, fnIncrease)

return giu.Line(label, left, frameCount, right)
}
Expand Down Expand Up @@ -424,8 +372,8 @@ func (p *widget) makeDirectionLayout() giu.Layout {
fnIncPriority := makeIncPriorityFn(currentIdx)
fnDecPriority := makeDecPriorityFn(currentIdx)

increasePriority := hswidget.MakeImageButton(strIncPri, buttonWidthHeight, buttonWidthHeight, p.textures.up, fnIncPriority)
decreasePriority := hswidget.MakeImageButton(strDecPri, buttonWidthHeight, buttonWidthHeight, p.textures.down, fnDecPriority)
increasePriority := hswidget.MakeImageButton(strIncPri, buttonWidthHeight, buttonWidthHeight, state.textures.up, fnIncPriority)
decreasePriority := hswidget.MakeImageButton(strDecPri, buttonWidthHeight, buttonWidthHeight, state.textures.down, fnDecPriority)

strLayerName := hsenum.GetLayerName(layers[idx])
strLayerLabel := fmt.Sprintf(fmtLayerLabel, idx, strLayerName)
Expand Down
17 changes: 13 additions & 4 deletions hswidget/fonttablewidget/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/ianling/giu"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils"

"github.com/OpenDiablo2/HellSpawner/hsassets"
)

type widgetMode int32
Expand All @@ -18,9 +20,10 @@ const (
)

type widgetState struct {
mode widgetMode
editRuneState editRuneState
addItemState addItemState
mode widgetMode
editRuneState editRuneState
addItemState addItemState
deleteButtonTexture *giu.Texture
}

// Dispose cleans state
Expand Down Expand Up @@ -135,9 +138,15 @@ func (p *widget) getState() *widgetState {
}

func (p *widget) initState() {
p.setState(&widgetState{
state := &widgetState{
mode: modeViewer,
}

p.textureLoader.CreateTextureFromFile(hsassets.DeleteIcon, func(texture *giu.Texture) {
state.deleteButtonTexture = texture
})

p.setState(state)
}

func (p *widget) setState(s giu.Disposable) {
Expand Down
17 changes: 9 additions & 8 deletions hswidget/fonttablewidget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font/d2fontglyph"

"github.com/OpenDiablo2/HellSpawner/hscommon"
"github.com/OpenDiablo2/HellSpawner/hswidget"
)

Expand All @@ -21,21 +22,21 @@ const (
)

type widget struct {
fontTable *d2font.Font
id string
deleteButtonTexture *giu.Texture
fontTable *d2font.Font
id string
textureLoader hscommon.TextureLoader
}

// Create creates a new FontTable widget
func Create(
state []byte,
del *giu.Texture,
tl hscommon.TextureLoader,
id string, fontTable *d2font.Font,
) giu.Widget {
result := &widget{
fontTable: fontTable,
id: id,
deleteButtonTexture: del,
fontTable: fontTable,
id: id,
textureLoader: tl,
}

if giu.Context.GetState(result.getStateID()) == nil && state != nil {
Expand Down Expand Up @@ -122,7 +123,7 @@ func (p *widget) makeGlyphLayout(r rune) *giu.RowWidget {
giu.Line(
hswidget.MakeImageButton("##"+p.id+"deleteFrame"+string(r),
delSize, delSize,
p.deleteButtonTexture,
state.deleteButtonTexture,
func() { p.deleteRow(r) },
),
),
Expand Down
28 changes: 3 additions & 25 deletions hswindow/hseditor/hscofeditor/cof_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/OpenDiablo2/HellSpawner/hscommon/hsproject"

"github.com/OpenDiablo2/HellSpawner/hsassets"
"github.com/OpenDiablo2/HellSpawner/hscommon"
"github.com/OpenDiablo2/HellSpawner/hsconfig"
"github.com/OpenDiablo2/HellSpawner/hsinput"
Expand All @@ -28,16 +27,11 @@ type COFEditor struct {
cof *d2cof.COF
textureLoader hscommon.TextureLoader
state []byte
textures struct {
up *g.Texture
down *g.Texture
right *g.Texture
left *g.Texture
}
}

// Create creates a new cof editor
func Create(config *hsconfig.Config, tl hscommon.TextureLoader,
func Create(config *hsconfig.Config,
tl hscommon.TextureLoader,
pathEntry *hscommon.PathEntry,
state []byte,
data *[]byte, x, y float32, project *hsproject.Project) (hscommon.EditorWindow, error) {
Expand All @@ -53,29 +47,13 @@ func Create(config *hsconfig.Config, tl hscommon.TextureLoader,
state: state,
}

tl.CreateTextureFromFile(hsassets.UpArrowIcon, func(texture *g.Texture) {
result.textures.up = texture
})

tl.CreateTextureFromFile(hsassets.DownArrowIcon, func(texture *g.Texture) {
result.textures.down = texture
})

tl.CreateTextureFromFile(hsassets.LeftArrowIcon, func(texture *g.Texture) {
result.textures.left = texture
})

tl.CreateTextureFromFile(hsassets.RightArrowIcon, func(texture *g.Texture) {
result.textures.right = texture
})

return result, nil
}

// Build builds a cof editor
func (e *COFEditor) Build() {
uid := e.Path.GetUniqueID()
cofWidget := cofwidget.Create(e.state, e.textures.up, e.textures.down, e.textures.right, e.textures.left, uid, e.cof)
cofWidget := cofwidget.Create(e.state, e.textureLoader, uid, e.cof)

e.IsOpen(&e.Visible)
e.Flags(g.WindowFlagsAlwaysAutoResize)
Expand Down
Loading

0 comments on commit e2f7fab

Please sign in to comment.