FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Storing function names in classes
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Storing function names in classes  (Read 539 times)
Markavian

Markavian+iTX WWW
Storing function names in classes
« on: Jul 9th, 2004, 12:10am »

I'm starting a new project, and predict I might have a problem.
 
I'm creating a menu class, which stores the text for menu items and a function name. At the moment I have set both up as strings.
 
Code:
class cr_menu_item() {
  // Constructor
  void cr_menu_item(String text, String function) {
    String text = text;
    String function = function;
  }
}

 
A separate class/function will build the menu out of these items and track rollovers. Is it possible to execute named functions based off variables?
i.e. variable function execution?
possible e.g.:
Code:
/*?????
 
void cr_launch_website() {
  // do stuff
}
 
String myFunction = "cr_launch_website";
run(myFunction);
 
?????*/

 
I'll go check the reference, I don't remember seeing anything though.
 
Many thanks to anyone who can help.
 
Regards,
- Markavian
 
arielm

WWW
Re: Storing function names in classes
« Reply #1 on: Jul 9th, 2004, 12:53am »

java has a "reflection" mechanism which enable to invoke methods via string names, but generally it's a bit overkill (unless you code something like a javascript interpreter...)
 
for menu matters, you should take a look at how it's done within pure-java apps... the pattern is going like that:
 
- you have a "menu" object, with child menu-items.
 
- it's possible to "add a listener" object to "menu"
 
- each time a menu item is activated: "menu" is going over its list of "listeners" and is "casting an event" (e.g invoking a "menuSelected" method) with some kind of id, representing the item in question
 
- so... your "listener" object's menuSelected() method is called with an id as a parameter
 
- from here, it's a matter of using a switch-case construct or a series of if's (switch case is preferable when there's a lot of options, it only implies that the passed "id" is an int)
 
hth!
 
EDIT:
 
here's an home-made event casting system from my ttc work:
 
Code:
public interface EventListener
{
  public void event(EventCaster source, int message);
}
 
public class EventCaster
{
  public Vector listeners;
 
  public void addListener(EventListener listener)
  {
    if (listeners == null)
    {
      listeners = new Vector();
    }
    if (!listeners.contains(listener))
    {
      listeners.addElement(listener);
    }
  }
 
  public void removeListener(EventListener listener)
  {
    listeners.removeElement(listener);
  }
 
  public void castEvent(int message)
  {
    for (int i = listeners.size() - 1; i > -1; i--)
    {
      ((EventListener) listeners.elementAt(i)).event(this, message);
    }
  }
}

(ouch... word-wrapping is tough with this forum!)
 
that was for the structure, from here:
 
1) your Menu class should "extend" EventCaster
2) your MenuObserver class should "implement" EventListener
 
+ in the setup-part of your code, you should have something like:
 
myMenu.addListener(myMenuObserver)
 
then, inside your Menu class, when a menu-item (say, an item symbolised by MENU_CLOSED) is selected, it should go like:
 
castEvent(MENU_CLOSED);
(it implies that MENU_CLOSED is defined with an int value somewhere...)
 
and of course, inside your MenuObserver, you have something like:
 
Code:
void event(EventCaster source, int message)
{
  if (source == myMenu && message == MENU_CLOSED)
  {
    foo();
  }
}

 
final note: the advantage of passing a "source" object in addition of a message/id, when casting an event is that the same "listener" gets the ability to respond to, say, different menu instances...
« Last Edit: Jul 29th, 2004, 3:35am by arielm »  

Ariel Malka | www.chronotext.org
Markavian

Markavian+iTX WWW
Re: Storing function names in classes
« Reply #2 on: Jul 9th, 2004, 11:35pm »

Hi Ariel,
 
I generally understand what you are saying (after reading it through a couple of times), but I don't quite understand your code, how it will benefit me, or extending and implementing classes work.
 
I understand what you mean about creating classes for each menu containing the logic and actions, and using a generic class containing the code for detecting states in another class, but I'm not sure the examples you gave are quite appropriate for what I need.
 
If you could give some basic examples about extending classes, or some useful links; then we may have solved my problem by building my menu in a different way.
 
(btw: I've got some follow up work on TTC, but its not really ready at the moment)
 
Best regards,
- Markavian
 
arielm

WWW
Re: Storing function names in classes
« Reply #3 on: Jul 10th, 2004, 11:56pm »

on Jul 9th, 2004, 11:35pm, Markavian wrote:
I generally understand what you are saying (after reading it through a couple of times), but I don't quite understand your code, how it will benefit me, or extending and implementing classes work.)

lol... bah, forget this brutal eruption of java geekness (i'm coding too much these days!)
 
on Jul 9th, 2004, 11:35pm, Markavian wrote:
(btw: I've got some follow up work on TTC, but its not really ready at the moment)
cool!
 

Ariel Malka | www.chronotext.org
Pages: 1 

« Previous topic | Next topic »