Files
dl_old/dl/ui/dl_ui_button_base.cpp

449 lines
8.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "dl_ui_button_base.h"
#ifndef DL_DISABLE_RENDER
#include "system/dl_audio.h"
#include "render/dl_factory.h"
#include "render/dl_graphics.h"
namespace dl::UI
{
Button* Button::_clickControl = nullptr;
void Button::_update_button()
{
_isClick = false;
if (_clickControl == this)
{
if (IsPickup())
{
if (g_input->KeyState(MouseCode::LEFT))
{
_state = State::DOWN;
}
else
{
_state = _use_over() ? State::OVER : State::NORMAL;
_clickControl = nullptr;
_isClick = true;
if(_cbClick)
_cbClick(this);
}
}
else
{
if (g_input->KeyState(MouseCode::LEFT))
{
}
else
{
_clickControl = nullptr;
}
_state = State::NORMAL;
}
}
else
{
if (IsPickup())
{
if (g_input->KeyDown(MouseCode::LEFT))
{
_clickControl = this;
_state = State::DOWN;
}
else
{
if (_clickControl == nullptr && _use_over())
{
_state = State::OVER;
}
else
{
_state = State::NORMAL;
}
}
}
else
_state = State::NORMAL;
}
}
void Button::_update_switch()
{
_isClick = false;
if (_clickControl == this)
{
if (IsPickup())
{
if (g_input->KeyState(MouseCode::LEFT))
{
_state = State::DOWN;
}
else
{
_bOpen = !_bOpen;
_state = (_bOpen || !_use_over()) ? State::DOWN : State::OVER;
_clickControl = nullptr;
_isClick = true;
if(_cbClick)
_cbClick(this);
}
}
else
{
if (g_input->KeyState(MouseCode::LEFT))
{
}
else
{
_clickControl = nullptr;
}
_state = _bOpen ? State::DOWN : State::NORMAL;
}
}
else
{
if (IsPickup())
{
if (g_input->KeyDown(MouseCode::LEFT))
{
_clickControl = this;
_state = State::DOWN;
}
else
{
if (_clickControl == nullptr)
{
_state = (_bOpen || !_use_over()) ? State::DOWN : State::OVER;
}
else
{
_state = State::NORMAL;
}
}
}
else
_state = _bOpen ? State::DOWN : State::NORMAL;
}
}
void Button::_update_radio()
{
_isClick = false;
//点击了里面,变为open(并不能撤销)
if (_clickControl == this)
{
if (IsPickup())
{
if (g_input->KeyUp(MouseCode::LEFT))
{
_bOpen = true;
_state = State::DOWN;
_clickControl = nullptr;
_isClick = true;
if(_cbClick)
_cbClick(this);
}
else if (g_input->KeyState(MouseCode::LEFT))
{
_state = State::DOWN;
}
else if (_bOpen == false)
{
_state = _use_over() ? State::OVER : State::NORMAL;
}
}
else
{
_state = _bOpen ? State::DOWN : State::NORMAL;
}
}
else
{
if (IsPickup())
{
if (g_input->KeyDown(MouseCode::LEFT))
{
_clickControl = this;
_state = State::DOWN;
}
else
{
if (_clickControl == nullptr)
{
_state = (_bOpen || !_use_over()) ? State::DOWN : State::OVER;
}
else
{
_state = State::NORMAL;
}
}
}
else
_state = _bOpen ? State::DOWN : State::NORMAL;
}
}
void Button::Update()
{
if (_bDisable)
{
UpdateChild(_statePre, _state);
return;
}
// 必须放在state修改之前
#ifndef NDEBUG
if (g_input->KeyUp(KeyCode::PAUSE))
{
if (IsPickup())
log_pause("pause键检测当前按钮:{}", "btn");
}
#endif
_statePre = _state;
switch (_mode)
{
case Mode::BUTTON:
_update_button();
break;
case Mode::SWITCH:
_update_switch();
break;
case Mode::RADIO:
_update_radio();
break;
default:
break;
}
if (!_strSndClick.empty() && IsClick())
snd_play(_strSndClick);
if (!_strSndOver.empty() && IsOverOnce())
snd_play(_strSndOver);
UpdateChild(_statePre, _state);
}
void Button::Render()
{
RenderChild(_statePre, _state);
if (_cbRender)
_cbRender(this);
}
void Button::SetSelect(bool open)
{
_bOpen = open;
// 手动设置,避免触发其他事件
//_state = _bOpen ? State::DOWN : (_state == State::OVER ? State::OVER : State::NORMAL);
/*_statePre = State::OVER;
_state = State::DOWN;*/
}
bool Button::IsClick()
{
return (!_bDisable && _isClick);
/*if (_use_over())
{
switch (_mode)
{
case Mode::BUTTON:
return (_statePre == State::DOWN) && (_state == State::OVER);
case Mode::SWITCH:
case Mode::RADIO:
return (_statePre == State::OVER) && (_state == State::DOWN);
default:
return false;
}
}
else
{
switch (_mode)
{
case Mode::BUTTON:
return (_statePre == State::DOWN) && (_state == State::NORMAL);
case Mode::SWITCH:
return (!_bDisable && _isClick);
case Mode::RADIO:
return (_statePre == State::NORMAL) && (_state == State::DOWN);
default:
return false;
}
}*/
}
bool Button::IsOverOnce()
{
return (_statePre == State::NORMAL) && (_state == State::OVER);
}
void Button::SetMode(Mode mode)
{
_mode = mode;
}
void Button::ClearState()
{
_state = State::NORMAL;
_statePre = State::NORMAL;
_isClick = false;
if (_clickControl == this)
_clickControl = nullptr;
}
void Button::AddText(gcText txt, const std::vector<Color>& mul_col, const std::vector<Position>& mul_offset)
{
if (!_txtData)
_txtData = new TextData;
_txtData->_txt = txt;
if (mul_col.size() == 1)
{
_txtData->_col[0] = mul_col[0];
_txtData->_col[1] = mul_col[0];
_txtData->_col[2] = mul_col[0];
}
else if (mul_col.size() == 2)
{
_txtData->_col[0] = mul_col[0];
_txtData->_col[1] = mul_col[1];
_txtData->_col[2] = mul_col[0];
}
else if (mul_col.size() >= 3)
{
_txtData->_col[0] = mul_col[0];
_txtData->_col[1] = mul_col[1];
_txtData->_col[2] = mul_col[2];
if (mul_col.size() == 4)
{
_txtData->_col[3] = mul_col[3];
_txtData->_col[4] = mul_col[3];
}
else if (mul_col.size() == 5)
{
_txtData->_col[3] = mul_col[3];
_txtData->_col[4] = mul_col[4];
}
else if (mul_col.size() != 3)
log_warn0("传入过多的颜色没有意义!");
}
for (size_t i = 0; i < mul_offset.size() && i < 5; ++i)
{
_txtData->_offset[i] = mul_offset[i];
}
_txtData->_txt->GetCoor()->SetParent(_coor);
_txtData->_txt->SetOrder(Math::OffsetOrder(GetOrder(), 0, 0, -1));
if (_txtData->_bSizeWithText)
{
OnTextSizeChange(_txtData->_extraSize + _txtData->_txt->GetSize());
}
}
void Button::AddText(gcText txt)
{
AddText(txt,
{ ColorDef::GRAY_DARK, ColorDef::BLACK, ColorDef::RED },
{ {}, {0, 0}, {0, 1} });
}
void Button::AddText(gcText txt, const std::vector<Color>& mul_col)
{
AddText(txt, mul_col,
{ {}, {0, 0}, {0, 1} });
}
void Button::AddTextDefault()
{
AddText(g_factory->CreateText(),
{ ColorDef::GRAY_DARK, ColorDef::BLACK, ColorDef::RED },
{ {}, {0, 0}, {0, 1} });
}
void Button::SetString(std::string_view str)
{
if (!_txtData)
{
log_notice0("按钮并没有添加文本,将自动添加文本!");
AddTextDefault();
}
_txtData->_txt->SetString(str);
if (_txtData->_bSizeWithText)
{
OnTextSizeChange(_txtData->_extraSize + _txtData->_txt->GetSize());
}
}
Button::Button(const Button& b) :
_state{ b._state },
_statePre{ b._statePre },
_mode{ b._mode },
_bOpen{ b._bOpen },
_isClick{ b._isClick },
_bDisable{ b._bDisable },
_cptElemData{ b._cptElemData },
_cpt{ this }
{
_coor = Coor::Create(const_cast<Button&>(b).GetCoor()->GetParent());
if (b._txtData)
{
_txtData = new TextData(*b._txtData);
_txtData->_txt = g_factory->CloneText(b._txtData->_txt);
_txtData->_txt->GetCoor()->SetParent(_coor);
}
_init_cpt();
}
void Button::RenderDebug()
{
if (_txtData)
{
Position p0 = _txtData->_txt->GetCoor()->GetPosition();
Position p1 = p0 + _txtData->_txt->GetSize();
g_gfx->SetCoor(_coor);
g_gfx->SetColor(createColor(ColorDef::PURPLE, 1.0f));
g_gfx->Rectangle2D(p0, p1);
}
}
void Button::UpdateChild(State state_pre, State state)
{
if (_txtData)
{
size_t index = GetIndex(state);
_txtData->_txt->SetColor(_txtData->_col[index]);
_txtData->_txt->GetCoor()->SetPosition(
_txtData->_offset[index]
+ Size2Anchor(GetSize(), toPosition(_txtData->_txt->GetSize()), _txtData->_hv));
}
}
void Button::RenderChild(State state_pre, State state)
{
if (_txtData)
_txtData->_txt->Render();
}
void Button::SetSizeWithText(bool with, const Position& extra_size)
{
if (!_txtData)
{
log_warn0("需要先AddText!");
return;
}
_txtData->_bSizeWithText = with;
_txtData->_extraSize = extra_size;
OnTextSizeChange(_txtData->_extraSize + _txtData->_txt->GetSize());
}
}
#endif