Skip to content

Commit

Permalink
Prettied up the code, and added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Iraklis Rossis committed Nov 3, 2011
1 parent f6bd444 commit a1bd4d5
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 118 deletions.
43 changes: 28 additions & 15 deletions HTMLScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// Namespaces we want to access.
using namespace MAUtil; // Class Moblet
using namespace NativeUI; // WebView widget.
using namespace Wormhole; // Class WebAppMoblet
using namespace Wormhole; // Class WebViewMessage

HTMLScreen::HTMLScreen(OGLScreen *oglScreen):Screen()
{
Expand Down Expand Up @@ -41,27 +41,32 @@ HTMLScreen::~HTMLScreen()

void HTMLScreen::createUI()
{
//Increase flow button
mAddButton = new Button();
mAddButton->fillSpaceHorizontally();
mAddButton->wrapContentVertically();
mAddButton->setText("Increase");
mAddButton->addButtonListener(this);

//Decrease flow button
mRemoveButton = new Button();
mRemoveButton->fillSpaceHorizontally();
mRemoveButton->wrapContentVertically();
mRemoveButton->setText("Decrease");
mRemoveButton->addButtonListener(this);

//The layout that holds the buttons
HorizontalLayout* hLayout = new HorizontalLayout();
hLayout->wrapContentVertically();
hLayout->addChild(mAddButton);
hLayout->addChild(mRemoveButton);

//The webview that renders the animation
mWebView = new WebView();
mWebView->fillSpaceHorizontally();
mWebView->fillSpaceVertically();

//The layout that forms the screen
VerticalLayout* vLayout = new VerticalLayout();
vLayout->addChild(hLayout);
vLayout->addChild(mWebView);
Expand All @@ -71,18 +76,16 @@ void HTMLScreen::createUI()
setTitle("HTML Rendering");
}

/**
* This method handles messages sent from the WebView.
* @param webView The WebView that sent the message.
* @param urlData Data object that holds message content.
* Note that the data object will be valid only during
* the life-time of the call of this method, then it
* will be deallocated.
*/
void HTMLScreen::webViewHookInvoked(WebView* webView, int hookType, MAHandle urlData)
{
// Create message object. This parses the message.
WebViewMessage message(webView, urlData);

//This event tells us whether we should disable
//a button or not. This happens if we hit the
//maximum or minimum flow of particles
//We also notify the OpenGL screen because it also manages
//a set of buttons.
if (message.is("enableButtons"))
{
if(message.getParam("add") == "true")
Expand Down Expand Up @@ -112,27 +115,35 @@ void HTMLScreen::webViewHookInvoked(WebView* webView, int hookType, MAHandle url
}
}

//The parameters that define the behavior of the animation
if (message.is("initOGLVariables"))
{
//PRECISION is used to convert floating point numbers to integers,
//and then back again. This makes communication between JS and C++
//more efficient
PRECISION = message.getParamInt("PRECISION");

mOGLScreen->initVariables(
this,
this, //We also pass a reference to this screen
message.getParamInt("MAX_PARTICLES"),
message.getParamInt("PARTICLE_LIFETIME"),
message.getParamInt("GRAVITY_SCALE")/(float)1000,
message.getParamInt("GRAVITY_SCALE") / PRECISION,
message.getParamInt("SCREN_WIDTH"),
message.getParamInt("SCREEN_HEIGHT")
);
}

//This message is received whenever a new particle is generated
//by the JS code, and needs to be passed to OpenGL for rendering
if (message.is("newParticle"))
{
mOGLScreen->addNewParticles(
message.getParamInt("x"),
message.getParamInt("y"),
message.getParamInt("z"),
message.getParamInt("xv")/(float)1000,
-message.getParamInt("yv")/(float)1000,
message.getParamInt("zv")/(float)1000
message.getParamInt("xv") / PRECISION,
message.getParamInt("yv") / PRECISION,
message.getParamInt("zv") / PRECISION
);
}

Expand All @@ -143,6 +154,7 @@ void HTMLScreen::webViewHookInvoked(WebView* webView, int hookType, MAHandle url

void HTMLScreen::buttonClicked(Widget* button)
{
//Notify JS that the user has clicked a button
if(button == mAddButton){
mWebView->callJS("increaseFlow()");
}
Expand All @@ -158,14 +170,15 @@ WebView* HTMLScreen::getWebView()

void HTMLScreen::sensorEvent(MASensor a)
{
//We make a call to JS whenever we have new accelerometer data.
char buffer[128];
sprintf(buffer,"setGravity(%f, %f, %f)",a.values[0],a.values[1],a.values[2]);
mWebView->callJS(buffer);
}

void HTMLScreen::shouldRender(bool render)
{
mShouldRender = render;
//We need to notify JS whether it needs to start or stop rendering
if(render)
{
mWebView->callJS("shouldRender(true)");
Expand Down
42 changes: 36 additions & 6 deletions HTMLScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,64 @@ using namespace Wormhole; // Class WebAppMoblet

#include "OGLScreen.h"

class HTMLScreen : public Screen, public ButtonListener, public SensorListener, public WebViewListener
class HTMLScreen : public Screen,
public ButtonListener,
public SensorListener,
public WebViewListener
{
public:
HTMLScreen(OGLScreen *oglScreen);

~HTMLScreen();

/**
* This method creates the user interface of the screen
*/
void createUI();

/**
* This method handles messages sent from the WebView.
* @param webView The WebView that sent the message.
* @param urlData Data object that holds message content.
* Note that the data object will be valid only during
* the life-time of the call of this method, then it
* will be deallocated.
*/
virtual void webViewHookInvoked(WebView* webView, int hookType, MAHandle urlData);

/**
* This method handles button clicks that this screen manages.
* @param button The button that was clicked
*/
virtual void buttonClicked(Widget* button);

/**
* This method handles accelerometer events
* @param a A struct containing the accelerometer info
*/
virtual void sensorEvent(MASensor a);

/**
* Tell the screen whether it should render the animation or not
* @param render The state of rendering.
*/
void shouldRender(bool render);

/**
* This method returns the webview that this screen manages
* @param render The state of rendering.
*/
WebView* getWebView();
private:
Button* mAddButton;
Button* mAddButton; //Increases the flow of particles

Button* mRemoveButton;
Button* mRemoveButton; //Decreases the flow of particles

WebView* mWebView;
WebView* mWebView; //Renders the animation in Javascript

bool mShouldRender;
OGLScreen *mOGLScreen; //The OpenGL rendering screen

OGLScreen *mOGLScreen;
float PRECISION; //Precision of floating point numbers
};


Expand Down
Loading

0 comments on commit a1bd4d5

Please sign in to comment.