So, workable version of text behavior(to create simple menu)
MouseClick.h
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MouseClick.h
//
// Code : Anael Seghezzi & hog
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _MOUSE_CLICK_H
#define _MOUSE_CLICK_H
#include <MEngine.h>
class MouseClick : public MBehavior
{
public:
// constructors / destructors
MouseClick(MObject3d * parentObject);
MouseClick(MouseClick & behavior, MObject3d * parentObject);
~MouseClick(void);
private:
// custom variables
const char* m_cameraName;
const char* m_sceneName;
MVector4 m_colour;
const char* m_soundName;
const char* m_functionName;
bool m_play;
public:
// destroy
void destroy(void);
// get new
static MBehavior * getNew(MObject3d * parentObject);
// get copy
MBehavior * getCopy(MObject3d * parentObject);
// name
static const char * getStaticName(void){ return "MouseClick"; }
const char * getName(void){ return getStaticName(); }
// variables
unsigned int getVariablesNumber(void);
MVariable getVariable(unsigned int id);
// events (virtuals from MBehavior class)
void update(void);
//void onBeginScene(void);
void runEvent(int param){}
};
#endif
MouseClick.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MouseClick.cpp
//
// Code : Anael Seghezzi & hog
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "MouseClick.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init, this part is always similar, constructor, copy constructor etc
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// constructor
MouseClick::MouseClick(MObject3d * parentObject):
MBehavior(parentObject),
m_cameraName(""),
m_sceneName(""),
m_colour(1,1,1,1),
m_soundName(""),
m_functionName(""),
m_play(true)
{}
// copy constructor
MouseClick::MouseClick(MouseClick & behavior, MObject3d * parentObject):
MBehavior(parentObject),
m_cameraName(behavior.m_cameraName),
m_sceneName(behavior.m_sceneName),
m_colour(behavior.m_colour),
m_soundName(behavior.m_soundName),
m_functionName(behavior.m_functionName),
m_play(behavior.m_play)
{}
// destructor
MouseClick::~MouseClick(void)
{}
// destroy function : always similar
void MouseClick::destroy(void)
{
delete this;
}
// getNew function : always similar
MBehavior * MouseClick::getNew(MObject3d * parentObject)
{
return new MouseClick(parentObject);
}
// getCopy function : always similar
MBehavior * MouseClick::getCopy(MObject3d * parentObject)
{
return new MouseClick(*this, parentObject);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables, allow to access custom variable from script and from Maratis Editor
/////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int MouseClick::getVariablesNumber(void){
return 5;
}
MVariable MouseClick::getVariable(unsigned int id)
{
switch(id)
{
default:
return MVariable("NULL", NULL, M_VARIABLE_NULL);
case 0:
return MVariable("cameraName", &m_cameraName, M_VARIABLE_STRING);
case 1:
return MVariable("sceneName", &m_sceneName, M_VARIABLE_STRING);
case 2:
return MVariable("colour", &m_colour, M_VARIABLE_VEC4);
case 3:
return MVariable("soundName", &m_soundName, M_VARIABLE_STRING);
case 4:
return MVariable("functionName", &m_functionName, M_VARIABLE_STRING);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// update function (called by MGame class by default)
void MouseClick::update(void)
{
MEngine * engine = MEngine::getInstance();//engine
MGame * game = engine->getGame();//game
MSystemContext * system = engine->getSystemContext();
MInputContext * input=engine->getInputContext();
if( !game->isRunning())
return;
MObject3d * parent = getParentObject();
MLevel * SampleLevel=engine->getLevel();//level
MScene * SampleScene = SampleLevel->getSceneByName(m_sceneName);//scene
MOText* SampleText = SampleScene->getTextByName(parent->getName());//text
MOCamera* SampleCamera=SampleScene->getCameraByName(m_cameraName);//camera
MOSound * SampleSound=NULL;
SampleSound=SampleScene->getSoundByName(m_soundName);
MScriptContext * SampleScript=engine->getScriptContext();
MBox3d* bbox = SampleText->getBoundingBox();
unsigned int width=0;
unsigned int height=0;
system->getScreenSize(&width, &height);
float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");
MVector3 origin = SampleCamera->getTransformedPosition();
MVector3 dest = SampleCamera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));
MVector3 localOrigin = SampleText->getInversePosition(origin);
MVector3 localDest = SampleText->getInversePosition(dest);
if(isEdgeToBoxCollision(localOrigin, localDest,bbox->min,bbox->max)){
SampleText->setColor(m_colour);
if(SampleSound)
if(m_play){
SampleSound->play();
m_play=false;
}
if(input->onKeyDown("MOUSE_BUTTON1"))
//if(SampleScript)
SampleScript->callFunction(m_functionName);
}
else
{
SampleText->setColor(MVector4(0.8,0.8,0.8,0.6));
m_play=true;
}
}
Behavior properties:
1)cameraName - camera that captures text
2) sceneName - scene, where texts/sounds take place
3)colour - text highlighting(r,y,b,a), when you put mouse on it
4)soundName - sound, when you put mouse on text
5)functionName - the function that would be called, when we press on text (must be in you scene script)
example:
...
function funcName()
...
end
you can change the default text colour by editing
SampleText->setColor(MVector4(0.8,0.8,0.8,0.6));
video
http://youtu.be/_l2oAbihUX4