forked from tweecode/twine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassagewidget.py
608 lines (480 loc) · 24.1 KB
/
passagewidget.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/usr/bin/env python
#
# PassageWidget
# A PassageWidget is a box standing in for a proxy for a single
# passage in a story. Users can drag them around, double-click
# to open a PassageFrame, and so on.
#
# This must have a StoryPanel as its parent.
#
# See the comments on StoryPanel for more information on the
# coordinate systems are used here. In general, you should
# always pass methods logical coordinates, and expect back
# logical coordinates. Use StoryPanel.toPixels() to convert.
#
import os, copy, math, re, wx, storypanel, tiddlywiki
import geometry, metrics
from passageframe import PassageFrame
class PassageWidget:
def __init__ (self, parent, app, id = wx.ID_ANY, pos = (0, 0), title = '', text = '', state = None):
# inner state
self.parent = parent
self.app = app
self.dimmed = False
self.brokenEmblem = wx.Bitmap(self.app.getPath() + os.sep + 'icons' + os.sep + 'brokenemblem.png')
self.paintBuffer = wx.MemoryDC()
self.paintBufferBounds = None
pos = list(pos)
if state:
self.passage = state['passage']
self.pos = state['pos']
self.selected = state['selected']
else:
self.passage = tiddlywiki.Tiddler('')
self.passage.title = title
self.passage.text = text
self.selected = False
self.pos = list(pos)
self.findSpace()
def getSize (self):
"""Returns this instance's logical size."""
return (PassageWidget.SIZE, PassageWidget.SIZE)
def getCenter (self):
"""Returns this instance's center in logical coordinates."""
pos = list(self.pos)
pos[0] += self.getSize()[0] / 2
pos[1] += self.getSize()[1] / 2
return pos
def getLogicalRect (self):
"""Returns this instance's rectangle in logical coordinates."""
return wx.Rect(self.pos[0], self.pos[1], PassageWidget.SIZE, PassageWidget.SIZE)
def getPixelRect (self):
"""Returns this instance's rectangle onscreen."""
origin = self.parent.toPixels(self.pos)
size = self.parent.toPixels((PassageWidget.SIZE, -1), scaleOnly = True)[0]
return wx.Rect(origin[0], origin[1], size, size)
def getDirtyPixelRect (self):
"""
Returns a pixel rectangle of everything that needs to be redrawn for the widget
in its current position. This includes the widget itself as well as any
other widgets it links to.
"""
dirtyRect = self.getPixelRect()
# first, passages we link to
for link in self.passage.links():
widget = self.parent.findWidget(link)
if widget: dirtyRect = dirtyRect.Union(widget.getPixelRect())
# then, those that link to us
# Python closures are odd, require lists to affect things outside
bridge = [ dirtyRect ]
def addLinkingToRect (widget):
if self.passage.title in widget.passage.links():
dirtyRect = bridge[0].Union(widget.getPixelRect())
self.parent.eachWidget(addLinkingToRect)
return dirtyRect
def offset (self, x = 0, y = 0):
"""Offsets this widget's position by logical coordinates."""
self.pos = list(self.pos)
self.pos[0] += x
self.pos[1] += y
def findSpace (self):
"""Moves this widget so it doesn't overlap any others."""
originalX = self.pos[0]
while self.intersectsAny():
self.pos[0] += self.parent.GRID_SPACING
rightEdge = self.pos[0] + PassageWidget.SIZE
maxWidth = self.parent.toLogical((self.parent.GetSize().width - self.parent.INSET[0], -1), \
scaleOnly = True)[0]
if rightEdge > maxWidth:
self.pos[0] = 10
self.pos[1] += self.parent.GRID_SPACING
def containsRegexp (self, regexp, flags):
"""
Returns whether this widget's passage contains a regexp.
"""
return (re.search(regexp, self.passage.title, flags) != None \
or re.search(regexp, self.passage.text, flags) != None)
def replaceRegexp (self, findRegexp, replaceRegexp, flags):
"""
Performs a regexp replace in this widget's passage title and
body text. Returns the number of replacements actually made.
"""
compiledRegexp = re.compile(findRegexp, flags)
titleReps = textReps = 0
self.passage.title, titleReps = re.subn(compiledRegexp, replaceRegexp, self.passage.title)
self.passage.text, textReps = re.subn(compiledRegexp, replaceRegexp, self.passage.text)
return titleReps + textReps
def getBrokenLinks (self):
"""Returns a list of broken links in this widget."""
brokens = []
for link in self.passage.links():
if not self.parent.findWidget(link): brokens.append(link)
return brokens
def setSelected (self, value, exclusive = True):
"""
Sets whether this widget should be selected. Pass a false value for
exclusive to prevent other widgets from being deselected.
"""
if (exclusive):
self.parent.eachWidget(lambda i: i.setSelected(False, False))
old = self.selected
self.selected = value
if self.selected != old:
self.clearPaintCache()
self.parent.Refresh(True, self.getPixelRect())
def setDimmed (self, value):
"""Sets whether this widget should be dimmed."""
old = self.dimmed
self.dimmed = value
if self.selected != old:
self.clearPaintCache()
def clearPaintCache (self):
"""
Forces the widget to be repainted from scratch.
"""
self.paintBufferBounds = None
def openContextMenu (self, event):
"""Opens a contextual menu at the event position given."""
self.parent.PopupMenu(PassageWidgetContext(self), event.GetPosition())
def openEditor (self, event = None, fullscreen = False):
"""Opens a PassageFrame to edit this passage."""
if (not hasattr(self, 'passageFrame')):
self.passageFrame = PassageFrame(None, self, self.app)
if fullscreen: self.passageFrame.openFullscreen()
else:
try:
self.passageFrame.Raise()
if fullscreen: self.passageFrame.openFullscreen()
except wx._core.PyDeadObjectError:
# user closed the frame, so we need to recreate it
delattr(self, 'passageFrame')
self.openEditor(event, fullscreen)
def closeEditor (self, event = None):
"""Closes the PassageFrame associated with this, if it exists."""
try: self.passageFrame.closeFullscreen()
except: pass
try: self.passageFrame.Destroy()
except: pass
def checkDelete (self):
"""Warns the user about deleting this passage if links exist to it."""
linked = False
for widget in self.parent.widgets:
if self.passage.title in widget.passage.links():
linked = True
break
if linked:
message = 'Are you sure you want to delete "' + self.passage.title + '"?' + \
' Links to it from other passages will become broken.'
dialog = wx.MessageDialog(self.parent, message, 'Delete Passage', \
wx.ICON_WARNING | wx.YES_NO | wx.NO_DEFAULT)
return dialog.ShowModal() == wx.ID_YES
return True
def intersectsAny (self):
"""Returns whether this widget intersects any other in the same StoryPanel."""
intersects = False
# we do this manually so we don't have to go through all of them
for widget in self.parent.widgets:
if (widget != self) and (self.intersects(widget)):
intersects = True
break
#Enforce positive coordinates
if not 'Twine.hide' in self.passage.tags:
if ((self.pos[0] < 0) or (self.pos[1] < 0)):
intersects = True
break
return intersects
def intersects (self, other):
"""
Returns whether this widget intersects another widget or wx.Rect.
This uses logical coordinates, so you can do this without actually moving the widget onscreen.
"""
selfRect = self.getLogicalRect()
if isinstance(other, PassageWidget):
other = other.getLogicalRect()
return selfRect.Intersects(other)
def applyPrefs (self):
"""Passes on the message to any editor windows."""
try: self.passageFrame.applyPrefs()
except: pass
try: self.passageFrame.fullscreen.applyPrefs()
except: pass
def paintConnectorTo (self, otherWidget, arrowheads, gc, updateRect = None):
"""
Paints a connecting line between this widget and another,
with optional arrowheads. You may pass either a wx.GraphicsContext
(anti-aliased drawing) or a wx.PaintDC.
"""
start = self.parent.toPixels(self.getCenter())
end = self.parent.toPixels(otherWidget.getCenter())
start, end = geometry.clipLineByRects([start, end], otherWidget.getPixelRect())
# does it actually need to be drawn?
if updateRect and not geometry.lineRectIntersection([start, end], updateRect):
return
if otherWidget == self:
return
# ok, really draw the line
lineWidth = max(self.parent.toPixels((PassageWidget.CONNECTOR_WIDTH, 0), scaleOnly = True)[0], 1)
gc.SetPen(wx.Pen(PassageWidget.CONNECTOR_COLOR, lineWidth))
if isinstance(gc, wx.GraphicsContext):
gc.StrokeLine(start[0], start[1], end[0], end[1])
else:
gc.DrawLine(start[0], start[1], end[0], end[1])
# arrowheads at end
if not arrowheads: return
arrowheadLength = max(self.parent.toPixels((PassageWidget.ARROWHEAD_LENGTH, 0), scaleOnly = True)[0], 1)
arrowhead = geometry.endPointProjectedFrom((start, end), angle = PassageWidget.ARROWHEAD_ANGLE, \
distance = arrowheadLength)
if isinstance(gc, wx.GraphicsContext):
gc.StrokeLine(end[0], end[1], arrowhead[0], arrowhead[1])
else:
gc.DrawLine(end[0], end[1], arrowhead[0], arrowhead[1])
arrowhead = geometry.endPointProjectedFrom((start, end), angle = 0 - PassageWidget.ARROWHEAD_ANGLE, \
distance = arrowheadLength)
if isinstance(gc, wx.GraphicsContext):
gc.StrokeLine(end[0], end[1], arrowhead[0], arrowhead[1])
else:
gc.DrawLine(end[0], end[1], arrowhead[0], arrowhead[1])
def paintConnectors (self, gc, arrowheads = True, dontDraw = [], updateRect = None):
"""
Paints all connectors originating from this widget. This accepts
a list of widget titles that will not be drawn to. It returns this
list, along with any other bad links this widget contains.
As with other paint calls, you may pass either a wx.GraphicsContext
or wx.PaintDC.
"""
if not self.app.config.ReadBool('fastStoryPanel'):
gc = wx.GraphicsContext.Create(gc)
for link in self.passage.links():
if link in dontDraw: continue
otherWidget = self.parent.findWidget(link)
if not otherWidget: dontDraw.append(link)
if otherWidget and not otherWidget.dimmed:
self.paintConnectorTo(otherWidget, arrowheads, gc, updateRect)
return dontDraw
def paint (self, dc):
"""
Handles paint events, either blitting our paint buffer or
manually redrawing.
"""
pixPos = self.parent.toPixels(self.pos)
pixSize = self.parent.toPixels(self.getSize(), scaleOnly = True)
rect = wx.Rect(pixPos[0], pixPos[1], pixSize[0], pixSize[1])
if (not self.paintBufferBounds) \
or (rect.width != self.paintBufferBounds.width \
or rect.height != self.paintBufferBounds.height):
self.cachePaint(wx.Size(rect.width, rect.height))
dc.Blit(rect.x, rect.y, rect.width, rect.height, self.paintBuffer, 0, 0)
def cachePaint (self, size):
"""
Caches the widget so self.paintBuffer is up-to-date.
"""
def wordWrap (text, lineWidth, gc):
"""
Returns a list of lines from a string
This is somewhat based on the wordwrap function built into wx.lib.
(For some reason, GraphicsContext.GetPartialTextExtents()
is returning totally wrong numbers but GetTextExtent() works fine.)
This assumes that you've already set up the font you want on the GC.
It gloms multiple spaces together, but for our purposes that's ok.
"""
words = text.split()
lines = []
currentWidth = 0
currentLine = ''
for word in words:
wordWidth = gc.GetTextExtent(word + ' ')[0]
if currentWidth + wordWidth < lineWidth:
currentLine += word + ' '
currentWidth += wordWidth
else:
lines.append(currentLine)
currentLine = word + ' '
currentWidth = wordWidth
lines.append(currentLine)
return lines
def dim (c, dim):
"""Lowers a color's alpha if dim is true."""
if isinstance(c, wx.Colour): c = list(c.Get(includeAlpha = True))
if len(c) < 4:
c = list(c)
c.append(255)
if dim: c[3] *= PassageWidget.DIMMED_ALPHA
return wx.Colour(c[0], c[1], c[2], c[3])
# set up our buffer
bitmap = wx.EmptyBitmap(size.width, size.height)
self.paintBuffer.SelectObject(bitmap)
# switch to a GraphicsContext as necessary
if self.app.config.ReadBool('fastStoryPanel'):
gc = self.paintBuffer
else:
gc = wx.GraphicsContext.Create(self.paintBuffer)
# text font sizes
# wxWindows works with points, so we need to doublecheck on actual pixels
titleFontSize = self.parent.toPixels((metrics.size('widgetTitle'), -1), scaleOnly = True)[0]
titleFontSize = min(titleFontSize, metrics.size('fontMax'))
titleFontSize = max(titleFontSize, metrics.size('fontMin'))
excerptFontSize = min(titleFontSize * 0.9, metrics.size('fontMax'))
excerptFontSize = max(excerptFontSize, metrics.size('fontMin'))
titleFont = wx.Font(titleFontSize, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Arial')
excerptFont = wx.Font(excerptFontSize, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Arial')
titleFontHeight = math.fabs(titleFont.GetPixelSize()[1])
excerptFontHeight = math.fabs(excerptFont.GetPixelSize()[1])
# inset for text (we need to know this for layout purposes)
inset = titleFontHeight / 3
# frame
frameColor = dim(PassageWidget.COLORS['frame'], self.dimmed)
frameInterior = (dim(PassageWidget.COLORS['bodyStart'], self.dimmed), \
dim(PassageWidget.COLORS['bodyEnd'], self.dimmed))
gc.SetPen(wx.Pen(frameColor, 1))
if isinstance(gc, wx.GraphicsContext):
gc.SetBrush(gc.CreateLinearGradientBrush(0, 0, 0, size.height, \
frameInterior[0], frameInterior[1]))
else:
gc.GradientFillLinear(wx.Rect(0, 0, size.width - 1, size.height - 1), \
frameInterior[0], frameInterior[1], wx.SOUTH)
gc.SetBrush(wx.TRANSPARENT_BRUSH)
gc.DrawRectangle(0, 0, size.width - 1, size.height - 1)
if size.width > PassageWidget.MIN_GREEKING_SIZE:
# title bar
titleBarHeight = titleFontHeight + (2 * inset)
titleBarColor = dim(PassageWidget.COLORS['titleBar'], self.dimmed)
gc.SetPen(wx.Pen(titleBarColor, 1))
gc.SetBrush(wx.Brush(titleBarColor))
gc.DrawRectangle(1, 1, size.width - 3, titleBarHeight)
# draw title
# we let clipping prevent writing over the frame
if isinstance(gc, wx.GraphicsContext):
gc.ResetClip()
gc.Clip(inset, inset, size.width - (inset * 2), titleBarHeight - 2)
else:
gc.DestroyClippingRegion()
gc.SetClippingRect(wx.Rect(inset, inset, size.width - (inset * 2), titleBarHeight - 2))
titleTextColor = dim(PassageWidget.COLORS['titleText'], self.dimmed)
if isinstance(gc, wx.GraphicsContext):
gc.SetFont(titleFont, titleTextColor)
else:
gc.SetFont(titleFont)
gc.SetTextForeground(titleTextColor)
gc.DrawText(self.passage.title, inset, inset)
# draw excerpt
excerptTop = inset + titleBarHeight
# we split the excerpt by line, then draw them in turn
# (we use a library to determine breaks, but have to draw the lines ourselves)
if isinstance(gc, wx.GraphicsContext):
gc.ResetClip()
gc.Clip(inset, inset, size.width - (inset * 2), size.height - (inset * 2))
else:
gc.DestroyClippingRegion()
gc.SetClippingRect(wx.Rect(inset, inset, size.width - (inset * 2), size.height - (inset * 2)))
excerptTextColor = dim(PassageWidget.COLORS['excerptText'], self.dimmed)
if isinstance(gc, wx.GraphicsContext):
gc.SetFont(excerptFont, excerptTextColor)
else:
gc.SetFont(excerptFont)
gc.SetTextForeground(excerptTextColor)
excerptLines = wordWrap(self.passage.text, size.width - (inset * 2), gc)
for line in excerptLines:
gc.DrawText(line, inset, excerptTop)
excerptTop += excerptFontHeight * PassageWidget.LINE_SPACING
if excerptTop + excerptFontHeight > size.height - inset: break
else:
# greek title
titleBarColor = dim(PassageWidget.COLORS['titleBar'], self.dimmed)
gc.SetPen(wx.Pen(titleBarColor, 1))
gc.SetBrush(wx.Brush(titleBarColor))
gc.DrawRectangle(1, 1, size.width - 3, PassageWidget.GREEK_HEIGHT * 3)
gc.SetPen(wx.Pen('#ffffff', PassageWidget.GREEK_HEIGHT))
height = inset
width = (size.width - inset) / 2
if isinstance(gc, wx.GraphicsContext):
gc.StrokeLine(inset, height, width, height)
else:
gc.DrawLine(inset, height, width, height)
height += PassageWidget.GREEK_HEIGHT * 3
# greek body text
gc.SetPen(wx.Pen('#666666', PassageWidget.GREEK_HEIGHT))
while height < size.height - inset:
width = size.height - inset
if height + (PassageWidget.GREEK_HEIGHT * 2) > size.height - inset:
width = width / 2
if isinstance(gc, wx.GraphicsContext):
gc.StrokeLine(inset, height, width, height)
else:
gc.DrawLine(inset, height, width, height)
height += PassageWidget.GREEK_HEIGHT * 2
if isinstance(gc, wx.GraphicsContext):
gc.ResetClip()
else:
gc.DestroyClippingRegion()
# draw a broken link emblem in the bottom right if necessary
# fixme: not sure how to do this with transparency
if len(self.getBrokenLinks()):
emblemSize = self.brokenEmblem.GetSize()
emblemPos = [ size.width - (emblemSize[0] + inset), \
size.height - (emblemSize[1] + inset) ]
if isinstance(gc, wx.GraphicsContext):
gc.DrawBitmap(self.brokenEmblem, emblemPos[0], emblemPos[1], emblemSize[0], emblemSize[1])
else:
gc.DrawBitmap(self.brokenEmblem, emblemPos[0], emblemPos[1])
# finally, draw a selection over ourselves if we're selected
if self.selected:
color = dim(wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT), self.dimmed)
gc.SetPen(wx.Pen(color, 2))
if isinstance(gc, wx.GraphicsContext):
r, g, b = color.Get()
color = wx.Colour(r, g, b, 64)
gc.SetBrush(wx.Brush(color))
else:
gc.SetBrush(wx.TRANSPARENT_BRUSH)
gc.DrawRectangle(1, 1, size.width - 2, size.height - 2)
self.paintBufferBounds = size
def serialize (self):
"""Returns a dictionary with state information suitable for pickling."""
return { 'selected': self.selected, 'pos': self.pos, 'passage': copy.copy(self.passage) }
def sort (first, second):
"""
Sorts PassageWidgets so that the results appear left to right,
top to bottom. A certain amount of slack is assumed here in
terms of positioning.
"""
xDistance = int(first.pos[0] - second.pos[0])
yDistance = int(first.pos[1] - second.pos[1])
if abs(yDistance) > 5:
return yDistance
else:
if xDistance != 0:
return xDistance
else:
return 1 # punt on ties
def __repr__ (self):
return "<PassageWidget '" + self.passage.title + "'>"
MIN_PIXEL_SIZE = 10
MIN_GREEKING_SIZE = 50
GREEK_HEIGHT = 2
SIZE = 120
SHADOW_SIZE = 5
COLORS = { 'frame': (0, 0, 0), \
'bodyStart': (255, 255, 255), \
'bodyEnd': (228, 228, 226), \
'titleBar': (52, 101, 164), \
'titleText': (255, 255, 255), \
'excerptText': (0, 0, 0) }
DIMMED_ALPHA = 0.25
LINE_SPACING = 1.2
CONNECTOR_WIDTH = 2.0
CONNECTOR_COLOR = '#babdb6'
ARROWHEAD_LENGTH = 10
MIN_ARROWHEAD_LENGTH = 5
ARROWHEAD_ANGLE = math.pi / 6
# contextual menu
class PassageWidgetContext (wx.Menu):
def __init__ (self, parent):
wx.Menu.__init__(self)
self.parent = parent
title = '"' + parent.passage.title + '"'
edit = wx.MenuItem(self, wx.NewId(), 'Edit ' + title)
self.AppendItem(edit)
self.Bind(wx.EVT_MENU, self.parent.openEditor, id = edit.GetId())
delete = wx.MenuItem(self, wx.NewId(), 'Delete ' + title)
self.AppendItem(delete)
self.Bind(wx.EVT_MENU, lambda e: self.parent.parent.removeWidget(self.parent), id = delete.GetId())