Commits

filipkunc committed 450818c

Fixed JSWrappers.

  • Participants
  • Parent commits a6f35da

Comments (0)

Files changed (4)

File Classes/JSWrappers.h

 //  Created by Filip Kunc on 10/27/12.
 //
 //
-/*
+
+#pragma once
+
 #import "ItemCollection.h"
 
 @class VertexWrapper;
 @class TriangleNodeIterator;
 @class EdgeNodeIterator;
 @class SimpleNodeEdgeIterator;
+@class ItemWrapper;
+
+@interface ItemCollectionWrapper : NSObject
+{
+    ItemCollection *_itemCollection;
+}
+
+- (id)initWithItemCollection:(ItemCollection *)itemCollection;
 
-@interface ItemCollection (JSWrappers)
+@property (readonly) uint count;
+
+- (ItemWrapper *)at:(uint)index;
 
 @end
 
-@interface Item (JSWrappers)
+@interface ItemWrapper : NSObject
+{
+    Item *_item;
+}
+
+- (id)initWithItem:(Item *)item;
 
 @property (readonly) VertexNodeIterator *vertexIterator;
 @property (readonly) TriangleNodeIterator *triQuadIterator;
 
 @property (readonly) uint vertexCount;
 @property (readonly) uint triQuadCount;
+@property (readwrite, assign) BOOL selected;
 
 - (VertexWrapper *)addVertexWithX:(float)x y:(float)y z:(float)z;
 - (TriangleWrapper *)addTriangleWithFirst:(VertexWrapper *)v0 second:(VertexWrapper *)v1 third:(VertexWrapper *)v2;
 DeclareIterator(TriangleNodeIterator, TriangleWrapper, TriangleNode)
 DeclareIterator(EdgeNodeIterator, EdgeWrapper, VertexEdgeNode)
 DeclareIterator(VertexNodeEdgeIterator, VertexNodeEdgeWrapper, Vertex2VEdgeNode)
-*/

File Classes/JSWrappers.mm

 //  Created by Filip Kunc on 10/27/12.
 //
 //
-/*
+
 #import "JSWrappers.h"
 
-@implementation ItemCollection (JSWrappers)
+@implementation ItemCollectionWrapper
 
 + (NSString *)webScriptNameForSelector:(SEL)sel
 {
-    if (sel == @selector(itemAtIndex:))
+    if (sel == @selector(at:))
         return @"at";
-    
     return nil;
 }
 
 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
 + (BOOL)isKeyExcludedFromWebScript:(const char *)name { return NO; }
 
+- (id)initWithItemCollection:(ItemCollection *)itemCollection
+{
+    self = [super init];
+    if (self)
+    {
+        _itemCollection = itemCollection;
+    }
+    return self;
+}
+
+- (uint)count
+{
+    return _itemCollection->count();
+}
+
+- (ItemWrapper *)at:(uint)index
+{
+    return [[ItemWrapper alloc] initWithItem:_itemCollection->itemAtIndex(index)];
+}
+
 @end
 
-@implementation Item (JSWrappers)
+@implementation ItemWrapper
+
+- (id)initWithItem:(Item *)item
+{
+    self = [super init];
+    if (self)
+    {
+        _item = item;
+    }
+    return self;
+}
 
 - (VertexNodeIterator *)vertexIterator
 {
-    return [[VertexNodeIterator alloc] initWithBegin:mesh->vertices().begin() end:mesh->vertices().end()];
+    return [[VertexNodeIterator alloc] initWithBegin:_item->mesh->vertices().begin() end:_item->mesh->vertices().end()];
 }
 
 - (TriangleNodeIterator *)triQuadIterator
 {
-    return [[TriangleNodeIterator alloc] initWithBegin:mesh->triangles().begin() end:mesh->triangles().end()];
+    return [[TriangleNodeIterator alloc] initWithBegin:_item->mesh->triangles().begin() end:_item->mesh->triangles().end()];
 }
 
 - (EdgeNodeIterator *)edgeIterator
 {
-    return [[EdgeNodeIterator alloc] initWithBegin:mesh->vertexEdges().begin() end:mesh->vertexEdges().end()];
-}
-
-- (uint)vertexCount { return mesh->vertexCount(); }
-- (uint)triQuadCount { return mesh->triangleCount(); }
-
-- (void)removeDegeneratedTriangles { mesh->removeDegeneratedTriangles(); }
-- (void)removeNonUsedVertices { mesh->removeNonUsedVertices(); }
-- (void)removeNonUsedTexCoords { mesh->removeNonUsedTexCoords(); }
-- (void)mergeSelected { mesh->mergeSelected(); }
-- (void)splitSelected { mesh->splitSelected(); }
-- (void)detachSelected { mesh->detachSelected(); }
-- (void)duplicateSelectedTriangles { mesh->duplicateSelectedTriangles(); }
-- (void)flipSelected { mesh->flipSelected(); }
-- (void)flipAllTriangles { mesh->flipAllTriangles(); }
-- (void)extrudeSelectedTriangles { mesh->extrudeSelectedTriangles(); }
-- (void)triangulate { mesh->triangulate(); }
-- (void)triangulateSelectedQuads { mesh->triangulateSelectedQuads(); }
-- (void)openSubdivision { mesh->openSubdivision(); }
-- (void)loopSubdivision { mesh->loopSubdivision(); }
-- (void)makeTexCoords { mesh->makeTexCoords(); }
-- (void)makeEdges { mesh->makeEdges(); }
-- (void)updateSelection { mesh->setSelectionMode(mesh->selectionMode()); }
-- (void)setSelectionModeVertices { mesh->setSelectionMode(MeshSelectionModeVertices); }
-- (void)setSelectionModeTriQuads { mesh->setSelectionMode(MeshSelectionModeTriangles); }
-- (void)setSelectionModeEdges { mesh->setSelectionMode(MeshSelectionModeEdges); }
+    return [[EdgeNodeIterator alloc] initWithBegin:_item->mesh->vertexEdges().begin() end:_item->mesh->vertexEdges().end()];
+}
+
+- (uint)vertexCount { return _item->mesh->vertexCount(); }
+- (uint)triQuadCount { return _item->mesh->triangleCount(); }
+- (BOOL)selected { return _item->selected; }
+- (void)setSelected:(BOOL)value { _item->selected = value; }
+
+- (void)removeDegeneratedTriangles { _item->mesh->removeDegeneratedTriangles(); }
+- (void)removeNonUsedVertices { _item->mesh->removeNonUsedVertices(); }
+- (void)removeNonUsedTexCoords { _item->mesh->removeNonUsedTexCoords(); }
+- (void)mergeSelected { _item->mesh->mergeSelected(); }
+- (void)splitSelected { _item->mesh->splitSelected(); }
+- (void)detachSelected { _item->mesh->detachSelected(); }
+- (void)duplicateSelectedTriangles { _item->mesh->duplicateSelectedTriangles(); }
+- (void)flipSelected { _item->mesh->flipSelected(); }
+- (void)flipAllTriangles { _item->mesh->flipAllTriangles(); }
+- (void)extrudeSelectedTriangles { _item->mesh->extrudeSelectedTriangles(); }
+- (void)triangulate { _item->mesh->triangulate(); }
+- (void)triangulateSelectedQuads { _item->mesh->triangulateSelectedQuads(); }
+- (void)openSubdivision { _item->mesh->openSubdivision(); }
+- (void)loopSubdivision { _item->mesh->loopSubdivision(); }
+- (void)makeTexCoords { _item->mesh->makeTexCoords(); }
+- (void)makeEdges { _item->mesh->makeEdges(); }
+- (void)updateSelection { _item->mesh->setSelectionMode(_item->mesh->selectionMode()); }
+- (void)setSelectionModeVertices { _item->mesh->setSelectionMode(MeshSelectionMode::Vertices); }
+- (void)setSelectionModeTriQuads { _item->mesh->setSelectionMode(MeshSelectionMode::Triangles); }
+- (void)setSelectionModeEdges { _item->mesh->setSelectionMode(MeshSelectionMode::Edges); }
+- (void)removeSelected { _item->removeSelected(); }
 
 - (VertexWrapper *)addVertexWithX:(float)x y:(float)y z:(float)z
 {
-    return [[VertexWrapper alloc] initWithNode:mesh->addVertex(Vector3D(x, y, z))];
+    return [[VertexWrapper alloc] initWithNode:_item->mesh->addVertex(Vector3D(x, y, z))];
 }
 
 - (TriangleWrapper *)addTriangleWithFirst:(VertexWrapper *)v0 second:(VertexWrapper *)v1 third:(VertexWrapper *)v2
 {
-    return [[TriangleWrapper alloc] initWithNode:mesh->addTriangle(v0.node, v1.node, v2.node)];
+    return [[TriangleWrapper alloc] initWithNode:_item->mesh->addTriangle(v0.node, v1.node, v2.node)];
 }
 
 - (TriangleWrapper *)addQuadWithFirst:(VertexWrapper *)v0 second:(VertexWrapper *)v1 third:(VertexWrapper *)v2 fourth:(VertexWrapper *)v3
 {
-    return [[TriangleWrapper alloc] initWithNode:mesh->addQuad(v0.node, v1.node, v2.node, v3.node)];
+    return [[TriangleWrapper alloc] initWithNode:_item->mesh->addQuad(v0.node, v1.node, v2.node, v3.node)];
 }
 
 - (void)removeTriQuad:(TriangleWrapper *)triQuad
 {
     TriangleNode *current = triQuad.node;
-    mesh->removeTriQuad(current);
+    _item->mesh->removeTriQuad(current);
     triQuad.node = current;
 }
 
 ImplementIterator(TriangleNodeIterator, TriangleNode, node)
 ImplementIterator(EdgeNodeIterator, VertexEdgeNode, node)
 ImplementIterator(VertexNodeEdgeIterator, Vertex2VEdgeNode, simpleNode)
-*/

File Classes/ScriptWindowController.h

 //
 //
 
+#pragma once
+
 #import <Cocoa/Cocoa.h>
 #import "ItemCollection.h"
+#import "JSWrappers.h"
 
 @protocol ScriptDelegate <NSObject>
 

File Classes/ScriptWindowController.mm

 }
 
 - (void)runScriptCode:(NSString *)code
-{/*
+{
+    ItemCollectionWrapper *wrapper = [[ItemCollectionWrapper alloc] initWithItemCollection:delegate.items];
+    
     NSString* script = [NSString stringWithFormat:@"try { %@ } catch (e) { e.toString() }", code];
-    [scriptObject setValue:delegate.items forKey:@"items"];
+    [scriptObject setValue:wrapper forKey:@"items"];
     [outputView setString:@"Output:\n"];
     
     [delegate allItemsActionWithName:@"Script Action On Items" block:^
     {
-        for (Item *item in delegate.items)
+        ItemCollection *items = delegate.items;
+        
+        for (uint i = 0; i < items->count(); i++)
         {
-            item.mesh->resetTriangleCache();
-            item.mesh->resetAlgorithmData();
+            Item *item = items->itemAtIndex(i);
+            item->mesh->resetTriangleCache();
+            item->mesh->resetAlgorithmData();
         }
         
         id data = [scriptObject evaluateWebScript:script];
     }];
 
     [delegate setNeedsDisplayOnAllViews];
-*/}
+}
 
 - (IBAction)runScript:(id)sender
 {