How yo use the mouse to create line?


#1

I just wanna make a line by using mouse but it is not work… :frowning:

import direct.directbase.DirectStart
from direct.task.Task import Task
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
import sys



class World(DirectObject):
  def __init__(self):



    base.disableMouse()                  
    camera.setPos(0,-10, 0)              

                          
    self.drawingLine()
    taskMgr.add(self.mousePos, "mousePos")

  def drawingLine(self):

    self.line = LineSegs()

    self.line.setThickness(4)
    self.line.setColor(1,0,0)
    self.line.moveTo(0,0,0)
   # self.line.drawTo(1,0,1)
    line = self.line.create()
    renderLine = NodePath(line)
    renderLine.reparentTo(render)

  def mousePos(self, task):

    if base.mouseWatcherNode.hasMouse():

      mpos = base.mouseWatcherNode.getMouse()

      self.line.drawTo((mpos.getX())*4,0,(mpos.getY())*3)
      print (mpos.getY())*5


    return Task.cont                        #Task continues infinitely



w = World()        
run()           

any wrong in my code?


#2

You need to call these lines:

    line = self.line.create()
    renderLine = NodePath(line)
    renderLine.reparentTo(render) 

after every time you call self.line.drawTo(), not just at the beginning, since self.line.create() only creates the line you have drawn up till that point. (The line you create isn’t automatically extended when self.line changes.)

But there are other problems, too. You’ll also need to remove the line you created last time, or they’ll just accumulate on top of each other and really slow down your frame rate. That means you’ll need to save the previous results.

You’ll probably also want to attach the line geometry to render2d, not render (and then you won’t need to scale the mouse coordinates.)

This will look something like this:

    if self.renderLine:
        self.renderLine.removeNode()
    line = self.line.create()
    self.renderLine = NodePath(line)
    self.renderLine.reparentTo(render2d) 

And, of course, in your setup you’ll have to initialize self.renderLine = None.

David


#3

Not sure which one you want, just switch between drawingLine or drawingLine2.

from pandac.PandaModules import *
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task


class World(DirectObject):
  def __init__(self):
    self.drawingLine()
#     self.drawingLine2()

  def drawingLine(self):
    self.line = LineSegs()
    self.line.setThickness(4)
    self.line.setColor(1,0,0)
    self.lineNP=NodePath('')
    self.points=[]
    self.lastMpos=None
    taskMgr.add(self.mousePos, "mousePos")

  def mousePos(self, task):
    if base.mouseWatcherNode.hasMouse():
       mpos = base.mouseWatcherNode.getMouse()
       if mpos!=self.lastMpos:
          if self.lastMpos:
             self.points.append(Point3(self.lastMpos[0],0,self.lastMpos[1]))
          else:
             self.points.append(Point3(mpos[0],0,mpos[1]))
          self.line.reset()
          self.line.moveTo(self.points[0])
          for p in self.points[1:]:
              self.line.drawTo(p)
          self.lineNP.removeNode()
          self.lineNP=render2d.attachNewNode(self.line.create())
          self.lastMpos=Point2(mpos)
    return Task.cont    #Task continues infinitely




  def drawingLine2(self):
    self.line = LineSegs()
    self.line.setThickness(4)
    self.line.setColor(1,0,0)
    self.line.moveTo(0,0,0)
    self.line.drawTo(0,0,0)
    self.lineNP=render2d.attachNewNode(self.line.create())

    self.lastMpos=Point2(0)
    self.points=[Point3(self.lastMpos[0],0,self.lastMpos[1])]
    self.points.append(Point3(self.points[-1]))
    self.accept('mouse1',self.nailPoint)
    taskMgr.add(self.mousePos2, "mousePos2")

  def mousePos2(self, task):
    if base.mouseWatcherNode.hasMouse():
       mpos = base.mouseWatcherNode.getMouse()
       if mpos!=self.lastMpos:
          self.line.setVertex(len(self.points)-1,mpos[0],0,mpos[1])
          self.lastMpos=Point2(mpos)
    return Task.cont    #Task continues infinitely

  def nailPoint(self):
    self.points[-1]=self.line.getVertex(len(self.points)-1)
    self.points.append(Point3(self.points[-1]))
    self.line.reset()
    self.line.moveTo(self.points[0])
    for p in self.points[1:]:
        self.line.drawTo(p)
    self.lineNP.removeNode()
    self.lineNP=render2d.attachNewNode(self.line.create())

w = World()
run()