Skip to content

Commit

Permalink
Supoort default arguments for global functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PGZXB committed Aug 1, 2024
1 parent 60d53d2 commit 84d1842
Show file tree
Hide file tree
Showing 13 changed files with 1,375 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ target/
.vscode/
.cache/
*.lock
*.macrocall
66 changes: 32 additions & 34 deletions examples/basic_widgets/src/main.cj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func makeHelp(desc: String): Unit {
TextDisabled("(?)")
if (BeginItemTooltip()) {
PushTextWrapPos(GetFontSize() * 35.0)
TextUnformatted(desc, null<UInt8>())
TextUnformatted(desc)
PopTextWrapPos()
EndTooltip()
}
Expand All @@ -66,26 +66,26 @@ func showBasicWidgetsDemoWindow(state: State): Unit {

SeparatorText("General")

if (Button("Button", ImVec2(0.0, 0.0))) {
if (Button("Button")) {
state.clicked++
}
if ((state.clicked & 1) != 0) {
SameLine(0.0, -1.0)
SameLine()
Text("Thanks for clicking me!")
}

Checkbox("checkbox", state.check)

RadioButton("radio a", state.e, 0)
SameLine(0.0, -1.0)
SameLine()
RadioButton("radio b", state.e, 1)
SameLine(0.0, -1.0)
SameLine()
RadioButton("radio c", state.e, 2)

// Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style.
for (i in 0..7) {
if (i > 0) {
SameLine(0.0, -1.0)
SameLine()
}
PushID(Int32(i))

Expand All @@ -112,7 +112,7 @@ func showBasicWidgetsDemoWindow(state: State): Unit {
// See 'Demo->Layout->Text Baseline Alignment' for details.
AlignTextToFramePadding()
Text("Hold to repeat:")
SameLine(0.0, -1.0)
SameLine()

// Arrow buttons with Repeater
let spacing = GetStyle().ItemInnerSpacing.x
Expand All @@ -125,7 +125,7 @@ func showBasicWidgetsDemoWindow(state: State): Unit {
state.counter++
}
PopButtonRepeat()
SameLine(0.0, -1.0)
SameLine()
Text("${state.counter}")

Button("Tooltip", ImVec2(0.0, 0.0))
Expand All @@ -137,9 +137,8 @@ func showBasicWidgetsDemoWindow(state: State): Unit {

// To wire InputText() with std::string or any other custom string type,
// see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file.
InputText("input text", state.str0, UIntNative(state.str0.size), 0,
CFunc<(data: CPointer<CImGuiInputTextCallbackData>) -> Int32>(CPointer<Unit>()), null<Unit>())
SameLine(0.0, -1.0)
InputText("input text", state.str0, UIntNative(state.str0.size))
SameLine()

makeHelp(
"""
Expand All @@ -155,24 +154,23 @@ PROGRAMMER:
You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputText() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example (this is not demonstrated in imgui_demo.cpp)."""
)

InputTextWithHint("input text (w/ hint)", "enter text here", state.str1, UIntNative(state.str1.size), 0,
CFunc<(data: CPointer<CImGuiInputTextCallbackData>) -> Int32>(CPointer<Unit>()), null<Unit>())
InputTextWithHint("input text (w/ hint)", "enter text here", state.str1, UIntNative(state.str1.size))

InputInt("input int", state.i0, 1, 100, 0)
InputFloat("input float", state.f0, 0.01, 1.0, "%.3f", 0)
InputDouble("input double", state.d0, 0.01, 1.0, "%.8f", 0)
InputFloat("input scientific", state.f1, 0.0, 0.0, "%e", 0)
SameLine(0.0, -1.0)
InputInt("input int", state.i0)
InputFloat("input float", state.f0, 0.01, 1.0, "%.3f")
InputDouble("input double", state.d0, 0.01, 1.0, "%.8f")
InputFloat("input scientific", state.f1, 0.0, 0.0, "%e")
SameLine()
makeHelp("""
You can input value using the scientific notation,
e.g. \"1e+8\" becomes \"100000000\".""")

InputFloat3("input float3", state.vec4a, "%.3f", 0)
InputFloat3("input float3", state.vec4a)

SeparatorText("Drags")

DragInt("drag int", state.i1, 1.0, 0, 0, "%d", 0)
SameLine(0.0, -1.0)
SameLine()
makeHelp(
"""
Click and drag to edit value.
Expand All @@ -182,19 +180,19 @@ Double-click or CTRL+click to input value."""

DragInt("drag int 0..100", state.i2, 1.0, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp)

DragFloat("drag float", state.f2, 0.005, 0.0, 0.0, "%.3f", 0)
DragFloat("drag small float", state.f3, 0.0001, 0.0, 0.0, "%.06f ns", 0)
DragFloat("drag float", state.f2, 0.005, 0.0, 0.0, "%.3f")
DragFloat("drag small float", state.f3, 0.0001, 0.0, 0.0, "%.06f ns")

SeparatorText("Sliders")

SliderInt("slider int", state.i3, -1, 3, "%d", 0)
SameLine(0.0, -1.0)
SliderInt("slider int", state.i3, -1, 3)
SameLine()
makeHelp("CTRL+click to input value.")

SliderFloat("slider float", state.f4, 0.0, 1.0, "ratio = %.3f", 0)
SliderFloat("slider float", state.f4, 0.0, 1.0, "ratio = %.3f")
SliderFloat("slider float (log)", state.f5, -10.0, 10.0, "%.4f", ImGuiSliderFlags_Logarithmic)

SliderAngle("slider angle", state.angle, -360.0, 360.0, "%.0f deg", 0)
SliderAngle("slider angle", state.angle, -360.0, 360.0)

// Using the format string to display a name instead of an integer.
// Here we completely omit '%d' from the format string, so it'll only display a name.
Expand All @@ -206,15 +204,15 @@ Double-click or CTRL+click to input value."""
} else {
"Unknown"
}
SliderInt("slider enum", state.elem, 0, Element_COUNT - 1, elemName, 0) // Use ImGuiSliderFlags_NoInput flag to disable CTRL+Click here.
SliderInt("slider enum", state.elem, 0, Element_COUNT - 1, elemName) // Use ImGuiSliderFlags_NoInput flag to disable CTRL+Click here.

SameLine(0.0, -1.0)
SameLine()
makeHelp("Using the format string parameter to display a name instead of the underlying integer.")

SeparatorText("Selectors/Pickers")

ColorEdit3("color 1", state.col1, 0)
SameLine(0.0, -1.0)
ColorEdit3("color 1", state.col1)
SameLine()
makeHelp(
"""
Click on the color square to open a color picker.
Expand All @@ -223,12 +221,12 @@ Right-click on the color square to show options.
CTRL+click on individual component to input value."""
)

ColorEdit4("color 2", state.col2, 0)
ColorEdit4("color 2", state.col2)

// Using the _simplified_ one-liner Combo() api here
// See "Combo" section for examples of how to use the more flexible BeginCombo()/EndCombo() api.
Combo("combo", state.itemCurrent, state.items, Int32(state.items.size), -1)
SameLine(0.0, -1.0)
Combo("combo", state.itemCurrent, state.items, Int32(state.items.size))
SameLine()
makeHelp(
"""
Using the simplified one-liner Combo API here.
Expand All @@ -238,7 +236,7 @@ Refer to the \"Combo\" section below for an explanation of how to use the more f
// Using the _simplified_ one-liner ListBox() api here
// See "List boxes" section for examples of how to use the more flexible BeginListBox()/EndListBox() api.
ListBox("listbox", state.listBoxItemCurrent, state.items, Int32(state.items.size), 4)
SameLine(0.0, -1.0)
SameLine()
makeHelp(
"""
Using the simplified one-liner ListBox API here.
Expand Down
12 changes: 6 additions & 6 deletions examples/helloworld/src/main.cj
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ main(): Int64 {

Text("Click Count: ${clickCount}")
SameLine(0.0, -1.0)
if (Button("Click Me!", ImVec2(0.0, 0.0))) {
if (Button("Click Me!")) {
clickCount.value += 1
}

Combo("combo", selectedItemIndex, items, Int32(items.size), -1)
Combo("combo", selectedItemIndex, items, Int32(items.size))
Text("Int Value: ${value}")
SliderInt("Int Value", value, 0, 200, "%d", 0)
DragInt("Int Value", value, 1.0, 0, 200, "%d", 0)
SliderInt("Int Value", value, 0, 200)
DragInt("Int Value", value, 1.0, 0, 200)

InputFloat("Font global scale", fontGlobalScale, 0.0, 0.0, "%.2f", 0)
ColorEdit4("Background Color", color, 0)
InputFloat("Font global scale", fontGlobalScale, 0.0, 0.0, "%.2f")
ColorEdit4("Background Color", color)

End()
}
Expand Down
1 change: 1 addition & 0 deletions generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $run_args += " --capi-pkg-name=imgui4cj.capi"
$run_args += " --c-src-path=$path/c_src"
$run_args += " --wcapi-pkg-path=$path/src/wcapi"
$run_args += " --wcapi-pkg-name=imgui4cj.wcapi"
$run_args += " --macros-pkg-name=imgui4cj.macros"
$run_args += " --doc-path=$path/docs"


Expand Down
1 change: 1 addition & 0 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ run_args="$run_args --capi-pkg-name='imgui4cj.capi'"
run_args="$run_args --c-src-path='$path/c_src'"
run_args="$run_args --wcapi-pkg-path='$path/src/wcapi'"
run_args="$run_args --wcapi-pkg-name='imgui4cj.wcapi'"
run_args="$run_args --macros-pkg-name='imgui4cj.macros'"
run_args="$run_args --doc-path='$path/docs'"

cd "$path/generator" && \
Expand Down
41 changes: 39 additions & 2 deletions generator/src/gen_funcs.cj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ struct FuncParameter {
struct FuncSignature {
FuncSignature(let name: String, let params: ArrayList<FuncParameter>, let retType: String, let structName: String,
let isClassStaticMethod: Bool, let isConstructor: Bool, let isDestructor: Bool, let originalName: String,
let nonUDT: Bool, let imguiFuncDecl: String, let cimguiFuncDecl: String) {
let nonUDT: Bool, let defaultaArgs: HashMap<String, String>, let imguiFuncDecl: String,
let cimguiFuncDecl: String) {
}
}

func parseFuncsJson(funcsJson: JsonValue): ArrayList<FuncSignature> {
let j2map = {
object: JsonObject =>
let result = HashMap<String, String>()
for ((k, v) in object.getFields()) {
result[k] = v.asString().getValue()
}
return result
}

let result = ArrayList<FuncSignature>()

for ((_, fnOverloads) in funcsJson.asObject().getFields()) {
Expand All @@ -35,6 +45,9 @@ func parseFuncsJson(funcsJson: JsonValue): ArrayList<FuncSignature> {
fnSig.put("funcname", JsonString("~" + fnSig["stname"].asString().getValue()))
fnSig.put("argsoriginal", JsonString("()"))
}
if (!fnSig.containsKey("defaults")) {
fnSig.put("defaults", JsonObject())
}
result.append(
FuncSignature(
fnSig["ov_cimguiname"].asString().getValue(), // name
Expand All @@ -60,6 +73,7 @@ func parseFuncsJson(funcsJson: JsonValue): ArrayList<FuncSignature> {
fnSig.get("destructor")?.asBool().getValue() ?? false, // isDestructor
fnSig["funcname"].asString().getValue(), // originalName
(fnSig.get("nonUDT")?.asInt().getValue() ?? 0) == 1, // nonUDT
j2map(fnSig["defaults"].asObject()), // defaultaArgs
"${fnSig["ret"].asString().getValue()} ${fnSig["funcname"].asString().getValue()}${fnSig["argsoriginal"].asString().getValue()}", // imguiFuncDecl
"${fnSig["ret"].asString().getValue()} ${fnSig["ov_cimguiname"].asString().getValue()}${fnSig["args"].asString().getValue()}" // cimguiFuncDecl
)
Expand Down Expand Up @@ -174,6 +188,7 @@ func genFuncsWCAPICode<T>(
os: StringWriter<T>,
pkgName: String,
capiPkgName: String,
macrosPkgName: String,
funcs: ArrayList<FuncSignature>,
typeAlias: HashMap<String, CjType>,
definedStructNames: HashSet<String>,
Expand All @@ -196,6 +211,8 @@ func genFuncsWCAPICode<T>(
os.writeln("package ${pkgName}")
os.writeln()
os.writeln("import ${capiPkgName}")
os.writeln("import ${macrosPkgName}.*")
os.writeln("import std.math.* // for MathExtension")
os.writeln()
for (fn in funcs) {
if (fn.isDestructor) {
Expand Down Expand Up @@ -266,6 +283,25 @@ func genFuncsWCAPICode<T>(
)
}


if (!fn.defaultaArgs.isEmpty()) {
let defaultArgs = ArrayList<String>()
for (p in params) {
if (let Some(arg) <- fn.defaultaArgs.get(p.name)) {
defaultArgs.append(transLiteralFromCToWCAPI(arg, p.ty, typeAlias))
}
}

os.write("@DefaultArgs[")
for ((i, arg) in withIndex(defaultArgs)) {
os.write(arg)
if (i != defaultArgs.size - 1) {
os.write(", ")
}
}
os.writeln("]")
}

os.writeln("${fnSb.toString()}: ${wcapiRetTy} {")
os.writeln(" return ${wcapiRet}")
os.writeln("}")
Expand All @@ -278,14 +314,15 @@ func genFuncsWCAPICode(
filename: String,
pkgName: String,
capiPkgName: String,
macrosPkgName: String,
funcs: ArrayList<FuncSignature>,
typeAlias: HashMap<String, CjType>,
definedStructNames: HashSet<String>,
apiInfo: APIInfo
) {
try (fp = File(filename, OpenOption.CreateOrTruncate(false))) {
try (sw = StringWriter(fp)) {
genFuncsWCAPICode(sw, pkgName, capiPkgName, funcs, typeAlias, definedStructNames, apiInfo)
genFuncsWCAPICode(sw, pkgName, capiPkgName, macrosPkgName, funcs, typeAlias, definedStructNames, apiInfo)
}
}
}
6 changes: 6 additions & 0 deletions generator/src/gen_structs.cj
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,12 @@ func genStructsWCAPICode<T>(os: StringWriter<T>, pkgName: String, capiPkgName: S
os.writeln()
}
}
// Create NULL
os.writeln(" public static func null(): ${def.name} {")
os.writeln(" return ${def.name}(CPointer<${getPkgName(capiPkgName)}.${def.name}>(),")
os.writeln(" count: None, owned: false)")
os.writeln(" }")
os.writeln()
// Finalizer for releasing managed pointer
os.writeln(" ~init() {")
if (let Some(d) <- destructorTable.get(def.name)) {
Expand Down
6 changes: 4 additions & 2 deletions generator/src/main.cj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ main(args: Array<String>): Int64 {
"--c-src-path=",
"--wcapi-pkg-path=",
"--wcapi-pkg-name=",
"--macros-pkg-name=",
"--doc-path="
]
)
Expand All @@ -43,6 +44,7 @@ main(args: Array<String>): Int64 {
let cSrcOutputDir = argopt.getArg("c-src-path").getOrThrow()
let wcapiOutputDir = argopt.getArg("wcapi-pkg-path").getOrThrow()
let wcapiPkgName = argopt.getArg("wcapi-pkg-name").getOrThrow()
let macrosPkgName = argopt.getArg("macros-pkg-name").getOrThrow()
let docOutputDir = argopt.getArg("doc-path").getOrThrow()

let apiInfo = APIInfo()
Expand Down Expand Up @@ -169,8 +171,8 @@ main(args: Array<String>): Int64 {
// Generate cimgui funcs wcapi -> src/wcapi/funcs.cj
let funcsWCAPICodeFilename = "${wcapiOutputDir}/funcs.cj"
println("Generating cimgui funcs wcapi -> ${funcsWCAPICodeFilename} ...")
genFuncsWCAPICode(funcsWCAPICodeFilename, wcapiPkgName, capiPkgName, funcsSigs, typeAlias, definedStructNames,
apiInfo)
genFuncsWCAPICode(funcsWCAPICodeFilename, wcapiPkgName, capiPkgName, macrosPkgName, funcsSigs, typeAlias,
definedStructNames, apiInfo)
println("Generated cimgui funcs wcapi -> ${funcsWCAPICodeFilename}")

// Generate cimgui structs wcapi (cj class) -> src/wcapi/structs.cj
Expand Down
Loading

0 comments on commit 84d1842

Please sign in to comment.