This shows you the differences between two versions of the page.
sample_code:img_toggle_button [2014/11/26 09:29] stevegarman created |
sample_code:img_toggle_button [2015/01/13 17:55] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Image button toggle ====== | ||
- | This code was written in response to a request for a Toggle Image Button. | ||
- | |||
- | The main body of the code is the function | ||
- | CreateImageToggle(img1, | ||
- | The images can be passed either as file-paths or as images | ||
- | The sample code passes one of each. | ||
- | ===== The code ===== | ||
- | <code javascript toggle.js> | ||
- | // | ||
- | function OnStart(){ | ||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | // | ||
- | var pauseImg = app.CreateImage( "/ | ||
- | |||
- | // | ||
- | btn = CreateImageToggle(pauseImg, | ||
- | btn.SetOnTouchDown(btnToggle); | ||
- | |||
- | lay.AddChild( btn ); | ||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | function btnToggle(ev){ | ||
- | this.Toggle = ! this.Toggle; | ||
- | if(this.Toggle){ | ||
- | this.SetImage(this.imgTrue); | ||
- | app.ShowPopup(" | ||
- | } else{ | ||
- | this.SetImage(this.imgFalse); | ||
- | app.ShowPopup(" | ||
- | } | ||
- | } | ||
- | |||
- | function CreateImageToggle(img1, | ||
- | // | ||
- | // | ||
- | if (typeof img1 == " | ||
- | img1 = app.CreateImage(img1); | ||
- | } | ||
- | if (typeof img2 == " | ||
- | img2 = img1; | ||
- | } else if (typeof img2 == " | ||
- | img2 = app.CreateImage(img2, | ||
- | } | ||
- | var obj = app.CreateImage(null, | ||
- | obj.SetImage(img1) | ||
- | obj.Toggle = false; | ||
- | obj.imgFalse = img1 | ||
- | obj.imgTrue = img2 | ||
- | return obj | ||
- | } | ||
- | |||
- | </ | ||
- | ===== Note ===== | ||
- | This code has had only rudimentary testing. You may well be able to improve it | ||