hud.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright (C) 2009-2011 id Software LLC, a ZeniMax Media company.
  3. Copyright (C) 2009 Id Software, Inc.
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "../doomiphone.h"
  17. #include <math.h>
  18. hud_t huds;
  19. void HudDraw();
  20. void HudWrite();
  21. void HudRead();
  22. ibutton_t *dragHud;
  23. int dragX, dragY;
  24. void SetHudPic( ibutton_t *hp, const char *image ) {
  25. pkTexture_t *gl;
  26. gl = PK_FindTexture( image );
  27. assert( gl );
  28. hp->texture = gl;
  29. hp->touch = NULL; // in case one was down when it was saved
  30. }
  31. void SetHudSpot( ibutton_t *hp, int x, int y, int dw, int dh ) {
  32. hp->touch = NULL; // in case one was down when it was saved
  33. float xRatio = ((float)displaywidth) / 480.0f;
  34. float yRatio = ((float)displayheight) / 320.0f;
  35. float themin = MIN( xRatio, yRatio );
  36. x *= ((float)displaywidth) / 480.0f;
  37. y *= ((float)displayheight) / 320.0f;
  38. dw *= themin;
  39. dh *= themin;
  40. hp->x = x - dw/2;
  41. hp->y = y - dh/2;
  42. hp->drawWidth = dw;
  43. hp->drawHeight = dh;
  44. hp->buttonFlags = 0;
  45. hp->scale = 1.0f;
  46. }
  47. void HudSetTexnums() {
  48. SetHudPic( &huds.forwardStick, "iphone/up_down.tga" );
  49. SetHudPic( &huds.sideStick, "iphone/side_2_side.tga" );
  50. SetHudPic( &huds.turnStick, "iphone/directional_2.tga" );
  51. SetHudPic( &huds.turnRotor, "iphone/rotate.tga" );
  52. SetHudPic( &huds.fire, "iphone/fire.tga" );
  53. SetHudPic( &huds.menu, "iphone/menu_button.tga" );
  54. SetHudPic( &huds.map, "iphone/map_button.tga" );
  55. SetHudSpot( &huds.weaponSelect, 240, 280, 40, 90 );
  56. }
  57. void HudSetForScheme( int schemeNum ) {
  58. for ( ibutton_t *hud = (ibutton_t *)&huds ; hud != (ibutton_t *)(&huds+1) ; hud++ ) {
  59. hud->buttonFlags = BF_IGNORE;
  60. }
  61. int STICK_SIZE = 128;
  62. int HALF_STICK = 128/2;
  63. if( displaywidth >= 1024 ) {
  64. STICK_SIZE = 64;
  65. HALF_STICK = 64/2;
  66. }
  67. static const int BOTTOM = 320 - 44; // above the status bar
  68. SetHudSpot( &huds.weaponSelect, 240, 280, 40, 90 ); // the touch area is doubled
  69. // make the forward / back sticks touch taller than they draw
  70. switch ( schemeNum ) {
  71. default:
  72. case 0: // turn stick
  73. SetHudSpot( &huds.forwardStick, HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  74. SetHudSpot( &huds.turnStick, HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  75. SetHudSpot( &huds.fire, 480-40, BOTTOM-HALF_STICK, 80, 80 );
  76. SetHudSpot( &huds.menu, 480-24, 24, 48, 48 );
  77. SetHudSpot( &huds.map, 24, 24, 48, 48 );
  78. break;
  79. case 1: // dual stick
  80. SetHudSpot( &huds.forwardStick, HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  81. SetHudSpot( &huds.sideStick, HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  82. SetHudSpot( &huds.turnStick, 480-HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  83. SetHudSpot( &huds.fire, 480-40, 40, 80, 80 );
  84. SetHudSpot( &huds.menu, 48+24, 24, 48, 48 );
  85. SetHudSpot( &huds.map, 24, 24, 48, 48 );
  86. break;
  87. case 2: // rotor
  88. SetHudSpot( &huds.forwardStick, HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  89. SetHudSpot( &huds.sideStick, HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  90. SetHudSpot( &huds.turnRotor, 480-HALF_STICK, BOTTOM-HALF_STICK, STICK_SIZE, STICK_SIZE );
  91. SetHudSpot( &huds.fire, 480-40, 40, 80, 80 );
  92. SetHudSpot( &huds.menu, 48+24, 24, 48, 48 );
  93. SetHudSpot( &huds.map, 24, 24, 48, 48 );
  94. break;
  95. }
  96. // don't process these in the update hud touch loop, because they will be
  97. // handled with normal button calls
  98. huds.menu.buttonFlags |= BF_HUDBUTTON;
  99. huds.map.buttonFlags |= BF_HUDBUTTON;
  100. // don't make the big button click sound for the fire button
  101. huds.fire.buttonFlags |= BF_SMALL_CLICK;
  102. }
  103. void SnapSticks( ibutton_t *test, const ibutton_t *to ) {
  104. if ( abs( test->x - to->x ) < test->drawWidth && abs( test->y - to->y ) < test->drawHeight ) {
  105. test->x = to->x;
  106. test->y = to->y;
  107. }
  108. }
  109. /*
  110. ==================
  111. HudEditFrame
  112. ==================
  113. */
  114. void HudEditFrame() {
  115. color3_t gray = { 32, 32, 32 };
  116. if ( numTouches == 0 && numPrevTouches == 1 && dragHud ) {
  117. Sound_StartLocalSound( "iphone/baction_01.wav" );
  118. dragHud = NULL;
  119. }
  120. if ( numTouches == 1 && numPrevTouches == 0 ) {
  121. // identify the hud being touched for drag
  122. int x = touches[0][0];
  123. int y = touches[0][1];
  124. dragHud = NULL;
  125. for ( ibutton_t *hud = (ibutton_t *)&huds ; hud != (ibutton_t *)(&huds+1) ; hud++ ) {
  126. if ( hud->buttonFlags & BF_IGNORE ) {
  127. continue;
  128. }
  129. if ( x >= hud->x && x - hud->x < hud->drawWidth && y >= hud->y && y - hud->y < hud->drawHeight ) {
  130. dragHud = hud;
  131. dragX = dragHud->x - x;
  132. dragY = dragHud->y - y;
  133. Sound_StartLocalSound( "iphone/bdown_01.wav" );
  134. break;
  135. }
  136. }
  137. }
  138. if ( numTouches == 1 && numPrevTouches == 1 && dragHud ) {
  139. // adjust the position of the dragHud
  140. dragHud->x = touches[0][0] + dragX;
  141. dragHud->y = touches[0][1] + dragY;
  142. if ( dragHud->x < 0 ) {
  143. dragHud->x = 0;
  144. }
  145. if ( dragHud->x > displaywidth - dragHud->drawWidth ) {
  146. dragHud->x = displaywidth - dragHud->drawWidth;
  147. }
  148. if ( dragHud->y < 0 ) {
  149. dragHud->y = 0;
  150. }
  151. if ( dragHud->y > displayheight - dragHud->drawHeight ) {
  152. dragHud->y = displayheight - dragHud->drawHeight;
  153. }
  154. // magnet pull a matchable axis
  155. if ( controlScheme->value == 0 ) {
  156. if ( dragHud == &huds.forwardStick ) {
  157. SnapSticks( &huds.turnStick, dragHud );
  158. }
  159. } else {
  160. if ( dragHud == &huds.forwardStick ) {
  161. SnapSticks( &huds.sideStick, dragHud );
  162. }
  163. }
  164. }
  165. // solid background color and some UI elements for context
  166. R_Draw_Fill( 0, 0, 480, 320, gray );
  167. glColor4f( 1, 1, 1, 1 );
  168. iphoneCenterText( 240, 20, 0.75, "Drag the controls" );
  169. // draw the status bar
  170. extern patchnum_t stbarbg;
  171. if ( statusBar->value ) {
  172. // force doom to rebind, since we have changed the active GL_TEXTURE_2D
  173. last_gltexture = NULL;
  174. gld_DrawNumPatch(0, ST_Y, stbarbg.lumpnum, CR_DEFAULT, VPT_STRETCH);
  175. }
  176. // draw the active items at their current locations
  177. for ( ibutton_t *hud = (ibutton_t *)&huds ; hud != (ibutton_t *)(&huds+1) ; hud++ ) {
  178. if ( !hud->texture ) {
  179. continue;
  180. }
  181. if ( hud->buttonFlags & BF_IGNORE ) {
  182. continue;
  183. }
  184. PK_StretchTexture( hud->texture, hud->x, hud->y, hud->drawWidth, hud->drawHeight );
  185. }
  186. // draw the done button
  187. static ibutton_t btnDone;
  188. if ( !btnDone.texture ) {
  189. // initial setup
  190. SetButtonPicsAndSizes( &btnDone, "iphone/back_button.tga", "Done", 240 - 32, 160-32, 64, 64 );
  191. }
  192. if ( HandleButton( &btnDone ) ) {
  193. menuState = IPM_MAIN;
  194. }
  195. }