PropertyFinder

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Revision as of 03:05, 28 April 2007 by Rackle (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

This code creates a tool to list the properties associated with the widgets of a scheme. It loads the .scheme files from within the Scheme directory and displays the widgets defined within that scheme. Selecting a widget will list every property associated to that widget by default. And selecting a property will display its help.

Please discuss this snippet within the Property Finder thread.

Files

PropertyFinder.h

<cpp/>

  1. ifndef _PropertyFinder_h_
  2. define _PropertyFinder_h_
  1. include "CEGuiSample.h"
  2. include "CEGUI.h"
  3. include "CEGUIDefaultResourceProvider.h"
  1. include <io.h>
  2. include <direct.h>
  3. include <map>

class DemoSample : public CEGuiSample { public:

   bool initialiseSample()

{ using namespace CEGUI;

// The executable is stored within <cegui>/bin // The following will change the location of the datafiles from their default of // ../datafiles (which works well for samples) to <cegui>/samples/datafiles DefaultResourceProvider* rp = reinterpret_cast<DefaultResourceProvider*>(System::getSingleton().getResourceProvider()); rp->setResourceGroupDirectory("fonts", "../samples/datafiles/fonts/"); rp->setResourceGroupDirectory("imagesets", "../samples/datafiles/imagesets/"); rp->setResourceGroupDirectory("layouts", "../samples/datafiles/layouts/"); rp->setResourceGroupDirectory("looknfeels", "../samples/datafiles/looknfeel/"); rp->setResourceGroupDirectory("lua_scripts", "../samples/datafiles/lua_scripts/"); rp->setResourceGroupDirectory("schemes", "../samples/datafiles/schemes/"); Font::setDefaultResourceGroup("fonts"); Imageset::setDefaultResourceGroup("imagesets"); WindowManager::setDefaultResourceGroup("layouts"); WidgetLookManager::setDefaultResourceGroup("looknfeels"); ScriptModule::setDefaultResourceGroup("lua_scripts"); Scheme::setDefaultResourceGroup("schemes");

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

// Load the TaharezLook scheme and set up the default mouse cursor and font Scheme* currentScheme = SchemeManager::getSingleton().loadScheme("TaharezLook.scheme"); System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); 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("PropertyFinder.layout"); sheet->addChildWindow(guiLayout);

// Retrieve the handle to the often used widgets mSchemes = static_cast<CEGUI::Combobox*>(winMgr.getWindow("PropertyFinder/SchemeList")); mWidgets = static_cast<CEGUI::Combobox*>(winMgr.getWindow("PropertyFinder/WidgetList")); mProperties = static_cast<CEGUI::Listbox*>(winMgr.getWindow("PropertyFinder/Properties")); mInformation = static_cast<CEGUI::MultiLineEditbox*>(winMgr.getWindow("PropertyFinder/Information"));

// Configure the widgets mSchemes->subscribeEvent(Combobox::EventTextChanged, Event::Subscriber(&DemoSample::onSchemeChanged, this)); mSchemes->subscribeEvent(Combobox::EventListSelectionAccepted, Event::Subscriber(&DemoSample::onSchemeChanged, this)); mSchemes->setSortingEnabled(true); mWidgets->subscribeEvent(Combobox::EventTextChanged, Event::Subscriber(&DemoSample::onWidgetChanged, this)); mWidgets->subscribeEvent(Combobox::EventListSelectionAccepted, Event::Subscriber(&DemoSample::onWidgetChanged, this)); mWidgets->setSortingEnabled(true); mProperties->setSortingEnabled(true); mProperties->setMultiselectEnabled(false); mProperties->subscribeEvent(Listbox::EventSelectionChanged, Event::Subscriber(&DemoSample::onPropertyChanged, this));

// Load every scheme loadEverySchemes(currentScheme); } 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)

{ }

void loadEverySchemes(CEGUI::Scheme* pCurrentScheme) { // Locate the scheme directory and suffix the scheme file search pattern CEGUI::DefaultResourceProvider* rp = reinterpret_cast<CEGUI::DefaultResourceProvider*>(CEGUI::System::getSingleton().getResourceProvider()); CEGUI::String schemeDirectory = rp->getResourceGroupDirectory("schemes") + "\\*.scheme";

// Read the filtered contents of the scheme directory struct _finddata_t c_file; intptr_t hFile = _findfirst(schemeDirectory.c_str(), &c_file ); if(hFile != -1L) { do { loadScheme(c_file.name); } while( _findnext( hFile, &c_file ) == 0 ); _findclose( hFile ); }


// Parse the loaded schemes and determine their internal names (as widget prefixes) const CEGUI::String separator("/"); CEGUI::String currentFal; CEGUI::String::size_type pos; CEGUI::String newLook, currentLook; CEGUI::String widget; CEGUI::WindowFactoryManager::FalagardMappingIterator itFalagardMapping = CEGUI::WindowFactoryManager::getSingletonPtr()->getFalagardMappingIterator(); while( !itFalagardMapping.isAtEnd() ) { currentFal = itFalagardMapping.getCurrentKey(); pos = currentFal.find(separator); newLook = currentFal.substr(0, pos); if(currentLook.compare(newLook) != 0) { currentLook = newLook; addScheme(newLook); } itFalagardMapping++; }

// Select the "current" scheme CEGUI::ListboxItem* current = mSchemes->findItemWithText( pCurrentScheme->getName(), 0 ); if(current) { mSchemes->setText( pCurrentScheme->getName() ); } }

void loadScheme(const CEGUI::String& pScheme, const CEGUI::String& pResourceGroup = "") { try { if( !CEGUI::SchemeManager::getSingleton().isSchemePresent(pScheme) ) { CEGUI::Scheme* scheme = CEGUI::SchemeManager::getSingleton().loadScheme(pScheme, pResourceGroup); } } catch(...) { // Silently ignore errors } }

void addScheme(const CEGUI::String& pScheme) { if( !mSchemes->findItemWithText(pScheme, 0) ) { // This scheme has not yet been added CEGUI::ListboxTextItem* schemeItem = new CEGUI::ListboxTextItem(pScheme); schemeItem->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush"); mSchemes->addItem(schemeItem); } }

void addWidget(const CEGUI::String& pWidget) { CEGUI::ListboxTextItem* widgetItem = new CEGUI::ListboxTextItem(pWidget); widgetItem->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush"); mWidgets->addItem(widgetItem); }

bool onSchemeChanged(const CEGUI::EventArgs& args) { // Refresh the widget list mWidgets->resetList();

CEGUI::String currentLook( mSchemes->getText() ); const CEGUI::String separator("/"); CEGUI::String currentFal; CEGUI::String::size_type pos; CEGUI::String newLook; CEGUI::String widget; CEGUI::WindowFactoryManager::FalagardMappingIterator itFalagardMapping = CEGUI::WindowFactoryManager::getSingletonPtr()->getFalagardMappingIterator(); while( !itFalagardMapping.isAtEnd() ) { currentFal = itFalagardMapping.getCurrentKey(); pos = currentFal.find(separator); newLook = currentFal.substr(0, pos); if(currentLook.compare(newLook) == 0) { widget = currentFal.substr(pos + 1); addWidget( widget ); } itFalagardMapping++; }

// Select the first widget if(mWidgets->getItemCount() > 0) { mWidgets->setText( mWidgets->getListboxItemFromIndex(0)->getText() ); } else { mWidgets->setText(""); }

return true; }

bool onWidgetChanged(const CEGUI::EventArgs& args) { // Refresh the property list mProperties->resetList(); mPropertyHelp.clear();

const CEGUI::String separator("/"); CEGUI::String currentLook( mSchemes->getText() ); CEGUI::String currentWidget( mWidgets->getText() );

CEGUI::Window* widget = CEGUI::WindowManager::getSingleton().createWindow(currentLook + separator + currentWidget);

CEGUI::ListboxTextItem* propertyItem; CEGUI::PropertySet::Iterator itPropertySet = ((CEGUI::PropertySet*) widget)->getIterator(); while (!itPropertySet.isAtEnd() ) { propertyItem = new CEGUI::ListboxTextItem(itPropertySet.getCurrentKey()); propertyItem->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush"); mProperties->addItem(propertyItem); CEGUI::String help = (*itPropertySet)->getHelp(); mPropertyHelp[itPropertySet.getCurrentKey()] = (*itPropertySet)->getHelp(); itPropertySet++; }

CEGUI::WindowManager::getSingleton().destroyWindow(widget);

// Select the first property if(mProperties->getItemCount() > 0) { CEGUI::ListboxItem* selectedProperty = mProperties->getListboxItemFromIndex(0); mProperties->setItemSelectState(selectedProperty, true); mProperties->ensureItemIsVisible(selectedProperty); }

return true; }

bool onPropertyChanged(const CEGUI::EventArgs& args) { // Display the appropriate help information CEGUI::ListboxItem* selectedItem = mProperties->getFirstSelectedItem(); if(selectedItem) { PropertyHelp::iterator itHelp = mPropertyHelp.find( selectedItem->getText() ); if( itHelp != mPropertyHelp.end() ) { mInformation->setText( (*itHelp).second ); } } else { mInformation->setText(""); }

return true; }

private:

// Widget containing the list of schemes CEGUI::Combobox* mSchemes;

// Widget containing the list of widgets in a scheme CEGUI::Combobox* mWidgets;

// Widget containing the list of properties CEGUI::Listbox* mProperties;

typedef std::map<CEGUI::String, CEGUI::String> PropertyHelp; PropertyHelp mPropertyHelp;

// Widget displaying additional information on a property CEGUI::MultiLineEditbox* mInformation; };

  1. endif // _PropertyFinder_h_

Main.cpp

<cpp/>

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

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

  1. endif
  1. include "PropertyFinder.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;
   int i = app.run();
   return i;

}

PropertyFinder.layout

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

<GUILayout >
    <Window Type="TaharezLook/FrameWindow" Name="PropertyFinder" >
        <Property Name="Text" Value="Property Finder" />
        <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.00937452,0},{0.0499997,0},{0.848439,0},{0.695834,0}}" />
        <Window Type="TaharezLook/StaticText" Name="PropertyFinder/SchemeLabel" >
            <Property Name="Text" Value="Scheme:" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.0195535,0},{0.109678,0},{0.114991,0},{0.201613,0}}" />
        </Window>
        <Window Type="TaharezLook/Combobox" Name="PropertyFinder/SchemeList" >
            <Property Name="ReadOnly" Value="True" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.116201,0},{0.109678,0},{0.440692,0},{0.875806,0}}" />
            <Property Name="MaxEditTextLength" Value="1073741823" />
        </Window>
        <Window Type="TaharezLook/StaticText" Name="PropertyFinder/WidgetLabel" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="Text" Value="Widget:" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.473929,0},{0.109678,0},{0.569368,0},{0.201613,0}}" />
        </Window>
        <Window Type="TaharezLook/Combobox" Name="PropertyFinder/WidgetList" >
            <Property Name="Font" Value="Commonwealth-10" />
            <Property Name="ReadOnly" Value="True" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.570578,0},{0.109678,0},{0.969551,0},{0.850001,0}}" />
            <Property Name="MaxEditTextLength" Value="1073741823" />
        </Window>
        <Window Type="TaharezLook/Listbox" Name="PropertyFinder/Properties" >
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="UnifiedAreaRect" Value="{{0.0195535,0},{0.225806,0},{0.440692,0},{0.972579,0}}" />
        </Window>
        <Window Type="TaharezLook/MultiLineEditbox" Name="PropertyFinder/Information" >
            <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.473929,0},{0.225806,0},{0.969551,0},{0.972579,0}}" />
        </Window>
    </Window>
</GUILayout>