Skip to content

Commit

Permalink
Ironed out the bugs and got the map displaying in the map editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorennorman committed Nov 8, 2009
1 parent 44c8282 commit 66c04bb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/controllers/maps_controller.rb
Expand Up @@ -18,7 +18,7 @@ def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @map }
format.json { render :json => @map.to_json(:only => [:id, :name], :methods => [:unique_terrain, :flat_terrain]) }
format.json { render :json => @map.to_json(:only => [:id, :name, :width, :height], :methods => [:unique_terrain, :flat_terrain]) }
end
end

Expand Down
3 changes: 2 additions & 1 deletion public/map_editor/AppController.j
Expand Up @@ -42,8 +42,9 @@

// Fire off the request! This object will handle the response.
[CPURLConnection connectionWithRequest:request delegate:self];
// Menubar for new, load, save Maps...


// TODO: Menubar for new, load, save Maps...
}

- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
Expand Down
19 changes: 7 additions & 12 deletions public/map_editor/MapModel.j
Expand Up @@ -4,9 +4,6 @@
var TILE_PIXEL_WIDTH = 100;
var TILE_PIXEL_HEIGHT = 100;

// Default MapModelDataObject
var defaultMapDataJSON = '{ \"name\":\"The Briney Depths\", \"width\":5, \"height\":6, \"terrain\":[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3], \"nothing\":\"nothing\" }';

@implementation MapModel : CPObject
{
CPString _name;
Expand All @@ -15,17 +12,14 @@ var defaultMapDataJSON = '{ \"name\":\"The Briney Depths\", \"width\":5, \"he
CPArray _terrain;
}

+ (id)findById:(int)uid
{
return [[MapModel alloc] initWithMapModelDataJSON:defaultMapDataJSON];
}

- (id)initWithMapModelJSONData:(CPObject)dataJSON
{
self = [super init];

var dataObject = JSON.parse(dataJSON);

//CPLogConsole("dataObject.unique_terrain[0].image_url: "+JSON.parse(dataObject.unique_terrain), "info", "MapModel");

if (self)
{
_name = dataObject.name;
Expand All @@ -34,7 +28,7 @@ var defaultMapDataJSON = '{ \"name\":\"The Briney Depths\", \"width\":5, \"he
_terrain = dataObject.flat_terrain;

// Now offload the unique_terrain hash into our TerrainItemResourceManager
[TerrainItemModel setAvailableTerrainModels:[CPArray arrayWithArray:dataObject.unique_terrain]];
[TerrainItemModel setAvailableTerrainModels:[CPArray arrayWithArray:JSON.parse(dataObject.unique_terrain)]];
// so our terrainItemModels() accessors can pull from the cache
}

Expand All @@ -45,12 +39,13 @@ var defaultMapDataJSON = '{ \"name\":\"The Briney Depths\", \"width\":5, \"he
{
var terrainItemModels = [];
// Map the _terrain array to an array of TerrainItemModels of the corresponding id
for (terrainItemId in _terrain)
for(var index=0;index<_terrain.length;index++)
{
terrainItemModels.push([TerrainItemModel findById:terrainItemId]);
//CPLogConsole("terrainItemModels index: "+index+" terrain[index]: "+_terrain[index], "info", "MapModel");
terrainItemModels.push([TerrainItemModel findById:_terrain[index]]);
}

return [CPArray arrayWithArray:terrainItemModels];
return terrainItemModels;
}

- (int)pixelWidth
Expand Down
6 changes: 3 additions & 3 deletions public/map_editor/MapView.j
Expand Up @@ -14,11 +14,10 @@
if (self)
{
// Create and configure the CPCollectionView that will display our beautiful map


_terrainView = [[CPCollectionView alloc] initWithFrame:CGRectMakeZero()];
[_terrainView setBackgroundColor:[CPColor blackColor]];
[_terrainView setAutoresizingMask:nil];
[_terrainView setVerticalMargin:0];
[_terrainView setMinItemSize:CGSizeMake(100, 100)];
[_terrainView setMaxItemSize:CGSizeMake(100, 100)];

Expand All @@ -40,7 +39,8 @@
- (void)setMapModel:(MapModel)aMapModel
{
// Calculate the width and height of this map in pixels based on the tile width and height
var terrainViewBounds = CGRectMake(0,0,([aMapModel pixelWidth] + 100), ([aMapModel pixelHeight] + 100));
var terrainViewBounds = CGRectMake(0,0,[aMapModel pixelWidth], [aMapModel pixelHeight]);
[_terrainView setBounds:terrainViewBounds];

[_terrainView setContent:[aMapModel terrainItemModels]];
}
20 changes: 9 additions & 11 deletions public/map_editor/TerrainItemModel.j
Expand Up @@ -9,24 +9,22 @@ var AvailableTerrainModels = [CPArray alloc];
CPString _image_url;
}

+ (id)findById:(int)uid
+ (id)findById:(CPString)uid
{
return [AvailableTerrainModels valueForKey:uid];
return AvailableTerrainModels[uid];
}

+ (void)setAvailableTerrainModels:(CPArray)terrainModelArray
{
for (terrainItemModelObject in terrainModelArray)
//CPLogConsole("setAvailableTerrainModels: entering", "info", "TerrainItemModel");

for(var index=0;index<terrainModelArray.length;index++)
{
[AvailableTerrainModels setValue:[[TerrainItemModel alloc] initWithTerrainItemModelDataObject:terrainItemModelObject] forKey:terrainItemModelObject.id];
//CPLogConsole("setAvaialbeTerrainModels iteration: "+terrainModelArray[index], "info", "TerrainItemModel");
var modelObject = terrainModelArray[index];
AvailableTerrainModels[modelObject.id] = [[TerrainItemModel alloc] initWithTerrainItemModelDataObject:modelObject];
}
}

- (id)initWithTerrainItemModelDataJSON:(CPObject)dataJSON
{
var dataObject = JSON.parse(dataJSON);

[self initWithTerrainItemModelDataObject:dataObject];
//CPLogConsole("setAvailableTerrainModels: exiting", "info", "TerrainItemModel");
}

- (id)initWithTerrainItemModelDataObject:(CPObject)dataObject
Expand Down
6 changes: 3 additions & 3 deletions public/map_editor/TerrainItemView.j
Expand Up @@ -9,19 +9,19 @@
// id: database id
// name: string representing the name of the terrain
// image_url: string with the image path on the server
- (void)setRepresentedObject:(id)anObject
- (void)setRepresentedObject:(id)terrainItemModel
{
// Initialize our image if needed
if(!_imageView)
{
_imageView = [[CPImageView alloc] initWithFrame:[self bounds]];
_imageView = [[CPImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[_imageView setImageScaling:CPScaleProportionally];

[self addSubview:_imageView];
}

// Create a new image to add to our ImageView
var newImage = [[CPImage alloc] initWithContentsOfFile:anObject.image_url size:CGSizeMake(100.0,100.0)];
var newImage = [[CPImage alloc] initWithContentsOfFile:[terrainItemModel image_url] size:CGSizeMake(100.0,100.0)];
[_imageView setImage:newImage];
}

Expand Down
13 changes: 1 addition & 12 deletions public/map_editor/TerrainPalettePanel.j
Expand Up @@ -123,15 +123,4 @@
alert(anError);
}

@end











@end

0 comments on commit 66c04bb

Please sign in to comment.