| 1 |
///<reference path="all.ts"/> |
| 2 |
module jg { |
| 3 |
/** |
| 4 |
* 文字を表示するクラス |
| 5 |
*/ |
| 6 |
export class Label extends E { |
| 7 |
/** 表示する文字列。変更する場合はこのフィールドを直接編集するのではなく、setTextを呼ぶべき */ |
| 8 |
text: string; |
| 9 |
/** 最大幅。変更する場合はこのフィールドを直接編集するのではなく、setMaxWidthを呼ぶべき */ |
| 10 |
maxWidth: number; |
| 11 |
/** 同期するオブジェクト */ |
| 12 |
syncObj: any; |
| 13 |
/** 同期するプロパティ */ |
| 14 |
syncProp: string; |
| 15 |
/** 同期時にMath.roundをかけるかどうか */ |
| 16 |
syncRound: boolean; |
| 17 |
|
| 18 |
/** |
| 19 |
* コンストラクタ |
| 20 |
* @param text 表示文字列 |
| 21 |
* @param fontSize 文字の大きさ |
| 22 |
* @param fontColor 文字色 |
| 23 |
* @param baseLine ベースライン |
| 24 |
*/ |
| 25 |
constructor(text?:string, fontSize?:number, fontColor?:string, baseline?:string) { |
| 26 |
super(); |
| 27 |
this.x = 0; |
| 28 |
this.y = 0; |
| 29 |
if (text) { |
| 30 |
this.setText(text); |
| 31 |
} else { |
| 32 |
this.setText(""); |
| 33 |
this.width = 0; |
| 34 |
this.height = 0; |
| 35 |
} |
| 36 |
this.setTextBaseline(baseline ? baseline : "top"); |
| 37 |
this.setFontSize(fontSize ? fontSize : 14); |
| 38 |
this.setColor(fontColor ? fontColor : "black"); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* 最大幅を変更する。描画される文字列がこの幅を上回る場合、自動的に縮小される |
| 43 |
* @param maxWidth 最大幅 |
| 44 |
*/ |
| 45 |
setMaxWidth(maxWidth:number) { |
| 46 |
this.maxWidth = maxWidth; |
| 47 |
this.updateSize(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* 内部サイズを更新する |
| 52 |
*/ |
| 53 |
updateSize() { |
| 54 |
var canvas = window.createCanvas(10, 10); |
| 55 |
var ctx = <CanvasRenderingContext2D>canvas.getContext("2d"); |
| 56 |
ctx.font = this.getFont(); |
| 57 |
//ctx.textAlign = this.getTextAlign(); |
| 58 |
var metrix:TextMetrics = ctx.measureText(this.text); |
| 59 |
this.width = metrix.width; |
| 60 |
this.height = this.getFontSize(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* 文字に影をつける |
| 65 |
* @param color カラーの色。省略時は黒 |
| 66 |
*/ |
| 67 |
addShadow(color?:string) { |
| 68 |
//this.setDrawOption("shadowBlur", 2); |
| 69 |
//this.setDrawOption("shadowColor", color ? color : "black"); |
| 70 |
this.setDrawOption("shadowBlur", 1); |
| 71 |
this.setDrawOption("shadowColor", color ? color : "rgba(0,0,0,0.8)"); |
| 72 |
this.setDrawOption("shadowOffsetX", 1); |
| 73 |
this.setDrawOption("shadowOffsetY", 1); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* 文字の影を削除する |
| 78 |
*/ |
| 79 |
removeShadow() { |
| 80 |
this.removeDrawOption("shadowBlur"); |
| 81 |
this.removeDrawOption("shadowColor"); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* 表示する文字列を変更する |
| 86 |
* @param text 変更後の文字列 |
| 87 |
*/ |
| 88 |
setText(text:string) { |
| 89 |
this.text = text; |
| 90 |
this.updateSize(); |
| 91 |
this.updated(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* 文字描画に利用するフォントを変更する。 |
| 96 |
* 指定できる値はCSS Font形式で、かつline-height指定を除いた値。line-heightについては常にnormalに固定される。 |
| 97 |
* @param fontString フォントを現す文字列 |
| 98 |
*/ |
| 99 |
setFont(fontString:string) { |
| 100 |
this.setDrawOption("font", fontString); |
| 101 |
this.updateSize(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* 現在の文字描画に利用するフォントを取得する |
| 106 |
*/ |
| 107 |
getFont():string { |
| 108 |
return this.getDrawOption("font"); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* フォントサイズを指定する。 |
| 113 |
* @param size 設定するフォントサイズ。単位は常にピクセル |
| 114 |
*/ |
| 115 |
setFontSize(size:number) { |
| 116 |
var dom = document.createElement("div"); |
| 117 |
dom.style.font = this.getFont(); |
| 118 |
dom.style.fontSize = size+"px"; |
| 119 |
this.setFont(dom.style.font); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* フォントサイズを取得する。単位は常にピクセル |
| 124 |
*/ |
| 125 |
getFontSize():number { |
| 126 |
var dom = document.createElement("div"); |
| 127 |
dom.style.font = this.getFont(); |
| 128 |
return Number(dom.style.fontSize.substr(0, dom.style.fontSize.length-2)); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* フォントファミリを取得する |
| 133 |
*/ |
| 134 |
getFontFamily():string { |
| 135 |
var dom = document.createElement("div"); |
| 136 |
dom.style.font = this.getFont(); |
| 137 |
return dom.style.fontFamily; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* フォントファミリを設定する |
| 142 |
* @param family 設定するフォントファミリ |
| 143 |
*/ |
| 144 |
setFontFamily(family:string) { |
| 145 |
var dom = document.createElement("div"); |
| 146 |
dom.style.font = this.getFont(); |
| 147 |
dom.style.fontFamily = family; |
| 148 |
this.setFont(dom.style.font); |
| 149 |
} |
| 150 |
|
| 151 |
//start, end, left, right, center |
| 152 |
/** |
| 153 |
* 文字列の寄せ方を、start, end, left, right, centerいずれかの文字列で指定する |
| 154 |
* @param align 文字列の寄せ方 |
| 155 |
*/ |
| 156 |
setTextAlign(align:string) { |
| 157 |
this.setDrawOption("textAlign", align); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* 現在の文字列の寄せ方を取得する |
| 162 |
*/ |
| 163 |
getTextAlign():string { |
| 164 |
return this.getDrawOption("textAlign"); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* 文字列の基準線をtop, hanging, middle, alphabetic, ideographic, bottomのいずれかの文字列で指定する |
| 169 |
* @param baseline 文字列の基準線 |
| 170 |
*/ |
| 171 |
setTextBaseline(baseline:string) { |
| 172 |
this.setDrawOption("textBaseline", baseline); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* 現在の文字列の基準線を取得する |
| 177 |
*/ |
| 178 |
getTextBaseline():string { |
| 179 |
return this.getDrawOption("textBaseline"); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* 文字色を設定する |
| 184 |
* @param color 設定する文字色。CSSカラーで指定 |
| 185 |
*/ |
| 186 |
setColor(color:string) { |
| 187 |
this.setDrawOption("fillStyle", color); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* 現在の文字色を取得する |
| 192 |
*/ |
| 193 |
getColor():string { |
| 194 |
return this.getDrawOption("fillStyle"); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* 表示文字列を特定オブジェクトの状態に同期させる。 |
| 199 |
* sprite.xに同期させる場合は次のようにする。 |
| 200 |
* label.synchronize(sprite, "x", true); |
| 201 |
* @param obj 同期させるオブジェクト |
| 202 |
* @param prop 同期させるオブジェクトのプロパティ |
| 203 |
* @param round 同期時にMath.roundをかけるかどうか。省略時はかけない |
| 204 |
*/ |
| 205 |
synchronize(obj:any, prop:string, round?:boolean) { |
| 206 |
this.syncObj = obj; |
| 207 |
this.syncProp = prop; |
| 208 |
this.syncRound = round; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* 描画 |
| 213 |
* @param context 対象の描画コンテキスト |
| 214 |
*/ |
| 215 |
draw(context:CanvasRenderingContext2D) { |
| 216 |
if (this.syncObj) { |
| 217 |
var val; |
| 218 |
if (typeof this.syncObj[this.syncProp] == "function") |
| 219 |
val = this.syncObj[this.syncProp](this); |
| 220 |
else |
| 221 |
val = this.syncObj[this.syncProp]; |
| 222 |
|
| 223 |
this.text = this.syncRound ? Math.round(val) : val; |
| 224 |
} |
| 225 |
if (this.maxWidth) { |
| 226 |
context.fillText( |
| 227 |
this.text, |
| 228 |
0, |
| 229 |
0, |
| 230 |
this.maxWidth |
| 231 |
); |
| 232 |
} else { |
| 233 |
context.fillText( |
| 234 |
this.text, |
| 235 |
0, |
| 236 |
0 |
| 237 |
); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |