WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-27-2010, 12:12 AM
renama renama is offline
Member
 
Join Date: Jun 2010
Posts: 15
Unhappy OO programing in vizard?

I tried to create a class to put an icon on the screen. When the icon is clicked, the music will be truned on or off.

In the class, I used callback viz.MOUSEDOWN_EVENT to capture the mouse clicked event. The class works fine. The problem is: I can not use viz.MOUSEDOWN_EVENT in main program or in other classes anymore once this class is used. That is a disaster. How to solve this problem while keeping the OO programing structure? Is there any other way to encapsulate the event operation in the class?

Thanks so much if anyone can help.

My class like this:
Attached Files
File Type: doc Class_MusicOnOff.doc (27.0 KB, 877 views)
Reply With Quote
  #2  
Old 06-27-2010, 12:47 AM
renama renama is offline
Member
 
Join Date: Jun 2010
Posts: 15
Here is the code of the class (same with the previous attachement)
Code:
class MusicOnOff:
       def __init__(self,musicObj):
             self. Obj = musicObj
             self.musicIcon = viz.addTexQuad()
             self.musicIcon.texture(viz.add((‘musicOff.png')))
             self.musicOn = True
             viz.callback( viz.MOUSEDOWN_EVENT, self.onMouseDown )
       def onMouseDown(self, button):
             if button == viz.MOUSEBUTTON_LEFT:
                   object = viz.pick()
                   if object.valid() == False: return
                   if object == self.musicIcon:
                          if self.musicOn == True:
                                 self.musicOn = False
                                 self.musicIcon.texture(viz.add((‘musicOn.png')))
                                 self.Obj.pause()				
                          else:
                                 self.musicOn = True
                                 self.musicIcon.texture(viz.add((‘musicOff.png')))
                                 self.Obj.play()

Last edited by renama; 06-27-2010 at 12:52 AM.
Reply With Quote
  #3  
Old 06-27-2010, 10:34 AM
IGoudt IGoudt is offline
Member
 
Join Date: Sep 2009
Posts: 20
From my experience, you can register events only once.
For example:

Class A registers a viz.MOUSEDOWN_EVENT
Class B registers a viz.MOUSEDOWN_EVENT

Then if I recall correctly the last register will overwrite the first. What I did to resolve this, is to put all the registers in one of the main, or container, classes. Whenever an event is triggered there, the main class redirects the event to the appropiate classes or objects.

In your case, I guess your main class creates an instance of the MusicOnOff class (a button), and saves it under a variable. Change to:

Code:
class MusicOnOff:
       def __init__(self,musicObj):
             self.Obj = musicObj
             self.musicIcon = viz.addTexQuad()
             self.pictures = [viz.add(‘musicOff.png'), viz.add('musicOn.png')]
             self.musicIcon.texture(self.pictures[1])
             self.musicOn = True
             
             
       def click(self):
             
             self.musicOn = not self.musicOn
             self.musicIcon.texture(self.pictures[self.musicOn])
             if not self.musicOn:
                    
                    self.Obj.pause()				
             else:
                    self.Obj.play()
Reply With Quote
  #4  
Old 06-27-2010, 10:03 PM
renama renama is offline
Member
 
Join Date: Jun 2010
Posts: 15
Thanks, IGoudt. You gave me a good idea for OO programming. I appreciate your help. It is your support that makes me keep searching and trying to work on it. Today I found vizact.onpick(object,fun,arg) can be used in this case. It can be called multiple times. Hope this found can help somebody who has the same coding problem in this situation
Reply With Quote
  #5  
Old 06-28-2010, 09:30 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I highly suggest you read the section in the documentation about events, especially the Event Basics page. It describes how to register multiple callbacks for an event using viz.EventClass.
Reply With Quote
  #6  
Old 06-28-2010, 02:33 PM
renama renama is offline
Member
 
Join Date: Jun 2010
Posts: 15
Thanks farshizzo, that is great. I need register other events,like mouse move in my class and creating Event Class solved my problem completely.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Realistic Light and Shadows Using Vizard and 3DS Max jde Vizard 4 07-13-2012 10:58 AM
.3DS importing in Vizard and 3D Studio jde Vizard 1 08-28-2009 03:14 PM
Vizard tech tip: Using the Python Imaging Library (PIL) Jeff Vizard 0 03-23-2009 11:13 AM
Vizard tech tip: Text to Speech Jeff Vizard 1 01-15-2009 09:39 PM
Vizard and Augmented Reality realvision Vizard 4 04-04-2008 10:59 AM


All times are GMT -7. The time now is 07:46 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC