Skip to content

Commit

Permalink
Actually using packer now
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Sep 7, 2020
1 parent 81a64e6 commit 5a8fd29
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 16 deletions.
9 changes: 6 additions & 3 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ func (ui *UI) loadFont() {
}

ui.fontAtlas = ui.win.MakePicture(pic)
id, _ := ui.packer.Insert(pixel.NewSprite(pic, pic.Bounds()))
ui.fontId = id
ui.fonts.SetTextureID(imgui.TextureID(id))
id := "default-font"
if err := ui.packer.Replace(id, pixel.NewSprite(pic, pic.Bounds())); err != nil {
log.Fatalln(err)
}
ui.fontId = ui.packer.IdOf(id)
ui.fonts.SetTextureID(imgui.TextureID(ui.fontId))
}

// loadDefaultFont loads the imgui default font if the user wants it.
Expand Down
4 changes: 2 additions & 2 deletions geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func PV(v imgui.Vec2) pixel.Vec {
// ProjectVec projects the vector by the UI's matrix (vertical flip)
// and returns that as a imgui vector
func ProjectVec(v pixel.Vec) imgui.Vec2 {
return IVec(currentUI.matrix.Project(v))
return IVec(CurrentUI.matrix.Project(v))
}

// ProjectV creates a pixel vector and projects it using ProjectVec
Expand All @@ -39,7 +39,7 @@ func ProjectV(x, y float64) imgui.Vec2 {
// UnprojectV unprojects the vector by the UI's matrix (vertical flip)
// and returns that as a pixel vector
func UnprojectV(v imgui.Vec2) pixel.Vec {
return currentUI.matrix.Unproject(PV(v))
return CurrentUI.matrix.Unproject(PV(v))
}

// IZV returns an imgui zero vector
Expand Down
17 changes: 17 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pixelui

import "github.com/inkyblackness/imgui-go"

// Image is a helper for imgui.Image that looks up the sprite in the internal packed atlas.
func (ui *UI) Image(alias interface{}, scale float64) {
id := ui.packer.IdOf(alias)
sprite := ui.packer.BoundsOf(alias)
imgui.Image(imgui.TextureID(id), IVec(sprite.Size().Scaled(scale)))
}

func (ui *UI) ImageButton(alias interface{}, scale float64) bool {
id := ui.packer.IdOf(alias)
sprite := ui.packer.BoundsOf(alias)

return imgui.ImageButton(imgui.TextureID(id), IVec(sprite.Size().Scaled(scale)))
}
17 changes: 16 additions & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import (
"github.com/inkyblackness/imgui-go"
)

type Clipboard struct {
win *pixelgl.Window
}

func (c Clipboard) Text() (text string, err error) {
text = c.win.ClipboardText()
return
}

func (c Clipboard) SetText(value string) {
c.win.SetClipboardText(value)
}

// prepareIO tells imgui.io about our current io state.
func (ui *UI) prepareIO() {
ui.io.SetDisplaySize(IVec(ui.win.Bounds().Size()))
Expand All @@ -31,7 +44,7 @@ func (ui *UI) prepareIO() {

// updateKeyMod tells imgui.io where to find our key modifiers
func (ui *UI) updateKeyMod() {
ui.io.KeyCtrl(int(pixelgl.KeyLeftAlt), int(pixelgl.KeyRightAlt))
ui.io.KeyCtrl(int(pixelgl.KeyLeftControl), int(pixelgl.KeyRightControl))
ui.io.KeyShift(int(pixelgl.KeyLeftShift), int(pixelgl.KeyRightShift))
ui.io.KeyAlt(int(pixelgl.KeyLeftAlt), int(pixelgl.KeyRightAlt))
ui.io.KeySuper(int(pixelgl.KeyLeftSuper), int(pixelgl.KeyRightSuper))
Expand Down Expand Up @@ -249,4 +262,6 @@ var keys = []pixelgl.Button{
pixelgl.KeyRightAlt,
pixelgl.KeyRightSuper,
pixelgl.KeyMenu,
pixelgl.KeyLeftControl,
pixelgl.KeyRightControl,
}
35 changes: 32 additions & 3 deletions sprite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package pixelui

import (
"image"
"log"
"os"
"path/filepath"
"unsafe"

"github.com/dusk125/pixelutils/packer"
Expand Down Expand Up @@ -28,7 +32,32 @@ func Sprite(sprite *pixel.Sprite) imgui.TextureID {
}))
}

func (ui *UI) AddSprite(sprite *pixel.Sprite) imgui.TextureID {
id, _ := ui.packer.InsertV(sprite, packer.InsertFlipped)
return imgui.TextureID(id)
func (ui *UI) AddSprite(name string, sprite *pixel.Sprite) imgui.TextureID {
if err := ui.packer.InsertV(name, sprite, packer.InsertFlipped); err != nil {
log.Fatalln(err)
}
return imgui.TextureID(ui.packer.IdOf(name))
}

func (ui *UI) AddSpriteFromFile(path string) (id imgui.TextureID, sprite *pixel.Sprite) {
return ui.AddSpriteFromFileV(filepath.Base(path[:len(path)-4]), path)
}

func (ui *UI) AddSpriteFromFileV(name, path string) (id imgui.TextureID, sprite *pixel.Sprite) {
file, err := os.Open(path)
if err != nil {
log.Fatalln(err)
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
log.Fatalln(err)
}

data := pixel.PictureDataFromImage(img)
sprite = pixel.NewSprite(data, data.Bounds())
id = ui.AddSprite(name, sprite)

return
}
14 changes: 7 additions & 7 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ type UI struct {
shader *pixelgl.GLShader
matrix pixel.Matrix
shaderTris *pixelgl.GLTriangles
packer *packer.Packer
packer *packer.AliasPacker
fontId int
}

var currentUI *UI
var CurrentUI *UI

// pixelui.NewUI flags:
// NO_DEFAULT_FONT: Do not load the default font during NewUI.
Expand All @@ -79,14 +79,15 @@ func NewUI(win *pixelgl.Window, flags uint8) *UI {
win: win,
context: context,
}
currentUI = ui
CurrentUI = ui

ui.packer = packer.NewPacker(4069, 4096, 0)
ui.packer = packer.NewAliasPacker(0, 0, packer.AllowGrowth)

ui.matrix = pixel.IM.ScaledXY(win.Bounds().Center(), pixel.V(1, -1))

ui.io = imgui.CurrentIO()
ui.io.SetDisplaySize(IVec(win.Bounds().Size()))
ui.io.SetClipboard(Clipboard{win: win})

ui.fonts = ui.io.Fonts()

Expand All @@ -102,7 +103,7 @@ func NewUI(win *pixelgl.Window, flags uint8) *UI {
return ui
}

func (ui UI) GetPacker() *packer.Packer {
func (ui UI) GetPacker() *packer.AliasPacker {
return ui.packer
}

Expand Down Expand Up @@ -174,7 +175,7 @@ func (ui *UI) Draw(win *pixelgl.Window) {
clipRect = clipRect.Norm()

id := int(cmd.TextureID())
texRect := ui.packer.BoundsOf(id)
texRect := ui.packer.BoundsOf(ui.packer.AliasOf(id))

intensity := 0.0
if id != ui.fontId {
Expand Down Expand Up @@ -208,7 +209,6 @@ func (ui *UI) Draw(win *pixelgl.Window) {
win.MakePicture(ui.packer.Picture()).Draw(win.MakeTriangles(ui.shaderTris))

win.SetMatrix(pixel.IM)
// ui.packer.Draw(win, pixel.IM.Moved(ui.packer.Center()))
}

// recip returns the reciprocal of the given number.
Expand Down

0 comments on commit 5a8fd29

Please sign in to comment.