ddnet/src/game/client/gc_ui.cpp

297 lines
6.3 KiB
C++
Raw Normal View History

2007-11-25 19:42:40 +00:00
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
2007-12-15 10:24:49 +00:00
#include <engine/e_system.h>
#include <engine/e_client_interface.h>
2007-12-15 10:24:49 +00:00
#include <engine/e_config.h>
2008-01-12 15:07:57 +00:00
#include "gc_ui.h"
2007-05-22 15:03:32 +00:00
/********************************************************
UI
*********************************************************/
2007-11-04 21:36:03 +00:00
static const void *hot_item = 0;
static const void *active_item = 0;
static const void *last_active_item = 0;
static const void *becomming_hot_item = 0;
2007-10-06 17:01:06 +00:00
static float mouse_x, mouse_y; /* in gui space */
static float mouse_wx, mouse_wy; /* in world space */
2007-05-22 15:03:32 +00:00
static unsigned mouse_buttons = 0;
2008-01-12 12:27:55 +00:00
static unsigned last_mouse_buttons = 0;
2007-05-22 15:03:32 +00:00
float ui_mouse_x() { return mouse_x; }
float ui_mouse_y() { return mouse_y; }
float ui_mouse_world_x() { return mouse_wx; }
float ui_mouse_world_y() { return mouse_wy; }
int ui_mouse_button(int index) { return (mouse_buttons>>index)&1; }
2008-01-12 12:27:55 +00:00
int ui_mouse_button_clicked(int index) { return ui_mouse_button(index) && !((last_mouse_buttons>>index)&1) ; }
2007-05-22 15:03:32 +00:00
2007-11-04 21:36:03 +00:00
void ui_set_hot_item(const void *id) { becomming_hot_item = id; }
void ui_set_active_item(const void *id) { active_item = id; if (id) last_active_item = id; }
2007-07-21 16:33:01 +00:00
void ui_clear_last_active_item() { last_active_item = 0; }
2007-11-04 21:36:03 +00:00
const void *ui_hot_item() { return hot_item; }
2008-01-13 22:03:32 +00:00
const void *ui_next_hot_item() { return becomming_hot_item; }
2007-11-04 21:36:03 +00:00
const void *ui_active_item() { return active_item; }
const void *ui_last_active_item() { return last_active_item; }
2007-05-22 15:03:32 +00:00
int ui_update(float mx, float my, float mwx, float mwy, int buttons)
{
mouse_x = mx;
mouse_y = my;
mouse_wx = mwx;
mouse_wy = mwy;
2008-01-12 12:27:55 +00:00
last_mouse_buttons = mouse_buttons;
2007-05-22 15:03:32 +00:00
mouse_buttons = buttons;
hot_item = becomming_hot_item;
2007-07-20 22:19:40 +00:00
if(active_item)
hot_item = active_item;
2007-05-22 15:03:32 +00:00
becomming_hot_item = 0;
return 0;
}
/*
bool ui_
*/
2008-01-12 12:27:55 +00:00
int ui_mouse_inside(const RECT *r)
2007-05-22 15:03:32 +00:00
{
2008-01-12 12:27:55 +00:00
if(mouse_x >= r->x && mouse_x <= r->x+r->w && mouse_y >= r->y && mouse_y <= r->y+r->h)
2007-05-22 15:03:32 +00:00
return 1;
return 0;
}
2008-01-12 12:27:55 +00:00
static RECT screen = { 0.0f, 0.0f, 848.0f, 480.0f };
2007-05-22 15:03:32 +00:00
2008-01-12 12:27:55 +00:00
RECT *ui_screen()
2007-05-22 15:03:32 +00:00
{
2008-01-12 12:27:55 +00:00
float aspect = gfx_screenaspect();
float w, h;
2007-05-22 15:03:32 +00:00
2008-01-12 12:27:55 +00:00
h = 600;
w = aspect*h;
2007-05-22 15:03:32 +00:00
2008-01-12 12:27:55 +00:00
screen.w = w;
screen.h = h;
2007-05-27 18:14:24 +00:00
2008-01-12 12:27:55 +00:00
return &screen;
2007-05-22 15:03:32 +00:00
}
2008-01-12 12:27:55 +00:00
void ui_set_scale(float s)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
config.ui_scale = (int)(s*100.0f);
2007-10-29 18:40:04 +00:00
}
2008-01-12 12:27:55 +00:00
float ui_scale()
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
return config.ui_scale/100.0f;
2007-10-29 18:40:04 +00:00
}
2008-01-12 12:27:55 +00:00
void ui_clip_enable(const RECT *r)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
float xscale = gfx_screenwidth()/ui_screen()->w;
float yscale = gfx_screenheight()/ui_screen()->h;
gfx_clip_enable((int)(r->x*xscale), (int)(r->y*yscale), (int)(r->w*xscale), (int)(r->h*yscale));
2007-10-29 18:40:04 +00:00
}
2008-01-12 12:27:55 +00:00
void ui_clip_disable()
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
gfx_clip_disable();
2007-10-29 18:40:04 +00:00
}
2008-01-12 12:27:55 +00:00
void ui_hsplit_t(const RECT *original, float cut, RECT *top, RECT *bottom)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
RECT r = *original;
cut *= ui_scale();
2007-10-29 18:40:04 +00:00
if (top)
{
top->x = r.x;
top->y = r.y;
top->w = r.w;
2008-01-12 12:27:55 +00:00
top->h = cut;
2007-10-29 18:40:04 +00:00
}
if (bottom)
{
bottom->x = r.x;
2008-01-12 12:27:55 +00:00
bottom->y = r.y + cut;
2007-10-29 18:40:04 +00:00
bottom->w = r.w;
2008-01-12 12:27:55 +00:00
bottom->h = r.h - cut;
2007-10-29 18:40:04 +00:00
}
}
2008-01-12 12:27:55 +00:00
void ui_hsplit_b(const RECT *original, float cut, RECT *top, RECT *bottom)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
RECT r = *original;
cut *= ui_scale();
2007-10-29 18:40:04 +00:00
if (top)
{
top->x = r.x;
top->y = r.y;
top->w = r.w;
2008-01-12 12:27:55 +00:00
top->h = r.h - cut;
2007-10-29 18:40:04 +00:00
}
if (bottom)
{
bottom->x = r.x;
2008-01-12 12:27:55 +00:00
bottom->y = r.y + r.h - cut;
2007-10-29 18:40:04 +00:00
bottom->w = r.w;
2008-01-12 12:27:55 +00:00
bottom->h = cut;
2007-10-29 18:40:04 +00:00
}
}
2008-01-12 12:27:55 +00:00
void ui_vsplit_mid(const RECT *original, RECT *left, RECT *right)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
RECT r = *original;
float cut = r.w/2;
2007-10-29 18:40:04 +00:00
if (left)
{
left->x = r.x;
left->y = r.y;
2008-01-12 12:27:55 +00:00
left->w = cut;
2007-10-29 18:40:04 +00:00
left->h = r.h;
}
if (right)
{
2008-01-12 12:27:55 +00:00
right->x = r.x + cut;
2007-10-29 18:40:04 +00:00
right->y = r.y;
2008-01-12 12:27:55 +00:00
right->w = r.w - cut;
2007-10-29 18:40:04 +00:00
right->h = r.h;
}
2008-01-12 12:27:55 +00:00
}
void ui_vsplit_l(const RECT *original, float cut, RECT *left, RECT *right)
{
RECT r = *original;
cut *= ui_scale();
if (left)
{
left->x = r.x;
left->y = r.y;
left->w = cut;
left->h = r.h;
}
2007-10-29 18:40:04 +00:00
2008-01-12 12:27:55 +00:00
if (right)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
right->x = r.x + cut;
right->y = r.y;
right->w = r.w - cut;
right->h = r.h;
2007-10-29 18:40:04 +00:00
}
}
2008-01-12 12:27:55 +00:00
void ui_vsplit_r(const RECT *original, float cut, RECT *left, RECT *right)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
RECT r = *original;
cut *= ui_scale();
2007-10-29 18:40:04 +00:00
if (left)
{
left->x = r.x;
left->y = r.y;
2008-01-12 12:27:55 +00:00
left->w = r.w - cut;
2007-10-29 18:40:04 +00:00
left->h = r.h;
}
if (right)
{
2008-01-12 12:27:55 +00:00
right->x = r.x + r.w - cut;
2007-10-29 18:40:04 +00:00
right->y = r.y;
2008-01-12 12:27:55 +00:00
right->w = cut;
2007-10-29 18:40:04 +00:00
right->h = r.h;
}
2008-01-12 12:27:55 +00:00
}
2007-10-29 18:40:04 +00:00
2008-01-12 12:27:55 +00:00
void ui_margin(const RECT *original, float cut, RECT *other_rect)
{
RECT r = *original;
cut *= ui_scale();
other_rect->x = r.x + cut;
other_rect->y = r.y + cut;
other_rect->w = r.w - 2*cut;
other_rect->h = r.h - 2*cut;
}
void ui_vmargin(const RECT *original, float cut, RECT *other_rect)
{
RECT r = *original;
cut *= ui_scale();
other_rect->x = r.x + cut;
other_rect->y = r.y;
other_rect->w = r.w - 2*cut;
other_rect->h = r.h;
2007-10-29 18:40:04 +00:00
}
2008-01-12 12:27:55 +00:00
void ui_hmargin(const RECT *original, float cut, RECT *other_rect)
2007-10-29 18:40:04 +00:00
{
2008-01-12 12:27:55 +00:00
RECT r = *original;
cut *= ui_scale();
2007-10-29 18:40:04 +00:00
2008-01-12 12:27:55 +00:00
other_rect->x = r.x;
other_rect->y = r.y + cut;
other_rect->w = r.w;
other_rect->h = r.h - 2*cut;
}
2008-01-13 22:03:32 +00:00
2008-01-12 12:27:55 +00:00
int ui_do_button(const void *id, const char *text, int checked, const RECT *r, ui_draw_button_func draw_func, const void *extra)
{
/* logic */
int ret = 0;
int inside = ui_mouse_inside(r);
2008-01-13 22:03:32 +00:00
static int button_used = 0;
2008-01-12 12:27:55 +00:00
if(ui_active_item() == id)
{
2008-01-13 22:03:32 +00:00
if(!ui_mouse_button(button_used))
2008-01-12 12:27:55 +00:00
{
if(inside && checked >= 0)
2008-01-13 22:03:32 +00:00
ret = 1+button_used;
2008-01-12 12:27:55 +00:00
ui_set_active_item(0);
}
}
else if(ui_hot_item() == id)
{
if(ui_mouse_button(0))
2008-01-13 22:03:32 +00:00
{
ui_set_active_item(id);
button_used = 0;
}
if(ui_mouse_button(1))
{
2008-01-12 12:27:55 +00:00
ui_set_active_item(id);
2008-01-13 22:03:32 +00:00
button_used = 1;
}
2008-01-12 12:27:55 +00:00
}
if(inside)
ui_set_hot_item(id);
if(draw_func)
draw_func(id, text, checked, r, extra);
return ret;
}
void ui_do_label(const RECT *r, const char *text, float size, int align, int max_width)
{
gfx_blend_normal();
size *= ui_scale();
if(align == 0)
{
2008-01-12 15:07:57 +00:00
float tw = gfx_text_width(0, size, text, max_width);
gfx_text(0, r->x + r->w/2-tw/2, r->y, size, text, max_width);
2008-01-12 12:27:55 +00:00
}
else if(align < 0)
2008-01-12 15:07:57 +00:00
gfx_text(0, r->x, r->y, size, text, max_width);
2008-01-12 12:27:55 +00:00
else if(align > 0)
{
2008-01-12 15:07:57 +00:00
float tw = gfx_text_width(0, size, text, max_width);
gfx_text(0, r->x + r->w-tw, r->y, size, text, max_width);
2008-01-12 12:27:55 +00:00
}
2007-10-29 18:40:04 +00:00
}