home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / examples / openquest / dialog.scc < prev    next >
Encoding:
Text File  |  2008-04-06  |  7.0 KB  |  246 lines

  1. /* ScummC
  2.  * Copyright (C) 2007  Alban Bedel, Gerrit Karius
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. #include <scummVars6.s>
  21. #include "common.sch"
  22. #include "dialog.sch"
  23. #include "verbs.sch"
  24.  
  25.  
  26. room Dialog {
  27.  
  28.     //
  29.     // Dialogs stuff
  30.     //
  31.  
  32.     // Reset the dialog list
  33.     script dialogClear(int kill) {
  34.         int i;
  35.         char* j;
  36.         if(dialogList) {
  37.             if(kill) {
  38.                 for(i = 0 ; i < numDialog ; i++) {
  39.                     // Undim can't work on array members directly.
  40.                     // Hopefully the compiler can hide that out one day.
  41.                     j = dialogList[i];
  42.                     undim(j);
  43.                 }
  44.             }
  45.             undim(dialogList);
  46.             dialogList = 0;
  47.         }
  48.         numDialog = numActiveDialog = 0;
  49.     }
  50.  
  51.     // Add an entry to the dialog list
  52.     script dialogAdd(char* str) {
  53.         if(numDialog >= MAX_DIALOG_SENTENCE) {
  54.             dbgPrint("Too many sentences, can't add another one.");
  55.             return;
  56.         }
  57.         unless(dialogList)
  58.             dimInt(dialogList,MAX_DIALOG_SENTENCE);
  59.         dialogList[numDialog] = str;
  60.         numDialog++;
  61.         if(str) numActiveDialog++;
  62.     }
  63.  
  64.     // Remove an entry, if kill is not zero undim the string too
  65.     script dialogRemove(int idx, int kill) {
  66.         int i;
  67.         if(idx < 0 || idx >= numDialog) {
  68.          dbgPrint("Dialog index out of range: %i{idx}, can't remove.");
  69.          return;
  70.         }
  71.         if(dialogList[idx]) {
  72.             numActiveDialog--;
  73.             // Remove the string from the array
  74.             if(kill)
  75.                 undim(dialogList[idx]);
  76.             dialogList[idx] = 0;
  77.         }
  78.         // shift the res
  79.         for(i = idx+1 ; i < numDialog ; i++)
  80.             dialogList[i-1] = dialogList[i];
  81.         numDialog--;
  82.     }
  83.  
  84.     script showDialog() {
  85.         int i,v,d,first,last,firstSentence,lastSentence;
  86.  
  87.         first = last = -1;
  88.         firstSentence = lastSentence = -1;
  89.         // switch the charset
  90.         initCharset(ResRoom::dialogCharset);
  91.         // Dialog lines
  92.         v = 0;
  93.         for(i = 0 ; i < numDialog ; i++) {
  94.             unless(dialogList[i]) continue;
  95.             if(firstSentence < 0) firstSentence = i;
  96.             lastSentence = i;
  97.             if(v < dialogOffset || v >= dialogOffset+MAX_DIALOG_LINES) {
  98.                 v++;
  99.                 continue;
  100.             }
  101.             if(first < 0) first = i;
  102.             last = i;
  103.             setCurrentVerb(dialogVerb0 + d);
  104.             initVerb();
  105.             setVerbNameString(dialogList[i]);
  106.             setVerbXY(12,145+11*d);
  107.             setVerbColor(dialogColor);
  108.             setVerbHiColor(dialogHiColor);
  109.             setVerbOn();
  110.             redrawVerb();
  111.             v++, d++;
  112.         }
  113.         for( ; d < MAX_DIALOG_LINES ; d++) {
  114.             setCurrentVerb(dialogVerb0 + d);
  115.             setVerbOff();
  116.             redrawVerb();
  117.         }
  118.         dbgPrint("Sentence: %i{firstSentence} %i{lastSentence}");
  119.         dbgPrint("Shown: %i{first} %i{last}");
  120.        
  121.         // Up arrow
  122.         setCurrentVerb(dialogUp);
  123.         if(first > firstSentence) {
  124.             initVerb();
  125.             setVerbName("\x03");
  126.             setVerbXY(2,145);
  127.             setVerbColor(dialogColor);
  128.             setVerbHiColor(dialogHiColor);
  129.             setVerbOn();
  130.             redrawVerb();
  131.         } else {
  132.             setVerbOff();
  133.             redrawVerb();
  134.         }            
  135.         // Down arrow
  136.         setCurrentVerb(dialogDown);
  137.         if(last < lastSentence) {
  138.             initVerb();
  139.             setVerbName("\x02");
  140.             setVerbXY(2,145+4*11);
  141.             setVerbColor(dialogColor);
  142.             setVerbHiColor(dialogHiColor);
  143.             setVerbOn();
  144.             redrawVerb();
  145.         } else {
  146.             setVerbOff();
  147.             redrawVerb();
  148.         }
  149.         
  150.         initCharset(ResRoom::chtest);
  151.  
  152.     }
  153.  
  154.     script dialogInputHandler(int area,int cmd, int btn) {
  155.         int i,v,d;
  156.  
  157.         dbgPrintBegin();
  158.         dbgPrint("Area=%i{area} cmd=%i{cmd} button=%i{btn}");
  159.         dbgPrintEnd();
  160.  
  161.         egoPrintBegin();
  162.         egoPrintOverhead();
  163.         actorPrintEnd();
  164.  
  165.         if(area == 4) { // area 4 is the keyboard
  166.             ResRoom::keyboardHandler(cmd);
  167.             return;
  168.         }
  169.  
  170.         if(cmd == dialogUp || cmd == dialogDown) {
  171.             dialogOffset += (cmd == dialogUp ? -1 : 1)*MAX_DIALOG_LINES/2;
  172.             if(dialogOffset > numActiveDialog-MAX_DIALOG_LINES)
  173.                 dialogOffset = numActiveDialog-MAX_DIALOG_LINES;
  174.             if(dialogOffset < 0) dialogOffset = 0;
  175.             dbgPrint("Dialog offset: %i{dialogOffset}");
  176.             showDialog();
  177.             return;
  178.         }
  179.         
  180.         if(cmd < dialogVerb0 || cmd > dialogVerb4)
  181.             return;
  182.  
  183.         // Find the selected sentence
  184.         for(i = 0 ; i < numDialog ; i++) {
  185.             unless(dialogList[i]) continue;
  186.             if(v < dialogOffset || v >= dialogOffset+MAX_DIALOG_LINES) {
  187.                 v++;
  188.                 continue;
  189.             }
  190.             if(d == cmd-dialogVerb0) break;
  191.             v++, d++;
  192.         }
  193.  
  194.         selectedSentence = i;
  195.     }
  196.  
  197.     // Start a dialog
  198.     script dialogStart(int color, int hiColor) {
  199.         int i;
  200.         selectedSentence = -1;
  201.         dialogOffset = 0;
  202.         if(numDialog < 1) {
  203.             dbgPrint("No dialog was setup, nothing to show.");
  204.             selectedSentence = -2;
  205.             return;
  206.         }
  207.         // Hide the normal verbs/inventory
  208.         Verbs::showVerbs(0);
  209.         // Show our stuff
  210.         // setup the verbs
  211.         dialogColor = color ? color : VERB_COLOR;
  212.         dialogHiColor = hiColor ? hiColor : VERB_HI_COLOR;
  213.         showDialog();
  214.         // Kill the mouseWatch
  215.         if(isScriptRunning(ResRoom::mouseWatch))
  216.             stopScript(ResRoom::mouseWatch);
  217.         // Set the dialog input handler
  218.         VAR_VERB_SCRIPT = dialogInputHandler;
  219.     }
  220.  
  221.     script dialogHide() {
  222.         int i;
  223.         for(i = 0 ; i < MAX_DIALOG_LINES ; i++) {
  224.             setCurrentVerb(dialogVerb0 + i);
  225.             setVerbOff();
  226.             redrawVerb();
  227.         }
  228.         setCurrentVerb(dialogUp);
  229.         setVerbOff();
  230.         redrawVerb();
  231.         setCurrentVerb(dialogDown);
  232.         setVerbOff();
  233.         redrawVerb();
  234.     }
  235.  
  236.     script dialogEnd() {
  237.         dialogHide();
  238.         Verbs::showVerbs(1);
  239.         VAR_VERB_SCRIPT = ResRoom::inputHandler;
  240.         // Restart the mouse watching thread
  241.         ResRoom::mouseWatch();
  242.     }
  243.  
  244.  
  245. }
  246.