mirror of
https://github.com/gosticks/gml.git
synced 2025-10-16 12:05:33 +00:00
further progress
This commit is contained in:
parent
b2c937d0a7
commit
e7e9fcd911
2
app.go
2
app.go
@ -127,7 +127,7 @@ func (a *App) Quit() (retCode int) {
|
||||
return
|
||||
}
|
||||
|
||||
func (a *App) SetRootContextProperty(name string, v interface{}) (err error) {
|
||||
func (a *App) SetContextProperty(name string, v interface{}) (err error) {
|
||||
if len(name) == 0 {
|
||||
return errors.New("property name is empty")
|
||||
}
|
||||
|
||||
@ -48,8 +48,9 @@ type genSlot struct {
|
||||
}
|
||||
|
||||
type genParam struct {
|
||||
Name string
|
||||
Type string
|
||||
Name string
|
||||
Type string
|
||||
CType string
|
||||
}
|
||||
|
||||
// TODO: make concurrent with multiple goroutines.
|
||||
|
||||
@ -232,8 +232,9 @@ func parseSignal(gs *genStruct, fset *token.FileSet, f *ast.Field, name string)
|
||||
}
|
||||
|
||||
signal.Params[i] = &genParam{
|
||||
Name: p.Names[0].Name,
|
||||
Type: ident.Name,
|
||||
Name: p.Names[0].Name,
|
||||
Type: ident.Name,
|
||||
CType: goTypeToC(ident.Name),
|
||||
}
|
||||
}
|
||||
|
||||
@ -268,8 +269,9 @@ func parseSlot(gs *genStruct, fset *token.FileSet, f *ast.Field, name string) (e
|
||||
}
|
||||
|
||||
slot.Params[i] = &genParam{
|
||||
Name: p.Names[0].Name,
|
||||
Type: ident.Name,
|
||||
Name: p.Names[0].Name,
|
||||
Type: ident.Name,
|
||||
CType: goTypeToC(ident.Name),
|
||||
}
|
||||
}
|
||||
|
||||
@ -281,3 +283,17 @@ func newParseError(fset *token.FileSet, p token.Pos, err error) error {
|
||||
pos := fset.Position(p)
|
||||
return fmt.Errorf("%s: line %v: %v", pos.Filename, pos.Line, err)
|
||||
}
|
||||
|
||||
func goTypeToC(t string) string {
|
||||
// TODO: add all missing.
|
||||
switch t {
|
||||
case "int":
|
||||
return "int"
|
||||
case "bool":
|
||||
return "int"
|
||||
case "string":
|
||||
return "*char"
|
||||
default:
|
||||
return "gml_variant" // TODO:
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ void {{$struct.CBaseName}}_free({{$struct.CBaseName}});
|
||||
|
||||
{{- /* Signals */ -}}
|
||||
{{- range $signal := $struct.Signals }}
|
||||
void {{$struct.CBaseName}}_{{$signal.Name}}({{$struct.CBaseName}} _v{{cParams $signal.Params false}});
|
||||
void {{$struct.CBaseName}}_{{$signal.Name}}({{$struct.CBaseName}} _v{{cParams $signal.Params true false}});
|
||||
{{end}}
|
||||
|
||||
{{- /* Slots */ -}}
|
||||
|
||||
@ -37,13 +37,13 @@ public:
|
||||
{{/* Signals */ -}}
|
||||
signals:
|
||||
{{- range $signal := $struct.Signals }}
|
||||
void {{$signal.CPPName}}({{cParams $signal.Params true}});
|
||||
void {{$signal.CPPName}}({{cParams $signal.Params true true}});
|
||||
{{- end}}
|
||||
|
||||
{{/* Slots */ -}}
|
||||
public slots:
|
||||
{{- range $slot := $struct.Slots }}
|
||||
void {{$slot.CPPName}}({{cParams $slot.Params true}});
|
||||
void {{$slot.CPPName}}({{cParams $slot.Params true true}});
|
||||
{{end}}
|
||||
};
|
||||
|
||||
@ -82,9 +82,9 @@ void {{$struct.CBaseName}}_free({{$struct.CBaseName}} _v) {
|
||||
|
||||
{{/* Signals */ -}}
|
||||
{{- range $signal := $struct.Signals }}
|
||||
void {{$struct.CBaseName}}_{{$signal.Name}}({{$struct.CBaseName}} _v{{cParams $signal.Params false}}) {
|
||||
void {{$struct.CBaseName}}_{{$signal.Name}}({{$struct.CBaseName}} _v{{cParams $signal.Params true false}}) {
|
||||
auto _vv = ({{$struct.CPPBaseName}}*)_v;
|
||||
emit _vv->{{$signal.CPPName}}();
|
||||
emit _vv->{{$signal.CPPName}}({{cParams $signal.Params false true}});
|
||||
}
|
||||
{{end}}
|
||||
|
||||
@ -96,7 +96,7 @@ void {{$struct.CBaseName}}_{{$slot.Name}}_cb_register({{$struct.CBaseName}}_{{$s
|
||||
{{$struct.CBaseName}}_{{$slot.Name}}_cb = cb;
|
||||
}
|
||||
|
||||
void {{$struct.CPPBaseName}}::{{$slot.CPPName}}({{cParams $slot.Params true}}) {
|
||||
void {{$struct.CPPBaseName}}::{{$slot.CPPName}}({{cParams $slot.Params true true}}) {
|
||||
try {
|
||||
{{$struct.CBaseName}}_{{$slot.Name}}_cb(this->goPtr);
|
||||
}
|
||||
|
||||
@ -9,26 +9,67 @@ package build
|
||||
import "text/template"
|
||||
|
||||
var tmplFuncMap = template.FuncMap{
|
||||
"goParams": tmplFuncGoParams,
|
||||
"cParams": tmplFuncCParams,
|
||||
"goParams": tmplFuncGoParams,
|
||||
"cParams": tmplFuncCParams,
|
||||
"goToCParams": tmplFuncGoToCParams,
|
||||
}
|
||||
|
||||
func tmplFuncGoParams(params []*genParam, skipFirst bool) (s string) {
|
||||
func tmplFuncGoParams(params []*genParam, withType, skipFirstComma bool, optPrefix ...string) (s string) {
|
||||
var prefix string
|
||||
if len(optPrefix) > 0 {
|
||||
prefix = optPrefix[0]
|
||||
}
|
||||
|
||||
for i, p := range params {
|
||||
if !skipFirst || i != 0 {
|
||||
if !skipFirstComma || i != 0 {
|
||||
s += ", "
|
||||
}
|
||||
s += p.Name + " " + p.Type
|
||||
s += prefix + p.Name
|
||||
if withType {
|
||||
s += " " + p.Type
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func tmplFuncCParams(params []*genParam, skipFirst bool) (s string) {
|
||||
func tmplFuncCParams(params []*genParam, withType, skipFirstComma bool) (s string) {
|
||||
for i, p := range params {
|
||||
if !skipFirst || i != 0 {
|
||||
if !skipFirstComma || i != 0 {
|
||||
s += ", "
|
||||
}
|
||||
s += p.Type + " " + p.Name
|
||||
if withType {
|
||||
s += p.CType + " "
|
||||
}
|
||||
s += p.Name
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func tmplFuncGoToCParams(params []*genParam, prefix string, optsIndent ...int) (s string) {
|
||||
var ident string
|
||||
if len(optsIndent) > 0 {
|
||||
for i := 0; i < optsIndent[0]; i++ {
|
||||
ident += " "
|
||||
}
|
||||
}
|
||||
|
||||
addLine := func(l string) {
|
||||
s += "\n" + ident + l
|
||||
}
|
||||
|
||||
// TODO: add all missing.
|
||||
for _, p := range params {
|
||||
switch p.Type {
|
||||
case "int":
|
||||
addLine(prefix + p.Name + " := C.int(" + p.Name + ")")
|
||||
case "bool":
|
||||
addLine(prefix + p.Name + " := C.int(" + p.Name + ")")
|
||||
case "string":
|
||||
return "*char" // TODO:
|
||||
default:
|
||||
return "gml_variant" // TODO:
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -64,9 +64,10 @@ func (_v *{{$struct.Name}}) GMLInit() {
|
||||
|
||||
{{- /* Signals */}}
|
||||
{{range $signal := $struct.Signals }}
|
||||
func (_v *{{$struct.Name}}) {{$signal.EmitName}}({{goParams $signal.Params true}}) {
|
||||
func (_v *{{$struct.Name}}) {{$signal.EmitName}}({{goParams $signal.Params true true}}) {
|
||||
_ptr := (C.{{$struct.CBaseName}})(_v.GMLObject_Pointer())
|
||||
C.{{$struct.CBaseName}}_{{$signal.Name}}(_ptr)
|
||||
{{- goToCParams $signal.Params "_c_" 4}}
|
||||
C.{{$struct.CBaseName}}_{{$signal.Name}}(_ptr{{goParams $signal.Params false false "_c_"}})
|
||||
}
|
||||
{{end}}
|
||||
|
||||
|
||||
@ -62,6 +62,6 @@ func FirstCharToUpper(s string) string {
|
||||
}
|
||||
|
||||
func GetThreadID() int {
|
||||
// TODO: check if this is supported in MaxOSX and Windows. Also create a unit test!
|
||||
// TODO: check if this is supported in MaxOSX and Windows.
|
||||
return syscall.Gettid()
|
||||
}
|
||||
|
||||
@ -17,15 +17,15 @@ import (
|
||||
type Bridge struct {
|
||||
gml.Object
|
||||
_ struct {
|
||||
state int `gml:"property"`
|
||||
hello func() `gml:"slot"`
|
||||
Connected func() `gml:"signal"`
|
||||
state int `gml:"property"`
|
||||
hello func() `gml:"slot"`
|
||||
Connected func(i int) `gml:"signal"`
|
||||
//sign func(i int, s string, b bool) `gml:"signal"`
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bridge) hello() {
|
||||
b.EmitConnected()
|
||||
b.EmitConnected(3)
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -36,7 +36,7 @@ func main() {
|
||||
|
||||
b := &Bridge{}
|
||||
b.GMLInit()
|
||||
app.SetRootContextProperty("bridge", b)
|
||||
app.SetContextProperty("bridge", b)
|
||||
|
||||
err = app.Load("qml/main.qml")
|
||||
if err != nil {
|
||||
|
||||
@ -25,7 +25,8 @@ ApplicationWindow {
|
||||
|
||||
Connections {
|
||||
target: bridge
|
||||
onConnected: function() {
|
||||
onConnected: function(i) {
|
||||
console.log(i)
|
||||
rect.color = "blue"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user