Skip to content

Commit

Permalink
Adding custom font addition
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Jun 23, 2020
1 parent 48aa362 commit 3fcdd79
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
40 changes: 40 additions & 0 deletions font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pixelui

import (
"image/color"
"log"
"unsafe"

"github.com/faiface/pixel"
)

// loadFont parses the imgui font data and creates a pixel picture from it.
func (ui *UI) loadFont() {
f := ui.fonts.TextureDataAlpha8()
pic := pixel.MakePictureData(pixel.R(0, 0, float64(f.Width), float64(f.Height)))

for y := 0; y < f.Height; y++ {
for x := 0; x < f.Width; x++ {
i := y*f.Width + x
ptr := (*uint8)(unsafe.Pointer(uintptr(f.Pixels) + uintptr(i)))
pic.Pix[i] = color.RGBA{R: 0, G: 0, B: 0, A: *ptr}
}
}

ui.fontAtlas = ui.win.MakePicture(pic)
}

// loadDefaultFont loads the imgui default font if the user wants it.
func (ui *UI) loadDefaultFont() {
ui.fonts.AddFontDefault()
ui.loadFont()
}

// AddTTFFont loads the given font into imgui.
func (ui *UI) AddTTFFont(path string, size float32) {
ui.fonts.AddFontFromFileTTF(path, size)
if err := ui.fonts.BuildWithFreeType(); err != nil {
log.Fatalln(err)
}
ui.loadFont()
}
43 changes: 19 additions & 24 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ void main() {

// UI Stores the state of the pixelui UI
type UI struct {
win *pixelgl.Window
context *imgui.Context
io imgui.IO
fonts imgui.FontAtlas
timer time.Time
pic *pixel.PictureData
picture pixel.TargetPicture
shader *pixelgl.GLShader
matrix pixel.Matrix
win *pixelgl.Window
context *imgui.Context
io imgui.IO
fonts imgui.FontAtlas
timer time.Time
fontAtlas pixel.TargetPicture
shader *pixelgl.GLShader
matrix pixel.Matrix
}

// pixelui.NewUI flags:
// NO_DEFAULT_FONT: Do not load the default font during NewUI.
const (
NO_DEFAULT_FONT = 0x0001
)

// NewUI Creates the UI and setups up its internal structures
func NewUI(win *pixelgl.Window) *UI {
func NewUI(win *pixelgl.Window, flags int) *UI {
var context *imgui.Context
mainthread.Call(func() {
context = imgui.CreateContext(nil)
Expand All @@ -64,22 +69,12 @@ func NewUI(win *pixelgl.Window) *UI {
ui.io.SetDisplaySize(pixelVecToimguiVec(win.Bounds().Size()))

ui.fonts = ui.io.Fonts()
ui.fonts.AddFontDefault()
f := ui.fonts.TextureDataAlpha8()

ui.pic = pixel.MakePictureData(pixel.R(0, 0, float64(f.Width), float64(f.Height)))

for y := 0; y < f.Height; y++ {
for x := 0; x < f.Width; x++ {
i := y*f.Width + x
ptr := (*uint8)(unsafe.Pointer(uintptr(f.Pixels) + uintptr(i)))
ui.pic.Pix[i] = color.RGBA{R: 0, G: 0, B: 0, A: *ptr}
}
}

ui.shader = pixelgl.NewGLShader(uiShader)

ui.picture = win.Canvas().MakePicture(ui.pic)
if flags&NO_DEFAULT_FONT == 0 {
ui.loadDefaultFont()
}
ui.setKeyMapping()

return ui
Expand Down Expand Up @@ -158,7 +153,7 @@ func (ui *UI) Draw(win *pixelgl.Window) {
clipRect.Max = ui.matrix.Project(clipRect.Max)
shaderTris := pixelgl.NewGLTriangles(ui.shader, tris)
shaderTris.SetClipRect(clipRect)
ui.picture.Draw(win.Canvas().MakeTriangles(shaderTris))
ui.fontAtlas.Draw(win.Canvas().MakeTriangles(shaderTris))
}
}
}
Expand Down

0 comments on commit 3fcdd79

Please sign in to comment.