Files
GDevelop/Extensions/TextEntryObject/TextEntryObject.cpp
T
2015-01-08 21:58:36 +01:00

119 lines
3.3 KiB
C++

/**
GDevelop - TextEntry Object Extension
Copyright (c) 2011-2014 Florian Rival (Florian.Rival@gmail.com)
This project is released under the MIT License.
*/
#include <SFML/Graphics.hpp>
#include <string>
#include "GDCpp/Object.h"
#include "GDCpp/ImageManager.h"
#include "GDCpp/Serialization/SerializerElement.h"
#include "GDCpp/Position.h"
#include "GDCpp/Project.h"
#include "GDCpp/RuntimeScene.h"
#include "TextEntryObject.h"
#if defined(GD_IDE_ONLY)
#include <wx/bitmap.h>
#include "GDCore/IDE/ArbitraryResourceWorker.h"
#include "GDCpp/CommonTools.h"
#include "GDCore/IDE/Dialogs/MainFrameWrapper.h"
#endif
using namespace std;
#if defined(GD_IDE_ONLY)
sf::Texture TextEntryObject::edittimeIconImage;
sf::Sprite TextEntryObject::edittimeIcon;
#endif
TextEntryObject::TextEntryObject(std::string name_) :
Object(name_)
{
}
RuntimeTextEntryObject::RuntimeTextEntryObject(RuntimeScene & scene_, const gd::Object & object) :
RuntimeObject(scene_, object),
scene(&scene_),
activated(true)
{
}
/**
* \brief Used to update input
*/
void RuntimeTextEntryObject::UpdateTime(float)
{
if (!activated || scene == NULL) return;
//Retrieve text entered
const std::vector<sf::Event> & events = scene->GetRenderTargetEvents();
for (unsigned int i = 0;i<events.size();++i)
{
if (events[i].type == sf::Event::TextEntered )
{
//Skip some non displayable characters
if (events[i].text.unicode > 30 && (events[i].text.unicode < 127 || events[i].text.unicode > 159))
text += events[i].text.unicode;
else if (events[i].text.unicode == 8)
{
//Backspace
if ( !text.empty() ) text.erase(text.end()-1);
}
}
}
}
#if defined(GD_IDE_ONLY)
void TextEntryObject::DrawInitialInstance(gd::InitialInstance & instance, sf::RenderTarget & renderTarget, gd::Project & project, gd::Layout & layout)
{
edittimeIcon.setPosition(instance.GetX(), instance.GetY());
renderTarget.draw(edittimeIcon);
}
void TextEntryObject::LoadEdittimeIcon()
{
edittimeIconImage.loadFromFile("CppPlatform/Extensions/textentry.png");
edittimeIcon.setTexture(edittimeIconImage);
}
bool TextEntryObject::GenerateThumbnail(const gd::Project & project, wxBitmap & thumbnail) const
{
thumbnail = wxBitmap("CppPlatform/Extensions/textentry.png", wxBITMAP_TYPE_ANY);
return true;
}
void RuntimeTextEntryObject::GetPropertyForDebugger(unsigned int propertyNb, string & name, string & value) const
{
if ( propertyNb == 0 ) {name = _("Text in memory"); value = GetString();}
else if ( propertyNb == 1 ) {name = _("Activated \?"); value = activated ? _("Yes") : _("No");}
}
bool RuntimeTextEntryObject::ChangeProperty(unsigned int propertyNb, string newValue)
{
if ( propertyNb == 0 ) { SetString(newValue); return true; }
else if ( propertyNb == 1 ) { activated = (newValue != _("No")); return true; }
return true;
}
unsigned int RuntimeTextEntryObject::GetNumberOfProperties() const
{
return 2;
}
#endif
RuntimeObject * CreateRuntimeTextEntryObject(RuntimeScene & scene, const gd::Object & object)
{
return new RuntimeTextEntryObject(scene, object);
}
gd::Object * CreateTextEntryObject(std::string name)
{
return new TextEntryObject(name);
}