How yo use the mouse to create line?

Panda3D Forum Index -> Scripting Issues Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Kinzia


Posts: 13

PostPosted: Thu Nov 06, 2008 3:39 am    Post subject: How yo use the mouse to create line? Reply with quote
I just wanna make a line by using mouse but it is not work.... Sad

Code:
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?
_________________
I'm newbie
drwr


Posts: 8006
Location: Glendale, CA

PostPosted: Thu Nov 06, 2008 11:06 am    Post subject: Reply with quote
You need to call these lines:
Code:
    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:
Code:
    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
ynjh_jo


Posts: 1596
Location: Malang, Indonesia

PostPosted: Thu Nov 06, 2008 11:48 am    Post subject: Reply with quote
Not sure which one you want, just switch between drawingLine or drawingLine2.
Code:
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()

_________________
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | Elixir 1.5GB | ATI HD4670 1GB GDDR3
Display posts from previous:   
Post new topic   Reply to topic    Panda3D Forum Index -> Scripting Issues All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group