From 581d47ed31ef64c875869fc53663da8a3b2c674f Mon Sep 17 00:00:00 2001 From: Kai O'Reilly Date: Tue, 27 Feb 2024 16:47:29 -0800 Subject: [PATCH] added back in more sprites logic to vector; sprites are now rendering, albeit quite poorly --- code/code/settings.go | 4 +- vector/path.go | 77 +++++++++-------- vector/prefs.go | 1 + vector/select.go | 190 +++++++++++++++++++++--------------------- vector/sprites.go | 29 +++---- 5 files changed, 151 insertions(+), 150 deletions(-) diff --git a/code/code/settings.go b/code/code/settings.go index 2179a698..ba0dec2f 100644 --- a/code/code/settings.go +++ b/code/code/settings.go @@ -21,7 +21,7 @@ import ( ) func init() { - gi.TheApp.SetName("CogentCode") + gi.TheApp.SetName("Cogent Code") gi.AllSettings = slices.Insert(gi.AllSettings, 1, gi.Settings(Settings)) DefaultKeyMap = "MacEmacs" // todo SetActiveKeyMapName(DefaultKeyMap) @@ -33,7 +33,7 @@ func init() { var Settings = &SettingsData{ SettingsBase: gi.SettingsBase{ Name: "Code", - File: filepath.Join(gi.TheApp.DataDir(), "CogentCode", "settings.toml"), + File: filepath.Join(gi.TheApp.DataDir(), "Cogent Code", "settings.toml"), }, } diff --git a/vector/path.go b/vector/path.go index ace255fb..ef35aab2 100644 --- a/vector/path.go +++ b/vector/path.go @@ -6,6 +6,7 @@ package vector import ( "fmt" + "image" "cogentcore.org/core/events" "cogentcore.org/core/gi" @@ -166,53 +167,51 @@ func (sv *SVGView) PathNodes(path *svg.Path) ([]*PathNode, []int) { } func (sv *SVGView) UpdateNodeSprites() { - /* - es := sv.EditState() - prvn := es.NNodeSprites + es := sv.EditState() + prvn := es.NNodeSprites - path := es.FirstSelectedPath() + path := es.FirstSelectedPath() - if path == nil { - sv.RemoveNodeSprites(win) - win.UpdateSig() - return - } + if path == nil { + sv.RemoveNodeSprites() + // win.UpdateSig() + return + } - es.PathNodes, es.PathCmds = sv.PathNodes(path) - es.NNodeSprites = len(es.PathNodes) - es.ActivePath = path - - for i, pn := range es.PathNodes { - idx := i // key to get local var - sp := SpriteConnectEvent(win, SpNodePoint, SpUnk, i, image.ZP, sv.This(), func(recv, send ki.Ki, sig int64, d any) { - ssvg := recv.Embed(KiT_SVGView).(*SVGView) - ssvg.NodeSpriteEvent(idx, events.EventType(sig), d) - }) - SetSpritePos(sp, image.Point{int(pn.WinPt.X), int(pn.WinPt.Y)}) - } + es.PathNodes, es.PathCmds = sv.PathNodes(path) + es.NNodeSprites = len(es.PathNodes) + es.ActivePath = path + + for i, pn := range es.PathNodes { + // sp := SpriteConnectEvent(win, SpNodePoint, SpUnk, i, image.ZP, sv.This(), func(recv, send ki.Ki, sig int64, d any) { + // ssvg := recv.Embed(KiT_SVGView).(*SVGView) + // ssvg.NodeSpriteEvent(idx, events.EventType(sig), d) + // }) + sp := Sprite(sv, SpNodePoint, SpUnk, i, image.Point{}) + SetSpritePos(sp, image.Point{int(pn.WinPt.X), int(pn.WinPt.Y)}) + } - // remove extra - for i := es.NNodeSprites; i < prvn; i++ { - spnm := SpriteName(SpNodePoint, SpUnk, i) - win.InactivateSprite(spnm) - } + // remove extra + sprites := &sv.Scene.Stage.Sprites + for i := es.NNodeSprites; i < prvn; i++ { + spnm := SpriteName(SpNodePoint, SpUnk, i) + sprites.InactivateSprite(spnm) + } - sv.VectorView.UpdateNodeToolbar() - */ + sv.VectorView.UpdateNodeToolbar() } func (sv *SVGView) RemoveNodeSprites() { - /* - es := sv.EditState() - for i := 0; i < es.NNodeSprites; i++ { - spnm := SpriteName(SpNodePoint, SpUnk, i) - win.InactivateSprite(spnm) - } - es.NNodeSprites = 0 - es.PathNodes = nil - es.PathCmds = nil - es.ActivePath = nil - */ + es := sv.EditState() + sprites := &sv.Scene.Stage.Sprites + for i := 0; i < es.NNodeSprites; i++ { + spnm := SpriteName(SpNodePoint, SpUnk, i) + sprites.InactivateSprite(spnm) + } + es.NNodeSprites = 0 + es.PathNodes = nil + es.PathCmds = nil + es.ActivePath = nil } /* diff --git a/vector/prefs.go b/vector/prefs.go index 9b29342b..a8d4fbc2 100644 --- a/vector/prefs.go +++ b/vector/prefs.go @@ -112,6 +112,7 @@ var Prefs = Preferences{} // InitPrefs must be called at startup in mainrun() func InitPrefs() { + gi.TheApp.SetName("Cogent Vector") Prefs.Defaults() Prefs.Open() // OpenPaths() // todo diff --git a/vector/select.go b/vector/select.go index fc8fe4f6..1fd4c7c1 100644 --- a/vector/select.go +++ b/vector/select.go @@ -136,10 +136,10 @@ func (sv *SVGView) UpdateSelect() { } func (sv *SVGView) RemoveSelSprites() { - // InactivateSprites(win, SpReshapeBBox) - // InactivateSprites(win, SpSelBBox) - // es := sv.EditState() - // es.NSelSprites = 0 + InactivateSprites(sv, SpReshapeBBox) + InactivateSprites(sv, SpSelBBox) + es := sv.EditState() + es.NSelSprites = 0 // win.UpdateSig() } @@ -148,84 +148,85 @@ func (sv *SVGView) UpdateSelSprites() { // updt := win.UpdateStart() // defer win.UpdateEnd(updt) - // es := sv.EditState() - // es.UpdateSelBBox() - // if !es.HasSelected() { - // sv.RemoveSelSprites(win) - // return - // } + updt := sv.UpdateStart() + defer sv.UpdateEnd(updt) - // for i := SpBBoxUpL; i <= SpBBoxRtM; i++ { - // spi := i // key to get a unique local var - // SpriteConnectEvent(win, SpReshapeBBox, spi, 0, image.ZP, sv.This(), func(recv, send ki.Ki, sig int64, d any) { - // ssvg := recv.Embed(KiT_SVGView).(*SVGView) - // ssvg.SelSpriteEvent(spi, events.EventType(sig), d) - // }) - // } - // sv.SetBBoxSpritePos(SpReshapeBBox, 0, es.SelBBox) - // sv.SetSelSpritePos() + es := sv.EditState() + es.UpdateSelBBox() + if !es.HasSelected() { + sv.RemoveSelSprites() + return + } + + for i := SpBBoxUpL; i <= SpBBoxRtM; i++ { + // SpriteConnectEvent(win, SpReshapeBBox, spi, 0, image.ZP, sv.This(), func(recv, send ki.Ki, sig int64, d any) { + // ssvg := recv.Embed(KiT_SVGView).(*SVGView) + // ssvg.SelSpriteEvent(spi, events.EventType(sig), d) + // }) + Sprite(sv, SpReshapeBBox, i, 0, image.Point{}) + } + sv.SetBBoxSpritePos(SpReshapeBBox, 0, es.SelBBox) + sv.SetSelSpritePos() // win.UpdateSig() } func (sv *SVGView) SetSelSpritePos() { - // win := sv.VectorView.ParentWindow() - // es := sv.EditState() - // nsel := es.NSelSprites - - // es.NSelSprites = 0 - // if len(es.Selected) > 1 { - // nbox := 0 - // sl := es.SelectedList(false) - // for si, sii := range sl { - // sn := sii.AsNodeBase() - // if sn.BBox.Size() == image.ZP { - // continue - // } - // bb := mat32.Box2{} - // bb.SetFromRect(sn.BBox) - // sv.SetBBoxSpritePos(SpSelBBox, si, bb) - // nbox++ - // } - // es.NSelSprites = nbox - // } + es := sv.EditState() + nsel := es.NSelSprites + + es.NSelSprites = 0 + if len(es.Selected) > 1 { + nbox := 0 + sl := es.SelectedList(false) + for si, sii := range sl { + sn := sii.AsNodeBase() + if sn.BBox.Size() == image.ZP { + continue + } + bb := mat32.Box2{} + bb.SetFromRect(sn.BBox) + sv.SetBBoxSpritePos(SpSelBBox, si, bb) + nbox++ + } + es.NSelSprites = nbox + } - // for si := es.NSelSprites; si < nsel; si++ { - // for i := SpBBoxUpL; i <= SpBBoxRtM; i++ { - // spnm := SpriteName(SpSelBBox, i, si) - // win.InactivateSprite(spnm) - // } - // } + sprites := &sv.Scene.Stage.Sprites + for si := es.NSelSprites; si < nsel; si++ { + for i := SpBBoxUpL; i <= SpBBoxRtM; i++ { + spnm := SpriteName(SpSelBBox, i, si) + sprites.InactivateSprite(spnm) + } + } } // SetBBoxSpritePos sets positions of given type of sprites func (sv *SVGView) SetBBoxSpritePos(typ Sprites, idx int, bbox mat32.Box2) { - // win := sv.VectorView.ParentWindow() - // _, spsz := HandleSpriteSize(1) - // midX := int(0.5 * (bbox.Min.X + bbox.Max.X - float32(spsz.X))) - // midY := int(0.5 * (bbox.Min.Y + bbox.Max.Y - float32(spsz.Y))) - // for i := SpBBoxUpL; i <= SpBBoxRtM; i++ { - // spi := i // key to get a unique local var - // sp := Sprite(win, typ, spi, idx, image.ZP) - // switch spi { - // case SpBBoxUpL: - // SetSpritePos(sp, image.Point{int(bbox.Min.X), int(bbox.Min.Y)}) - // case SpBBoxUpC: - // SetSpritePos(sp, image.Point{midX, int(bbox.Min.Y)}) - // case SpBBoxUpR: - // SetSpritePos(sp, image.Point{int(bbox.Max.X), int(bbox.Min.Y)}) - // case SpBBoxDnL: - // SetSpritePos(sp, image.Point{int(bbox.Min.X), int(bbox.Max.Y)}) - // case SpBBoxDnC: - // SetSpritePos(sp, image.Point{midX, int(bbox.Max.Y)}) - // case SpBBoxDnR: - // SetSpritePos(sp, image.Point{int(bbox.Max.X), int(bbox.Max.Y)}) - // case SpBBoxLfM: - // SetSpritePos(sp, image.Point{int(bbox.Min.X), midY}) - // case SpBBoxRtM: - // SetSpritePos(sp, image.Point{int(bbox.Max.X), midY}) - // } - // } + _, spsz := HandleSpriteSize(1) + midX := int(0.5 * (bbox.Min.X + bbox.Max.X - float32(spsz.X))) + midY := int(0.5 * (bbox.Min.Y + bbox.Max.Y - float32(spsz.Y))) + for i := SpBBoxUpL; i <= SpBBoxRtM; i++ { + sp := Sprite(sv, typ, i, idx, image.ZP) + switch i { + case SpBBoxUpL: + SetSpritePos(sp, image.Point{int(bbox.Min.X), int(bbox.Min.Y)}) + case SpBBoxUpC: + SetSpritePos(sp, image.Point{midX, int(bbox.Min.Y)}) + case SpBBoxUpR: + SetSpritePos(sp, image.Point{int(bbox.Max.X), int(bbox.Min.Y)}) + case SpBBoxDnL: + SetSpritePos(sp, image.Point{int(bbox.Min.X), int(bbox.Max.Y)}) + case SpBBoxDnC: + SetSpritePos(sp, image.Point{midX, int(bbox.Max.Y)}) + case SpBBoxDnR: + SetSpritePos(sp, image.Point{int(bbox.Max.X), int(bbox.Max.Y)}) + case SpBBoxLfM: + SetSpritePos(sp, image.Point{int(bbox.Min.X), midY}) + case SpBBoxRtM: + SetSpritePos(sp, image.Point{int(bbox.Max.X), midY}) + } + } } /* @@ -260,33 +261,32 @@ func (sv *SVGView) SelSpriteEvent(sp Sprites, et events.EventType, d any) { // SetRubberBand updates the rubber band postion func (sv *SVGView) SetRubberBand(cur image.Point) { - // win := sv.VectorView.ParentWindow() - // es := sv.EditState() + es := sv.EditState() - // if !es.InAction() { - // es.ActStart("BoxSelect", fmt.Sprintf("%v", es.DragStartPos)) - // es.ActUnlock() - // } - // es.DragCurPos = cur + if !es.InAction() { + es.ActStart("BoxSelect", fmt.Sprintf("%v", es.DragStartPos)) + es.ActUnlock() + } + es.DragCurPos = cur - // bbox := image.Rectangle{Min: es.DragStartPos, Max: es.DragCurPos} - // bbox = bbox.Canon() + bbox := image.Rectangle{Min: es.DragStartPos, Max: es.DragCurPos} + bbox = bbox.Canon() - // sz := bbox.Size() - // if sz.X < 4 { - // sz.X = 4 - // } - // if sz.Y < 4 { - // sz.Y = 4 - // } - // rt := Sprite(win, SpRubberBand, SpBBoxUpC, 0, sz) - // rb := Sprite(win, SpRubberBand, SpBBoxDnC, 0, sz) - // rr := Sprite(win, SpRubberBand, SpBBoxRtM, 0, sz) - // rl := Sprite(win, SpRubberBand, SpBBoxLfM, 0, sz) - // SetSpritePos(rt, bbox.Min) - // SetSpritePos(rb, image.Point{bbox.Min.X, bbox.Max.Y}) - // SetSpritePos(rr, image.Point{bbox.Max.X, bbox.Min.Y}) - // SetSpritePos(rl, bbox.Min) + sz := bbox.Size() + if sz.X < 4 { + sz.X = 4 + } + if sz.Y < 4 { + sz.Y = 4 + } + rt := Sprite(sv, SpRubberBand, SpBBoxUpC, 0, sz) + rb := Sprite(sv, SpRubberBand, SpBBoxDnC, 0, sz) + rr := Sprite(sv, SpRubberBand, SpBBoxRtM, 0, sz) + rl := Sprite(sv, SpRubberBand, SpBBoxLfM, 0, sz) + SetSpritePos(rt, bbox.Min) + SetSpritePos(rb, image.Point{bbox.Min.X, bbox.Max.Y}) + SetSpritePos(rr, image.Point{bbox.Max.X, bbox.Min.Y}) + SetSpritePos(rl, bbox.Min) // win.UpdateSig() } diff --git a/vector/sprites.go b/vector/sprites.go index 529c394a..fcd300d8 100644 --- a/vector/sprites.go +++ b/vector/sprites.go @@ -127,13 +127,13 @@ func SpriteProps(sp *gi.Sprite) (typ, subtyp Sprites, idx int) { // making it if not yet made. trgsz is the target size (e.g., for rubber // band boxes) func Sprite(ctx gi.Widget, typ, subtyp Sprites, idx int, trgsz image.Point) *gi.Sprite { - sps := &ctx.AsWidget().Scene.Stage.Sprites + sprites := &ctx.AsWidget().Scene.Stage.Sprites spnm := SpriteName(typ, subtyp, idx) - sp, ok := sps.SpriteByName(spnm) + sp, ok := sprites.SpriteByName(spnm) if !ok { - sp = gi.NewSprite(spnm, image.ZP, image.ZP) + sp = gi.NewSprite(spnm, image.Point{}, image.Point{}) SetSpriteProps(sp, typ, subtyp, idx) - sps.Add(sp) + sprites.Add(sp) } switch typ { case SpReshapeBBox: @@ -159,8 +159,8 @@ func Sprite(ctx gi.Widget, typ, subtyp Sprites, idx int, trgsz image.Point) *gi. DrawAlignMatchVert(sp, trgsz) } } - sps.ActivateSprite(sp.Name) - return nil + sprites.ActivateSprite(sp.Name) + return sp } /* @@ -221,14 +221,15 @@ func SetSpritePos(sp *gi.Sprite, pos image.Point) { } // InactivateSprites inactivates sprites of given type -func InactivateSprites(typ Sprites) { - // for _, spkv := range win.Sprites.Names.Order { - // sp := spkv.Val - // st, _, _ := SpriteProps(sp) - // if st == typ { - // win.InactivateSprite(sp.Name) - // } - // } +func InactivateSprites(ctx gi.Widget, typ Sprites) { + sprites := &ctx.AsWidget().Scene.Stage.Sprites + for _, spkv := range sprites.Names.Order { + sp := spkv.Value + st, _, _ := SpriteProps(sp) + if st == typ { + sprites.InactivateSprite(sp.Name) + } + } } ///////////////////////////////////////////////////////////////////