diff options
author | Justin Lewis <jtl1728@rit.edu> | 2010-01-22 19:46:00 (GMT) |
---|---|---|
committer | Justin Lewis <jtl1728@rit.edu> | 2010-01-22 19:46:00 (GMT) |
commit | dadd4e0289ca9f7726add5710cee61e667568363 (patch) | |
tree | b5f57f5e89c0fe143dede139e721277822c396c2 /FortuneMaker.activity | |
parent | 3946457c5c7602735248421afe76496aaf73cfb3 (diff) |
Added gui interface to select dungeons to edit.
Also modified system to handle loading of the file and save format to
include theme.
Diffstat (limited to 'FortuneMaker.activity')
-rw-r--r-- | FortuneMaker.activity/Dungeon.py | 13 | ||||
-rw-r--r-- | FortuneMaker.activity/FortuneMaker.py | 90 | ||||
-rw-r--r-- | FortuneMaker.activity/Room.py | 31 |
3 files changed, 119 insertions, 15 deletions
diff --git a/FortuneMaker.activity/Dungeon.py b/FortuneMaker.activity/Dungeon.py index f8ad618..502a4d4 100644 --- a/FortuneMaker.activity/Dungeon.py +++ b/FortuneMaker.activity/Dungeon.py @@ -1,17 +1,21 @@ from Room import Room class Dungeon: - def __init__( self, name, theme, width, height ): + def __init__( self, name, theme, width, height, room_str = None ): self.name = name self.theme = theme self.width = width self.height = height self.roomlist = [] - for y in range(0, width): + + for y in range(0, height): room_row = [] - for x in range(0, height): - room_row.append( Room(x,y) ) + for x in range(0, width): + if room_str: + room_row.append( Room(x,y, room_str.pop(0) ) ) + else: + room_row.append( Room(x,y) ) self.roomlist.append(room_row) def get_room_array(self): @@ -22,6 +26,7 @@ class Dungeon: def export(self): text = str(self.width) + "x" + str(self.height) + "\n" + text += str(self.theme) + "\n" for row in self.roomlist: for room in row: text += room.room_to_string() + "\n" diff --git a/FortuneMaker.activity/FortuneMaker.py b/FortuneMaker.activity/FortuneMaker.py index 1dba8d5..66ca241 100644 --- a/FortuneMaker.activity/FortuneMaker.py +++ b/FortuneMaker.activity/FortuneMaker.py @@ -12,12 +12,15 @@ from gettext import gettext as _ import gtk import os +import re MAX_GRID_WIDTH = 15 MAX_GRID_HEIGHT = 15 MIN_GRID_WIDTH = 2 MIN_GRID_HEIGHT = 2 +class BadInputException(Exception):pass + class FortuneMaker(Activity): def __init__(self, handle): Activity.__init__(self, handle) @@ -34,7 +37,8 @@ class FortuneMaker(Activity): self.set_toolbox(toolbox) toolbox.show() - self.set_create_dungeon_settings() + self.show_dungeon_selection() + #self.set_create_dungeon_settings() def view_change_cb(self, widget, view=None): if view == 'stats': @@ -46,15 +50,22 @@ class FortuneMaker(Activity): elif view == 'export': self.export_view() + def list_fh_files(self): + ds_objects, num_objects = datastore.find({'FortuneMaker_VERSION':'1'}) + file_list = [] + for i in xrange(0, num_objects, 1): + file_list.append( ds_objects[i] ) + return file_list + def export_view(self): data = self.dungeon.export() textbuffer = gtk.Label() - filename = "MAFH_%s.txt" % self.dungeon.name + filename = self.dungeon.name self._write_textfile( filename, data) - textbuffer.set_text( "File Saved to %s\n\n%s"%(filename,data)) + textbuffer.set_text( "File Saved to %s"%(filename) ) self.set_gui_view( textbuffer, True ) @@ -64,9 +75,7 @@ class FortuneMaker(Activity): # with filetext as the data put in the file. # @Returns: a DSObject representing the file in the datastore. def _write_textfile(self, filename, filetext=''): - - - ds_objects, num_objects = datastore.find({'title':filename}) + ds_objects, num_objects = datastore.find({'title':filename,'FortuneMaker_VERSION':'1'}) if num_objects == 0: # Create a datastore object @@ -78,6 +87,7 @@ class FortuneMaker(Activity): # specify that this is a plain text file). file_dsobject.metadata['title'] = filename file_dsobject.metadata['mime_type'] = 'text/plain' + file_dsobject.metadata['FortuneMaker_VERSION'] = '1' #Write the actual file to the data directory of this activity's root. file_path = os.path.join(self.get_activity_root(), 'instance', filename) @@ -128,7 +138,46 @@ class FortuneMaker(Activity): return button_tabs - def set_create_dungeon_settings(self): + def show_dungeon_selection(self): + window_container = gtk.VBox() + + button = gtk.Button( _("Create New Dungeon") ) + button.connect("clicked", self.set_create_dungeon_settings, None) + window_container.pack_start( button, False ) + + frame = gtk.Frame( _("Load Dungeon") ) + file_container = gtk.VBox() + + ##LOAD FILE LIST HERE + file_list = self.list_fh_files() + + for dfile in file_list: + row = gtk.HBox() + label = gtk.Label(dfile.metadata['title']) + row.pack_start( label, False ) + + button = gtk.Button(_("Load")) + button.connect( 'clicked', self.load_dungeon, dfile ) + row.pack_end(button, False) + + file_container.pack_start( row, False ) + + scroll = gtk.ScrolledWindow() + scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC) + scroll.add_with_viewport( file_container ) + + frame.add( scroll ) + window_container.pack_start( frame ) + + room_center = gtk.HBox() + room_center.pack_start( gtk.Label() ) + room_center.pack_start( window_container ) + room_center.pack_start( gtk.Label() ) + + self.set_gui_view( room_center ) + + + def set_create_dungeon_settings(self, trash=None, trash2=None): window_container = gtk.VBox() ## Dungeon Properties @@ -202,6 +251,33 @@ class FortuneMaker(Activity): self.set_gui_view( room_center ) + def load_dungeon(self, widget, file_data): + name = file_data.metadata['title'] + dgnFile=open(file_data.get_file_path(),'r') + + grab = 0 + room_str = [] + for line in dgnFile: + if grab == 0: + match = re.match('(\d+)x(\d+)',line) + if match: + x = int(match.group(1)) + y = int(match.group(2)) + grab = 1 + else: + raise BadInputException() + + elif grab == 1: + theme = int(line) + grab = 2 + + elif grab == 2: + room_str.append(line) + + self.dungeon = Dungeon( name, theme, x, y, room_str) + self.view_dungeon_stats() + + def create_dungeon_cb(self, widget, data): name = data['name'].get_text() theme = data['theme'].get_active() #.get_active_text() diff --git a/FortuneMaker.activity/Room.py b/FortuneMaker.activity/Room.py index c6f3a00..876c586 100644 --- a/FortuneMaker.activity/Room.py +++ b/FortuneMaker.activity/Room.py @@ -6,7 +6,7 @@ from constants import ( import gtk class Room: - def __init__(self, x = -1, y = -1): + def __init__(self, x = -1, y = -1, str=None): self._x = x self._y = y self.doors = {} @@ -24,8 +24,27 @@ class Room: for index in range(0,4): self.item.append( ['0', '0'] ) + # Load room from str + # TODO VALIDATE FLAGS + if str: + self.add_door( str[0], str[1] ) + self.add_door( str[2], str[3] ) + self.add_door( str[4], str[5] ) + self.add_door( str[6], str[7] ) + self.set_room_flag( str[8] ) + self.set_enemy( 0, str[9] ) + self.set_enemy( 1, str[10] ) + self.set_enemy( 2, str[11] ) + self.set_enemy( 3, str[12] ) + self.set_item( 0, str[13], str[14] ) + self.set_item( 1, str[15], str[16] ) + self.set_item( 2, str[17], str[18] ) + self.set_item( 3, str[19], str[20] ) + def add_door(self, door, flag): - if door in DOOR_INDEX and flag in DOOR_FLAGS: + if door == "0": + return + elif door in DOOR_INDEX and flag in DOOR_FLAGS: self.doors[door] = [door, flag] else: print "INVALID DOOR AND/OR FLAG" @@ -50,7 +69,9 @@ class Room: return self.special def set_enemy( self, pos, enemy ): - if pos >= 0 and pos <=3 and enemy in ENEM_INDEX: + if enemy == "0": + return + elif pos >= 0 and pos <=3 and enemy in ENEM_INDEX: self.enemy[pos] = enemy else: print "INVALID ENEMY POS OR ID" @@ -60,7 +81,9 @@ class Room: return self.enemy[pos] def set_item(self, pos, id, flag): - if pos >= 0 and pos <=3 and id in ITEM_INDEX and flag in ITEM_FLAGS: + if id == "0": + return + elif pos >= 0 and pos <=3 and id in ITEM_INDEX and flag in ITEM_FLAGS: self.item[ pos ] = [id, flag] else: print "INVALID POS OR ID OR FLAG" |