gabstv / ebiten-imgui

Dear ImGui renderer for Ebitengine
MIT License
122 stars 18 forks source link

Need some example #15

Closed aprchen closed 2 years ago

aprchen commented 2 years ago

imgui.Text("ภาษาไทย测试조선말") // To display these, you'll need to register a compatible font

Can you provide an example? thx

gabstv commented 2 years ago

Hello,

You can merge fonts for different unicode code points like the example below:

package main

import (
    "fmt"
    "image/color"

    "github.com/gabstv/ebiten-imgui/renderer"
    "github.com/hajimehoshi/ebiten/v2"
    "github.com/hajimehoshi/ebiten/v2/ebitenutil"
    "github.com/inkyblackness/imgui-go/v4"
)

func main() {
    mgr := renderer.New(nil)

    // specify which glyphs to merge this font into
    glyphs := imgui.CurrentIO().Fonts().GlyphRangesThai()
    glyphs2 := imgui.CurrentIO().Fonts().GlyphRangesChineseSimplifiedCommon()
    glyphs3 := imgui.CurrentIO().Fonts().GlyphRangesKorean()
    cfg0 := imgui.NewFontConfig()
    cfg0.SetMergeMode(true)
    // merge example.ttf (I used arial_unicode_ms for my tests):
    imgui.CurrentIO().Fonts().AddFontFromFileTTFV("example.ttf", 16, cfg0, glyphs) // Thai
    imgui.CurrentIO().Fonts().AddFontFromFileTTFV("example.ttf", 16, cfg0, glyphs2) // Chinese simplified
    imgui.CurrentIO().Fonts().AddFontFromFileTTFV("example.ttf", 16, cfg0, glyphs3) // Korean
    imgui.CurrentIO().Fonts().Build()

    ebiten.SetWindowSize(800, 600)

    gg := &G{
        mgr: mgr,
    }

    ebiten.RunGame(gg)
}

type G struct {
    mgr *renderer.Manager
    // demo members:
    clearColor [3]float32
    floatVal   float32
    counter    int
    name       string
    demoWindow bool
}

func (g *G) Draw(screen *ebiten.Image) {
    screen.Fill(color.RGBA{uint8(g.clearColor[0] * 255), uint8(g.clearColor[1] * 255), uint8(g.clearColor[2] * 255), 255})
    ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %.2f", ebiten.CurrentTPS()))
    g.mgr.Draw(screen)
}

func (g *G) Update() error {
    g.mgr.Update(1.0 / 60.0)
    g.mgr.BeginFrame()
    {
        imgui.Text("ภาษาไทย测试조선말")                        // To display these, you'll need to register a compatible font
        imgui.Text("Hello, world!")                       // Display some text
        imgui.SliderFloat("float", &g.floatVal, 0.0, 1.0) // Edit 1 float using a slider from 0.0f to 1.0f
        imgui.ColorEdit3("clear color", &g.clearColor)    // Edit 3 floats representing a color

        //imgui.Checkbox("Demo Window", &showDemoWindow) // Edit bools storing our window open/close state
        //imgui.Checkbox("Go Demo Window", &showGoDemoWindow)
        //imgui.Checkbox("Another Window", &showAnotherWindow)

        if imgui.Button("Button") { // Buttons return true when clicked (most widgets return true when edited/activated)
            g.counter++
        }
        imgui.SameLine()
        imgui.Text(fmt.Sprintf("counter = %d", g.counter))

        imgui.InputText("Name", &g.name)
        if imgui.Button("open demo window") {
            g.demoWindow = true
        }
        if g.demoWindow {
            imgui.ShowDemoWindow(&g.demoWindow)
        }
    }
    g.mgr.EndFrame()
    return nil
}

func (g *G) Layout(outsideWidth, outsideHeight int) (int, int) {
    g.mgr.SetDisplaySize(float32(800), float32(600))
    return 800, 600
}

Result:

Screen Shot 2022-05-09 at 13 23 35

You can also add new fonts without merging with:

myfont := imgui.CurrentIO().Fonts().AddFontFromFileTTF("myfont.ttf", 16)
// ...
imgui.PushFont(myfont)
imgui.Text("myfont.ttf example")
Imgui.PopFont()
gabstv commented 2 years ago

Also, for the font settings, the majority of the original imgui instructions still apply: https://github.com/ocornut/imgui/blob/master/docs/FONTS.md

aprchen commented 2 years ago

Thanks a lot, it worked @gabstv

Hello,

You can merge fonts for different unicode code points like the example below:

package main

import (
  "fmt"
  "image/color"

  "github.com/gabstv/ebiten-imgui/renderer"
  "github.com/hajimehoshi/ebiten/v2"
  "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  "github.com/inkyblackness/imgui-go/v4"
)

func main() {
  mgr := renderer.New(nil)

  // specify which glyphs to merge this font into
  glyphs := imgui.CurrentIO().Fonts().GlyphRangesThai()
  glyphs2 := imgui.CurrentIO().Fonts().GlyphRangesChineseSimplifiedCommon()
  glyphs3 := imgui.CurrentIO().Fonts().GlyphRangesKorean()
  cfg0 := imgui.NewFontConfig()
  cfg0.SetMergeMode(true)
  // merge example.ttf (I used arial_unicode_ms for my tests):
  imgui.CurrentIO().Fonts().AddFontFromFileTTFV("example.ttf", 16, cfg0, glyphs) // Thai
  imgui.CurrentIO().Fonts().AddFontFromFileTTFV("example.ttf", 16, cfg0, glyphs2) // Chinese simplified
  imgui.CurrentIO().Fonts().AddFontFromFileTTFV("example.ttf", 16, cfg0, glyphs3) // Korean
  imgui.CurrentIO().Fonts().Build()

  ebiten.SetWindowSize(800, 600)

  gg := &G{
      mgr: mgr,
  }

  ebiten.RunGame(gg)
}

type G struct {
  mgr *renderer.Manager
  // demo members:
  clearColor [3]float32
  floatVal   float32
  counter    int
  name       string
  demoWindow bool
}

func (g *G) Draw(screen *ebiten.Image) {
  screen.Fill(color.RGBA{uint8(g.clearColor[0] * 255), uint8(g.clearColor[1] * 255), uint8(g.clearColor[2] * 255), 255})
  ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %.2f", ebiten.CurrentTPS()))
  g.mgr.Draw(screen)
}

func (g *G) Update() error {
  g.mgr.Update(1.0 / 60.0)
  g.mgr.BeginFrame()
  {
      imgui.Text("ภาษาไทย测试조선말")                        // To display these, you'll need to register a compatible font
      imgui.Text("Hello, world!")                       // Display some text
      imgui.SliderFloat("float", &g.floatVal, 0.0, 1.0) // Edit 1 float using a slider from 0.0f to 1.0f
      imgui.ColorEdit3("clear color", &g.clearColor)    // Edit 3 floats representing a color

      //imgui.Checkbox("Demo Window", &showDemoWindow) // Edit bools storing our window open/close state
      //imgui.Checkbox("Go Demo Window", &showGoDemoWindow)
      //imgui.Checkbox("Another Window", &showAnotherWindow)

      if imgui.Button("Button") { // Buttons return true when clicked (most widgets return true when edited/activated)
          g.counter++
      }
      imgui.SameLine()
      imgui.Text(fmt.Sprintf("counter = %d", g.counter))

      imgui.InputText("Name", &g.name)
      if imgui.Button("open demo window") {
          g.demoWindow = true
      }
      if g.demoWindow {
          imgui.ShowDemoWindow(&g.demoWindow)
      }
  }
  g.mgr.EndFrame()
  return nil
}

func (g *G) Layout(outsideWidth, outsideHeight int) (int, int) {
  g.mgr.SetDisplaySize(float32(800), float32(600))
  return 800, 600
}

Result: Screen Shot 2022-05-09 at 13 23 35

You can also add new fonts without merging with:

myfont := imgui.CurrentIO().Fonts().AddFontFromFileTTF("myfont.ttf", 16)
// ...
imgui.PushFont(myfont)
imgui.Text("myfont.ttf example")
Imgui.PopFont()