Explorar el Código

ScreenFade Library Module

Added a new library module called ScreenFade to handle switching canvas contents by fading out and in. ScreenFade can also be used as a background for popup dialogs or windows.
Peter Robinson hace 3 años
padre
commit
abe426fc57

+ 3 - 0
library/ScreenFade/gui/background.image.taml

@@ -0,0 +1,3 @@
+<ImageAsset
+    AssetName="background"
+    ImageFile="background.png" />

BIN
library/ScreenFade/gui/background.png


+ 15 - 0
library/ScreenFade/module.taml

@@ -0,0 +1,15 @@
+<ModuleDefinition
+	ModuleId="ScreenFade"
+	VersionId="1"
+	BuildID="5"
+	Synchronized="1"
+	Description="Switches the Canvas with a quick screen fade. Can be used for a temporary background for dialogs or windows."
+	Author="Torque2D"
+	ScriptFile="screenFade.cs"
+	CreateFunction="create"
+	DestroyFunction="destroy">
+		<DeclaredAssets
+			Path="gui"
+			Extension="image.taml"
+			Recurse="false"/>
+</ModuleDefinition>

+ 71 - 0
library/ScreenFade/screenFade.cs

@@ -0,0 +1,71 @@
+function ScreenFade::create(%this)
+{
+	exec("./scripts/ScreenFadeBackground.cs");
+
+	%this.background = new GuiSpriteCtrl() {
+		class = "ScreenFadeBackground";
+		profile = "GuiDefaultProfile";
+		HorizSizing = "relative";
+		VertSizing = "relative";
+		Position = "0 0";
+		Image = "ScreenFade:background";
+		FullSize = 1;
+		ConstrainProportions = 0;
+		ImageColor = "255 255 255 0";
+		Owner = %this;
+	};
+}
+
+function ScreenFade::destroy(%this)
+{
+	if(isObject(%this.background))
+	{
+		%this.background.delete();
+	}
+}
+
+//Switches the canvas by fading in to color and back out to the new content. The process takes the given time in milliseconds.
+//Color and time are optional.
+//ScreenFade will post event: onSwapComplete().
+function ScreenFade::swapCanvas(%this, %content, %color, %time)
+{
+	if(%color $= "")
+	{
+		%color = "0 0 0 0";
+	}
+
+	if(%time $= "")
+	{
+		%time = 1400;
+	}
+
+	%base = getWord(%color, 0) SPC getWord(%color, 1) SPC getWord(%color, 2);
+	%this.background.solidColor = %base SPC "255";
+	%this.background.transparentColor = %base SPC "0";
+	%this.background.swapContent = %content;
+	%this.background.swapTime = mRound((%time / 5) * 2);
+	%this.background.startSwap();
+}
+
+//Fades the screen to color and then puts the dialog on top of it.
+//Color and time are optional.
+//When your dialog closes, call %this.post("dialogClose"); to inform ScreenFade where %this is the same object passed to openDialog().
+//ScreenFade will post events: onOpenComplete() and onCloseComplete().
+function ScreenFade::openDialog(%this, %dialog, %color, %time)
+{
+	if(%color $= "")
+	{
+		%color = "0 0 0 230";
+	}
+
+	if(%time $= "")
+	{
+		%time = 300;
+	}
+
+	%this.background.solidColor = %color;
+	%this.background.transparentColor = getWord(%color, 0) SPC getWord(%color, 1) SPC getWord(%color, 2) SPC "0";
+	%this.background.dialog = %dialog;
+	%this.background.dialogTime = %time;
+	%this.background.openDialog();
+}

+ 75 - 0
library/ScreenFade/scripts/ScreenFadeBackground.cs

@@ -0,0 +1,75 @@
+function ScreenFadeBackground::resetColor(%this)
+{
+	if(getWord(%this.getImageColor(), 3) == 0)
+	{
+		%this.setImageColor(%this.transparentColor);
+		%extent = Canvas.getExtent();
+		%this.setExtent(getWord(%extent, 0), getWord(%extent, 1));
+	}
+}
+
+function ScreenFadeBackground::startSwap(%this)
+{
+	%this.resetColor();
+	Canvas.pushDialog(%this);
+	%this.fadeTo(%this.solidColor, %this.swapTime, "EaseInOut");
+	%this.schedule(%this.swapTime, "doSwap");
+}
+
+function ScreenFadeBackground::doSwap(%this)
+{
+	Canvas.setContent(%this.swapContent);
+	Canvas.pushDialog(%this);
+	%this.schedule(%this.swapTime/2, "fadeSwap");
+}
+
+function ScreenFadeBackground::fadeSwap(%this)
+{
+	%this.fadeTo(%this.transparentColor, %this.swapTime, "EaseInOut");
+	%this.schedule(%this.swapTime, "finishSwap");
+}
+
+function ScreenFadeBackground::finishSwap(%this)
+{
+	Canvas.popDialog(%this);
+	%this.Owner.postEvent("SwapComplete");
+}
+
+function ScreenFadeBackground::openDialog(%this)
+{
+	%this.resetColor();
+	%this.add(%this.dialog);
+	Canvas.pushDialog(%this);
+	%this.fadeTo(%this.solidColor, %this.dialogTime, "EaseInOut");
+	%this.schedule(%this.dialogTime, "openDialogComplete");
+}
+
+function ScreenFadeBackground::openDialogComplete(%this)
+{
+	%this.Owner.postEvent("OpenComplete");
+	%this.startListening(%this.dialog);
+}
+
+function ScreenFadeBackground::onDialogClose(%this)
+{
+	if(%this.isAwake())
+	{
+		if(isEventPending(%this.hideSchedule))
+		{
+			cancel(%this.hideSchedule);
+		}
+		%this.fadeTo(%this.transparentColor, %this.dialogTime, "EaseIn");
+		%this.hideSchedule = %this.schedule(%this.dialogTime, "onCloseComplete");
+	}
+}
+
+function ScreenFadeBackground::onCloseComplete(%this)
+{
+	if(isObject(%this.dialog))
+	{
+		%this.stopListening(%this.dialog);
+		%this.removeIfMember(%this.dialog);
+	}
+	Canvas.popDialog(%this);
+	%this.Owner.postEvent("CloseComplete");
+}