Skip to content

Commit

Permalink
Cleaned up code, added HBox and VBox
Browse files Browse the repository at this point in the history
  • Loading branch information
GimmickNG authored Aug 10, 2018
1 parent 70bf103 commit 02efa29
Show file tree
Hide file tree
Showing 49 changed files with 4,268 additions and 1,530 deletions.
179 changes: 56 additions & 123 deletions src/syncomps/CheckBox.as
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package syncomps
{
import flash.accessibility.AccessibilityProperties;
import flash.display.CapsStyle;
import flash.display.Graphics;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.ui.Keyboard;
import syncomps.events.StyleEvent;
import syncomps.interfaces.IAutoResize;
import syncomps.interfaces.ILabel;
import syncomps.interfaces.graphics.IAutoResize;
import syncomps.interfaces.graphics.ILabel;
import syncomps.styles.DefaultLabelStyle;
import syncomps.styles.Style;
import syncomps.styles.DefaultStyle;
import syncomps.styles.StyleManager;

Expand All @@ -34,31 +30,27 @@ package syncomps
public static const DEF_HEIGHT:int = 32;
protected static var DEFAULT_STYLE:Class = DefaultLabelStyle

private var b_dispatchClick:Boolean;
private var b_selected:Boolean;
private var tf_label:SkinnableTextField;
private var shp_graphic:Shape;
private var b_dispatchClick:Boolean;
private var cmpi_label:Label;

public function CheckBox()
{
public function CheckBox() {
init()
}

private function init():void
{
shp_graphic = new Shape()
tf_label = new SkinnableTextField()
StyleManager.unregister(tf_label)
addChild(tf_label)
addChild(shp_graphic)
cmpi_label = new Label()
StyleManager.unregister(cmpi_label)

addChild(cmpi_label)

drawGraphics(DEF_WIDTH, DEF_HEIGHT, DefaultStyle.BACKGROUND);
tf_label.selectable = tf_label.multiline = tf_label.mouseEnabled = false;
selected = false //warning: these 3 lines redraws the graphics,
label = null //so make sure width and height are preinitialized

styleDefinition.addEventListener(StyleEvent.STYLE_CHANGE, updateStyles, false, 0, true)
accessibilityProperties = new AccessibilityProperties()

addEventListener(MouseEvent.CLICK, toggleButton, false, 0, true);
addEventListener(MouseEvent.MOUSE_UP, changeState, false, 0, true)
addEventListener(MouseEvent.ROLL_OUT, changeState, false, 0, true)
Expand All @@ -69,12 +61,13 @@ package syncomps
addEventListener(MouseEvent.RELEASE_OUTSIDE, changeState, false, 0, true)
addEventListener(KeyboardEvent.KEY_UP, dispatchClickEvent, false, 0, true)
addEventListener(KeyboardEvent.KEY_DOWN, startDispatchClickEvent, false, 0, true)
addEventListener(StyleEvent.STYLE_CHANGE, updateStyles, false, 0, true)
}

private function updateStyles(evt:StyleEvent):void
{
tf_label.setStyle(evt.style, evt.value)
drawGraphics(width, height, str_state)
cmpi_label.setStyle(evt.style, evt.value)
drawGraphics(width, height, state)
}

private function changeState(evt:Event):void
Expand Down Expand Up @@ -102,9 +95,9 @@ package syncomps
if (b_dispatchClick)
{
resetButton()
dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false))
b_dispatchClick = false
toggleButton(null)
}
b_dispatchClick = false
}

private function startDispatchClickEvent(evt:KeyboardEvent):void
Expand All @@ -124,29 +117,14 @@ package syncomps
DEFAULT_STYLE = styleClass
}

override public function set width(value:Number):void
{
drawGraphics(value, height, str_state)
}

override public function set height(value:Number):void
{
drawGraphics(width, value, str_state)
}

public function set label(text:String):void
{
if (text && text.length) {
tf_label.text = text;
}
else {
tf_label.text = ""
}
drawGraphics(width, height, str_state)
cmpi_label.label = text
drawGraphics(width, height, state)
}

public function get label():String {
return tf_label.text
return cmpi_label.label
}

public function set labelPosition(position:int):void {
Expand All @@ -158,7 +136,7 @@ package syncomps
}

public function get textField():TextField {
return tf_label
return cmpi_label.textField
}

override public function unload():void
Expand All @@ -177,27 +155,32 @@ package syncomps

public function resizeWidth():void
{
tf_label.autoSize = TextFieldAutoSize.LEFT
var widthVal:Number = tf_label.width
switch(labelPosition)
{
case DefaultLabelStyle.LABEL_RIGHT:
case DefaultLabelStyle.LABEL_LEFT:
widthVal += 32;
break;
}
tf_label.autoSize = TextFieldAutoSize.NONE
width = widthVal
cmpi_label.resizeWidth()
drawGraphics(width, height, state)
}

public function resizeHeight():void
{
cmpi_label.resizeHeight()
drawGraphics(width, height, state)
}

public function resizeHeight():void {
height = tf_label.textHeight + 16
/* DELEGATE syncomps.Label */

public function get iconSize():int {
return cmpi_label.iconSize;
}

public function set iconSize(value:int):void
{
cmpi_label.iconSize = value;
drawGraphics(width, height, state)
}

private function toggleButton(evt:MouseEvent):void
{
selected = !selected
drawGraphics(width, height, str_state);
drawGraphics(width, height, state);
dispatchEvent(new Event(Event.CHANGE, false, false))
}

Expand All @@ -224,87 +207,37 @@ package syncomps
{
super.drawGraphics(width, height, state)
const shapeGraphics:Graphics = shp_graphic.graphics
var xCenter:Number, yCenter:Number, size:Number, colourAlpha:Number;
var colour:uint = uint(getStyle(state)), checkHeight:int = height - 16
var color:uint = uint(getStyle(state)), checkHeight:int = height - 16
var size:Number = cmpi_label.iconSize / 2, colorAlpha:Number;
if(!enabled) {
colour = uint(getStyle(DefaultStyle.DISABLED))
}
colourAlpha = ((colour & 0xFF000000) >>> 24) / 0xFF;
colour = colour & 0x00FFFFFF
var textHeight:int = tf_label.textHeight
if(checkHeight < textHeight - 8) {
checkHeight = textHeight - 8
}
switch(labelPosition)
{
case DefaultLabelStyle.LABEL_RIGHT:
case DefaultLabelStyle.LABEL_LEFT:
tf_label.height = height - 4
if((tf_label.height < textHeight && textHeight <= height) || (tf_label.textHeight && tf_label.height >= textHeight)) {
tf_label.height = textHeight
}
yCenter = height * 0.5;
size = checkHeight * 0.5
tf_label.y = 0
tf_label.y = (height - tf_label.height) * 0.5;
break;
case DefaultLabelStyle.LABEL_BELOW:
case DefaultLabelStyle.LABEL_ABOVE:
size = checkHeight * 0.3
tf_label.height = height - (size * 2)
if((tf_label.height < textHeight && textHeight <= height) || (tf_label.textHeight && tf_label.height >= textHeight)) {
tf_label.height = textHeight
}
xCenter = width * 0.5;
tf_label.width = width;
break;
}
switch(labelPosition)
{
case DefaultLabelStyle.LABEL_RIGHT:
xCenter = size + 2;
tf_label.x = (xCenter + size) + 4
tf_label.width = width - tf_label.x
break;
case DefaultLabelStyle.LABEL_LEFT:
tf_label.x = 0
xCenter = width - (size + 2)
tf_label.width = width - ((size * 2) + 4)
break;
case DefaultLabelStyle.LABEL_BELOW:
tf_label.y = height - tf_label.height
tf_label.x = 0;
yCenter = size;
break;
case DefaultLabelStyle.LABEL_ABOVE:
tf_label.y = tf_label.x = 0;
yCenter = height - size
break;
default:
throw new Error("Invalid label position.")
break;
color = uint(getStyle(DefaultStyle.DISABLED))
}
colorAlpha = ((color & 0xFF000000) >>> 24) / 0xFF;
color = color & 0x00FFFFFF

//outer border (invisible)
shapeGraphics.clear()
shapeGraphics.beginFill(0, 0)
shapeGraphics.drawRect(0, 0, width, height)
shapeGraphics.endFill()
graphics.clear()
graphics.beginFill(0, 0)
graphics.drawRect(0, 0, width, height)
graphics.endFill()

//checkbox
shapeGraphics.clear()
shapeGraphics.lineStyle(1)
shapeGraphics.beginFill(colour, colourAlpha)
shapeGraphics.drawRect((xCenter - size), (yCenter - size), (size * 2), (size * 2))
shapeGraphics.beginFill(color, colorAlpha)
shapeGraphics.drawRect(0, 0, (size * 2), (size * 2))
shapeGraphics.endFill()
if (b_selected)
{
//draw tick
shapeGraphics.lineStyle(2, 0, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE, JointStyle.BEVEL, 5)
shapeGraphics.moveTo((xCenter + 1) - size, (yCenter - 1) + (size * 0.5))
shapeGraphics.lineTo((xCenter + 1) - (size * 0.5), (yCenter - 1) + size)
shapeGraphics.lineTo((xCenter - 1) + size, (yCenter + 1) - size)
shapeGraphics.endFill()
shapeGraphics.moveTo(1, (size - 1) + (size * 0.5))
shapeGraphics.lineTo((size + 1) - (size * 0.5), (size * 2) - 1)
shapeGraphics.lineTo((size * 2) - 1, 1)
}
cmpi_label.icon = shp_graphic
cmpi_label.height = height
cmpi_label.width = width
}
}

Expand Down
Loading

0 comments on commit 02efa29

Please sign in to comment.