2020-11-22 16:02:42 -05:00
|
|
|
//nolint:dupl,golint,stylecheck // component declarations are supposed to look the same
|
2020-10-28 18:49:49 -04:00
|
|
|
package d2components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gravestench/akara"
|
2020-11-22 16:02:42 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2geom"
|
2020-10-28 18:49:49 -04:00
|
|
|
)
|
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
// static check that Viewport implements Component
|
|
|
|
var _ akara.Component = &Viewport{}
|
2020-10-28 18:49:49 -04:00
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
// Viewport represents the size and position of a scene viewport. This is used
|
2020-11-22 16:02:42 -05:00
|
|
|
// to control where on screen a viewport is rendered.
|
2020-11-28 00:45:58 -05:00
|
|
|
type Viewport struct {
|
2020-10-28 18:49:49 -04:00
|
|
|
*d2geom.Rectangle
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
// New creates a new Viewport. By default, the viewport size is 800x600,
|
|
|
|
// and is positioned at the top-left of the screen.
|
|
|
|
func (*Viewport) New() akara.Component {
|
|
|
|
c := &Viewport{}
|
2020-10-28 18:49:49 -04:00
|
|
|
|
2020-11-22 16:02:42 -05:00
|
|
|
const defaultWidth, defaultHeight = 800, 600
|
2020-10-28 18:49:49 -04:00
|
|
|
|
2020-11-22 16:02:42 -05:00
|
|
|
c.Rectangle = &d2geom.Rectangle{
|
|
|
|
Left: 0,
|
|
|
|
Top: 0,
|
|
|
|
Width: defaultWidth,
|
|
|
|
Height: defaultHeight,
|
2020-10-28 18:49:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-22 16:02:42 -05:00
|
|
|
return c
|
2020-10-28 18:49:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
// ViewportFactory is a wrapper for the generic component factory that returns Viewport component instances.
|
|
|
|
// This can be embedded inside of a system to give them the methods for adding, retrieving, and removing a Viewport.
|
|
|
|
type ViewportFactory struct {
|
|
|
|
Viewport *akara.ComponentFactory
|
2020-10-28 18:49:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
// AddViewport adds a Viewport component to the given entity and returns it
|
|
|
|
func (m *ViewportFactory) AddViewport(id akara.EID) *Viewport {
|
|
|
|
return m.Viewport.Add(id).(*Viewport)
|
2020-10-28 18:49:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
// GetViewport returns the Viewport component for the given entity, and a bool for whether or not it exists
|
|
|
|
func (m *ViewportFactory) GetViewport(id akara.EID) (*Viewport, bool) {
|
|
|
|
component, found := m.Viewport.Get(id)
|
|
|
|
if !found {
|
|
|
|
return nil, found
|
2020-11-22 16:02:42 -05:00
|
|
|
}
|
2020-10-28 18:49:49 -04:00
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
return component.(*Viewport), found
|
2020-10-28 18:49:49 -04:00
|
|
|
}
|