2019-07-26 11:57:28 -07:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2019-09-22 07:53:27 -07:00
|
|
|
#include "IGUIButtonBehavior.h"
|
|
|
|
|
|
2019-09-18 13:51:45 -07:00
|
|
|
#include "gui/CGUI.h"
|
2019-09-20 06:11:18 -07:00
|
|
|
#include "gui/CGUISprite.h"
|
2019-09-18 13:51:45 -07:00
|
|
|
|
2019-08-21 03:12:33 -07:00
|
|
|
IGUIButtonBehavior::IGUIButtonBehavior(CGUI& pGUI)
|
2019-09-22 07:53:27 -07:00
|
|
|
: IGUIObject(pGUI),
|
|
|
|
|
m_Pressed(false),
|
|
|
|
|
m_PressedRight(false)
|
2003-11-23 18:18:41 -08:00
|
|
|
{
|
2019-09-22 07:53:27 -07:00
|
|
|
AddSetting<CStrW>("sound_disabled");
|
|
|
|
|
AddSetting<CStrW>("sound_enter");
|
|
|
|
|
AddSetting<CStrW>("sound_leave");
|
|
|
|
|
AddSetting<CStrW>("sound_pressed");
|
|
|
|
|
AddSetting<CStrW>("sound_released");
|
2003-11-23 18:18:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IGUIButtonBehavior::~IGUIButtonBehavior()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void IGUIButtonBehavior::HandleMessage(SGUIMessage& Message)
|
2003-11-23 18:18:41 -08:00
|
|
|
{
|
2019-08-26 05:25:07 -07:00
|
|
|
const bool enabled = GetSetting<bool>("enabled");
|
2019-08-23 07:43:10 -07:00
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
// TODO Gee: easier access functions
|
2003-12-26 22:26:03 -08:00
|
|
|
switch (Message.type)
|
2003-11-23 18:18:41 -08:00
|
|
|
{
|
2013-06-30 21:15:09 -07:00
|
|
|
case GUIM_MOUSE_ENTER:
|
2019-08-22 15:34:12 -07:00
|
|
|
if (enabled)
|
|
|
|
|
PlaySound("sound_enter");
|
2013-12-31 22:05:41 -08:00
|
|
|
break;
|
2013-06-30 21:15:09 -07:00
|
|
|
|
|
|
|
|
case GUIM_MOUSE_LEAVE:
|
2019-08-22 15:34:12 -07:00
|
|
|
if (enabled)
|
|
|
|
|
PlaySound("sound_leave");
|
2013-12-31 22:05:41 -08:00
|
|
|
break;
|
2013-06-30 21:15:09 -07:00
|
|
|
|
2013-12-29 11:31:48 -08:00
|
|
|
case GUIM_MOUSE_DBLCLICK_LEFT:
|
2013-12-31 22:05:41 -08:00
|
|
|
if (!enabled)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Since GUIM_MOUSE_PRESS_LEFT also gets called twice in a
|
|
|
|
|
// doubleclick event, we let it handle playing sounds.
|
|
|
|
|
SendEvent(GUIM_DOUBLE_PRESSED, "doublepress");
|
|
|
|
|
break;
|
|
|
|
|
|
2003-11-23 18:18:41 -08:00
|
|
|
case GUIM_MOUSE_PRESS_LEFT:
|
2004-05-28 21:06:50 -07:00
|
|
|
if (!enabled)
|
2013-06-30 21:15:09 -07:00
|
|
|
{
|
2019-08-22 15:34:12 -07:00
|
|
|
PlaySound("sound_disabled");
|
2003-12-26 22:26:03 -08:00
|
|
|
break;
|
2013-06-30 21:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 15:34:12 -07:00
|
|
|
PlaySound("sound_pressed");
|
2013-12-29 11:31:48 -08:00
|
|
|
SendEvent(GUIM_PRESSED, "press");
|
2003-11-23 18:18:41 -08:00
|
|
|
m_Pressed = true;
|
2013-12-31 22:05:41 -08:00
|
|
|
break;
|
2013-01-03 14:53:46 -08:00
|
|
|
|
|
|
|
|
case GUIM_MOUSE_DBLCLICK_RIGHT:
|
2013-12-31 22:05:41 -08:00
|
|
|
if (!enabled)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Since GUIM_MOUSE_PRESS_RIGHT also gets called twice in a
|
|
|
|
|
// doubleclick event, we let it handle playing sounds.
|
|
|
|
|
SendEvent(GUIM_DOUBLE_PRESSED_MOUSE_RIGHT, "doublepressright");
|
|
|
|
|
break;
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2013-12-29 11:31:48 -08:00
|
|
|
case GUIM_MOUSE_PRESS_RIGHT:
|
2013-01-03 14:53:46 -08:00
|
|
|
if (!enabled)
|
|
|
|
|
{
|
2019-08-22 15:34:12 -07:00
|
|
|
PlaySound("sound_disabled");
|
2013-12-29 11:31:48 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-31 22:05:41 -08:00
|
|
|
// Button was right-clicked
|
2019-08-22 15:34:12 -07:00
|
|
|
PlaySound("sound_pressed");
|
2013-12-29 11:31:48 -08:00
|
|
|
SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright");
|
|
|
|
|
m_PressedRight = true;
|
2013-12-31 22:05:41 -08:00
|
|
|
break;
|
2013-01-03 14:53:46 -08:00
|
|
|
|
2013-12-29 11:31:48 -08:00
|
|
|
case GUIM_MOUSE_RELEASE_RIGHT:
|
2013-01-03 14:53:46 -08:00
|
|
|
if (!enabled)
|
2013-12-29 11:31:48 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (m_PressedRight)
|
2013-06-30 21:15:09 -07:00
|
|
|
{
|
2013-12-29 11:31:48 -08:00
|
|
|
m_PressedRight = false;
|
2019-08-22 15:34:12 -07:00
|
|
|
PlaySound("sound_released");
|
2013-06-30 21:15:09 -07:00
|
|
|
}
|
2013-12-31 22:05:41 -08:00
|
|
|
break;
|
2003-11-23 18:18:41 -08:00
|
|
|
|
|
|
|
|
case GUIM_MOUSE_RELEASE_LEFT:
|
2004-05-28 21:06:50 -07:00
|
|
|
if (!enabled)
|
2003-12-26 22:26:03 -08:00
|
|
|
break;
|
|
|
|
|
|
2003-11-23 18:18:41 -08:00
|
|
|
if (m_Pressed)
|
|
|
|
|
{
|
|
|
|
|
m_Pressed = false;
|
2019-08-22 15:34:12 -07:00
|
|
|
PlaySound("sound_released");
|
2003-11-23 18:18:41 -08:00
|
|
|
}
|
2013-12-31 22:05:41 -08:00
|
|
|
break;
|
2003-11-23 18:18:41 -08:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void IGUIButtonBehavior::DrawButton(const CRect& rect, const float& z, CGUISpriteInstance& sprite, CGUISpriteInstance& sprite_over, CGUISpriteInstance& sprite_pressed, CGUISpriteInstance& sprite_disabled, int cell_id)
|
2004-05-28 21:06:50 -07:00
|
|
|
{
|
2019-08-26 05:25:07 -07:00
|
|
|
if (!GetSetting<bool>("enabled"))
|
2019-08-21 03:12:33 -07:00
|
|
|
m_pGUI.DrawSprite(sprite_disabled || sprite, cell_id, z, rect);
|
2015-08-21 10:08:41 -07:00
|
|
|
else if (m_MouseHovering)
|
|
|
|
|
{
|
|
|
|
|
if (m_Pressed)
|
2019-08-21 03:12:33 -07:00
|
|
|
m_pGUI.DrawSprite(sprite_pressed || sprite, cell_id, z, rect);
|
2013-12-29 12:46:02 -08:00
|
|
|
else
|
2019-08-21 03:12:33 -07:00
|
|
|
m_pGUI.DrawSprite(sprite_over || sprite, cell_id, z, rect);
|
2004-05-28 21:06:50 -07:00
|
|
|
}
|
2015-08-21 10:08:41 -07:00
|
|
|
else
|
2019-08-21 03:12:33 -07:00
|
|
|
m_pGUI.DrawSprite(sprite, cell_id, z, rect);
|
2004-06-10 19:14:18 -07:00
|
|
|
}
|