Middle-click to Pan + Remember Scroll Position

In the editor, clicking and dragging with the middle mouse button
will scroll the view of the editor in place of the arrow keys.

When entering Play Mode, the original scroll position in the level
editor is remembered for when you come back - no more having to
scroll from 0,0 each time to get back to where you were working!
This commit is contained in:
Noah 2021-10-04 20:49:11 -07:00
parent 489a43ea8c
commit c2c91e45a9
5 changed files with 49 additions and 14 deletions

View File

@ -29,6 +29,7 @@ type EditorScene struct {
OpenFile bool OpenFile bool
Filename string Filename string
DoodadSize int DoodadSize int
RememberScrollPosition render.Point // Play mode remembers it for us
UI *EditorUI UI *EditorUI
d *Doodle d *Doodle
@ -209,6 +210,11 @@ func (s *EditorScene) setupAsync(d *Doodle) error {
// Recompute the UI Palette window for the level's palette. // Recompute the UI Palette window for the level's palette.
s.UI.FinishSetup(d) s.UI.FinishSetup(d)
// Scroll the level to the remembered position from when we went
// to Play Mode and back. If no remembered position, this is zero
// anyway.
s.UI.Canvas.ScrollTo(s.RememberScrollPosition)
d.Flash("Editor Mode.") d.Flash("Editor Mode.")
if s.DrawingType == enum.LevelDrawing { if s.DrawingType == enum.LevelDrawing {
d.Flash("Press 'P' to playtest this level.") d.Flash("Press 'P' to playtest this level.")
@ -224,6 +230,7 @@ func (s *EditorScene) Playtest() {
Filename: s.filename, Filename: s.filename,
Level: s.Level, Level: s.Level,
CanEdit: true, CanEdit: true,
RememberScrollPosition: s.UI.Canvas.Scroll,
}) })
} }

View File

@ -244,3 +244,8 @@ func Down(ev *event.State) bool {
func Use(ev *event.State) bool { func Use(ev *event.State) bool {
return ev.Space || ev.KeyDown("q") return ev.Space || ev.KeyDown("q")
} }
// MiddleClick of the mouse for panning the level.
func MiddleClick(ev *event.State) bool {
return ev.Button2
}

View File

@ -27,6 +27,7 @@ type PlayScene struct {
Level *level.Level Level *level.Level
CanEdit bool // i.e. you came from the Editor Mode CanEdit bool // i.e. you came from the Editor Mode
HasNext bool // has a next level to load next HasNext bool // has a next level to load next
RememberScrollPosition render.Point // for the Editor quality of life
// Private variables. // Private variables.
d *Doodle d *Doodle
@ -296,6 +297,7 @@ func (s *PlayScene) EditLevel() {
s.d.Goto(&EditorScene{ s.d.Goto(&EditorScene{
Filename: s.Filename, Filename: s.Filename,
Level: s.Level, Level: s.Level,
RememberScrollPosition: s.RememberScrollPosition,
}) })
} }

View File

@ -96,6 +96,9 @@ type Canvas struct {
// We inherit the ui.Widget which manages the width and height. // We inherit the ui.Widget which manages the width and height.
Scroll render.Point // Scroll offset for which parts of canvas are visible. 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
} }
// NewCanvas initializes a Canvas widget. // NewCanvas initializes a Canvas widget.

View File

@ -54,6 +54,24 @@ func (w *Canvas) loopEditorScroll(ev *event.State) error {
w.ScrollBy(scrollBy) w.ScrollBy(scrollBy)
} }
// Middle click of the mouse to pan the level.
// NOTE: returns are below!
if keybind.MiddleClick(ev) {
if !w.scrollDragging {
w.scrollDragging = true
w.scrollStartAt = shmem.Cursor
w.scrollWasAt = w.Scroll
} else {
delta := shmem.Cursor.Compare(w.scrollStartAt)
w.Scroll = w.scrollWasAt
w.Scroll.Subtract(delta)
}
} else {
if w.scrollDragging {
w.scrollDragging = false
}
}
return nil return nil
} }