Skip to content

Instantly share code, notes, and snippets.

@itoz itoz/BalloonPanel
Created Apr 7, 2011

Embed
What would you like to do?
/**
*============================================================
* copyright(c) 2011
* @author itoz
* 2011/04/01
*============================================================
*/
package com.romatica.display
{
import caurina.transitions.Tweener;
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.geom.Point;
/**
* BallonPanel.as
* フキダシを作る
*/
public class BalloonPanel extends Sprite
{
private var _side : int;//上下左右どちらにバルーンを出すか 0:バルーンが上 以下時計回り
public static const UP_SIDE : int = 0;
public static const RIGHT_SIDE : int = 1;
public static const BOTTOM_SIDE : int = 2;
public static const LEFT_SIDE : int = 3;
// ----------------------------------------------------------------------
public var angle : Number= 90; // 支点と接辺のなす角度 (0〜180) 90で垂直
public var leng : Number= 30;//支点と接点までの距離
public var sideLeng : Number= 30;//三角形とパネルが接している部分の長さ
public var triPos : Number= 0.5;// 接辺の位置を表す (0〜1) 0.5で中心
public var panelWidth : int= 200;
public var panelHeight : int =100;
public var round : int= 5;//パネルの角丸
// ----------------------------------------------------------------------
private var _panel : RoundPanel;
private var _color : Number;
public function BalloonPanel(side:int =BalloonPanel.UP_SIDE ,color:Number=0xcccccc,propObj:Object = null)
{
_panel = new RoundPanel();
_side = side;
_color = color;
addChild(_panel);
draw(propObj);
}
// ======================================================================
/**
* tween
* @param obj    
*/
public function tween(obj : Object) : void
{
var tweenObj :Object= toObject();
for (var i : String in obj) {
if(i == "onUpdate"){
throw new ArgumentError("onUpdateはこのクラス内で使用するので、有効になりません");
}
tweenObj[i] = obj[i];
}
Tweener.removeTweens(this);
tweenObj["onUpdate"] = draw;
Tweener.addTween(this,tweenObj);
}
// ======================================================================
/**
* draw
* @param obj
*/
public function draw(obj:Object=null) : void
{
//自信にプロパティを設定
for (var i : String in obj) {
this[i] = obj[i];
};
var sideCP : Point; // 接辺の中心点
var _p0 : Point;
var _p1 : Point;
// ----------------------------------------------------------------
// リミットチェック
// 距離
if (leng < 0) {
leng = 0;
}
// else if (triLeng >= Math.min(h, w) - (round*2) ) {
// triLeng = Math.min(h, w)- (round*2) ;
// }
// 辺上のポイント
if (triPos > 1) {
triPos = 1;
}
else if (triPos < 0) {
triPos = 0;
}
// 角度
if (angle > 180 ) {
angle = 180;
}
else if (angle < 0) {
angle = 0;
}
// ----------------------------------------------------------------
// 角度補正
var ang : Number = 180 + (90 * _side) + angle;
// ----------------------------------------------------------------
//三角形部分のポイントを得る
sideCP = Point.polar(leng, ang * (Math.PI / 180));
switch(_side){
case 0 :
case 2 :
_p0 = new Point(sideCP.x - (sideLeng / 2), sideCP.y);
_p1 = new Point(sideCP.x + (sideLeng / 2), sideCP.y);
break;
case 1:
case 3:
_p0 = new Point(sideCP.x, sideCP.y - (sideLeng / 2));
_p1 = new Point(sideCP.x, sideCP.y + (sideLeng / 2));
break;
default:
}
// ----------------------------------------------------------------
//三角形を描く
drawTriangle(new Point(0,0), _p0, _p1,_color);
// ----------------------------------------------------------------
//パネル部分を描く
var panerlP:Point = new Point();
var possibleLeng :Number;////移動可能な最大値
var diff:Number; // 移動可能値*パーセント
var offset:Number;//角丸と三角辺を考慮した、最終の接点オフセット値
switch(_side) {
case 0:
possibleLeng = panelWidth - (sideLeng) - (round * 2);
diff = possibleLeng * (1 - triPos);
offset = -diff - (sideLeng / 2) - round;
panerlP.x = sideCP.x + offset;
panerlP.y = sideCP.y - panelHeight;
break;
case 1:
possibleLeng = panelHeight - (sideLeng) - (round * 2);
diff = possibleLeng * (1 - triPos);
offset = -diff - ( sideLeng / 2) - round;
panerlP.x = sideCP.x ;
panerlP.y = sideCP.y + offset;
break;
case 2:
possibleLeng = panelWidth - (sideLeng) - (round * 2);
diff = possibleLeng * triPos;
offset = -diff - (sideLeng / 2 ) - round ;
panerlP.x = sideCP.x + offset;
panerlP.y = sideCP.y;
break;
case 3:
possibleLeng = panelHeight - (sideLeng) - (round * 2);
diff = possibleLeng * triPos;
offset = -diff - ( sideLeng / 2) - round;
panerlP.x = sideCP.x - panelWidth;
panerlP.y = sideCP.y + offset;
break;
default:
}
_panel.draw(panelWidth, panelHeight, round, _color, _color);
_panel.x = panerlP.x;
_panel.y = panerlP.y;
}
// ======================================================================
/**
* 3角形部分を描く
*/
private function drawTriangle(p0 : Point, p1 : Point, p2 : Point, col : Number = 0xffffff) : void
{
with(graphics) {
clear();
beginFill(col);
moveTo(p0.x, p0.y);
lineTo(p1.x, p1.y);
lineTo(p2.x, p2.y);
endFill();
}
}
// ______________________________________________________________________
/** GlowFilter このメソッドぶっちゃけなくてもいい
*/
public function shadow(col : Number = 0x000000, alpha : Number = 1, blurX : Number = 6, blurY : Number = 6, strength : Number = 2, quality : int = 1, inner : Boolean = false, knockout : Boolean = false) : void
{
// shadow
filters = [new GlowFilter(col, alpha, blurX, blurY, strength, quality, inner, knockout)];
}
// ______________________________________________________________________
/** プロパティーをOBJで取得
*/
public function toObject() : Object
{
return {
angle:angle
,leng:leng
,sideLeng:sideLeng
,panelWidth:panelWidth
,panelHeight:panelHeight
,triPos:triPos
,round:round
};
}
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// ======================================================================
//                                   setter getter
// ======================================================================
public function set side(side : int) : void
{
if (side < 0 || side > 3) {
// side = 0;
throw new ArgumentError("0~3の整数を指定してください")
}
_side = side;
}
public function set color(color : Number) : void
{
_color = color;
}
public function get panel() : RoundPanel
{
return _panel;
}
}
}
/**
*============================================================
* copyright(c).
* @author itoz
*============================================================
*-2011.4.6 drawを追加
*/
package com.romatica.display
{
import flash.display.GradientType;
/**
* RoundPanel
* 角丸パネルを作る
*/
public class RoundPanel extends Sprite
{
protected var _col1 : Number;
protected var _col2 : Number;
protected var _round : int;
protected var _w : int;
protected var _h : int;
public function RoundPanel ( )
{
}
// ======================================================================
/**
* draw.
*/
public function draw(w:int,h:int,round:int=5,col1:Number =0xeeeeee,col2:Number =0xededed):void
{
_w = w;
_h = h;
_col1 = col1;
_col2 = col2;
_round = round;
//TODO グラデ一旦未対応に変更
var gradType : String = GradientType.LINEAR;
var gradColors : Array = [_col1 ,_col1 ];
var gradAlphas : Array = [1 , 1];
var gradRadios : Array = [0 , 150];
var gradMrx : Matrix = new Matrix();
var gradSpread : String = SpreadMethod.PAD;
gradMrx.createGradientBox( _w , _h , Math.PI / 2 , 0 , 0 );
with(graphics) {
clear();
beginGradientFill(gradType, gradColors, gradAlphas, gradRadios, gradMrx, gradSpread);
drawRoundRect(0, 0, _w, _h, _round);
endFill();
}
}
}
}
/**
*============================================================
* copyright(c) 2011
* @author itoz
* 2011/04/01
*============================================================
*/
package
{
import com.romatica.display.BalloonPanel;
/**
* TestBalloonPanel.as
*/
public class TestBalloonPanel extends Sprite
{
private var balloon : BalloonPanel;
public function TestBalloonPanel()
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
// balloon = new BalloonPanel();
balloon = new BalloonPanel(BalloonPanel.UP_SIDE, 0xffffff, {panelWidth:300, angle:10, sideLeng:50,triPos:1});
balloon.shadow(0x000000,0.3);
// balloon.side =1
// balloon.side =2
// balloon.side =3
// balloon.color = 0xcc00cc;
balloon.tween({time:4,
panelWidth:100,
panelHeight:100,
angle:170,
leng:100,
round:20,
sideLeng:20,
triPos:0,
onComplete:comp});
balloon.x = 300;
balloon.y = 300;
addChild(balloon);
}
private function comp():void
{
trace("★test");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.