/Main.as
http://ddw1997flash-test-swf.googlecode.com/ · ActionScript · 1 lines · 1 code · 0 blank · 0 comment · 0 complexity · 0bff290bcb20a1ad96104a139c5156b8 MD5 · raw file
- /*
* Simple Object Example for Graffiti Library 3.0 Beta 1
* ______________________________________________________________________
* www.nocircleno.com/graffiti/
*/
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package {
import com.nocircleno.graffiti.display.LineObject;
import com.nocircleno.graffiti.display.TextObject;
import com.nocircleno.graffiti.tools.LineTool;
import com.nocircleno.graffiti.tools.LineType;
import com.nocircleno.graffiti.tools.TextTool;
import fl.controls.Button;
import fl.controls.ColorPicker;
import fl.controls.ComboBox;
import fl.data.DataProvider;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.Font;
import flash.text.TextFormat;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import com.nocircleno.graffiti.GraffitiCanvas;
import com.nocircleno.graffiti.display.GraffitiObject;
import com.nocircleno.graffiti.events.CanvasEvent;
import com.nocircleno.graffiti.events.GraffitiObjectEvent;
import com.nocircleno.graffiti.tools.SelectionTool;
import com.nocircleno.graffiti.tools.BrushType;
import com.nocircleno.graffiti.tools.LayerType;
import com.nocircleno.graffiti.tools.ToolRenderType;
import com.nocircleno.graffiti.tools.ToolMode;
import com.nocircleno.graffiti.managers.GraffitiObjectManager;
import com.nocircleno.graffiti.tools.TextSettings;
import com.nocircleno.graffiti.tools.BrushTool;
import com.nocircleno.graffiti.tools.ShapeTool;
import com.nocircleno.graffiti.tools.ShapeType;
import com.nocircleno.graffiti.display.ShapeObject;
import com.nocircleno.graffiti.display.BrushObject;
import com.nocircleno.graffiti.converters.FormatType;
import com.nocircleno.graffiti.tools.EditableParams;
public class Main extends MovieClip {
// ui
public var selection_button:Button;
public var rect_button:Button;
public var brush_button:Button;
public var eclipse_button:Button;
public var line_button:Button;
public var text_button:Button;
public var save_degrafa_button:Button;
public var load_degrafa_button:Button;
public var fill_color_mc:ColorPicker;
public var stroke_color_mc:ColorPicker;
public var font_cb:ComboBox;
public var stroke_multiple_color_icon:Sprite;
public var fill_multiple_color_icon:Sprite;
public var font_multiple_icon:Sprite;
private var _canvas:GraffitiCanvas;
private var _objectManager:GraffitiObjectManager;
private var _brushTool:BrushTool;
private var _selectionTool:SelectionTool;
private var _rectTool:ShapeTool;
private var _eclipseTool:ShapeTool;
private var _lineTool:LineTool;
private var _textTool:TextTool;
private var _fontDataProvider:DataProvider;
private var _textSizeDataProvider:DataProvider;
private var _strokeSizeDataProvider:DataProvider;
private var _saveFileRef:FileReference;
private var _loadFileRef:FileReference;
public function Main() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
// file reference used to save and load object xml files
_saveFileRef = new FileReference();
_loadFileRef = new FileReference();
_loadFileRef.addEventListener(Event.SELECT, selectFile);
_loadFileRef.addEventListener(Event.CANCEL, cancelFile);
// add tool button events
selection_button.addEventListener(MouseEvent.CLICK, toolSelectHandler);
brush_button.addEventListener(MouseEvent.CLICK, toolSelectHandler);
rect_button.addEventListener(MouseEvent.CLICK, toolSelectHandler);
eclipse_button.addEventListener(MouseEvent.CLICK, toolSelectHandler);
line_button.addEventListener(MouseEvent.CLICK, toolSelectHandler);
text_button.addEventListener(MouseEvent.CLICK, toolSelectHandler);
// save and load objects eventss
load_degrafa_button.addEventListener(MouseEvent.CLICK, generateObjects);
// get all fonts on users computer
_fontDataProvider = new DataProvider();
var fonts:Array = Font.enumerateFonts(true);
var numberFonts:int = fonts.length;
for (var i:uint = 0; i < numberFonts; i++) {
_fontDataProvider.addItem( { label:fonts[i].fontName, data:fonts[i] } );
}
// setup font ui
font_cb.dataProvider = _fontDataProvider;
font_cb.addEventListener(Event.CHANGE, textSettingsHandler);
var graffitiContainer:Sprite = new Sprite();
graffitiContainer.x = 0;
graffitiContainer.y = 72;
addChild(graffitiContainer);
// create new instance of graffiti canvas
_canvas = new GraffitiCanvas(stage.stageWidth, stage.stageHeight - 72);
_canvas.addEventListener(GraffitiObjectEvent.SELECT, objectEventHandler);
_canvas.addEventListener(GraffitiObjectEvent.DESELECT, objectEventHandler);
_canvas.addEventListener(GraffitiObjectEvent.ENTER_EDIT, objectEventHandler);
_canvas.addEventListener(GraffitiObjectEvent.EXIT_EDIT, objectEventHandler);
_canvas.addEventListener(GraffitiObjectEvent.MOVE, objectEventHandler);
_canvas.addEventListener(GraffitiObjectEvent.DELETE, objectEventHandler);
graffitiContainer.addChildAt(_canvas, 0);
// hide icons used for multi-value selections
// - this is the easy way out.
stroke_multiple_color_icon.mouseEnabled = false;
stroke_multiple_color_icon.visible = false;
fill_multiple_color_icon.mouseEnabled = false;
fill_multiple_color_icon.visible = false;
font_multiple_icon.visible = false;
font_multiple_icon.mouseEnabled = false;
// get an instance of object manager
_objectManager = GraffitiObjectManager.getInstance();
// get first font in the list to start with
var font:Font = _fontDataProvider.getItemAt(0).data;
// create textformat object to use with creating text
// no need to assign the font to the textformat object, the TextSettings class takes care of this.
var fmt:TextFormat = new TextFormat();
fmt.size = 18;
fmt.color = 0x000000;
// create tool objects
_brushTool = new BrushTool(8, 0x000000, 1, 0, BrushType.DIAMOND, ToolMode.NORMAL, true);
_selectionTool = new SelectionTool();
_rectTool = new ShapeTool(2, 0x000000, 0x000000, 1, 1, ShapeType.RECTANGLE, ToolMode.NORMAL, true);
_eclipseTool = new ShapeTool(2, 0x000000, 0x000000, 1, 1, ShapeType.OVAL, ToolMode.NORMAL, true);
_lineTool = new LineTool(4, 0x000000, 1, LineType.SOLID, null, true);
_textTool = new TextTool(new TextSettings(font, fmt));
// assign the text tool as the active tool used by the Canvas
_canvas.activeTool = _selectionTool;
selection_button.addEventListener(Event.ENTER_FRAME, initApp);
fill_color_mc.addEventListener(Event.CHANGE, colorSelectHandler);
stroke_color_mc.addEventListener(Event.CHANGE, colorSelectHandler);
}
/*********************************************************
Method : initApp()
Purpose : This method is used to disable button because
the flash components suck.
Params : e -- Event object.
**********************************************************/
private function initApp(e:Event):void {
selection_button.removeEventListener(Event.ENTER_FRAME, initApp);
selection_button.enabled = false;
}
/*********************************************************
Method : generateObjects()
Purpose : Method will open browse menu to load
an object xml file.
Params : e -- MouseEvent object.
**********************************************************/
private function generateObjects(e:MouseEvent):void {
_loadFileRef.browse([new FileFilter("All Formats (*.xml)", "*.xml")]);
}
/*********************************************************
Method : cancelFile()
Purpose : Method will handle the cancel event.
Params : e -- Event object.
**********************************************************/
private function cancelFile(e:Event):void {}
/*********************************************************
Method : selectFile()
Purpose : Method will handle the select event.
Params : e -- Event object.
**********************************************************/
private function selectFile(e:Event):void {
_loadFileRef.load();
}
/*********************************************************
Method : colorSelectHandler()
Purpose : Method will handle all color picker events.
Params : e -- Event object.
**********************************************************/
private function colorSelectHandler(e:Event):void {
var selectedColor:uint = e.currentTarget.selectedColor;
if(e.currentTarget == fill_color_mc) {
_objectManager.changeSettingsForSelectedObjects({FillTextColor:selectedColor});
_brushTool.color = selectedColor;
_rectTool.fillColor = selectedColor;
_eclipseTool.fillColor = selectedColor;
_textTool.textSettings.textFormat.color = selectedColor;
fill_multiple_color_icon.visible = false;
} else if(e.currentTarget == stroke_color_mc) {
_rectTool.strokeColor = selectedColor;
_eclipseTool.strokeColor = selectedColor;
_lineTool.color = selectedColor;
_objectManager.changeSettingsForSelectedObjects({StrokeColor:selectedColor});
stroke_multiple_color_icon.visible = false;
}
}
/**************************************************************************
Method : setActiveToolByButton()
Purpose : This method will change tools based on the tool button.
Params : button - Tool Button
***************************************************************************/
private function setActiveToolByButton(button:Button):void {
// set all ui to an active state (cheap way of doing this)
selection_button.enabled = true;
brush_button.enabled = true;
rect_button.enabled = true;
eclipse_button.enabled = true;
line_button.enabled = true;
text_button.enabled = true;
stroke_color_mc.enabled = true;
fill_color_mc.enabled = true;
font_cb.visible = true;
if (button == selection_button) {
// set active tool
_canvas.activeTool = _selectionTool;
// set button states
selection_button.enabled = false;
} else if (button == brush_button) {
// set active tool
_canvas.activeTool = _brushTool;
// set button states
brush_button.enabled = false;
stroke_color_mc.enabled = false;
font_cb.visible = false;
} else if (button == rect_button) {
// set active tool
_canvas.activeTool = _rectTool;
// set button states
rect_button.enabled = false;
font_cb.visible = false;
} else if(button == eclipse_button) {
// set active tool
_canvas.activeTool = _eclipseTool;
// set button states
eclipse_button.enabled = false;
font_cb.visible = false;
} else if(button == line_button) {
// set active tool
_canvas.activeTool = _lineTool;
// set button states
line_button.enabled = false;
fill_color_mc.enabled = false;
font_cb.visible = false;
} else if (button == text_button) {
// get selected font
var font:Font = Font(font_cb.selectedItem.data);
// create textformat object from size and color combo boxes
var fmt:TextFormat = new TextFormat();
fmt.size = 18;
fmt.color = fill_color_mc.selectedColor;
// update text tool settings
_textTool.textSettings = new TextSettings(font, fmt);
// assign text tool
_canvas.activeTool = _textTool;
// set button states
text_button.enabled = false;
stroke_color_mc.enabled = false;
}
}
/**************************************************************************
Method : toolSelectHandler()
Purpose : This method will handle the selection of a new tool.
Params : e - MouseEvent Object
***************************************************************************/
private function toolSelectHandler(e:MouseEvent):void {
setActiveToolByButton(Button(e.currentTarget));
}
/**************************************************************************
Method : textSettingsHandler()
Purpose : This method will handle font seletion in the UI.
Params : e - Event Object
***************************************************************************/
private function textSettingsHandler(e:Event):void {
var ts:TextSettings;
if(e.currentTarget == font_cb) {
if(font_cb.selectedItem.data != null) {
var font:Font = Font(font_cb.selectedItem.data);
_objectManager.changeSettingsForSelectedObjects({Font:font});
ts = _textTool.textSettings;
ts.font = font;
_textTool.textSettings = ts;
font_multiple_icon.visible = false;
}
}
}
/**************************************************************************
Method : objectEventHandler()
Purpose : This method will handle events related to GraffitiObjects.
We want to handle when an object is selected and enters
the edit mode.
Params : e - GraffitiObjectEvent Object
***************************************************************************/
private function objectEventHandler(e:GraffitiObjectEvent):void {
var font:Font;
var fmt:TextFormat;
var i:int;
// text object was selected
if (e.type == GraffitiObjectEvent.SELECT) {
// check to see if multiple objects are selected
if(_objectManager.areMultipleObjectsSelected()) {
// check to see if multiple fill values exist for selected objects
if(_objectManager.areMultipleValuesInSelection(EditableParams.FILL_TEXT_COLOR)) {
fill_multiple_color_icon.visible = true;
} else {
fill_multiple_color_icon.visible = false;
// set swatch color
if (e.graffitiObjects[0] is BrushObject) {
fill_color_mc.selectedColor = BrushObject(e.graffitiObjects[0]).brushDefinition.color;
} else if (e.graffitiObjects[0] is ShapeObject) {
fill_color_mc.selectedColor = ShapeObject(e.graffitiObjects[0]).shapeDefinition.fillColor;
} else if (e.graffitiObjects[0] is TextObject) {
fill_color_mc.selectedColor = int(TextObject(e.graffitiObjects[0]).textSetting.textFormat.color);
}
}
// check to see if multiple stroke values exist for selected objects
if(_objectManager.areMultipleValuesInSelection(EditableParams.STROKE_COLOR)) {
stroke_multiple_color_icon.visible = true;
} else {
stroke_multiple_color_icon.visible = false;
// set swatch color
if (e.graffitiObjects[0] is LineObject) {
fill_color_mc.selectedColor = LineObject(e.graffitiObjects[0]).lineDefinition.strokeColor;
} else if (e.graffitiObjects[0] is ShapeObject) {
fill_color_mc.selectedColor = ShapeObject(e.graffitiObjects[0]).shapeDefinition.strokeColor;
}
}
// check to see if multiple fonts exist for selected objects
if(_objectManager.areMultipleValuesInSelection(EditableParams.FONT)) {
font_multiple_icon.visible = true;
} else {
font_multiple_icon.visible = false;
}
// NOTE: you can check for multiple for all values in the EditableParams Class.
} else {
// update UI based on what was selected
if (e.graffitiObjects[0] is BrushObject) {
fill_color_mc.selectedColor = BrushObject(e.graffitiObjects[0]).brushDefinition.color;
} else if (e.graffitiObjects[0] is ShapeObject) {
stroke_color_mc.selectedColor = ShapeObject(e.graffitiObjects[0]).shapeDefinition.strokeColor;
fill_color_mc.selectedColor = ShapeObject(e.graffitiObjects[0]).shapeDefinition.fillColor;
} else if (e.graffitiObjects[0] is LineObject) {
stroke_color_mc.selectedColor = LineObject(e.graffitiObjects[0]).lineDefinition.strokeColor;
} else if (e.graffitiObjects[0] is TextObject) {
font = TextObject(e.graffitiObjects[0]).textSetting.font;
fmt = TextObject(e.graffitiObjects[0]).textSetting.textFormat;
var fontFromList:Font;
var numberFonts:uint = _fontDataProvider.length;
// find select text font in font list
for (i = 0; i < numberFonts; i++) {
fontFromList = Font(_fontDataProvider.getItemAt(i).data);
if (fontFromList.fontName == font.fontName && fontFromList.fontStyle == font.fontStyle) {
font_cb.selectedIndex = i;
break;
}
}
// if text tool is current tool then update settings
if (_canvas.activeTool is TextTool) {
TextTool(_canvas.activeTool).textSettings = new TextSettings(font, fmt);
}
fill_color_mc.selectedColor = uint(TextObject(e.graffitiObjects[0]).textSetting.textFormat.color);
}
}
} else if(e.type == GraffitiObjectEvent.MOVE) {
} else if(e.type == GraffitiObjectEvent.DESELECT) {
if(_objectManager.areMultipleValuesInSelection(EditableParams.FILL_TEXT_COLOR)) {
fill_multiple_color_icon.visible = true;
} else {
fill_multiple_color_icon.visible = false;
}
if(_objectManager.areMultipleValuesInSelection(EditableParams.STROKE_COLOR)) {
stroke_multiple_color_icon.visible = true;
} else {
stroke_multiple_color_icon.visible = false;
}
if(_objectManager.areMultipleValuesInSelection(EditableParams.FONT)) {
font_multiple_icon.visible = true;
} else {
font_multiple_icon.visible = false;
}
} else if (e.type == GraffitiObjectEvent.ENTER_EDIT) {
// the user can double click on a TextObject to enter edit mode
// this will automatically set the text tool to selected state.
if (e.graffitiObjects[0] is TextObject && !(_canvas.activeTool is TextTool)) {
setActiveToolByButton(text_button);
}
} else if(e.type == GraffitiObjectEvent.DELETE) {
}
}
}
}