8000 getting canvas working by rcoreilly · Pull Request #370 · cogentcore/cogent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

getting canvas working #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
176 changes: 65 additions & 111 deletions canvas/align.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package canvas

import (
"image"

"cogentcore.org/cogent/canvas/cicons"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
Expand Down Expand Up @@ -66,197 +64,153 @@
tree.AddChildAt(w, al.String(), func(w *core.Button) {
w.SetIcon(AlignIcons[al]).SetType(core.ButtonTonal).SetTooltip(al.Desc())
w.OnClick(func(e events.Event) {
av.Canvas.Align(av.Anchor, al)
av.Canvas.SVG.Align(av.Anchor, al)
})
})
}
})
}

/////////////////////////////////////////////////////////////////////////
// Actions
//////// Actions

func (vv *Canvas) Align(aa AlignAnchors, al Aligns) {
func (sv *SVG) Align(aa AlignAnchors, al Aligns) {
astr := al.String()
switch al {
case AlignRightAnchor:
vv.AlignMaxAnchor(aa, math32.X, astr)
sv.AlignMaxAnchor(aa, math32.X, astr)
case AlignLeft:
vv.AlignMin(aa, math32.X, astr)
sv.AlignMin(aa, math32.X, astr)
case AlignCenter:
vv.AlignCenter(aa, math32.X, astr)
sv.AlignCenter(aa, math32.X, astr)
case AlignRight:
vv.AlignMax(aa, math32.X, astr)
sv.AlignMax(aa, math32.X, astr)
case AlignLeftAnchor:
vv.AlignMinAnchor(aa, math32.X, astr)
sv.AlignMinAnchor(aa, math32.X, astr)
case AlignBaselineHoriz:
vv.AlignMin(aa, math32.X, astr) // todo: should be baseline, not min
sv.AlignMin(aa, math32.X, astr) // todo: should be baseline, not min

case AlignBottomAnchor:
vv.AlignMaxAnchor(aa, math32.Y, astr)
sv.AlignMaxAnchor(aa, math32.Y, astr)
case AlignTop:
vv.AlignMin(aa, math32.Y, astr)
sv.AlignMin(aa, math32.Y, astr)
case AlignMiddle:
vv.AlignCenter(aa, math32.Y, astr)
sv.AlignCenter(aa, math32.Y, astr)
case AlignBottom:
vv.AlignMax(aa, math32.Y, astr)
sv.AlignMax(aa, math32.Y, astr)
case AlignTopAnchor:
vv.AlignMinAnchor(aa, math32.Y, astr)
sv.AlignMinAnchor(aa, math32.Y, astr)
case AlignBaselineVert:
vv.AlignMin(aa, math32.Y, astr) // todo: should be baseline, not min
sv.AlignMin(aa, math32.Y, astr) // todo: should be baseline, not min
}
sv.UpdateView(true)
sv.Canvas.ChangeMade()
sv.UpdateSelSprites()
}

// AlignAnchorBBox returns the bounding box for given type of align anchor
// alignAnchorBBox returns the bounding box for given type of align anchor
// and the anchor node if non-nil
func (vv *Canvas) AlignAnchorBBox(aa AlignAnchors) (image.Rectangle, svg.Node) {
es := &vv.EditState
sv := vv.SVG()
svoff := sv.Root().BBox.Min
func (sv *SVG) alignAnchorBBox(aa AlignAnchors) (math32.Box2, svg.Node) {
es := sv.EditState()
// svoff := sv.Root().BBox.Min
var an svg.Node
var bb image.Rectangle
var bb math32.Box2
switch aa {
case AlignFirst:
sl := es.SelectedList(false)
an = sl[0]
bb = an.AsNodeBase().BBox

Check failure on line 121 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use an.AsNodeBase().BBox (variable of type image.Rectangle) as "cogentcore.org/core/math32".Box2 value in assignment
case AlignLast:
sl := es.SelectedList(true) // descending
an = sl[0]
bb = an.AsNodeBase().BBox

Check failure on line 125 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use an.AsNodeBase().BBox (variable of type image.Rectangle) as "cogentcore.org/core/math32".Box2 value in assignment
case AlignSelectBox:
bb = image.Rectangle{Min: es.DragSelectCurrentBBox.Min.ToPointFloor(), Max: es.DragSelectCurrentBBox.Max.ToPointCeil()}
bb = es.DragSelectCurrentBBox
}
bb = bb.Sub(svoff)
// bb = bb.Sub(svoff)
return bb, an
}

// AlignMin aligns to min coordinate (Left, Top) in bbox
func (vv *Canvas) AlignMin(aa AlignAnchors, dim math32.Dims, act string) {
es := &vv.EditState
// alignImpl does alignment
func (sv *SVG) alignImpl(apos math32.Vector2, an svg.Node, useMax bool, dim math32.Dims, act string) {
es := sv.EditState()
if !es.HasSelected() {
return
}
sv := vv.SVG()
svoff := sv.Root().BBox.Min
sv.UndoSave(act, es.SelectedNamesString())
abb, an := vv.AlignAnchorBBox(aa)
sc := math32.Vec2(1, 1)
odim := math32.OtherDim(dim)
for sn := range es.Selected {
if sn == an {
continue
}
sng := sn.AsNodeBase()
bb := sng.BBox.Sub(svoff)
del := math32.FromPoint(abb.Min.Sub(bb.Min))
bb := sng.BBox
var del math32.Vector2
if useMax {
del = apos.Sub(bb.Max)

Check failure on line 150 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use bb.Max (variable of type image.Point) as "cogentcore.org/core/math32".Vector2 value in argument to apos.Sub
} else {
del = apos.Sub(bb.Min)

Check failure on line 152 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use bb.Min (variable of type image.Point) as "cogentcore.org/core/math32".Vector2 value in argument to apos.Sub
}
del.SetDim(odim, 0)
sn.ApplyDeltaTransform(vv.SSVG(), del, sc, 0, math32.FromPoint(bb.Min))
sn.ApplyDeltaTransform(sv.SVG, del, sc, 0, bb.Min)

Check failure on line 155 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use bb.Min (variable of type image.Point) as "cogentcore.org/core/math32".Vector2 value in argument to sn.ApplyDeltaTransform
}
sv.UpdateView(true)
vv.ChangeMade()
}

func (vv *Canvas) AlignMinAnchor(aa AlignAnchors, dim math32.Dims, act string) {
es := &vv.EditState
if !es.HasSelected() {
// AlignMin aligns to min coordinate (Left, Top) in bbox
func (sv *SVG) AlignMin(aa AlignAnchors, dim math32.Dims, act string) {
if !sv.EditState().HasSelected() {
return
}
sv := vv.SVG()
svoff := sv.Root().BBox.Min
sv.UndoSave(act, es.SelectedNamesString())
abb, an := vv.AlignAnchorBBox(aa)
sc := math32.Vec2(1, 1)
odim := math32.OtherDim(dim)
for sn := range es.Selected {
if sn == an {
continue
}
sng := sn.AsNodeBase()
bb := sng.BBox.Sub(svoff)
del := math32.FromPoint(abb.Max.Sub(bb.Min))
del.SetDim(odim, 0)
sn.ApplyDeltaTransform(vv.SSVG(), del, sc, 0, math32.FromPoint(bb.Min))
}
sv.UpdateView(true)
vv.ChangeMade()
abb, an := sv.alignAnchorBBox(aa)
sv.alignImpl(abb.Min, an, false, dim, act)
}

func (vv *Canvas) AlignMax(aa AlignAnchors, dim math32.Dims, act string) {
es := &vv.EditState
if !es.HasSelected() {
func (sv *SVG) AlignMinAnchor(aa AlignAnchors, dim math32.Dims, act string) {
if !sv.EditState().HasSelected() {
return
}
sv := vv.SVG()
svoff := sv.Root().BBox.Min
sv.UndoSave(act, es.SelectedNamesString())
abb, an := vv.AlignAnchorBBox(aa)
sc := math32.Vec2(1, 1)
odim := math32.OtherDim(dim)
for sn := range es.Selected {
if sn == an {
continue
}
sng := sn.AsNodeBase()
bb := sng.BBox.Sub(svoff)
del := math32.FromPoint(abb.Max.Sub(bb.Max))
del.SetDim(odim, 0)
sn.ApplyDeltaTransform(vv.SSVG(), del, sc, 0, math32.FromPoint(bb.Min))
}
sv.UpdateView(true)
vv.ChangeMade()
abb, an := sv.alignAnchorBBox(aa)
sv.alignImpl(abb.Max, an, false, dim, act)
}

func (vv *Canvas) AlignMaxAnchor(aa AlignAnchors, dim math32.Dims, act string) {
es := &vv.EditState
if !es.HasSelected() {
func (sv *SVG) AlignMax(aa AlignAnchors, dim math32.Dims, act string) {
if !sv.EditState().HasSelected() {
return
}
sv := vv.SVG()
svoff := sv.Root().BBox.Min
sv.UndoSave(act, es.SelectedNamesString())
abb, an := vv.AlignAnchorBBox(aa)
sc := math32.Vec2(1, 1)
odim := math32.OtherDim(dim)
for sn := range es.Selected {
if sn == an {
continue
}
sng := sn.AsNodeBase()
bb := sng.BBox.Sub(svoff)
del := math32.FromPoint(abb.Min.Sub(bb.Max))
del.SetDim(odim, 0)
sn.ApplyDeltaTransform(vv.SSVG(), del, sc, 0, math32.FromPoint(bb.Min))
abb, an := sv.alignAnchorBBox(aa)
sv.alignImpl(abb.Max, an, true, dim, act)
}

func (sv *SVG) AlignMaxAnchor(aa AlignAnchors, dim math32.Dims, act string) {
if !sv.EditState().HasSelected() {
return
}
sv.UpdateView(true)
vv.ChangeMade()
abb, an := sv.alignAnchorBBox(aa)
sv.alignImpl(abb.Min, an, true, dim, act)
}

func (vv *Canvas) AlignCenter(aa AlignAnchors, dim math32.Dims, act string) {
es := &vv.EditState
func (sv *SVG) AlignCenter(aa AlignAnchors, dim math32.Dims, act string) {
es := sv.EditState()
if !es.HasSelected() {
return
}
sv := vv.SVG()
svoff := sv.Root().BBox.Min
sv.UndoSave(act, es.SelectedNamesString())
abb, an := vv.AlignAnchorBBox(aa)
ctr := math32.FromPoint(abb.Min.Add(abb.Max)).MulScalar(0.5)
abb, an := sv.alignAnchorBBox(aa)
ctr := abb.Min.Add(abb.Max).MulScalar(0.5)
sc := math32.Vec2(1, 1)
odim := math32.OtherDim(dim)
for sn := range es.Selected {
if sn == an {
continue
}
sng := sn.AsNodeBase()
bb := sng.BBox.Sub(svoff)
nctr := math32.FromPoint(bb.Min.Add(bb.Max)).MulScalar(0.5)
bb := sng.BBox // .Sub(svoff)
nctr := bb.Min.Add(bb.Max).MulScalar(0.5)

Check failure on line 207 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

bb.Min.Add(bb.Max).MulScalar undefined (type image.Point has no field or method MulScalar)
del := ctr.Sub(nctr)
del.SetDim(odim, 0)
sn.ApplyDeltaTransform(vv.SSVG(), del, sc, 0, math32.FromPoint(bb.Min))
sn.ApplyDeltaTransform(sv.SVG, del, sc, 0, bb.Min)

Check failure on line 210 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use bb.Min (variable of type image.Point) as "cogentcore.org/core/math32".Vector2 value in argument to sn.ApplyDeltaTransform
}
sv.UpdateView(true)
vv.ChangeMade()
sv.Canvas.ChangeMade()
}

// GatherAlignPoints gets all the potential points of alignment for objects not
Expand All @@ -282,7 +236,7 @@
return tree.Break // go no further into kids
}
for ap := BBLeft; ap < BBoxPointsN; ap++ {
es.AlignPts[ap] = append(es.AlignPts[ap], ap.PointRect(nb.BBox))
es.AlignPts[ap] = append(es.AlignPts[ap], ap.PointBox(nb.BBox))

Check failure on line 239 in canvas/align.go

View workflow job for this annotation

GitHub Actions / build

cannot use nb.BBox (variable of type image.Rectangle) as "cogentcore.org/core/math32".Box2 value in argument to ap.PointBox
}
return tree.Continue
})
Expand Down
36 changes: 0 additions & 36 deletions canvas/bbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package canvas

import (
"image"

"cogentcore.org/core/math32"
)

Expand All @@ -22,26 +20,6 @@ const (
BBBottom
)

// ValueRect returns the relevant value for a given bounding box
// as an image.Rectangle
func (ev BBoxPoints) ValueRect(bb image.Rectangle) float32 {
switch ev {
case BBLeft:
return float32(bb.Min.X)
case BBCenter:
return 0.5 * float32(bb.Min.X+bb.Max.X)
case BBRight:
return float32(bb.Max.X)
case BBTop:
return float32(bb.Min.Y)
case BBMiddle:
return 0.5 * float32(bb.Min.Y+bb.Max.Y)
case BBBottom:
return float32(bb.Max.Y)
}
return 0
}

// ValueBox returns the relevant value for a given bounding box as a
// math32.Box2
func (ev BBoxPoints) ValueBox(bb math32.Box2) float32 {
Expand Down Expand Up @@ -115,22 +93,8 @@ func ReshapeBBoxPoints(reshape Sprites) (bbX, bbY BBoxPoints) {
return
}

// PointRect returns the relevant point for a given bounding box, where
// relevant dimension is from ValRect and other is midpoint -- for drawing lines.
// BBox is an image.Rectangle
func (ev BBoxPoints) PointRect(bb image.Rectangle) math32.Vector2 {
val := ev.ValueRect(bb)
switch ev {
case BBLeft, BBCenter, BBRight:
return math32.Vec2(val, 0.5*float32(bb.Min.Y+bb.Max.Y))
default:
return math32.Vec2(0.5*float32(bb.Min.X+bb.Max.X), val)
}
}

// PointBox returns the relevant point for a given bounding box, where
// relevant dimension is from ValRect and other is midpoint -- for drawing lines.
// BBox is an image.Rectangle
func (ev BBoxPoints) PointBox(bb math32.Box2) math32.Vector2 {
val := ev.ValueBox(bb)
switch ev {
Expand Down
Loading
Loading
0