1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
//=============================================================================
// Author: Arvind
// Purpose: Contains the button functions
//=============================================================================
#include "crab/crab.h"
#include "crab/input/cursor.h"
#include "crab/ui/button.h"
namespace Crab {
using namespace pyrodactyl::ui;
using namespace pyrodactyl::image;
using namespace pyrodactyl::input;
using namespace pyrodactyl::music;
using namespace pyrodactyl::text;
//------------------------------------------------------------------------
// Purpose: Constructor
//------------------------------------------------------------------------
Button::Button() {
_visible = false;
_canmove = false;
_seClick = -1;
_seHover = -1;
_hoverPrev = false;
reset();
}
//------------------------------------------------------------------------
// Purpose: Load a new Button from a file
//------------------------------------------------------------------------
void Button::load(rapidxml::xml_node<char> *node, const bool &echo) {
_img.load(node, echo);
Element::load(node, _img._normal, echo);
loadNum(_seClick, "click", node, echo);
loadNum(_seHover, "hover", node, echo);
if (nodeValid("hotkey", node, false))
_hotkey.load(node->first_node("hotkey"));
_tooltip.load(node->first_node("tooltip"), this);
_caption.load(node->first_node("caption"), this);
_visible = true;
_canmove = false;
reset();
}
//------------------------------------------------------------------------
// Purpose: Load a new Button
//------------------------------------------------------------------------
void Button::init(const Button &ref, const int &xOffset, const int &yOffset) {
_img = ref._img;
Element::init(ref, _img._normal, xOffset, yOffset);
_seClick = ref._seClick;
_seHover = ref._seHover;
_caption.init(ref._caption, xOffset, yOffset);
_tooltip.init(ref._tooltip, xOffset, yOffset);
_visible = true;
_canmove = false;
reset();
}
//------------------------------------------------------------------------
// Purpose: Reset the button
//------------------------------------------------------------------------
void Button::reset() {
_mousePressed = false;
_hoverMouse = false;
_hoverKey = false;
}
//------------------------------------------------------------------------
// Purpose: Draw
//------------------------------------------------------------------------
void Button::draw(const int &xOffset, const int &yOffset, Rect *clip) {
if (_visible) {
if (_mousePressed) {
g_engine->_imageManager->draw(x + xOffset, y + yOffset, _img._select, clip);
_tooltip.draw(xOffset, yOffset);
_caption.draw(true, xOffset, yOffset);
} else if (_hoverMouse || _hoverKey) {
g_engine->_imageManager->draw(x + xOffset, y + yOffset, _img._hover, clip);
_tooltip.draw(xOffset, yOffset);
_caption.draw(true, xOffset, yOffset);
} else {
g_engine->_imageManager->draw(x + xOffset, y + yOffset, _img._normal, clip);
_caption.draw(false, xOffset, yOffset);
}
}
}
void Button::imageCaptionOnlyDraw(const int &xOffset, const int &yOffset, Rect *clip) {
if (_visible) {
if (_mousePressed) {
g_engine->_imageManager->draw(x + xOffset, y + yOffset, _img._select, clip);
_caption.draw(true, xOffset, yOffset);
} else if (_hoverMouse || _hoverKey) {
g_engine->_imageManager->draw(x + xOffset, y + yOffset, _img._hover, clip);
_caption.draw(true, xOffset, yOffset);
} else {
g_engine->_imageManager->draw(x + xOffset, y + yOffset, _img._normal, clip);
_caption.draw(false, xOffset, yOffset);
}
}
}
void Button::hoverInfoOnlyDraw(const int &xOffset, const int &yOffset, Rect *clip) {
if (_visible) {
if (_mousePressed || _hoverMouse || _hoverKey)
_tooltip.draw(xOffset, yOffset);
}
}
//------------------------------------------------------------------------
// Purpose: Handle input and stuff
//------------------------------------------------------------------------
ButtonAction Button::handleEvents(const Common::Event &event, const int &xOffset, const int &yOffset) {
Rect dim = *this;
dim.x += xOffset;
dim.y += yOffset;
if (_visible) {
if (dim.contains(g_engine->_mouse->_motion.x, g_engine->_mouse->_motion.y)) {
_hoverMouse = true;
if (!_hoverPrev) {
_hoverPrev = true;
g_engine->_musicManager->playEffect(_seHover, 0);
}
} else {
_hoverPrev = false;
_hoverMouse = false;
}
if (event.type == Common::EVENT_MOUSEMOVE) {
if (_canmove && _mousePressed) {
x += g_engine->_mouse->_rel.x;
y += g_engine->_mouse->_rel.y;
return BUAC_GRABBED;
}
} else if (event.type == Common::EVENT_LBUTTONDOWN || event.type == Common::EVENT_RBUTTONDOWN) {
// The g_engine->_mouse button pressed, then released, comprises of a click action
if (dim.contains(g_engine->_mouse->_button.x, g_engine->_mouse->_button.y))
_mousePressed = true;
} else if ((event.type == Common::EVENT_LBUTTONUP || event.type == Common::EVENT_RBUTTONUP) && _mousePressed) {
reset();
if (dim.contains(g_engine->_mouse->_button.x, g_engine->_mouse->_button.y)) {
_mousePressed = false;
if (event.type == Common::EVENT_LBUTTONUP) {
g_engine->_musicManager->playEffect(_seClick, 0);
return BUAC_LCLICK;
} else if (event.type == Common::EVENT_RBUTTONUP)
return BUAC_RCLICK;
}
} else if (_hotkey.handleEvents(event)) {
g_engine->_musicManager->playEffect(_seClick, 0);
return BUAC_LCLICK;
}
}
return BUAC_IGNORE;
}
void Button::setUI(Rect *parent) {
Element::setUI(parent);
_tooltip.setUI(this);
_caption.setUI(this);
}
} // End of namespace Crab
|