2019-12-26 11:13:05 -05:00
|
|
|
package d2surface
|
|
|
|
|
|
|
|
import (
|
2019-12-28 16:46:08 -05:00
|
|
|
"fmt"
|
2019-12-26 11:13:05 -05:00
|
|
|
"image/color"
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2corehelper"
|
2019-12-26 11:13:05 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type surfaceState struct {
|
2019-12-28 16:46:08 -05:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
mode ebiten.CompositeMode
|
|
|
|
filter ebiten.Filter
|
|
|
|
color color.Color
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Surface struct {
|
|
|
|
stateStack []surfaceState
|
|
|
|
stateCurrent surfaceState
|
|
|
|
image *ebiten.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateSurface(image *ebiten.Image) *Surface {
|
|
|
|
return &Surface{
|
|
|
|
image: image,
|
|
|
|
stateCurrent: surfaceState{
|
2019-12-28 16:46:08 -05:00
|
|
|
filter: ebiten.FilterNearest,
|
|
|
|
mode: ebiten.CompositeModeSourceOver,
|
2019-12-26 11:13:05 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Surface) PushTranslation(x, y int) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.x += x
|
|
|
|
s.stateCurrent.y += y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Surface) PushCompositeMode(mode ebiten.CompositeMode) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.mode = mode
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (s *Surface) PushFilter(filter ebiten.Filter) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.filter = filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Surface) PushColor(color color.Color) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.color = color
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
func (s *Surface) Pop() {
|
|
|
|
count := len(s.stateStack)
|
|
|
|
if count == 0 {
|
|
|
|
panic("empty stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stateCurrent = s.stateStack[count-1]
|
|
|
|
s.stateStack = s.stateStack[:count-1]
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (s *Surface) PopN(n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
s.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
func (s *Surface) Render(image *ebiten.Image) error {
|
|
|
|
opts := &ebiten.DrawImageOptions{CompositeMode: s.stateCurrent.mode}
|
|
|
|
opts.GeoM.Translate(float64(s.stateCurrent.x), float64(s.stateCurrent.y))
|
2019-12-28 16:46:08 -05:00
|
|
|
opts.Filter = s.stateCurrent.filter
|
|
|
|
if s.stateCurrent.color != nil {
|
|
|
|
opts.ColorM = d2corehelper.ColorToColorM(s.stateCurrent.color)
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
return s.image.DrawImage(image, opts)
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (s *Surface) DrawText(format string, params ...interface{}) {
|
|
|
|
ebitenutil.DebugPrintAt(s.image, fmt.Sprintf(format, params...), s.stateCurrent.x, s.stateCurrent.y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Surface) DrawLine(x, y int, color color.Color) {
|
|
|
|
ebitenutil.DrawLine(
|
|
|
|
s.image,
|
|
|
|
float64(s.stateCurrent.x),
|
|
|
|
float64(s.stateCurrent.y),
|
|
|
|
float64(s.stateCurrent.x+x),
|
|
|
|
float64(s.stateCurrent.y+y),
|
|
|
|
color,
|
|
|
|
)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Surface) DrawRect(width, height int, color color.Color) {
|
|
|
|
ebitenutil.DrawRect(
|
|
|
|
s.image,
|
|
|
|
float64(s.stateCurrent.x),
|
|
|
|
float64(s.stateCurrent.y),
|
|
|
|
float64(width),
|
|
|
|
float64(height),
|
|
|
|
color,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (s *Surface) Clear(color color.Color) error {
|
|
|
|
return s.image.Fill(color)
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
func (s *Surface) GetSize() (int, int) {
|
|
|
|
return s.image.Size()
|
|
|
|
}
|
2019-12-28 16:46:08 -05:00
|
|
|
|
|
|
|
func (s *Surface) GetDepth() int {
|
|
|
|
return len(s.stateStack)
|
|
|
|
}
|