2020-02-17 22:11:52 -05:00
|
|
|
package d2gui
|
|
|
|
|
|
|
|
import (
|
2020-09-12 16:51:30 -04:00
|
|
|
"errors"
|
2020-11-13 15:03:30 -05:00
|
|
|
"image/color"
|
2020-09-12 16:51:30 -04:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-08-05 00:03:33 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2020-11-13 15:03:30 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-09-18 16:10:52 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-02-17 22:11:52 -05:00
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
const layoutDebug = false // turns on debug rendering stuff for layouts
|
|
|
|
|
|
|
|
const (
|
|
|
|
white = 0xffffff_ff
|
|
|
|
magenta = 0xff00ff_ff
|
|
|
|
grey2 = 0x808080_ff
|
|
|
|
green = 0x0000ff_ff
|
|
|
|
yellow = 0xffff00_ff
|
|
|
|
)
|
|
|
|
|
|
|
|
// VerticalAlign type, determines alignment along y-axis within a layout
|
2020-02-24 22:35:21 -05:00
|
|
|
type VerticalAlign int
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// VerticalAlign types
|
2020-02-24 22:35:21 -05:00
|
|
|
const (
|
|
|
|
VerticalAlignTop VerticalAlign = iota
|
|
|
|
VerticalAlignMiddle
|
|
|
|
VerticalAlignBottom
|
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// HorizontalAlign type, determines alignment along x-axis within a layout
|
2020-02-24 22:35:21 -05:00
|
|
|
type HorizontalAlign int
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Horizontal alignment types
|
2020-02-24 22:35:21 -05:00
|
|
|
const (
|
|
|
|
HorizontalAlignLeft HorizontalAlign = iota
|
|
|
|
HorizontalAlignCenter
|
|
|
|
HorizontalAlignRight
|
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// PositionType determines layout positioning
|
2020-02-24 22:35:21 -05:00
|
|
|
type PositionType int
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Positioning types
|
2020-02-24 22:35:21 -05:00
|
|
|
const (
|
|
|
|
PositionTypeAbsolute PositionType = iota
|
|
|
|
PositionTypeVertical
|
|
|
|
PositionTypeHorizontal
|
|
|
|
)
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
// static check that Layout implements a widget
|
|
|
|
var _ widget = &Layout{}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Layout is a gui element container which will automatically position/align gui elements.
|
|
|
|
// Layouts are gui elements as well, so they can be nested in other layouts.
|
2020-02-17 22:11:52 -05:00
|
|
|
type Layout struct {
|
|
|
|
widgetBase
|
2020-02-24 22:35:21 -05:00
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
renderer d2interface.Renderer
|
|
|
|
assetManager *d2asset.AssetManager
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
verticalAlign VerticalAlign
|
|
|
|
horizontalAlign HorizontalAlign
|
|
|
|
positionType PositionType
|
|
|
|
entries []*layoutEntry
|
|
|
|
}
|
|
|
|
|
2020-10-22 02:41:21 -04:00
|
|
|
// CreateLayout creates a new GUI layout
|
2020-09-18 16:10:52 -04:00
|
|
|
func CreateLayout(renderer d2interface.Renderer, positionType PositionType, assetManager *d2asset.AssetManager) *Layout {
|
2020-07-03 14:00:56 -04:00
|
|
|
layout := &Layout{
|
|
|
|
renderer: renderer,
|
|
|
|
positionType: positionType,
|
2020-09-18 16:10:52 -04:00
|
|
|
assetManager: assetManager,
|
2020-07-03 14:00:56 -04:00
|
|
|
}
|
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
layout.SetVisible(true)
|
|
|
|
|
|
|
|
return layout
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetSize sets the size of the layout
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) SetSize(width, height int) {
|
|
|
|
l.width = width
|
|
|
|
l.height = height
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetVerticalAlign sets the vertical alignment type of the layout
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) SetVerticalAlign(verticalAlign VerticalAlign) {
|
|
|
|
l.verticalAlign = verticalAlign
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetHorizontalAlign sets the horizontal alignment type of the layout
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) SetHorizontalAlign(horizontalAlign HorizontalAlign) {
|
|
|
|
l.horizontalAlign = horizontalAlign
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddLayout adds a nested layout to this layout, given a position type.
|
|
|
|
// Returns a pointer to the nested layout
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) AddLayout(positionType PositionType) *Layout {
|
2020-09-18 16:10:52 -04:00
|
|
|
layout := CreateLayout(l.renderer, positionType, l.assetManager)
|
2020-02-24 22:35:21 -05:00
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: layout})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-17 22:11:52 -05:00
|
|
|
return layout
|
|
|
|
}
|
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
// AddLayoutFromSource adds a nested layout to this layout, given a position type.
|
|
|
|
// Returns a pointer to the nested layout
|
|
|
|
func (l *Layout) AddLayoutFromSource(source *Layout) *Layout {
|
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: source})
|
|
|
|
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddSpacerStatic adds a spacer with explicitly defined height and width
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) AddSpacerStatic(width, height int) *SpacerStatic {
|
|
|
|
spacer := createSpacerStatic(width, height)
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: spacer})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
return spacer
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddSpacerDynamic adds a spacer which has dynamic width and height. The width
|
|
|
|
// and height are computed based off of the position/alignment type of the layout
|
|
|
|
// and the dimensions/positions of the layout entries.
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) AddSpacerDynamic() *SpacerDynamic {
|
|
|
|
spacer := createSpacerDynamic()
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: spacer})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
return spacer
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddSprite given a path and palette, adds a Sprite as a layout entry
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) AddSprite(imagePath, palettePath string) (*Sprite, error) {
|
2020-09-18 16:10:52 -04:00
|
|
|
sprite, err := createSprite(imagePath, palettePath, l.assetManager)
|
2020-02-24 22:35:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: sprite})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
return sprite, nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddAnimatedSprite given a path, palette, and direction will add an animated
|
|
|
|
// sprite as a layout entry
|
2020-06-25 16:27:16 -04:00
|
|
|
func (l *Layout) AddAnimatedSprite(imagePath, palettePath string, direction AnimationDirection) (*AnimatedSprite, error) {
|
2020-09-18 16:10:52 -04:00
|
|
|
sprite, err := createAnimatedSprite(imagePath, palettePath, direction, l.assetManager)
|
2020-06-25 16:27:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: sprite})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
return sprite, nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddLabel given a string and a FontStyle, adds a text label as a layout entry
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) AddLabel(text string, fontStyle FontStyle) (*Label, error) {
|
2020-09-18 16:10:52 -04:00
|
|
|
font, err := l.loadFont(fontStyle)
|
2020-02-24 22:35:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-13 15:03:30 -05:00
|
|
|
label := createLabel(l.renderer, text, font, d2util.Color(ColorWhite))
|
|
|
|
|
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: label})
|
|
|
|
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLabelWithColor given a string and a FontStyle and a Color, adds a text label as a layout entry
|
|
|
|
func (l *Layout) AddLabelWithColor(text string, fontStyle FontStyle, col color.RGBA) (*Label, error) {
|
|
|
|
font, err := l.loadFont(fontStyle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
label := createLabel(l.renderer, text, font, col)
|
2020-09-18 16:10:52 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: label})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AddButton given a string and ButtonStyle, adds a button as a layout entry
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) AddButton(text string, buttonStyle ButtonStyle) (*Button, error) {
|
2020-09-12 16:51:30 -04:00
|
|
|
button, err := l.createButton(l.renderer, text, buttonStyle)
|
2020-02-24 22:35:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.entries = append(l.entries, &layoutEntry{widget: button})
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
return button, nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Clear removes all layout entries
|
2020-02-24 22:35:21 -05:00
|
|
|
func (l *Layout) Clear() {
|
|
|
|
l.entries = nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
func (l *Layout) render(target d2interface.Surface) {
|
2020-06-25 17:28:48 -04:00
|
|
|
l.AdjustEntryPlacement()
|
2020-02-24 22:35:21 -05:00
|
|
|
|
2020-02-17 22:11:52 -05:00
|
|
|
for _, entry := range l.entries {
|
2020-02-24 22:35:21 -05:00
|
|
|
if !entry.widget.isVisible() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
l.renderEntry(entry, target)
|
2020-02-24 22:35:21 -05:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
if layoutDebug {
|
2020-10-28 14:17:42 -04:00
|
|
|
l.renderEntryDebug(entry, target)
|
2020-07-18 18:06:36 -04:00
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Layout) advance(elapsed float64) error {
|
|
|
|
for _, entry := range l.entries {
|
2020-02-24 22:35:21 -05:00
|
|
|
if err := entry.widget.advance(elapsed); err != nil {
|
|
|
|
return err
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
func (l *Layout) renderEntry(entry *layoutEntry, target d2interface.Surface) {
|
2020-02-24 22:35:21 -05:00
|
|
|
target.PushTranslation(entry.x, entry.y)
|
2020-02-17 22:11:52 -05:00
|
|
|
defer target.Pop()
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
entry.widget.render(target)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
func (l *Layout) renderEntryDebug(entry *layoutEntry, target d2interface.Surface) {
|
2020-02-24 22:35:21 -05:00
|
|
|
target.PushTranslation(entry.x, entry.y)
|
2020-02-17 22:11:52 -05:00
|
|
|
defer target.Pop()
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
drawColor := rgbaColor(white)
|
2020-02-24 22:35:21 -05:00
|
|
|
switch entry.widget.(type) {
|
|
|
|
case *Layout:
|
2020-07-18 18:06:36 -04:00
|
|
|
drawColor = rgbaColor(magenta)
|
2020-02-24 22:35:21 -05:00
|
|
|
case *SpacerStatic, *SpacerDynamic:
|
2020-07-18 18:06:36 -04:00
|
|
|
drawColor = rgbaColor(grey2)
|
2020-02-24 22:35:21 -05:00
|
|
|
case *Label:
|
2020-07-18 18:06:36 -04:00
|
|
|
drawColor = rgbaColor(green)
|
2020-02-24 22:35:21 -05:00
|
|
|
case *Button:
|
2020-07-18 18:06:36 -04:00
|
|
|
drawColor = rgbaColor(yellow)
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
target.DrawLine(entry.width, 0, drawColor)
|
|
|
|
target.DrawLine(0, entry.height, drawColor)
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
target.PushTranslation(entry.width, 0)
|
|
|
|
target.DrawLine(0, entry.height, drawColor)
|
2020-02-17 22:11:52 -05:00
|
|
|
target.Pop()
|
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
target.PushTranslation(0, entry.height)
|
|
|
|
target.DrawLine(entry.width, 0, drawColor)
|
2020-02-17 22:11:52 -05:00
|
|
|
target.Pop()
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (l *Layout) getContentSize() (width, height int) {
|
2020-02-24 22:35:21 -05:00
|
|
|
for _, entry := range l.entries {
|
|
|
|
x, y := entry.widget.getPosition()
|
|
|
|
w, h := entry.widget.getSize()
|
|
|
|
|
|
|
|
switch l.positionType {
|
|
|
|
case PositionTypeVertical:
|
2020-08-05 00:03:33 -04:00
|
|
|
width = d2math.MaxInt(width, w)
|
2020-02-24 22:35:21 -05:00
|
|
|
height += h
|
|
|
|
case PositionTypeHorizontal:
|
|
|
|
width += w
|
2020-08-05 00:03:33 -04:00
|
|
|
height = d2math.MaxInt(height, h)
|
2020-02-24 22:35:21 -05:00
|
|
|
case PositionTypeAbsolute:
|
2020-08-05 00:03:33 -04:00
|
|
|
width = d2math.MaxInt(width, x+w)
|
|
|
|
height = d2math.MaxInt(height, y+h)
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return width, height
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:41:21 -04:00
|
|
|
// GetSize returns the layout width and height
|
2020-10-07 21:20:05 -04:00
|
|
|
func (l *Layout) GetSize() (width, height int) {
|
|
|
|
return l.getSize()
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (l *Layout) getSize() (width, height int) {
|
|
|
|
width, height = l.getContentSize()
|
2020-08-05 00:03:33 -04:00
|
|
|
return d2math.MaxInt(width, l.width), d2math.MaxInt(height, l.height)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (l *Layout) onMouseButtonDown(event d2interface.MouseEvent) bool {
|
2020-02-17 22:11:52 -05:00
|
|
|
for _, entry := range l.entries {
|
2020-07-08 09:15:20 -04:00
|
|
|
if entry.IsIn(event) {
|
2020-11-13 15:03:30 -05:00
|
|
|
entry.widget.onMouseButtonClick(event)
|
2020-07-08 09:15:20 -04:00
|
|
|
entry.widget.onMouseButtonDown(event)
|
2020-07-03 15:09:16 -04:00
|
|
|
entry.mouseDown[event.Button()] = true
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (l *Layout) onMouseButtonUp(event d2interface.MouseEvent) bool {
|
2020-02-17 22:11:52 -05:00
|
|
|
for _, entry := range l.entries {
|
2020-07-08 09:15:20 -04:00
|
|
|
if entry.IsIn(event) {
|
2020-07-03 15:09:16 -04:00
|
|
|
if entry.mouseDown[event.Button()] {
|
2020-07-08 09:15:20 -04:00
|
|
|
entry.widget.onMouseButtonClick(event)
|
|
|
|
entry.widget.onMouseButtonUp(event)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
entry.mouseDown[event.Button()] = false
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (l *Layout) onMouseMove(event d2interface.MouseMoveEvent) bool {
|
2020-02-17 22:11:52 -05:00
|
|
|
for _, entry := range l.entries {
|
2020-07-08 09:15:20 -04:00
|
|
|
if entry.IsIn(event) {
|
|
|
|
entry.widget.onMouseMove(event)
|
2020-02-17 22:11:52 -05:00
|
|
|
|
|
|
|
if entry.mouseOver {
|
2020-07-08 09:15:20 -04:00
|
|
|
entry.widget.onMouseOver(event)
|
2020-02-17 22:11:52 -05:00
|
|
|
} else {
|
2020-07-08 09:15:20 -04:00
|
|
|
entry.widget.onMouseEnter(event)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
2020-07-08 09:15:20 -04:00
|
|
|
|
2020-02-17 22:11:52 -05:00
|
|
|
entry.mouseOver = true
|
|
|
|
} else if entry.mouseOver {
|
2020-07-08 09:15:20 -04:00
|
|
|
entry.widget.onMouseLeave(event)
|
2020-02-17 22:11:52 -05:00
|
|
|
entry.mouseOver = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AdjustEntryPlacement calculates and sets the position for all layout entries.
|
|
|
|
// This is based on the position/horizontal/vertical alignment type set, as well as the
|
|
|
|
// expansion types of spacers.
|
2020-06-25 17:28:48 -04:00
|
|
|
func (l *Layout) AdjustEntryPlacement() {
|
2020-02-24 22:35:21 -05:00
|
|
|
width, height := l.getSize()
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
var expanderCount, expanderWidth, expanderHeight int
|
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
for _, entry := range l.entries {
|
|
|
|
if entry.widget.isVisible() && entry.widget.isExpanding() {
|
|
|
|
expanderCount++
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
if expanderCount > 0 {
|
|
|
|
contentWidth, contentHeight := l.getContentSize()
|
|
|
|
|
|
|
|
switch l.positionType {
|
|
|
|
case PositionTypeVertical:
|
|
|
|
expanderHeight = (height - contentHeight) / expanderCount
|
|
|
|
case PositionTypeHorizontal:
|
|
|
|
expanderWidth = (width - contentWidth) / expanderCount
|
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-08-05 00:03:33 -04:00
|
|
|
expanderWidth = d2math.MaxInt(0, expanderWidth)
|
|
|
|
expanderHeight = d2math.MaxInt(0, expanderHeight)
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var offsetX, offsetY int
|
2020-07-18 18:06:36 -04:00
|
|
|
|
|
|
|
for idx := range l.entries {
|
|
|
|
entry := l.entries[idx]
|
2020-02-24 22:35:21 -05:00
|
|
|
if !entry.widget.isVisible() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.widget.isExpanding() {
|
|
|
|
entry.width, entry.height = expanderWidth, expanderHeight
|
|
|
|
} else {
|
|
|
|
entry.width, entry.height = entry.widget.getSize()
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
l.handleEntryPosition(offsetX, offsetY, entry)
|
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
switch l.positionType {
|
|
|
|
case PositionTypeVertical:
|
|
|
|
offsetY += entry.height
|
|
|
|
case PositionTypeHorizontal:
|
|
|
|
offsetX += entry.width
|
|
|
|
}
|
2020-06-25 16:27:16 -04:00
|
|
|
|
2020-07-08 09:15:20 -04:00
|
|
|
sx, sy := l.ScreenPos()
|
2020-07-18 18:06:36 -04:00
|
|
|
entry.widget.SetScreenPos(entry.x+sx, entry.y+sy)
|
2020-06-25 16:27:16 -04:00
|
|
|
entry.widget.setOffset(offsetX, offsetY)
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
2020-07-08 09:15:20 -04:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (l *Layout) handleEntryPosition(offsetX, offsetY int, entry *layoutEntry) {
|
|
|
|
width, height := l.getSize()
|
|
|
|
|
|
|
|
switch l.positionType {
|
|
|
|
case PositionTypeVertical:
|
|
|
|
entry.y = offsetY
|
|
|
|
l.handleEntryVerticalAlign(width, entry)
|
|
|
|
|
|
|
|
case PositionTypeHorizontal:
|
|
|
|
entry.x = offsetX
|
|
|
|
l.handleEntryHorizontalAlign(height, entry)
|
|
|
|
|
|
|
|
case PositionTypeAbsolute:
|
|
|
|
entry.x, entry.y = entry.widget.getPosition()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Layout) handleEntryHorizontalAlign(height int, entry *layoutEntry) {
|
|
|
|
switch l.verticalAlign {
|
|
|
|
case VerticalAlignTop:
|
|
|
|
entry.y = 0
|
|
|
|
case VerticalAlignMiddle:
|
|
|
|
entry.y = half(height) - half(entry.height)
|
|
|
|
case VerticalAlignBottom:
|
|
|
|
entry.y = height - entry.height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Layout) handleEntryVerticalAlign(width int, entry *layoutEntry) {
|
|
|
|
switch l.horizontalAlign {
|
|
|
|
case HorizontalAlignLeft:
|
|
|
|
entry.x = 0
|
|
|
|
case HorizontalAlignCenter:
|
|
|
|
entry.x = half(width) - half(entry.width)
|
|
|
|
case HorizontalAlignRight:
|
|
|
|
entry.x = width - entry.width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
func (l *Layout) createButton(renderer d2interface.Renderer, text string,
|
|
|
|
buttonStyle ButtonStyle) (*Button,
|
|
|
|
error) {
|
|
|
|
config := getButtonStyleConfig(buttonStyle)
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("invalid button style")
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
animation, loadErr := l.assetManager.LoadAnimation(config.animationPath, config.palettePath)
|
2020-09-12 16:51:30 -04:00
|
|
|
if loadErr != nil {
|
|
|
|
return nil, loadErr
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
var buttonWidth int
|
|
|
|
|
|
|
|
for i := 0; i < config.segmentsX; i++ {
|
|
|
|
w, _, err := animation.GetFrameSize(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonWidth += w
|
|
|
|
}
|
|
|
|
|
|
|
|
var buttonHeight int
|
|
|
|
|
|
|
|
for i := 0; i < config.segmentsY; i++ {
|
|
|
|
_, h, err := animation.GetFrameSize(i * config.segmentsY)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonHeight += h
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
font, loadErr := l.loadFont(config.fontStyle)
|
2020-09-12 16:51:30 -04:00
|
|
|
if loadErr != nil {
|
|
|
|
return nil, loadErr
|
|
|
|
}
|
|
|
|
|
|
|
|
textColor := rgbaColor(grey)
|
|
|
|
textWidth, textHeight := font.GetTextMetrics(text)
|
|
|
|
textX := half(buttonWidth) - half(textWidth)
|
|
|
|
textY := half(buttonHeight) - half(textHeight) + config.textOffset
|
|
|
|
|
|
|
|
surfaceCount := animation.GetFrameCount() / (config.segmentsX * config.segmentsY)
|
|
|
|
surfaces := make([]d2interface.Surface, surfaceCount)
|
|
|
|
|
|
|
|
for i := 0; i < surfaceCount; i++ {
|
2020-10-28 14:17:42 -04:00
|
|
|
surface := renderer.NewSurface(buttonWidth, buttonHeight)
|
2020-09-12 16:51:30 -04:00
|
|
|
|
|
|
|
segX, segY, frame := config.segmentsX, config.segmentsY, i
|
|
|
|
if segErr := renderSegmented(animation, segX, segY, frame, surface); segErr != nil {
|
|
|
|
return nil, segErr
|
|
|
|
}
|
|
|
|
|
|
|
|
font.SetColor(textColor)
|
|
|
|
|
|
|
|
var textOffsetX, textOffsetY int
|
|
|
|
|
|
|
|
switch buttonState(i) {
|
|
|
|
case buttonStatePressed, buttonStatePressedToggled:
|
|
|
|
textOffsetX = -2
|
|
|
|
textOffsetY = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
surface.PushTranslation(textX+textOffsetX, textY+textOffsetY)
|
2020-10-28 14:17:42 -04:00
|
|
|
surfaceErr := font.RenderText(text, surface)
|
2020-09-12 16:51:30 -04:00
|
|
|
surface.Pop()
|
|
|
|
|
|
|
|
if surfaceErr != nil {
|
|
|
|
return nil, surfaceErr
|
|
|
|
}
|
|
|
|
|
|
|
|
surfaces[i] = surface
|
|
|
|
}
|
|
|
|
|
|
|
|
button := &Button{width: buttonWidth, height: buttonHeight, surfaces: surfaces}
|
|
|
|
button.SetVisible(true)
|
|
|
|
|
|
|
|
return button, nil
|
2020-07-08 09:15:20 -04:00
|
|
|
}
|
2020-09-18 16:10:52 -04:00
|
|
|
|
|
|
|
func (l *Layout) loadFont(fontStyle FontStyle) (*d2asset.Font, error) {
|
|
|
|
config := getFontStyleConfig(fontStyle)
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("invalid font style")
|
|
|
|
}
|
|
|
|
|
|
|
|
return l.assetManager.LoadFont(config.fontBasePath+".tbl", config.fontBasePath+".dc6", config.palettePath)
|
|
|
|
}
|