Switch On The Code RSS Button - Click to Subscribe
Apr
14

Flex - Rotating Cube ViewStack Component

flex icon

Today I am going to show and give out a cool component I have built. The component is built to make it easy to use Alex Uhlmann's Distortion Effects in a ViewStack based component to rotate to the sides. I will show an example application and how to use the component, CubeEffect. Hopefully someone out there will find a good use for the component or at least some use of the source provided here.

The example below is nice little application to check out the feeds of a couple of popular sites out there. You can select one of the feeds and the cube will rotate to the appropriate side. If you want, you can click through to the entire article by clicking the title of the post. Also on the sides are buttons to go to the top which is a graphic and one button to go to the bottom which has some relevant info about the example. Ok go ahead and take a couple minutes to play around. Source Code



Well your probably now wondering how does this thing work. The easy answer is by pure magic. The long answer involves the Sandy 3D Engine, Alex Uhlmann's Distortion Effects, and a comment from Alex's Post where someone had created a CubeStack. This gentlemen had the basic idea down but I wanted to expand on his implementation and perhaps increase the usability, you can grab his version from www.charlieszymanski.com. So this led in the right direction.

The component extends the basic functionality of the ViewStack component provided in Flex. I set out to make it so the end user would only have to tell the new component, CubeStack, which side to display and it would take care of the rest. So after some small math magic the component would figure out which side to display and start the effects to get there. I am not going to go over all the boring code behind deciding which side to go to, you can check out the source if you would like to. I will say it may not be completely optimized but there would be minimal performance increase for refactoring the code. All you have to do is set selectedSide on the component.

Now to make things a bit easier I will give a quick overview of how to use the component and what's available. To use the component you simply need to create it in mxml or actionscript and add sides to it, I would suggest 4 or 6 sides. The sides do need to be in a certain order, which is Front, Left, Back, Right, Top, and Bottom. So a simple but not very useful example would be:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml"
  xmlns:components="com.pfp.components.*"
  layout="vertical">

  <components:CubeStack selectedSide="Front">
    <mx:Canvas name="Front" />
    <mx:Canvas name="Left" />
    <mx:Canvas name="Back" />
    <mx:Canvas name="Right" />
    <mx:Canvas name="Top" />
    <mx:Canvas name="Bottom" />
  </components:CubeStack>
</mx:Application>

Now of course in order for this to be really useful, you would probably want something more than empty canvases. But that gives you the basic idea. There are a couple other items I should mention about using the component. If you decide to use the selectedIndex property everything should still work as planned. There are also a couple events and properties to be aware of outlined below.

Properties
  • selectedSide : String - the selected side, changing this will start the transistion to new side, names are pulled from CubeStack.Sides
  • flipDuration : Number - the length in time the rotate effect takes
Events
  • rotateStart : CubeRotateEvent - event fired when the cube begins to rotate to a new side
  • rotateEnd : CubeRotateEvent - event fired when the cube rotation ends
  • sideChange : CubeRotateEvent - event fired when side is told to be changed
Constants
  • Sides : Array - array with the side values used, they are in order that the sides need to be placed in the container

Well I think that is all I am going to say about the component. I encourage you to look at the source code and modify it or simply let me know what you all think about it. I do plan on actually putting in real documentation for it someday. As always feel free to drop a comment about anything, within limits of course. Also note that the component still probably has a couple bugs in it, especially if you use less than 6 sides. Well I hope someone finds a use for it. If you just want the CubeStack Library .swc click on through.

References



Posted in Flex, All Tutorials by The Fattest |

10 Responses

  1. Alan Says:

    Slick, thanks for the explanation

  2. Marcel Humes Says:

    I’ve been working with tutorial today. I created a ‘Back’ panel as opposed to using the ‘Right’ one for the back as you did in your tutorial.

    The app does not navigate to the ‘Back’. It continues to use the ‘Right’ for the back.

    Any ideas? ITMT (in the mean time) I’ll look a little deeper.

    Marcel

  3. Marcel Humes Says:

    Oh, I got it. Used the code above. It’s called ‘getting ahead of yourself.’. Awesome. Great, great tutorial.

    Marcel

  4. gwoo Says:

    Thanks for tutorial and component. I just added it to Ohlair (http://ohloh.net/projects/ohlair) I am hoping to integrate it a little better but for now its a great effect that was ridiculously simple to add.

  5. Luisk Says:

    someone can help me, please i have some problem adding the efect, someone can explain me how? where i find the library ? thanx

  6. wpageiii Says:

    The libs folder should be added into the project above the src level…then unzip the contents of the CubeStack Library .swc zip file into the folder

  7. Marco Frigerio Says:

    Hello I’m trying to use this control, but I can not find the .swc? am I doing something worng? how can I add this library to my project?

    Thanks
    Marco

  8. The Fattest Says:

    Just download the .swc (link in last paragraph) and if you’re using Flex Builder 3 simply add it to the libs folder of the project. Let me know if you are not using Flex Builder 3.

  9. Marco F Says:

    Hi The Fattest, yes I’m using Flex 3, thanks for your help, I create a Libs folder later uncompress your zip inside [libs] folder, copy your example to a brand new mxml and nothing happend, could you send me your example mxml?

  10. Laurus Says:

    Hmm cant find the .swc file either. Downloaded the file, but it contains a folder named CubeStack.swc not an actual swc package file.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash