Skip to content

Commit

Permalink
feat: adds pixel drag mode to move tool (#286)
Browse files Browse the repository at this point in the history
* adds pixel drag mode to move tool

* format
  • Loading branch information
mc-oofert committed Jul 5, 2024
1 parent 0f0f158 commit 29722e7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 29 deletions.
11 changes: 11 additions & 0 deletions internal/app/ui/cpwsarea/wsmap/pmap/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package editor

import (
"sdmm/internal/app/command"
"sdmm/internal/app/prefs"
"sdmm/internal/app/ui/cpwsarea/wsmap/pmap/canvas"
"sdmm/internal/app/ui/cpwsarea/wsmap/pmap/overlay"
"sdmm/internal/dmapi/dm"
Expand Down Expand Up @@ -65,6 +66,8 @@ type app interface {

SyncPrefabs()
SyncVarEditor()

Prefs() prefs.Prefs
}

type attachedMap interface {
Expand Down Expand Up @@ -158,3 +161,11 @@ func (e *Editor) FocusCameraOnPosition(coord util.Point) {
e.pMap.SetActiveLevel(coord.Z)
e.OverlaySetTileFlick(coord)
}

func (e *Editor) ZoomLevel() float32 {
return e.pMap.Canvas().Render().Camera.Scale
}

func (e *Editor) Prefs() prefs.Prefs {
return e.app.Prefs()
}
1 change: 1 addition & 0 deletions internal/app/ui/cpwsarea/wsmap/pmap/panel_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var (
w.TextFrame("4"),
w.Separator(),
w.Text("Move a singular object"),
w.Line(w.TextFrame("Hold Shift"), w.Text("Pixel/Step offset the selected object via dragging")),
},
},
tools.TNPick: {
Expand Down
99 changes: 70 additions & 29 deletions internal/app/ui/cpwsarea/wsmap/tools/move.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package tools

import (
"sdmm/internal/app/prefs"
"sdmm/internal/dmapi/dm"
"sdmm/internal/dmapi/dmmap"
"sdmm/internal/dmapi/dmmap/dmmdata/dmmprefab"
"sdmm/internal/dmapi/dmmap/dmminstance"
"sdmm/internal/dmapi/dmvars"
"sdmm/internal/imguiext"
"sdmm/internal/util"
"strconv"

"github.com/SpaiR/imgui-go"
)

// ToolPick can be used move a single object.
// ToolMove can be used move a single object.
type ToolMove struct {
tool
instance *dmminstance.Instance
lastTile *dmmap.Tile
instance *dmminstance.Instance
lastTile *dmmap.Tile
lastMouseCoords imgui.Vec2
lastOffsets [2]int
}

func (ToolMove) Name() string {
Expand All @@ -34,43 +43,75 @@ func (t *ToolMove) onStart(util.Point) {
if hoveredInstance := ed.HoveredInstance(); hoveredInstance != nil {
ed.InstanceSelect(hoveredInstance)
t.instance = hoveredInstance
t.lastMouseCoords = imgui.MousePos()
vars := t.instance.Prefab().Vars()
if ed.Prefs().Editor.NudgeMode == prefs.SaveNudgeModePixel {
t.lastOffsets = [2]int{vars.IntV("pixel_x", 0), vars.IntV("pixel_y", 0)}
} else {
t.lastOffsets = [2]int{vars.IntV("step_x", 0), vars.IntV("step_y", 0)}
}
}
}

func (t *ToolMove) process() {
if t.instance == nil || !imguiext.IsShiftDown() {
return
}
xAxis := "pixel_x"
yAxis := "pixel_y"
if ed.Prefs().Editor.NudgeMode == prefs.SaveNudgeModeStep {
xAxis = "step_x"
yAxis = "step_y"
}
origPrefab := t.instance.Prefab()
mouseCoords := imgui.MousePos()
offsetX := (mouseCoords.X - t.lastMouseCoords.X) / ed.ZoomLevel()
offsetY := (t.lastMouseCoords.Y - mouseCoords.Y) / ed.ZoomLevel()

newVars := dmvars.Set(origPrefab.Vars(), xAxis, strconv.Itoa(t.lastOffsets[0]+int(offsetX)))
newVars = dmvars.Set(newVars, yAxis, strconv.Itoa(t.lastOffsets[1]+int(offsetY)))
t.instance.SetPrefab(dmmprefab.New(dmmprefab.IdNone, origPrefab.Path(), newVars))

ed.UpdateCanvasByCoords([]util.Point{t.instance.Coord()})
}

func (t *ToolMove) onMove(coord util.Point) {
if t.instance != nil {
prefab := t.instance.Prefab()
if t.lastTile != nil {
t.lastTile.InstancesRegenerate() //should stop some issues
}
t.lastTile = ed.Dmm().GetTile(coord)
ed.InstanceDelete(t.instance)
t.lastTile.InstancesAdd(prefab)
t.lastTile.InstancesRegenerate()
for _, found := range t.lastTile.Instances() {
if found.Prefab().Id() == prefab.Id() {
t.instance = found
break
}
if t.instance == nil || imguiext.IsShiftDown() {
return
}

prefab := t.instance.Prefab()
if t.lastTile != nil {
t.lastTile.InstancesRegenerate() //should stop some issues
}
t.lastTile = ed.Dmm().GetTile(coord)
ed.InstanceDelete(t.instance)
t.lastTile.InstancesAdd(prefab)
t.lastTile.InstancesRegenerate()
for _, found := range t.lastTile.Instances() {
if found.Prefab().Id() == prefab.Id() {
t.instance = found
break
}
ed.UpdateCanvasByCoords([]util.Point{coord})
}
ed.UpdateCanvasByCoords([]util.Point{coord})
}

func (t *ToolMove) onStop(util.Point) {
if t.instance != nil {
//remove other turfs if we moved a turf
if t.lastTile != nil {
if dm.IsPath(t.instance.Prefab().Path(), "/turf") {
for _, found := range t.lastTile.Instances() {
if dm.IsPath(found.Prefab().Path(), "/turf") && found != t.instance {
ed.InstanceDelete(found)
}
if t.instance == nil {
return
}
//remove other turfs if we moved a turf
if t.lastTile != nil {
if dm.IsPath(t.instance.Prefab().Path(), "/turf") {
for _, found := range t.lastTile.Instances() {
if dm.IsPath(found.Prefab().Path(), "/turf") && found != t.instance {
ed.InstanceDelete(found)
}
}
}
t.instance = nil
t.lastTile = nil
go ed.CommitChanges("Moved Prefab")
}
t.instance = nil
t.lastTile = nil
go ed.CommitChanges("Moved Prefab")
}
3 changes: 3 additions & 0 deletions internal/app/ui/cpwsarea/wsmap/tools/tools.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools

import (
"sdmm/internal/app/prefs"
"sdmm/internal/app/window"
"sdmm/internal/imguiext"

Expand Down Expand Up @@ -60,6 +61,8 @@ type editor interface {
TileDeleteSelected()
TileDelete(util.Point)
HoveredInstance() *dmminstance.Instance
ZoomLevel() float32
Prefs() prefs.Prefs
}

var (
Expand Down

0 comments on commit 29722e7

Please sign in to comment.