ddnet/src/game/client/ui.cpp

329 lines
6.1 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 */
#include <base/system.h>
#include <engine/e_client_interface.h>
2007-12-15 10:24:49 +00:00
#include <engine/e_config.h>
2009-10-27 14:38:53 +00:00
#include <engine/client/graphics.h>
2008-08-30 21:31:01 +00:00
#include "ui.hpp"
2007-05-22 15:03:32 +00:00
/********************************************************
UI
*********************************************************/
2009-10-27 14:38:53 +00:00
CUI::CUI()
{
m_pHotItem = 0;
m_pActiveItem = 0;
m_pLastActiveItem = 0;
m_pBecommingHotItem = 0;
m_MouseX = 0;
m_MouseY = 0;
m_MouseWorldX = 0;
m_MouseWorldY = 0;
m_MouseButtons = 0;
m_LastMouseButtons = 0;
m_Screen.x = 0;
m_Screen.y = 0;
m_Screen.w = 848.0f;
m_Screen.h = 480.0f;
}
int CUI::Update(float mx, float my, float mwx, float mwy, int Buttons)
2007-05-22 15:03:32 +00:00
{
2009-10-27 14:38:53 +00:00
m_MouseX = mx;
m_MouseY = my;
m_MouseWorldX = mwx;
m_MouseWorldY = mwy;
m_LastMouseButtons = m_MouseButtons;
m_MouseButtons = Buttons;
m_pHotItem = m_pBecommingHotItem;
if(m_pActiveItem)
m_pHotItem = m_pActiveItem;
m_pBecommingHotItem = 0;
2007-05-22 15:03:32 +00:00
return 0;
}
2009-10-27 14:38:53 +00:00
int CUI::MouseInside(const CUIRect *r)
2007-05-22 15:03:32 +00:00
{
2009-10-27 14:38:53 +00:00
if(m_MouseX >= r->x && m_MouseX <= r->x+r->w && m_MouseY >= r->y && m_MouseY <= r->y+r->h)
2007-05-22 15:03:32 +00:00
return 1;
return 0;
}
2009-10-27 14:38:53 +00:00
CUIRect *CUI::Screen()
2007-05-22 15:03:32 +00:00
{
2009-10-27 14:38:53 +00:00
float aspect = Graphics()->ScreenAspect();
2008-01-12 12:27:55 +00:00
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
2009-10-27 14:38:53 +00:00
m_Screen.w = w;
m_Screen.h = h;
2007-05-27 18:14:24 +00:00
2009-10-27 14:38:53 +00:00
return &m_Screen;
2007-05-22 15:03:32 +00:00
}
2009-10-27 14:38:53 +00:00
void CUI::SetScale(float s)
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
//config.UI()->Scale = (int)(s*100.0f);
2007-10-29 18:40:04 +00:00
}
2009-10-27 14:38:53 +00:00
/*float CUI::Scale()
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
return config.UI()->Scale/100.0f;
}*/
2007-10-29 18:40:04 +00:00
2009-10-27 14:38:53 +00:00
void CUI::ClipEnable(const CUIRect *r)
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
float xscale = Graphics()->ScreenWidth()/Screen()->w;
float yscale = Graphics()->ScreenHeight()/Screen()->h;
Graphics()->ClipEnable((int)(r->x*xscale), (int)(r->y*yscale), (int)(r->w*xscale), (int)(r->h*yscale));
2007-10-29 18:40:04 +00:00
}
2009-10-27 14:38:53 +00:00
void CUI::ClipDisable()
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
Graphics()->ClipDisable();
2007-10-29 18:40:04 +00:00
}
2009-10-27 14:38:53 +00:00
void CUIRect::HSplitTop(float cut, CUIRect *top, CUIRect *bottom) const
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= 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
}
}
2009-10-27 14:38:53 +00:00
void CUIRect::HSplitBottom(float cut, CUIRect *top, CUIRect *bottom) const
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= 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
2009-10-27 14:38:53 +00:00
void CUIRect::VSplitMid(CUIRect *left, CUIRect *right) const
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
2008-01-12 12:27:55 +00:00
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
}
2009-10-27 14:38:53 +00:00
void CUIRect::VSplitLeft(float cut, CUIRect *left, CUIRect *right) const
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= Scale();
2008-01-12 12:27:55 +00:00
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
}
}
2009-10-27 14:38:53 +00:00
void CUIRect::VSplitRight(float cut, CUIRect *left, CUIRect *right) const
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= 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
2009-10-27 14:38:53 +00:00
void CUIRect::Margin(float cut, CUIRect *other_rect) const
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= Scale();
2008-01-12 12:27:55 +00:00
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;
}
2009-10-27 14:38:53 +00:00
void CUIRect::VMargin(float cut, CUIRect *other_rect) const
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= Scale();
2008-01-12 12:27:55 +00:00
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
}
2009-10-27 14:38:53 +00:00
void CUIRect::HMargin(float cut, CUIRect *other_rect) const
2007-10-29 18:40:04 +00:00
{
2009-10-27 14:38:53 +00:00
CUIRect r = *this;
cut *= 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;
}
2009-10-27 14:38:53 +00:00
int CUI::DoButtonLogic(const void *pID, const char *pText, int Checked, const CUIRect *pRect)
2008-01-12 12:27:55 +00:00
{
/* logic */
2009-10-27 14:38:53 +00:00
int ReturnValue = 0;
int Inside = MouseInside(pRect);
static int ButtonUsed = 0;
if(ActiveItem() == pID)
{
if(!MouseButton(ButtonUsed))
{
if(Inside && Checked >= 0)
ReturnValue = 1+ButtonUsed;
SetActiveItem(0);
}
}
else if(HotItem() == pID)
{
if(MouseButton(0))
{
SetActiveItem(pID);
ButtonUsed = 0;
}
if(MouseButton(1))
{
SetActiveItem(pID);
ButtonUsed = 1;
}
}
if(Inside)
SetHotItem(pID);
return ReturnValue;
}
/*
int CUI::DoButton(const void *id, const char *text, int checked, const CUIRect *r, ui_draw_button_func draw_func, const void *extra)
{
// logic
2008-01-12 12:27:55 +00:00
int ret = 0;
2009-10-27 14:38:53 +00:00
int inside = ui_MouseInside(r);
2008-01-13 22:03:32 +00:00
static int button_used = 0;
2008-01-12 12:27:55 +00:00
2009-10-27 14:38:53 +00:00
if(ui_ActiveItem() == id)
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
if(!ui_MouseButton(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;
2009-10-27 14:38:53 +00:00
ui_SetActiveItem(0);
2008-01-12 12:27:55 +00:00
}
}
2009-10-27 14:38:53 +00:00
else if(ui_HotItem() == id)
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
if(ui_MouseButton(0))
2008-01-13 22:03:32 +00:00
{
2009-10-27 14:38:53 +00:00
ui_SetActiveItem(id);
2008-01-13 22:03:32 +00:00
button_used = 0;
}
2009-10-27 14:38:53 +00:00
if(ui_MouseButton(1))
2008-01-13 22:03:32 +00:00
{
2009-10-27 14:38:53 +00:00
ui_SetActiveItem(id);
2008-01-13 22:03:32 +00:00
button_used = 1;
}
2008-01-12 12:27:55 +00:00
}
if(inside)
2009-10-27 14:38:53 +00:00
ui_SetHotItem(id);
2008-01-12 12:27:55 +00:00
if(draw_func)
draw_func(id, text, checked, r, extra);
return ret;
2009-10-27 14:38:53 +00:00
}*/
2008-01-12 12:27:55 +00:00
2009-10-27 14:38:53 +00:00
void CUI::DoLabel(const CUIRect *r, const char *text, float size, int align, int max_width)
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
// TODO: FIX ME!!!!
//Graphics()->BlendNormal();
size *= Scale();
2008-01-12 12:27:55 +00:00
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
}