win_pal.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, 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. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "texpaint.h"
  19. #define PALETTE_WINDOW_CLASS "TPPalette"
  20. HDC paldc;
  21. int pal_width, pal_height;
  22. int blocks_x, blocks_y;
  23. int selected_index;
  24. unsigned selected_rgb;
  25. byte palette[768];
  26. float SnapAspect (float aspect)
  27. {
  28. if (aspect > 128)
  29. return 256;
  30. if (aspect > 32)
  31. return 128;
  32. if (aspect > 8)
  33. return 64;
  34. if (aspect > 2)
  35. return 32;
  36. return 16;
  37. }
  38. void Pal_SetIndex (int index)
  39. {
  40. selected_index = index;
  41. selected_rgb = palette[index*3] + (palette[index*3+1]<<8) + (palette[index*3+2]<<16);
  42. InvalidateRect (palettewindow, NULL, false);
  43. }
  44. void Pal_Draw (void)
  45. {
  46. int x, y;
  47. float aspect;
  48. float xs, ys;
  49. int c;
  50. if (pal_width < 1 || pal_height < 1)
  51. return;
  52. //
  53. // determine the block arrangement
  54. //
  55. if (pal_width > pal_height)
  56. {
  57. aspect = SnapAspect (pal_width / pal_height);
  58. blocks_x = aspect;
  59. blocks_y = 256/blocks_x;
  60. }
  61. else
  62. {
  63. aspect = SnapAspect (pal_height / pal_width);
  64. blocks_y = aspect;
  65. blocks_x = 256/blocks_y;
  66. }
  67. //
  68. // draw it
  69. //
  70. glViewport (0,0,pal_width, pal_height);
  71. glMatrixMode (GL_PROJECTION);
  72. glLoadIdentity ();
  73. glOrtho (0,1,0,1,-100,100);
  74. glMatrixMode (GL_MODELVIEW);
  75. glLoadIdentity ();
  76. glClear (GL_COLOR_BUFFER_BIT);
  77. glDisable (GL_DEPTH_TEST);
  78. glDisable (GL_CULL_FACE);
  79. glDisable (GL_TEXTURE_2D);
  80. xs = 1.0/blocks_x;
  81. ys = 1.0/blocks_y;
  82. for (x=0 ; x<blocks_x ; x++)
  83. {
  84. for (y=0 ; y<blocks_y ; y++)
  85. {
  86. c = x*blocks_y+(blocks_y-1-y);
  87. glColor3ubv (palette+c*3);
  88. glRectf (x*xs, y*ys, (x+1)*xs, (y+1)*ys);
  89. }
  90. }
  91. // highlight the selected texture
  92. y = selected_index % blocks_y;
  93. x = selected_index / blocks_y;
  94. y = blocks_y-1-y;
  95. glColor3f (0,0,0);
  96. glRectf ( (x+0.4)*xs, (y+0.4)*ys, (x+0.6)*xs, (y+0.6)*ys);
  97. }
  98. void Pal_Click (int x, int y)
  99. {
  100. int index;
  101. x = x*blocks_x/pal_width;
  102. y = y*blocks_y/pal_height;
  103. y = blocks_y-1-y;
  104. index = x*blocks_y + y;
  105. Pal_SetIndex (index);
  106. }
  107. /*
  108. ============
  109. Palette_WndProc
  110. ============
  111. */
  112. LONG WINAPI Palette_WndProc (
  113. HWND hWnd,
  114. UINT uMsg,
  115. WPARAM wParam,
  116. LPARAM lParam)
  117. {
  118. LONG lRet = 1;
  119. int fwKeys, xPos, yPos;
  120. RECT rect;
  121. GetClientRect(hWnd, &rect);
  122. pal_width = rect.right-rect.left;
  123. pal_height = rect.bottom-rect.top;
  124. switch (uMsg)
  125. {
  126. case WM_CREATE:
  127. paldc = GetDC(hWnd);
  128. bSetupPixelFormat(paldc);
  129. break;
  130. case WM_PAINT:
  131. {
  132. PAINTSTRUCT ps;
  133. BeginPaint(hWnd, &ps);
  134. if (!wglMakeCurrent( paldc, baseRC ))
  135. Error ("wglMakeCurrent failed");
  136. Pal_Draw ();
  137. EndPaint(hWnd, &ps);
  138. SwapBuffers(paldc);
  139. }
  140. break;
  141. case WM_MOUSEMOVE:
  142. if (wParam != MK_LBUTTON)
  143. break;
  144. case WM_LBUTTONDOWN:
  145. if (GetTopWindow(mainwindow) != hWnd)
  146. BringWindowToTop(hWnd);
  147. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  148. yPos = (short)HIWORD(lParam); // vertical position of cursor
  149. yPos = (int)rect.bottom - 1 - yPos;
  150. Pal_Click (xPos, yPos);
  151. break;
  152. case WM_MBUTTONUP:
  153. case WM_RBUTTONUP:
  154. case WM_LBUTTONUP:
  155. fwKeys = wParam; // key flags
  156. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  157. yPos = (short)HIWORD(lParam); // vertical position of cursor
  158. yPos = (int)rect.bottom - 1 - yPos;
  159. ReleaseCapture ();
  160. break;
  161. case WM_SIZE:
  162. InvalidateRect(skinwindow, NULL, false);
  163. break;
  164. case WM_NCCALCSIZE:// don't let windows copy pixels
  165. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  166. return WVR_REDRAW;
  167. case WM_CLOSE:
  168. /* call destroy window to cleanup and go away */
  169. DestroyWindow (hWnd);
  170. break;
  171. default:
  172. /* pass all unhandled messages to DefWindowProc */
  173. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  174. break;
  175. }
  176. /* return 1 if handled message, 0 if not */
  177. return lRet;
  178. }
  179. /*
  180. ==============
  181. WPal_Create
  182. ==============
  183. */
  184. void WPal_Create (HINSTANCE hInstance)
  185. {
  186. WNDCLASS wc;
  187. /* Register the skin class */
  188. memset (&wc, 0, sizeof(wc));
  189. wc.style = 0;
  190. wc.lpfnWndProc = (WNDPROC)Palette_WndProc;
  191. wc.cbClsExtra = 0;
  192. wc.cbWndExtra = 0;
  193. wc.hInstance = hInstance;
  194. wc.hIcon = 0;
  195. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  196. wc.hbrBackground = NULL;
  197. wc.lpszMenuName = 0;
  198. wc.lpszClassName = PALETTE_WINDOW_CLASS;
  199. if (!RegisterClass (&wc) )
  200. Error ("RegisterClass failed");
  201. palettewindow = CreateWindow (PALETTE_WINDOW_CLASS ,
  202. "Palette View",
  203. QE3_STYLE,
  204. (int)(screen_width*0.5),
  205. 0,
  206. (int)(screen_width*0.5),
  207. (int)(screen_height*.2), // size
  208. mainwindow, // parent window
  209. 0, // no menu
  210. hInstance,
  211. 0);
  212. if (!palettewindow)
  213. Error ("Couldn't create palettewindow");
  214. // RestoreWindowState(palettewindow, "palettewindow");
  215. ShowWindow (palettewindow, SW_SHOWDEFAULT);
  216. }