forked from tweecode/twine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·324 lines (274 loc) · 12.2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python2
import sys, os, locale, re, pickle, wx, platform
import metrics
from header import Header
from storyframe import StoryFrame
from prefframe import PreferenceFrame
from version import versionString
class App(wx.App):
"""This bootstraps our application and keeps track of preferences, etc."""
NAME = 'Twine'
VERSION = '%s (running on %s %s)' % (versionString, platform.system(), platform.release()) #Named attributes not available in Python 2.6
RECENT_FILES = 10
def __init__(self, redirect = False):
"""Initializes the application."""
wx.App.__init__(self, redirect = redirect)
locale.setlocale(locale.LC_ALL, '')
self.stories = []
self.loadPrefs()
self.determinePaths()
self.loadTargetHeaders()
# try to load our app icon
# if it doesn't work, we continue anyway
self.icon = wx.EmptyIcon()
try:
self.icon = wx.Icon(self.iconsPath + 'app.ico', wx.BITMAP_TYPE_ICO)
except:
pass
# restore save location
try:
os.chdir(self.config.Read('savePath'))
except:
os.chdir(os.path.expanduser('~'))
if not self.openOnStartup():
if self.config.HasEntry('LastFile') \
and os.path.exists(self.config.Read('LastFile')):
self.open(self.config.Read('LastFile'))
else:
self.newStory()
def newStory(self, event = None):
"""Opens a new, blank story."""
s = StoryFrame(parent = None, app = self)
self.stories.append(s)
s.Show(True)
def removeStory(self, story, byMenu = False):
"""Removes a story from our collection. Should be called when it closes."""
try:
self.stories.remove(story)
if byMenu:
counter = 0
for s in self.stories:
if isinstance(s, StoryFrame):
counter = counter + 1
if counter == 0:
self.newStory()
except ValueError:
None
def openDialog(self, event = None):
"""Opens a story file of the user's choice."""
opened = False
dialog = wx.FileDialog(None, 'Open Story', os.getcwd(), "", "Twine Story (*.tws)|*.tws", \
wx.FD_OPEN | wx.FD_CHANGE_DIR)
if dialog.ShowModal() == wx.ID_OK:
opened = True
self.config.Write('savePath', os.getcwd())
self.addRecentFile(dialog.GetPath())
self.open(dialog.GetPath())
dialog.Destroy()
def openRecent(self, story, index):
"""Opens a recently-opened file."""
filename = story.recentFiles.GetHistoryFile(index)
if not os.path.exists(filename):
self.removeRecentFile(story, index)
else:
self.open(filename)
self.addRecentFile(filename)
def MacOpenFile(self, path):
"""OS X support"""
self.open(path)
def open(self, path):
"""Opens a specific story file."""
try:
openedFile = open(path, 'r')
newStory = StoryFrame(None, app = self, state = pickle.load(openedFile))
newStory.saveDestination = path
self.stories.append(newStory)
newStory.Show(True)
self.addRecentFile(path)
self.config.Write('LastFile', path)
openedFile.close()
# weird special case:
# if we only had one story opened before
# and it's pristine (e.g. no changes ever made to it),
# then we close it after opening the file successfully
if (len(self.stories) == 2) and (self.stories[0].pristine):
self.stories[0].Destroy()
except:
self.displayError('opening your story')
def openOnStartup(self):
"""
Opens any files that were passed via argv[1:]. Returns
whether anything was opened.
"""
if len(sys.argv) is 1:
return False
for file in sys.argv[1:]:
self.open(file)
return True
def exit(self, event = None):
"""Closes all open stories, implicitly quitting."""
# need to make a copy of our stories list since
# stories removing themselves will alter the list midstream
for s in list(self.stories):
if isinstance(s, StoryFrame):
s.Close()
def showPrefs(self, event = None):
"""Shows the preferences dialog."""
if (not hasattr(self, 'prefFrame')):
self.prefFrame = PreferenceFrame(self)
else:
try:
self.prefFrame.Raise()
except wx._core.PyDeadObjectError:
# user closed the frame, so we need to recreate it
delattr(self, 'prefFrame')
self.showPrefs(event)
def addRecentFile(self, path):
"""Adds a path to the recent files history and updates the menus."""
for s in self.stories:
if isinstance(s, StoryFrame):
s.recentFiles.AddFileToHistory(path)
s.recentFiles.Save(self.config)
def removeRecentFile(self, story, index):
"""Remove all missing files from the recent files history and update the menus."""
def removeRecentFile_do(story, index, showdialog = True):
filename = story.recentFiles.GetHistoryFile(index)
story.recentFiles.RemoveFileFromHistory(index)
story.recentFiles.Save(self.config)
if showdialog:
text = 'The file ' + filename + ' no longer exists.\n' + \
'This file has been removed from the Recent Files list.'
dlg = wx.MessageDialog(None, text, 'Information', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
return True
else:
return False
showdialog = True
for s in self.stories:
if s != story and isinstance(s, StoryFrame):
removeRecentFile_do(s, index, showdialog)
showdialog = False
removeRecentFile_do(story, index, showdialog)
def verifyRecentFiles(self, story):
done = False
while done == False:
for index in range(story.recentFiles.GetCount()):
if not os.path.exists(story.recentFiles.GetHistoryFile(index)):
self.removeRecentFile(story, index)
done = False
break
else:
done = True
def about(self, event = None):
"""Shows the about dialog."""
info = wx.AboutDialogInfo()
info.SetName(self.NAME)
info.SetVersion(self.VERSION)
info.SetIcon(self.icon)
info.SetWebSite('http://twinery.org/')
info.SetDescription('An open-source tool for telling interactive stories\nwritten by Chris Klimas')
info.SetDevelopers(['Leon Arnott','Emmanuel Turner','Henry Soule','Misty De Meo','Phillip Sutton','Thomas M. Edwards','Maarten ter Huurne','and others.'])
info.SetLicense('The Twine development application and its Python source code is free software: you can redistribute it and/or modify'
+ ' it under the terms of the GNU General Public License as published by the Free Software'
+ ' Foundation, either version 3 of the License, or (at your option) any later version.'
+ ' See the GNU General Public License for more details.\n\nThe Javascript game engine in compiled game files is a derivative work of Jeremy Ruston\'s TiddlyWiki project,'
+ ' and is used under the terms of the MIT license.')
wx.AboutBox(info)
def storyFormatHelp(self, event = None):
"""Opens the online manual to the section on story formats."""
wx.LaunchDefaultBrowser('http://twinery.org/wiki/story_format')
def openForum(self, event = None):
"""Opens the forum."""
wx.LaunchDefaultBrowser('http://twinery.org/forum/')
def openDocs(self, event = None):
"""Opens the online manual."""
wx.LaunchDefaultBrowser('http://twinery.org/wiki/')
def openGitHub(self, event = None):
"""Opens the GitHub page."""
wx.LaunchDefaultBrowser('https://github.com/tweecode/twine')
def loadPrefs(self):
"""Loads user preferences into self.config, setting up defaults if none are set."""
sc = self.config = wx.Config('Twine')
monoFont = wx.SystemSettings.GetFont(wx.SYS_ANSI_FIXED_FONT)
for k,v in {
'savePath' : os.path.expanduser('~'),
'fsTextColor' : '#afcdff',
'fsBgColor' : '#100088',
'fsFontFace' : metrics.face('mono'),
'fsFontSize' : metrics.size('fsEditorBody'),
'fsLineHeight' : 120,
'windowedFontFace' : metrics.face('mono'),
'monospaceFontFace' : metrics.face('mono2'),
'windowedFontSize' : metrics.size('editorBody'),
'monospaceFontSize' : metrics.size('editorBody'),
'flatDesign' : False,
'storyFrameToolbar' : True,
'storyPanelSnap' : False,
'fastStoryPanel' : False,
'imageArrows' : True,
'displayArrows' : True,
'createPassagePrompt' : True,
'importImagePrompt' : True,
'passageWarnings' : True
}.iteritems():
if not sc.HasEntry(k):
if type(v) == str:
sc.Write(k,v)
elif type(v) == int:
sc.WriteInt(k,v)
elif type(v) == bool:
sc.WriteBool(k,v)
def applyPrefs(self):
"""Asks all of our stories to update themselves based on a preference change."""
map(lambda s: s.applyPrefs(), self.stories)
def displayError(self, activity):
"""
Displays an error dialog with diagnostic info. Call with what you were doing
when the error occurred (e.g. 'saving your story', 'building your story'.)
"""
exception = sys.exc_info()
text = 'An error occurred while ' + activity + ' ('
text += str(exception[1]) + ').'
error = wx.MessageDialog(None, text, 'Error', wx.OK | wx.ICON_ERROR)
error.ShowModal()
def MacReopenApp(self):
"""OS X support"""
self.GetTopWindow().Raise()
def determinePaths(self):
"""Determine the paths to relevant files used by application"""
scriptPath = os.path.dirname(os.path.realpath(sys.argv[0]))
if sys.platform == 'win32':
# Windows py2exe'd apps add an extraneous library.zip at the end
scriptPath = re.sub('\\\\\w*.zip', '', scriptPath)
elif sys.platform == "darwin":
scriptPath = re.sub('MacOS\/.*', '', scriptPath)
scriptPath += os.sep
self.iconsPath = scriptPath + 'icons' + os.sep
self.builtinTargetsPath = scriptPath + 'targets' + os.sep
if sys.platform == "darwin":
self.externalTargetsPath = re.sub('[^/]+.app/.*', 'targets' + os.sep, self.builtinTargetsPath)
if not os.path.isdir(self.externalTargetsPath):
self.externalTargetsPath = ''
else:
self.externalTargetsPath = ''
def loadTargetHeaders(self):
"""Load the target headers and populate the self.headers dictionary"""
self.headers = {}
# Get paths to built-in targets
paths = [(t, self.builtinTargetsPath + t + os.sep) for t in os.listdir(self.builtinTargetsPath)]
if self.externalTargetsPath:
# Get paths to external targets
paths += [(t, self.externalTargetsPath + t + os.sep) for t in os.listdir(self.externalTargetsPath)]
# Look in subdirectories only for the header file
for path in paths:
try:
if not os.path.isfile(path[1]) and os.access(path[1] + 'header.html', os.R_OK):
header = Header.factory(*path, builtinPath = self.builtinTargetsPath)
self.headers[header.id] = header
except:
pass
# start things up if we were called directly
if __name__ == "__main__":
app = App()
app.MainLoop()