heartbone wrote:so your only option is to use a pre-processor or compiler that translates OOP syntax to procedural PB.
Translates OOP syntax to PB????
Just curious,
why?
Because that seems insane.
Why not to machine code?
Because it integrates nicely with PB. It's just an extension on top of PB.
For example, I use some macros to make the work with Interfaces easier for me.
It supports inheritance, constructors, destructors, method overriding.
The constructors and destructors are called in proper way for the inheritance hierarchy.
An example "Class"/Interface looks like this:
Code: Select all
;------- SMALL EXAMPLE ---------
;- Class Window
;{ Class Window
Interface Window Extends Object
Show()
Hide()
Close()
GetWidth.l()
GetHeight.l()
EndInterface
Structure sWindow Extends sObject
Window_Num.i
EndStructure
Method(Window,destroy)(*this.sWindow)
Dbg("Window::destroy()")
If *this\Window_Num
CloseWindow(*this\Window_Num)
EndIf
_Object_Destroy(*this)
EndMethod()
Method(Window,Show)(*this.sWindow)
If *this\Window_Num
HideWindow(*this\Window_Num,#False)
EndIf
EndMethod()
Method(Window,Hide)(*this.sWindow)
If *this\Window_Num
HideWindow(*this\Window_Num,#True)
EndIf
EndMethod()
Method(Window,Close)(*this.sWindow)
If *this\Window_Num
CloseWindow(*this\Window_Num)
*this\Window_Num = 0
EndIf
EndMethod()
Method(Window,GetWidth,.l)(*this.sWindow)
If *this\Window_Num
ProcedureReturn WindowWidth(*this\Window_Num)
EndIf
EndMethod()
Method(Window,GetHeight,.l)(*this.sWindow)
If *this\Window_Num
ProcedureReturn WindowHeight(*this\Window_Num)
EndIf
EndMethod()
Procedure.i New_Window(x,y,width,height,title$, flags=-1)
InitInterface(Window,Object)
Override(Window,destroy)
AddMethod(Window,Show)
AddMethod(Window,Hide)
AddMethod(Window,Close)
AddMethod(Window,GetWidth)
AddMethod(Window,GetHeight)
CHECKINTERFACE(Window)
If flags = -1
flags = #PB_Window_TitleBar|#PB_Window_SystemMenu
EndIf
*ptr\Window_Num = OpenWindow(#PB_Any,x,y,width,height,title$,flags)
ProcedureReturn *ptr
EndProcedure
;}
win1.Window = New( Window(100,100,800,600,"Title") )
Debug win1\GetWidth()
Debug win1\GetHeight()
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Delete( win1 )
It helps me to make this little bit more readable and structured to prevent errors. The macros have also checks if Debugger is enabled,
so I don't forget to add a method etc.
With my small pre-processor hack, I can do it even more simple and structured. Just some excerpt:
Code: Select all
UseClass(GadgetBase)
Class ButtonGadget Extends GadgetBase
Public Method Constructor(x=0, y=0, width=0, height=0, text.s="", flags=0)
me\SetPosition(x,y)
me\SetSize(width,height)
me\SetText(text)
me\SetFlags(flags)
EndMethod
Override Public Method __create()
me\Close()
me\_pb_object = ButtonGadget(#PB_Any,me\GetX(),me\GetY(),me\GetWidth(),me\GetHeight(),me\GetText(),me\GetFlags())
me\__init()
EndMethod
EndClass
Code: Select all
UseClass(GadgetBase)
Class CanvasGadget Extends GadgetBase
Public Method Constructor(x=0, y=0, width=0, height=0, flags=0)
me\SetPosition(x,y)
me\SetSize(width,height)
me\SetFlags(flags)
EndMethod
Override Public Method __create()
me\Close()
me\_pb_object = CanvasGadget(#PB_Any,me\GetX(),me\GetY(),me\GetWidth(),me\GetHeight(),me\GetFlags())
me\__init()
EndMethod
Public Method Output()
If me\_pb_object
ProcedureReturn CanvasOutput(me\_pb_object)
EndIf
EndMethod
Public Method OnMouseEnter(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_MouseEnter = *function
EndMethod
Public Method OnMouseLeave(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_MouseLeave = *function
EndMethod
Public Method OnMouseMove(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_MouseMove = *function
EndMethod
Public Method OnMouseWheel(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_MouseWheel = *function
EndMethod
Public Method OnLeftButtonDown(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_LeftButtonDown = *function
EndMethod
Public Method OnLeftButtonUp(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_LeftButtonUp = *function
EndMethod
Public Method OnRightButtonDown(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_RightButtonDown = *function
EndMethod
Public Method OnRightButtonUp(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_RightButtonUp = *function
EndMethod
Public Method OnMiddleButtonDown(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_MiddleButtonDown = *function
EndMethod
Public Method OnMiddleButtonUp(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_MiddleButtonUp = *function
EndMethod
Public Method OnKeyDown(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_KeyDown = *function
EndMethod
Public Method OnKeyUp(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_KeyUp = *function
EndMethod
Public Method OnInput(*function.__EM_ProcessEvent__)
OOP_GadgetEvents( Str(me) + ":-1")\Event_Input = *function
EndMethod
EndClass
Code: Select all
UseClass(GadgetBase)
Class ItemStateGadget Extends GadgetBase
Public Method.i GetItemState(item)
If me\_pb_object
ProcedureReturn GetGadgetItemState(me\_pb_object, item)
EndIf
EndMethod
Public Method SetItemState(item, state)
If me\_pb_object
SetGadgetItemState(me\_pb_object, item, state)
EndIf
EndMethod
EndClass
Class ItemGadget Extends GadgetBase
Public Method.i ItemID(item)
If me\_pb_object
ProcedureReturn GadgetItemID(me\_pb_object, item)
EndIf
EndMethod
Public Method AddItem(position, text.s, imageID=0, flags=0)
If me\_pb_object
AddGadgetItem(me\_pb_object, position, text.s, imageID, flags)
EndIf
EndMethod
Public Method RemoveItem(position)
If me\_pb_object
RemoveGadgetItem(me\_pb_object, position)
EndIf
EndMethod
Public Method ClearItems()
If me\_pb_object
ClearGadgetItems(me\_pb_object)
EndIf
EndMethod
Public Method.i CountItems()
If me\_pb_object
ProcedureReturn CountGadgetItems(me\_pb_object)
EndIf
EndMethod
Public Method SetItemText(item, text.s, column=0)
If me\_pb_object
SetGadgetItemText(me\_pb_object, item, text, column)
EndIf
EndMethod
Public Method.s GetItemText(item, column=0)
If me\_pb_object
ProcedureReturn GetGadgetItemText(me\_pb_object, item, column)
EndIf
EndMethod
[...]
Code: Select all
Class GadgetBase
_pb_object.i
_tooltip.s
Map _x.i()
Map _y.i()
_width.i
_height.i
_flags.i
_text.s
_state.i
_fontID.i
Map _data.i()
Map _attributes.i()
Map _colors.i()
_disabled.i
_hidden.i
Public Method Constructor()
me\_fontID = #PB_Default ; -1 on Windows
EndMethod
Public Method.i ID()
If me\_pb_object
ProcedureReturn GadgetID(me\_pb_object)
EndIf
EndMethod
Public Method.i Gadget()
ProcedureReturn me\_pb_object
EndMethod
Public Method.i Type()
If me\_pb_object
ProcedureReturn GadgetType(me\_pb_object)
EndIf
EndMethod
Public Method.i IsGadget()
If me\_pb_object
ProcedureReturn IsGadget(me\_pb_object)
EndIf
EndMethod
Public Method Free()
If me\_pb_object
FreeGadget(me\_pb_object)
me\_pb_object = 0
EndIf
EndMethod
Public Method Close()
me\Free()
EndMethod
Public Method Disable(state=1)
me\_disabled = state
If me\_pb_object
DisableGadget(me\_pb_object, state)
EndIf
EndMethod
Public Method Enable(state=1)
state!1
me\_disabled = state
If me\_pb_object
DisableGadget(me\_pb_object,state)
EndIf
EndMethod
Public Method Hide(state=1)
me\_hidden = state
If me\_pb_object
HideGadget(me\_pb_object, state)
EndIf
EndMethod
Public Method Show(state=1)
state!1
me\_hidden = state
If me\_pb_object
HideGadget(me\_pb_object, state)
EndIf
EndMethod
Public Method Resize(x,y,width,height)
me\_x(Str(#PB_Gadget_ContainerCoordinate)) = x
me\_y(Str(#PB_Gadget_ContainerCoordinate)) = y
me\_width = width
me\_height = height
If me\_pb_object
ResizeGadget(me\_pb_object,x,y,width,height)
EndIf
EndMethod
Public Method Activate()
If me\_pb_object
SetActiveGadget(me\_pb_object)
EndIf
EndMethod
Public Method.i IsActivated()
If me\_pb_object
If GetActiveGadget() = me\_pb_object
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndMethod
Public Method.i GetWidth()
If me\_pb_object
me\_width = GadgetWidth(me\_pb_object)
EndIf
ProcedureReturn me\_width
EndMethod
Public Method SetWidth(width)
me\_width = width
If me\_pb_object
ResizeGadget(me\_pb_object,#PB_Ignore,#PB_Ignore,width,#PB_Ignore)
EndIf
EndMethod
Public Method.i GetHeight()
If me\_pb_object
me\_height = GadgetHeight(me\_pb_object)
EndIf
ProcedureReturn me\_height
EndMethod
Public Method SetHeight(height)
me\_height = height
If me\_pb_object
ResizeGadget(me\_pb_object,#PB_Ignore,#PB_Ignore,#PB_Ignore,height)
EndIf
EndMethod
Public Method GetX(flags=#PB_Gadget_ContainerCoordinate)
If me\_pb_object
me\_x(Str(flags)) = GadgetX(me\_pb_object,flags)
EndIf
ProcedureReturn me\_x(Str(flags))
EndMethod
Public Method SetX(x)
me\_x(Str(#PB_Gadget_ContainerCoordinate)) = x
If me\_pb_object
ResizeGadget(me\_pb_object,x,#PB_Ignore,#PB_Ignore,#PB_Ignore)
EndIf
EndMethod
Public Method GetY(flags=#PB_Gadget_ContainerCoordinate)
If me\_pb_object
me\_y(Str(flags)) = GadgetY(me\_pb_object,flags)
EndIf
ProcedureReturn me\_y(Str(flags))
EndMethod
Public Method SetY(y)
me\_y(Str(#PB_Gadget_ContainerCoordinate)) = y
If me\_pb_object
ResizeGadget(me\_pb_object,#PB_Ignore,y,#PB_Ignore,#PB_Ignore)
EndIf
EndMethod
Public Method SetSize(width,height)
me\_width = width
me\_height = height
If me\_pb_object
ResizeGadget(me\_pb_object,#PB_Ignore,#PB_Ignore,width,height)
EndIf
EndMethod
Public Method SetPosition(x,y)
me\_x(Str(#PB_Gadget_ContainerCoordinate)) = x
me\_y(Str(#PB_Gadget_ContainerCoordinate)) = y
If me\_pb_object
ResizeGadget(me\_pb_object,x,y,#PB_Ignore,#PB_Ignore)
EndIf
EndMethod
Public Method.s GetText()
If me\_pb_object
me\_text = GetGadgetText(me\_pb_object)
EndIf
ProcedureReturn me\_text
EndMethod
Public Method SetText(text.s)
me\_text = text
If me\_pb_object
SetGadgetText(me\_pb_object, text)
EndIf
EndMethod
Public Method.i GetState()
If me\_pb_object
me\_state = GetGadgetState(me\_pb_object)
EndIf
ProcedureReturn me\_state
EndMethod
Public Method SetState(state)
me\_state = state
If me\_pb_object
SetGadgetState(me\_pb_object, state)
EndIf
EndMethod
Public Method.i GetFlags()
ProcedureReturn me\_flags
EndMethod
Public Method SetFlags(flags)
me\_flags = flags ; re-create on flag change?
;If me\_pb_object
; ; re-create the gadget
;EndIf
EndMethod
Public Method.i GetData(index=0)
;If me\_pb_object
; ProcedureReturn GetGadgetData(me\_pb_object)
;EndIf
ProcedureReturn me\_data(Str(index))
EndMethod
Public Method SetData(value, index=0)
;If me\_pb_object
; SetGadgetData(me\_pb_object, value)
;EndIf
me\_data(Str(index)) = value
EndMethod
Public Method.i GetAttribute(attribute)
If me\_pb_object
me\_attributes(Str(attribute)) = GetGadgetAttribute(me\_pb_object, attribute)
EndIf
ProcedureReturn me\_attributes(Str(attribute))
EndMethod
Public Method SetAttribute(attribute, value)
me\_attributes(Str(attribute)) = value
If me\_pb_object
SetGadgetAttribute(me\_pb_object, attribute, value)
EndIf
EndMethod
Public Method.i GetFont()
If me\_pb_object
me\_fontID = GetGadgetFont(me\_pb_object)
EndIf
ProcedureReturn me\_fontID
EndMethod
Public Method SetFont(fontID)
me\_fontID = fontID
If me\_pb_object
SetGadgetFont(me\_pb_object, fontID)
EndIf
EndMethod
Public Method.s GetTooltip()
ProcedureReturn me\_tooltip
EndMethod
Public Method SetTooltip(text.s)
me\_tooltip = text
If me\_pb_object
GadgetToolTip(me\_pb_object, text)
EndIf
EndMethod
Public Method.i GetColor(colorType)
;If me\_pb_object
; me\_colors(Str(colorType)) = GetGadgetColor(me\_pb_object, colorType)
;EndIf
ProcedureReturn me\_colors(Str(colorType))
EndMethod
Public Method SetColor(colorType, color)
me\_colors(Str(colorType)) = color
If me\_pb_object
SetGadgetColor(me\_pb_object, colorType, color)
EndIf
EndMethod
[...]
It is just an old example. Nonetheless, in my opinion, a large Framework is much more easier to manage this way. Other brains may think different.
