1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
Copyright (C) 2011 azazello and ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "quakedef.h"
#include "Ctrl.h"
#include "Ctrl_Tab.h"
#include "keys.h"
#include "qsound.h"
// initialize control
void CTab_Init(CTab_t *tab)
{
memset(tab, 0, sizeof(CTab_t));
tab->lastViewedPage = -1;
tab->hoveredPage = -1;
}
// clear control
void CTab_Clear(CTab_t *tab)
{
// no dynamic data, just clear it
CTab_Init(tab);
}
// create new control
CTab_t * CTab_New(void)
{
CTab_t *tab = (CTab_t *) Q_malloc(sizeof(CTab_t));
CTab_Init(tab);
return tab;
}
// free control
void CTab_Free(CTab_t *tab)
{
CTab_Clear(tab);
Q_free(tab);
}
// add tab
void CTab_AddPage(CTab_t *tab, const char *name, int id, const CTabPage_Handlers_t *handlers)
{
int i;
CTabPage_t *page;
if (tab->nPages >= TAB_MAX_TABS)
{
Sys_Error("CTab_AddPage: Maximum page number reached "
"while adding %s", name);
return;
}
for (i=0; i < tab->nPages; i++)
{
if (tab->pages[i].id == id)
Sys_Error("CTab_AddPage: such id already exist");
if (!strcmp(tab->pages[i].name, name))
Sys_Error("CTab_AddPage: such name already exist");
}
page = &tab->pages[tab->nPages++];
// set name
strlcpy (page->name, name, sizeof (page->name));
// set id
page->id = id;
// set handlers
memcpy(&page->handlers, handlers, sizeof(CTabPage_Handlers_t));
}
// will draw tab navigation bar, remember labels positions
// and will return the width of the navigation bar
static int CTab_Draw_PageLinks(CTab_t *tab, int x, int y, int w, int h)
{
char buf[TAB_MAX_NAME_LENGTH+3];
int i, cx, cy, ww, wh; // current x/y, word width/height
qbool ap, hp; // active page / hovered page
cx = 0; cy = 0;
for (i = 0; i < tab->nPages; i++)
{
*buf = 0;
ap = tab->activePage == i;
hp = tab->hoveredPage == i;
// add leading space/brace
strlcat (buf, ap ? "\x10" : " ", sizeof (buf));
// adds white or red variant of the page name to the buf string
strlcat (buf, tab->pages[i].name, sizeof (buf));
// add closing space/brace
strlcat (buf, ap ? "\x11" : " ", sizeof (buf));
ww = strlen(buf) * LETTERWIDTH;
wh = LETTERHEIGHT;
// this is not the first word we are printing and we don't fit on the line anymore
if (cx && (cx + ww > w)) {
// so wrap the line already
cx = 0;
cy += LETTERHEIGHT;
}
UI_Print(x + cx, y + cy, buf, ap || hp);
// remember where the text was so we can point to it with the mouse later
tab->navi_boxes[i].x = cx;
tab->navi_boxes[i].y = cy;
tab->navi_boxes[i].x2 = cx+ww-LETTERWIDTH;
tab->navi_boxes[i].y2 = cy+wh;
// we substract 1 letter here and above because we want only one (common) space
// between the labels, not two (one for each)
cx += ww - LETTERWIDTH;
}
return cy + LETTERHEIGHT;
}
// draw control
void CTab_Draw(CTab_t *tab, int x, int y, int w, int h)
{
char line[1024];
int nav_height;
if (tab->activePage != tab->lastViewedPage && tab->activePage < tab->nPages)
{
CTabPage_OnShowType onshowFnc = tab->pages[tab->activePage].handlers.onshow;
if (onshowFnc != NULL) onshowFnc();
tab->lastViewedPage = tab->activePage;
}
tab->width = w;
tab->height = h;
nav_height = CTab_Draw_PageLinks(tab, x, y, w, h);
// draw separator
memset(line, '\x1E', w/8);
line[w/8] = 0;
line[w/8-1] = '\x1F';
line[0] = '\x1D';
// memcpy(line + 2, " \x10shift\x11+\x10tab\x11 ", min((w/8)-3, 15));
// memcpy(line + w/8 - 2 - 7, " \x10tab\x11 ", 7);
UI_Print_Center(x, y + nav_height, w, line, false);
nav_height += 8;
// draw page
if (tab->pages[tab->activePage].handlers.draw != NULL)
tab->pages[tab->activePage].handlers.draw(x, y + nav_height, w, h- nav_height, tab, &tab->pages[tab->activePage]);
}
// process key
int CTab_Key(CTab_t *tab, int key, wchar unichar)
{
int handled;
if (tab->hoveredPage >= 0 && key == K_MOUSE1)
{
tab->activePage = tab->hoveredPage;
return true;
}
// we first call tabs handlers, because they might override
// default tab keys for modal dialogs
// tabs are responsible for not using "our" keys for other
// purposes
handled = false;
if (tab->pages[tab->activePage].handlers.key != NULL)
handled = tab->pages[tab->activePage].handlers.key(key, unichar, tab, &tab->pages[tab->activePage]);
// then try our handlers
if (!handled)
{
switch (key)
{
case K_PGUP:
case K_MOUSE4:
S_LocalSound ("misc/menu1.wav");
tab->activePage--;
handled = true;
break;
case K_PGDN:
case K_MOUSE5:
S_LocalSound ("misc/menu1.wav");
tab->activePage++;
handled = true;
break;
case K_LEFTARROW:
S_LocalSound ("misc/menu1.wav");
tab->activePage--;
handled = true;
break;
case K_RIGHTARROW:
S_LocalSound ("misc/menu1.wav");
tab->activePage++;
handled = true;
break;
case K_TAB:
S_LocalSound ("misc/menu1.wav");
if (keydown[K_SHIFT]) tab->activePage--; else tab->activePage++;
handled = true;
break;
}
if (handled)
tab->activePage = (tab->activePage + tab->nPages) % tab->nPages;
}
// return handled status
return handled;
}
static qbool CTab_Navi_Mouse_Event (CTab_t *tab, const mouse_state_t *ms)
{
int i;
if (!tab->width) return false;
if (ms->button_up == 1) {
CTab_Key(tab, K_MOUSE1, 0);
return true;
} else if (ms->button_up == 2) {
CTab_Key(tab, K_MOUSE2, 0);
return true;
}
for (i = 0; i < tab->nPages; i++)
{
if (!(tab->navi_boxes[i].x > ms->x || tab->navi_boxes[i].y > ms->y || tab->navi_boxes[i].x2 < ms->x || tab->navi_boxes[i].y2 < ms->y))
{ // pointer is within the bounds
tab->hoveredPage = i;
return true;
}
}
return false;
}
qbool CTab_Mouse_Event(CTab_t *tab, const mouse_state_t *ms)
{
int nav_height = tab->navi_boxes[tab->nPages-1].y2;
if (ms->x < 0 || ms->x > tab->width || ms->y < 0 || ms->y > tab->height)
return false;
if (ms->y <= nav_height)
{ // pointer is in the navigation area
return CTab_Navi_Mouse_Event(tab, ms);
}
else if (ms->y >= nav_height + LETTERHEIGHT)
{ // pointer is in the main area
CTabPage_MouseMoveType mmf;
mouse_state_t lms = *ms;
lms.y -= nav_height + LETTERHEIGHT;
tab->hoveredPage = -1;
mmf = tab->pages[tab->activePage].handlers.mousemove;
if (mmf)
return mmf(&lms);
}
return false;
}
// get current page
CTabPage_t * CTab_GetCurrent(CTab_t *tab)
{
return &tab->pages[tab->activePage];
}
// get current page id
int CTab_GetCurrentId(CTab_t *tab)
{
return tab->pages[tab->activePage].id;
}
// get current page name
char * CTab_GetCurrentPage(CTab_t *tab)
{
return tab->pages[tab->activePage].name;
}
// set current page by ID
void CTab_SetCurrentId(CTab_t *tab, int id)
{
int i;
for (i=0; i < tab->nPages; i++)
{
if (tab->pages[i].id == id)
{
tab->activePage = i;
return;
}
}
Sys_Error("CTab_SetCurrentId: id not found");
}
// set current page by string
void SetCurrentPage(CTab_t *tab, char *name)
{
int i;
for (i=0; i < tab->nPages; i++)
{
if (!strcmp(tab->pages[i].name, name))
{
tab->activePage = i;
return;
}
}
Sys_Error("CTab_SetCurrentName: name not found");
}
|