Last active
June 9, 2017 13:26
-
-
Save stoffera/39ae45b2101105863b50c94396495504 to your computer and use it in GitHub Desktop.
Mono: Swipe between Pages (Scenes) App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "app_controller.h" | |
#include <Fonts/FreeSerif24pt7b.h> | |
#include <Fonts/FreeSans9pt7b.h> | |
using namespace mono::geo; | |
AppController::AppController() : | |
helloLabel(Rect(0,110-35,176,70), "Slide Left\nor right"), | |
scnOne(Rect(10,10,156,20), "Scene One"), | |
scnTwo(Rect(10,10,156,20), "Scene Two") | |
{ | |
// Attach callback methods to gesture detctor | |
gesture.slideLeft.attach(this, &AppController::pageUp); | |
gesture.slideRight.attach(this, &AppController::pageDown); | |
gesture.Activate(); | |
// Setup text label | |
helloLabel.setAlignment(TextLabelView::ALIGN_CENTER); | |
helloLabel.setText(display::TurquoiseColor); | |
scnOne.setFont(FreeSans9pt7b); | |
scnTwo.setFont(FreeSans9pt7b); | |
scnOne.setAlignment(TextLabelView::ALIGN_CENTER); | |
scnTwo.setAlignment(TextLabelView::ALIGN_CENTER); | |
// add the text labels to both scenes | |
sceneOne.addView(helloLabel); | |
sceneTwo.addView(helloLabel); | |
sceneOne.addView(scnOne); | |
sceneTwo.addView(scnTwo); | |
// Set the scene dismiss callback functions | |
sceneOne.setDismissCallback(this, &AppController::sceneSegueOne); | |
sceneTwo.setDismissCallback(this, &AppController::sceneSegueTwo); | |
pageCount = 1; | |
} | |
void AppController::pageUp() | |
{ | |
pageCount++; | |
changeScene(); | |
} | |
void AppController::pageDown() | |
{ | |
if (pageCount == 1) | |
return; | |
pageCount--; | |
changeScene(); | |
} | |
void AppController::changeScene() | |
{ | |
helloLabel.setFont(FreeSerif24pt7b); | |
if (sceneOne.Visible()) | |
sceneOne.requestDismiss(); | |
else if (sceneTwo.Visible()) | |
sceneTwo.requestDismiss(); | |
} | |
void AppController::sceneSegueOne() | |
{ | |
helloLabel.setText(String::Format("%i", pageCount)); | |
sceneOne.hide(); | |
sceneTwo.show(); | |
} | |
void AppController::sceneSegueTwo() | |
{ | |
helloLabel.setText(String::Format("%i", pageCount)); | |
sceneTwo.hide(); | |
sceneOne.show(); | |
} | |
void AppController::monoWakeFromReset() | |
{ | |
sceneOne.show(); | |
} | |
void AppController::monoWillGotoSleep() | |
{ | |
} | |
void AppController::monoWakeFromSleep() | |
{ | |
if (sceneOne.Visible()) | |
sceneOne.scheduleRepaint(); | |
else if (sceneTwo.Visible()) | |
sceneTwo.scheduleRepaint(); | |
saver.undim(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This App presents a page (scene) with a number on. Swipe left to decrement, right to increment. | |
* This is an example of how to use 2 SceneControllers to change between pages. | |
*/ | |
#ifndef app_controller_h | |
#define app_controller_h | |
#include <mono.h> | |
#include "gesture_detect.hpp" | |
using namespace mono; | |
using namespace mono::ui; | |
class AppController : public mono::IApplication { | |
TextLabelView helloLabel, scnOne, scnTwo; | |
SceneController sceneOne, sceneTwo; // the two scenes to switch between | |
int pageCount; | |
PowerSaver saver; // auto go-to sleep handler | |
GestureDetect gesture; // gesture (swipe) recognizer object | |
public: | |
AppController(); | |
void pageUp(); // increment page count | |
void pageDown(); // decrement | |
void changeScene(); // trigger dismiss on the visible scene | |
void sceneSegueOne(); // update text label and present scene one | |
void sceneSegueTwo(); // same as above with scene two | |
void monoWakeFromReset(); | |
void monoWillGotoSleep(); | |
void monoWakeFromSleep(); | |
}; | |
#endif /* app_controller_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "gesture_detect.hpp" | |
GestureDetect::GestureDetect() | |
{ | |
gestureInProgress = false; | |
} | |
void GestureDetect::RespondTouchBegin(mono::TouchEvent &event) | |
{ | |
if (event.handled) | |
return; | |
mono::TouchCalibration cali = mono::IApplicationContext::Instance->TouchSystem->CurrentCalibration(); | |
int maxX = event.IsScreenCoords ? mono::ui::View::DisplayWidth() : cali.Width(); | |
beginPos = event.Position; | |
if (event.Position.X() > maxX/2) { | |
Begin = RIGHT; | |
} | |
else { | |
Begin = LEFT; | |
} | |
} | |
void GestureDetect::RespondTouchEnd(mono::TouchEvent &event) | |
{ | |
if (event.handled) | |
return; | |
mono::TouchCalibration cali = mono::IApplicationContext::Instance->TouchSystem->CurrentCalibration(); | |
int maxX = event.IsScreenCoords ? mono::ui::View::DisplayWidth() : cali.Width(); | |
endPos = event.Position; | |
if (event.Position.X() > maxX/2) { | |
End = RIGHT; | |
} | |
else { | |
End = LEFT; | |
} | |
processMove(event.IsScreenCoords); | |
} | |
void GestureDetect::processMove(bool isScreenCoords) | |
{ | |
mono::geo::Point diff = endPos - beginPos; | |
mono::TouchCalibration cali = mono::IApplicationContext::Instance->TouchSystem->CurrentCalibration(); | |
int maxVal = isScreenCoords ? mono::ui::View::DisplayWidth() : cali.Width(); | |
if (diff.Abs() < (uint32_t)(maxVal / 2)) | |
{ | |
return; | |
} | |
if (Begin == LEFT && End == RIGHT) | |
slideRight.call(); | |
else if (Begin == RIGHT && End == LEFT) | |
slideLeft.call(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef gesture_detect_hpp | |
#define gesture_detect_hpp | |
#include <mono.h> | |
#include <FunctionPointer.h> | |
class GestureDetect : public mono::TouchResponder { | |
enum Positions { | |
LEFT, | |
RIGHT, | |
TOP, | |
BOTTOM | |
}; | |
bool gestureInProgress; | |
Positions Begin, End; | |
mono::geo::Point beginPos, endPos; | |
void processMove(bool isScreenCoords); | |
public: | |
mbed::FunctionPointer slideLeft, slideRight; | |
GestureDetect(); | |
void RespondTouchBegin(mono::TouchEvent &event); | |
void RespondTouchEnd(mono::TouchEvent &event); | |
}; | |
#endif /* gesture_detect_hpp */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment