scummvm/graphics/macgui/macbutton.cpp

238 lines
6.7 KiB
C++
Raw Normal View History

2020-06-04 23:08:52 -04:00
/* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/timer.h"
#include "common/system.h"
#include "graphics/primitives.h"
#include "graphics/macgui/macwindowmanager.h"
#include "graphics/macgui/macfontmanager.h"
#include "graphics/macgui/macmenu.h"
#include "graphics/macgui/macbutton.h"
#include "graphics/macgui/macwidget.h"
#include "graphics/macgui/macwindow.h"
namespace Graphics {
MacButton::MacButton(MacButtonType buttonType, TextAlign textAlignment, MacWidget *parent, int x, int y, int w, int h, MacWindowManager *wm, const Common::U32String &s, const MacFont *macFont, int fgcolor, int bgcolor) :
MacText(parent, x, y, w, h, wm, s, macFont, fgcolor, bgcolor, w, textAlignment), _pd(Graphics::MacPlotData(_composeSurface, nullptr, &_wm->getPatterns(), 1, 0, 0, 1, 0, true)) {
2020-06-04 23:08:52 -04:00
_buttonType = buttonType;
_invertOuter = false;
_invertInner = false;
_checkBoxType = 0;
2020-06-04 23:08:52 -04:00
switch (buttonType) {
case kCheckBox:
_alignOffset.x += 16;
_dims.right += 16;
break;
case kRound:
_alignOffset.x += 2;
_alignOffset.y += 2;
_dims.right += 2;
_dims.bottom += 4;
break;
case kRadio:
_alignOffset.x += 16;
_dims.right += 16;
break;
}
_composeSurface->create(_dims.width(), _dims.height(), _wm->_pixelformat);
_composeSurface->clear(_bgcolor);
}
MacButton::MacButton(MacButtonType buttonType, TextAlign textAlignment, MacWidget *parent, int x, int y, int w, int h, MacWindowManager *wm, const Common::String &s, const MacFont *macFont, int fgcolor, int bgcolor, Common::CodePage encodeType) :
MacText(parent, x, y, w, h, wm, s, macFont, fgcolor, bgcolor, w, textAlignment, 0, 0, 0, 0, 0, encodeType), _pd(Graphics::MacPlotData(_composeSurface, nullptr, &_wm->getPatterns(), 1, 0, 0, 1, 0, true)) {
_buttonType = buttonType;
_invertOuter = false;
_invertInner = false;
_checkBoxType = 0;
switch (buttonType) {
case kCheckBox:
_alignOffset.x += 16;
_dims.right += 16;
2020-06-04 23:08:52 -04:00
break;
case kRound:
_alignOffset.x += 2;
2020-06-04 23:08:52 -04:00
_alignOffset.y += 2;
_dims.right += 2;
_dims.bottom += 4;
2020-06-04 23:08:52 -04:00
break;
case kRadio:
_alignOffset.x += 16;
_dims.right += 16;
2020-06-04 23:08:52 -04:00
break;
}
_composeSurface->create(_dims.width(), _dims.height(), _wm->_pixelformat);
_composeSurface->clear(_bgcolor);
2020-06-04 23:08:52 -04:00
}
void MacButton::setActive(bool active) {
if (active == _active)
return;
MacWidget::setActive(active);
if (_composeSurface) {
_invertOuter = !_invertOuter;
_contentIsDirty = true;
}
2020-06-04 23:08:52 -04:00
}
// whether to use getDrawPixel or getDrawInvertPixel to draw invert pixel, maybe depends on the pattle we are using
2020-06-04 23:08:52 -04:00
void MacButton::invertOuter() {
Common::Rect r(_dims.width() - 1, _dims.height() - 1);
switch (_buttonType) {
case kCheckBox: {
Common::Rect c = Common::Rect(r.left + 1, r.top + 3, r.left + 9, r.top + 11);
Graphics::drawRect(c, 0, _wm->getDrawPixel(), &_pd);
2020-06-04 23:08:52 -04:00
}
break;
case kRound:
Graphics::drawRoundRect(r, 4, 0, true, _wm->getDrawPixel(), &_pd);
2020-06-04 23:08:52 -04:00
break;
case kRadio:
Graphics::drawEllipse(r.left + 1, r.top + 3, r.left + 10, r.top + 12, 0, false, _wm->getDrawPixel(), &_pd);
2020-06-04 23:08:52 -04:00
break;
}
}
void MacButton::setCheckBoxType(int type) {
if (_checkBoxType == type)
return;
_checkBoxType = type;
_contentIsDirty = true;
}
2020-06-04 23:08:52 -04:00
void MacButton::invertInner() {
Common::Rect r(_dims.width() - 1, _dims.height() - 1);
Common::Rect c;
2020-06-04 23:08:52 -04:00
switch (_buttonType) {
case kCheckBox:
switch(_checkBoxType) {
case kNormal:
Graphics::drawLine(r.left + 1, r.top + 3, r.left + 9, r.top + 11, 0, _wm->getDrawPixel(), &_pd);
Graphics::drawLine(r.left + 1, r.top + 11, r.left + 9, r.top + 3, 0, _wm->getDrawPixel(), &_pd);
(_wm->getDrawInvertPixel())(5, 7, 0, &_pd);
break;
case kInsetBlack:
c = Common::Rect(r.left + 2, r.top + 4, r.left + 2 + 6, r.top + 4 + 6);
Graphics::drawFilledRect(c, 0, _wm->getDrawPixel(), &_pd);
break;
case kFilledBlack:
c = Common::Rect(r.left + 1, r.top + 3, r.left + 1 + 8, r.top + 3 + 8);
Graphics::drawFilledRect(c, 0, _wm->getDrawPixel(), &_pd);
break;
}
2020-06-04 23:08:52 -04:00
break;
case kRound:
break;
case kRadio:
Graphics::drawEllipse(r.left + 3, r.top + 5, r.left + 8, r.top + 10, 0, true, _wm->getDrawPixel(), &_pd);
2020-06-04 23:08:52 -04:00
break;
}
}
2021-06-13 20:09:25 +08:00
void MacButton::setHilite(bool hilite) {
if (hilite == _invertInner)
return;
_invertInner = hilite;
_contentIsDirty = true;
}
2020-06-04 23:08:52 -04:00
bool MacButton::draw(bool forceRedraw) {
if (!_contentIsDirty && !forceRedraw)
2020-06-04 23:08:52 -04:00
return false;
MacText::draw();
2020-06-04 23:08:52 -04:00
Common::Rect r(_dims.width() - 1, _dims.height() - 1);
Graphics::MacPlotData pd(_composeSurface, nullptr, &_wm->getPatterns(), 1, 0, 0, 1, 0);
2020-06-04 23:08:52 -04:00
switch (_buttonType) {
case kCheckBox: {
Common::Rect c = Common::Rect(r.left, r.top + 2, r.left + 10, r.top + 2 + 10);
Graphics::drawRect(c, 0, _wm->getDrawPixel(), &pd);
2020-06-04 23:08:52 -04:00
break;
}
case kRound:
Graphics::drawRoundRect(r, 4, 0, false, _wm->getDrawPixel(), &pd);
2020-06-04 23:08:52 -04:00
break;
case kRadio:
Graphics::drawEllipse(r.left, r.top + 2, r.left + 11, r.top + 13, 0, false, _wm->getDrawPixel(), &pd);
2020-06-04 23:08:52 -04:00
break;
}
if (_invertOuter)
invertOuter();
if (_invertInner)
invertInner();
2020-06-04 23:08:52 -04:00
return true;
}
bool MacButton::draw(ManagedSurface *g, bool forceRedraw) {
if (!MacButton::draw(forceRedraw))
return false;
g->transBlitFrom(*_composeSurface, _composeSurface->getBounds(), Common::Point(_dims.left - 2, _dims.top - 2), _wm->_colorGreen2);
2020-06-04 23:08:52 -04:00
return true;
}
bool MacButton::processEvent(Common::Event &event) {
switch (event.type) {
case Common::EVENT_MOUSEMOVE:
if (_wm->_mouseDown) {
if (_wm->_mode & kWMModeButtonDialogStyle)
return true;
else if (!_dims.contains(_wm->_lastClickPos))
return false;
setActive(true);
}
2020-06-04 23:08:52 -04:00
break;
case Common::EVENT_LBUTTONDOWN:
setActive(true);
break;
case Common::EVENT_LBUTTONUP:
setActive(false);
_invertInner = !_invertInner;
2020-06-04 23:08:52 -04:00
break;
default:
warning("MacButton:: processEvent: Event not handled");
}
return false;
}
2020-06-13 22:43:59 -04:00
Common::Point MacButton::calculateOffset() {
return Common::Point(_alignOffset.x + _border + _gutter, _alignOffset.y + _border + _gutter/2);
}
2020-06-04 23:08:52 -04:00
} // End of namespace Graphics