2018-09-26 17:04:46 +00:00
|
|
|
package uix
|
2018-08-17 03:37:19 +00:00
|
|
|
|
|
|
|
import (
|
2018-10-08 20:06:42 +00:00
|
|
|
"fmt"
|
2018-10-28 05:22:13 +00:00
|
|
|
"os"
|
2018-10-08 20:06:42 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/events"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/ui"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
2019-04-16 02:12:25 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/collision"
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/doodads"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/level"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2019-04-16 06:07:15 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/scripting"
|
2018-10-28 05:22:13 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/wallpaper"
|
2018-08-17 03:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Canvas is a custom ui.Widget that manages a single drawing.
|
|
|
|
type Canvas struct {
|
|
|
|
ui.Frame
|
2018-09-26 17:04:46 +00:00
|
|
|
Palette *level.Palette
|
2018-08-17 03:37:19 +00:00
|
|
|
|
2018-10-20 22:42:49 +00:00
|
|
|
// Editable and Scrollable go hand in hand and, if you initialize a
|
|
|
|
// NewCanvas() with editable=true, they are both enabled.
|
|
|
|
Editable bool // Clicking will edit pixels of this canvas.
|
|
|
|
Scrollable bool // Cursor keys will scroll the viewport of this canvas.
|
|
|
|
|
2018-10-21 00:08:20 +00:00
|
|
|
// Selected draw tool/mode, default Pencil, for editable canvases.
|
|
|
|
Tool Tool
|
|
|
|
|
2018-10-20 22:42:49 +00:00
|
|
|
// MaskColor will force every pixel to render as this color regardless of
|
|
|
|
// the palette index of that pixel. Otherwise pixels behave the same and
|
|
|
|
// the palette does work as normal. Set to render.Invisible (zero value)
|
|
|
|
// to remove the mask.
|
|
|
|
MaskColor render.Color
|
2018-08-17 03:37:19 +00:00
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
// Actor ID to follow the camera on automatically, i.e. the main player.
|
|
|
|
FollowActor string
|
|
|
|
|
2018-10-28 05:22:13 +00:00
|
|
|
// Debug tools
|
|
|
|
// NoLimitScroll suppresses the scroll limit for bounded levels.
|
|
|
|
NoLimitScroll bool
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Underlying chunk data for the drawing.
|
|
|
|
chunks *level.Chunker
|
|
|
|
|
|
|
|
// Actors to superimpose on top of the drawing.
|
2019-04-14 22:25:03 +00:00
|
|
|
actor *Actor // if this canvas IS an actor
|
|
|
|
actors []*Actor // if this canvas CONTAINS actors (i.e., is a level)
|
2018-10-19 20:31:58 +00:00
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
// Doodad scripting engine supervisor.
|
|
|
|
// NOTE: initialized and managed by the play_scene.
|
|
|
|
scripting *scripting.Supervisor
|
|
|
|
|
2018-10-28 05:22:13 +00:00
|
|
|
// Wallpaper settings.
|
|
|
|
wallpaper *Wallpaper
|
|
|
|
|
2018-10-21 00:08:20 +00:00
|
|
|
// When the Canvas wants to delete Actors, but ultimately it is upstream
|
|
|
|
// that controls the actors. Upstream should delete them and then reinstall
|
|
|
|
// the actor list from scratch.
|
|
|
|
OnDeleteActors func([]*level.Actor)
|
|
|
|
OnDragStart func(filename string)
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Tracking pixels while editing. TODO: get rid of pixelHistory?
|
2018-09-26 17:04:46 +00:00
|
|
|
pixelHistory []*level.Pixel
|
|
|
|
lastPixel *level.Pixel
|
2018-08-17 03:37:19 +00:00
|
|
|
|
|
|
|
// We inherit the ui.Widget which manages the width and height.
|
|
|
|
Scroll render.Point // Scroll offset for which parts of canvas are visible.
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCanvas initializes a Canvas widget.
|
2018-09-26 17:04:46 +00:00
|
|
|
//
|
|
|
|
// If editable is true, Scrollable is also set to true, which means the arrow
|
|
|
|
// keys will scroll the canvas viewport which is desirable in Edit Mode.
|
2018-09-23 22:20:45 +00:00
|
|
|
func NewCanvas(size int, editable bool) *Canvas {
|
2018-08-17 03:37:19 +00:00
|
|
|
w := &Canvas{
|
2018-09-26 17:04:46 +00:00
|
|
|
Editable: editable,
|
|
|
|
Scrollable: editable,
|
|
|
|
Palette: level.NewPalette(),
|
|
|
|
chunks: level.NewChunker(size),
|
2018-10-19 20:31:58 +00:00
|
|
|
actors: make([]*Actor, 0),
|
2018-10-28 05:22:13 +00:00
|
|
|
wallpaper: &Wallpaper{},
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
w.setup()
|
2018-10-08 17:38:49 +00:00
|
|
|
w.IDFunc(func() string {
|
2018-10-08 20:06:42 +00:00
|
|
|
var attrs []string
|
|
|
|
|
|
|
|
if w.Editable {
|
|
|
|
attrs = append(attrs, "editable")
|
|
|
|
} else {
|
|
|
|
attrs = append(attrs, "read-only")
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Scrollable {
|
|
|
|
attrs = append(attrs, "scrollable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("Canvas<%d; %s>", size, strings.Join(attrs, "; "))
|
2018-10-08 17:38:49 +00:00
|
|
|
})
|
2018-08-17 03:37:19 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load initializes the Canvas using an existing Palette and Grid.
|
2018-09-26 17:04:46 +00:00
|
|
|
func (w *Canvas) Load(p *level.Palette, g *level.Chunker) {
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Palette = p
|
2018-09-23 22:20:45 +00:00
|
|
|
w.chunks = g
|
2018-08-17 03:37:19 +00:00
|
|
|
|
|
|
|
if len(w.Palette.Swatches) > 0 {
|
|
|
|
w.SetSwatch(w.Palette.Swatches[0])
|
|
|
|
}
|
2018-09-25 16:40:34 +00:00
|
|
|
}
|
2018-08-17 03:37:19 +00:00
|
|
|
|
2018-09-25 16:40:34 +00:00
|
|
|
// LoadLevel initializes a Canvas from a Level object.
|
2018-10-28 05:22:13 +00:00
|
|
|
func (w *Canvas) LoadLevel(e render.Engine, level *level.Level) {
|
2018-09-25 16:40:34 +00:00
|
|
|
w.Load(level.Palette, level.Chunker)
|
2018-10-28 05:22:13 +00:00
|
|
|
|
|
|
|
// TODO: wallpaper paths
|
|
|
|
filename := "assets/wallpapers/" + level.Wallpaper
|
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
|
|
log.Error("LoadLevel: %s", err)
|
|
|
|
filename = "assets/wallpapers/notebook.png" // XXX TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
wp, err := wallpaper.FromFile(e, filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("wallpaper FromFile(%s): %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.wallpaper.maxWidth = level.MaxWidth
|
|
|
|
w.wallpaper.maxHeight = level.MaxHeight
|
|
|
|
err = w.wallpaper.Load(e, level.PageType, wp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("wallpaper Load: %s", err)
|
|
|
|
}
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// LoadDoodad initializes a Canvas from a Doodad object.
|
|
|
|
func (w *Canvas) LoadDoodad(d *doodads.Doodad) {
|
|
|
|
// TODO more safe
|
|
|
|
w.Load(d.Palette, d.Layers[0].Chunker)
|
|
|
|
}
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// SetSwatch changes the currently selected swatch for editing.
|
2018-09-26 17:04:46 +00:00
|
|
|
func (w *Canvas) SetSwatch(s *level.Swatch) {
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Palette.ActiveSwatch = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup common configs between both initializers of the canvas.
|
|
|
|
func (w *Canvas) setup() {
|
2018-10-19 20:31:58 +00:00
|
|
|
// XXX: Debug code.
|
|
|
|
if balance.DebugCanvasBorder != render.Invisible {
|
|
|
|
w.Configure(ui.Config{
|
|
|
|
BorderColor: balance.DebugCanvasBorder,
|
|
|
|
BorderSize: 2,
|
|
|
|
BorderStyle: ui.BorderSolid,
|
|
|
|
})
|
|
|
|
}
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop is called on the scene's event loop to handle mouse interaction with
|
|
|
|
// the canvas, i.e. to edit it.
|
|
|
|
func (w *Canvas) Loop(ev *events.State) error {
|
2019-04-10 02:17:56 +00:00
|
|
|
// Process the arrow keys scrolling the level in Edit Mode.
|
|
|
|
// canvas_scrolling.go
|
|
|
|
w.loopEditorScroll(ev)
|
|
|
|
if err := w.loopFollowActor(ev); err != nil {
|
|
|
|
log.Error("Follow actor: %s", err) // not fatal but nice to know
|
|
|
|
}
|
2019-04-19 23:21:04 +00:00
|
|
|
_ = w.loopConstrainScroll()
|
2019-04-10 02:17:56 +00:00
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// Remove any actors that were destroyed the previous tick.
|
|
|
|
var newActors []*Actor
|
|
|
|
for _, a := range w.actors {
|
|
|
|
if a.flagDestroy {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newActors = append(newActors, a)
|
|
|
|
}
|
|
|
|
if len(newActors) < len(w.actors) {
|
|
|
|
w.actors = newActors
|
|
|
|
}
|
|
|
|
|
2019-04-16 02:12:25 +00:00
|
|
|
// Move any actors. As we iterate over all actors, track their bounding
|
|
|
|
// rectangles so we can later see if any pair of actors intersect each other.
|
|
|
|
boxes := make([]render.Rect, len(w.actors))
|
|
|
|
for i, a := range w.actors {
|
|
|
|
// Get the actor's velocity to see if it's moving this tick.
|
|
|
|
v := a.Velocity()
|
|
|
|
|
|
|
|
// If not moving, grab the bounding box right now.
|
|
|
|
if v == render.Origin {
|
|
|
|
boxes[i] = doodads.GetBoundingRect(a)
|
|
|
|
continue
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
2019-04-16 02:12:25 +00:00
|
|
|
|
|
|
|
// Create a delta point from their current location to where they
|
|
|
|
// want to move to this tick.
|
|
|
|
delta := a.Position()
|
|
|
|
delta.Add(v)
|
|
|
|
|
|
|
|
// Check collision with level geometry.
|
|
|
|
info, ok := collision.CollidesWithGrid(a, w.chunks, delta)
|
|
|
|
if ok {
|
|
|
|
// Collision happened with world.
|
|
|
|
}
|
|
|
|
delta = info.MoveTo // Move us back where the collision check put us
|
|
|
|
|
|
|
|
// Move the actor's World Position to the new location.
|
|
|
|
a.MoveTo(delta)
|
|
|
|
|
|
|
|
// Keep the actor from leaving the world borders of bounded maps.
|
|
|
|
w.loopContainActorsInsideLevel(a)
|
|
|
|
|
|
|
|
// Store this actor's bounding box after they've moved.
|
|
|
|
boxes[i] = doodads.GetBoundingRect(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check collisions between actors.
|
|
|
|
for tuple := range collision.BetweenBoxes(boxes) {
|
2019-04-19 01:15:05 +00:00
|
|
|
log.Debug("Actor %s collides with %s",
|
2019-04-16 02:12:25 +00:00
|
|
|
w.actors[tuple[0]].ID(),
|
|
|
|
w.actors[tuple[1]].ID(),
|
|
|
|
)
|
2019-04-16 06:07:15 +00:00
|
|
|
a, b := w.actors[tuple[0]], w.actors[tuple[1]]
|
|
|
|
|
|
|
|
// Call the OnCollide handler.
|
|
|
|
if w.scripting != nil {
|
2019-04-19 01:15:05 +00:00
|
|
|
if err := w.scripting.To(a.ID()).Events.RunCollide(); err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
|
|
|
if err := w.scripting.To(b.ID()).Events.RunCollide(); err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 00:08:20 +00:00
|
|
|
// If the canvas is editable, only care if it's over our space.
|
|
|
|
if w.Editable {
|
|
|
|
cursor := render.NewPoint(ev.CursorX.Now, ev.CursorY.Now)
|
|
|
|
if cursor.Inside(ui.AbsoluteRect(w)) {
|
|
|
|
return w.loopEditable(ev)
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Viewport returns a rect containing the viewable drawing coordinates in this
|
|
|
|
// canvas. The X,Y values are the scroll offset (top left) and the W,H values
|
|
|
|
// are the scroll offset plus the width/height of the Canvas widget.
|
2018-10-19 20:31:58 +00:00
|
|
|
//
|
|
|
|
// The Viewport rect are the Absolute World Coordinates of the drawing that are
|
|
|
|
// visible inside the Canvas. The X,Y is the top left World Coordinate and the
|
|
|
|
// W,H are the bottom right World Coordinate, making this rect an absolute
|
|
|
|
// slice of the world. For a normal rect with a relative width and height,
|
|
|
|
// use ViewportRelative().
|
|
|
|
//
|
|
|
|
// The rect X,Y are the negative Scroll Value.
|
|
|
|
// The rect W,H are the Canvas widget size minus the Scroll Value.
|
2018-08-17 03:37:19 +00:00
|
|
|
func (w *Canvas) Viewport() render.Rect {
|
|
|
|
var S = w.Size()
|
|
|
|
return render.Rect{
|
2018-10-18 06:01:21 +00:00
|
|
|
X: -w.Scroll.X,
|
|
|
|
Y: -w.Scroll.Y,
|
|
|
|
W: S.W - w.Scroll.X,
|
|
|
|
H: S.H - w.Scroll.Y,
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// ViewportRelative returns a relative viewport where the Width and Height
|
|
|
|
// values are zero-relative: so you can use it with point.Inside(viewport)
|
|
|
|
// to see if a World Index point should be visible on screen.
|
|
|
|
//
|
|
|
|
// The rect X,Y are the negative Scroll Value
|
|
|
|
// The rect W,H are the Canvas widget size.
|
|
|
|
func (w *Canvas) ViewportRelative() render.Rect {
|
|
|
|
var S = w.Size()
|
|
|
|
return render.Rect{
|
|
|
|
X: -w.Scroll.X,
|
|
|
|
Y: -w.Scroll.Y,
|
|
|
|
W: S.W,
|
|
|
|
H: S.H,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:42:49 +00:00
|
|
|
// WorldIndexAt returns the World Index that corresponds to a Screen Pixel
|
|
|
|
// on the screen. If the screen pixel is the mouse coordinate (relative to
|
|
|
|
// the application window) this will return the World Index of the pixel below
|
|
|
|
// the mouse cursor.
|
|
|
|
func (w *Canvas) WorldIndexAt(screenPixel render.Point) render.Point {
|
|
|
|
var P = ui.AbsolutePosition(w)
|
|
|
|
return render.Point{
|
|
|
|
X: screenPixel.X - P.X - w.Scroll.X,
|
|
|
|
Y: screenPixel.Y - P.Y - w.Scroll.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Chunker returns the underlying Chunker object.
|
2018-09-26 17:04:46 +00:00
|
|
|
func (w *Canvas) Chunker() *level.Chunker {
|
2018-09-23 22:20:45 +00:00
|
|
|
return w.chunks
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// ScrollTo sets the viewport scroll position.
|
|
|
|
func (w *Canvas) ScrollTo(to render.Point) {
|
|
|
|
w.Scroll.X = to.X
|
|
|
|
w.Scroll.Y = to.Y
|
|
|
|
}
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// ScrollBy adjusts the viewport scroll position.
|
|
|
|
func (w *Canvas) ScrollBy(by render.Point) {
|
|
|
|
w.Scroll.Add(by)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the canvas.
|
|
|
|
func (w *Canvas) Compute(e render.Engine) {
|
|
|
|
|
|
|
|
}
|