Drawing text dinamically

edited February 2012 in Help request
Hello! it's me again! This is my button class. It draws perfectly. But he has a problem: can not draw a text dynamically.

Boton.h
#ifndef __ENGINE_OBJETOS_BOTON_H__
#define __ENGINE_OBJETOS_BOTON_H__

#include "../GameObject.h"
#include "../Eventos.h"

namespace Engine
{
	namespace Objetos
	{
		namespace Var
		{
			static orxOBJECT* highlighted_button_ = orxNULL;
		}

		class Boton : public Engine::GameObject
		{
			private:
				orxFONT* pstFont;
				orxTEXT* pstText;

				void FreeFont();
				void UpdateFont();

			protected:
				std::string eventName;
				std::string fontName;
				std::string text;

			public:
				Boton(const orxSTRING conf) : Engine::GameObject()
				{
					text = "none";
					pstFont = orxNULL;
					pstText = orxNULL;
					myObject = orxObject_CreateFromConfig(conf);
					eventName = "ButtonLeft";
					SetFont("ButtonFont");
					if(orxConfig_PushSection(orxObject_GetName(myObject)))
					{
						orxObject_SetTargetAnim(myObject, orxConfig_GetString("AnimationInactive"));
						orxConfig_PopSection();
					}
				}

				~Boton();

				void OnUpdate();
				virtual void OnButtonClick() {}

				void SetFont(const std::string& fontName);
				
				void SetText(const std::string& text);
				std::string GetText();
		};
	};
};

#endif

Boton.cpp
#include "Boton.h"
#include <orx.h>

namespace Engine 
{ 
	namespace Objetos 
	{
		Boton::~Boton()
		{				
			FreeFont();
		}

		void Boton::FreeFont()
		{
			if(pstFont != orxNULL)
			{
				orxFont_Delete(pstFont);
				pstFont = orxNULL;
			}

			if(pstText != orxNULL)
			{
				orxText_Delete(pstText);
				pstText = orxNULL;
			}
		}

		void Boton::SetFont(const std::string& fontName)
		{
			FreeFont();
			this->fontName = fontName;
			pstFont = orxFont_CreateFromConfig(fontName.c_str());
			pstText = orxText_Create();
			orxText_SetFont(pstText, pstFont);
			orxText_SetString(pstText, this->text.c_str());
		}

		void Boton::UpdateFont()
		{
			SetFont(fontName);
		}

		void Boton::OnUpdate()
		{
		  // ...
		}

		void Boton::SetText(const std::string& text)
		{
			this->text = text;
			UpdateFont();
		}

		std::string Boton::GetText()
		{
			return text;
		}
	};
};

How I can select the position of the text? (dinamically)

Comments

  • edited February 2012
    Hi!

    Well, I think you can go with something simpler. :)

    You don't need to create your own font or text directly, everything can be done from config.

    In config:
    [MyButton]
    Graphic = MyButtonG
    
    [MyButtonG]
    Text = MyButtonT
    
    [MyButtonT]
    String = Button text; <= This can be a simple placeholder, not the definitive text
    Font = MyFont
    

    In code:
    orxOBJECT *pstButton = orxObject_CreateFromConfig("MyButton");
    
    // To move it
    orxObject_SetPosition(pstButton, &vPos);
    
    // To change its content
    orxObject_SetTextString(pstButton, "My new text");
    

    If you want to change the font dynamically, I'd recommend to either delete that button and recreate one with the new font or to use the localization module that can associate localized texts with fonts (see the tutorial #10).
  • edited February 2012
    [MyButton]
    Graphic = MyButtonG
    
    [MyButtonG]
    Text = MyButtonT
    
    [MyButtonT]
    String = Button text; <= This can be a simple placeholder, not the definitive text
    Font = MyFont
    

    Thanks! it's working!! i'm using "MyButton" like a text object, and i move it over the button :) :)
  • edited February 2012
    No problem, I'm glad it works for you. :)
Sign In or Register to comment.