1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-12-26 05:32:25 +02:00

Pass action to IconButton constructor

This removes the need for a separate setAction method.

The default action is the empty action, which does nothing. However,
a touch event on a button with the empty action is no longer considered
handled.

Menu::btnContextMenu was changed from a unique_ptr to a plain data
member.
This commit is contained in:
Maarten ter Huurne 2014-08-16 06:35:27 +02:00
parent 428a316bc1
commit 7e308879c1
13 changed files with 93 additions and 113 deletions

View File

@ -8,7 +8,6 @@
using std::string;
using std::unique_ptr;
using std::move;
BrowseDialog::BrowseDialog(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -19,26 +18,27 @@ BrowseDialog::BrowseDialog(
, subtitle(subtitle)
, ts_pressed(false)
{
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png")));
unique_ptr<IconButton> btnUp(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Up one folder"]));
btnUp->setAction(BIND(&BrowseDialog::directoryUp));
buttonBox.add(move(btnUp));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png")));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Up one folder"],
BIND(&BrowseDialog::directoryUp))));
unique_ptr<IconButton> btnSelect(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btnSelect->setAction(BIND(&BrowseDialog::directoryEnter));
buttonBox.add(move(btnSelect));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Select"],
BIND(&BrowseDialog::directoryEnter))));
unique_ptr<IconButton> btnConfirm(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]));
btnConfirm->setAction(BIND(&BrowseDialog::confirm));
buttonBox.add(move(btnConfirm));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/start.png",
gmenu2x->tr["Confirm"],
BIND(&BrowseDialog::confirm))));
unique_ptr<IconButton> btnExit(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"]));
btnExit->setAction(BIND(&BrowseDialog::quit));
buttonBox.add(move(btnExit));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/select.png",
gmenu2x->tr["Exit"],
BIND(&BrowseDialog::quit))));
iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");

View File

@ -9,22 +9,18 @@ using namespace std;
IconButton::IconButton(
GMenu2X *gmenu2x, Touchscreen &ts,
const string &icon, const string &label)
const string &icon, const string &label, function_t action)
: gmenu2x(gmenu2x)
, ts(ts)
, icon(icon)
, label(label)
, action([] {})
, action(action)
, rect({ 0, 0, 0, 0 })
{
iconSurface = gmenu2x->sc[icon];
recalcRects();
}
void IconButton::setAction(function_t action) {
this->action = action;
}
void IconButton::setPosition(int x, int y) {
if (rect.x != x || rect.y != y) {
rect.x = x;
@ -57,7 +53,7 @@ void IconButton::recalcRects() {
}
bool IconButton::handleTS() {
if (ts.released() && ts.inRect(rect)) {
if (action && ts.released() && ts.inRect(rect)) {
ts.setHandled();
action();
return true;

View File

@ -15,9 +15,8 @@ class Touchscreen;
class IconButton {
public:
IconButton(GMenu2X *gmenu2x, Touchscreen &ts,
const std::string &icon, const std::string &label = "");
void setAction(function_t action);
const std::string &icon, const std::string &label = "",
function_t action = nullptr);
SDL_Rect getRect() { return rect; }
void setPosition(int x, int y);

View File

@ -97,25 +97,25 @@ InputDialog::InputDialog(GMenu2X *gmenu2x, InputManager &inputMgr_,
setKeyboard(0);
unique_ptr<IconButton> btnBackspace(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]));
btnBackspace->setAction(BIND(&InputDialog::backspace));
buttonbox.add(move(btnBackspace));
buttonbox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/l.png",
gmenu2x->tr["Backspace"],
BIND(&InputDialog::backspace))));
unique_ptr<IconButton> btnSpace(
new IconButton(gmenu2x, ts, "skin:imgs/buttons/r.png", gmenu2x->tr["Space"]));
btnSpace->setAction(BIND(&InputDialog::space));
buttonbox.add(move(btnSpace));
buttonbox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/r.png",
gmenu2x->tr["Space"],
BIND(&InputDialog::space))));
unique_ptr<IconButton> btnConfirm(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]));
btnConfirm->setAction(BIND(&InputDialog::confirm));
buttonbox.add(move(btnConfirm));
buttonbox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Confirm"],
BIND(&InputDialog::confirm))));
unique_ptr<IconButton> btnChangeKeys(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Change keys"]));
btnChangeKeys->setAction(BIND(&InputDialog::changeKeys));
buttonbox.add(move(btnChangeKeys));
buttonbox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Change keys"],
BIND(&InputDialog::changeKeys))));
}
void InputDialog::setKeyboard(int kb) {

View File

@ -40,7 +40,6 @@
#include "filelister.h"
#include "utilities.h"
#include "debug.h"
#include "iconbutton.h"
using namespace std;
@ -71,7 +70,8 @@ void Menu::Animation::step()
Menu::Menu(GMenu2X *gmenu2x, Touchscreen &ts)
: gmenu2x(gmenu2x)
, ts(ts)
, btnContextMenu(new IconButton(gmenu2x, ts, "skin:imgs/menu.png"))
, btnContextMenu(gmenu2x, ts, "skin:imgs/menu.png", "",
std::bind(&GMenu2X::showContextMenu, gmenu2x))
{
readSections(GMENU2X_SYSTEM_DIR "/sections");
readSections(GMenu2X::getHome() + "/sections");
@ -100,8 +100,7 @@ Menu::Menu(GMenu2X *gmenu2x, Touchscreen &ts)
}
#endif
btnContextMenu->setPosition(gmenu2x->resX - 38, gmenu2x->bottomBarIconY);
btnContextMenu->setAction(std::bind(&GMenu2X::showContextMenu, gmenu2x));
btnContextMenu.setPosition(gmenu2x->resX - 38, gmenu2x->bottomBarIconY);
}
Menu::~Menu() {
@ -265,7 +264,7 @@ void Menu::paint(Surface &s) {
}
if (ts.available()) {
btnContextMenu->paint(s);
btnContextMenu.paint(s);
}
}
@ -301,7 +300,7 @@ bool Menu::handleButtonPress(InputManager::Button button) {
}
bool Menu::handleTouchscreen(Touchscreen &ts) {
btnContextMenu->handleTS();
btnContextMenu.handleTS();
ConfIntHash &skinConfInt = gmenu2x->skinConfInt;
const int topBarHeight = skinConfInt["topBarHeight"];

View File

@ -22,6 +22,7 @@
#define MENU_H
#include "delegate.h"
#include "iconbutton.h"
#include "layer.h"
#include "link.h"
@ -55,7 +56,7 @@ private:
GMenu2X *gmenu2x;
Touchscreen &ts;
std::unique_ptr<IconButton> btnContextMenu;
IconButton btnContextMenu;
int iSection, iLink;
uint iFirstDispRow;
std::vector<std::string> sections;

View File

@ -30,7 +30,6 @@
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingBool::MenuSettingBool(
GMenu2X *gmenu2x, Touchscreen &ts,
@ -60,10 +59,10 @@ MenuSettingBool::MenuSettingBool(
void MenuSettingBool::initButton()
{
unique_ptr<IconButton> btn(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Switch"]));
btn->setAction(BIND(&MenuSettingBool::toggle));
buttonBox.add(move(btn));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Switch"],
BIND(&MenuSettingBool::toggle))));
}
void MenuSettingBool::draw(int valueX, int y, int h)

View File

@ -27,7 +27,6 @@
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingDir::MenuSettingDir(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -35,15 +34,15 @@ MenuSettingDir::MenuSettingDir(
: MenuSettingStringBase(gmenu2x, name, description, value)
, ts(ts_)
{
unique_ptr<IconButton> btnCancel(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]));
btnCancel->setAction(BIND(&MenuSettingDir::clear));
buttonBox.add(move(btnCancel));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Clear"],
BIND(&MenuSettingDir::clear))));
unique_ptr<IconButton> btnAccept(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btnAccept->setAction(BIND(&MenuSettingDir::edit));
buttonBox.add(move(btnAccept));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Select"],
BIND(&MenuSettingDir::edit))));
}
void MenuSettingDir::edit()

View File

@ -27,7 +27,6 @@
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingFile::MenuSettingFile(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -37,15 +36,15 @@ MenuSettingFile::MenuSettingFile(
, ts(ts_)
, filter(filter_)
{
unique_ptr<IconButton> btnClear(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]));
btnClear->setAction(BIND(&MenuSettingFile::clear));
buttonBox.add(move(btnClear));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Clear"],
BIND(&MenuSettingFile::clear))));
unique_ptr<IconButton> btnSelect(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btnSelect->setAction(BIND(&MenuSettingFile::edit));
buttonBox.add(move(btnSelect));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Select"],
BIND(&MenuSettingFile::edit))));
}
void MenuSettingFile::edit()

View File

@ -31,7 +31,6 @@
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::move;
MenuSettingInt::MenuSettingInt(
GMenu2X *gmenu2x, Touchscreen &ts,
@ -50,25 +49,19 @@ MenuSettingInt::MenuSettingInt(
function_t actionInc = BIND(&MenuSettingInt::inc);
function_t actionDec = BIND(&MenuSettingInt::dec);
unique_ptr<IconButton> btnL1(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/l.png"));
btnL1->setAction(actionDec);
buttonBox.add(move(btnL1));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/l.png",
"", actionDec)));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png",
gmenu2x->tr["Decrease"], actionDec)));
unique_ptr<IconButton> btnL2(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png", gmenu2x->tr["Decrease"]));
btnL2->setAction(actionDec);
buttonBox.add(move(btnL2));
unique_ptr<IconButton> btnR1(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/r.png"));
btnR1->setAction(actionInc);
buttonBox.add(move(btnR1));
unique_ptr<IconButton> btnR2(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Increase"]));
btnR2->setAction(actionInc);
buttonBox.add(move(btnR2));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/r.png",
"", actionInc)));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/right.png",
gmenu2x->tr["Increase"], actionInc)));
}
void MenuSettingInt::draw(int valueX, int y, int h)

View File

@ -29,7 +29,6 @@ using std::find;
using std::string;
using std::vector;
using std::unique_ptr;
using std::move;
MenuSettingMultiString::MenuSettingMultiString(
GMenu2X *gmenu2x, Touchscreen &ts,
@ -40,15 +39,13 @@ MenuSettingMultiString::MenuSettingMultiString(
{
setSel(find(choices->begin(), choices->end(), *value) - choices->begin());
unique_ptr<IconButton> btnDec(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png"));
btnDec->setAction(BIND(&MenuSettingMultiString::decSel));
buttonBox.add(move(btnDec));
unique_ptr<IconButton> btnInc(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Change value"]));
btnInc->setAction(BIND(&MenuSettingMultiString::incSel));
buttonBox.add(move(btnInc));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png", "",
BIND(&MenuSettingMultiString::decSel))));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/right.png",
gmenu2x->tr["Change value"],
BIND(&MenuSettingMultiString::incSel))));
}
bool MenuSettingMultiString::handleButtonPress(InputManager::Button button)

View File

@ -30,7 +30,6 @@
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::move;
constexpr unsigned int COMPONENT_WIDTH = 28;

View File

@ -27,7 +27,6 @@
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingString::MenuSettingString(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -38,15 +37,15 @@ MenuSettingString::MenuSettingString(
, diagTitle(diagTitle_)
, diagIcon(diagIcon_)
{
unique_ptr<IconButton> btnClear(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]));
btnClear->setAction(BIND(&MenuSettingString::clear));
buttonBox.add(move(btnClear));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Clear"],
BIND(&MenuSettingString::clear))));
unique_ptr<IconButton> btnEdit(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Edit"]));
btnEdit->setAction(BIND(&MenuSettingString::edit));
buttonBox.add(move(btnEdit));
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Edit"],
BIND(&MenuSettingString::edit))));
}
void MenuSettingString::edit()