Browse files

Add blocks with ctrl-click in editor (can't change their properties y…

…et), added focus tracking system to editor.
  • Loading branch information...
1 parent c1a39ea commit 6098bb7536cbf5973728e53b704e86312c56f26c @RileyA committed Apr 29, 2012
Showing with 125 additions and 31 deletions.
  1. +23 −1 src/Editor/BlockProxy.as
  2. +6 −0 src/Editor/EditorProxy.as
  3. +69 −22 src/Editor/EditorState.as
  4. +25 −6 src/Editor/PlayerProxy.as
  5. +2 −2 src/Editor/Scalable.as
View
24 src/Editor/BlockProxy.as
@@ -4,10 +4,12 @@ package Editor {
import Box2D.Dynamics.*;
import Box2D.Collision.Shapes.*;
import Box2D.Collision.*;
+ import flash.display.Shape;
+ import flash.display.LineScaleMode;
/** A scalable containing a block, lets you take a block and scale
it and drag it around and such */
- public class BlockProxy extends Scalable {
+ public class BlockProxy extends Scalable implements EditorProxy {
private var m_child:Block;
@@ -52,5 +54,25 @@ package Editor {
m_child.getInfo().position.y = pos.y + m_child.getInfo().scale.y / 2;
m_child.reinit();
}
+
+ public function gainFocus():void {
+ m_children[0].alpha = 0.7;
+ var tmp:Shape = m_children[0].getChildAt(0) as Shape;
+ tmp.graphics.clear();
+ tmp.graphics.lineStyle(3.0,0x99cc99,1.0,false,LineScaleMode.NONE);
+ tmp.graphics.beginFill(0xaaffaa);
+ tmp.graphics.drawRect(0, 0, m_scale_x, m_scale_y);
+ tmp.graphics.endFill();
+ }
+
+ public function loseFocus():void {
+ m_children[0].alpha = 0.2;
+ var tmp:Shape = m_children[0].getChildAt(0) as Shape;
+ tmp.graphics.clear();
+ tmp.graphics.lineStyle(3.0,0xcc9999,1.0,false,LineScaleMode.NONE);
+ tmp.graphics.beginFill(0xffaaaa);
+ tmp.graphics.drawRect(0, 0, m_scale_x, m_scale_y);
+ tmp.graphics.endFill();
+ }
}
}
View
6 src/Editor/EditorProxy.as
@@ -0,0 +1,6 @@
+package Editor {
+ public interface EditorProxy {
+ function loseFocus():void;
+ function gainFocus():void;
+ }
+}
View
91 src/Editor/EditorState.as
@@ -29,32 +29,41 @@ package Editor {
private var m_pauseKey:Boolean;
private var m_resetKey:Boolean;
- private var m_saveKey:Boolean;
private var m_paused:Boolean;
private var m_levelInfo:LevelInfo;
private var m_levelLoaded:Boolean;
private var m_loadRef:FileReference;
private var m_menu:EditorMenu;
+ private var m_focused:EditorProxy;
+
private var m_levelSprite:Sprite;
public function EditorState(game:Game):void {
super(game);
m_pauseKey = true;
m_resetKey = true;
- m_saveKey = true;
m_paused = true;
}
override public function init():void {
/** create editor UI stuffs */
m_levelLoaded = false;
m_levelSprite = new Sprite();
+ // add something clickable
+ var s:Shape = new Shape();
+ s.alpha = 0.0;
+ s.graphics.beginFill(0x000000);
+ s.graphics.drawRect(-800,-600,2400,1800);
+ s.graphics.endFill();
+ m_levelSprite.addChild(s);
addChild(m_levelSprite);
m_menu = new EditorMenu("N/A");
addChild(m_menu);
+ addEventListener(MouseEvent.CLICK, addBlock);
+ addEventListener(MouseEvent.MOUSE_DOWN, handleFocus);
m_menu.saveButton.addEventListener(MouseEvent.CLICK, save);
m_menu.loadButton.addEventListener(MouseEvent.CLICK, load);
m_menu.newButton.addEventListener(MouseEvent.CLICK, newLevel);
@@ -63,12 +72,19 @@ package Editor {
}
private function unloadLevel():void {
+ m_focused = null;
m_levelSprite.x = 0;
m_levelSprite.y = 0;
m_level = null;
while (m_levelSprite.numChildren > 0)
m_levelSprite.removeChildAt(0);
m_levelLoaded = false;
+ var s:Shape = new Shape();
+ s.alpha = 0.0;
+ s.graphics.beginFill(0x000000);
+ s.graphics.drawRect(-800,-600,2400,1800);
+ s.graphics.endFill();
+ m_levelSprite.addChild(s);
}
public function loadLevel(info:LevelInfo):void {
@@ -132,33 +148,64 @@ package Editor {
m_resetKey = false;
}
- /*if (!m_saveKey && Keys.isKeyPressed(SAVE_KEY)) {
- // make a new block
- var info:BlockInfo = new BlockInfo();
- info.scale.x = 1;
- info.scale.y = 1;
- info.position.x = mouseX / m_level.pixelsPerMeter;
- info.position.y = mouseY / m_level.pixelsPerMeter;
- info.movement = "free";
-
- var newBlock:Block = new Block(info, m_level);
- m_level.addBlock(newBlock);
- newBlock.updateTransform(m_level.pixelsPerMeter);
- var proxy:BlockProxy = new BlockProxy(newBlock);
- m_blocks.push(proxy);
- m_levelSprite.addChild(proxy);
- m_saveKey = true;
- } else if (!Keys.isKeyPressed(SAVE_KEY)) {
- m_saveKey = false;
- }*/
-
m_level.update(delta);
}
return !Keys.isKeyPressed(Keyboard.ESCAPE);
}
+ public function addBlock(e:MouseEvent):void {
+ if (!m_levelLoaded) return;
+ if (e.target == m_levelSprite
+ && Keys.isKeyPressed(Keyboard.CONTROL)) {
+
+ // make a new block, default 1x1 meter, fixed
+ var info:BlockInfo = new BlockInfo();
+ info.scale.x = 1;
+ info.scale.y = 1;
+ info.position.x = mouseX / m_level.pixelsPerMeter;
+ info.position.y = mouseY / m_level.pixelsPerMeter;
+ info.movement = "fixed";
+ info.insulated = false;
+ info.strong = false;
+
+ var newBlock:Block = new Block(info, m_level);
+ m_level.addBlock(newBlock);
+ newBlock.updateTransform(m_level.pixelsPerMeter);
+ var proxy:BlockProxy = new BlockProxy(newBlock);
+ m_blocks.push(proxy);
+ m_levelSprite.addChild(proxy);
+
+ if (m_focused) {
+ m_focused.loseFocus();
+ }
+ m_focused = proxy;
+ m_focused.gainFocus();
+ } else {
+ handleFocus(e);
+ }
+ }
+
+ public function handleFocus(e:MouseEvent):void {
+ if (!m_levelLoaded) return;
+ if (e.target.parent is EditorProxy
+ || e.target is EditorProxy) {
+ var tmp:EditorProxy = e.target.parent is EditorProxy ?
+ e.target.parent as EditorProxy : e.target as EditorProxy;
+ if (m_focused) {
+ m_focused.loseFocus();
+ }
+ m_focused = tmp;
+ m_focused.gainFocus();
+ } else if(e.target == m_levelSprite) {
+ if (m_focused) {
+ m_focused.loseFocus();
+ }
+ m_focused = null;
+ }
+ }
+
public function save(e:MouseEvent):void {
e.stopPropagation();
if (m_levelLoaded) {
View
31 src/Editor/PlayerProxy.as
@@ -8,7 +8,7 @@ package Editor {
/** A scalable containing a block, lets you take a block and scale
it and drag it around and such */
- public class PlayerProxy extends Draggable {
+ public class PlayerProxy extends Draggable implements EditorProxy {
private var m_child:Player;
@@ -20,10 +20,7 @@ package Editor {
override public function reposition():void {
super.reposition();
-
- // x,y + m_children[0].x,y are coords
var pos:UVec2 = new UVec2(x, y);
-
pos.x /= m_child.scaleX;
pos.y /= m_child.scaleY;
pos.x += Player.WIDTH/2;
@@ -35,8 +32,8 @@ package Editor {
scale_x:Number, scale_y:Number):void {
var tmp:Shape = new Shape();
alpha = 0.4;
- tmp.graphics.lineStyle(3.0,0x66cc66,1.0,false,LineScaleMode.NONE);
- tmp.graphics.beginFill(0x88ff88);
+ tmp.graphics.lineStyle(3.0,0xcc9999,1.0,false,LineScaleMode.NONE);
+ tmp.graphics.beginFill(0xffaaaa);
tmp.graphics.drawRect(0, 0, scale_x, scale_y);
tmp.graphics.endFill();
addChild(tmp);
@@ -57,5 +54,27 @@ package Editor {
pos.x += Player.WIDTH/2;
return pos;
}
+
+ public function gainFocus():void {
+ alpha = 0.7;
+ var tmp:Shape = getChildAt(0) as Shape;
+ tmp.graphics.clear();
+ tmp.graphics.lineStyle(3.0,0x99cc99,1.0,false,LineScaleMode.NONE);
+ tmp.graphics.beginFill(0xaaffaa);
+ tmp.graphics.drawRect(0, 0, Player.WIDTH * m_child.scaleX,
+ Player.HEIGHT * m_child.scaleY);
+ tmp.graphics.endFill();
+ }
+
+ public function loseFocus():void {
+ alpha = 0.2;
+ var tmp:Shape = getChildAt(0) as Shape;
+ tmp.graphics.clear();
+ tmp.graphics.lineStyle(3.0,0xcc9999,1.0,false,LineScaleMode.NONE);
+ tmp.graphics.beginFill(0xffaaaa);
+ tmp.graphics.drawRect(0, 0, Player.WIDTH * m_child.scaleX,
+ Player.HEIGHT * m_child.scaleY);
+ tmp.graphics.endFill();
+ }
}
}
View
4 src/Editor/Scalable.as
@@ -10,8 +10,8 @@ package Editor {
private var m_drag:Number = 0;
private var m_anchor:Number = 0;
private var m_dragging:Boolean = false;
- private var m_scale_x:Number;
- private var m_scale_y:Number;
+ protected var m_scale_x:Number;
+ protected var m_scale_y:Number;
protected var m_children:Vector.<Sprite>;
public var m_scalepx_x:Number;
public var m_scalepx_y:Number;

0 comments on commit 6098bb7

Please sign in to comment.