2013/03/28

To Customize Interactions in TWaver

Recently, there comes a fairly demanding requirement that a node dragged from the toolbar will be inserted in the link under the mouse, during which that the the section of the link to be inserted by the node will protrude over its original height.
Nodes can be inserted in to a link through the deletion of the original one and the addition of two new ones, but the protruding part is troublesome, which, however can be realized with the help of ShapeLink in TWaverFlex.
Method:
  1. Listen to the event "DragEvent.DRAG_OVER" in network and judge whether there is Link under the mouse by the method "network#getElementsByDisplayObject". If there is, set this link as transparent(link.setStyle(Styles.LINK_ALPHA, 0) and then add a ShapeLink; if there is not, set it as transparent, restore it and delete ShapeLink. Please refer to the method "updateMark" or "removeMark" for detailed codes:
    private function updateMark(link:Link, data:Object, point:Point):void {
        if(!network.elementBox.contains(markLink)){
            markLink.fromNode = link.fromNode;
            markLink.toNode = link.toNode;
            markLink.setStyle(Styles.SHAPELINK_TYPE, Consts.SHAPELINK_TYPE_QUADTO);
            markLink.setStyle(Styles.LINK_WIDTH, link.getStyle(Styles.LINK_WIDTH));
            markLink.setStyle(Styles.LINK_COLOR, link.getStyle(Styles.LINK_COLOR));
            markLink.setStyle(Styles.LINK_ALPHA, link.getStyle(Styles.LINK_ALPHA));
           
            network.elementBox.add(markLink);
            lastLink = link;
            link.setStyle(Styles.LINK_ALPHA, 0);
        }
       
        var points:Collection = new Collection();
       
        var fromCenter:Point = markLink.fromNode.centerLocation;
        var toCenter:Point = markLink.toNode.centerLocation;
        var radius:Number = Math.sqrt(data.width*data.width + data.height*data.height)/2;
        var crossPoints:Object = getCrossPoints(fromCenter, toCenter, point, radius*2);
        if (crossPoints){
            if(fromCenter.x<toCenter.x){
                points.addItem(crossPoints.p2);
                points.addItem(crossPoints.p2);
            }else{
                points.addItem(crossPoints.p1);
                points.addItem(crossPoints.p1);
            }
           
            var crossPoint:Point = new Point(crossPoints.p1.x + (crossPoints.p2.x-crossPoints.p1.x)/2,
                crossPoints.p1.y + (crossPoints.p2.y-crossPoints.p1.y)/2);
            var controlPoints:Object = getCrossPoints(crossPoint, point, point, radius*3);
            if(controlPoints){
                if(crossPoint.y >= point.y){
                    points.addItem(controlPoints.p2);
                }else{
                    points.addItem(controlPoints.p1);
                }
            }else{
                if(point.y>=fromCenter.y){
                    point.y = point.y - radius * 3;
                }else{
                    point.y = point.y + radius * 3;
                }
                points.addItem(point);
            }
            if(fromCenter.x<toCenter.x){
                points.addItem(crossPoints.p1);
                points.addItem(crossPoints.p1);
            }else{
                points.addItem(crossPoints.p2);
                points.addItem(crossPoints.p2);
            }
        }else{
            points.addItem(fromCenter);
        }
        markLink.points = points;
    }

    private function removeMark(moving:Boolean):void {
        if(network.elementBox.contains(markLink)){
            var fromNode:Node = markLink.fromNode;
            var toNode:Node = markLink.toNode;
            network.elementBox.remove(markLink);
            if(lastLink && moving){
                lastLink.setStyle(Styles.LINK_ALPHA, markLink.getStyle(Styles.LINK_ALPHA));
                lastLink = null;
            }
        }
    }

  2. The key is how to make sure which points among ShapeLink will protrude. The solution is to calculate the intersection of the semi-circle and line. Please refer to the method "getCrossPoints" for detailed codes: 
    private function getCrossPoints(p1:Point, p2:Point, circleCenter:Point, radius:Number):Object{
        var line_p1_x:Number = p1.x;
        var line_p1_y:Number = p1.y;
        var line_p2_x:Number = p2.x;
        var line_p2_y:Number = p2.y;
        var circle_center_x:Number = circleCenter.x;
        var circle_center_y:Number = circleCenter.y;
        var circle_radius:Number = radius;
        var line_k:Number =(line_p2_y-line_p1_y)/(line_p2_x-line_p1_x);
        var line_b:Number =line_p1_y-line_k*line_p1_x;
        var a:Number =1+line_k*line_k;
        var b:Number =-2*circle_center_x+2*line_k*line_b-2*line_k*circle_center_y;
        var c:Number =circle_center_x*circle_center_x+(line_b-circle_center_y)*(line_b-circle_center_y)-circle_radius*circle_radius; 
        var delta:Number =b*b-4*a*c;
        var x1:Number =(-b+Math.sqrt(delta))/(2*a);
        var x2:Number =(-b-Math.sqrt(delta))/(2*a);
        var y1:Number =line_k*x1+line_b;
        var y2:Number =line_k*x2+line_b;
       
        if(delta>=0){
            return {p1: new Point(x1, y1), p2: new Point(x2, y2)};
        }else{
            return null;
        }
    }
  3. Delete ShapeLink and establish two Links in the event "DragEvent.DRAG_DROP".

2013/03/10

An Overview of TWaver Android

The characteristics of Android
Accounting for a large part in smartphone market, Android of Google is an open source operating system which is based on Linux kernel, mainly used on mobiles devices. The application layer of Android is developed by Java. It runs in Dalvik virtual machine. Every Android application functions in separate virtual machine, which ensures the protection and the thread safety of the resources between applications.

Android framework
Programming languages in Android: Java, NDK, HTML

Other programming languages can also be used to develop Android application apart from the usually used Java in Android application.

The three languages above have their own features. Java is the native-support language of Android, so it can be used in local development. As the improvement of Android system, it becomes more and more efficient; the development of Android NDK is comparatively complicated, but with the advantage in performance of C programming language, the efficiency of the program can be improved(As a cross-platform programming language, C programming language is widely used in games); now HTML has become a tendency and the applications on cross-browsers and cross-platforms can also be available through Web App.

There are quite some devices supporting Android and it is frequently updated. Progress has been made to the improvement on Dalvik virtual machine and Android application framework by Google. For example, the JIT system has been introduced in Android 2.2, so the efficiency has been increased twice to four times. At the same time, Android 4.1 has also made its contribution to UI fluency. The efforts Google made deserve praise, however, at the other hand, that also tells the inefficiency in the earlier versions. Then how much room of improvement remains? Let's see the data. We will test Android Java and SunJava on Android tablet and PC respectively to see their difference in performance and the suitable scenario to them.

The difference between Dalvik VM and SunJavaVM in perfomance

Devices: MacBook Pro(2.26 GHz Intel Core 2 Duo),Google Nexus 7(1.3GHz quad-core Tegra 3)
Software environment: Java 1.6.0_37 and Android 4.2.1
Contents: basic APIs including arithmetics, set operation(List and Map operation)

Here only the results are listed as the test codes in Java 1.6.0_37 are also java programming language. Please refer to PerformanceDemo.java in TWaver Android Demo:

Android 4.2.1 takes 12-50 times as Java 6 does in running a same program. In addition, the efficiency with other programming language on PC (like Flex or JavaScript) is also better than Nexus 7 tablet.

What about HTML? HTML on desktop is four times quicker than on mobile Chrome server, which mainly reflect the reality of the hardware; Android with JavaScript is superior to with original Java, at least in Chrome browser. However, this test only reflects the basic performance of programming languages. As for the convenience of development, Java is still the first choice.

The test indicates that compared to PC android devices is obviously deficient while Dalvik VM is not able to fully exert the performance of hardware, which should be noticed in developing applications in order to balance user experience and functions.

The difference in performance under platforms with programming languages:

The Target Platform of TWaver Android

There are many developing languages can be chosen in TWaver graphical components while there is still a vacancy on mobile platform. Although TWaver HTML5 is capable of running on Android and iOS, it is quite difficult to have the same user experience as in local application. In order to make that come true, TWaver Android is set to run on mobile platforms for graphical presentation of data with brand-new design architecture and higher UI efficiency to compensate for the deficiency in the performance of the mobile device itself. What's more, a smooth user experience with new multi-touch and roaming operation.

The Support of large amounts of data in TWaver Android

Compared to other edition of TWaver, some changes have been made in TWaver Android framework. The efficiency of DataBox has been improved several times and the problems existing in Link and Group have also been solved; the delay-invalid system has also been advanced in UI presentation; touch operations have been completely compatible; the scenarios in which Node, Link and Group have been used together are available. Even operations with more than one thousand data are still smooth on Nexus 7 tablet. The result is quite agreeable as it can be widely applied considering the problem that lies in the difference between PC and tablet and the efficiency of DM.

The contrast between the loading time of network with different amounts of elements.

Suggested Platforms of TWaver Android

A better view of graphical presentation of data can be enjoyed with a big screen, so tablets are suggested. Versions higher than Android 3.0 (Android 3.0 in which Drag and Drop is available is also included) are supported. Actually TWaver Android is tested and adjusted mainly with the help of Nexus 7/10 and Samsung Android, which, however, does not mean that it is incapable of functioning in mobile phones or low-version Android devices. Instead, TWaver Android Demo is able to run smoothly on Google Nexus S mobile phones. The simplified version of TWaver Android can be applied if you would like your device with Android 2.* to be compatible with it.

Run on Nexus 7 and Nexus S