#import <simtools.h>
#import <objectbase/SwarmObject.h>
#import <analysis.h>
#import  <misc.h> // sin
#import <tkobjc/global.h>

@interface Agent: SwarmObject
- compute: xPtr: yPtr;
@end

@implementation Agent
- compute: xPtr: yPtr
{
  double x = *(double *)xPtr;
  double y = sin (x);

  *(double *)yPtr = y;
  return (id)YES;
}
@end

@interface Controller: SwarmObject
{
  id <FunctionGraph> fg;
  id <GraphElement> fe;
  id <Graph> graph;
  id agent;
  id panel;
}
- createEnd;
- graph;
- exit;
@end

@implementation Controller

- createEnd
{
  id fgd;

  agent = [Agent create: [self getZone]];

  fgd = [Frame create: [self getZone]];
  
  graph = [Graph createParent: fgd];
  [graph setWindowTitle: "FunctionGraph"];

  [graph pack];

  fe = [graph createElement];
  [fe setLabel: "myLabel"];
  [fe setColor: "red"];
  [fe setWidth: 0];    
  [fe setSymbol: "circle"];
  [globalTkInterp eval: "%s element configure %s -pixels 0.01i",
                  [graph getWidgetName],
                  [fe getName]];
  
  fg = [FunctionGraph createBegin: [self getZone]];
  [fg setElement: fe];
  [fg setDataFeed: agent];
  [fg setFunctionSelector: M(compute::)];
  [fg setXMin: 0.0 Max: 2.0 * M_PI Resolution: 100];
  [fg setResetFrequency: 0];
  fg = [fg createEnd]; 

  panel = [ButtonPanel createBegin: [self getZone]];
  [panel setButtonTarget: self];
  panel = [panel createEnd];
  [panel setWindowTitle: "ControlPanel"];
  [panel addButtonName: "Exit" method: @selector (exit)];

  return self;
}

- exit
{
  exit (0);
}

- graph
{
  [fg graph];

  return self;
}
@end

main (int argc, const char **argv)
{ 
  id controller;

  initSwarm (argc, argv);

  controller = [Controller create: globalZone];
  
  [controller graph];
  
  while (1)
    {
      while (GUI_EVENT_ASYNC ()) {}
    }  
}

/*This is for MS windows:*/
/*
Local Variables:
compile-command: "gcc -DDLL -o funcgraph -g -Wno-import -I$SWARMHOME/include -L$SWARMHOME/lib funcgraph.m -lswarmdll -lobjcdll"
End:
*/

/*For RedHat linux, use this instead: 
/*
Local Variables:
compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -D_GNU_SOURCE -DDLL -o funcgraph -g -Wno-import -I$SWARMHOME/include -I$SWARMHOME/include/swarm -L/$SWARMHOME/lib/swarm -L$SWARMHOME/lib funcgraph.m -lobjc -lswarm"
End:
*/
*/end of "For RedHat..." comment






