#include "dl_ui_button_base.h" namespace dl::UI { Button* Button::_clickControl = nullptr; void Button::_update_button() { if (_clickControl == this) { if (IsPickup()) { if (g_input->KeyState(MouseCode::LEFT)) { _state = State::DOWN; } else { _state = State::OVER; _clickControl = nullptr; } } 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) { _state = State::OVER; } else { _state = State::NORMAL; } } } else _state = State::NORMAL; } } void Button::_update_switch() { _isSwitchClick = false; if (_clickControl == this) { if (IsPickup()) { if (g_input->KeyState(MouseCode::LEFT)) { _state = State::DOWN; } else { _bOpen = !_bOpen; _state = _bOpen ? State::DOWN : State::OVER; _clickControl = nullptr; _isSwitchClick = true; } } else { if (g_input->KeyState(MouseCode::LEFT)) { } else { _clickControl = nullptr; } _state = _bOpen ? State::DOWN : State::NORMAL; } } else { if (IsPickup()) { if (g_input->KeyState(MouseCode::LEFT)) { _clickControl = this; _state = State::DOWN; } else { if (_clickControl == nullptr) { _state = _bOpen ? State::DOWN : State::OVER; } else { _state = State::NORMAL; } } } else _state = _bOpen ? State::DOWN : State::NORMAL; } } void Button::_update_radio() { //点击了里面,变为open(并不能撤销) if (IsPickup()) { if (g_input->KeyUp(MouseCode::LEFT)) { _bOpen = true; _state = State::DOWN; } else if (g_input->KeyState(MouseCode::LEFT)) { _state = State::DOWN; } else if (_bOpen == false) { _state = State::OVER; } } else { _state = _bOpen ? State::DOWN : State::NORMAL; } } #define snd_play void Button::Update() { if (_bDisable) { UpdateChild(_statePre, _state); return; } _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.c_str()); if (!_strSndOver.empty() && IsThrough()) snd_play(_strSndOver.c_str()); UpdateChild(_statePre, _state); } void Button::Render() { RenderChild(_statePre, _state); } void Button::SetSelect(bool open) { _bOpen = open; //放止Release事件 _state = _bOpen ? State::DOWN : (_state == State::OVER ? State::OVER : State::NORMAL); } bool Button::IsClick() { switch (_mode) { case Mode::BUTTON: return (_statePre == State::DOWN) && (_state == State::OVER); case Mode::SWITCH: return (!_bDisable && _isSwitchClick); case Mode::RADIO: return (_statePre == State::OVER) && (_state == State::DOWN); default: return false; } } bool Button::IsThrough() { return (_statePre == State::NORMAL) && (_state == State::OVER); } void Button::SetMode(Mode mode) { _mode = mode; } }