From dea4668ae86e95b0be3f57a7e2a22797deeac740 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sun, 20 Oct 2013 00:34:48 +0200 Subject: [PATCH] Editable touch controls: Added Reset button and fixed build in VS 2010 --- UI/GameSettingsScreen.cpp | 2 +- UI/GamepadEmu.cpp | 2 +- UI/TouchControlLayoutScreen.cpp | 139 +++++++++++++++++++------------- UI/TouchControlLayoutScreen.h | 2 +- UI/UI.vcxproj | 4 +- UI/UI.vcxproj.filters | 8 +- 6 files changed, 98 insertions(+), 59 deletions(-) diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index a9bdbb1183..07913a7407 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -214,7 +214,7 @@ void GameSettingsScreen::CreateViews() { #endif controlsSettings->Add(new ItemHeader(c->T("OnScreen", "On-Screen Touch Controls"))); controlsSettings->Add(new CheckBox(&g_Config.bShowTouchControls, c->T("OnScreen", "On-Screen Touch Controls"))); - controlsSettings->Add(new Choice(c->T("Reposition on-screen Controls")))->OnClick.Handle(this, &GameSettingsScreen::OnTouchControlLayout); + controlsSettings->Add(new Choice(c->T("Custom layout...")))->OnClick.Handle(this, &GameSettingsScreen::OnTouchControlLayout); controlsSettings->Add(new PopupSliderChoice(&g_Config.iTouchButtonOpacity, 0, 100, c->T("Button Opacity"), screenManager())); controlsSettings->Add(new PopupSliderChoiceFloat(&g_Config.fButtonScale, 0.80, 2.0, c->T("Button Scaling"), screenManager())); controlsSettings->Add(new CheckBox(&g_Config.bShowAnalogStick, c->T("Show Left Analog Stick"))); diff --git a/UI/GamepadEmu.cpp b/UI/GamepadEmu.cpp index adc0447a3f..729b4d6159 100644 --- a/UI/GamepadEmu.cpp +++ b/UI/GamepadEmu.cpp @@ -269,7 +269,7 @@ void PSPStick::ProcessTouch(float x, float y, bool down) { } } -void InitPadLayout(){ +void InitPadLayout() { // TODO: See if we can make some kind of global scaling for views instead of this hackery. float scale = g_Config.fButtonScale; diff --git a/UI/TouchControlLayoutScreen.cpp b/UI/TouchControlLayoutScreen.cpp index 56c5191a24..e48bbbed8f 100644 --- a/UI/TouchControlLayoutScreen.cpp +++ b/UI/TouchControlLayoutScreen.cpp @@ -1,3 +1,22 @@ +// Copyright (c) 2013- PPSSPP Project. + +// 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, version 2.0 or later versions. + +// 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 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include + #include "TouchControlLayoutScreen.h" #include "Core/Config.h" #include "Core/System.h" @@ -10,31 +29,31 @@ static const int leftMargin = 140; - -//convert from screen coordinates (leftMargin to dp_xres) to actual fullscreen coordinates (0 to dp_xres) -int toFullscreenCoord(int screenx){ +// convert from screen coordinates (leftMargin to dp_xres) to actual fullscreen coordinates (0 to dp_xres) +static inline int toFullscreenCoord(int screenx) { return ((float)dp_xres / (dp_xres - leftMargin)) * (screenx - leftMargin); } -//convert from external fullscreen coordinates(0 to dp_xres) to the current partial coordinates (leftMargin to dp_xres) -int fromFullscreenCoord(int controllerX){ +// convert from external fullscreen coordinates(0 to dp_xres) to the current partial coordinates (leftMargin to dp_xres) +static inline int fromFullscreenCoord(int controllerX) { return leftMargin + ((dp_xres - leftMargin) / (float)dp_xres) * controllerX; }; -class DragDropButton : public MultiTouchButton{ +class DragDropButton : public MultiTouchButton { public: DragDropButton(int &x, int &y, int bgImg, int img, float scale) - : MultiTouchButton(bgImg, img, scale, new UI::AnchorLayoutParams(fromFullscreenCoord(x), y, UI::NONE, UI::NONE, true)), x_(x), y_(y){ + : MultiTouchButton(bgImg, img, scale, new UI::AnchorLayoutParams(fromFullscreenCoord(x), y, UI::NONE, UI::NONE, true)), + x_(x), y_(y) { scale_ = scale; } - virtual bool IsDown(){ + virtual bool IsDown() { //don't want the button to enlarge and throw the user's perspective //of button size off whack. return false; }; - void SavePosition(){ + void SavePosition() { x_ = toFullscreenCoord(bounds_.centerX()); y_ = bounds_.centerY(); } @@ -43,11 +62,10 @@ private: int &x_, &y_; }; - -class PSPActionButtons : public DragDropButton{ +class PSPActionButtons : public DragDropButton { public: PSPActionButtons(int &x, int &y, int actionButtonSpacing, float scale) - : DragDropButton(x, y, -1, -1, scale), actionButtonSpacing_(actionButtonSpacing){ + : DragDropButton(x, y, -1, -1, scale), actionButtonSpacing_(actionButtonSpacing) { using namespace UI; roundId_ = I_ROUND; @@ -57,10 +75,9 @@ public: squareId_ = I_SQUARE; }; - void Draw(UIContext &dc){ + void Draw(UIContext &dc) { float opacity = g_Config.iTouchButtonOpacity / 100.0f; - uint32_t colorBg = colorAlpha(0xc0b080, opacity); uint32_t color = colorAlpha(0xFFFFFF, opacity); @@ -88,6 +105,7 @@ public: //w += 2 * actionButtonSpacing_; //h += 2 * actionButtonSpacing_; }; + private: int roundId_; int circleId_, crossId_, triangleId_, squareId_; @@ -95,13 +113,13 @@ private: int actionButtonSpacing_; }; -class PSPDPadButtons : public DragDropButton{ +class PSPDPadButtons : public DragDropButton { public: PSPDPadButtons(int &x, int &y, int DpadRadius, float scale) - : DragDropButton(x, y, -1, -1, scale), DpadRadius_(DpadRadius){ + : DragDropButton(x, y, -1, -1, scale), DpadRadius_(DpadRadius) { } - void Draw(UIContext &dc){ + void Draw(UIContext &dc) { float opacity = g_Config.iTouchButtonOpacity / 100.0f; uint32_t colorBg = colorAlpha(0xc0b080, opacity); @@ -133,19 +151,16 @@ private: int DpadRadius_; }; - - -TouchControlLayoutScreen::TouchControlLayoutScreen(){ +TouchControlLayoutScreen::TouchControlLayoutScreen() { pickedControl_ = 0; }; -void TouchControlLayoutScreen::touch(const TouchInput &touch){ +void TouchControlLayoutScreen::touch(const TouchInput &touch) { UIScreen::touch(touch); using namespace UI; -//this is if - not else if as up and move can occur simultaneously - if ((touch.flags & TOUCH_MOVE) && pickedControl_ != 0){ + if ((touch.flags & TOUCH_MOVE) && pickedControl_ != 0) { const Bounds &bounds = pickedControl_->GetBounds(); int mintouchX = leftMargin + bounds.w * 0.5; @@ -157,37 +172,33 @@ void TouchControlLayoutScreen::touch(const TouchInput &touch){ int newX = bounds.centerX(), newY = bounds.centerY(); //we have to handle x and y separately since even if x is blocked, y may not be. - if(touch.x > mintouchX && touch.x < maxTouchX){ + if (touch.x > mintouchX && touch.x < maxTouchX) { //if the leftmost point of the control is ahead of the margin, //move it. Otherwise, don't. newX = touch.x; } - if(touch.y > minTouchY && touch.y < maxTouchY){ + if (touch.y > minTouchY && touch.y < maxTouchY) { newY = touch.y; } - ILOG("position: x = %d; y = %d", newX, newY); - pickedControl_->ReplaceLayoutParams(new UI::AnchorLayoutParams(newX, newY, NONE, NONE,true)); + // ILOG("position: x = %d; y = %d", newX, newY); + pickedControl_->ReplaceLayoutParams(new UI::AnchorLayoutParams(newX, newY, NONE, NONE, true)); } - - else if ((touch.flags & TOUCH_DOWN) && pickedControl_ == 0){ + if ((touch.flags & TOUCH_DOWN) && pickedControl_ == 0) { ILOG("->->->picked up") pickedControl_ = getPickedControl(touch.x, touch.y); } - - else if ((touch.flags & TOUCH_UP) && pickedControl_ != 0){ + if ((touch.flags & TOUCH_UP) && pickedControl_ != 0) { pickedControl_->SavePosition(); ILOG("->->->dropped down") pickedControl_ = 0; - } - - + } }; -UI::EventReturn TouchControlLayoutScreen::OnBack(UI::EventParams &e){ +UI::EventReturn TouchControlLayoutScreen::OnBack(UI::EventParams &e) { g_Config.Save(); - if(PSP_IsInited()) { + if (PSP_IsInited()) { screenManager()->finishDialog(this, DR_CANCEL); } else { screenManager()->finishDialog(this, DR_OK); @@ -196,13 +207,33 @@ UI::EventReturn TouchControlLayoutScreen::OnBack(UI::EventParams &e){ return UI::EVENT_DONE; }; - - - -void TouchControlLayoutScreen::CreateViews(){ -//setup g_Config for button layout +UI::EventReturn TouchControlLayoutScreen::OnReset(UI::EventParams &e) { + g_Config.iActionButtonSpacing = -1; + g_Config.iActionButtonCenterX = -1; + g_Config.iActionButtonCenterY = -1; + g_Config.iDpadRadius = -1; + g_Config.iDpadX = -1; + g_Config.iDpadY = -1; + g_Config.iStartKeyX = -1; + g_Config.iStartKeyY = -1; + g_Config.iSelectKeyX = -1; + g_Config.iSelectKeyY = -1; + g_Config.iUnthrottleKeyX = -1; + g_Config.iUnthrottleKeyY = -1; + g_Config.iLKeyX = -1; + g_Config.iLKeyY = -1; + g_Config.iRKeyX = -1; + g_Config.iRKeyY = -1; + g_Config.iAnalogStickX = -1; + g_Config.iAnalogStickY = -1; InitPadLayout(); + RecreateViews(); + return UI::EVENT_DONE; +}; +void TouchControlLayoutScreen::CreateViews() { + // setup g_Config for button layout + InitPadLayout(); using namespace UI; @@ -210,11 +241,13 @@ void TouchControlLayoutScreen::CreateViews(){ root_ = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT)); + Choice *reset = new Choice(d->T("Reset"), "", false, new AnchorLayoutParams(leftMargin, WRAP_CONTENT, 10, NONE, NONE, 84)); Choice *back = new Choice(d->T("Back"), "", false, new AnchorLayoutParams(leftMargin, WRAP_CONTENT, 10, NONE, NONE, 10)); + reset->OnClick.Handle(this, &TouchControlLayoutScreen::OnReset); back->OnClick.Handle(this, &TouchControlLayoutScreen::OnBack); + root_->Add(reset); root_->Add(back); - TabHolder *tabHolder = new TabHolder(ORIENT_VERTICAL, leftMargin, new AnchorLayoutParams(10, 0, 10, 0, false)); root_->Add(tabHolder); @@ -224,6 +257,7 @@ void TouchControlLayoutScreen::CreateViews(){ AnchorLayout *controlsHolder = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT)); float scale = g_Config.fButtonScale; + controls_.clear(); controls_.push_back(new PSPActionButtons(g_Config.iActionButtonCenterX, g_Config.iActionButtonCenterY, g_Config.iActionButtonSpacing, scale)); controls_.push_back(new PSPDPadButtons(g_Config.iDpadX, g_Config.iDpadY, g_Config.iDpadRadius, scale)); @@ -241,28 +275,25 @@ void TouchControlLayoutScreen::CreateViews(){ I18NCategory *c = GetI18NCategory("Controls"); tabHolder->AddTab(c->T("Controls"), controlsHolder); - - for(UI::View *control : controls_){ - root_->Add(control); + for (size_t i = 0; i < controls_.size(); i++) { + root_->Add(controls_[i]); } }; - - -//return the control which was picked up bu the touchEvent. If a control -//was already picked up, then it's being dragged around, so just return that instead -DragDropButton *TouchControlLayoutScreen::getPickedControl(const int x, const int y){ - - if(pickedControl_ != 0){ +// return the control which was picked up by the touchEvent. If a control +// was already picked up, then it's being dragged around, so just return that instead +DragDropButton *TouchControlLayoutScreen::getPickedControl(const int x, const int y) { + if (pickedControl_ != 0) { return pickedControl_; } - for(DragDropButton *control : controls_){ + for (size_t i = 0; i < controls_.size(); i++) { + DragDropButton *control = controls_[i]; const Bounds &bounds = control->GetBounds(); static const int thresholdFactor = 1.5; Bounds tolerantBounds(bounds.x, bounds.y, bounds.w * thresholdFactor, bounds.h * thresholdFactor); - if(tolerantBounds.Contains(x, y)){ + if (tolerantBounds.Contains(x, y)) { return control; } } diff --git a/UI/TouchControlLayoutScreen.h b/UI/TouchControlLayoutScreen.h index 514e152784..2f5a439b1e 100644 --- a/UI/TouchControlLayoutScreen.h +++ b/UI/TouchControlLayoutScreen.h @@ -26,7 +26,6 @@ class DragDropButton; class TouchControlLayoutScreen : public UIDialogScreenWithBackground{ public: - TouchControlLayoutScreen(); virtual void CreateViews(); @@ -34,6 +33,7 @@ public: protected: virtual UI::EventReturn OnBack(UI::EventParams &e); + virtual UI::EventReturn OnReset(UI::EventParams &e); private: DragDropButton *pickedControl_; diff --git a/UI/UI.vcxproj b/UI/UI.vcxproj index e36ee846a7..54e9afcd6e 100644 --- a/UI/UI.vcxproj +++ b/UI/UI.vcxproj @@ -31,6 +31,7 @@ + @@ -46,6 +47,7 @@ + @@ -178,4 +180,4 @@ - + \ No newline at end of file diff --git a/UI/UI.vcxproj.filters b/UI/UI.vcxproj.filters index c328d8b7a3..da68e560d8 100644 --- a/UI/UI.vcxproj.filters +++ b/UI/UI.vcxproj.filters @@ -31,6 +31,9 @@ Screens + + Screens + @@ -62,10 +65,13 @@ Screens + + Screens + {faee5dce-633b-4ba6-b19d-ea70ee3c1c38} - + \ No newline at end of file