Hello, I am having a bit of trouble in a couple of areas and have a couple of questions.
1) When handling an event using types, do you place the actions to be performed upon in the update_state method for that type that you want the action performed on?
For example, I have a TNewCanvas type and a TNewWindow type. When I resize the TNewWindow (MainWindow) Object I want to resize the TNewCanvas to fit the new size of the TNewWinow Object. Do I put the statement to reshap the canvas in the parent TNewWindow object update_state method or the TNewCanvas obect (CHild)? I am currently using an event hook to handle the resizing using a resizeWindows function. This seems to work but doesn't seem perfect.
2) I am not clear if its a good idea to have all my event handler case statements in my Type objects themselves. Is this a good idea?
3) I would like to make this application more event driven, where I check what event happened and which object it affects and take the appropriate action instead of going through each objects update_state method (This doesn't seem very effective).
I have seen Noel Cower's version wrapper but I can't say I really understand it as it doesn't have the main routine code that actually calls the different types\functions.
Sorry this is a long request but I would really appreciate any responses or direction anyone could give me. (Only been doing blitzmax for a short time).
Here is my code:
1) When handling an event using types, do you place the actions to be performed upon in the update_state method for that type that you want the action performed on?
For example, I have a TNewCanvas type and a TNewWindow type. When I resize the TNewWindow (MainWindow) Object I want to resize the TNewCanvas to fit the new size of the TNewWinow Object. Do I put the statement to reshap the canvas in the parent TNewWindow object update_state method or the TNewCanvas obect (CHild)? I am currently using an event hook to handle the resizing using a resizeWindows function. This seems to work but doesn't seem perfect.
2) I am not clear if its a good idea to have all my event handler case statements in my Type objects themselves. Is this a good idea?
3) I would like to make this application more event driven, where I check what event happened and which object it affects and take the appropriate action instead of going through each objects update_state method (This doesn't seem very effective).
I have seen Noel Cower's version wrapper but I can't say I really understand it as it doesn't have the main routine code that actually calls the different types\functions.
Sorry this is a long request but I would really appreciate any responses or direction anyone could give me. (Only been doing blitzmax for a short time).
Here is my code:
SuperStrict Global GAME_WIDTH:Int=800 Global GAME_HEIGHT:Int=600 AutoMidHandle True 'Global tImg:TImage=LoadImage(LoadBank("C:\BlitzmaxDev\Saxon_MapEditor\Tilesets\tileset.png")) '***********Tiles 'Global tImg_Tileset1:TImage=LoadAnimImage("C:\C:\BlitzmaxDev\Saxon_MapEditor\Tilesets\tileset.png",32,32,0,7) Local wx:Int=(GadgetWidth(Desktop())-GAME_WIDTH)/2 Local wy:Int=(GadgetHeight(Desktop())-GAME_HEIGHT)/2 '*****************************MAIN OBJECT LIST******************************************* Global GadgetObjectList:TList=CreateList() '*****************************CREATE OUR MAIN WINDOW OBJECT****************************** Global MainWindow:TNewWindow= New TNewWindow MainWindow.create(MainWindow,"The Main Window",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null) Global text$= "My current size (w,h) is " + ClientWidth(MainWindow.Tgad)+ "," + ClientHeight(MainWindow.Tgad) SetStatusText MainWindow.Tgad, text$ '*****************************CREATE OUR MAIN DRAW CANVAS******************************** Global MainCanvas:TNewCanvas= New TNewCanvas MainCanvas.Create(MainCanvas,0,0,ClientWidth(MainWindow.Tgad),ClientHeight(MainWindow.Tgad),MainWindow.Tgad,Null) SetGraphics CanvasGraphics(MainCanvas.Tgad) '*****************************Create the Panels***************************************** Global TilePanel:TNewPanel = New TNewPanel TilePanel.create(TilePanel,0,0,ClientWidth(MainCanvas.Tgad)/3,ClientHeight(MainCanvas.Tgad),MainCanvas.Tgad,PANEL_GROUP,"TILE PANEL") SetGadgetLayout TilePanel.Tgad,1,0,1,0 '*****************************Create the Tile Canvas************************************ Global TileCanvas:TNewCanvas = New TNewCanvas TileCanvas.create(TileCanvas, 0,ClientHeight(TilePanel.Tgad)/2,ClientWidth(TilePanel.Tgad),ClientHeight(TilePanel.Tgad)/1.5,TilePanel.Tgad,Null) SetGraphics CanvasGraphics(TileCanvas.Tgad) SetGadgetColor TileCanvas.Tgad,100,100,100 '****************************Main Processing Loop****************************** Repeat Cls SetGraphics CanvasGraphics(Maincanvas.Tgad) SetGraphics CanvasGraphics(TileCanvas.Tgad) For Local o:TMyGadget=EachIn GadgetObjectList o.UpdateState() Next Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() End '*********************************TYPES*****************************************8 '******************************************************************************* Type TMyGadget Field Tgad:TGadget Field Tgadx:Int Field Tgady:Int Field Tgadw:Int Field Tgadh:Int Method UpdateState() Abstract End Type '************************************THE WINDOW TYPE********************************* Type TNewWindow Extends TMyGadget Method Create(ObjectName:TNewWindow,wCaption:String,wX:Int,wY:Int,wWidth:Int,wHeight:Int,wParent:TGadget) Tgad= CreateWindow(wCaption,wX,wY,wWidth,wHeight,wParent,WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_MENU | WINDOW_STATUS) ListAddLast GadgetObjectList, ObjectName End Method Method UpdateState() AddHook EmitEventHook, Mainhook WaitEvent() Select EventID() Case EVENT_WINDOWCLOSE End ' Case EVENT_WINDOWSIZE ' Case EVENT_MOUSEMOVE ' Case EVENT_GADGETPAINT ' SetGraphics CanvasGraphics(Maincanvas) ' SetViewport 0, 0, ClientWidth(Mainwindow), ClientHeight(Mainwindow) ' Cls Case EVENT_APPTERMINATE End End Select 'Flip End Method End Type '*************************************THE CANVAS TYPE*********************************************** Type TNewCanvas Extends TMyGadget Method Create(ObjectName:TNewCanvas,wX:Int,wY:Int,wWidth:Int,wHeight:Int,wParent:TGadget,Format:Int) Tgad= CreateCanvas(wX,wY,wWidth,wHeight,wParent) ListAddLast GadgetObjectList, ObjectName End Method Method UpdateState() AddHook EmitEventHook, Mainhook WaitEvent() Select EventID() Case EVENT_WINDOWCLOSE End 'Case EVENT_WINDOWSIZE ' Case EVENT_MOUSEMOVE Local x:Int=0 Local y:Int=0 x=EventX() y=EventY() Local statustext:String statustext= text$ + " X Pixel loc= " + x + " Y Pixel loc = " + y SetStatusText MainWindow.Tgad, statustext Case EVENT_GADGETPAINT SetGraphics CanvasGraphics(Maincanvas.Tgad) Case EVENT_APPTERMINATE End End Select 'Flip End Method End Type '*****************************************PANEL TYPE**************************** Type TNewPanel Extends TMyGadget Method Create(ObjectName:TNewPanel,wX:Int,wY:Int,wWidth:Int,wHeight:Int,wParent:TGadget,Format:Int,Title:String) Tgad= CreatePanel(wX,wY,wWidth,wHeight,wParent,Format,Title) ListAddLast GadgetObjectList, ObjectName End Method Method UpdateState() AddHook EmitEventHook, Mainhook WaitEvent() Select EventID() Case EVENT_WINDOWCLOSE End Case EVENT_APPTERMINATE End End Select 'Flip End Method End Type '**********************************FUNCTIONS************************************ '******************************************************************************* Function MainHook:Object(iId:Int,tData:Object,tContext:Object) Local Event:TEvent=TEvent(tData) If event=Null Return Null If Event.source=MainCanvas And Event.ID=EVENT_MOUSEDOWN Return Null EndIf If Event.source=MainWindow Or MainCanvas And event.id=EVENT_WINDOWSIZE ResizeWindows() End If Return tData End Function '**********************************Resize our Windows*************************** Function ResizeWindows() SetGadgetShape(Maincanvas.Tgad, 0, 0, ClientWidth(Mainwindow.Tgad), ClientHeight(Mainwindow.Tgad)) SetGadgetShape(TilePanel.Tgad,0,0,ClientWidth(MainCanvas.Tgad)/3,ClientHeight(MainCanvas.Tgad)) SetGadgetShape(TileCanvas.Tgad,0,ClientHeight(TilePanel.Tgad)/2,ClientWidth(TilePanel.Tgad),ClientHeight(TilePanel.Tgad)/1.5) SetViewport 0, 0, ClientWidth(MainWindow.Tgad),ClientHeight(MainWindow.Tgad) End Function