This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/pr_uimenu/mfuncs.c

437 lines
7.2 KiB
C

///////////////////////////////////////////////
// Functions Source File
///////////////////////
// This file belongs to dpmod/darkplaces
// AK contains all menu controlling stuff (sub-menus)
///////////////////////////////////////////////
// raise function
/* template
void(entity ent) raise_x =
{
entity old;
if(!ent._x)
return;
old = pev;
pev = ent;
pev._x();
pev = old;
};
*/
void(entity ent) raise_reinit =
{
entity old;
if(!ent._reinit)
return;
old = pev;
pev = ent;
pev._reinit();
pev = old;
};
void(entity ent) raise_destroy =
{
entity old;
if(!ent._destroy)
return;
old = pev;
pev = ent;
pev._destroy();
pev = old;
};
void(entity ent, float keynr, string ascii) raise_key =
{
entity old;
if(!ent._key)
return;
old = pev;
pev = ent;
pev._key(keynr, ascii);
pev = old;
};
void(entity ent) raise_draw =
{
entity old;
if(!ent._draw)
return;
old = pev;
pev = ent;
pev._draw();
pev = old;
};
void(entity ent) raise_mouse_enter =
{
entity old;
if(!ent._mouse_enter)
return;
old = pev;
pev = ent;
pev._mouse_enter();
pev = old;
};
void(entity ent) raise_mouse_leave =
{
entity old;
if(!ent._mouse_leave)
return;
old = pev;
pev = ent;
pev._mouse_leave();
pev = old;
};
void(entity ent) raise_action =
{
entity old;
if(!ent._action)
return;
old = pev;
pev = ent;
pev._action();
pev = old;
};
void(entity ent) raise_refresh =
{
entity old;
if(!ent._refresh)
return;
old = pev;
pev = ent;
pev._refresh();
pev = old;
};
// safe call control function functions
// default control functions
/* template
void(void) ctcall_x =
{
if(pev.x)
pev.x();
};
*/
void(void) ctcall_init =
{
if(pev.init)
pev.init();
};
void(void) ctcall_reinit =
{
if(pev.reinit)
pev.reinit();
};
void(void) ctcall_destroy =
{
if(pev.destroy)
pev.destroy();
};
float(float keynr, float ascii) ctcall_key =
{
if(pev.key)
return pev.key(keynr, ascii);
return 0;
};
void(void) ctcall_draw =
{
if(pev.draw)
pev.draw();
};
void(void) ctcall_mouse_enter =
{
if(pev.mouse_enter)
pev.mouse_enter();
};
void(void) ctcall_mouse_leave =
{
if(pev.mouse_leave)
pev.mouse_leave();
};
void(void) ctcall_action =
{
if(pev.action)
pev.action();
};
void(void) ctcall_refresh =
{
if(pev.refresh)
pev.refresh();
}
// default control functions
/* template (expect defct_key)
void(void) defct_x =
{
ctcall_x();
};
*/
// defct_init not needed cause its the same like the type function
void(void) defct_reinit =
{
ctcall_reinit();
};
void(void) defct_destroy =
{
ctcall_destroy();
};
void(float keynr, float ascii) defct_key =
{
if(!ctcall_key(keynr, ascii))
def_keyevent(keynr, ascii);
};
void(void) defct_draw =
{
ctcall_draw();
};
void(void) defct_mouse_enter =
{
ctcall_mouse_enter();
};
void(void) defct_mouse_leave =
{
ctcall_mouse_leave();
};
void(void) defct_action =
{
ctcall_action();
};
void(void) defct_refresh =
{
// do first the own fresh stuff and *then* call refresh
def_refresh();
ctcall_refresh();
}
// default refresh function
void(void) def_refresh =
{
// refresh stuff
if(pev.flag & FLAG_AUTOSETCLICK)
{
pev.click_pos = pev.pos;
pev.click_size = pev.size;
}
if(pev.flag & FLAG_AUTOSETCLIP)
{
pev.clip_pos = pev.pos;
pev.clip_size = pev.size;
}
};
// default key function
void(float keynr, float ascii) def_keyevent =
{
if(keynr == K_ESCAPE)
{
// move up to the parent
menu_selectup();
} else if(keynr == K_LEFTARROW || keynr == K_UPARROW)
{
// move to the previous element
menu_loopprev();
if(menu_selected == pev)
{
if(pev._prev)
{
menu_selected = pev._prev;
menu_selectdown();
if(menu_selected != pev._prev)
{
return;
}
}
menu_selected = pev;
}
} else if(keynr == K_RIGHTARROW || keynr == K_DOWNARROW)
{
// move to the next element
menu_loopnext();
if(menu_selected == pev)
{
if(pev._next)
{
menu_selected = pev._next;
menu_selectdown();
if(menu_selected != pev._next)
{
return;
}
}
menu_selected = pev;
}
} else if(keynr == K_ENTER || keynr == K_MOUSE1)
{
eprint(pev);
print("Action called\n");
if(pev._action)
pev._action();
// move to the child menu
menu_selectdown();
} else if(keynr == K_TAB)
{
// select next and try to "go" down
if(pev._next)
{
menu_selected = pev._next;
menu_selectdown();
if(menu_selected != pev._next)
{
return;
}
}
if(pev._prev)
{
menu_selected = pev._prev;
menu_selectdown();
if(menu_selected != pev._prev)
{
return;
}
}
menu_selected = pev;
}
};
// a rect is described by the top-left point and its size
float(vector point, vector r_pos, vector r_size) inrect =
{
if(point_x < r_pos_x)
return false;
if(point_y < r_pos_y)
return false;
if(point_x > (r_pos_x + r_size_x))
return false;
if(point_y > (r_pos_y + r_size_y))
return false;
return true;
};
vector(vector r_pos, vector r_size, vector c_pos, vector c_size) cliprectpos =
{
vector v;
// clip r_pos only
v_x = max(c_pos_x, r_pos_x);
v_y = max(c_pos_y, r_pos_y);
return v;
};
vector(vector r_pos, vector r_size, vector c_pos, vector c_size) cliprectsize =
{
vector v;
// safe version
//r_size_x = bound(c_pos_x, r_pos_x + r_size_x, c_pos_x + c_size_x) - bound(c_pos_x, r_pos_x, c_pos_x + c_size_x);
//r_size_y = bound(c_pos_y, r_pos_y + r_size_y, c_pos_y + c_size_y) - bound(c_pos_y, r_pos_y, c_pos_y + c_size_y);
v_x = min(c_pos_x + c_size_x, r_pos_x + r_size_x) - max(c_pos_x, r_pos_x);
v_y = min(c_pos_y + c_size_y, r_pos_y + r_size_y) - max(c_pos_y, r_pos_y);
if(v_x <= 0 || v_y <= 0)
v = '0 0 0';
return v;
}
void(void(void) reinitevent, void(void) destroyevent, void(float key, float ascii) keyevent, void(void) drawevent, void(void) mouse_enterevent, void(void) mouse_leaveevent, void(void) actionevent, void(void) refreshevent)
item_init =
{
pev._reinit = reinitevent;
pev._destroy = destroyevent;
pev._key = keyevent;
pev._draw = drawevent;
pev._mouse_enter = mouse_enterevent;
pev._mouse_leave = mouse_leaveevent;
pev._action = actionevent;
pev._refresh = refreshevent;
};
float(float tfactor) getflicker =
{
// TODO: use tfactor to vary the result
return (sin(tfactor * time) + 1)/2;
};
float(string s) getaltstringcount =
{
float len;
float count;
float pos;
len = strlen(s);
count = 0;
for(pos = 0; pos < len; pos = pos + 1)
{
if(substring(s,pos,1) == "'")
count = count + 1;
}
if(mod(count,2) == 1)
return 0; // bad string
return (count / 2);
};
string(float n, string s) getaltstring =
{
float start, length;
float tmp;
n = rint(n);
if(n >= getaltstringcount(s) || n < 0)
return "";
// go to the beginning of the altstring
tmp = 0;
for(start = 0;tmp <= n * 2 ; start = start + 1)
{
if(substring(s, start, 1) == "'")
tmp = tmp + 1;
}
for(length = 0; substring(s, start + length, 1) != "'"; length = length + 1)
{
}
return substring(s, start, length);
};