1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-22 12:36:17 +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::string;
using std::unique_ptr; using std::unique_ptr;
using std::move;
BrowseDialog::BrowseDialog( BrowseDialog::BrowseDialog(
GMenu2X *gmenu2x, Touchscreen &ts_, GMenu2X *gmenu2x, Touchscreen &ts_,
@ -19,26 +18,27 @@ BrowseDialog::BrowseDialog(
, subtitle(subtitle) , subtitle(subtitle)
, ts_pressed(false) , ts_pressed(false)
{ {
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png"))); buttonBox.add(unique_ptr<IconButton>(new IconButton(
unique_ptr<IconButton> btnUp(new IconButton( gmenu2x, ts, "skin:imgs/buttons/left.png")));
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Up one folder"])); buttonBox.add(unique_ptr<IconButton>(new IconButton(
btnUp->setAction(BIND(&BrowseDialog::directoryUp)); gmenu2x, ts, "skin:imgs/buttons/cancel.png",
buttonBox.add(move(btnUp)); gmenu2x->tr["Up one folder"],
BIND(&BrowseDialog::directoryUp))));
unique_ptr<IconButton> btnSelect(new IconButton( buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"])); gmenu2x, ts, "skin:imgs/buttons/accept.png",
btnSelect->setAction(BIND(&BrowseDialog::directoryEnter)); gmenu2x->tr["Select"],
buttonBox.add(move(btnSelect)); BIND(&BrowseDialog::directoryEnter))));
unique_ptr<IconButton> btnConfirm(new IconButton( buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"])); gmenu2x, ts, "skin:imgs/buttons/start.png",
btnConfirm->setAction(BIND(&BrowseDialog::confirm)); gmenu2x->tr["Confirm"],
buttonBox.add(move(btnConfirm)); BIND(&BrowseDialog::confirm))));
unique_ptr<IconButton> btnExit(new IconButton( buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"])); gmenu2x, ts, "skin:imgs/buttons/select.png",
btnExit->setAction(BIND(&BrowseDialog::quit)); gmenu2x->tr["Exit"],
buttonBox.add(move(btnExit)); BIND(&BrowseDialog::quit))));
iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png"); iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
iconFolder = gmenu2x->sc.skinRes("imgs/folder.png"); iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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