Skip to content

Commit

Permalink
Merge pull request #340 from gucio321/fix-our-examples
Browse files Browse the repository at this point in the history
examples: major refactor
  • Loading branch information
gucio321 authored Sep 25, 2024
2 parents 9ef4d5b + 24f9717 commit 58292ac
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 551 deletions.
131 changes: 131 additions & 0 deletions examples/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Package common contains code commonly used in the examples section of cimgui-go.
package common

import (
"bytes"
_ "embed"
"fmt"
"image"

"github.com/AllenDang/cimgui-go/backend"
"github.com/AllenDang/cimgui-go/imgui"
_ "github.com/AllenDang/cimgui-go/immarkdown"
_ "github.com/AllenDang/cimgui-go/imnodes"
"github.com/AllenDang/cimgui-go/implot"
)

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
//go:embed test.jpeg
imgData []byte
img *image.RGBA
texture *backend.Texture
barValues []int64
)

// Initialize prepares global variables. Call before anything related to backend.
func Initialize() {
imgImage, _, _ := image.Decode(bytes.NewReader(imgData))
img = backend.ImageToRgba(imgImage)

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

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

func AfterCreateContext() {
texture = backend.NewTextureFromRgba(img)
implot.PlotCreateContext()
}

func BeforeDestroyContext() {
implot.PlotDestroyContext()
}

func Loop() {
ShowWidgetsDemo()
ShowPictureLoadingDemo()
ShowImPlotDemo()
}

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)) {
fmt.Println("Button clicked")
}
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, InputTextCallback)
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 implot.PlotBeginPlotV("Plot", imgui.NewVec2(-1, -1), 0) {
implot.PlotPlotBarsS64PtrInt("Bar", barValues, int32(len(barValues)))
implot.PlotPlotLineS64PtrInt("Line", barValues, int32(len(barValues)))
implot.PlotEndPlot()
}
imgui.End()
}

func Image() *image.RGBA {
return img
}
File renamed without changes
2 changes: 2 additions & 0 deletions examples/ebiten-game-in-texture/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
example-ebiten
imgui.ini
83 changes: 7 additions & 76 deletions examples/ebiten-game-in-texture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/AllenDang/cimgui-go/backend"
ebitenbackend "github.com/AllenDang/cimgui-go/backend/ebiten-backend"
"github.com/AllenDang/cimgui-go/examples/common"
"github.com/AllenDang/cimgui-go/imgui"
"github.com/AllenDang/cimgui-go/implot"
"github.com/hajimehoshi/ebiten/v2"
Expand All @@ -14,68 +15,10 @@ import (
)

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
currentBackend *ebitenbackend.EbitenBackend
texture *backend.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 := currentBackend.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()
Expand All @@ -86,19 +29,6 @@ func showPictureLoadingDemo() {
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 implot.PlotBeginPlotV("Plot", imgui.NewVec2(-1, -1), 0) {
implot.PlotPlotBarsS64PtrInt("Bar", barValues, int32(len(barValues)))
implot.PlotPlotLineS64PtrInt("Line", barValues, int32(len(barValues)))
implot.PlotEndPlot()
}
imgui.End()
}

func afterCreateContext() {
texture = &backend.Texture{
ID: currentBackend.CreateTextureFromGame(&Game{}, screenWidth, screenHeight),
Expand All @@ -110,19 +40,17 @@ func afterCreateContext() {
}

func loop() {
showWidgetsDemo()
common.ShowWidgetsDemo()
showPictureLoadingDemo()
showImPlotDemo()
common.ShowImPlotDemo()
}

func beforeDestroyContext() {
implot.PlotDestroyContext()
}

func main() {
for i := 0; i < 10; i++ {
barValues = append(barValues, int64(i+1))
}
common.Initialize()

currentBackend = ebitenbackend.NewEbitenBackend()
_, _ = backend.CreateBackend(currentBackend)
Expand All @@ -147,6 +75,9 @@ func main() {
currentBackend.Run(loop)
}

// code from ebiten/examples/shape (https://github.com/hajimehoshi/ebiten
// for details see LICENSE of ebiten

const (
screenWidth, screenHeight = 800, 600
)
Expand Down
7 changes: 7 additions & 0 deletions examples/ebiten-game/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: run

build:
go build -v -o example-ebiten .

run: build
./example-ebiten
20 changes: 20 additions & 0 deletions examples/ebiten-game/imgui.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0

[Window][Window 1]
Pos=-4,11
Size=300,300
Collapsed=0

[Window][Image]
Pos=105,492
Size=272,479
Collapsed=0

[Window][Plot window]
Pos=400,60
Size=500,300
Collapsed=0

Loading

0 comments on commit 58292ac

Please sign in to comment.