About Monkey 2 › Forums › Monkey 2 Programming Help › Object Events
Tagged: Events
This topic contains 6 replies, has 3 voices, and was last updated by
Mark Sibly
2 years, 3 months ago.
-
AuthorPosts
-
January 9, 2017 at 2:15 pm #6338
How can I capture the events from the Button, TextField, CheckBox etc from the code below.
Please create a dir called gui and add the image attached to it
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#Import "gui/"#Import ""#Import ""#Import ""Using std..Using mojo..Using mojox..Global virtualResolution:=New Vec2i( 640,480 )Class MyApp Extends WindowMethod New( title:String,width:Int,height:Int,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )Layout="letterbox"'Create Docking ViewsLocal mainview:=New DockingViewLocal dv:=New DockingView'Create ButtonsLocal button:=New PushButton( "OK" )Local buttonUP:=New PushButton( "",Image.Load("asset::pad_up.png" ))Local pb:=New PushButton("info")'Create Check ButtonLocal check:=New CheckButton()'Create LabelLocal label:=New Label("Enter Text")'Create Text FieldLocal name:=New TextField("",20)'add the above objects to dv docking viewdv.AddView(button,"left")dv.AddView(buttonUP,"left")dv.AddView(check,"left")dv.AddView(label,"left")dv.AddView(name,"left")dv.AddView(pb,"left")'add the dv to the main viewmainview.AddView(dv,"top")' add the mainview to the WindowContentView=mainview'check or button clickedbutton.Clicked=Lambda()Alert( "Well done sir!" )EndEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()EndMethod OnMeasure:Vec2i() OverrideReturn virtualResolutionEndEndFunction Main()New AppInstanceNew MyApp( "Examples",640,480 )App.Run()EndAttachments:
January 9, 2017 at 3:16 pm #6340I’m just starting to learn mojox too. According to the documents each gadget/widget like buttons has different fields that invoke a method or lambda. Here’s some examples for the code you gave, and my preferred way which would be to extend the element and keep the events separate in their own class (see MyButton below). This is similar to how wxWidgets works. But I’m a newb so maybe someone else can chime in as well
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114#Import "gui/"#Import "<std>"#Import "<mojo>"#Import "<mojox>"Using std..Using mojo..Using mojox..Global virtualResolution:=New Vec2i( 640,480 )Class MyApp Extends WindowField name:TextFieldField check:CheckButtonMethod New( title:String,width:Int,height:Int,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )Layout="letterbox"'Create Docking ViewsLocal mainview:=New DockingViewLocal dv:=New DockingView'Create ButtonsLocal button:=New MyButton( "OK" )Local buttonUP:=New PushButton( "",Image.Load("asset::pad_up.png" ))Local pb:=New PushButton("info")'Create Check Buttoncheck = New CheckButton()'Create LabelLocal label:=New Label("Enter Text")'Create Text Fieldname = New TextField("",20)'add the above objects to dv docking viewdv.AddView(button,"left")dv.AddView(buttonUP,"left")dv.AddView(check,"left")dv.AddView(label,"left")dv.AddView(name,"left")dv.AddView(pb,"left")'add the dv to the main viewmainview.AddView(dv,"top")' add the mainview to the WindowContentView=mainview'Point to methods in this class for each event.buttonUP.Clicked = OnButtonUpname.TextChanged = OnTextModifiedname.Entered = OnTextEntercheck.Clicked = OnCheckClickedEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()EndMethod OnMeasure:Vec2i() OverrideReturn virtualResolutionEndMethod OnButtonUp:Void()Print "Button Up Clicked"EndMethod OnTextModified:Void()Print name.TextEndMethod OnTextEnter:Void()Print "Enter Hit: " + name.TextEndMethod OnCheckClicked:Void()If check.CheckedPrint "Checked"ElsePrint "Not Checked"EndEndEnd'You can extend buttons and other things to package events into their own classesClass MyButton Extends ButtonField ICouldBeAnything:StringMethod New( text:String="",icon:Image=Null )Super.New( text,icon )ICouldBeAnything = "Hello!"Clicked = OnClickedEndMethod OnClicked:Void()Print "Clicked! " + ICouldBeAnythingEndEndFunction Main()New AppInstanceNew MyApp( "Examples",640,480 )App.Run()EndJanuary 9, 2017 at 4:06 pm #6343Hello peterigz,
Thank you for your response, where did you find this information?
After creating a button with image or text when I try to get the Width and Height it returns 0
The button Width and Height are read only.
Monkey1234567891011pb=New PushButton("info")Print " pb W:"+pb.WidthPrint " pb H:"+pb.HeightbuttonUP=New Button( "",Image.Load("asset::pad_up.png" ))Print " buttonUP W:"+buttonUP.WidthPrint " buttonUP H:"+buttonUP.HeightJanuary 9, 2017 at 7:21 pm #6349I’m just using the docs here and trying to figure things out by looking at the source code for Ted2. I think I’ll write a tutorial for mojox once I’ve figured most things out, I like the look of mojox so far I think it’ll be nice to work with.
Regarding the sizes, try using MeasuredSize which is a property that returns a Vec2.
Monkey1234567891011pb=New PushButton("info")Print " pb W:"+pb.MeasuredSize.xPrint " pb H:"+pb.MeasuredSize.ybuttonUP=New Button( "",Image.Load("asset::pad_up.png" ))Print " buttonUP W:"+buttonUP.MeasuredSize.xPrint " buttonUP H:"+buttonUP.MeasuredSize.yJanuary 9, 2017 at 11:27 pm #6355peterigz,
I tried <span class=”crayon-sy”>.</span><span class=”crayon-v”>MeasuredSize</span><span class=”crayon-sy”>.</span><span class=”crayon-i”>x and <span class=”crayon-sy”>.</span><span class=”crayon-v”>MeasuredSize</span><span class=”crayon-sy”>.y, they both returned 0.</span></span>
I use in Monkey 1 for about 4 years, and I love what they did to Monkey 2. I also used Pyro from Planiax http://www.playniax.com/pyro.html
They did a great job writing Pyro 2 for Monkey 2
January 10, 2017 at 12:36 am #6359Ahh, possibly you’re trying to get the size before mojox has actually figured out what size they should be? Try a bit later on in the process after View.OnMeasure has been called (I’m not sure when that is though exactly or if you can hook into it somehow)
January 10, 2017 at 1:42 am #6362Have a look in the modules/mojox/tests directory for some fairly useful how-to mojox examples.
-
AuthorPosts
You must be logged in to reply to this topic.
