Files
dl_old/dl/ui/dl_ui_button_base.cpp
T
2026-01-10 21:25:15 +08:00

284 lines
4.4 KiB
C++

#include "dl_ui_button_base.h"
#ifndef DL_DISABLE_RENDER
#include "system/dl_audio.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;
}
}
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;
}
}
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;
}
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() && IsThrough())
snd_play(_strSndOver);
UpdateChild(_statePre, _state);
}
void Button::Render()
{
RenderChild(_statePre, _state);
}
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::IsThrough()
{
return (_statePre == State::NORMAL) && (_state == State::OVER);
}
void Button::SetMode(Mode mode)
{
_mode = mode;
}
}
#endif