Skip to content

Commit

Permalink
Merge pull request #299 from gucio321/examples-update
Browse files Browse the repository at this point in the history
examples: add separated example for each backend
  • Loading branch information
gucio321 authored Sep 11, 2024
2 parents 12321aa + 311ddbd commit 61f0fe2
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 4 deletions.
File renamed without changes
11 changes: 7 additions & 4 deletions examples/main.go → examples/ebiten/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func init() {

func main() {
var err error
img, err = imgui.LoadImage("./test.jpeg")
img, err = imgui.LoadImage("../assets/test.jpeg")
if err != nil {
panic("Failed to load test.jpeg")
}
Expand All @@ -134,9 +134,12 @@ func main() {

backend.CreateWindow("Hello from cimgui-go", 1200, 900)

backend.SetDropCallback(func(p []string) {
fmt.Printf("drop triggered: %v", p)
})
// TODO: not implemented
/*
backend.SetDropCallback(func(p []string) {
fmt.Printf("drop triggered: %v", p)
})
*/

backend.SetCloseCallback(func(b imgui.Backend[ebitenbackend.EbitenBackendFlags]) {
fmt.Println("window is closing")
Expand Down
147 changes: 147 additions & 0 deletions examples/glfw/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package main

import (
"fmt"
"image"
"runtime"

imgui "github.com/AllenDang/cimgui-go"
)

var (
showDemoWindow bool
value1 int32
value2 int32
value3 int32
values [2]int32 = [2]int32{value1, value2}
content string = "Let me try"
r float32
g float32
b float32
a float32
color4 [4]float32 = [4]float32{r, g, b, a}
selected bool
backend imgui.Backend[imgui.GLFWWindowFlags]
img *image.RGBA
texture *imgui.Texture
barValues []int64
)

func callback(data imgui.InputTextCallbackData) int {
fmt.Println("got call back")
return 0
}

func showWidgetsDemo() {
if showDemoWindow {
imgui.ShowDemoWindowV(&showDemoWindow)
}

imgui.SetNextWindowSizeV(imgui.NewVec2(300, 300), imgui.CondOnce)
imgui.Begin("Window 1")
if imgui.ButtonV("Click Me", imgui.NewVec2(80, 20)) {
w, h := backend.DisplaySize()
fmt.Println(w, h)
}
imgui.TextUnformatted("Unformatted text")
imgui.Checkbox("Show demo window", &showDemoWindow)
if imgui.BeginCombo("Combo", "Combo preview") {
imgui.SelectableBoolPtr("Item 1", &selected)
imgui.SelectableBool("Item 2")
imgui.SelectableBool("Item 3")
imgui.EndCombo()
}

if imgui.RadioButtonBool("Radio button1", selected) {
selected = true
}

imgui.SameLine()

if imgui.RadioButtonBool("Radio button2", !selected) {
selected = false
}

imgui.InputTextWithHint("Name", "write your name here", &content, 0, callback)
imgui.Text(content)
imgui.SliderInt("Slider int", &value3, 0, 100)
imgui.DragInt("Drag int", &value1)
imgui.DragInt2("Drag int2", &values)
value1 = values[0]
imgui.ColorEdit4("Color Edit3", &color4)
imgui.End()
}

func showPictureLoadingDemo() {
// demo of showing a picture
basePos := imgui.MainViewport().Pos()
imgui.SetNextWindowPosV(imgui.NewVec2(basePos.X+60, 600), imgui.CondOnce, imgui.NewVec2(0, 0))
imgui.Begin("Image")
imgui.Text(fmt.Sprintf("pointer = %v", texture.ID))
imgui.ImageV(texture.ID, imgui.NewVec2(float32(texture.Width), float32(texture.Height)), imgui.NewVec2(0, 0), imgui.NewVec2(1, 1), imgui.NewVec4(1, 1, 1, 1), imgui.NewVec4(0, 0, 0, 0))
imgui.End()
}

func showImPlotDemo() {
basePos := imgui.MainViewport().Pos()
imgui.SetNextWindowPosV(imgui.NewVec2(basePos.X+400, basePos.Y+60), imgui.CondOnce, imgui.NewVec2(0, 0))
imgui.SetNextWindowSizeV(imgui.NewVec2(500, 300), imgui.CondOnce)
imgui.Begin("Plot window")
if imgui.PlotBeginPlotV("Plot", imgui.NewVec2(-1, -1), 0) {
imgui.PlotPlotBarsS64PtrInt("Bar", barValues, int32(len(barValues)))
imgui.PlotPlotLineS64PtrInt("Line", barValues, int32(len(barValues)))
imgui.PlotEndPlot()
}
imgui.End()
}

func afterCreateContext() {
texture = imgui.NewTextureFromRgba(img)
imgui.PlotCreateContext()
}

func loop() {
showWidgetsDemo()
showPictureLoadingDemo()
showImPlotDemo()
}

func beforeDestroyContext() {
imgui.PlotDestroyContext()
}

func init() {
runtime.LockOSThread()
}

func main() {
var err error
img, err = imgui.LoadImage("../assets/test.jpeg")
if err != nil {
panic("Failed to load test.jpeg")
}

for i := 0; i < 10; i++ {
barValues = append(barValues, int64(i+1))
}

backend, _ = imgui.CreateBackend(imgui.NewGLFWBackend())
backend.SetAfterCreateContextHook(afterCreateContext)
backend.SetBeforeDestroyContextHook(beforeDestroyContext)

backend.SetBgColor(imgui.NewVec4(0.45, 0.55, 0.6, 1.0))

backend.CreateWindow("Hello from cimgui-go", 1200, 900)

backend.SetDropCallback(func(p []string) {
fmt.Printf("drop triggered: %v", p)
})

backend.SetCloseCallback(func(b imgui.Backend[imgui.GLFWWindowFlags]) {
fmt.Println("window is closing")
})

backend.SetIcons(img)

backend.Run(loop)
}
22 changes: 22 additions & 0 deletions examples/sdl/imgui.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0

[Window][Window 1]
Pos=60,60
Size=300,300
Collapsed=0

[Window][Image]
Pos=60,484
Size=272,308
Collapsed=0

[Window][Plot window]
Pos=342,349
Size=500,253
Collapsed=0

[Docking][Data]

149 changes: 149 additions & 0 deletions examples/sdl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//go:build !darwin

package main

import (
"fmt"
"image"
"runtime"

imgui "github.com/AllenDang/cimgui-go"
)

var (
showDemoWindow bool
value1 int32
value2 int32
value3 int32
values [2]int32 = [2]int32{value1, value2}
content string = "Let me try"
r float32
g float32
b float32
a float32
color4 [4]float32 = [4]float32{r, g, b, a}
selected bool
backend imgui.Backend[imgui.SDLWindowFlags]
img *image.RGBA
texture *imgui.Texture
barValues []int64
)

func callback(data imgui.InputTextCallbackData) int {
fmt.Println("got call back")
return 0
}

func showWidgetsDemo() {
if showDemoWindow {
imgui.ShowDemoWindowV(&showDemoWindow)
}

imgui.SetNextWindowSizeV(imgui.NewVec2(300, 300), imgui.CondOnce)
imgui.Begin("Window 1")
if imgui.ButtonV("Click Me", imgui.NewVec2(80, 20)) {
w, h := backend.DisplaySize()
fmt.Println(w, h)
}
imgui.TextUnformatted("Unformatted text")
imgui.Checkbox("Show demo window", &showDemoWindow)
if imgui.BeginCombo("Combo", "Combo preview") {
imgui.SelectableBoolPtr("Item 1", &selected)
imgui.SelectableBool("Item 2")
imgui.SelectableBool("Item 3")
imgui.EndCombo()
}

if imgui.RadioButtonBool("Radio button1", selected) {
selected = true
}

imgui.SameLine()

if imgui.RadioButtonBool("Radio button2", !selected) {
selected = false
}

imgui.InputTextWithHint("Name", "write your name here", &content, 0, callback)
imgui.Text(content)
imgui.SliderInt("Slider int", &value3, 0, 100)
imgui.DragInt("Drag int", &value1)
imgui.DragInt2("Drag int2", &values)
value1 = values[0]
imgui.ColorEdit4("Color Edit3", &color4)
imgui.End()
}

func showPictureLoadingDemo() {
// demo of showing a picture
basePos := imgui.MainViewport().Pos()
imgui.SetNextWindowPosV(imgui.NewVec2(basePos.X+60, 600), imgui.CondOnce, imgui.NewVec2(0, 0))
imgui.Begin("Image")
imgui.Text(fmt.Sprintf("pointer = %v", texture.ID))
imgui.ImageV(texture.ID, imgui.NewVec2(float32(texture.Width), float32(texture.Height)), imgui.NewVec2(0, 0), imgui.NewVec2(1, 1), imgui.NewVec4(1, 1, 1, 1), imgui.NewVec4(0, 0, 0, 0))
imgui.End()
}

func showImPlotDemo() {
basePos := imgui.MainViewport().Pos()
imgui.SetNextWindowPosV(imgui.NewVec2(basePos.X+400, basePos.Y+60), imgui.CondOnce, imgui.NewVec2(0, 0))
imgui.SetNextWindowSizeV(imgui.NewVec2(500, 300), imgui.CondOnce)
imgui.Begin("Plot window")
if imgui.PlotBeginPlotV("Plot", imgui.NewVec2(-1, -1), 0) {
imgui.PlotPlotBarsS64PtrInt("Bar", barValues, int32(len(barValues)))
imgui.PlotPlotLineS64PtrInt("Line", barValues, int32(len(barValues)))
imgui.PlotEndPlot()
}
imgui.End()
}

func afterCreateContext() {
texture = imgui.NewTextureFromRgba(img)
imgui.PlotCreateContext()
}

func loop() {
showWidgetsDemo()
showPictureLoadingDemo()
showImPlotDemo()
}

func beforeDestroyContext() {
imgui.PlotDestroyContext()
}

func init() {
runtime.LockOSThread()
}

func main() {
var err error
img, err = imgui.LoadImage("../assets/test.jpeg")
if err != nil {
panic("Failed to load test.jpeg")
}

for i := 0; i < 10; i++ {
barValues = append(barValues, int64(i+1))
}

backend, _ = imgui.CreateBackend(imgui.NewSDLBackend())
backend.SetAfterCreateContextHook(afterCreateContext)
backend.SetBeforeDestroyContextHook(beforeDestroyContext)

backend.SetBgColor(imgui.NewVec4(0.45, 0.55, 0.6, 1.0))

backend.CreateWindow("Hello from cimgui-go", 1200, 900)

backend.SetDropCallback(func(p []string) {
fmt.Printf("drop triggered: %v", p)
})

backend.SetCloseCallback(func(b imgui.Backend[imgui.SDLWindowFlags]) {
fmt.Println("window is closing")
})

backend.SetIcons(img)

backend.Run(loop)
}

0 comments on commit 61f0fe2

Please sign in to comment.