Draw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /***********************************************************************
  2. *
  3. * SPACE TRADER 1.2.0
  4. *
  5. * Draw.c
  6. *
  7. * Copyright (C) 2000-2002 Pieter Spronck, All Rights Reserved
  8. *
  9. * Additional coding by Sam Anderson (rulez2@home.com)
  10. * Additional coding by Samuel Goldstein (palm@fogbound.net)
  11. *
  12. * Some code of Matt Lee's Dope Wars program has been used.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. *
  28. * You can contact the author at space_trader@hotmail.com
  29. *
  30. * For those who are familiar with the classic game Elite: many of the
  31. * ideas in Space Trader are heavily inspired by Elite.
  32. *
  33. **********************************************************************/
  34. // *************************************************************************
  35. // ControlPtr RectangularButton( FormPtr frmP, int Nr )
  36. // void SBufMultiples( long Count, char* Txt )
  37. // void setFormTitle( int formID, CharPtr s )
  38. // void setCurrentWinTitle( CharPtr s )
  39. // void setLabelText( FormPtr frm, int labelid, CharPtr s )
  40. // void RectangularShortcuts( FormPtr frm, int First )
  41. // void EraseRectangle( int x, int y, int ex, int ey )
  42. // void DrawChars( char* Text, int x, int y )
  43. // int ScrollButton( EventPtr eventP )
  44. //
  45. // Modifications:
  46. // mm/dd/yy - description - author
  47. // *************************************************************************
  48. // Needed to change the frames of buttons to rectangles. There is currently no function
  49. // that allows to do it. When an OS is published that fails on this access (probably OS 5.0),
  50. // the RectangularButton function must be adapted.
  51. #ifndef ALLOW_ACCESS_TO_INTERNALS_OF_CONTROLS
  52. #define ALLOW_ACCESS_TO_INTERNALS_OF_CONTROLS 1
  53. #endif
  54. #include "external.h"
  55. // *************************************************************************
  56. // Make button rectangular
  57. // *************************************************************************
  58. ControlPtr RectangularButton( FormPtr frmP, int Nr )
  59. {
  60. int objindex;
  61. ControlPtr cp;
  62. objindex = FrmGetObjectIndex( frmP, Nr );
  63. cp = (ControlPtr)FrmGetObjectPtr( frmP, objindex );
  64. if (BELOW50 || RectangularButtonsOn)
  65. cp->attr.frame = rectangleButtonFrame;
  66. return cp;
  67. }
  68. // *************************************************************************
  69. // String number and qualifier together
  70. // *************************************************************************
  71. void SBufMultiples( long Count, char* Txt )
  72. {
  73. StrIToA( SBuf2, Count );
  74. StrCat( SBuf, SBuf2 );
  75. StrCat( SBuf, " " );
  76. StrCat( SBuf, Txt );
  77. if (Count != 1)
  78. StrCat( SBuf, "s" );
  79. }
  80. // *************************************************************************
  81. // String number and qualifier together, but no digit for singulars
  82. // *************************************************************************
  83. void SBufShortMultiples( long Count, char* Txt )
  84. {
  85. if (Count != 1)
  86. {
  87. StrIToA( SBuf2, Count );
  88. StrCat( SBuf, SBuf2 );
  89. StrCat( SBuf, " " );
  90. StrCat( SBuf, Txt );
  91. if (Count != 1)
  92. StrCat( SBuf, "s" );
  93. }
  94. else
  95. {
  96. StrCat( SBuf, Txt );
  97. }
  98. }
  99. // *************************************************************************
  100. // Setting the title of a form.
  101. // CW 8 required a rewrite, as FrmGetTitle now returns a const Char *
  102. // *************************************************************************
  103. void setFormTitle( int formID, CharPtr s )
  104. {
  105. FormPtr frm;
  106. // CharPtr wintitle;
  107. frm = FrmGetFormPtr( formID );
  108. // wintitle = FrmGetTitle( frm );
  109. // StrCopy( wintitle, s );
  110. // FrmSetTitle( frm, wintitle );
  111. FrmCopyTitle( frm, s );
  112. }
  113. // *************************************************************************
  114. // Setting the title of the current form.
  115. // *************************************************************************
  116. void setCurrentWinTitle( CharPtr s )
  117. {
  118. FormPtr frm = FrmGetActiveForm();
  119. FrmSetTitle( frm, s );
  120. }
  121. // *************************************************************************
  122. // Setting the text of a label.
  123. // CW 8 required a rewrite, as CtlGetLabel now returns a const Char *
  124. // *************************************************************************
  125. void setLabelText( FormPtr frm, int labelid, CharPtr s )
  126. {
  127. int objindex;
  128. // ControlPtr cp;
  129. // CharPtr c;
  130. objindex = FrmGetObjectIndex( frm, labelid );
  131. // cp = (ControlPtr)FrmGetObjectPtr( frm, objindex );
  132. // c = CtlGetLabel( cp );
  133. // StrCopy( c, s );
  134. FrmHideObject(frm, objindex);
  135. FrmCopyLabel(frm, labelid, s);
  136. FrmShowObject(frm, objindex);
  137. }
  138. // *************************************************************************
  139. // Make shortcut buttons rectangular
  140. // *************************************************************************
  141. void RectangularShortcuts( FormPtr frm, int First )
  142. {
  143. SetControlLabel(frm, First+0, Shortcuts[Shortcut1]);
  144. RectangularButton( frm, First+0 );
  145. SetControlLabel(frm, First+1, Shortcuts[Shortcut2]);
  146. RectangularButton( frm, First+1 );
  147. SetControlLabel(frm, First+2, Shortcuts[Shortcut3]);
  148. RectangularButton( frm, First+2 );
  149. SetControlLabel(frm, First+3, Shortcuts[Shortcut4]);
  150. RectangularButton( frm, First+3 );
  151. }
  152. // *************************************************************************
  153. // Erases a rectangle
  154. // *************************************************************************
  155. void EraseRectangle( int x, int y, int ex, int ey )
  156. {
  157. RectangleType a;
  158. a.topLeft.x = x;
  159. a.topLeft.y = y;
  160. a.extent.x = ex;
  161. a.extent.y = ey;
  162. WinEraseRectangle( &a, 0 );
  163. }
  164. // *************************************************************************
  165. // Draws a text on the screen
  166. // *************************************************************************
  167. void DrawChars( char* Text, int x, int y )
  168. {
  169. WinDrawChars( Text, StrLen( Text ), x, y );
  170. }
  171. // ******
  172. // Debug Alerts
  173. // ******
  174. // only compile this in if desired. The resource does get included regardless,
  175. // but that's only at a cost of a handful of bytes.
  176. #ifdef _INCLUDE_DEBUG_DIALOGS_
  177. void DebugIntAlert(int a, int b, int c)
  178. {
  179. char as[51],bs[51],cs[51];
  180. StrIToA(as, a);
  181. StrIToA(bs, b);
  182. StrIToA(cs, c);
  183. FrmCustomAlert(DebugAlert, as, bs, cs);
  184. }
  185. void DebugLongAlert(long a, long b, long c)
  186. {
  187. char as[51],bs[51],cs[51];
  188. StrPrintF(as, "%ld", a);
  189. StrPrintF(bs, "%ld", b);
  190. StrPrintF(cs, "%ld", c);
  191. FrmCustomAlert(DebugAlert, as, bs, cs);
  192. }
  193. void DebugCharAlert(char *a, char *b, char *c)
  194. {
  195. FrmCustomAlert(DebugAlert, a,b,c);
  196. }
  197. #endif // end of debug code
  198. // *************************************************************************
  199. // Draws a text centered on the screen
  200. // *************************************************************************
  201. void DrawCharsCentered( char* Text, int y, Boolean useBold )
  202. {
  203. FontID originalFont;
  204. Int16 width=160, length=StrLen(Text);
  205. Boolean fits;
  206. if (useBold)
  207. {
  208. originalFont = FntSetFont(boldFont);
  209. }
  210. else
  211. {
  212. originalFont = FntSetFont(stdFont);
  213. }
  214. FntCharsInWidth(Text, &width, &length, &fits);
  215. // should probably find the width of the screen for the hi-rez crowd.
  216. WinDrawChars( Text, StrLen( Text ), (int)(80 - width/2), y );
  217. FntSetFont(originalFont);
  218. }
  219. // *****************************************************************
  220. // Utility function for displaying headlines and incrementing the
  221. // line counter. If we're too far down the page, we simply don't
  222. // display the headline (we'll return a false if that's the case)
  223. // *****************************************************************
  224. Boolean DisplayHeadline(char *text, int *y_coord)
  225. {
  226. int offset=0;
  227. Int16 width=158 - (NEWSINDENT1 * 2), length=StrLen(text);
  228. Boolean fits;
  229. char theText[72];
  230. FntCharsInWidth(text, &width, &length, &fits);
  231. // check to see if we've got room to put it on one line
  232. if (fits)
  233. {
  234. // do we have room on the page?
  235. if (*y_coord > 128)
  236. return false;
  237. DrawChars(text, NEWSINDENT1, *y_coord);
  238. }
  239. else
  240. {
  241. // do we have room for two lines on the page?
  242. if (*y_coord > 107)
  243. return false;
  244. // is our text too long?
  245. if (StrLen(text) > 71)
  246. {
  247. return false;
  248. }
  249. StrCopy(theText, text);
  250. offset = length;
  251. while (offset > 0 && theText[offset] != ' ')
  252. offset--;
  253. theText[offset]='\0';
  254. DrawChars(theText, NEWSINDENT1, *y_coord);
  255. *y_coord += 11;
  256. DrawChars(theText + offset + 1, NEWSINDENT2, *y_coord);
  257. }
  258. *y_coord += 15;
  259. return true;
  260. }
  261. // *****************************************************************
  262. // Utility function for displaying a page of text.
  263. // *****************************************************************
  264. void DisplayPage(char *text, int start_y)
  265. {
  266. int offset=0;
  267. int internalOffset = 0;
  268. int vert = start_y;
  269. Int16 width=148, length, origLength=StrLen(text);
  270. Boolean fits, looping;
  271. char theText[46];
  272. looping = true;
  273. while (looping)
  274. {
  275. StrNCopy(theText, text + offset, min(45, origLength-offset));
  276. theText[min (45, origLength - offset) ] = '\0';
  277. length=StrLen(theText);
  278. FntCharsInWidth(theText, &width, &length, &fits);
  279. if (fits)
  280. {
  281. offset += 45;
  282. }
  283. else
  284. {
  285. internalOffset = length;
  286. while (internalOffset > 0 &&
  287. theText[internalOffset] != ' ' && theText[internalOffset] != '-')
  288. internalOffset--;
  289. if (theText[internalOffset] == '-' && internalOffset < length)
  290. {
  291. theText[internalOffset+1]='\0';
  292. }
  293. else
  294. {
  295. theText[internalOffset]='\0';
  296. }
  297. offset += internalOffset + 1;
  298. }
  299. DrawChars(theText, 8, vert);
  300. vert += 12;
  301. if (offset >= origLength)
  302. {
  303. looping = false;
  304. theText[45]='\0';
  305. }
  306. }
  307. }
  308. // *************************************************************************
  309. // Return scrollbutton that is pushed
  310. // *************************************************************************
  311. int ScrollButton( EventPtr eventP )
  312. {
  313. ULong Ticks;
  314. if ((eventP->data.keyDown.chr == chrPageUp) ||
  315. (eventP->data.keyDown.chr == chrPageDown))
  316. {
  317. Ticks = TimGetTicks();
  318. if (Ticks < GalacticChartUpdateTicks + (SysTicksPerSecond() >> 1))
  319. return 0;
  320. GalacticChartUpdateTicks = Ticks;
  321. return eventP->data.keyDown.chr;
  322. }
  323. return 0;
  324. }