diff --git a/components/scg/base/scg_editor.py b/components/scg/base/scg_editor.py index d1b3f14..580f4c4 100644 --- a/components/scg/base/scg_editor.py +++ b/components/scg/base/scg_editor.py @@ -177,7 +177,28 @@ def _createContour(self, points): sheet.addChild(contour) contour.setPoints(points) contour.setState(objects.Object.OS_Normal) + return contour - - + def _createLine(self,begin, end): + """Create line + """ + import random + line = render_engine._ogreSceneManager.createManualObject("temp_"+str(random.random())) + myManualObjectNode = render_engine._ogreSceneManager.getRootSceneNode().createChildSceneNode("temp_node"+str(random.random())) + line.setDynamic(True) + + manualObjectMaterial = ogre.MaterialManager.getSingleton().create("temp_material","General") + manualObjectMaterial.setReceiveShadows(False); + manualObjectMaterial.getTechnique(0).setLightingEnabled(True); + manualObjectMaterial.getTechnique(0).getPass(0).setDiffuse(0,0,1,0); + manualObjectMaterial.getTechnique(0).getPass(0).setAmbient(0,0,1); + manualObjectMaterial.getTechnique(0).getPass(0).setSelfIllumination(0,0,1); + + line.begin("temp_material", ogre.RenderOperation.OT_LINE_LIST) + line.position(begin) + line.position(end) + line.end() + myManualObjectNode.attachObject(line) + + return myManualObjectNode \ No newline at end of file diff --git a/components/scg/base/scg_modes.py b/components/scg/base/scg_modes.py index 3236893..dc757a0 100644 --- a/components/scg/base/scg_modes.py +++ b/components/scg/base/scg_modes.py @@ -177,6 +177,10 @@ def __init__(self, _logic): # button.eventPush = self._onToolBarButtonPush # # self.toolbar.setButtonSize(38) + + #contour points + self.contour_points = [] + self.contour_lines = [] def __del__(self): @@ -397,12 +401,20 @@ def _onMousePressed(self, _evt, _id): # * if pressed left button and there are no objects under mouse, then # creating new scg-node elif _id == ois.MB_Right: + #check if modifire key pressed + is_modifire_down = render_engine._oisKeyboard.isModifierDown(render_engine._oisKeyboard.Modifier.Shift) # check objects under mouse if self.state is SCgEditMode.ES_None: if len(mobjects) is 0: - self._logic._createNode((mstate.X.abs, mstate.Y.abs)) - self.state = SCgEditMode.ES_None - return True + #check if contour draw mode + if is_modifire_down: + self.contour_points.append(render_engine.pos2dTo3dIsoPos([mstate.X.abs, mstate.Y.abs])) + self.state = SCgEditMode.ES_ContourCreate + + else: + self._logic._createNode((mstate.X.abs, mstate.Y.abs)) + self.state = SCgEditMode.ES_None + return True else: if self.line_mode_beg is None: self.state = SCgEditMode.ES_LineCreate @@ -417,7 +429,26 @@ def _onMousePressed(self, _evt, _id): self._updateLineCreationObjects() return True - + # contour creation state + elif self.state is SCgEditMode.ES_ContourCreate: + sheet=self._logic._getSheet() + self.contour_points.append(render_engine.pos2dTo3dIsoPos(((mstate.X.abs, mstate.Y.abs)))) + self.contour_lines.append(self._logic._createLine(self.contour_points[-1],self.contour_points[-2])) + if is_modifire_down and len(self.contour_points)>2: + contour = self._logic._createContour(self.contour_points) + for line in self.contour_lines: + render_engine._ogreSceneManager.getRootSceneNode().removeChild(line) + self.contour_lines = [] + sheet.removeChild(contour) + childs = sheet.getChilds() + for i in range(len(childs)): + current = childs[0] + if scg_utils.checkOnPointInContour(current.position,self.contour_points): + sheet.removeChild(current) + render_engine.SceneNode.addChild(contour.sceneNode, current.sceneNode) + self._logic._getSheet().addChild(contour) + self.contour_points = [] + self.state = SCgEditMode.ES_None # line creation state elif (self.state is SCgEditMode.ES_LineCreate) and (self.line_mode_beg is not None): # check if is there diff --git a/components/scg/base/scg_utils.py b/components/scg/base/scg_utils.py index ab555b0..12d154a 100644 --- a/components/scg/base/scg_utils.py +++ b/components/scg/base/scg_utils.py @@ -98,3 +98,38 @@ def createWindowFromNode(_node, _format, _is_edit): return sheet + +def checkOnPointInContour(point, polygon): + if len(polygon) < 3: + return False + q_patt = ((0,1), (3,2)); + + import ogre.renderer.OGRE as ogre + + pred_pt = ogre.Vector3(polygon[-1].x, polygon[-1].y, 0) + pred_pt.x -= point.x + pred_pt.y -= point.y + + pred_q=q_patt[pred_pt.y<0][pred_pt.x<0]; + + w = 0 + + for cur_pt in polygon: + cur_pt = ogre.Vector3(cur_pt.x, cur_pt.y, 0) + cur_pt.x -= point.x + cur_pt.y -= point.y + + q=q_patt[cur_pt.y<0][cur_pt.x<0]; + + if q-pred_q == -3: + w+=1 + if q-pred_q == 3: + w-=1 + if q-pred_q == -2: + if pred_pt[0]*cur_pt[1]>=pred_pt[1]*cur_pt[0]: w+=1 + if q-pred_q == 2: + if not (pred_pt[0]*cur_pt[1]>=pred_pt[1]*cur_pt[0]): w-=1 + + pred_pt = cur_pt + pred_q = q + return w != 0