import copy, math, colorsys, re, wx, tiddlywiki, tweelexer
import geometry, metrics, images
from passageframe import PassageFrame, ImageFrame, StorySettingsFrame
class PassageWidget(object):
"""
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.
"""
def __init__(self, parent, app, pos = (0, 0), title = '', text = '', tags = (), state = None):
# inner state
self.parent = parent
self.app = app
self.dimmed = False
self.brokenEmblem = wx.Bitmap(self.app.iconsPath + 'brokenemblem.png')
self.externalEmblem = wx.Bitmap(self.app.iconsPath + 'externalemblem.png')
self.paintBuffer = wx.MemoryDC()
self.paintBufferBounds = None
if state:
self.passage = state['passage']
self.pos = list(pos) if pos != (0,0) else state['pos']
self.selected = state['selected']
else:
self.passage = tiddlywiki.Tiddler('')
self.selected = False
self.pos = list(pos)
if title: self.passage.title = title
if text: self.passage.text = text
if tags: self.passage.tags += tags
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.Union(widget.getPixelRect())
# then, those that link to us
def addLinkingToRect(widget):
if self.passage.title in widget.passage.links:
dirtyRect.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."""
turns = 0.0
movecount = 1
"""
Don't adhere to the grid if snapping isn't enabled.
Instead, move in 1/5 grid increments.
"""
griddivision = 1 if self.parent.snapping else 0.2
while self.intersectsAny() and turns < 99*griddivision:
"""Move in an Ulam spiral pattern: n spaces left, n spaces up, n+1 spaces right, n+1 spaces down"""
self.pos[int(math.floor((turns*2) % 2))] += self.parent.GRID_SPACING * griddivision * int(math.copysign(1, turns % 2 - 1))
movecount -= 1
if movecount <= 0:
turns += 0.5
movecount = int(math.ceil(turns)/griddivision)
def findSpaceQuickly(self):
""" A quicker findSpace where the position and visibility doesn't really matter """
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) is not None
or re.search(regexp, self.passage.text, flags) is not 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)
def getShorthandDisplays(self):
"""Returns a list of macro tags which match passage names."""
return filter(self.parent.passageExists, self.passage.macros)
def getBrokenLinks(self):
"""Returns a list of broken links in this widget."""
return filter(lambda a: not self.parent.passageExists(a), self.passage.links)
def getIncludedLinks(self):
"""Returns a list of included passages in this widget."""
return filter(self.parent.includedPassageExists, self.passage.links)
def getVariableLinks(self):
"""Returns a list of links which use variables/functions, in this widget."""
return filter(lambda a: tweelexer.TweeLexer.linkStyle(a) == tweelexer.TweeLexer.PARAM, self.passage.links)
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()
# Figure out the dirty rect
dirtyRect = self.getPixelRect()
for link in self.linksAndDisplays() + self.passage.images:
widget = self.parent.findWidget(link)
if widget:
dirtyRect.Union(widget.getDirtyPixelRect())
if self.passage.isStylesheet():
for t in self.passage.tags:
if t not in tiddlywiki.TiddlyWiki.INFO_TAGS:
for widget in self.parent.taggedWidgets(t):
if widget:
dirtyRect.Union(widget.getDirtyPixelRect())
self.parent.Refresh(True, dirtyRect)
def setDimmed(self, value):
"""Sets whether this widget should be dimmed."""
old = self.dimmed
self.dimmed = value
if self.dimmed != 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."""
image = self.passage.isImage()
if not hasattr(self, 'passageFrame'):
if image:
self.passageFrame = ImageFrame(None, self, self.app)
elif self.passage.title == "StorySettings":
self.passageFrame = StorySettingsFrame(None, self, self.app)
else:
self.passageFrame = PassageFrame(None, self, self.app)
if fullscreen: self.passageFrame.openFullscreen()
else:
try:
self.passageFrame.Iconize(False)
self.passageFrame.Raise()
if fullscreen and not image: 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.closeEditor()
except: pass
try: self.passageFrame.Destroy()
except: pass
def verifyPassage(self, window):
"""
Check that the passage syntax is well-formed.
Return -(corrections made) if the check was aborted, +(corrections made) otherwise
"""
passage = self.passage
checks = tweelexer.VerifyLexer(self).check()
problems = 0
oldtext = passage.text
newtext = ""
index = 0
for warning, replace in checks:
problems += 1
if replace:
start, sub, end = replace
answer = wx.MessageDialog(window, warning + "\n\nMay I try to fix this for you?",
'Problem in ' + self.passage.title,
wx.ICON_WARNING | wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT
).ShowModal()
if answer == wx.ID_YES:
newtext += oldtext[index:start] + sub
index = end
if hasattr(self, 'passageFrame') and self.passageFrame:
self.passageFrame.bodyInput.SetText(newtext + oldtext[index:])
elif answer == wx.ID_CANCEL:
return -problems
else:
answer = wx.MessageDialog(window, warning+"\n\nKeep checking?", 'Problem in '+self.passage.title, wx.ICON_WARNING | wx.YES_NO) \
.ShowModal()
if answer == wx.ID_NO:
return problems
passage.text = newtext + oldtext[index:]
return problems
def intersectsAny(self, dragging = False):
"""Returns whether this widget intersects any other in the same StoryPanel."""
#Enforce positive coordinates
if not 'Twine.hide' in self.passage.tags:
if self.pos[0] < 0 or self.pos[1] < 0:
return True
# we do this manually so we don't have to go through all of them
for widget in self.parent.notDraggingWidgets if dragging else self.parent.widgetDict.itervalues():
if widget != self and self.intersects(widget):
return True
return False
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."""
self.clearPaintCache()
try: self.passageFrame.applyPrefs()
except: pass
try: self.passageFrame.fullscreen.applyPrefs()
except: pass
def updateBitmap(self):
"""If an image passage, updates the bitmap to match the contained base64 data."""
if self.passage.isImage():
self.bitmap = images.base64ToBitmap(self.passage.text)
def getConnectorLine(self, otherWidget, clipped=True):
"""
Get the line that would be drawn between this widget and another.
"""
start = self.getCenter()
end = otherWidget.getCenter()
#Tweak to make overlapping lines easier to see by shifting the end point
#Devision by a large constant to so the behavior is not overly noticeable while dragging
lengthSquared = ((start[0]-end[0])**2+(start[1]-end[1])**2)/1024**2
end[0] += (0.5 - math.sin(lengthSquared))*PassageWidget.SIZE/8.0
end[1] += (0.5 - math.cos(lengthSquared))*PassageWidget.SIZE/8.0
if clipped:
[start, end] = geometry.clipLineByRects([start, end], otherWidget.getLogicalRect())
return self.parent.toPixels(start), self.parent.toPixels(end)
def getConnectedWidgets(self, displayArrows, imageArrows):
"""
Returns a list of titles of all widgets that will have lines drawn to them.
"""
ret = []
for link in self.linksAndDisplays():
if link in self.passage.links or displayArrows:
widget = self.parent.findWidget(link)
if widget:
ret.append(widget)
if imageArrows:
for link in self.passage.images:
widget = self.parent.findWidget(link)
if widget:
ret.append(widget)
if self.passage.isStylesheet():
for t in self.passage.tags:
if t not in tiddlywiki.TiddlyWiki.INFO_TAGS:
for otherWidget in self.parent.taggedWidgets(t):
if not otherWidget.dimmed and not otherWidget.passage.isStylesheet():
ret.append(otherWidget)
return ret
def addConnectorLinesToDict(self, displayArrows, imageArrows, flatDesign, lineDictonary, arrowDictonary=None, updateRect=None):
"""
Appended the connector lines originating from this widget to the list contained in the
line directory under the appropriate color,width key.
If an arrow dictionary is also passed it adds the arrows in a similar manner.
If an update rect is passed it skips any lines, and the associated arrows,
which lie outside the update rectangle.
Note: Assumes the list existed in the passed in dictionaries. Either make sure this is the case or
use a defaultDict.
"""
colors = PassageWidget.FLAT_COLORS if flatDesign else PassageWidget.COLORS
# Widths for selected and non selected lines
widths = 2 * (2 * flatDesign + 1), 1 * (2 * flatDesign + 1)
widths = max(self.parent.toPixels((widths[0], 0), scaleOnly=True)[0], 2), \
max(self.parent.toPixels((widths[1], 0), scaleOnly=True)[0], 1)
widgets = self.getConnectedWidgets(displayArrows, imageArrows)
if widgets:
for widget in widgets:
link = widget.passage.title
if self.passage.isAnnotation():
color = colors['connectorAnnotation']
elif (link in self.passage.displays + self.passage.macros) and link not in self.passage.links:
color = colors['connectorDisplay']
elif link in self.passage.images or self.passage.isStylesheet():
color = colors['connectorResource']
else:
color = colors['connector']
width = widths[not self.selected]
line, arrow = self.getConnectorTo(widget, not arrowDictonary is None, updateRect)
lineDictonary[(color, width)].extend(line)
if arrow:
arrowDictonary[(color, width)].extend(arrow)
def getConnectorTo(self, otherWidget, arrowheads=False, updateRect=None):
# does it actually need to be drawn?
if otherWidget == self:
return [], []
start, end = self.getConnectorLine(otherWidget)
if updateRect and not geometry.lineRectIntersection([start, end], updateRect):
return [], []
if (not self.paintBufferBounds) \
or (rect.width != self.paintBufferBounds.width \
or rect.height != self.paintBufferBounds.height):
self.cachePaint(wx.Size(rect.width, rect.height))
def getTitleColor(self):
"""
Returns the title bar style that matches this widget's passage.
"""
flat = self.app.config.ReadBool('flatDesign')
# First, rely on the header to supply colours
custom = self.getHeader().passageTitleColor(self.passage)
if custom:
return custom[flat]
# Use default colours
if self.passage.isAnnotation():
ind = 'annotation'
elif self.passage.isImage():
ind = 'imageTitleBar'
elif any(t.startswith('Twine.') for t in self.passage.tags):
ind = 'privateTitleBar'
elif not self.linksAndDisplays() and not self.getIncludedLinks() and not self.passage.variableLinks:
ind = 'endTitleBar'
else:
ind = 'titleBar'
colors = PassageWidget.FLAT_COLORS if flat else PassageWidget.COLORS
return colors[ind]
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 = re.finditer('\S+\s*', text.replace('\r',''))
lines = ''
currentLine = ''
for w in words:
word = w.group(0)
wordWidth = gc.GetTextExtent(currentLine + word)[0]
if wordWidth < lineWidth:
currentLine += word
if '\n' in word:
lines += currentLine
currentLine = ''
else:
lines += currentLine + '\n'
currentLine = word
lines += currentLine
return lines.split('\n')
# Which set of colors to use
flat = self.app.config.ReadBool('flatDesign')
colors = PassageWidget.FLAT_COLORS if flat else PassageWidget.COLORS
def dim(c, dim, flat=flat):
"""Lowers a color's alpha if dim is true."""
if isinstance(c, wx.Colour):
c = list(c.Get(includeAlpha=True))
elif type(c) is str:
c = list(ord(a) for a in c[1:].decode('hex'))
else:
c = list(c)
if len(c) < 4:
c.append(255)
if dim:
a = PassageWidget.FLAT_DIMMED_ALPHA if flat else PassageWidget.DIMMED_ALPHA
if not self.app.config.ReadBool('fastStoryPanel'):
c[3] *= a
else:
c[0] *= a
c[1] *= a
c[2] *= a
return wx.Colour(*c)
# set up our buffer
bitmap = wx.EmptyBitmap(size.width, size.height)
self.paintBuffer.SelectObject(bitmap)
# switch to a GraphicsContext as necessary
gc = self.paintBuffer if self.app.config.ReadBool('fastStoryPanel') else wx.GraphicsContext.Create(self.paintBuffer)
# text font sizes
# wxWindows works with points, so we need to doublecheck on actual pixels
for line in excerptLines:
gc.DrawText(line, inset, excerptTop)
excerptTop += excerptFontHeight * PassageWidget.LINE_SPACING \
* min(1.75,max(1,1.75*size.width/260 if (self.passage.isAnnotation() and line) else 1))
if excerptTop + excerptFontHeight > size.height - inset: break
if (self.passage.isStoryText() or self.passage.isStylesheet()) and tags:
# choose smaller of vertical and horizontal scale factor, to preserve aspect ratio
scale = min(width/float(self.bitmap.GetWidth()), height/float(self.bitmap.GetHeight()))
img = self.bitmap.ConvertToImage()
if scale != 1:
img = img.Scale(scale*self.bitmap.GetWidth(),scale*self.bitmap.GetHeight())
# offset image horizontally or vertically, to centre after scaling
offsetWidth = (width - img.GetWidth())/2
offsetHeight = (height - img.GetHeight())/2
if isinstance(gc, wx.GraphicsContext):
gc.DrawBitmap(img.ConvertToBitmap(self.bitmap.GetDepth()),
1 + offsetWidth, titleBarHeight + 1 + offsetHeight,
img.GetWidth(), img.GetHeight())
else:
gc.DrawBitmap(img.ConvertToBitmap(self.bitmap.GetDepth()),
1 + offsetWidth, titleBarHeight + 1 + offsetHeight)
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()):
showEmblem(self.brokenEmblem)
elif len(self.getIncludedLinks()) or len(self.passage.variableLinks):
showEmblem(self.externalEmblem)
# finally, draw a selection over ourselves if we're selected
if self.selected:
color = dim(titleBarColor if flat else wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT), self.dimmed)
if self.app.config.ReadBool('fastStoryPanel'):
gc.SetPen(wx.Pen(color, 2 + flat))
else:
gc.SetPen(wx.TRANSPARENT_PEN)
if isinstance(gc, wx.GraphicsContext):
r, g, b = color.Get(False)
color = wx.Colour(r, g, b, 64)
gc.SetBrush(wx.Brush(color))
else:
gc.SetBrush(wx.TRANSPARENT_BRUSH)
gc.DrawRectangle(0, 0, size.width, size.height)
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) }
@staticmethod
def posCompare(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.
"""
class PassageWidgetContext(wx.Menu):
def __init__(self, parent):
wx.Menu.__init__(self)
self.parent = parent
title = '"' + parent.passage.title + '"'
if parent.passage.isStoryPassage():
test = wx.MenuItem(self, wx.NewId(), 'Test Play From Here')
self.AppendItem(test)
self.Bind(wx.EVT_MENU, lambda e: self.parent.parent.parent.testBuild(startAt = parent.passage.title), id = test.GetId())