trackplacer: initialization code for editing.

This commit is contained in:
Eric S. Raymond 2008-10-11 12:34:44 +00:00
parent a0b97cca3d
commit aef93974d5

View File

@ -2,7 +2,7 @@
#
# Map journey track editor. Just a stub at the moment, not yet working.
import sys
import sys, exceptions
import pygtk
pygtk.require('2.0')
@ -10,7 +10,67 @@ import gtk
import wesnoth.wmltools
# All dependencies on the shape of the data tree live here
default_map = "data/core/images/maps/wesnoth.png"
journey_icon = "data/core/images/misc/new-journey.png"
battle_icon = "data/core/images/misc/new-battle.png"
rest_icon = "data/core/images/misc/flag-red.png"
class ReadException(exceptions.Exception):
"Exception thrown while reading a track file."
def __init__(self, message, filename, lineno):
self.message = message
self.filename = filename
self.lineno = lineno
class JourneyTrack:
"Represent a journey track on a map."
def __init__(self):
self.filename = None # Map background of the journey
self.track = [] # List of (action, x, y) tuples
def write(self, fp):
"Record a journey track."
fp.write("FILE %s\n" % self.filename)
for location in self.track:
fp.write("%s %d %d\n" % location)
def read(self, fp):
"Initialize a journey from map and track information."
if type(fp) == type(""):
try:
fp = open(fp)
except FileError:
raise ReadException("cannot read", fp)
if self.track:
raise ReadException("reading with track nonempty", fp.name, 1)
if fp.name.endswith(".png") or fp.name.endswith(".jpg"):
self.filename = fp.name
return
if not fp.name.endswith(".trk"):
raise ReadException("cannot read this filetype", fp.name, 0)
header = fp.readline().split()
if header[0] != 'FILE':
raise ReadException("missing FILE element", fp.name, 1)
else:
self.filename = header[1]
while (i, line) in enumerate(fp):
fields = line.split()
if len(fields) != 3:
raise ReadException("ill-formed line", fp.name, i+1)
(tag, x, y) = fields
if tag not in ("JOURNEY", "BATTLE", "REST"):
raise ReadException("invalid tag field", fp.name, i+1)
try:
x = int(x)
y = int(y)
except ValuError:
raise ReadException("invalid coordinate field", fp.name, i+1)
self.track.append((tag, x, y))
def __getitem__(self, n):
return self.track[n]
def __setitem__(self, n, v):
self.track[n] = v
def pop(self):
self.track.pop()
class ModalFileSelector:
def __init__(self, default):
@ -39,8 +99,43 @@ class ModalFileSelector:
gtk.main_quit()
sys.exit(0)
def log(msg):
"Notify user of error and die."
# FIXME: conditionalize of a debug flag
print >>sys.stderr, "trackplacer:", msg
def fatal_error(msg):
"Notify user of error and die."
# FIXME: Needs to change to modal notification window
print >>sys.stderr, msg
sys.exit(1)
class TrackEditor:
def __init__(self):
# Initialize our info about the map and track
self.journey = JourneyTrack()
self.journey.read(ModalFileSelector(default_map).filename)
# We need two copies of the map -- one scratchpad for scribbling on,
# and one for restoring from when we erase track dots.
log("about to read map %s" % self.journey.filename)
try:
self.background = gtk.Image()
self.background.set_from_file(self.journey.filename)
self.scratchpad = gtk.Image()
self.scratchpad.set_from_file(self.journey.filename)
except:
fatal_error("Error while reading background map %s" % self.journey.filename)
#try:
self.journey_image = gtk.Image()
self.journey_image.set_from_file(journey_icon)
self.battle.image = gtk.Image()
self.battle_image.set_from_file(battle_icon)
self.rest_image = gtk.Image()
self.rest_image.set_from_file(rest_icon)
#except:
# fatal_error("Error while reading icons")
log("initialization successful")
if __name__ == "__main__":
wesnoth.wmltools.pop_to_top("trackplacer")
operand = ModalFileSelector(default_map).filename
print "I see", operand
TrackEditor()