Game Development Community

GuiSpriteCtrl's initial mode (SOLVED)

by Eero Karvonen · in Torque 2D Beginner · 07/02/2014 (4:57 pm) · 10 replies

There is an undocumented class called GuiSpriteCtrl, which seems to work like a SpriteBase except meant to be used in GUIs.

When I create an instance of it,
%o = new GuiSpriteCtrl();
// setup position and extent etc here...

echo( %o.isStaticFrameProvider() );
it prints zero. How can I change it to static mode to start with in order to be able to change the frames as needed?

#1
07/02/2014 (8:53 pm)
You create an animation asset or an image asset and assign it to the GuiSpriteCtrl.Animation (static is false) or GuiSpriteCtrl.Image (static is true). Just like a Sprite object (it's based on the Sprite object, if I recall).
%ctrl.Animation = %myAnimAsset; // mode is automatically set by
%ctrl.Image = %myImageAsset;    // the type of asset assigned.
#2
07/03/2014 (1:06 am)
GuiSpriteCtrl is an alternative to GuiBitmapCtrl that allows you to use the asset system to assign it a texture. It's similar to how SpriteBase works - they both share the ImageFrameProvider class as a parent. The GuiSpriteCtrl goes down a different rendering path though, you don't get the benefits of the batching system when using it.

When the control is initialized, it checks if an image asset is assigned to it. If no asset is assigned, the isStaticFrameProvider method will return false (basically what Richard said).
#3
07/03/2014 (1:07 pm)
I have defined a 64-celled image asset and loaded it with the corresponding module.
%o = new GuiSpriteCtrl()
{
	Image = %imageAsset;
	Extent = "32 32";
	Position = "0 0";
};
%o.setImageFrame( 10 );
This works fine to recognize and assign the first frame of the image to the GuiSpriteCtrl, but the last line complains,
ImageFrameProviderCore::setImageFrame() - Cannot set Frame without existing asset Id.
Sounds a bit contradictory to me.
#4
07/03/2014 (5:21 pm)
Looks like a bug - there are significant differences in the way that Sprite and GuiSpriteCtrl handle this and perhaps Sprite "moved ahead of" GuiSpriteCtrl because of the lack of GUI tools (most people have generally shied away from messing with GUI stuff). At a wild guess, I'd say that the image isn't being set up quite right based on a quick visual comparison between Sprite and GuiSpriteCtrl.
#5
07/03/2014 (11:50 pm)
Yeah, this is a bug. Trying to set the frame, either in the example above or in the constructor, gives you that error message. As a workaround until a fix is submitted, you can set/change the frame after it has been pushed or added as a child to another Gui element though.

SandboxWindow.add(%o);
%o.setImageFrame(3);   // This works
#6
07/04/2014 (5:19 am)
It only seems that this workaround functions when the parent GUI element is a scenewindow. Here I try to first add the GuiSpriteCtrl to a temporary scenewindow and then move it to an array where I need it to go:
%o = new GuiSpriteCtrl()
{
	Image = %imageAsset;
	Extent = "32 32";
	Position = "0 0";
};
		
$TemporaryWindow.add( %o );

%o.setImageFrame( 3 );

echo( %o.getImageFrame() ); // This prints "3"

%array.add( %o );
echo( %o.getImageFrame() ); // This prints "0" again
So, no luck yet.
#7
07/04/2014 (5:40 am)
There's also a bug with this control where toggling its state from wake to sleep to wake again will reset the image frame back to 0.

I'm going to work on an overhaul to this object. If I don't run into any issues, it will include the following:
- fix to set the frame before waking the control
- fix to prevent the frame field from resetting when pushing/popping
- named frame support
- improved animation support

Hopefully I'll have something pushed to my branch on Github this weekend.
#8
07/04/2014 (10:43 am)
Thanks Mike! Personally I'd start by bringing it's "Spriteness" in line with the Sprite object - Shouldn't take horribly long and should resolve most of the issues.

Sorry I can't be more help - I'm busy learning Chef, Packer, Jeeves, Vagrant, general VMware and VirtualBox management and handling for virtualizing my team's builds....
#9
07/05/2014 (12:44 am)
So, here it is:

github.com/lilligreen/Torque2D/tree/GuiSpriteCtrl

Initial testing looks good, but I'm sure there are plenty of scenarios I haven't covered yet - so if people want to start using it in their games and provide feedback, that would be great. I'll submit this as a pull request as soon as I'm confident there are no major issues left with this specific control.
#10
07/05/2014 (12:54 pm)
You guys are on fire!

@Mike, I compiled your additions into my project and will report if anything comes up. For starters, it works perfectly for setting the images. Thank you so much!