home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Games / Showwit! 1.0 / source code / Source / CShLevelsTable.p < prev    next >
Encoding:
Text File  |  1996-02-03  |  6.3 KB  |  237 lines  |  [TEXT/PJMM]

  1. {****************************************************}
  2. {}
  3. {        CShLevelsTable.p                                                                                                                                                                                        }
  4. {}
  5. {        Array pane class for displaying the list of levels held by the game director.            }
  6. {}
  7. {****************************************************}
  8.  
  9.  
  10. unit CShLevelsTable;
  11.  
  12. interface
  13.  
  14.     uses
  15.         TCL, ShIntf;
  16.  
  17. implementation
  18.  
  19.     const
  20.         kNumh = 0;
  21.         kNameh = 35;
  22.         kPlayerh = 190;
  23.         kMovesh = 320;
  24.         kTimeh = 380;
  25.  
  26.  
  27. {****************************************************}
  28. {}
  29. {        CShLevelsTable                                                                                                                                                                                                }
  30. {}
  31. {        Construction of the levels table object.                                                                                                                        }
  32. {}
  33. {****************************************************}
  34.  
  35.     procedure CShLevelsTable.IShLevelsTable (anEnclosure: CView;
  36.                                     aSupervisor: CShGameDirector;
  37.                                     aWidth, aHeight, aHEncl, aVEncl: Integer;
  38.                                     aHSizing, aVSizing: SizingOption);
  39.  
  40.     begin { IShLevelsTable }
  41.         itsGameDirector := aSupervisor;
  42.  
  43.         IArrayPane(anEnclosure, aSupervisor, aWidth, aHeight, aHEncl, aVEncl, aHSizing, aVSizing);
  44.  
  45.         FitToEnclosure(kDoHorizontal, kDoVertical);
  46.         SetSelectionFlags(selOnlyOne);
  47.         SetDblClickCmd(cmdClickStart);
  48.     end; { IShLevelsTable }
  49.  
  50.  
  51. {****************************************************}
  52. {}
  53. {        Free                                                                                                                                                                                                                                }
  54. {}
  55. {        Destruction of the levels table object. We set our pointers to nil, for the                    }
  56. {        objects to which they were pointing will be disposed of in inherited methods.        }
  57. {}
  58. {****************************************************}
  59.  
  60.     procedure CShLevelsTable.Free;
  61.  
  62.     begin { Free }
  63.         itsGameDirector := nil;
  64.  
  65.         inherited Free;
  66.     end; { Free }
  67.  
  68.  
  69. {****************************************************}
  70. {}
  71. {        DoCommand                                                                                                                                                                                                            }
  72. {}
  73. {        The levels table is responsible for clearing the players from its array,                         }
  74. {        and then bouncing the command to the game director.                                                                                }
  75. {}
  76. {****************************************************}
  77.  
  78.     procedure CShLevelsTable.DoCommand (theCommand: longint);
  79.  
  80.         procedure Level_SetDefaultBestPlayer (aLevel: CShLevel);
  81.  
  82.         begin { Level_SetDefaultBestPlayer }
  83.             aLevel.SetDefaultBestPlayer;
  84.         end; { Level_SetDefaultBestPlayer }
  85.  
  86.     begin { DoCommand }
  87.         if theCommand = cmdClearBestPlayers then begin
  88.             CCluster(GetArray).DoForEach(Level_SetDefaultBestPlayer);
  89.             Refresh;
  90.         end; { if }
  91.         inherited DoCommand(theCommand); { Pass on to application to do its part. }
  92.     end; { DoCommand }
  93.  
  94.  
  95. {****************************************************}
  96. {}
  97. {        UpdateMenus                                                                                                                                                                                                        }
  98. {}
  99. {        The levels table is responsible for enabling the clear best players command.            }
  100. {}
  101. {****************************************************}
  102.  
  103.     procedure CShLevelsTable.UpdateMenus;
  104.  
  105.     begin { UpdateMenus }
  106.         inherited UpdateMenus;
  107.  
  108.         if not itsGameDirector.IsPlaying then begin
  109.             gBartender.EnableCmd(cmdClearBestPlayers);
  110.         end; { if }
  111.     end; { UpdateMenus }
  112.  
  113.  
  114. {****************************************************}
  115. {}
  116. {        DoKeyDown                                                                                                                                                                                                            }
  117. {}
  118. {        Do the double click command if return/enter is pressed.                                                                        }
  119. {}
  120. {****************************************************}
  121.  
  122.     procedure CShLevelsTable.DoKeyDown (theChar: Char;
  123.                                     keyCode: Byte;
  124.                                     macEvent: EventRecord);
  125.  
  126.     begin { DoKeyDown }
  127.         { If we are playing a game, then the levels table ignores all keystrokes. }
  128.         { That is, effectively it doesn't exist. }
  129.         if not itsGameDirector.IsPlaying then begin
  130.             if (theChar = kReturnKey) | (theChar = kEnterKey) then begin
  131.                 { The double click command is to simulate a click on the start button. }
  132.  
  133.                 DoCommand(dblClickCmd);
  134.             end { if }
  135.             else begin
  136.                 inherited DoKeyDown(theChar, keyCode, macEvent);
  137.             end; { else }
  138.         end { if }
  139.         else begin
  140.             { Pass onto supervisor. }
  141.             itsGameDirector.DoKeyDown(theChar, keyCode, macEvent);
  142.         end; { else }
  143.     end; { DoKeyDown }
  144.  
  145.  
  146. {****************************************************}
  147. {}
  148. {        DrawCell                                                                                                                                                                                                                    }
  149. {}
  150. {        Draw the level number, name and best player statistics.                                                                    }
  151. {}
  152. {****************************************************}
  153.  
  154.     procedure CShLevelsTable.DrawCell (theCell: Cell;
  155.                                     cellRect: Rect);
  156.  
  157.         var
  158.             theNumStr: Str255;
  159.             theLevel: CShLevel;
  160.  
  161.         function TimeText (aDuration: LongInt): Str255;
  162.  
  163.             var
  164.                 hoursStr, minutesStr, secondsStr: Str255;
  165.  
  166.         begin { TimeText }
  167.             aDuration := aDuration div 60;
  168.             NumToString(aDuration mod 60, secondsStr);
  169.             if Length(secondsStr) < 2 then begin
  170.                 secondsStr := Concat('0', secondsStr);
  171.             end; { if }
  172.  
  173.             aDuration := aDuration div 60;
  174.             NumToString(aDuration mod 60, minutesStr);
  175.             if Length(minutesStr) < 2 then begin
  176.                 minutesStr := Concat('0', minutesStr);
  177.             end; { if }
  178.  
  179.             aDuration := aDuration div 60;
  180.             NumToString(aDuration, hoursStr);
  181.  
  182.             TimeText := Concat(hoursStr, ':', minutesStr, ':', secondsStr);
  183.         end; { TimeText }
  184.  
  185.     begin { DrawCell }
  186.         { The list of levels is owned by the array pane.  Because }
  187.         { it is an array, we have to cast it, but it is a safe cast. }
  188.  
  189.         theLevel := CShLevel(CList(GetArray).NthItem(theCell.v + 1));
  190.         if theLevel <> nil then begin
  191.  
  192.             NumToString(theCell.v + 1, theNumStr);
  193.             MoveTo(cellRect.left + indent.h + kNumh, cellRect.top + indent.v);
  194.             DrawString(theNumStr);
  195.  
  196.             MoveTo(cellRect.left + indent.h + kNameh, cellRect.top + indent.v);
  197.             DrawString(theLevel.GetName);
  198.  
  199.             MoveTo(cellRect.left + indent.h + kPlayerh, cellRect.top + indent.v);
  200.             DrawString(theLevel.GetPlayer);
  201.  
  202.             MoveTo(cellRect.left + indent.h + kMovesh, cellRect.top + indent.v);
  203.             NumToString(theLevel.GetMoves, theNumStr);
  204.             DrawString(theNumStr);
  205.  
  206.             MoveTo(cellRect.left + indent.h + kTimeh, cellRect.top + indent.v);
  207.             DrawString(TimeText(theLevel.GetTime));
  208.         end; { if }
  209.     end; { DrawCell }
  210.  
  211.  
  212.  
  213. {****************************************************}
  214. {}
  215. {        RowSelected                                                                                                                                                                                                        }
  216. {}
  217. {        Returns the index of the row in the table which is selected, 0 if none.                                }
  218. {}
  219. {****************************************************}
  220.  
  221.     function CShLevelsTable.RowSelected: LevelsRange;
  222.  
  223.         var
  224.             theCell: Cell;
  225.  
  226.     begin { RowSelected }
  227.         SetCell(theCell, 0, 0);
  228.         if GetSelect(TRUE, theCell) then begin
  229.             RowSelected := LevelsRange(theCell.v + 1);
  230.         end
  231.         else begin
  232.             RowSelected := kNoLevel;
  233.         end; { if }
  234.     end; { RowSelected }
  235.  
  236.  
  237. end. { CShLevelsTable }