nim-lang / ui

Beginnings of what might become Nim's official UI library.
MIT License
240 stars 32 forks source link

Problems with wrapping rawui.Area #37

Open KnorrFG opened 3 years ago

KnorrFG commented 3 years ago

Hey, I'm trying to wrap the low level drawing stuff into a nicer API, and I'm facing some trouble. If I add the following code to the very bottom of ui.nim

type 
  Area* = ref object of Widget
    #onDraw*: proc(self: Area, p: AreaDrawParams)
    #onMouseEvent*: proc(self: Area, ev: AreaMouseEvent)
    #onMouseCrossed*: proc(self: Area, left: bool)
    #onDragBroken*: proc(self: Area)
    #onKeyEvent*: proc(self: Area, ev: AreaKeyEvent): bool
    handler: rawui.AreaHandler

genImplProcs(Area)

proc drawDefaultCallback(a: ptr AreaHandler, area: ptr Area,
                         p: ptr AreaDrawParams) {.cdecl.} =
  discard

proc meDefaultCallback(self: Area, ev: AreaMouseEvent) {.cdecl.} = discard
proc mcDefaultCallback(self: Area, left: bool) {.cdecl.} = discard
proc dbDefaultCallback(self: Area) {.cdecl.} = discard
proc keDefaultCallback(self: Area, ev: AreaKeyEvent): bool {.cdecl.} = false

proc newArea*(dc = drawDefaultCallback,
              mec = meDefaultCallback,
              mcc = mcDefaultCallback,
              dbc = dbDefaultCallback,
              kec = keDefaultCallback): Area =
  new result
  result.handler.draw = dc
  result.handler.mouseEvent = mec
  result.handler.mouseCrossed = mcc
  result.handler.dragBroken = dbc
  result.handler.keyEvent = kec

Ill get the error: Error: type mismatch: got <proc (a: ptr AreaHandler, area: ptr Area, p: ptr AreaDrawParams){.cdecl, noSideEffect, gcsafe, locks: 0.}> but expected 'proc (a2: ptr AreaHandler, a3: ptr Area, a4: ptr AreaDrawParams){.cdecl.}' for the line: result.handler.draw = dc. Can someone explain why that is, and how to fix it?

neroist commented 1 year ago

Replace ptr Area with ptr rawui.Area. Nim thinks you're referring to the Area type which you defined, not the one from rawui.