Thursday, September 6, 2012

Creating Advanced Alert Window class: Part 4

In this tutorial we will add more skinning options to the alert window.

Go to AdvAlertWindow.as and declare more variables:

private var titlePadding:TextPadding;
private var titleFormat:TextFormat;
private var textFormat:TextFormat;
private var selectable:Boolean;

The titlePadding variable will be used to create padding for the title text field inside the header box. Right now we have padding for header box only, which moves the header and the title together. Adding this variable will mean that we will also be able to tweak position of the title inside the header.

The titleFormat and textFormat objects are used for formatting the title and body text.

The selectable boolean will determine whether the text is selectable or not.

Go to the constructor and let's update the window skinning part.

Firstly, we'll get rid of lines that call setPadding() methods of TextPadding objects. Instead, we'll pass the padding values to the constructor.

textPadding = new TextPadding(5, 5, 5, 5);
headerPadding = new TextPadding(5, 5, 5, 5);
titlePadding = new TextPadding(2, 2, 2, 2);

Next let's set up some text formatting.

titleFormat = new TextFormat();
titleFormat.size = 20;
titleFormat.font = "Arial";
titleFormat.bold = true;
textFormat = new TextFormat();
textFormat.size = 16;
textFormat.font = "Arial";

Then we set selectable to false.

selectable = false;

Full constructor:

public function AdvAlertWindow(pt:String, ptt:String, pw:int, ph:int, ppos:Point) 
{
t = pt;
tt = ptt;
w = pw;
h = ph;
pos = ppos;

// skinning
textPadding = new TextPadding(5, 5, 5, 5);
headerPadding = new TextPadding(5, 5, 5, 5);
titlePadding = new TextPadding(2, 2, 2, 2);
headerHeight = 30;
titleFormat = new TextFormat();
titleFormat.size = 20;
titleFormat.font = "Arial";
titleFormat.bold = true;
textFormat = new TextFormat();
textFormat.size = 16;
textFormat.font = "Arial";
selectable = false;

textField = new TextField();
addChild(textField);

titleField = new TextField();
addChild(titleField);

updateDraw();
}

Now go to updateDraw() function. In the lines that set x and y coordinates of titleField, add titlePadding's left and top values.

Set text formats of textField and titleField to textFormat and titleFormat. Also set their selectable values to selectable variable value.

public function updateDraw():void {
// bg
this.graphics.clear();
this.graphics.beginFill(0xcccccc, 1);
this.graphics.drawRect(pos.x, pos.y, w, h);
this.graphics.endFill();
// header
this.graphics.beginFill(0xeeeeee, 1);
this.graphics.drawRect(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight);
this.graphics.endFill();
// title
titleField.width = w - (headerPadding.left + headerPadding.right);
titleField.text = tt;
titleField.height = headerHeight;
titleField.x = pos.x + headerPadding.left + titlePadding.left;
titleField.y = pos.y + headerPadding.top + titlePadding.top;
// text
textField.width = w - (textPadding.right + textPadding.left);
textField.height = h - (textPadding.top + textPadding.bottom + headerPadding.top + headerPadding.bottom + headerHeight);
textField.text = t;
textField.x = pos.x + textPadding.right;
textField.y = pos.y + textPadding.top + headerHeight + headerPadding.bottom + headerPadding.top;
// formats
textField.setTextFormat(textFormat);
titleField.setTextFormat(titleFormat);
textField.selectable = selectable;
titleField.selectable = selectable;
}

Full AdvAlertWindow.as code:

package com.kircode 
{
import flash.display.MovieClip;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.GradientType;

/**
 * Advanced Alert window object.
 * @author Kirill Poletaev
 */
public class AdvAlertWindow extends MovieClip
{
private var t:String;
private var tt:String;
private var w:int;
private var h:int;
private var pos:Point;

private var textField:TextField;
private var titleField:TextField;

private var textPadding:TextPadding;
private var headerPadding:TextPadding;
private var titlePadding:TextPadding;
private var headerHeight:int;
private var titleFormat:TextFormat;
private var textFormat:TextFormat;
private var selectable:Boolean;

public function AdvAlertWindow(pt:String, ptt:String, pw:int, ph:int, ppos:Point) 
{
t = pt;
tt = ptt;
w = pw;
h = ph;
pos = ppos;

// skinning
textPadding = new TextPadding(5, 5, 5, 5);
headerPadding = new TextPadding(5, 5, 5, 5);
titlePadding = new TextPadding(2, 2, 2, 2);
headerHeight = 30;
titleFormat = new TextFormat();
titleFormat.size = 20;
titleFormat.font = "Arial";
titleFormat.bold = true;
textFormat = new TextFormat();
textFormat.size = 16;
textFormat.font = "Arial";
selectable = false;

textField = new TextField();
addChild(textField);

titleField = new TextField();
addChild(titleField);

updateDraw();
}

public function updateDraw():void {
// bg
this.graphics.clear();
this.graphics.beginFill(0xcccccc, 1);
this.graphics.drawRect(pos.x, pos.y, w, h);
this.graphics.endFill();
// header
this.graphics.beginFill(0xeeeeee, 1);
this.graphics.drawRect(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight);
this.graphics.endFill();
// title
titleField.width = w - (headerPadding.left + headerPadding.right);
titleField.text = tt;
titleField.height = headerHeight;
titleField.x = pos.x + headerPadding.left + titlePadding.left;
titleField.y = pos.y + headerPadding.top + titlePadding.top;
// text
textField.width = w - (textPadding.right + textPadding.left);
textField.height = h - (textPadding.top + textPadding.bottom + headerPadding.top + headerPadding.bottom + headerHeight);
textField.text = t;
textField.x = pos.x + textPadding.right;
textField.y = pos.y + textPadding.top + headerHeight + headerPadding.bottom + headerPadding.top;
// formats
textField.setTextFormat(textFormat);
titleField.setTextFormat(titleFormat);
textField.selectable = selectable;
titleField.selectable = selectable;
}

}

}

Go to TextPadding.as and update it to be able to pass padding values in the constructor:

package com.kircode 
{
/**
 * ...
 * @author Kirill Poletaev
 */
public class TextPadding
{
public var left:int = 5;
public var right:int = 5;
public var top:int = 5;
public var bottom:int = 5;

/**
 * Padding values of a text field. All default values are 5.
 * @paramtopPadding Top padding of the text field.
 * @paramrightPadding Right padding of the text field.
 * @parambottomPadding Bottom padding of the text field.
 * @paramleftPadding Left padding of the text field.
 */

public function TextPadding(topPadding:int = 5, rightPadding:int = 5, bottomPadding:int = 5, leftPadding:int = 5) 
{
top = topPadding;
right = rightPadding;
bottom = bottomPadding;
left = leftPadding;
}

/**
 * Padding values of a text field. All default values are 5.
 * @paramtopPadding Top padding of the text field.
 * @paramrightPadding Right padding of the text field.
 * @parambottomPadding Bottom padding of the text field.
 * @paramleftPadding Left padding of the text field.
 */

public function setPadding(topPadding:int, rightPadding:int, bottomPadding:int, leftPadding:int):void {
top = topPadding;
right = rightPadding;
bottom = bottomPadding;
left = leftPadding;
}

}

}

And that's all for today.

Thanks for reading!

No comments:

Post a Comment