home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / source / things.m < prev    next >
Encoding:
Text File  |  1996-08-08  |  5.6 KB  |  318 lines

  1.  
  2. #import "qedefs.h"
  3.  
  4. id    things_i;
  5.  
  6. @implementation Things
  7.  
  8. - init
  9. {
  10.     [super init];
  11.  
  12.     things_i = self;
  13.     lastSelected = 0;
  14.     
  15.     return self;
  16. }
  17.  
  18. //
  19. //    Load the TEXT object with the entity comment
  20. //
  21. - loadEntityComment:(id)obj
  22. {
  23.     [entity_comment_i selectAll:self];
  24.     [entity_comment_i replaceSel:[obj comments]];
  25.  
  26.     return self;
  27. }
  28.  
  29.  
  30. - initEntities
  31. {    
  32.     char    *path;
  33.  
  34.     path = [project_i getProgDirectory];
  35.  
  36.     [prog_path_i setStringValue: path];
  37.     
  38.     [[EntityClassList alloc] initForSourceDirectory: path];
  39.  
  40.     [self loadEntityComment:[entity_classes_i objectAt:lastSelected]];
  41.     [entity_browser_i loadColumnZero];
  42.     [[entity_browser_i matrixInColumn:0] selectCellAt:lastSelected :0];
  43.  
  44.     [entity_browser_i setDoubleAction: @selector(doubleClickEntity:)];
  45.     
  46.     return self;
  47. }
  48.  
  49. - selectEntity: sender
  50. {
  51.     id        matr;
  52.     
  53.     matr = [sender matrixInColumn: 0];
  54.     lastSelected = [matr selectedRow];
  55.     [self loadEntityComment:[entity_classes_i objectAt:lastSelected]];
  56.     [quakeed_i makeFirstResponder: quakeed_i];
  57.     
  58.     return self;
  59. }
  60.  
  61. - doubleClickEntity: sender
  62. {
  63.     [map_i makeEntity: sender];
  64.     [quakeed_i makeFirstResponder: quakeed_i];
  65.     return self;
  66. }
  67.  
  68. - (char *)spawnName
  69. {
  70.     return [[entity_classes_i objectAt:lastSelected] classname];
  71. }
  72.  
  73.  
  74. //
  75. //    Flush entity classes & reload them!
  76. //
  77. - reloadEntityClasses: sender
  78. {
  79.     EntityClass *ent;
  80.     char    *path;
  81.     
  82.     path = (char *)[prog_path_i stringValue];
  83.     if (!path || !path[0])
  84.     {
  85.         path = [project_i getProgDirectory];
  86.         [prog_path_i setStringValue: path];
  87.     }
  88.     
  89.     //    Free all entity info in memory...
  90.     [entity_classes_i freeObjects];
  91.     [entity_classes_i free];
  92.     
  93.     //    Now, RELOAD!
  94.     [[EntityClassList alloc] initForSourceDirectory: path];
  95.  
  96.     lastSelected = 0;
  97.     ent = [entity_classes_i objectAt:lastSelected];
  98.     [self loadEntityComment:[entity_classes_i objectAt:lastSelected]];
  99.  
  100.     [entity_browser_i loadColumnZero];
  101.     [[entity_browser_i matrixInColumn:0] selectCellAt:lastSelected :0];
  102.  
  103.     [self newCurrentEntity];    // in case flags changed
  104.     
  105.     return self;
  106. }
  107.  
  108.  
  109. - selectClass: (char *)class
  110. {
  111.     id        classent;
  112.         
  113.     classent = [entity_classes_i classForName:class];
  114.     if (!classent)
  115.         return self;
  116.     lastSelected = [entity_classes_i indexOf: classent];
  117.     
  118.     if (lastSelected < 0)
  119.         lastSelected = 0;
  120.         
  121.     [self loadEntityComment:classent];
  122.     [[entity_browser_i matrixInColumn:0] selectCellAt:lastSelected :0];
  123.     [[entity_browser_i matrixInColumn:0] scrollCellToVisible:lastSelected :0];
  124.  
  125.     return self;
  126. }
  127.  
  128.  
  129. - newCurrentEntity
  130. {
  131.     id        ent, classent, cell;
  132.     char    *classname;
  133.     int        r, c;
  134.     char    *flagname;
  135.     int        flags;
  136.     
  137.     ent = [map_i currentEntity];
  138.     classname = [ent valueForQKey: "classname"];
  139.     if (ent != [map_i objectAt: 0])
  140.         [self selectClass: classname];    // don't reset for world
  141.     classent = [entity_classes_i classForName:classname];
  142.     flagname = [ent valueForQKey: "spawnflags"];
  143.     if (!flagname)
  144.         flags = 0;
  145.     else
  146.         flags = atoi(flagname);
  147.     
  148.     [flags_i setAutodisplay: NO];
  149.     for (r=0 ; r<4 ; r++)
  150.         for (c=0 ; c<3 ; c++)
  151.         {
  152.             cell = [flags_i cellAt: r : c];
  153.             if (c < 2)
  154.             {
  155.                 flagname = [classent flagName: c*4 + r];
  156.                 [cell setTitle: flagname];
  157.             }
  158.             [cell setIntValue: (flags & (1<< ((c*4)+r)) ) > 0];
  159.         }
  160.     [flags_i setAutodisplay: YES];
  161.     [flags_i display];
  162.     
  163. //    [keyInput_i setStringValue: ""];
  164. //    [valueInput_i setStringValue: ""];
  165.  
  166.     [keypairview_i calcViewSize];
  167.     [keypairview_i display];
  168.     
  169.     [quakeed_i makeFirstResponder: quakeed_i];
  170.     return self;
  171. }
  172.  
  173. //
  174. //    Clicked in the Keypair view - set as selected
  175. //
  176. - setSelectedKey:(epair_t *)ep;
  177. {
  178.     [keyInput_i setStringValue:ep->key];
  179.     [valueInput_i setStringValue:ep->value];
  180.     [valueInput_i    selectText:self];
  181.     return self;
  182. }
  183.  
  184. - clearInputs
  185. {
  186. //    [keyInput_i setStringValue: ""];
  187. //    [valueInput_i setStringValue: ""];
  188.     
  189.     [quakeed_i makeFirstResponder: quakeed_i];
  190.     return self;
  191. }
  192.  
  193. //
  194. //    Action methods
  195. //
  196.  
  197. -addPair:sender
  198. {
  199.     char    *key, *value;
  200.     
  201.     key = (char *)[keyInput_i stringValue];
  202.     value = (char *)[valueInput_i stringValue];
  203.     
  204.     [ [map_i currentEntity] setKey: key toValue: value ];
  205.  
  206.     [keypairview_i calcViewSize];
  207.     [keypairview_i display];
  208.  
  209.     [self clearInputs];
  210.     [quakeed_i updateXY];
  211.     
  212.     return self;
  213. }
  214.  
  215. -delPair:sender
  216. {
  217.     [quakeed_i makeFirstResponder: quakeed_i];
  218.  
  219.     [ [map_i currentEntity] removeKeyPair: (char *)[keyInput_i stringValue] ];
  220.  
  221.     [keypairview_i calcViewSize];
  222.     [keypairview_i display];
  223.  
  224.     [self clearInputs];
  225.  
  226.     [quakeed_i updateXY];
  227.  
  228.     return self;
  229. }
  230.  
  231.  
  232. //
  233. //    Set the key/value fields to "angle <button value>"
  234. //
  235. - setAngle:sender
  236. {
  237.     const char *title;
  238.     char    value[10];
  239.     
  240.     title = [[sender selectedCell] title];
  241.     if (!strcmp(title,"Up"))
  242.         strcpy (value, "-1");
  243.     else if (!strcmp(title,"Dn"))
  244.         strcpy (value, "-2");
  245.     else
  246.         strcpy (value, title);
  247.     
  248.     [keyInput_i setStringValue:"angle"];
  249.     [valueInput_i setStringValue:value];
  250.     [self addPair:NULL];
  251.     
  252.     [self clearInputs];
  253.  
  254.     [quakeed_i updateXY];
  255.     
  256.     return self;
  257. }
  258.  
  259. - setFlags:sender
  260. {
  261.     int        flags;
  262.     int        r, c, i;
  263.     id        cell;
  264.     char    str[20];
  265.     
  266.     [self clearInputs];
  267.     flags = 0;
  268.  
  269.     for (r=0 ; r<4 ; r++)
  270.         for (c=0 ; c<3 ; c++)
  271.         {
  272.             cell = [flags_i cellAt: r : c];
  273.             i = ([cell intValue] > 0);
  274.             flags |= (i<< ((c*4)+r));
  275.         }
  276.     
  277.     if (!flags)
  278.         [[map_i currentEntity] removeKeyPair: "spawnflags"];
  279.     else
  280.     {
  281.         sprintf (str, "%i", flags);
  282.         [[map_i currentEntity] setKey: "spawnflags" toValue: str];
  283.     }
  284.     
  285.     [keypairview_i calcViewSize];
  286.     [keypairview_i display];
  287.  
  288.     return self;
  289. }
  290.  
  291.  
  292. //
  293. //    Fill the Entity browser
  294. //    (Delegate method - delegated in Interface Builder)
  295. //
  296. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  297. {
  298.     id        cell;
  299.     int        max;
  300.     int        i;
  301.     id        object;
  302.     
  303.     max = [entity_classes_i count];
  304.     i = 0;
  305.     while(max--)
  306.     {
  307.         object = [entity_classes_i objectAt:i];
  308.         [matrix addRow];
  309.         cell = [matrix cellAt:i++ :0];
  310.         [cell setStringValue:[object classname]];
  311.         [cell setLeaf:YES];
  312.         [cell setLoaded:YES];
  313.     }
  314.     return i;
  315. }
  316.  
  317. @end
  318.