From e4b210d60772a784b78183e58f2840a0cf02e739 Mon Sep 17 00:00:00 2001 From: John Loehrlein Date: Fri, 23 Jan 2009 20:05:19 +0000 Subject: [PATCH] direct entry scroll, a virtual window for direct entries --- direct/src/gui/DirectEntryScroll.py | 114 ++++++++++++++++++++++++++++ direct/src/gui/DirectGui.py | 1 + direct/src/gui/DirectGuiGlobals.py | 1 + 3 files changed, 116 insertions(+) create mode 100644 direct/src/gui/DirectEntryScroll.py diff --git a/direct/src/gui/DirectEntryScroll.py b/direct/src/gui/DirectEntryScroll.py new file mode 100644 index 0000000000..6f4710d8cf --- /dev/null +++ b/direct/src/gui/DirectEntryScroll.py @@ -0,0 +1,114 @@ +__all__ = ['DirectEntryScroll'] + +from pandac.PandaModules import * +import DirectGuiGlobals as DGG +from DirectScrolledFrame import * +from DirectFrame import * +from DirectEntry import * + +class DirectEntryScroll(DirectFrame): + def __init__(self, entry, parent = None, **kw): + optiondefs = ( + ('pgFunc', PGVirtualFrame, None), + ('relief', None, None), + ('clipSize', (-1, 1, -1, 1), self.setClipSize), + ) + + self.defineoptions(kw, optiondefs) + DirectFrame.__init__(self, parent, **kw) + self.canvas = None + self.visXMin = 0.0 + self.visXMax = 0.0 + self.clipXMin = 0.0 + self.clipXMax = 0.0 + self.initialiseoptions(DirectEntryScroll) + + self.entry = entry + self.canvas = NodePath(self.guiItem.getCanvasNode()) + self.entry.reparentTo(self.canvas) + self.canvas.setPos(0,0,0) + + self.entry.bind(DGG.CURSORMOVE,self.cursorMove) + + self.canvas.node().setBounds(OmniBoundingVolume()) + self.canvas.node().setFinal(1) + self.resetCanvas() + + base.scroller = self + + + + def cursorMove(self, cursorX, cursorY): + cursorX = self.entry.guiItem.getCursorX() * self.entry['text_scale'][0] + canvasX = self.canvas.getX() + visXMin = self.clipXMin - canvasX + visXMax = self.clipXMax - canvasX + visXCenter = (visXMin + visXMax) * 0.5 + distanceToCenter = visXCenter - cursorX + clipExtent = self.clipXMax - self.clipXMin + + entryExtent = self.entry['text_scale'][0] * self.entry['width'] + entryWiggle = entryExtent - clipExtent + + if abs(distanceToCenter) > (clipExtent * 0.5): + self.moveToCenterCursor() + + + def moveToCenterCursor(self): + cursorX = self.entry.guiItem.getCursorX() * self.entry['text_scale'][0] + canvasX = self.canvas.getX() + visXMin = self.clipXMin - canvasX + visXMax = self.clipXMax - canvasX + visXCenter = (visXMin + visXMax) * 0.5 + distanceToCenter = visXCenter - cursorX + newX = canvasX + distanceToCenter + + clipExtent = self.clipXMax - self.clipXMin + + entryExtent = self.entry['text_scale'][0] * self.entry['width'] + entryWiggle = entryExtent - clipExtent + + if self.entry.guiItem.getCursorPosition() <= 0: #deals with the cursor jump bug + newX = 0.0 + elif newX > 0.0: + newX = 0.0 + elif newX < (-entryWiggle): + newX = -entryWiggle + + #print("CursorX %s CanvasX %s VisCenter %s Distance %s NewX %s Wiggle %s" % (cursorX, canvasX, visXCenter, distanceToCenter, newX, entryWiggle)) + + self.canvas.setX(newX) + + def destroy(self): + # Destroy children of the canvas + for child in self.canvas.getChildren(): + childGui = self.guiDict.get(child.getName()) + if childGui: + childGui.destroy() + else: + parts = child.getName().split('-') + simpleChildGui = self.guiDict.get(parts[-1]) + if simpleChildGui: + simpleChildGui.destroy() + self.entry.destroy() + self.entry = None + DirectFrame.destroy(self) + + + def getCanvas(self): + return self.canvas + + def setClipSize(self): + self.guiItem.setClipFrame(self['clipSize']) + self.clipXMin = self['clipSize'][0] + self.clipXMax = self['clipSize'][1] + self.visXMin = self.clipXMin + self.visXMax = self.clipXMax + if self.canvas: + self.resetCanvas() + + def resetCanvas(self): + self.canvas.setPos(0,0,0) + + + \ No newline at end of file diff --git a/direct/src/gui/DirectGui.py b/direct/src/gui/DirectGui.py index 6a528b7437..1d3c0636de 100644 --- a/direct/src/gui/DirectGui.py +++ b/direct/src/gui/DirectGui.py @@ -15,6 +15,7 @@ from OnscreenImage import * from DirectFrame import * from DirectButton import * from DirectEntry import * +from DirectEntryScroll import * from DirectLabel import * from DirectScrolledList import * from DirectDialog import * diff --git a/direct/src/gui/DirectGuiGlobals.py b/direct/src/gui/DirectGuiGlobals.py index 91e868ea05..c60d8b31fe 100644 --- a/direct/src/gui/DirectGuiGlobals.py +++ b/direct/src/gui/DirectGuiGlobals.py @@ -74,6 +74,7 @@ ACCEPT = PGEntry.getAcceptPrefix() + KeyboardButton.enter().getName() + '-' ACCEPTFAILED = PGEntry.getAcceptFailedPrefix() + KeyboardButton.enter().getName() + '-' TYPE = PGEntry.getTypePrefix() ERASE = PGEntry.getErasePrefix() +CURSORMOVE = PGEntry.getCursormovePrefix() # For DirectSlider and DirectScrollBar widgets ADJUST = PGSliderBar.getAdjustPrefix()