1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-10-04 02:23:15 +03:00
gmenu2x/src/link.cpp
Maarten ter Huurne 742444c919 Removed Button class
It was only used to share implementation between IconButton and Link.
However, there was only one non-trivial method, handleTS(), and that
method used a different code path for each use case: doubleClick was
always false for IconButton and always true for Link. So the total
amount of code was actually reduced by eliminating this code sharing.

The main motivation for this split is that I can now freely refactor
Link without having to worry about IconButton.
2013-08-14 04:26:04 +02:00

165 lines
4.3 KiB
C++

/***************************************************************************
* Copyright (C) 2006 by Massimiliano Torromeo *
* massimiliano.torromeo@gmail.com *
* *
* 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., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "link.h"
#include "gmenu2x.h"
#include "menu.h"
#include "selector.h"
#include "surface.h"
#include "utilities.h"
#include <fstream>
#include <sstream>
using namespace std;
Link::Link(GMenu2X *gmenu2x, Touchscreen &ts, function_t action)
: gmenu2x(gmenu2x)
, ts(ts)
, action(action)
, rect((SDL_Rect) { 0, 0, 0, 0 })
, lastTick(0)
{
edited = false;
iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
iconX = 0;
padding = 0;
updateSurfaces();
}
bool Link::isPressed() {
return ts.pressed() && ts.inRect(rect);
}
bool Link::handleTS() {
if (ts.released() && ts.inRect(rect)) {
int tickNow = SDL_GetTicks();
if (tickNow - lastTick < 400) {
ts.setHandled();
action();
}
lastTick = tickNow;
return true;
}
return false;
}
void Link::paint() {
if (iconSurface) {
iconSurface->blit(gmenu2x->s, iconX, rect.y+padding, 32,32);
}
gmenu2x->s->write(gmenu2x->font, getTitle(), iconX+16, rect.y + gmenu2x->skinConfInt["linkHeight"]-padding, Font::HAlignCenter, Font::VAlignBottom);
}
void Link::paintHover() {
if (gmenu2x->useSelectionPng)
gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s, rect, Font::HAlignCenter, Font::VAlignMiddle);
else
gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
}
void Link::updateSurfaces()
{
iconSurface = gmenu2x->sc[getIconPath()];
}
const string &Link::getTitle() {
return title;
}
void Link::setTitle(const string &title) {
this->title = title;
edited = true;
}
const string &Link::getDescription() {
return description;
}
void Link::setDescription(const string &description) {
this->description = description;
edited = true;
}
const string &Link::getIcon() {
return icon;
}
void Link::loadIcon() {
if (icon.compare(0, 5, "skin:") == 0) {
setIconPath(gmenu2x->sc.getSkinFilePath(icon.substr(5, string::npos)));
}
}
void Link::setIcon(const string &icon) {
this->icon = icon;
if (icon.compare(0, 5, "skin:") == 0)
this->iconPath = gmenu2x->sc.getSkinFilePath(
icon.substr(5, string::npos));
else
this->iconPath = icon;
edited = true;
updateSurfaces();
}
const string &Link::searchIcon() {
iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
return iconPath;
}
const string &Link::getIconPath() {
if (iconPath.empty()) searchIcon();
return iconPath;
}
void Link::setIconPath(const string &icon) {
if (fileExists(icon))
iconPath = icon;
else
iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
updateSurfaces();
}
void Link::setSize(int w, int h) {
rect.w = w;
rect.h = h;
recalcCoordinates();
}
void Link::setPosition(int x, int y) {
rect.x = x;
rect.y = y;
recalcCoordinates();
}
void Link::recalcCoordinates() {
iconX = rect.x+(rect.w-32)/2;
padding = (gmenu2x->skinConfInt["linkHeight"] - 32 - gmenu2x->font->getHeight()) / 3;
}
void Link::run() {
this->action();
}