Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove ImNodes/Plot prefixes #378

Merged
merged 9 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/codegen/enum_deffinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type EnumsSection struct {

// EnumDef represents a definition of an ImGui enum (like flags).
type EnumDef struct {
Name CIdentifier
Name EnumIdentifier
CommentAbove string
Values []EnumValueDef
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func getEnumDefs(enumJsonBytes []byte) ([]EnumDef, error) {
}

enum := EnumDef{
Name: CIdentifier(k),
Name: EnumIdentifier(k),
Values: enumValues,
}

Expand Down
17 changes: 10 additions & 7 deletions cmd/codegen/gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

type (
// EnumIdentifier is in theory EnumName_
EnumIdentifier string
// CIdentifier is a string representing name of struct/func/enum in C
CIdentifier string
// GoIdentifier is a string representing name of struct/func/enum in Go
Expand Down Expand Up @@ -63,14 +65,17 @@ var replace = map[CIdentifier]GoIdentifier{
"igGetPlatformIO": "CurrentPlatformIO",
"igGetStyle": "CurrentStyle",
"igGetMouseCursor": "CurrentMouseCursor",
"ImAxis": "PlotAxisEnum",
"ImAxis": "AxisEnum",
"GetItem_ID": "ItemByID",
}

func (c CIdentifier) trimImGuiPrefix() CIdentifier {
// order sensitive!
prefixes := []string{
"ImGuizmo",
"imnodes",
"ImNodes",
"ImPlot",
"ImGui",
"Im",
"ig",
Expand Down Expand Up @@ -102,8 +107,6 @@ func (c CIdentifier) renameGoIdentifier() GoIdentifier {
c = "new" + c[3:].trimImGuiPrefix()
case HasPrefix(c, "*"):
c = "*" + c[1:].trimImGuiPrefix()
case HasPrefix(c, "imnodes"):
c = Replace(c, "imnodes", "ImNodes", 1)
}

c = TrimPrefix(c, "Get")
Expand All @@ -114,8 +117,8 @@ func (c CIdentifier) renameGoIdentifier() GoIdentifier {
return GoIdentifier(c)
}

func (c CIdentifier) renameEnum() GoIdentifier {
return TrimSuffix(c, "_").renameGoIdentifier()
func (c EnumIdentifier) renameEnum() CIdentifier {
return CIdentifier(TrimSuffix(c, "_"))
}

// returns true if s is of form TypeName<*> <(>Name<*><)>(args)
Expand All @@ -135,8 +138,8 @@ func IsStructName(name CIdentifier, ctx *Context) bool {
return ok
}

func IsEnumName(name CIdentifier, enums map[GoIdentifier]bool) bool {
_, ok := enums[name.renameEnum()]
func IsEnumName(name CIdentifier, enums map[CIdentifier]bool) bool {
_, ok := enums[name]
return ok
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/codegen/gengo_enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
)

// Generate enums and return enum type names
func generateGoEnums(prefix string, enums []EnumDef, ctx *Context) []GoIdentifier {
func generateGoEnums(prefix string, enums []EnumDef, ctx *Context) ([]CIdentifier, error) {
var sb strings.Builder

sb.WriteString(getGoPackageHeader(ctx))

var enumNames []GoIdentifier
var enumNames []CIdentifier
for _, e := range enums {
originalName := e.Name
eName := e.Name.renameEnum()
eName := e.Name.renameEnum().renameGoIdentifier()

enumNames = append(enumNames, eName)
enumNames = append(enumNames, e.Name.renameEnum())

sb.WriteString(fmt.Sprintf("%s\n", e.CommentAbove))
sb.WriteString(fmt.Sprintf("// original name: %s\n", originalName))
Expand All @@ -37,12 +37,12 @@ func generateGoEnums(prefix string, enums []EnumDef, ctx *Context) []GoIdentifie

enumFile, err := os.Create(fmt.Sprintf("%s_enums.go", prefix))
if err != nil {
panic(err.Error())
return nil, err
}

defer enumFile.Close()

_, _ = enumFile.WriteString(sb.String())

return enumNames
return enumNames, nil
}
12 changes: 4 additions & 8 deletions cmd/codegen/gengo_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func GenerateGoFuncs(
type goFuncsGenerator struct {
prefix string
structNames map[CIdentifier]bool
enumNames map[GoIdentifier]bool
enumNames map[CIdentifier]bool
refTypedefs map[CIdentifier]bool

sb strings.Builder
Expand Down Expand Up @@ -426,12 +426,8 @@ func (g *goFuncsGenerator) writeFinishers(shouldDefer bool, finishers []string)
}

// isEnum returns true when given string is a valid enum type.
func isEnum(argType CIdentifier, enumNames map[GoIdentifier]bool) bool {
for en := range enumNames {
if argType.renameEnum() == en {
return true
}
}
func isEnum(argType CIdentifier, enumNames map[CIdentifier]bool) bool {
_, ok := enumNames[argType]

return false
return ok
}
28 changes: 17 additions & 11 deletions cmd/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)

// this cextracts enums and structs names from json file.
func getEnumAndStructNames(enumJsonBytes []byte) (enumNames []GoIdentifier, structNames []CIdentifier, err error) {
func getEnumAndStructNames(enumJsonBytes []byte) (enumNames []EnumIdentifier, structNames []CIdentifier, err error) {
enums, err := getEnumDefs(enumJsonBytes)
if err != nil {
return nil, nil, fmt.Errorf("cannot get enum definitions: %w", err)
Expand All @@ -28,7 +28,7 @@ func getEnumAndStructNames(enumJsonBytes []byte) (enumNames []GoIdentifier, stru
}

for _, e := range enums {
enumNames = append(enumNames, e.Name.renameEnum())
enumNames = append(enumNames, e.Name)
}

for _, s := range structs {
Expand Down Expand Up @@ -113,17 +113,15 @@ type Context struct {

prefix string

funcNames map[CIdentifier]bool
enumNames map[GoIdentifier]bool

structNames map[CIdentifier]bool

funcNames map[CIdentifier]bool
enumNames map[CIdentifier]bool
structNames map[CIdentifier]bool
typedefsNames map[CIdentifier]bool

arrayIndexGetters map[CIdentifier]CIdentifier

refStructNames map[CIdentifier]bool
refEnumNames map[GoIdentifier]bool
refEnumNames map[CIdentifier]bool
refTypedefs map[CIdentifier]bool

// TODO: might want to remove this
Expand Down Expand Up @@ -174,15 +172,19 @@ func parseJson(jsonData *jsonData) (*Context, error) {
return nil, fmt.Errorf("cannot get reference struct and enums names: %w", err)
}

result.refEnumNames = make(map[GoIdentifier]bool)
result.refEnumNames = make(map[CIdentifier]bool)
result.refStructNames = make(map[CIdentifier]bool)
if len(jsonData.refStructAndEnums) > 0 {
refEnums, refStructs, err := getEnumAndStructNames(jsonData.refStructAndEnums)
if err != nil {
return nil, fmt.Errorf("cannot get reference struct and enums names: %w", err)
}

result.refEnumNames = SliceToMap(refEnums)
result.refEnumNames = make(map[CIdentifier]bool)
for _, refEnum := range refEnums {
result.refEnumNames[refEnum.renameEnum()] = true
}

result.refStructNames = SliceToMap(refStructs)
}

Expand All @@ -208,7 +210,11 @@ func main() {

// 1. Generate code
// 1.1. Generate Go Enums
enumNames := generateGoEnums(flags.prefix, context.enums, context)
enumNames, err := generateGoEnums(flags.prefix, context.enums, context)
if err != nil {
glg.Fatalf("Generating enum names: %v", err)
}

context.enumNames = MergeMaps(SliceToMap(enumNames), context.refEnumNames)

// 1.2. Generate Go typedefs
Expand Down
4 changes: 2 additions & 2 deletions cmd/codegen/return_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func getReturnWrapper(
CType: GoIdentifier(fmt.Sprintf("*C.%s", t)),
}, nil
case isEnum(t, context.enumNames):
goType := prefixGoPackage(t.renameEnum(), srcPackage, context)
goType := prefixGoPackage(t.renameGoIdentifier(), srcPackage, context)
return returnWrapper{
returnType: goType,
returnStmt: fmt.Sprintf("%s(%%s)", goType),
Expand All @@ -131,7 +131,7 @@ func getReturnWrapper(
CType: GoIdentifier(fmt.Sprintf("*C.%s", t)),
}, nil
case HasSuffix(t, "*") && isEnum(TrimSuffix(t, "*"), context.enumNames):
goType := prefixGoPackage("*"+TrimSuffix(t, "*").renameEnum(), srcPackage, context)
goType := prefixGoPackage("*"+TrimSuffix(t, "*").renameGoIdentifier(), srcPackage, context)
return returnWrapper{
returnType: goType,
returnStmt: fmt.Sprintf("(%s)(%%s)", goType),
Expand Down
12 changes: 6 additions & 6 deletions examples/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func InputTextCallback(data imgui.InputTextCallbackData) int {

func AfterCreateContext() {
texture = backend.NewTextureFromRgba(img)
implot.PlotCreateContext()
implot.CreateContext()
textEditor = cte.NewTextEditor()
textEditor.SetLanguageDefinition(cte.Cpp)
textEditor.SetText(`// Colorize a C++ file
Expand All @@ -64,7 +64,7 @@ ImGui::Text("Hello World")`)
}

func BeforeDestroyContext() {
implot.PlotDestroyContext()
implot.DestroyContext()
}

func Loop() {
Expand Down Expand Up @@ -135,10 +135,10 @@ func ShowImPlotDemo() {
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()
if implot.BeginPlotV("Plot", imgui.NewVec2(-1, -1), 0) {
implot.PlotBarsS64PtrInt("Bar", barValues, int32(len(barValues)))
implot.PlotLineS64PtrInt("Line", barValues, int32(len(barValues)))
implot.EndPlot()
}
imgui.End()
}
Expand Down
5 changes: 3 additions & 2 deletions examples/ebiten-game-in-texture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ func afterCreateContext() {
Height: screenHeight,
}

implot.PlotCreateContext()
implot.CreateContext()
}

func loop() {
imgui.ClearSizeCallbackPool()
common.ShowWidgetsDemo()
showPictureLoadingDemo()
common.ShowImPlotDemo()
}

func beforeDestroyContext() {
implot.PlotDestroyContext()
implot.DestroyContext()
}

func main() {
Expand Down
3 changes: 2 additions & 1 deletion examples/ebiten-game/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ func main() {

game := &Game{showCimgui: true, cimgui: ebitenBackend}

game.cimgui.CreateWindow("Hello, world!", 800, 600)

common.AfterCreateContext()
defer common.BeforeDestroyContext()

game.cimgui.CreateWindow("Hello, world!", 800, 600)
game.cimgui.SetBgColor(imgui.Vec4{0.2, 0.2, 0.2, 0.7})
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Shapes (Ebitengine Demo)")
Expand Down
Loading
Loading