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"
|
2019-06-27 01:36:54 +00:00
|
|
|
"runtime"
|
2018-10-08 20:06:42 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/collision"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/cursor"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/doodads"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/drawtool"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/filesystem"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/level"
|
2023-02-19 01:37:54 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/levelpack"
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/wallpaper"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
"git.kirsle.net/go/render/event"
|
|
|
|
"git.kirsle.net/go/ui"
|
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
|
|
|
|
2022-01-09 21:16:29 +00:00
|
|
|
// Parent Canvas widget, e.g. for Actors inside of a Level so they can
|
|
|
|
// find the parent canvas and see where they are drawing in relation to
|
|
|
|
// it (to handle top/left edge cropping on scroll)
|
|
|
|
parent *Canvas
|
|
|
|
|
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.
|
2020-11-20 04:08:38 +00:00
|
|
|
Zoom int // Zoom level on the canvas.
|
2018-10-20 22:42:49 +00:00
|
|
|
|
2023-02-18 05:09:11 +00:00
|
|
|
// Set this if your Canvas is a small fixed size (e.g. in doodad dropper),
|
|
|
|
// so that doodads will crop their texture (if chunk size larger than your
|
|
|
|
// Canvas) as to not overflow the canvas bounds. Not needed for Level canvases.
|
|
|
|
CroppedSize bool
|
|
|
|
|
Doodad/Actor Runtime Options
* Add "Options" support for Doodads: these allow for individual Actor instances
on your level to customize properties about the doodad. They're like "Tags"
except the player can customize them on a per-actor basis.
* Doodad Editor: you can specify the Options in the Doodad Properties window.
* Level Editor: when the Actor Tool is selected, on mouse-over of an actor,
clicking on the gear icon will open a new "Actor Properties" window which
shows metadata (title, author, ID, position) and an Options tab to configure
the actor's options.
Updates to the scripting API:
* Self.Options() returns a list of option names defined on the Doodad.
* Self.GetOption(name) returns the value for the named option, or nil if
neither the actor nor its doodad have the option defined. The return type
will be correctly a string, boolean or integer type.
Updates to the doodad command-line tool:
* `doodad show` will print the Options on a .doodad file and, when showing a
.level file with --actors, prints any customized Options with the actors.
* `doodad edit-doodad` adds a --option parameter to define options.
Options added to the game's built-in doodads:
* Warp Doors: "locked (exit only)" will make it so the door can not be opened
by the player, giving the "locked" message (as if it had no linked door),
but the player may still exit from the door if sent by another warp door.
* Electric Door & Electric Trapdoor: "opened" can make the door be opened by
default when the level begins instead of closed. A switch or a button that
removes power will close the door as normal.
* Colored Doors & Small Key Door: "unlocked" will make the door unlocked at
level start, not requiring a key to open it.
* Colored Keys & Small Key: "has gravity" will make the key subject to gravity
and set its Mobile flag so that if it falls onto a button, it will activate.
* Gemstones: they had gravity by default; you can now uncheck "has gravity" to
remove their Gravity and IsMobile status.
* Gemstone Totems: "has gemstone" will set the totem to its unlocked status by
default with the gemstone inserted. No power signal will be emitted; it is
cosmetic only.
* Fire Region: "name" can let you set a name for the fire region similarly to
names for fire pixels: "Watch out for ${name}!"
* Invisible Warp Door: "locked (exit only)" added as well.
2022-10-10 00:41:24 +00:00
|
|
|
// Toogle for doodad canvases in the Level Editor to show their buttons.
|
|
|
|
ShowDoodadButtons bool
|
|
|
|
doodadButtonFrame ui.Widget // lazy init
|
|
|
|
doodadButtonFrameHovering bool
|
|
|
|
OnDoodadConfig func(*Actor)
|
|
|
|
|
2021-01-04 01:06:33 +00:00
|
|
|
// Custom label to place in the lower-right corner of the canvas.
|
|
|
|
// Used for e.g. the quantity badge on Inventory items.
|
|
|
|
CornerLabel string
|
|
|
|
|
2018-10-21 00:08:20 +00:00
|
|
|
// Selected draw tool/mode, default Pencil, for editable canvases.
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
Tool drawtool.Tool
|
|
|
|
BrushSize int // thickness of selected brush
|
2018-10-21 00:08:20 +00:00
|
|
|
|
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.
|
Update savegame format, Allow out-of-bounds camera
Updates the savegame.json file format:
* Levels now have a UUID value assigned at first save.
* The savegame.json will now track level completion/score based on UUID,
making it robust to filename changes in either levels or levelpacks.
* The savegame file is auto-migrated on startup - for any levels not
found or have no UUID, no change is made, it's backwards compatible.
* Level Properties window adds an "Advanced" tab to show/re-roll UUID.
New JavaScript API for doodad scripts:
* `Actors.CameraFollowPlayer()` tells the camera to return focus to the
player character. Useful for "cutscene" doodads that freeze the player,
call `Self.CameraFollowMe()` and do a thing before unfreezing and sending the
camera back to the player. (Or it will follow them at their next directional
input control).
* `Self.MoveBy(Point(x, y int))` to move the current actor a bit.
New option for the `doodad` command-line tool:
* `doodad resave <.level or .doodad>` will load and re-save a drawing, to
migrate it to the newest file format versions.
Small tweaks:
* On bounded levels, allow the camera to still follow the player if the player
finds themselves WELL far out of bounds (40 pixels margin). So on bounded
levels you can create "interior rooms" out-of-bounds to Warp Door into.
* New wallpaper: "Atmosphere" has a black starscape pattern that fades into a
solid blue atmosphere.
* Camera strictly follows the player the first 20 ticks, not 60 of level start
* If player is frozen, directional inputs do not take the camera focus back.
2023-03-08 05:55:10 +00:00
|
|
|
NoLimitScroll bool
|
|
|
|
scrollOutOfBounds bool // player is far out of bounds = temporary NoLimitScroll.
|
2018-10-28 05:22:13 +00:00
|
|
|
|
2022-05-05 05:38:26 +00:00
|
|
|
// Show custom mouse cursors over this canvas (eg. editor tools)
|
|
|
|
FancyCursors bool
|
|
|
|
cursor *cursor.Cursor
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Underlying chunk data for the drawing.
|
2020-11-16 02:02:35 +00:00
|
|
|
level *level.Level
|
|
|
|
chunks *level.Chunker
|
2021-10-11 23:10:04 +00:00
|
|
|
doodad *doodads.Doodad
|
2020-11-16 02:02:35 +00:00
|
|
|
modified bool // set to True when the drawing has been modified, like in Editor Mode.
|
2018-10-19 20:31:58 +00:00
|
|
|
|
2023-02-19 01:37:54 +00:00
|
|
|
// PlayScene can set whether the Levelpack has a valid signature on it, so that
|
|
|
|
// in InstallActors() we can allow a levelpack to load attached doodads on free
|
|
|
|
// mode for their levels.
|
|
|
|
IsSignedLevelPack *levelpack.LevelPack // filled in ONLY IF SIGNED.
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// 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-05-07 05:57:32 +00:00
|
|
|
// Collision memory for the actors.
|
2024-07-08 05:45:21 +00:00
|
|
|
collidingActors ActorCollisionMap // mapping their IDs to each other
|
2019-05-07 05:57:32 +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.
|
2022-04-09 21:41:24 +00:00
|
|
|
OnDeleteActors func([]*Actor)
|
2019-07-05 23:04:36 +00:00
|
|
|
OnDragStart func(*level.Actor)
|
2018-10-21 00:08:20 +00:00
|
|
|
|
2019-06-23 23:15:09 +00:00
|
|
|
// -- WHEN Canvas.Tool is "Link" --
|
2019-06-09 00:02:28 +00:00
|
|
|
// When the Canvas wants to link two actors together. Arguments are the IDs
|
|
|
|
// of the two actors.
|
2019-06-28 05:54:46 +00:00
|
|
|
OnLinkActors func(a, b *Actor)
|
2019-06-23 23:15:09 +00:00
|
|
|
linkFirst *Actor
|
2019-06-09 00:02:28 +00:00
|
|
|
|
Add Switches, Fire/Water Collision and Play Menu
* New doodads: Switches.
* They come in four varieties: wall switch (background element, with
"ON/OFF" text) and three side-profile switches for the floor, left
or right walls.
* On collision with the player, they flip their state from "OFF" to
"ON" or vice versa. If the player walks away and then collides
again, the switch flips again.
* Can be used to open/close Electric Doors when turned on/off. Their
default state is "off"
* If a switch receives a power signal from another linked switch, it
sets its own state to match. So, two "on/off" switches that are
connected to a door AND to each other will both flip on/off when one
of them flips.
* Update the Level Collision logic to support Decoration, Fire and Water
pixel collisions.
* Previously, ALL pixels in the level were acting as though solid.
* Non-solid pixels don't count for collision detection, but their
attributes (fire and water) are collected and returned.
* Updated the MenuScene to support loading a map file in Play Mode
instead of Edit Mode. Updated the title screen menu to add a button
for playing levels instead of editing them.
* Wrote some documentation.
2019-07-07 01:30:03 +00:00
|
|
|
// Collision handlers for level geometry.
|
|
|
|
OnLevelCollision func(*Actor, *collision.Collide)
|
|
|
|
|
2022-01-19 05:24:36 +00:00
|
|
|
// Handler when a doodad script called Actors.SetPlayerCharacter.
|
|
|
|
// The filename.doodad is given.
|
|
|
|
OnSetPlayerCharacter func(filename string)
|
|
|
|
|
2022-03-27 18:51:14 +00:00
|
|
|
// Handler for when a doodad script calls Level.ResetTimer().
|
|
|
|
OnResetTimer func()
|
|
|
|
|
2019-07-03 23:22:30 +00:00
|
|
|
/********
|
|
|
|
* Editable canvas private variables.
|
|
|
|
********/
|
|
|
|
// The current stroke actively being drawn by the user, during a
|
|
|
|
// mousedown-and-dragging event.
|
|
|
|
currentStroke *drawtool.Stroke
|
|
|
|
strokes map[int]*drawtool.Stroke // active stroke mapped by ID
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
lastPixel *level.Pixel
|
2018-08-17 03:37:19 +00:00
|
|
|
|
|
|
|
// We inherit the ui.Widget which manages the width and height.
|
2021-10-07 03:02:09 +00:00
|
|
|
Scroll render.Point // Scroll offset for which parts of canvas are visible.
|
|
|
|
scrollDragging bool // Middle-click to pan scroll
|
|
|
|
scrollStartAt render.Point // Cursor point at beginning of pan
|
|
|
|
scrollWasAt render.Point // copy of Scroll at beginning of pan
|
|
|
|
scrollLastDelta render.Point // multitouch spam
|
2022-04-10 19:39:27 +00:00
|
|
|
|
|
|
|
// LoadUnloadChunks metrics for the debug overlay.
|
|
|
|
loadUnloadInside int
|
|
|
|
loadUnloadOutside int
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCanvas initializes a Canvas widget.
|
2018-09-26 17:04:46 +00:00
|
|
|
//
|
2023-02-17 05:47:18 +00:00
|
|
|
// size is the Chunker size (uint8)
|
|
|
|
//
|
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.
|
2023-02-17 05:47:18 +00:00
|
|
|
func NewCanvas(size uint8, 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(),
|
2021-03-31 06:33:25 +00:00
|
|
|
BrushSize: 1,
|
2023-02-17 05:47:18 +00:00
|
|
|
chunks: level.NewChunker(uint8(size)),
|
2018-10-19 20:31:58 +00:00
|
|
|
actors: make([]*Actor, 0),
|
2018-10-28 05:22:13 +00:00
|
|
|
wallpaper: &Wallpaper{},
|
2019-07-03 23:22:30 +00:00
|
|
|
|
|
|
|
strokes: map[int]*drawtool.Stroke{},
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-09 21:41:24 +00:00
|
|
|
/*
|
|
|
|
Destroy the canvas.
|
|
|
|
|
|
|
|
This function satisfies the ui.Widget interface but it also calls Teardown() methods
|
|
|
|
on the level or doodad as well as any level actors, which frees up SDL2 texture memory.
|
|
|
|
|
|
|
|
Note: the rest of the data can be garbage collected by Go normally, the textures are
|
|
|
|
able to regenerate themselves again if needed.
|
|
|
|
*/
|
|
|
|
func (w *Canvas) Destroy() {
|
|
|
|
if w.level != nil {
|
|
|
|
w.level.Teardown()
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.doodad != nil {
|
|
|
|
w.doodad.Teardown()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, actor := range w.actors {
|
|
|
|
actor.Canvas.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.wallpaper.WP != nil {
|
|
|
|
if freed := w.wallpaper.WP.Free(); freed > 0 {
|
|
|
|
log.Debug("%s.Destroy(): freed %d wallpaper textures", w, freed)
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 19:39:27 +00:00
|
|
|
|
|
|
|
if w.scripting != nil {
|
|
|
|
w.scripting.Teardown()
|
|
|
|
}
|
2022-04-09 21:41:24 +00:00
|
|
|
}
|
2022-01-02 02:48:34 +00:00
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// 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
|
2020-11-16 02:02:35 +00:00
|
|
|
w.modified = false
|
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.
|
2021-07-20 00:14:00 +00:00
|
|
|
func (w *Canvas) LoadLevel(level *level.Level) {
|
2019-07-03 23:22:30 +00:00
|
|
|
w.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
|
2021-06-13 21:53:21 +00:00
|
|
|
filename := balance.EmbeddedWallpaperBasePath + level.Wallpaper
|
2019-06-27 01:36:54 +00:00
|
|
|
if runtime.GOOS != "js" {
|
2019-06-28 05:54:46 +00:00
|
|
|
// Check if the wallpaper wasn't found. Check bindata and file system.
|
2021-06-07 01:59:04 +00:00
|
|
|
if _, err := filesystem.FindFileEmbedded(filename, level); err != nil {
|
|
|
|
log.Error("LoadLevel: wallpaper %s did not appear to exist, default to notebook.png", filename)
|
2021-06-13 21:53:21 +00:00
|
|
|
filename = balance.EmbeddedWallpaperBasePath + "notebook.png"
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
2018-10-28 05:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 00:14:00 +00:00
|
|
|
wp, err := wallpaper.FromFile(filename, level)
|
2018-10-28 05:22:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("wallpaper FromFile(%s): %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.wallpaper.maxWidth = level.MaxWidth
|
|
|
|
w.wallpaper.maxHeight = level.MaxHeight
|
2021-07-20 00:14:00 +00:00
|
|
|
err = w.wallpaper.Load(level.PageType, wp)
|
2018-10-28 05:22:13 +00:00
|
|
|
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
|
2021-10-11 23:10:04 +00:00
|
|
|
w.doodad = d
|
2018-09-26 17:04:46 +00:00
|
|
|
w.Load(d.Palette, d.Layers[0].Chunker)
|
|
|
|
}
|
|
|
|
|
2020-11-17 07:20:24 +00:00
|
|
|
// LoadDoodadToLayer initializes a Canvas from a Doodad object and picks
|
|
|
|
// a layer to load.
|
|
|
|
func (w *Canvas) LoadDoodadToLayer(d *doodads.Doodad, index int) {
|
|
|
|
if index < 0 || index > len(d.Layers) {
|
|
|
|
log.Error("LoadDoodadToLayer: index %d out of range", index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Load(d.Palette, d.Layers[index].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.
|
2019-12-22 22:11:01 +00:00
|
|
|
func (w *Canvas) Loop(ev *event.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
|
|
|
|
2022-04-10 19:39:27 +00:00
|
|
|
// Every so often, eager-load/unload chunk bitmaps to save on memory.
|
2022-04-30 03:34:59 +00:00
|
|
|
if w.level != nil {
|
|
|
|
// Unloads bitmaps and textures every N frames...
|
|
|
|
w.LoadUnloadChunks()
|
|
|
|
|
|
|
|
// Unloads chunks themselves (from zipfile levels) that aren't
|
|
|
|
// recently accessed.
|
|
|
|
w.chunks.FreeCaches()
|
|
|
|
}
|
2022-04-10 19:39:27 +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 {
|
2022-04-10 19:39:27 +00:00
|
|
|
a.Canvas.Destroy()
|
2019-04-19 01:15:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
newActors = append(newActors, a)
|
|
|
|
}
|
|
|
|
if len(newActors) < len(w.actors) {
|
|
|
|
w.actors = newActors
|
|
|
|
}
|
|
|
|
|
2019-04-16 02:12:25 +00:00
|
|
|
// Check collisions between actors.
|
2019-07-05 22:02:22 +00:00
|
|
|
if w.scripting != nil {
|
|
|
|
if err := w.loopActorCollision(); err != nil {
|
|
|
|
log.Error("loopActorCollision: %s", err)
|
|
|
|
}
|
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 {
|
2019-12-28 03:16:34 +00:00
|
|
|
cursor := render.NewPoint(ev.CursorX, ev.CursorY)
|
2018-10-21 00:08:20 +00:00
|
|
|
if cursor.Inside(ui.AbsoluteRect(w)) {
|
|
|
|
return w.loopEditable(ev)
|
2018-08-17 03:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-10 19:39:27 +00:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 19:39:27 +00:00
|
|
|
// LoadingViewport is the viewport of chunks that ought to be preloaded and
|
|
|
|
// ready to display soon. It is the Viewport of chunks on screen + a margin
|
|
|
|
// of neighboring chunks outside the screen.
|
|
|
|
//
|
|
|
|
// For memory optimization, chunks falling inside this viewport have their
|
|
|
|
// Go image.Image rendered and cached ready to convert to an SDL2 Texture
|
|
|
|
// when they come on screen. Chunks outside of the LoadingViewport can be
|
|
|
|
// unloaded (textures and images freed) to keep memory consumption on large
|
|
|
|
// levels under control.
|
|
|
|
func (w *Canvas) LoadingViewport() render.Rect {
|
|
|
|
var (
|
2023-02-17 05:47:18 +00:00
|
|
|
chunkSize uint8
|
2022-04-10 19:39:27 +00:00
|
|
|
vp = w.Viewport()
|
|
|
|
margin = balance.LoadingViewportMarginChunks
|
|
|
|
)
|
|
|
|
|
|
|
|
// This function is meant for levels only, but..
|
|
|
|
if w.level != nil {
|
|
|
|
chunkSize = w.level.Chunker.Size
|
|
|
|
} else if w.doodad != nil {
|
2023-02-17 05:47:18 +00:00
|
|
|
chunkSize = w.doodad.ChunkSize8()
|
2022-04-10 19:39:27 +00:00
|
|
|
} else {
|
|
|
|
chunkSize = balance.ChunkSize
|
|
|
|
log.Error("Canvas.LoadingViewport: no drawing to get chunk size from, default to %d", chunkSize)
|
|
|
|
}
|
|
|
|
|
2023-02-17 05:47:18 +00:00
|
|
|
var size = int(chunkSize)
|
2022-04-10 19:39:27 +00:00
|
|
|
return render.Rect{
|
2023-02-17 05:47:18 +00:00
|
|
|
X: vp.X - size*margin.X,
|
|
|
|
Y: vp.Y - size*margin.Y,
|
|
|
|
W: vp.W + size*margin.X,
|
|
|
|
H: vp.H + size*margin.Y,
|
2022-04-10 19:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2020-11-20 04:08:38 +00:00
|
|
|
world := render.Point{
|
2018-10-20 22:42:49 +00:00
|
|
|
X: screenPixel.X - P.X - w.Scroll.X,
|
|
|
|
Y: screenPixel.Y - P.Y - w.Scroll.Y,
|
|
|
|
}
|
2020-11-20 04:08:38 +00:00
|
|
|
|
|
|
|
// Handle Zoomies
|
|
|
|
if w.Zoom != 0 {
|
2021-07-13 04:20:45 +00:00
|
|
|
// Zoom Out - logic is 100% correct, do not touch.
|
|
|
|
// ZoomDivide's logic at time of writing is to:
|
|
|
|
// return int(float64(v) * divider)
|
|
|
|
// Where divider is a map of w.Zoom to:
|
|
|
|
// -2=4 -1=2 0=1 1=0.5 2=0.25 3=0.125
|
|
|
|
// The -2 and -1 do the right things (zoom out), zoom
|
|
|
|
// in was jank. NOW FIXED with the following maps:
|
|
|
|
// -2=4 -1=2 0=1 1=0.675 2=0.5 3=0.404
|
|
|
|
// Values for zoom levels 1 and 3 are jank but works?
|
|
|
|
world.X = w.ZoomDivide(world.X)
|
|
|
|
world.Y = w.ZoomDivide(world.Y)
|
2020-11-20 04:08:38 +00:00
|
|
|
}
|
2021-07-13 04:20:45 +00:00
|
|
|
|
2020-11-20 04:08:38 +00:00
|
|
|
return world
|
2018-10-20 22:42:49 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
}
|