Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Prototyped serialization of a pattern.

  • Loading branch information...
commit fecdd06ee95decd9f78a38f3c52e74ac4643a0fe 1 parent 8045c60
@arahlf authored
View
2  index.html
@@ -14,6 +14,8 @@
<script type="text/javascript" src="src/js/LazySusan.js"></script>
<script type="text/javascript" src="src/js/WoodType.js"></script>
<script type="text/javascript" src="src/js/WoodTypes.js"></script>
+ <script type="text/javascript" src="src/js/SerializationStrategy.js"></script>
+ <script type="text/javascript" src="src/js/CompressionSerializationStrategy.js"></script>
</head>
<body></body>
</html>
View
8 src/css/app.css
@@ -1,5 +1,9 @@
-.lsd-icon-paintcan {
- background-image: url('../images/paintcan.png');
+.lsd-icon-link {
+ background-image: url('../images/link.png');
+}
+
+.lsd-icon-help {
+ background-image: url('../images/help.png');
}
.lsd-menu-row {
View
BIN  src/images/paintcan.png
Deleted file not rendered
View
77 src/js/CompressionSerializationStrategy.js
@@ -0,0 +1,77 @@
+Ext.define('LSD.CompressionSerializationStrategy', {
+ extend: 'LSD.SerializationStrategy',
+
+ serialize: function(lazySusan) {
+ var diamonds = lazySusan.getAllDiamonds();
+ var sb = [], diamond;
+ var currentCode;
+ var currentCount = 0;
+
+ for (var d = 0; d < diamonds.length; d++) {
+ diamond = diamonds[d];
+
+ var code = diamond.getWoodType().getCode();
+
+ if (currentCode === undefined) {
+ currentCode = code;
+ currentCount = 1;
+ }
+ else if (code === currentCode) {
+ currentCount++;
+ }
+ else {
+ if (currentCount > 1) {
+ sb.push(currentCount);
+ }
+
+ sb.push(code);
+
+ currentCode = code;
+ currentCount = 1;
+ }
+ }
+
+ if (currentCount > 1) {
+ sb.push(currentCount);
+ }
+ sb.push(currentCode);
+
+ return sb.join('');
+ },
+
+ deserialize: function(lazySusan) {
+ function getWoodType(code) {
+ for (var i = 0; i < LSD.WoodTypes.length; i++) {
+ var woodType = LSD.WoodTypes[i];
+
+ if (woodType.getCode() === code) {
+ return woodType;
+ }
+ }
+
+ throw new Error('Unrecognized wood type code: ' + code);
+ }
+
+ var str = '3k3y2b216b';
+ var diamonds = lazySusan.getAllDiamonds();
+
+ while (str.length > 0) {
+ var count = 1;
+ var match = str.match(/^\d+/);
+
+ if (match != null) {
+ count = parseInt(match[0], 10);
+ str = str.substr(match[0].length);
+ }
+
+ var code = str.slice(0, 1);
+ str = str.substr(1);
+ var woodType = getWoodType(code);
+
+ for (var i = 0; i < count; i++) {
+ var diamond = diamonds.shift();
+ diamond.setWoodType(woodType);
+ }
+ }
+ }
+});
View
4 src/js/Diamond.js
@@ -30,6 +30,10 @@ Ext.define('LSD.Diamond', {
this.lastWoodType = this.woodType;
},
+ getWoodType: function() {
+ return this.woodType;
+ },
+
setWoodType: function(woodType) {
this.lastWoodType = this.woodType;
this.woodType = woodType;
View
12 src/js/LazySusan.js
@@ -4,7 +4,7 @@ Ext.define('LSD.LazySusan', {
centerX: 325,
centerY: 325,
diamondSize: 40,
- quadrants: 8, // calculate angle based off of this
+ quadrants: 8,
ringCount: 7,
constructor: function() {
@@ -45,6 +45,10 @@ Ext.define('LSD.LazySusan', {
this.show(true);
},
+ getAllDiamonds: function() {
+ return Ext.Array.flatten(this.rings);
+ },
+
getRingFromDiamond: function(diamond) {
for (var i = 0; i < this.rings.length; i++) {
var ring = this.rings[i];
@@ -55,18 +59,12 @@ Ext.define('LSD.LazySusan', {
}
},
- serialize: function() {
- return null;
- },
-
createDiamond: function(x, y, rotation) {
return Ext.create('LSD.Diamond', {
x: x,
y: y,
angle: this.diamondAngle,
side: this.diamondSize,
- fill: '#f00',
- baseColor: '#f00',
shortSide: this.diamondHeight,
rotate: {
x: this.centerX,
View
10 src/js/SerializationStrategy.js
@@ -0,0 +1,10 @@
+Ext.define('LSD.SerializationStrategy', {
+
+ serialize: function(lazySusan) {
+ throw new Error('Must implement the {serialize} method.');
+ },
+
+ deserialize: function(lazySusan) {
+ throw new Error('Must implement the {deserialize} method.');
+ }
+});
View
5 src/js/WoodType.js
@@ -1,13 +1,14 @@
Ext.define('LSD.WoodType', {
config: {
+ code: null,
displayName: null,
color: null
},
- constructor: function(displayName, color) {
+ constructor: function(code, displayName, color) {
this.callParent(arguments);
- this.initConfig({displayName: displayName, color: color});
+ this.initConfig({code: code, displayName: displayName, color: color});
}
});
View
12 src/js/WoodTypes.js
@@ -1,8 +1,8 @@
LSD.WoodTypes = [
- new LSD.WoodType('Black Walnut', '#5f3c12'),
- new LSD.WoodType('Maple', '#fdffe5'),
- new LSD.WoodType('Padauk', '#b52500'),
- new LSD.WoodType('Palea', '#ea934e'),
- new LSD.WoodType('Purpleheart', '#7b3764'),
- new LSD.WoodType('Yellowheart', '#eae954')
+ new LSD.WoodType('b', 'Black Walnut', '#5f3c12'),
+ new LSD.WoodType('m', 'Maple', '#fdffe5'),
+ new LSD.WoodType('k', 'Padauk', '#b52500'),
+ new LSD.WoodType('a', 'Palea', '#ea934e'),
+ new LSD.WoodType('p', 'Purpleheart', '#7b3764'),
+ new LSD.WoodType('y', 'Yellowheart', '#eae954')
];
View
20 src/js/app.js
@@ -48,10 +48,28 @@ Ext.onReady(function() {
selectRow(null, Ext.select('.lsd-menu-row').item(3).dom);
+ var lazySusan;
+
+
var panel = Ext.create('Ext.panel.Panel', {
title: 'Lazy Susan Designer',
bodyStyle: 'background-color: #cecece',
layout: 'fit',
+ dockedItems: [{
+ xtype: 'toolbar',
+ items: [{
+ xtype: 'button',
+ iconCls: 'lsd-icon-link',
+ handler: function() {
+ var strategy = new LSD.CompressionSerializationStrategy();
+ console.log(strategy.serialize(lazySusan));
+ //strategy.deserialize(lazySusan);
+ }
+ }, {
+ xtype: 'button',
+ iconCls: 'lsd-icon-help'
+ }]
+ }],
items: [{
xtype: 'draw',
viewBox: false,
@@ -66,7 +84,7 @@ Ext.onReady(function() {
var draw = Ext.getCmp('foo');
- var lazySusan = Ext.create('LSD.LazySusan', {
+ lazySusan = Ext.create('LSD.LazySusan', {
surface: draw.surface
});
Please sign in to comment.
Something went wrong with that request. Please try again.