Showing posts with label class. Show all posts
Showing posts with label class. Show all posts
Thursday, January 29, 2015
Creating Advanced Alert Window class Part 9
In this tutorial we will add support for creating backgrounds and headers filled with bitmap.
We have SolidColorRect, GradientColorRect, now lets create a new class - BitmapRect.as.
Declare the following variables:
Their usage is described in the comments:
Add the constructor, which receives parameters and applies their values to these variables:
Full BitmapRect.as code:
Now go to AdvAlertWindow.as.
Add 2 if..statements to the updateDraw() function which are executed if bgRect/headerRect is an object of BitmapRect class. Simply pass the needed values to methods of the graphics class:
Full AdvAlertWindow.as code:
Now you can go to main.as and create an alert window filled with bitmap. In this example, I generate a perlin noise map and apply it to the alert windows background:
Thanks for reading!
We have SolidColorRect, GradientColorRect, now lets create a new class - BitmapRect.as.
Declare the following variables:
public var _bitmap:BitmapData;
public var _matrix:Matrix;
public var _repeat:Boolean;
public var _smooth:Boolean;
public var _radius:Array;
public var _lineThickness:Number;
public var _lineColor:uint;
public var _alpha:Number;
public var _lineAlpha:Number;
Their usage is described in the comments:
/**
* Create a rectangle filled with bitmap. Has the same parameters as beginBitmapFill(), plus lineThickness, lineColor, lineAlpha and cornerRadius.
* @parambitmap BitmapData to be used to fill the rectangle.
* @parammatrix Matrix object for transformation.
* @paramrepeat Repeat the bitmap when filling.
* @paramsmooth Smoothen the bitmap.
* @paramcornerRadius Radius of the corners of the box - top left, top right, bottom left, bottom right.
* @paramlineThickness Thickness of the stroke.
* @paramlineColor Color of the stroke.
* @paramalpha Alpha channel of the background fill.
* @paramlineAlpha Alpha channel of the stroke.
*/
Add the constructor, which receives parameters and applies their values to these variables:
public function BitmapRect(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = true, smooth:Boolean = false, cornerRadius:Array = null, lineThickness:Number = 1, lineColor:uint = 0x333333, alpha:Number = 1, lineAlpha:Number = 1)
{
if (cornerRadius == null) cornerRadius = [10, 10, 0, 0];
_bitmap = bitmap;
_matrix = matrix;
_repeat = repeat;
_smooth = smooth;
_radius = cornerRadius;
_lineThickness = lineThickness;
_lineColor = lineColor;
_alpha = alpha
_lineAlpha = lineAlpha;
}
Full BitmapRect.as code:
package com.kircode.AdvAlert
{
import flash.display.BitmapData;
import flash.geom.Matrix;
/**
* ...
* @author Kirill Poletaev
*/
public class BitmapRect
{
public var _bitmap:BitmapData;
public var _matrix:Matrix;
public var _repeat:Boolean;
public var _smooth:Boolean;
public var _radius:Array;
public var _lineThickness:Number;
public var _lineColor:uint;
public var _alpha:Number;
public var _lineAlpha:Number;
/**
* Create a rectangle filled with bitmap. Has the same parameters as beginBitmapFill(), plus lineThickness, lineColor, lineAlpha and cornerRadius.
* @parambitmap BitmapData to be used to fill the rectangle.
* @parammatrix Matrix object for transformation.
* @paramrepeat Repeat the bitmap when filling.
* @paramsmooth Smoothen the bitmap.
* @paramcornerRadius Radius of the corners of the box - top left, top right, bottom left, bottom right.
* @paramlineThickness Thickness of the stroke.
* @paramlineColor Color of the stroke.
* @paramalpha Alpha channel of the background fill.
* @paramlineAlpha Alpha channel of the stroke.
*/
public function BitmapRect(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = true, smooth:Boolean = false, cornerRadius:Array = null, lineThickness:Number = 1, lineColor:uint = 0x333333, alpha:Number = 1, lineAlpha:Number = 1)
{
if (cornerRadius == null) cornerRadius = [10, 10, 0, 0];
_bitmap = bitmap;
_matrix = matrix;
_repeat = repeat;
_smooth = smooth;
_radius = cornerRadius;
_lineThickness = lineThickness;
_lineColor = lineColor;
_alpha = alpha
_lineAlpha = lineAlpha;
}
}
}
Now go to AdvAlertWindow.as.
Add 2 if..statements to the updateDraw() function which are executed if bgRect/headerRect is an object of BitmapRect class. Simply pass the needed values to methods of the graphics class:
public function updateDraw():void {
// bg
this.graphics.clear();
if (bgRect is SolidColorRect) {
this.graphics.beginFill(bgRect._backgroundColor, bgRect._alpha);
this.graphics.lineStyle(bgRect._lineThickness, bgRect._lineColor, bgRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x, pos.y, w, h,
bgRect._radius[0], bgRect._radius[1], bgRect._radius[2], bgRect._radius[3]);
this.graphics.endFill();
}
if (bgRect is GradientColorRect) {
this.graphics.beginGradientFill(bgRect._gradientType, bgRect._colors, bgRect._alphas, bgRect._ratios, bgRect._matrix, bgRect._spreadMethod, bgRect._interpolationMethod, bgRect._focalPointRatio);
this.graphics.lineStyle(bgRect._lineThickness, bgRect._lineColor, bgRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x, pos.y, w, h,
bgRect._radius[0], bgRect._radius[1], bgRect._radius[2], bgRect._radius[3]);
this.graphics.endFill();
}
if (bgRect is BitmapRect) {
this.graphics.beginBitmapFill(bgRect._bitmap, bgRect._matrix, bgRect._repeat, bgRect._smooth);
this.graphics.lineStyle(bgRect._lineThickness, bgRect._lineColor, bgRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x, pos.y, w, h,
bgRect._radius[0], bgRect._radius[1], bgRect._radius[2], bgRect._radius[3]);
this.graphics.endFill();
}
// header
if (headerRect is SolidColorRect) {
this.graphics.beginFill(headerRect._backgroundColor, headerRect._alpha);
this.graphics.lineStyle(headerRect._lineThickness, headerRect._lineColor, headerRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight,
headerRect._radius[0], headerRect._radius[1], headerRect._radius[2], headerRect._radius[3]);
this.graphics.endFill();
}
if (headerRect is GradientColorRect) {
this.graphics.beginGradientFill(headerRect._gradientType, headerRect._colors, headerRect._alphas, headerRect._ratios, headerRect._matrix, headerRect._spreadMethod, headerRect._interpolationMethod, headerRect._focalPointRatio);
this.graphics.lineStyle(headerRect._lineThickness, headerRect._lineColor, headerRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight,
headerRect._radius[0], headerRect._radius[1], headerRect._radius[2], headerRect._radius[3]);
this.graphics.endFill();
}
if (headerRect is BitmapRect) {
this.graphics.beginBitmapFill(headerRect._bitmap, headerRect._matrix, headerRect._repeat, headerRect._smooth);
this.graphics.lineStyle(headerRect._lineThickness, headerRect._lineColor, headerRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight,
headerRect._radius[0], headerRect._radius[1], headerRect._radius[2], headerRect._radius[3]);
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;
textField.multiline = true;
textField.wordWrap = true;
}
Full AdvAlertWindow.as code:
package com.kircode.AdvAlert
{
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;
private var headerRect:*;
private var bgRect:*;
public function AdvAlertWindow(pt:String, ptt:String, pw:int, ph:int, ppos:Point, sk:AdvAlertSkin)
{
t = pt;
tt = ptt;
w = pw;
h = ph;
pos = ppos;
setSkin(sk);
textField = new TextField();
addChild(textField);
titleField = new TextField();
addChild(titleField);
updateDraw();
}
public function setSkin(skin:AdvAlertSkin):void {
textPadding = skin.textPadding;
headerPadding = skin.headerPadding;
titlePadding = skin.titlePadding;
headerHeight = skin.headerHeight;
titleFormat = skin.titleFormat;
textFormat = skin.textFormat;
selectable = skin.selectable;
bgRect = skin.bgRect;
headerRect = skin.headerRect;
}
public function updateDraw():void {
// bg
this.graphics.clear();
if (bgRect is SolidColorRect) {
this.graphics.beginFill(bgRect._backgroundColor, bgRect._alpha);
this.graphics.lineStyle(bgRect._lineThickness, bgRect._lineColor, bgRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x, pos.y, w, h,
bgRect._radius[0], bgRect._radius[1], bgRect._radius[2], bgRect._radius[3]);
this.graphics.endFill();
}
if (bgRect is GradientColorRect) {
this.graphics.beginGradientFill(bgRect._gradientType, bgRect._colors, bgRect._alphas, bgRect._ratios, bgRect._matrix, bgRect._spreadMethod, bgRect._interpolationMethod, bgRect._focalPointRatio);
this.graphics.lineStyle(bgRect._lineThickness, bgRect._lineColor, bgRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x, pos.y, w, h,
bgRect._radius[0], bgRect._radius[1], bgRect._radius[2], bgRect._radius[3]);
this.graphics.endFill();
}
if (bgRect is BitmapRect) {
this.graphics.beginBitmapFill(bgRect._bitmap, bgRect._matrix, bgRect._repeat, bgRect._smooth);
this.graphics.lineStyle(bgRect._lineThickness, bgRect._lineColor, bgRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x, pos.y, w, h,
bgRect._radius[0], bgRect._radius[1], bgRect._radius[2], bgRect._radius[3]);
this.graphics.endFill();
}
// header
if (headerRect is SolidColorRect) {
this.graphics.beginFill(headerRect._backgroundColor, headerRect._alpha);
this.graphics.lineStyle(headerRect._lineThickness, headerRect._lineColor, headerRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight,
headerRect._radius[0], headerRect._radius[1], headerRect._radius[2], headerRect._radius[3]);
this.graphics.endFill();
}
if (headerRect is GradientColorRect) {
this.graphics.beginGradientFill(headerRect._gradientType, headerRect._colors, headerRect._alphas, headerRect._ratios, headerRect._matrix, headerRect._spreadMethod, headerRect._interpolationMethod, headerRect._focalPointRatio);
this.graphics.lineStyle(headerRect._lineThickness, headerRect._lineColor, headerRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight,
headerRect._radius[0], headerRect._radius[1], headerRect._radius[2], headerRect._radius[3]);
this.graphics.endFill();
}
if (headerRect is BitmapRect) {
this.graphics.beginBitmapFill(headerRect._bitmap, headerRect._matrix, headerRect._repeat, headerRect._smooth);
this.graphics.lineStyle(headerRect._lineThickness, headerRect._lineColor, headerRect._lineAlpha);
this.graphics.drawRoundRectComplex(pos.x + headerPadding.left, pos.y + headerPadding.top, w - (headerPadding.left + headerPadding.right), headerHeight,
headerRect._radius[0], headerRect._radius[1], headerRect._radius[2], headerRect._radius[3]);
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;
textField.multiline = true;
textField.wordWrap = true;
}
}
}
Now you can go to main.as and create an alert window filled with bitmap. In this example, I generate a perlin noise map and apply it to the alert windows background:
package
{
import com.kircode.AdvAlert.AdvAlertManager;
import com.kircode.AdvAlert.AdvAlertSkin;
import com.kircode.AdvAlert.BitmapRect;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* @author Kirill Poletaev
*/
public class main extends MovieClip
{
private var AlertManager:AdvAlertManager;
public function main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void {
var bitmapData:BitmapData = new BitmapData(300, 200, false, 0xffffff);
bitmapData.perlinNoise(300, 200, 6, 657, true, true, 7, false, null);
var mySkin:AdvAlertSkin = new AdvAlertSkin();
mySkin.bgRect = new BitmapRect(bitmapData, null, true, true, [10, 10, 0, 0], 2, 0x222222, 1, 1);
AlertManager = new AdvAlertManager(this, stage.stageWidth, stage.stageHeight, mySkin);
AlertManager.alert("Text message.", "Custom skin - way 1");
}
}
}
Thanks for reading!
Sunday, January 18, 2015
Creating EasyTooltip class Part 6
Today well improve our tooltip styling abilities, as well as the way the tooltips are drawn.
Go to TooltipStyle and add a number of new variables. These include backgroundAlpha, textAlpha, textPadding, offsetX and offsetY. It should be noted that textPadding is an object of class TextPadding, which I will add shortly.
TooltipStyle with the new properties and defautl values set:
The TextPadding class is a simple class containing 4 public properties (left, right, top, bottom), constructor and setPadding() method, which I used in AdvAlert library. This is the exact same class, just with a different package (com.kircode.EasyTooltip now).
The class is used for storing padding of a text field in 4 directions. As I said, theres setPadding() method which makes it easy to set all 4 values in one line. Its also possible to do using the constructor.
Create the TextPadding.as class and use this code:
Now go to TooltipCursor.as. Here add 5 new variables - background (Shape), w, h, actual_x and actual_y (integers). In the constructor, declare the background as a new Shape and then add it to the cursor (add it before adding text field to ensure that text field spawns on top of it):
Update setStyle() function so that it only sets the style property and doesnt calculate/set anything else:
Update setText() method so that it only sets the text of the tooltip and applies text format, then calls 2 functions called updateSize() and updateDisplay():
The updateSize() function will later be used to calculate the proper size and position according to settings, but for now lets just hardcode width and height to 150x40. We can calculate the actual_x and actual_y values by adding the offset values to (0;-h):
In updateDisplay() function the tooltip will be drawn and positioned. Use all the provided values to properly draw the background box and position the text field:
Full TooltipCursor code so far:
Thats all for today.
Next time well work on calculating size and position of the tooltip.
Thanks for reading!
Go to TooltipStyle and add a number of new variables. These include backgroundAlpha, textAlpha, textPadding, offsetX and offsetY. It should be noted that textPadding is an object of class TextPadding, which I will add shortly.
TooltipStyle with the new properties and defautl values set:
package com.kircode.EasyTooltip
{
import flash.text.TextFormat;
/**
* Used for visually styling tooltips.
* @author Kirill Poletaev
*/
public class TooltipStyle
{
public var backgroundAlpha:Number;
public var backgroundColor:uint;
public var textAlpha:Number;
public var textFormat:TextFormat;
public var textPadding:TextPadding;
public var offsetX:int;
public var offsetY:int;
public function TooltipStyle()
{
backgroundAlpha = 1;
backgroundColor = 0x000000;
textAlpha = 1;
textFormat = new TextFormat("Arial", 17, 0xffffff);
textPadding = new TextPadding(5, 5, 5, 5);
offsetX = 0;
offsetY = 0;
}
}
}
The TextPadding class is a simple class containing 4 public properties (left, right, top, bottom), constructor and setPadding() method, which I used in AdvAlert library. This is the exact same class, just with a different package (com.kircode.EasyTooltip now).
The class is used for storing padding of a text field in 4 directions. As I said, theres setPadding() method which makes it easy to set all 4 values in one line. Its also possible to do using the constructor.
Create the TextPadding.as class and use this code:
package com.kircode.EasyTooltip
{
/**
* Text padding management.
* @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;
}
}
}
Now go to TooltipCursor.as. Here add 5 new variables - background (Shape), w, h, actual_x and actual_y (integers). In the constructor, declare the background as a new Shape and then add it to the cursor (add it before adding text field to ensure that text field spawns on top of it):
public var txt:TextField;
private var style:TooltipStyle;
private var background:Shape;
private var w:int;
private var h:int;
private var actual_x:int;
private var actual_y:int;
public function TooltipCursor()
{
txt = new TextField();
background = new Shape();
addChild(background);
addChild(txt);
}
Update setStyle() function so that it only sets the style property and doesnt calculate/set anything else:
/**
* Set style of the tooltips.
* @paramst TooltipStyle object to apply.
*/
public function setStyle(st:TooltipStyle):void {
style = st;
}
Update setText() method so that it only sets the text of the tooltip and applies text format, then calls 2 functions called updateSize() and updateDisplay():
/**
* Change the text value of the tooltip.
* @parammessage The new text value.
*/
public function setText(message:String):void {
txt.text = message;
txt.setTextFormat(style.textFormat);
updateSize();
updateDisplay();
}
The updateSize() function will later be used to calculate the proper size and position according to settings, but for now lets just hardcode width and height to 150x40. We can calculate the actual_x and actual_y values by adding the offset values to (0;-h):
private function updateSize():void {
// Here we will later calculate the size, for now lets just leave some hardcoded values
w = 150;
h = 40;
actual_x = 0 + style.offsetX;
actual_y = -h + style.offsetY;
}
In updateDisplay() function the tooltip will be drawn and positioned. Use all the provided values to properly draw the background box and position the text field:
private function updateDisplay():void {
background.graphics.clear();
background.graphics.beginFill(style.backgroundColor, style.backgroundAlpha);
background.graphics.drawRect(actual_x, actual_y, w, h);
background.graphics.endFill();
txt.width = w - style.textPadding.left - style.textPadding.right;
txt.height = h - style.textPadding.top - style.textPadding.bottom;
txt.x = actual_x + style.textPadding.left;
txt.y = actual_y + style.textPadding.top;
}
Full TooltipCursor code so far:
package com.kircode.EasyTooltip
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFormat;
/**
* The cursor that follows the mouse (if not set otherwise) and displays each tooltip.
* @author Kirill Poletaev
*/
public class TooltipCursor extends MovieClip
{
public var txt:TextField;
private var style:TooltipStyle;
private var background:Shape;
private var w:int;
private var h:int;
private var actual_x:int;
private var actual_y:int;
public function TooltipCursor()
{
txt = new TextField();
background = new Shape();
addChild(background);
addChild(txt);
}
/**
* Set style of the tooltips.
* @paramst TooltipStyle object to apply.
*/
public function setStyle(st:TooltipStyle):void {
style = st;
}
/**
* Change the text value of the tooltip.
* @parammessage The new text value.
*/
public function setText(message:String):void {
txt.text = message;
txt.setTextFormat(style.textFormat);
updateSize();
updateDisplay();
}
private function updateDisplay():void {
background.graphics.clear();
background.graphics.beginFill(style.backgroundColor, style.backgroundAlpha);
background.graphics.drawRect(actual_x, actual_y, w, h);
background.graphics.endFill();
txt.width = w - style.textPadding.left - style.textPadding.right;
txt.height = h - style.textPadding.top - style.textPadding.bottom;
txt.x = actual_x + style.textPadding.left;
txt.y = actual_y + style.textPadding.top;
}
private function updateSize():void {
// Here we will later calculate the size, for now lets just leave some hardcoded values
w = 150;
h = 40;
actual_x = 0 + style.offsetX;
actual_y = -h + style.offsetY;
}
}
}
Thats all for today.
Next time well work on calculating size and position of the tooltip.
Thanks for reading!
Subscribe to:
Posts (Atom)