Skip to content

Commit

Permalink
gencpp: remove ImVec* replaces ad add ref to FLT_* to preset
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Dec 20, 2024
1 parent 3c6fe4a commit 78b4ffb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 39 deletions.
4 changes: 4 additions & 0 deletions cmd/codegen/cimgui-go-preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
"OriginReplace": {
"TextEditor_GetText": "TextEditor_GetText_alloc"
},
"DefaultArgReplace": {
"FLT_MIN": "igGET_FLT_MIN()",
"FLT_MAX": "igGET_FLT_MAX()"
},
"ExtraCGOPreamble": [
"#include \"../imgui/extra_types.h\""
],
Expand Down
52 changes: 13 additions & 39 deletions cmd/codegen/gencpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"regexp"
"strings"
"unicode"
)
Expand Down Expand Up @@ -221,48 +222,21 @@ extern "C" {
invocationStmt := fmt.Sprintf("(%s)", Join(invocationArgs, ","))

for k, v := range f.Defaults {
if v == "ImVec2(0,0)" || v == "ImVec2(0.0f,0.0f)" {
v = "(ImVec2){.x=0, .y=0}"
}

if v == "ImVec2(1,1)" {
v = "(ImVec2){.x=1, .y=1}"
}

if v == "ImVec2(1,0)" {
v = "(ImVec2){.x=1, .y=0}"
}

if v == "ImVec2(0,1)" {
v = "(ImVec2){.x=0, .y=1}"
}

if v == "ImVec2(-1,0)" {
v = "(ImVec2){.x=-1, .y=0}"
}

if v == "ImVec2(-FLT_MIN,0)" {
v = "(ImVec2){.x=-1*igGET_FLT_MIN(), .y=0}"
}

if v == "ImVec4(0,0,0,0)" {
v = "(ImVec4){.x=0, .y=0, .z=0, .w=0}"
}

if v == "ImVec4(1,1,1,1)" {
v = "(ImVec4){.x=1, .y=1, .z=1, .w=1}"
}

if v == "ImVec4(0,0,0,-1)" {
v = "(ImVec4){.x=0, .y=0, .z=0, .w=-1}"
}
// match anything of form ImVec2(0, 0) or ImVec2(0.0f, 0.0f)
re := regexp.MustCompile(`(\w*)\((.*)\)`)
// 3, because according to FindStringSubmatch doc, it returns [ImVec2(x, y), ImVec2, "x, y"]
// we also need to ensure aur match matches the whole string
// we also don't want sizeof
if m := re.FindStringSubmatch(v); len(m) == 3 && m[0] == v && m[1] != "sizeof" {
for k, v := range ctx.preset.DefaultArgReplace {
m[2] = ReplaceAll(m[2], k, v)
}

if v == "ImPlotPoint(0,0)" {
v = "(ImPlotPoint){.x=0, .y=0}"
v = fmt.Sprintf("(%s){%s}", m[1], m[2])
}

if v == "ImPlotPoint(1,1)" {
v = "(ImPlotPoint){.x=1, .y=1}"
if r, ok := ctx.preset.DefaultArgReplace[CIdentifier(v)]; ok {
v = string(r)
}

if v == "FLT_MAX" {
Expand Down
7 changes: 7 additions & 0 deletions cmd/codegen/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ type Preset struct {
// OriginReplace allows to force-replace function name with some other name.
// Introduced to replace TextEditor_GetText -> TextEditor_GetText_alloc
// but could be re-used to force use of another function than json tells us to use.
//
// It differs from Replace - Replace renames an identifier in general (changes its name but refers to the same function).
// This allows to absolutely abandon the source C function and use some OTHER C function.
OriginReplace map[CIdentifier]CIdentifier
// DefaultArgReplace is used in C-side default args generation (gencpp.go).
// cimgui-go uses this to change FLT_MIN with igGet_FLT_MIN()
// NOTE: Iterated randomly!
DefaultArgReplace map[CIdentifier]CIdentifier
// ExtraCGOPreamble allows to specify additional C code elements in Go files.
// For example could be used to include extra files.
// For ease of use, its in form of []string. These lines will be merged and prefixed (if appliable) with '//'
Expand Down

0 comments on commit 78b4ffb

Please sign in to comment.