Game Development Community

Animated Gui's from image maps

by Joao Caxaria · in Technical Issues · 10/13/2008 (12:23 pm) · 4 replies

Hi guys,

I've been reading the documentation and I can't seem to be able to find a GUI control that plays a animation created in torque script.

I was trying to create a button that would show a frame from a t2dImageMapDatablock and, on mouse events, would play a t2dAnimationDatablock created from that image map.

Can someone point me to some resource or just share some ideas?

Thanks!

#2
10/13/2008 (1:11 pm)
Hi Anthony,

I've actually seen that one but it says that it is for TGE. I was hoping for a similar solution for TGB Pro, if possible without having to change the c++ code :) I assumed that it would be possible as (I'm guessing here) this should be a somewhat fairly common problem/feature.

I guess (I am trying it right now) is to add a t2dStaticSprite on my level and, on mouse over, play a t2dAnimatedSprite but, as I would be trying to mimic the button functionality, this seems somewhat wrong.

Is there any recommendation or rule of thumb I should be aware?

Anyway, thanks for the reference!
#3
10/14/2008 (5:00 am)
If you want to use tgb objects such as animated sprites for some of your GUI elements, take a look at useObjectMouseEvents (see the reference doc) which will give you onMouseDown/Up callbacks on your object.
#4
10/14/2008 (6:06 am)
Yep, that's what I did. Just as a future reference, this is what I ended up with:

function NewGameMainMenu::onMouseEnter(%this, %modifier, %worldPos)
{	
	if(%this.state !$= "normal")
		return;
	echo("mouse enter");
	new t2dAnimatedSprite() {
		scenegraph = %this.scenegraph;
		class = "NewGameMainMenuAnimation";
		animationName = "NewGamePressedAnimation";
      canSaveDynamicFields = "1";
      Position = %this.Position;
      size = %this.size;
      menuItem = %this;
      nextState = "pressed";
   };
   %this.state = "pressing";
   %this.frame = 14;
}

function NewGameMainMenu::onMouseDown(%this, %modifier, %worldPos)
{	
	$Game::Controller.loadLevelFile("level1.t2d");
}

function NewGameMainMenuAnimation::onAnimationEnd(%this)
{
	echo("finished animation");
	%this.menuItem.state = %this.nextState;    
	%this.Visible = false;
	%this.safeDelete();
}

function NewGameMainMenu::onMouseLeave(%this, %modifier, %worldPos)
{
	if(%this.state $= "pressing")
	{
		%objects = %this.scenegraph.pickPoint(%worldPos);
		%count = getWordCount(%objects);
		for(%i = 0; %i < %count; %i++)
			if(getWord(%objects, %i) $= %this)
				return;
		%this.schedule(50, "onMouseLeave", %modifier, %worldPos);
		return;
	}
	if(%this.state !$= "pressed")
	{
		//%hoverObjects = %this.scenegraph.pickPoint(%worldPos);
		//%this.schedule(50, "onMouseLeave", %modifier, %worldPos);
		return;
	}
	echo("mouse leave");
	new t2dAnimatedSprite() {
		scenegraph = %this.scenegraph;
		class = "NewGameMainMenuAnimation";
		animationName = "NewGameReleasedAnimation";
      canSaveDynamicFields = "1";
      Position = %this.Position;
      size = %this.size;
      menuItem = %this;
      nextState = "normal";
   };
   %this.frame = 0;
}

What it does is run a animation called "NewGamePressedAnimation" on mouse over and, when the mouse leaves the menu item, it runs the "NewGameReleasedAnimation" animation.

I use one state variable with 3 possible values: "normal", "pressing" and "pressed" to control the animation flows (ex: if the mouse is removed before the animation ends, we still want to run the animation back to the normal button).

Thanks Gary, for your reply, at least I know I'm not the odd one to come up with this solution :)