B4J: ABMaterial building a QR Code component

qrcodecomp

On the B4J forum, liulifeng77 asked the question how he could draw a QR Code in an ABMaterial web page.

I had 5 minutes to spare, so I created the QRCode component using ABMCustomComponent. It is using this javascript library: https://github.com/davidshimjs/qrcodejs

The component class CompQRCode:

'Class module
Sub Class_Globals
     Public ABMComp As ABMCustomComponent
     Public myText As String
   Public myWidth As Int
   Public myHeight As Int
   Public myColorDark As String
   Public myColorLight As String
   Public myCorrectLevel As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(InternalPage As ABMPage, ID As String, text As String, width As Int, height As Int, colorDark As String, colorLight As String, correctLevel As String)
     ABMComp.Initialize("ABMComp", Me, InternalPage, ID)
     myText = text
   myWidth = width
   myHeight = height
   myColorDark = colorDark
   myColorLight = colorLight
   myCorrectLevel = correctLevel
End Sub
Sub ABMComp_Build(internalID As String) As String
  Return $"
<div id="${internalID}svg"/></div>
<script>var qrcode${internalID};</script>"$
End Sub
' Is useful to run some initalisation script.
Sub ABMComp_FirstRun(InternalPage As ABMPage, internalID As String)
   Dim script As String = $"qrcode${internalID} = new QRCode(document.getElementById("${internalID}svg"), {
  text: "${myText}",
  width: ${myWidth},
  height: ${myHeight},
  colorDark : "${myColorDark}",
  colorLight : "${myColorLight}",
  correctLevel : ${myCorrectLevel}
});"$
     InternalPage.ws.Eval(script, Array As Object(ABMComp.ID))
     ' flush not needed, it's done in the refresh method in the lib
End Sub
' runs when a refresh is called
Sub ABMComp_Refresh(InternalPage As ABMPage, internalID As String)
   Dim script As String = $"qrcode${internalID}.clear();
   qrcode${internalID}.makeCode("${myText}");"$
     InternalPage.ws.Eval(script, Array As Object(ABMComp.ID))
End Sub
' do the stuff needed when the object is removed
Sub ABMComp_CleanUp(InternalPage As ABMPage, internalID As String)
End Sub

Usage (if you want to change its text at run time, put the declaration in Class_globals):

Dim QRCode As CompQRCode

In BuildPage() load the javascript library:

page.AddExtraJavaScriptFile("custom/qrcode.min.js")

In ConnectPage():

QRCode.Initialize(page, "qrcode", "Alwaysbusy's ABMaterial", 128,128, "#000000", "transparent", "QRCode.CorrectLevel.H")
page.Cell(3,1).AddComponent(QRCode.ABMComp)

Et voila, another new component!

Alwaysbusy

Click here to Donation and support ABMaterial


B4J: ABMaterial Public 1.22/Donators 2.00 now released

It has been a long night to get everything ready, but I’m happy to release the new version(s) of ABMaterial for B4J!

2.00 is a major release.  Lots of new stuff and some changes that were really needed in preparation of the upcoming (ABMaterial Abstract Desinger) ABMAD tool.

Highlights of the releases:

1.22: New component ABMPatternLock

ABMPatternLock is yet another authentication method you can use based on the popular Pattern Lock you see in e.g. Android.

patternlock

1.22: ABMContainer can collapse

ABMContainer can be initialized as a collapsable component.  As a collapsable has a header and a body and clicking on the header will collapse/expand the body.  This can be used as an alternative for an ABMCard so you can build your own.

1.22: Support for Font-Awesome icons

If you set: page.UseFontAwesome = true then you can, next to the material icons, also use the Font Awesome Icon library (634 icons in used version Font Awesome 4.6.3)
See fontawesome.io for a list of icons.

fontawesome

2.00: New component ABMChronologylist

The ABMChronologyList is a vertical timeline component. Useful to give an overview of a limited period. It is device aware so e.g. on a phone, all items will be one under each other.

chronology

2.00: New helper ABMSideBar

In ABMaterial 2.00, you will be able to define multiple side bars (sliding in from the right). I’ve tried to mimic the usage of ABMSideBar as much of the existing Side Navigation menu. The tricky part was making it work well with the NavigationBar, on all screensizes.

Check out the online demo abmaterial.com or download your free copy from the B4J website.  Make sure you read the included README.TXT files.  It contains some important info and shows some tips and tricks.

That’s it for now.  Next big release will be all about ABMAD. Until next time!

Alwaysbusy

Click here to Donation if you like my work


B4J: Future steps for ABMaterial

evolution

As ABMaterial has grown to a production ready framework for WebApps using B4J, I recently started a poll on the B4X forum to find out what could be the next steps in its evolution.

I came up with 3 things to do. They would require about the same develop time, but it could be useful to see what the actual users of ABMaterial would like me to work on next.

The possible choices were:

1. ABMXPlay
This is the component you have seen some demos of and will be more for game developers. It still needs a lot of attention before it can be released.

2. B4JS
The Basic to Javascript transpiler is now in its very early stages. It is a tough one to write without finishing ABMXplay first, as the ABMXPlay component would be the first to really benefit from it. The B4J core functions have been covered, but big parts of all ABMComponents need to be refactored to start using B4JS.

3. ABMaterialAbstractDesigner
A full blown Abstract Visual Designer like the one in B4J. This is NOT a code generator but the real deal. Code generators may look nice at first sight, but  have a huge disadvantage: they go one way. ABMAbstractDesigner would work like the B4X ones: you create a layout, develop, change the layout, continue to develop and so on without losing what you have written in code. I’ll use the grid designer I’ve shown as a proof of concept in another post + the possibility to put ABMComponents on them. I’ve done some tests this weekend and it would work very similar as the other B4X tools.

4. Other (please specify)

The poll is now nearly ended and besides some interesting messages with excellent pointers, it was quite obvious from the beginning that the ABMaterialAbstractDesigner (will need a working title for this project, lets call it ABMAD for now…) would be the next part I’ll be working on. (Almost 75% in the poll at the time of writing).  This was also my preferred choice as one user did not really have the patience to wait for me and started to write some kind of code generator.  However, his limited knowledge of the ABMaterial framework has the potential of starting to reflect bad on ABMaterial itself so it is time for me to jump in.

Making ABMAD will happen in a couple of logical steps:

  1. Making the ABMaterial components ready for a designer.  This will ask for some (minor) changes to them so they can be serialized and expose their properties to the designer.
  2. Making a grid builder.  This is the biggest step new users have to overcome nowadays.
  3. Making a properties view for the components.
  4. Drag/Drop facilities to add components to specific cells
  5. Let the ABMaterial library communicate with the Designer output in a similar way as B4X does now.

This will not happen overnight, but the benefits for ABMaterial programmers will be substantial.  This will NOT be a simple code generator. ABMAD will work bidirectional.  And you’ll be able to make layouts in ABMAD and mix them with manually written code like you can do in B4X/ABMaterial now.

Note: To avoid future misunderstandings, the license of ABMaterial 2.0 will include the following statement:

4. YOU AGREE NOT TO DISTRIBUTE, FOR A FEE, AN APPLICATION USING THE LIBRARY THAT, AS ITS PRIMARY PURPOSE, IS DESIGNED TO BE AN AID IN THE DEVELOPMENT OF SOFTWARE FOR YOUR APPLICATION’S END USER. SUCH APPLICATION INCLUDES, BUT IS NOT LIMITED TO, A DEVELOPMENT IDE OR A B4J SOURCE CODE GENERATOR.

Let me explain. The philosophy of ABMaterial (and myself) is giving anyone the chance to easily build great WebApps without having to spend any money. One of the reasons I picked B4J was because it is free. Those who can spare something, donate to stimulate my continuation on the project and in so help giving other not so fortunate people an equal chance to grow. Any derived development tool or generator that uses ABMaterial must follow this philosophy so the tool must be available with 100% of its features for free (not even a ‘limited’ free version is acceptable).

This has no impact on whatever WebApp you make with ABMaterial, but is solely a protection against some ‘money-grubber’ who sees the potential of the ABMaterial framework and thinks of making some easy money using my engine by simply writing a layer above it. Just to be clear, ‘FEE’ here means ANY form of payment or compensation, donations included.

So now that this is out of the way, I think it’s time to step up to the drawing board and start to make some decisions on how ABMAD will look like.

Fun times ahead!

Alwaysbusy

Click here to Donation and support ABMaterial

 

 

 

 

 

 

 


B4J: Creating your own components in ABMaterial

abmcustgauge

Although ABMaterial has already a lot of components build-in, sometimes you just need a specific one.  I had a question on the B4J forum from CGP who needed a Gauge component.

So let’s build one! CGP had found a very nice library JustGage that would allow him to get the Gauge he wanted. Its value had to be updateable from within B4J, and this was a bit tricky. It looked like we have to be a bit creative because this javascript library did not connect its parent tag with the created JustGage.

We will have to keep track of the JustGage variable ourselves by adding a script tag in the build:

Sub ABMComp_Build(internalID As String) As String
  Return $"<div id="${internalID}"></div><script>var _${internalID};</script>"$
End Sub

The rest of the code is straight forward and we can just follow the javascript instructions and add them in our relative events.

An ABMaterial custom component has 4 important events:

ABMComp_Build(): This is where we have to add the html tag (anchor) for the new component.  In this particular case we also have to add a <script> tag that will hold our justGage variable.  We can then later use this variable to update the Gauge chart.

ABMComp_FirstRun(): Here we can add a script that will run, well, at first run. In most cases, this is where we add the libraries javascript initialization code. The B4J $””$ Smartstrings are in methods like this particulary handy. Use them!

ABMComp_Refresh(): The place where we can adjust properties of the javascript component.  Like for this component, we’ll adjust the value of the gauge.

ABMComp_CleanUp(): An event that is raised when the component is removed.  Could be useful to do some cleanup in the html of set javascript variables to null.

Here is the complete B4J code for our customGauge component:

Sub Class_Globals
   Public ABMComp As ABMCustomComponent
  Public myValue As Int
  Public myLabel As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(InternalPage As ABMPage, ID As String, value As Int, name As String)
  ABMComp.Initialize("ABMComp", Me, InternalPage, ID)
  myValue = value
  myLabel = name
End Sub

Sub ABMComp_Build(internalID As String) As String
  Return $"<div id="${internalID}"></div><script>var _${internalID};</script>"$
End Sub

' Is useful to run some initalisation script.
Sub ABMComp_FirstRun(InternalPage As ABMPage, internalID As String)
  Dim script As String = $"_${internalID} = new JustGage({
  id: "${internalID}",
  value: ${myValue},
  Min: 0,
  Max: 100,
  relativeGaugeSize: true,
  title: "${myLabel}"
  });"$

  InternalPage.ws.Eval(script, Array As Object(ABMComp.ID))
  ' flush not needed, it's done in the refresh method in the lib
End Sub

' runs when a refresh is called
Sub ABMComp_Refresh(InternalPage As ABMPage, internalID As String)
  Dim script As String = $"_${internalID}.refresh(${myValue});"$
  InternalPage.ws.Eval(script, Null)
End Sub

' do the stuff needed when the object is removed
Sub ABMComp_CleanUp(InternalPage As ABMPage, internalID As String)

End Sub

Now, how do we use our new component in our B4J ABMaterial WebApp?

First, we’ll need to make our custGauge a global variable on the page:

Sub Class_Globals
  Dim custGauge As CustomGauge
  ...

Next, we add the justGauge libraries in the BuildPage().  We’ve placed them in \www\js\custom folder so we can add:

Sub BuildPage()
  ...
  page.AddExtraJavaScriptFile("custom/justgage.js")
  page.AddExtraJavaScriptFile("custom/raphael-2.1.4.min.js")
  ...

In the ConnectPage, we’ll create the actual Gauge and we add a button so we can change its value:

Sub ConnectPage()
  ...
  custGauge.Initialize(page, "custGauge", 25, "Test")
  page.Cell(7,1).AddComponent(custGauge.ABMComp)

  Dim custGaugebtn1 As ABMButton
  custGaugebtn1.InitializeRaised(page, "custGaugebtn1", "", "", "BUTTON", "")
   page.Cell(7,1).AddComponent(custGaugebtn1)
   ...

And finally we add out button code to adjust the value of the Gauge:

Sub custGaugebtn1_Clicked(Target As String)
   custGauge.myValue = Rnd(10,100)
   custGauge.ABMComp.Refresh
End Sub

And just like that, it looks like a fun new component we’ve got in B4J ABMaterial!

Until next time,

Alwaysbusy

Click here to Donation and support ABMaterial

 


B4J: ABMXPlay first online demo

abmxplay-demo

This is the first public demo of the new ABMaterial 2.0 component ABMXPlay.  It contains several basic demos of what this new component can do. Use the ‘hamburger’ menu on the top left to see another demo.

Although the current version of the demo still uses pure B4J server code (e.g. when you click, it does a round-trip to the server), I’m quite happy with the responiveness.  But as I mentioned in a previous post, even this round-trip will be eliminated when it starts using the Basic to JavaScript transpiler B4JS.

I’m making good progress on B4JS.  At the time of this writing, I’m able to transpile a lot of the B4J core functions. See here for an overview.

An example of some B4J core function tests and their translations to Javascript.

B4J code:

Sub B4JSCoreFunctions
   Log("------- Start B4JSCoreFunctions")
   Log("(True And False): false <=> " & (True And False)) 
   Log("(True Or False): true <=> " & (True Or False)) 
   Log("Rnd(0,10): Number between 0 and 10 <=> " & Rnd(0,10))
   Log("Abs(-2): 2 <=> " & Abs(-2))
   Log("Not(False): true <=> " & Not(False))
   Log("CosD(45):  0.70710678... <=> " & CosD(45))
   Log("Cos(0.5):  0.87758256... <=> " & Cos(0.5))
   Log("SinD(45):  0.70710678... <=> " & SinD(45))
   Log("Sin(0.5):  0.47942553... <=> " & Sin(0.5))
   Log("ACosD(-1):  180... <=> " & ACosD(-1))
   Log("ACos(0.5):  1.04719755... <=> " & ACos(0.5))
   Log("ASinD(-1):  -90... <=> " & ASinD(-1))
   Log("ASin(0.5):  0.52359877... <=> " & ASin(0.5))
   Log("TanD(45):  0.99999999... <=> " & TanD(45))
   Log("Tan(0.5):  0.54630248... <=> " & Tan(0.5))
   Log("ATanD(45):  88.7269699... <=> " & ATanD(45))
   Log("ATan(0.5):  0.46364760... <=> " & ATan(0.5))
   Log("ATan2D(90,45): 3.4349488... <=> " & ATan2D(90,45))
   Log("ATan2(8,4):  1.10714871... <=> " & ATan2(8,4))

   Log("try/Catch example, only visible in B4JS.")
   Try
     Dim i As Int = 1/0 'ignore
   Catch
     Log(LastException.Message)
   End Try

   Log("Chr(65): A <=> " & Chr(65))
   Log("Asc(""A""): 65 <=> " & Asc("A"))
   Log("cE: 2.71828182... <=> " & cE)
   Log("Ceil(1.65): 2 <=> " & Ceil(1.65))
   Log("cPI: 3.14159265... <=> " & cPI)
   Log("Floor(1.65): 1 <=> " & Floor(1.65))
   Log("IsNumber(""alain""): false <=> " & IsNumber("alain"))
   Log("IsNumber(""15.2""): true <=> " & IsNumber("15.2"))
   Log("Logarithm(10, 1000): 0.33333333.... <=> " & Logarithm(10, 1000))
   Log("Max(10,5): 10 <=> " & Max(10,5))
   Log("Min(10,5): 5 <=> " & Min(10,5))
   Log("Power(2,3): 8 <=> " & Power(2,3))
   Log("Round(1.12345): 1 <=> " & Round(1.12345))
   Log("Round2(1.12345, 2): 1.12 <=> " & Round2(1.12345, 2))
   Log("Sqrt(9): 3 <=> " & Sqrt(9))

   Dim str1 As String = "Test €100 is £84.73"
   Dim theBytes() As Byte
   theBytes = str1.GetBytes("UTF8")
   Log("BytesToString(theBytes,0,theBytes.Length, ""UTF-8""): Test €100 is £84.73 <=> " & BytesToString(theBytes,0,theBytes.Length, "UTF-8"))
End Sub

The JavaScript transpiled code:

this.b4jscorefunctions=b4jscorefunctions=function(){
     try {
       console.log("------- Start B4JSCoreFunctions");
       console.log("(True And False): false <=> "+(true && false));
       console.log("(True Or False): true <=> "+(true || false));
       console.log("Rnd(0,10): Number between 0 and 10 <=> "+(Math.floor(Math.random() * (10 - 0) + 0)));
       console.log("Abs(-2): 2 <=> "+(Math.abs(-2)));
       console.log("Not(False): true <=> "+!(false));
       console.log("CosD(45):  0.70710678... <=> "+(Math.cos((45)/(180/Math.PI))));
       console.log("Cos(0.5):  0.87758256... <=> "+(Math.cos(0.5)));
       console.log("SinD(45):  0.70710678... <=> "+(Math.sin((45)/(180/Math.PI))));
       console.log("Sin(0.5):  0.47942553... <=> "+(Math.sin(0.5)));
       console.log("ACosD(-1):  180... <=> "+(Math.acos(-1)*(180/Math.PI)));
       console.log("ACos(0.5):  1.04719755... <=> "+(Math.acos(0.5)));
       console.log("ASinD(-1):  -90... <=> "+(Math.asin(-1)*(180/Math.PI)));
       console.log("ASin(0.5):  0.52359877... <=> "+(Math.asin(0.5)));
       console.log("TanD(45):  0.99999999... <=> "+(Math.tan((45)/(180/Math.PI))));
       console.log("Tan(0.5):  0.54630248... <=> "+(Math.tan(0.5)));
       console.log("ATanD(45):  88.7269699... <=> "+(Math.atan(45)*(180/Math.PI)));
       console.log("ATan(0.5):  0.46364760... <=> "+(Math.atan(0.5)));
       console.log("ATan2D(90,45): 3.4349488... <=> "+(Math.atan2(90,45)*(180/Math.PI)));
       console.log("ATan2(8,4):  1.10714871... <=> "+(Math.atan2(8,4)));
       console.log("try/Catch example, only visible in B4JS.");
       try {
         var _i=1/0;
       } catch(err) {
         console.log(lastexception.message);
       }
       console.log("Chr(65): A <=> "+String.fromCharCode(65));
       console.log("Asc(\"A\"): 65 <=> "+"A".charCodeAt(0));
       console.log("cE: 2.71828182... <=> "+2.7182818284590452353602874713527);
       console.log("Ceil(1.65): 2 <=> "+(Math.ceil(1.65)));
       console.log("cPI: 3.14159265... <=> "+Math.PI);
       console.log("Floor(1.65): 1 <=> "+(Math.floor(1.65)));
       console.log("IsNumber(\"alain\"): false <=> "+(!isNaN(parseFloat("alain")) && isFinite("alain")));
       console.log("IsNumber(\"15.2\"): true <=> "+(!isNaN(parseFloat("15.2")) && isFinite("15.2")));
       console.log("Logarithm(10, 1000): 0.33333333.... <=> "+(Math.log(10)/Math.log(1000)));
       console.log("Max(10,5): 10 <=> "+Math.max(10,5));
       console.log("Min(10,5): 5 <=> "+Math.min(10,5));
       console.log("Power(2,3): 8 <=> "+Math.pow(2,3));
       console.log("Round(1.12345): 1 <=> "+Math.round(1.12345));
       console.log("Round2(1.12345, 2): 1.12 <=> "+(+(Math.round(1.12345+"e+"+2)+"e-"+2)));
       console.log("Sqrt(9): 3 <=> "+(Math.sqrt(9)));
       var _str1="Test €100 is £84.73";
       var _thebytes=[];
       _thebytes.length=0;
       _thebytes = _str1.getBytes();
       console.log("BytesToString(theBytes,0,theBytes.Length, \"UTF-8\"): Test €100 is £84.73 <=> "+bytesToString(_thebytes.slice(0,_thebytes.length)));
     }
     catch(err) {
       console.log(err.message + ' ' + err.stack);
     }
   };

Or a more challenging transpiling of the B4J ‘String.GetBytes()’ command:

String.prototype.getBytes = function() {
     var utf8 = [];
     var str = this;
     for (var i=0; i < str.length; i++) {
       var charcode = str.charCodeAt(i);
       if (charcode < 0x80) utf8.push(charcode);
       else if (charcode < 0x800) {
         utf8.push(0xffffffc0 | (charcode >> 6), 0xffffff80 | (charcode & 0x3f));
       } else if (charcode < 0xd800 || charcode >= 0xe000) {
         utf8.push(0xffffffe0 | (charcode >> 12), 0xffffff80 | ((charcode>>6) & 0x3f), 0xffffff80 | (charcode & 0x3f));
       } else {
         utf8.push(0xef, 0xbf, 0xbd);
       }
     }
     return utf8;
   };

Now that I can write real B4J code and translate it to JavaScript, I can start introducing it into ABMaterial. Once I’ve done this for the ABMXplay component, I’ll put up a new demo (using B4JS) and then we can compare the results. My first tests look very promising!

WARNING: the demo contains audio fragments and the South Park characters can be rather offensive!

It runs on my personal server. Please be gentle: ABMXPlay demo

Happy programming!

Alwaysbusy


ABXPlay for Xojo is dead, long live ABMXPlay for B4J!

abmxplaylogo

As many will have noticed in the last year,  there are fewer articles about Xojo on Alwaysbusy’s Corner.  I do like to write graphics projects/frameworks and Xojo is more a language for business apps. Over the years, I’ve written several ‘Proof of Concept’ articles on graphics programming with RB/Xojo.  And although the algorithms could be written, I always quickly hit a wall when it came to speed. I sometimes get requests to update the existing articles, but IMHO, Xojo is becoming the new VB6.  Just like at the end of the popular VB6, using declares in Xojo are in many cases the only solution to make your program work in a modern way.

But it has been fun and I’m sure Xojo can be used to write several other type of projects. I have no regrets having learned RB/Xojo in the past.

But in this view, I have discontinued the ABXPlay for Xojo project and decided to start all over again with ABMXPlay for B4J as a ABMaterial 2.0 component. B4J is excellent when it comes to pure speed in WebApps and that is exactly what is needed for a game framework.

The complete rewrite of ABMXPlay for B4J is better than the Xojo version.  It is integrated in ABMaterial so it can make use of this engine too.  As the B4J syntax is a lot easier than the one in Xojo to interpret (it is very similar to what RB used to be) and the used file format is plain text, I’m working on a B4JS transpiler that will translate B4J code to Javascript.  This changes everything massively!  While still being able to write simple Basic syntax, you are actually writing powerful and fast client side Javascript.

ABMXPlay is going to be the first ABMaterial component with this functionality. In the following months, I’ll gradually will introduce this to other ABMaterial components as they can equally benefit from B4JS. Imagine having a form, being able to check the input fields, making some price calculations etc without having to talk to the server.  Only when the user hits the ‘buy’ button, an actual request is made to the server.  And for you as a programmer, all you have to do is continue programming in B4J as you always did!

So, still a lot of work to do. Coffee and Red bull will be my best friends in the upcoming nights. But it is going to be worth it!

Note: ABXVision for Xojo is also DISCONTINUED: Interested parties in purchasing the source code for the Xojo versions can contact me via email.

Until the next update,

Alwaysbusy

Click here to Donation and support ABMaterial


B4J: ABMaterial officially named…

Just a little announcement: ABMaterial for B4J finally got its own domain abmaterial.com! My brainchild is moving out…

57920667

Click here to Donation and support ABMaterial

 


%d bloggers like this: