Page 1 of 1

arrow key control

Posted: Sun Jun 17, 2012 9:56 am
by jameshale
Hi,

I am trying out rTree as a table of contents display for an ebook.
Things are working well, almost.
I have been unable to to get the "collapse all" script to work at all, and..
I tried to get the left and right arrow keys to collapse and expand the selected node.
I didn't want to interfere with the existing up and down arrow key navigation so I placed the following script into the control.

Code: Select all

on arrowKey theKey 
   put the hilitedNodeIDs of control "TOCNavigation" into theNodeID
   switch (theKey)
      case("left")
         set the expanded_of_node_ID_[theNodeID] of control "TOCNavigation" to false
         break
      case("right")
         set the expanded_of_node_ID_[theNodeID] of control "TOCNavigation" to true
         break
      case("up")
         pass arrowKey
         break
      case("down")
         pass arrowKey
         break
   end switch
end arrowKey
Now the "hilitedNodeIDs" function works a treat (tested by just putting it) and the "Switch" control does what is expected.
However setting the expanded_of_node_ID_[theNodeID] of the control does nothing.
I tried replacing "control "TOCNavigation"" with "me" but this made no difference (nor generated an error).

I also noticed that pressing the left or right arrow keys caused the node being highlighted to change.
For this to occur it implies the arrow keys are being acted on before they hit my handler.

Anyway, any suggestions?
Is there something I need do to the control itself to allow this?
Should I place the script someplace else?

James

Re: arrow key control

Posted: Mon Jun 18, 2012 11:19 am
by wilstrand
Hi James!

You are right in your assumptions! rTree uses a rawkeyDown handler internally to control arrow keys navigation and other key functions. You should be able to accomplish what you want by some small changes to your approach. Please try the following code:

Code: Select all

on rawkeyDown theKey
   local theNodeID
   put the hilitedNodeIDs of me into theNodeID
   switch theKey
      case 65361 # left
         set the expanded_of_node_ID_[theNodeID] of me to false
         break
      case 65363 # right
         set the expanded_of_node_ID_[theNodeID] of me to true
         break
      case 65362 # up
         pass rawkeyDown
         break
      case 65364 # down
         pass rawkeyDown
         break
   end switch
   dispatch "render_tree" to me
end rawkeyDown
If you use the rawKeyDown handler in your controls script you will get full control over the behaviors from key events. Also note that you need to dispatch a "render_tree" message to reflect the changes of expanded/collapsed nodes.

As for the "collapse all" script not working, could you please post the code you are using?

With my best regards
Mats

Re: arrow key control

Posted: Mon Jun 18, 2012 2:46 pm
by jameshale
Excellent!
Works like a charm.
I had initially coded for the rawkeyDown handler but then saw the Arrow key one and changed it.
I had forgotten to redraw the tree through.
This was the key to the Collapse All script too.
Once I despatched a redraw it worked perfectly.
I guess the example in the docs is despatching the redraw outside the displayed script. Might want to change that.
In any case, thank you very much for your prompt and helpful answer.
Now that is really can do what I want I had better hop over to the store and buy it :D

Re: arrow key control

Posted: Mon Jun 18, 2012 3:20 pm
by jameshale
Hi Mats,

I noticed my original code also left something to be desired if the highlighted node was without children.
I expected the parent node to close but alas it didn't.
So I modified the left arrow case to close the parent in such a case as well as move the highlight to the parent.
Thus..

Code: Select all

...
     case 65361 # left
         if  the children_of_node_ID_[theNodeID] of me >0 then
            set the expanded_of_node_ID_[theNodeID] of me to false
         else
            put the parent_of_node_ID_[theNodeID] of me into ptheNodeID
            set the expanded_of_node_ID_[ptheNodeID] of me to false
            set the hilitedNodeIDs of me to ptheNodeID
         end if
        break
...
Now it works just like it would in the Finder.

Thanks again.

James

Re: arrow key control

Posted: Tue Jun 19, 2012 3:27 am
by Jamesplank
Hi Mats
I have just purchased rTree and very happy :D
just wondering if a more "attractive" (ie native) scroll bar for ios will eventually find its way
into rTree
And any idea when V2 will hit the livecode store?
Regards James