# HG changeset patch # User laurent # Date 1332885310 -7200 # Node ID 46c3d54108881fab840c09d47c14b714c2d23b93 # Parent 89d20745b0615a99cc7102c00ba40aa75bd42bee Improving HitTest on Function Blocks with long names so that they only answer to click on visible parts instead of on the whole bounding box diff -r 89d20745b061 -r 46c3d5410888 graphics/FBD_Objects.py --- a/graphics/FBD_Objects.py Tue Mar 27 23:50:19 2012 +0200 +++ b/graphics/FBD_Objects.py Tue Mar 27 23:55:10 2012 +0200 @@ -113,24 +113,42 @@ def RefreshExecutionOrderSize(self): self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) - # Refresh the block bounding box - def RefreshBoundingBox(self): + # Returns if the point given is in the bounding box + def HitTest(self, pt, connectors=True): + if self.Name != "": + test_text = self.GetTextBoundingBox().InsideXY(pt.x, pt.y) + else: + test_text = False + test_block = self.GetBlockBoundingBox(connectors).InsideXY(pt.x, pt.y) + return test_text or test_block + + # Returns the bounding box of the name outside the block + def GetTextBoundingBox(self): # Calculate the size of the name outside the block text_width, text_height = self.NameSize - # Calculate the bounding box size - bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2) - bbx_width = max(self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE, text_width) - if self.Name != "": - bbx_y = self.Pos.y - (text_height + 2) - bbx_height = self.Size[1] + (text_height + 2) - else: - bbx_y = self.Pos.y - bbx_height = self.Size[1] + return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2, + self.Pos.y - (text_height + 2), + text_width, + text_height) + + # Returns the bounding box of function block without name outside + def GetBlockBoundingBox(self, connectors=True): + bbx_x, bbx_y = self.Pos.x, self.Pos.y + bbx_width, bbx_height = self.Size + if connectors: + bbx_x -= min(1, len(self.Inputs)) * CONNECTOR_SIZE + bbx_width += (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE if self.ExecutionOrder != 0: bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0]) bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0]) bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2) - self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) + return wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) + + # Refresh the block bounding box + def RefreshBoundingBox(self): + self.BoundingBox = self.GetBlockBoundingBox() + if self.Name != "": + self.BoundingBox.Union(self.GetTextBoundingBox()) # Refresh the positions of the block connectors def RefreshConnectors(self):