From c2c91e45a96857bf207a7d15ff2408dadff6adb8 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Mon, 4 Oct 2021 20:49:11 -0700 Subject: [PATCH] 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! --- pkg/editor_scene.go | 21 ++++++++++++++------- pkg/keybind/keybind.go | 5 +++++ pkg/play_scene.go | 14 ++++++++------ pkg/uix/canvas.go | 5 ++++- pkg/uix/canvas_scrolling.go | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/pkg/editor_scene.go b/pkg/editor_scene.go index 2c7407e..2979495 100644 --- a/pkg/editor_scene.go +++ b/pkg/editor_scene.go @@ -25,10 +25,11 @@ import ( // EditorScene manages the "Edit Level" game mode. type EditorScene struct { // Configuration for the scene initializer. - DrawingType enum.DrawingType - OpenFile bool - Filename string - DoodadSize int + DrawingType enum.DrawingType + OpenFile bool + Filename string + DoodadSize int + RememberScrollPosition render.Point // Play mode remembers it for us UI *EditorUI d *Doodle @@ -209,6 +210,11 @@ func (s *EditorScene) setupAsync(d *Doodle) error { // Recompute the UI Palette window for the level's palette. 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.") if s.DrawingType == enum.LevelDrawing { d.Flash("Press 'P' to playtest this level.") @@ -221,9 +227,10 @@ func (s *EditorScene) setupAsync(d *Doodle) error { func (s *EditorScene) Playtest() { log.Info("Play Mode, Go!") s.d.Goto(&PlayScene{ - Filename: s.filename, - Level: s.Level, - CanEdit: true, + Filename: s.filename, + Level: s.Level, + CanEdit: true, + RememberScrollPosition: s.UI.Canvas.Scroll, }) } diff --git a/pkg/keybind/keybind.go b/pkg/keybind/keybind.go index 9af66b9..3a9a612 100644 --- a/pkg/keybind/keybind.go +++ b/pkg/keybind/keybind.go @@ -244,3 +244,8 @@ func Down(ev *event.State) bool { func Use(ev *event.State) bool { return ev.Space || ev.KeyDown("q") } + +// MiddleClick of the mouse for panning the level. +func MiddleClick(ev *event.State) bool { + return ev.Button2 +} diff --git a/pkg/play_scene.go b/pkg/play_scene.go index 094aadf..c6ada7e 100644 --- a/pkg/play_scene.go +++ b/pkg/play_scene.go @@ -23,10 +23,11 @@ import ( // PlayScene manages the "Edit Level" game mode. type PlayScene struct { // Configuration attributes. - Filename string - Level *level.Level - CanEdit bool // i.e. you came from the Editor Mode - HasNext bool // has a next level to load next + Filename string + Level *level.Level + CanEdit bool // i.e. you came from the Editor Mode + HasNext bool // has a next level to load next + RememberScrollPosition render.Point // for the Editor quality of life // Private variables. d *Doodle @@ -294,8 +295,9 @@ func (s *PlayScene) setupPlayer() { func (s *PlayScene) EditLevel() { log.Info("Edit Mode, Go!") s.d.Goto(&EditorScene{ - Filename: s.Filename, - Level: s.Level, + Filename: s.Filename, + Level: s.Level, + RememberScrollPosition: s.RememberScrollPosition, }) } diff --git a/pkg/uix/canvas.go b/pkg/uix/canvas.go index 5736539..193cc25 100644 --- a/pkg/uix/canvas.go +++ b/pkg/uix/canvas.go @@ -95,7 +95,10 @@ type Canvas struct { lastPixel *level.Pixel // 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. diff --git a/pkg/uix/canvas_scrolling.go b/pkg/uix/canvas_scrolling.go index 5c627fd..4499c8d 100644 --- a/pkg/uix/canvas_scrolling.go +++ b/pkg/uix/canvas_scrolling.go @@ -54,6 +54,24 @@ func (w *Canvas) loopEditorScroll(ev *event.State) error { 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 }