Difference between revisions of "Dynamic Fonts"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
(Introduction)
(DynamicFont.h)
Line 34: Line 34:
 
SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
 
SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
 
System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
 
System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
FontManager::getSingleton().createFont("Commonwealth-10.font");
+
if(!FontManager::getSingleton().isFontPresent("Commonwealth-10"))
 +
FontManager::getSingleton().createFont("Commonwealth-10.font");
  
 
// Set the GUI Sheet
 
// Set the GUI Sheet

Revision as of 13:11, 1 February 2008

Introduction

The datafiles\fonts folder contains static font definitions such as Commonwealth-10.font. The XML contents of that file specify one particular font, mainly a logical name (Commonwealth-10), a filename (Commonv2c.ttf), and a size (10). To load this static font definition you'd write the following line of code: CEGUI::FontManager::getSingleton().createFont("Commonwealth-10.font");

This works well, but sometimes you need to display a same font in various sizes. One approach is to create multiple .font files, one per font size. But there is a better approach, a way to dynamically create fonts.

Please post comments or questions on the message board thread for the DynamicFont

Files

DynamicFont.h

</cpp>

  1. ifndef _DynamicFont_h_
  2. define _DynamicFont_h_
  1. include "CEGuiSample.h"
  2. include "CEGUI.h"
  3. include "CEGUIXMLAttributes.h"

class DemoSample : public CEGuiSample { public:

   bool initialiseSample()

{ using namespace CEGUI; try { // Retrieve the window manager WindowManager& winMgr = WindowManager::getSingleton();

// Load the TaharezLook scheme and set up the default mouse cursor and font SchemeManager::getSingleton().loadScheme("TaharezLook.scheme"); System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); if(!FontManager::getSingleton().isFontPresent("Commonwealth-10")) FontManager::getSingleton().createFont("Commonwealth-10.font");

// Set the GUI Sheet Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd"); System::getSingleton().setGUISheet(sheet);

// Load a layout Window* guiLayout = winMgr.loadWindowLayout("DynamicFont.layout"); sheet->addChildWindow(guiLayout);

// Configure the event for the list of created fonts CEGUI::Combobox* existingFonts = static_cast<CEGUI::Combobox*>(winMgr.getWindow("DynamicFont/Fonts")); existingFonts->subscribeEvent(CEGUI::Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber(&DemoSample::eventSelectExistingFont, this));

// Configure the event for the add button CEGUI::PushButton* add = static_cast<CEGUI::PushButton*>(winMgr.getWindow("DynamicFont/Add")); add->subscribeEvent(CEGUI::PushButton::EventMouseClick, CEGUI::Event::Subscriber(&DemoSample::eventAddFont, this));

// Add the font previously created addExistingFont("Commonwealth-10"); } catch(Exception &e) { #if defined( __WIN32__ ) || defined( _WIN32 ) MessageBox(NULL, e.getMessage().c_str(), "Error initializing the demo", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "Error initializing the demo:" << e.getMessage().c_str() << "\n"; #endif }

return true; }

void cleanupSample(void) { }

bool eventAddFont(const CEGUI::EventArgs& args) { // Add a font from the specified parameters using namespace CEGUI;

WindowManager& winMgr = WindowManager::getSingleton(); Window* nameText = winMgr.getWindow("DynamicFont/Name"); Window* filenameText = winMgr.getWindow("DynamicFont/Filename"); Window* sizeText = winMgr.getWindow("DynamicFont/Size");

setFont(nameText->getText(), filenameText->getText(), sizeText->getText()); return true; }

void addExistingFont(const CEGUI::String& pName) { // Once a font has been created we add it to the combobox of fonts using namespace CEGUI; WindowManager& winMgr = WindowManager::getSingleton();

Combobox* existingFonts = static_cast<CEGUI::Combobox*>(winMgr.getWindow("DynamicFont/Fonts")); ListboxTextItem* itemCombobox = new ListboxTextItem(pName); itemCombobox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush"); itemCombobox->setSelected(true); existingFonts->addItem(itemCombobox); }

bool eventSelectExistingFont(const CEGUI::EventArgs& args) { // Use an already created font using namespace CEGUI; WindowManager& winMgr = WindowManager::getSingleton(); Combobox* existingFonts = static_cast<CEGUI::Combobox*>(winMgr.getWindow("DynamicFont/Fonts")); setFont(existingFonts->getText(), "", ""); return true; }

void setFont(const CEGUI::String& pName, const CEGUI::String& pFilename, const CEGUI::String& pSize) { // Set the font displayed within the multiline editbox using namespace CEGUI;

String sampleText(pName);

WindowManager& winMgr = WindowManager::getSingleton(); CEGUI::Window* text = winMgr.getWindow("DynamicFont/Text"); Font* font;

if(FontManager::getSingleton().isFontPresent(pName)) { // Select a previously created font font = FontManager::getSingleton().getFont(pName); } else { // Dynamically create a new font XMLAttributes xmlAttributes;

// Attributes specified within CEGUIFont.cpp xmlAttributes.add("Name", pName); xmlAttributes.add("Filename", pFilename); xmlAttributes.add("ResourceGroup", ""); xmlAttributes.add("AutoScaled", "true"); xmlAttributes.add("NativeHorzRes", "800"); xmlAttributes.add("NativeVertRes", "600");

// Attributes specified within CEGUIXMLAttributes.cpp xmlAttributes.add("Size", pSize); xmlAttributes.add("AntiAlias", "true");

try { font = FontManager::getSingleton().createFont("FreeType", xmlAttributes); if(font) { font->load(); // This function must be used addExistingFont(pName); } } catch(Exception &e) { // Display the error message sampleText = e.getMessage(); } }

text->setFont(font); text->setText(sampleText); }

private: };

  1. endif // _DynamicFont_h_

main.cpp

</cpp>

  1. if defined( __WIN32__ ) || defined( _WIN32 )

#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include "windows.h"

  1. endif
  1. include "DynamicFont.h"
  1. if defined( __WIN32__ ) || defined( _WIN32 )

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)

  1. else

int main(int argc, char *argv[])

  1. endif

{

   DemoSample app;
   return app.run();

}

DynamicFont.layout

<?xml version="1.0" encoding="UTF-8"?>

<GUILayout >
    <Window Type="TaharezLook/FrameWindow" Name="DynamicFont" >
        <Property Name="Text" Value="Dynamic Font" />
        <Property Name="TitlebarFont" Value="Commonwealth-10" />
        <Property Name="InheritsAlpha" Value="False" />
        <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
        <Property Name="TitlebarEnabled" Value="True" />
        <Property Name="UnifiedAreaRect" Value="{{0.02,0},{0.054167,0},{0.98,0},{0.781251,0}}" />
        <Window Type="TaharezLook/MultiLineEditbox" Name="DynamicFont/Text" >
            <Property Name="Text" >
</Property>
            <Property Name="ReadOnly" Value="True" />
            <Property Name="MaxTextLength" Value="1073741823" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.01,0},{0.075,0},{0.99,0},{0.715617,0}}" />
        </Window>
        <Window Type="TaharezLook/StaticText" Name="DynamicFont/NameLabel" >
            <Property Name="Text" Value="Name:" />
            <Property Name="HorzFormatting" Value="RightAligned" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.018409,0},{0.881661,0},{0.0809004,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/Editbox" Name="DynamicFont/Name" >
            <Property Name="MaxTextLength" Value="1073741823" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.0809,0},{0.881661,0},{0.328819,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/StaticText" Name="DynamicFont/FilenameLabel" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="Text" Value="Filename:" />
            <Property Name="HorzFormatting" Value="RightAligned" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.347544,0},{0.881661,0},{0.438807,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/Editbox" Name="DynamicFont/Filename" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="MaxTextLength" Value="1073741823" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.438807,0},{0.881661,0},{0.713682,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/StaticText" Name="DynamicFont/SizeLabel" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="Text" Value="Size:" />
            <Property Name="HorzFormatting" Value="RightAligned" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.731982,0},{0.881661,0},{0.793499,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/Editbox" Name="DynamicFont/Size" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="MaxTextLength" Value="1073741823" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.793499,0},{0.881661,0},{0.858294,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/Button" Name="DynamicFont/Add" >
            <Property Name="Text" Value="Add" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.867109,0},{0.881661,0},{0.974324,0},{0.968338,0}}" />
        </Window>
        <Window Type="TaharezLook/StaticText" Name="DynamicFont/FontsLabel" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="Text" Value="Fonts:" />
            <Property Name="HorzFormatting" Value="RightAligned" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.018409,0},{0.768338,0},{0.0859395,0},{0.855015,0}}" />
        </Window>
        <Window Type="TaharezLook/Combobox" Name="DynamicFont/Fonts" >
            <Property Name="ReadOnly" Value="True" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.085939,0},{0.768338,0},{0.974324,0},{0.994412,0}}" />
            <Property Name="MaxEditTextLength" Value="1073741823" />
        </Window>
    </Window>
</GUILayout>