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