ストップウォッチ
アンドロイド携帯で使えるような形で作りました。
一応デジタル数字の部分はクラス化してあるので他でも利用可能?
作者の携帯はまだアンドロイド携帯でないので誰か確認してくれるとうれしいです。
/**
* Copyright hi.kurosawa ( http://wonderfl.net/user/hi.kurosawa )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vkTV
*/
package {
//----------------------------------------------
//ストップウォッチ
// http://programmingatelier.net/
//----------------------------------------------
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getTimer;
import flash.text.TextFieldAutoSize;
import flash.net.FileReference;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
//import cls.*;
public class StopWatch extends Sprite {
private var objTime:clsTime;
private var objTimeLap:clsTime;
private var btnStart:clsButton;
private var btnClear:clsButton;
private var btnSave:clsButton;
private var txtKiroku:TextField;
private var intJyoutai:int = 0; //状態(停止中=0/計測中=1)
private var intST:int; //計測開示時間(ミリ秒)
private var intLAP:int; //LAP計測開示時間(ミリ秒)
public function StopWatch():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
//下の2行を加えるとHTMLで定義したサイズで表示
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align =StageAlign.TOP_LEFT;
intJyoutai = 0; //状態(停止中=0)
//計測時間の表示
objTime = new clsTime(5,10,27);
addChild(objTime);
//LAP計測時間の表示
addChild(fncText("LAP", 13, 5, 95, 0x00ff00));
objTimeLap = new clsTime(30,70);
addChild(objTimeLap);
//START/LAPボタン
btnStart = new clsButton("START", 5,130,145, 40, fncStart);
addChild(btnStart);
//CLEAR/STOPボタン
btnClear = new clsButton("CLEAR", 160,130,145, 40, fncClear);
addChild(btnClear);
//計測履歴
addChild(fncText("Total-Time---Lap-Time", 13, 50, 180, 0x005500));
txtKiroku = fncTextBox(50, 200, 200, 180);
addChild(txtKiroku);
//履歴の保存ボタン
btnSave = new clsButton("SAVE", 50, 385, 200, 20, fncSave);
addChild(btnSave);
btnSave.enable(false);
}
//START/LAPボタン
private function fncStart(e:MouseEvent):void {
if (intJyoutai == 0) { //Start
fncJyoutai(1); //計測中
} else { //Lap
fncDispTime(true); //時間の表示
}
}
//CLEAR/STOPボタン
private function fncClear(e:MouseEvent):void {
if (intJyoutai == 0) { //CLEAR
txtKiroku.text = ""; //表示の初期化
objTime.fncDispTime(0);
objTimeLap.fncDispTime(0);
btnSave.enable(false);
} else { //STOP
fncJyoutai(0);
}
}
//状態の変更
//jyoutai:int = 0; //状態(停止中=0/計測中=1)
private function fncJyoutai(jyoutai:int):void {
intJyoutai = jyoutai;
if (intJyoutai == 0) { //計測終了
btnStart.setText("START");
btnClear.setText("CLEAR");
//計測中イベントクリア
this.removeEventListener(Event.ENTER_FRAME, onKeisoku);
fncDispTime(true); //時間の表示
if (txtKiroku.text == "") {
btnSave.enable(false);
} else {
btnSave.enable(true);
}
} else { //計測開始
btnStart.setText("LAP");
btnClear.setText("STOP");
intST = getTimer(); //開始時間セット
intLAP = intST;
//繰り返しに計測中の表示関数
addEventListener(Event.ENTER_FRAME, onKeisoku);
btnSave.enable(false);
}
}
//計測中イベント
private function onKeisoku(e:Event):void {
fncDispTime(false); //時間表示
}
//時間表示
// bFlg:履歴追加する(=true)/しない(=false)
private function fncDispTime(bFlg:Boolean):void {
var iNow:int = getTimer();
var iT:int = iNow - intST;
var iL:int = iNow - intLAP;
objTime.fncDispTime(iT); //計測時間
objTimeLap.fncDispTime(iL); //Lap
if (bFlg == true) { //履歴追加
txtKiroku.text = fncTextTimeFormat(iT) + "-" +
fncTextTimeFormat(iL)+"\n" + txtKiroku.text;
intLAP = iNow; //LAPの計測開始時間セット
}
}
//履歴用の時間文字列作成
// intTime:時間
// 戻り値:履歴用の時間文字列("00:00:00:00")
private function fncTextTimeFormat(intTime:int):String {
var s:String = "";
s += (int(intTime / (1000 * 60 * 60 * 10)) % 10).toString();
s += (int(intTime / (1000 * 60 * 60 )) % 10).toString();
s += ":";
s += (int(intTime / (1000 * 60 * 10 )) % 10).toString();
s += (int(intTime / (1000 * 60 )) % 10).toString();
s += ":";
s += (int(intTime / (1000 * 10 )) % 10).toString();
s += (int(intTime / (1000 )) % 10).toString();
s += ":";
s += (int(intTime / (100 )) % 10).toString();
s += (int((intTime+5) / (10 )) % 10).toString();
return s;
}
//履歴をファイルとして保存
private function fncSave(e:MouseEvent):void {
if (intJyoutai != 0) {return; }
if (txtKiroku.text == "") { return; }
var strData:String = txtKiroku.text;
//改行の変更
var arrDat:Array = strData.split("\r");
strData = arrDat.join("\r\n");
strData = "Total-Time Lap-Time\r\n" + strData;
// ファイル名:SW2011-4-30-11-30.dat
var d:Date = new Date();
var fNam:String = "SW"+d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() +
"-" + d.getHours() + "-" + d.getMinutes()+".dat";
var filRef:FileReference = new FileReference();
filRef.save(strData,fNam);
}
//固定文字文字列表示関数
// sText:表示文字列
// nSiz:文字サイズ
// nx,ny:文字位置
// uic:カラー
// 戻り値:作成TextField
private function fncText(sText:String , nSiz:Number,nx:int,ny:int,uic:uint):TextField {
var tf:TextField = new TextField();
tf.text = sText;
tf.textColor = uic;
tf.x = nx;
tf.y = ny;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.selectable = false;
var format:TextFormat = new TextFormat();
format.size = nSiz;
format.italic = true;
tf.setTextFormat(format);
return tf;
}
//履歴表示エリア
// nx,ny:文字位置
// nwid:, nhei:幅、高さ
// 戻り値:作成TextField
private function fncTextBox( nx:int, ny:int, nwid:Number, nhei:Number):TextField {
var tf:TextField = new TextField();
tf.x = nx;
tf.y = ny;
tf.width = nwid;
tf.height = nhei;
tf.border = true;
tf.multiline=true;
return tf;
}
}
}
//package cls {
//----------------------------------------------
//ボタンクラス:TextFieldをボタンのように使う
// http://programmingatelier.net/
//----------------------------------------------
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.ui.MouseCursor;
import flash.ui.Mouse;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
//public class clsButton extends TextField {
class clsButton extends TextField {
private var tfFormat:TextFormat;
private var bolEnable:Boolean;
private var fncChk:Function;
//ボタンクラス:TextFieldをボタンのように使う
// sText:表示する文字
// nX,nY:表示位置
// nWidth,nHeight:ボタンのサイズ
// fChk:クリックしたときの関数「fChk(e:MouseEvent):void」
public function clsButton(sText:String ,
nX:Number, nY:Number, nWidth:Number, nHeight:Number , fChk:Function) {
this.text = sText;
this.x = nX;
this.y = nY;
this.width = nWidth;
this.height = nHeight;
this.border = true;
this.borderColor = 0xffffff;
this.background = true;
this.backgroundColor = 0x77dd77;
this.textColor = 0xffffff;
this.selectable = false;
tfFormat = new TextFormat();
tfFormat.align = TextFormatAlign.CENTER;
tfFormat.size = nHeight *0.8;
tfFormat.italic = true;
this.setTextFormat(tfFormat);
bolEnable = false;
fncChk = fChk;
this.enable(true);
}
//表示文字の変更
// sText:表示する文字
public function setText(sText:String):void {
this.text = sText;
this.setTextFormat(tfFormat);
}
//ボタンの有効(bflg=true)・無効(bflg=false)
public function enable(bflg:Boolean):void {
if (bflg == bolEnable) { return; }
bolEnable = bflg;
if (bolEnable) {
this.addEventListener(MouseEvent.CLICK, fncChk);
this.addEventListener(MouseEvent.ROLL_OVER, fncOver);
this.addEventListener(MouseEvent.ROLL_OUT, fncOut);
this.alpha = 1.0;
}else {
this.removeEventListener(MouseEvent.CLICK, fncChk);
this.removeEventListener(MouseEvent.ROLL_OVER, fncOver);
this.removeEventListener(MouseEvent.ROLL_OUT, fncOut);
this.alpha = 0.5;
}
}
//マウスオーバー処理(ボタンらしく見せる)
public function fncOver(e:MouseEvent):void {
e.target.backgroundColor = 0x55bb55;
//Mouse.cursor=flash.ui.MouseCursor.HAND;
}
public function fncOut(e:MouseEvent):void {
e.target.backgroundColor = 0x77dd77;
//Mouse.cursor=flash.ui.MouseCursor.ARROW;
}
}
//}
//package cls {
//----------------------------------------------
//ストップウォッチのデジタル数字表示(00:00:00:00)
// http://programmingatelier.net/
//----------------------------------------------
import flash.display.Sprite;
import flash.events.Event;
//public class clsTime extends Sprite {
class clsTime extends Sprite {
private var arrNum:Array;
private var numSiz:Number;
//ストップウォッチのデジタル数字表示
// nX,nY:表示位置
// nSiz:文字高さ
public function clsTime(nX:Number,nY:Number,nSiz:Number = 25.0) {
numSiz = nSiz;
this.x = nX;
this.y = nY;
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
arrNum = new Array();
var nX:Number = 0;
for (var i:int = 0; i < 8; i++) {
var ledNum:clsLedNum = new clsLedNum("0",nX,0,numSiz);
addChild(ledNum);
arrNum.push(ledNum);
nX += 30*numSiz/25.0;
if (i % 2 == 1 && i!=7) {
var ledKug:clsLedKugiri = new clsLedKugiri(":",nX,0,numSiz);
addChild(ledKug);
nX += 10*numSiz/25.0;
}
}
}
//時間の表示
// intTime:ミリ秒
public function fncDispTime(intTime:int):void {
//時
arrNum[0].setVal((int(intTime / (1000 * 60 * 60 * 10)) % 10).toString());
arrNum[1].setVal((int(intTime / (1000 * 60 * 60 )) % 10).toString());
//分
arrNum[2].setVal((int(intTime / (1000 * 60 * 10 )) % 10).toString());
arrNum[3].setVal((int(intTime / (1000 * 60 )) % 10).toString());
//秒
arrNum[4].setVal((int(intTime / (1000 * 10 )) % 10).toString());
arrNum[5].setVal((int(intTime / (1000 )) % 10).toString());
//1/100秒
arrNum[6].setVal((int(intTime / (100 )) % 10).toString());
arrNum[7].setVal((int((intTime+5) / (10 )) % 10).toString());
}
}
//}
//package cls {
//----------------------------------------------
//デジタル数字クラス
// http://programmingatelier.net/
//----------------------------------------------
import flash.display.Sprite;
import flash.events.Event;
//public class clsLedNum extends Sprite {
class clsLedNum extends Sprite {
//デジタル数字形状(7形状*6点)
private const arrPoint1:Array = [
[7,0],[20,0],[23,3],[20,6],[7,6],[4,3],
[21,7],[21,20],[24,23],[27,20],[27,7],[24,4],
[21,28],[21,41],[24,44],[27,41],[27,28],[24,25],
[7,42],[20,42],[23,45],[20,48],[7,48],[4,45],
[0,28],[0,41],[3,44],[6,41],[6,28],[3,25],
[0,7],[0,20],[3,23],[6,20],[6,7],[3,4],
[7,21],[20,21],[23,24],[20,27],[7,27],[4,24]
];
//数字(文字)に対する形状の表示ON(=1)・OFF(=0)
// 16進対応
private const arrFlg:Array = [
{code:"",flg:[0,0,0,0,0,0,0]},
{code:"-",flg:[0,0,0,0,0,0,1]},
{code:"0",flg:[1,1,1,1,1,1,0]},
{code:"1",flg:[0,1,1,0,0,0,0]},
{code:"2",flg:[1,1,0,1,1,0,1]},
{code:"3",flg:[1,1,1,1,0,0,1]},
{code:"4",flg:[0,1,1,0,0,1,1]},
{code:"5",flg:[1,0,1,1,0,1,1]},
{code:"6",flg:[1,0,1,1,1,1,1]},
{code:"7",flg:[1,1,1,0,0,0,0]},
{code:"8",flg:[1,1,1,1,1,1,1]},
{code:"9",flg:[1,1,1,1,0,1,1]},
{code:"A",flg:[1,1,1,0,1,1,1]},
{code:"B",flg:[0,0,1,1,1,1,1]},
{code:"C",flg:[1,0,0,1,1,1,0]},
{code:"D",flg:[0,1,1,1,1,0,1]},
{code:"E",flg:[1,0,0,1,1,1,1]},
{code:"F",flg:[1,0,0,0,1,1,1]},
];
private const numD0:Number = 25.0;
private var numDD:Number; //数字幅
private var numAng:Number; //数字の傾き
private var strCod:String; //表示中の数字(文字)
//デジタル数字
// sCod:数字(文字)"","-","0"-"9","A"-"F"
// nX,nY:位置
// nSiz:数字幅
// nAng:数字の傾き
public function clsLedNum(sCod:String , nX:Number, nY:Number,
nSiz:Number = 25.0, nAng:Number = 10 ) {
strCod = sCod.toUpperCase();
numDD = nSiz/numD0;
numAng = nAng * Math.PI / 180;
this.x = nX;
this.y = nY;
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
DispNum();
}
//表示数字(文字)の変更
// sCod:数字(文字)
public function setVal(sCod:String):void {
strCod = sCod.toUpperCase();
DispNum();
}
//数字(文字)の表示
private function DispNum():void {
graphics.clear();
var no:int = 0; //対象外は””と同じ扱いにする
var i:int;
for (i = 0; i < arrFlg.length; i++) {
if (arrFlg[i].code == strCod) {no = i; }
}
for (i = 0; i < 7; i++) {
var ic:uint = 0x00ff00; //表示ONのとき
var ar:Number =1.0;
if (arrFlg[no].flg[i] == 0) { //表示OFFの時
ic = 0x888888;
ar = 0.2;
}
dispSeg(i, ic,ar);
}
}
//1形状の表示
// no:形状のNO
// ic:カラー
// ar:alpha値
private function dispSeg(no:int,ic:uint,ar:Number):void {
var i:int;
var dx:Number;
graphics.lineStyle(1, ic,0);
graphics.beginFill(ic,ar);
dx = (48 - arrPoint1[no * 6 ][1]) * numAng;
graphics.moveTo((arrPoint1[no*6][0]+dx)*numDD, (arrPoint1[no*6][1])*numDD);
for (i = 1; i < 6; i++) {
dx = (48 - arrPoint1[no * 6 + i][1]) * numAng;
graphics.lineTo((arrPoint1[no*6+i][0]+dx)*numDD, (arrPoint1[no*6+i][1])*numDD);
}
graphics.endFill();
}
}
//}
//package cls {
//----------------------------------------------
//デジタル数字の区切り(":")クラス
// http://programmingatelier.net/
//----------------------------------------------
import flash.display.Sprite;
import flash.events.Event;
//public class clsLedKugiri extends Sprite {
class clsLedKugiri extends Sprite {
//デジタル数字形状(2形状*4点)
private var arrPoint1:Array = [
[0,30],[5,30],[5,35],[0,35],
[0,20],[5,20],[5,15],[0,15]
];
//文字に対する形状の表示ON(=1)・OFF(=0)
private var arrFlg:Array = [
{code:"",flg:[0,0]},
{code:":",flg:[1,1]}
];
private var numD0:Number = 25.0;
private var numDD:Number; //数字幅
private var numAng:Number; //数字の傾き
private var strCod:String; //表示中の数字(文字)
//デジタル数字の区切り
// sCod:文字":",""
// nX,nY:位置
// nSiz:数字幅
// nAng:数字の傾き
public function clsLedKugiri(sCod:String ,
nX:Number, nY:Number, nSiz:Number = 25.0, nAng:Number = 10 ) {
strCod = sCod.toUpperCase();
numDD = nSiz/numD0;
numAng = nAng * Math.PI / 180;
this.x = nX;
this.y = nY;
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
DispKugiri();
}
//文字の変更
// sCod:数字の区切り
public function setVal(sCod:String):void {
strCod = sCod.toUpperCase();
DispKugiri();
}
//数字(文字)の表示
private function DispKugiri():void {
graphics.clear();
var no:int = 0; //対象外は””と同じ扱いにする
var i:int;
for (i = 0; i < arrFlg.length; i++) {
if (arrFlg[i].code == strCod) {no = i; }
}
for (i = 0; i < 2; i++) {
var ic:uint = 0x00ff00; //表示ONのとき
var ar:Number =1.0;
if (arrFlg[no].flg[i] == 0) { //表示OFFの時
ic = 0x888888;
ar = 0.2;
}
dispSeg(i, ic,ar);
}
}
//1形状の表示
// no:形状のNO
// ic:カラー
// ar:alpha値
private function dispSeg(no:int,ic:uint,ar:Number):void {
var i:int;
var dx:Number;
graphics.lineStyle(1, ic,0);
graphics.beginFill(ic,ar);
dx = (48 - arrPoint1[no * 4 ][1]) * numAng;
graphics.moveTo((arrPoint1[no*4][0]+dx)*numDD, (arrPoint1[no*4][1])*numDD);
for (i = 1; i < 4; i++) {
dx = (48 - arrPoint1[no * 4 + i][1]) * numAng;
graphics.lineTo((arrPoint1[no*4+i][0]+dx)*numDD, (arrPoint1[no*4+i][1])*numDD);
}
graphics.endFill();
}
}
//}