Go to the documentation of this file.00001 import BigWorld, GUI, Math, Keys
00002
00003 import Utils
00004
00005 class DraggableComponent:
00006
00007 def __init__( self, horzDrag, vertDrag, restrictToParent ):
00008 self.horzDrag = horzDrag
00009 self.vertDrag = vertDrag
00010 self.restrictToParent = restrictToParent
00011
00012 self.component.focus = True
00013 self.component.moveFocus = True
00014 self.component.crossFocus = True
00015
00016 self.onBeginDrag = lambda:None
00017 self.onEndDrag = lambda:None
00018 self.onDragging = lambda:None
00019
00020
00021 def handleMouseButtonEvent( self, comp, key, down, modifiers ):
00022
00023
00024 if key == Keys.KEY_LEFTMOUSE and (self.horzDrag or self.vertDrag):
00025 if down:
00026 self.startDragging()
00027 elif dragManager.isDragging():
00028 self.stopDragging()
00029
00030 return True
00031
00032 def onSave( self, dataSection ):
00033 dataSection.writeBool( "horzDrag", self.horzDrag )
00034 dataSection.writeBool( "vertDrag", self.vertDrag )
00035 dataSection.writeBool( "restrictToParent", self.restrictToParent )
00036
00037 def onLoad( self, dataSection ):
00038 self.horzDrag = dataSection.readBool( "horzDrag" )
00039 self.vertDrag = dataSection.readBool( "vertDrag" )
00040 self.restrictToParent = dataSection.readBool( "restrictToParent" )
00041
00042 def startDragging( self ):
00043 dragManager.startDragging( self.component, self.horzDrag,
00044 self.vertDrag, self.restrictToParent )
00045
00046 def stopDragging( self ):
00047 dragManager.stopDragging()
00048
00049
00050
00051
00052
00053
00054
00055 def _horzAnchorOffset( component ):
00056 if component.horizontalAnchor == "LEFT":
00057 return 0
00058 elif component.horizontalAnchor == "CENTER":
00059 return -component.width/2.0
00060 elif component.horizontalAnchor == "RIGHT":
00061 return -component.width
00062
00063 def _vertAnchorOffset( component ):
00064 if component.verticalAnchor == "TOP":
00065 return 0
00066 elif component.verticalAnchor == "CENTER":
00067 return component.height/2.0
00068 elif component.verticalAnchor == "BOTTOM":
00069 return component.height
00070
00071 class ComponentDragManager:
00072 def __init__( self ):
00073 self.component = None
00074 self.offset = (0,0)
00075 self.horzDrag = False
00076 self.vertDrag = False
00077 self.restrictToParent = False
00078
00079
00080 def isDragging( self ):
00081 return self.component is not None
00082
00083
00084 def startDragging( self, component, horzDrag, vertDrag, restrictToParent ):
00085 if self.component:
00086 print "WARNING: startDragging called again without first calling stop!"
00087 self.stopDragging()
00088
00089 self.horzDrag = horzDrag
00090 self.vertDrag = vertDrag
00091 self.restrictToParent = restrictToParent
00092
00093 cpos = Utils.absoluteClipPosition( component )
00094 mpos = GUI.mcursor().position
00095
00096 self.offset = Math.Vector2( mpos.x - cpos.x, mpos.y - cpos.y )
00097 self.component = component
00098
00099 if restrictToParent:
00100 self.restrictHorizontally()
00101 self.restrictVertically()
00102
00103
00104
00105 if hasattr( self.component.script, "onBeginDrag" ):
00106 self.component.script.onBeginDrag()
00107
00108
00109 def stopDragging( self ):
00110 if not self.component:
00111 print "WARNING: stopDragging called without any component currently being dragged!"
00112
00113 if self.component is not None:
00114 if hasattr( self.component.script, "onEndDrag" ):
00115 self.component.script.onEndDrag()
00116
00117
00118 self.component = None
00119
00120
00121 def handleKeyEvent( self, down, key, mods ):
00122 if key == Keys.KEY_LEFTMOUSE and not down and self.isDragging():
00123 self.stopDragging()
00124 return True
00125
00126 return False
00127
00128
00129 def handleMouseEvent( self, dx, dy, dz ):
00130
00131 if self.component is None:
00132 return False
00133
00134 mpos = GUI.mcursor().position
00135
00136
00137 cpos = Math.Vector2( mpos[0] - self.offset.x, mpos[1] - self.offset.y )
00138
00139
00140
00141 nrp = Utils.nearestRelativeParent( self.component )
00142 while nrp is not None:
00143 nrpPos = Utils.legacyPosition( nrp )
00144 cpos.x = cpos.x - nrpPos.x
00145 cpos.y = cpos.y - nrpPos.y
00146 nrp = Utils.nearestRelativeParent( nrp )
00147
00148
00149
00150
00151 startPos = tuple(self.component.position)
00152
00153 if self.horzDrag:
00154 horzMode = self.component.horizontalPositionMode
00155 self.component.horizontalPositionMode = "LEGACY"
00156 self.component.position.x = cpos.x
00157 x = cpos.x
00158 self.component.horizontalPositionMode = horzMode
00159 if self.restrictToParent:
00160 self.restrictHorizontally()
00161
00162 if self.vertDrag:
00163 vertMode = self.component.verticalPositionMode
00164 self.component.verticalPositionMode = "LEGACY"
00165 self.component.position.y = cpos.y
00166 self.component.verticalPositionMode = vertMode
00167 if self.restrictToParent:
00168 self.restrictVertically()
00169
00170 draggedHorz = self.component.position.x != startPos[0]
00171 draggedVert = self.component.position.y != startPos[1]
00172 dragged = draggedHorz or draggedVert
00173
00174 if dragged and hasattr( self.component.script, "onDragging" ):
00175 self.component.script.onDragging()
00176
00177 return True
00178
00179
00180 def restrictHorizontally( self ):
00181 if self.component is None:
00182 return
00183
00184 horzMode = self.component.horizontalPositionMode
00185 widthMode = self.component.widthMode
00186 self.component.horizontalPositionMode = "CLIP"
00187 self.component.widthMode = "CLIP"
00188
00189 anchorOffsetX = _horzAnchorOffset( self.component )
00190
00191 restricted = True
00192
00193 if self.component.position.x < -1.0 - anchorOffsetX:
00194 self.component.position.x = -1.0 - anchorOffsetX
00195 elif self.component.position.x > 1.0 + anchorOffsetX:
00196 self.component.position.x = 1.0 + anchorOffsetX
00197 else:
00198 restricted = False
00199
00200 self.component.horizontalPositionMode = horzMode
00201 self.component.widthMode = widthMode
00202
00203 return restricted
00204
00205
00206 def restrictVertically( self ):
00207 if self.component is None:
00208 return
00209
00210 vertMode = self.component.verticalPositionMode
00211 heightMode = self.component.heightMode
00212 self.component.verticalPositionMode = "CLIP"
00213 self.component.heightMode = "CLIP"
00214
00215 anchorOffsetY = _vertAnchorOffset( self.component )
00216
00217 restricted = True
00218
00219 if self.component.position.y < -1.0 + anchorOffsetY:
00220 self.component.position.y = -1.0 + anchorOffsetY
00221 elif self.component.position.y > 1.0 - anchorOffsetY:
00222 self.component.position.y = 1.0 - anchorOffsetY
00223 else:
00224 restricted = False
00225
00226 self.component.verticalPositionMode = vertMode
00227 self.component.heightMode = heightMode
00228
00229 return restricted
00230
00231
00232
00233 dragManager = ComponentDragManager()
00234