Aquí os dejo una clase/componente que tuve que hacer para un proyecto, es una clase para mostrar mensajes en pantalla al estilo alert de flex pero para flash, para hacerla me inspire en lo poco que recordaba de cuando programaba en visual Basic sobre las ventanas msgBox, vbBox.
La clase es un poco chapucera porque la hice con prisas pero a lo mejor a alguien le sirve y si le pone ganas la dejará bien arregladita.
Esta clase requiere un botón y la fuente arial agregadas en la librería para funcionar correctamente además de los gráficos que representan la ventana.
Dentro de la clase encontraremos el método msg que admite 8 parámetros
public function msg(functionRecived=null,functionRecived2=null,MsgBoxStyle=»OkOnly»,recivedText=»»,Title = «Alert», scale_X = 600, Align = 0,pos_Y=200):void {
Los 2 primeros en el caso de no ser necesarios lo pondremos como null obligatoriamente de lo contrario indicaremos el nombre de una función o las dos.
– En el primer parametro functionRecived=null, si le pasamos el nombre de una función le estamos diciendo que cuando pulse el botón aceptar ejecute dicha función antes de que se cierre la ventana msgBox, si no queremos ejecutar ninguna función al pulsar el botón entonces pasamos como parámetro el valor null
– El segundo parámetro functionRecived2=null, es lo mismo que el primero pero podemos especificar en el una función que queramos llamar cuando la ventana muestre 2 botones y pulsemos en cancelar por ejemplo
– El tercer parámetro MsgBoxStyle=»OKOnly» especifica que botones se vana mostrar:
«OKOnly» muestra el botón aceptar únicamente.
«OKCancel» muestra los 2 botones aceptar y cancelar.
«OKClose» muestra 1 botón cerrar.
«YesNo» muestra 2 botones SI, NO.
«RetryCancel» muestra 2 botones REINTENTAR, CANCELAR.
«SaveQuit» muestra 2 botones Salir y guardar»,»Salir sin guardar.
«Information» no muestra ningún botón, solo muestra el mensaje.
Si enviamos el tercer parámetro como «Information» deberemos cerrar después la ventana msgBox manualmente con msgBox.msgBoxClose();.
– El cuarto parámetro recivedText=»», debe contener un String que será el mensaje a mostrar tanto en formato TXT o HTML.
– El quinto parámetro Title = «Alert», es el titulo de la ventana.
– El sesto parámetro scale_X = 600, es la anchura que queramos que tenga la ventana, la altura no es necesario especificarla porque se ajusta automáticamente dependiendo de la longitud del texto.
– El séptimo parámetro Align = 0, indicamos la alineación del texto 0 para centro y 1 para izquierda.
– El octavo y ultimo parámetro pos_Y=200, podemos indicar la posición de la ventana en Y manualmente, la posición en X es automática.
Codigo de la clase vbBox5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
package { import flash.display.*; import flash.events.*; import fl.controls.*; import fl.data.*; import flash.text.*; //----------------------------------- public class vbBox5 extends MovieClip { public var msgBox:MsgBox = new MsgBox(); public var mcBG:MovieClip; var btn_Aceptar:Button; var btn_Cancelar:Button; private var dinamicFunction:Object; private var dinamicFunction2:Object; var posIniX:int; var posIniY:int; var scale_X:int; var Align:int; var pos_Y:int; var stageLoaded:Boolean=false; var THIS:Object; var btnMarginY:int=45; var _stageWidth:int; private var doubleButton:Boolean; //------------------------------- public function vbBox5(_THIS:Object = null, stageWidth:int= 400) { _stageWidth=stageWidth; doubleButton=false; if(_THIS == null){ THIS=this; }else{ THIS=_THIS; } this.addEventListener(Event.ADDED_TO_STAGE,addSTAGE); //creamos el mc contenedor mcBG=new MovieClip(); msgBox.y=200; THIS.addChild(mcBG); addChild(msgBox); //btn_Aceptar btn_Aceptar=new Button(); btn_Aceptar.height=22; btn_Aceptar.label="ACEPTAR"; msgBox.addChild(btn_Aceptar); //btn_Cancelar btn_Cancelar=new Button(); btn_Cancelar.height=22; btn_Cancelar.label="CANCELAR"; msgBox.addChild(btn_Cancelar); //--------------------------------------- msgBoxClose(); }//End vbBox Constructor //-------------------------------------------------------------------------dinamicFunctionES public function msgBoxClose():void { msgBox.visible=false; mcBG.visible=false; } //------------------------------- function msgBoxOpen():void { THIS.addChild(mcBG); THIS.addChild(msgBox); msgBox.visible=true; mcBG.visible=true; } //------------------------------------------- public function msg(functionRecived=null,functionRecived2=null,MsgBoxStyle="OkOnly",recivedText="",Title = "Alert", scale_X = 600, Align = 0,pos_Y=200):void { var format=msgBox.output.getTextFormat(); if (Align>0) { format.align="left"; } else { format.align="center"; } msgBox.output.type=TextFieldType.DYNAMIC; msgBox.output.multiline=true; msgBox.output.autoSize=TextFieldAutoSize.LEFT; msgBox.output.wordWrap=true; //msgBox.output.border = true; msgBox.output.antiAliasType=AntiAliasType.ADVANCED; msgBox.output.gridFitType=GridFitType.PIXEL; msgBox.output.selectable=false; msgBox.output.htmlText=recivedText; msgBox.output.width=scale_X-10; msgBox.output.y=msgBox.titulobg.height+10; msgBox.output.x = (scale_X/2) - (msgBox.output.width/2); msgBox.titulo.htmlText=Title; msgBox.titulobg.addEventListener(MouseEvent.MOUSE_DOWN,dragging); msgBox.btnClose.addEventListener(MouseEvent.CLICK,Close); msgBox.titulo.mouseEnabled=false; msgBox.titulobg.buttonMode=true; msgBox.btnClose.buttonMode=true; this.scale_X=scale_X; this.pos_Y=pos_Y; msgBox.output.setTextFormat(format); if (MsgBoxStyle=="Information") { btn_Aceptar.visible=false; btn_Cancelar.visible=false; } if (MsgBoxStyle=="OKOnly") { btn_Aceptar.addEventListener(MouseEvent.CLICK,Acept); btn_Aceptar.label="ACEPTAR"; btn_Aceptar.visible=true; btn_Cancelar.visible=false; doubleButton=false; } if (MsgBoxStyle=="OKClose") { btn_Aceptar.addEventListener(MouseEvent.CLICK,Acept); btn_Aceptar.label="CERRAR"; btn_Aceptar.visible=true; btn_Cancelar.visible=false; doubleButton=false; } if (MsgBoxStyle=="OKCancel") { groupBtn("ACEPTAR","CANCELAR"); doubleButton=true; } if (MsgBoxStyle=="YesNo") { groupBtn("SI","NO"); doubleButton=true; } if (MsgBoxStyle=="RetryCancel") { groupBtn("REINTENTAR","CANCELAR"); doubleButton=true; } if (MsgBoxStyle=="SaveQuit") { groupBtn("Salir y guardar","Salir sin guardar"); doubleButton=true; } setProperti(); this.dinamicFunction=functionRecived; this.dinamicFunction2=functionRecived2; msgBoxOpen(); mcBG.graphics.clear(); mcBG.graphics.beginFill(0xE6E6E6,0.50); //mcBG.graphics.lineStyle(2, 0x000000,0.5); mcBG.graphics.drawRoundRect(0,0,1008,1440,15,15); mcBG.graphics.endFill(); } //------------------------------- function groupBtn(lbAc,lbCa):void { btn_Aceptar.label=lbAc; btn_Cancelar.label=lbCa; btn_Cancelar.addEventListener(MouseEvent.CLICK,Cancel); btn_Aceptar.addEventListener(MouseEvent.CLICK,Acept); btn_Aceptar.visible=true; btn_Cancelar.visible=true; } //------------------------------- function Acept(e:Event=null):void { delListButtons(); msgBoxClose(); if (dinamicFunction!=null) { dinamicFunction(); } } //------------------------------- function Cancel(e:Event=null):void { delListButtons(); msgBoxClose(); if (dinamicFunction2!=null) { dinamicFunction2(); } } function Close(e:Event=null):void { delListButtons(); msgBoxClose(); } //------------------------------- public function delListButtons():void { btn_Aceptar.removeEventListener(MouseEvent.CLICK,Acept); btn_Cancelar.removeEventListener(MouseEvent.CLICK,Cancel); msgBox.titulobg.removeEventListener(MouseEvent.MOUSE_DOWN,dragging); msgBox.btnClose.removeEventListener(MouseEvent.CLICK,Close); } //------------------------------- function dragging(event:MouseEvent):void { stage.addEventListener(MouseEvent.MOUSE_MOVE, follow); msgBox.titulobg.addEventListener(MouseEvent.MOUSE_UP,dropping); posIniX=event.stageX-msgBox.x; posIniY=event.stageY-msgBox.y; THIS.addChild(mcBG); THIS.addChild(msgBox); } //------------------------------------------- function follow(event:MouseEvent) { msgBox.x=event.stageX-posIniX; msgBox.y=event.stageY-posIniY; } //------------------------------------------- function dropping(e:Event):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, follow); msgBox.titulobg.removeEventListener(MouseEvent.MOUSE_UP,dropping); } //------------------------------------------- function addSTAGE(e:Event):void { stageLoaded=true; THIS.addChild(mcBG); THIS.addChild(msgBox); removeEventListener(Event.ADDED_TO_STAGE,addSTAGE); stRez(); } //------------------------------------------- function stRez(e:Event = null):void { if (stageLoaded) { //msgBox.y = (msgBox.stage.stageHeight/2)-(msgBox.height / 2); msgBox.x = (msgBox.stage.stageWidth / 2) - (msgBox.width / 2); msgBox.y=pos_Y; } } //------------------------------------------- function setProperti():void { stRez(); msgBox.fondo.width=scale_X; msgBox.titulobg.width=scale_X; msgBox.btnClose.x=msgBox.titulobg.width-(msgBox.btnClose.width+3); msgBox.titulo.width=scale_X-(msgBox.icono.width+msgBox.btnClose.width); msgBox.titulo.x = msgBox.icono.x + (msgBox.icono.width+3); msgBox.fondo.height=msgBox.output.height+(btnMarginY+msgBox.titulobg.height); //msgBox.output.y=5; if (doubleButton==true) { btn_Aceptar.x = (scale_X/2) - ((btn_Aceptar.width +15+btn_Cancelar.width)/2); btn_Cancelar.x = btn_Aceptar.x + (btn_Aceptar.width+15); btn_Aceptar.y = (msgBox.output.height+btnMarginY); btn_Cancelar.y=btn_Aceptar.y; } else { btn_Aceptar.x = (msgBox.width/2) - (btn_Aceptar.width/2); btn_Aceptar.y = (msgBox.output.height+btnMarginY); } stRez(); } //------------------------------------------- }//End Class }//End Package |
Ejemplo de uso de la clase en el stage
1 2 3 4 5 6 7 8 9 10 |
import clas.vbBox5; var msgBox:vbBox5 = new vbBox5(); addChild(msgBox); function saludar():void{ trace("hola"); } msgBox.msg(saludar,null,"OKOnly","HOLA MUNDO","Saludo",300); |
Ejemplo:
[download id=»11″ format=»2″]
2 comments on “AS3 – Útiles – Componente / Clase Alert vbBox MsgBox en Action Script 3 para flash”
Hola,
Excelente trabajo. Solo una nota (ya se que no está acabado). el html no funciona porque lo de ‘var format’ debe ir después de crear msgbox.output, o sea:
ponerlo después de:
Si te parece lo voy a modificar para que acepte un «input» del usuario como una opción mas.
Hola Jose, ya ni recuerdo porque razón lo puse en ese orden, sé que me encontré con algún problema raro con el tema del html pero con las prisas y para el uso que le iba a dar, me bastaba con que me funcionara con texto normal, puedes hacerle todas las modificaciones que quieras, otra de las cosas pendientes que tiene esta clase es poder añadirle otra opción en msgBoxStyle para mostrar 3 botones. Si algún día tengo tiempo intentare hacer otra clase nueva que se parezca a la clase msgBox del lenguaje c# Sharp que me pareció muy versátil.
Bueno me alegro que te sirva la clase, por cierto si le realizas mejoras substanciales te animo a pasarme el código para compartirlo y lo subiré al blog gustosamente bajo tus créditos.
Namaste!