Root/src/link.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include "link.h"
22
23#include "gmenu2x.h"
24#include "menu.h"
25#include "selector.h"
26#include "surface.h"
27#include "utilities.h"
28
29#include <fstream>
30#include <sstream>
31
32using namespace std;
33
34
35Link::Link(GMenu2X *gmenu2x, function_t action)
36    : gmenu2x(gmenu2x)
37    , ts(gmenu2x->getTouchscreen())
38    , action(action)
39    , lastTick(0)
40{
41// ts = gmenu2x->getTouchscreen();
42    rect.w = gmenu2x->skinConfInt["linkWidth"];
43    rect.h = gmenu2x->skinConfInt["linkHeight"];
44    edited = false;
45    iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
46    iconX = 0;
47    padding = 0;
48
49    updateSurfaces();
50}
51
52bool Link::isPressed() {
53    return ts.pressed() && ts.inRect(rect);
54}
55
56bool Link::handleTS() {
57    if (ts.released() && ts.inRect(rect)) {
58        int tickNow = SDL_GetTicks();
59        if (tickNow - lastTick < 400) {
60            ts.setHandled();
61            action();
62        }
63        lastTick = tickNow;
64        return true;
65    }
66    return false;
67}
68
69void Link::paint() {
70    if (iconSurface) {
71        iconSurface->blit(gmenu2x->s, iconX, rect.y+padding, 32,32);
72    }
73    gmenu2x->s->write(gmenu2x->font, getTitle(), iconX+16, rect.y + gmenu2x->skinConfInt["linkHeight"]-padding, Font::HAlignCenter, Font::VAlignBottom);
74}
75
76void Link::paintHover() {
77    if (gmenu2x->useSelectionPng)
78        gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s, rect, Font::HAlignCenter, Font::VAlignMiddle);
79    else
80        gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
81}
82
83void Link::updateSurfaces()
84{
85    iconSurface = gmenu2x->sc[getIconPath()];
86}
87
88const string &Link::getTitle() {
89    return title;
90}
91
92void Link::setTitle(const string &title) {
93    this->title = title;
94    edited = true;
95}
96
97const string &Link::getDescription() {
98    return description;
99}
100
101void Link::setDescription(const string &description) {
102    this->description = description;
103    edited = true;
104}
105
106const string &Link::getIcon() {
107    return icon;
108}
109
110void Link::loadIcon() {
111    if (icon.compare(0, 5, "skin:") == 0) {
112        setIconPath(gmenu2x->sc.getSkinFilePath(icon.substr(5, string::npos)));
113    }
114}
115
116void Link::setIcon(const string &icon) {
117    this->icon = icon;
118
119    if (icon.compare(0, 5, "skin:") == 0)
120        this->iconPath = gmenu2x->sc.getSkinFilePath(
121                    icon.substr(5, string::npos));
122    else
123        this->iconPath = icon;
124
125    edited = true;
126    updateSurfaces();
127}
128
129const string &Link::searchIcon() {
130    iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
131    return iconPath;
132}
133
134const string &Link::getIconPath() {
135    if (iconPath.empty()) searchIcon();
136    return iconPath;
137}
138
139void Link::setIconPath(const string &icon) {
140    if (fileExists(icon))
141        iconPath = icon;
142    else
143        iconPath = gmenu2x->sc.getSkinFilePath("icons/generic.png");
144    updateSurfaces();
145}
146
147void Link::setSize(int w, int h) {
148    rect.w = w;
149    rect.h = h;
150    recalcCoordinates();
151}
152
153void Link::setPosition(int x, int y) {
154    rect.x = x;
155    rect.y = y;
156    recalcCoordinates();
157}
158
159void Link::recalcCoordinates() {
160    iconX = rect.x+(rect.w-32)/2;
161    padding = (gmenu2x->skinConfInt["linkHeight"] - 32 - gmenu2x->font->getHeight()) / 3;
162}
163
164void Link::run() {
165    this->action();
166}
167

Archive Download this file



interactive