Ergebnis 1 bis 11 von 11

Thema: [Bildung] Kampf der Toolkits

Baum-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #9
    Zitat Zitat von mq Beitrag anzeigen
    Okay, ich hab da mal was haessliches hingerotzt.
    #2
    Ich poste mal nicht den gesamten Code, sondern nur die CEGUI-Relevanten Stellen.

    Die XML-Datei mit dem GUI-Layout. Die GUI in C++ zu schreiben ist zwar möglich aber meistens nicht Sinnvoll.
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <GUILayout>
      <Window Type="DefaultWindow" Name="root" >
        <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{1,0},{1,0}}" />
        
        <Window Type="TaharezLook/FrameWindow" Name="fwRechner" >
          <Property Name="Text" Value="Toller Taschenrechner 2000" />
          <Property Name="TitlebarEnabled" Value="True" />
          <Property Name="TitlebarFont" Value="Commonwealth-10" />
          <Property Name="UnifiedAreaRect" Value="{{0.5,-100},{0.5,-100},{0.5,100},{0.5,100}}" />
    
          <Window Type="TaharezLook/Editbox" Name="fwRechner/eingabefeld" >
            <Property Name="Text" Value="0" />
            <Property Name="UnifiedPosition" Value="{{0.04,0},{0.15,0}}" />
            <Property Name="UnifiedSize" Value="{{0.92,0},{0.10,0}}" />
          </Window>
          
          <Window Type="TaharezLook/Button" Name="fwRechner/clear" >
            <Property Name="Text" Value="C" />
            <Property Name="UnifiedPosition" Value="{{0.04,0},{0.30,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/pm" >
            <Property Name="Text" Value="+/-" />
            <Property Name="UnifiedPosition" Value="{{0.28,0},{0.30,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/division" >
            <Property Name="Text" Value="/" />
            <Property Name="UnifiedPosition" Value="{{0.52,0},{0.30,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/multiplikation" >
            <Property Name="Text" Value="x" />
            <Property Name="UnifiedPosition" Value="{{0.76,0},{0.30,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
    
          <Window Type="TaharezLook/Button" Name="fwRechner/7" >
            <Property Name="Text" Value="7" />
            <Property Name="UnifiedPosition" Value="{{0.04,0},{0.44,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/8" >
            <Property Name="Text" Value="8" />
            <Property Name="UnifiedPosition" Value="{{0.28,0},{0.44,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/9" >
            <Property Name="Text" Value="9" />
            <Property Name="UnifiedPosition" Value="{{0.52,0},{0.44,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/-" >
            <Property Name="Text" Value="-" />
            <Property Name="UnifiedPosition" Value="{{0.76,0},{0.44,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
    
          <Window Type="TaharezLook/Button" Name="fwRechner/4" >
            <Property Name="Text" Value="4" />
            <Property Name="UnifiedPosition" Value="{{0.04,0},{0.58,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/5" >
            <Property Name="Text" Value="5" />
            <Property Name="UnifiedPosition" Value="{{0.28,0},{0.58,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/6" >
            <Property Name="Text" Value="6" />
            <Property Name="UnifiedPosition" Value="{{0.52,0},{0.58,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/+" >
            <Property Name="Text" Value="+" />
            <Property Name="UnifiedPosition" Value="{{0.76,0},{0.58,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
    
          <Window Type="TaharezLook/Button" Name="fwRechner/1" >
            <Property Name="Text" Value="1" />
            <Property Name="UnifiedPosition" Value="{{0.04,0},{0.72,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/2" >
            <Property Name="Text" Value="2" />
            <Property Name="UnifiedPosition" Value="{{0.28,0},{0.72,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/3" >
            <Property Name="Text" Value="3" />
            <Property Name="UnifiedPosition" Value="{{0.52,0},{0.72,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/=" >
            <Property Name="Text" Value="=" />
            <Property Name="UnifiedPosition" Value="{{0.76,0},{0.72,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.25,0}}" />
          </Window>
    
          <Window Type="TaharezLook/Button" Name="fwRechner/0" >
            <Property Name="Text" Value="0" />
            <Property Name="UnifiedPosition" Value="{{0.04,0},{0.86,0}}" />
            <Property Name="UnifiedSize" Value="{{0.44,0},{0.10,0}}" />
          </Window>
          <Window Type="TaharezLook/Button" Name="fwRechner/." >
            <Property Name="Text" Value="." />
            <Property Name="UnifiedPosition" Value="{{0.52,0},{0.86,0}}" />
            <Property Name="UnifiedSize" Value="{{0.2,0},{0.10,0}}" />
          </Window>
          
          
          
        </Window>
      </Window>
    </GUILayout>
    Erstellen des CEGUI Renderers und Systems, laden des Skins, zuweisen eines Standard Mouse Cursor Bildes und laden des GUI-Layouts aus der XML-Datei. In *.scheme können font, lookNFeels und imageset festgelegt werden. In imagesets wird die Position von GUI-Elementen ,bzw. Einzelteilen aus denen sich die Elemente zusammensetzen, in einer Bildatei angegeben. In *.looknfeel wird festgelegt, aus welchen Bildern ein bestimmtes GUI-Element besteht und wo sie sich befinden.
    Code:
    mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
    mGUISystem = new CEGUI::System(mGUIRenderer);
    
        CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
    CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
    
    CEGUI::Window* rootWindow = CEGUI::WindowManager::getSingleton().loadWindowLayout("taschenrechner.xml");
    CEGUI::System::getSingleton().setGUISheet(rootWindow);
    Allen PushButtons wird dieselbe Callback Funktion zugewiesen:
    Code:
    	CEGUI::WindowManager::getSingleton().getWindow("fwRechner/eingabefeld")->setText("0");
    
    	CEGUI::WindowManager::WindowIterator wi = CEGUI::WindowManager::getSingleton().getIterator();
    	CEGUI::Window* w;
    	while(!wi.isAtEnd())
    	{
    		w = *wi;
    		if(w->getType() == "TaharezLook/Button")
    		{
    			w->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&TRFrameListener::buttonPressed, this));
    		}
    		wi++;
    	}
    Die Callbackfunktion. Kann man einen Taschenrechner schlimmer programmieren?
    Code:
    bool TRFrameListener::buttonPressed(const CEGUI::EventArgs& e)
    {
    	//Zeiger zum Eingabefeld
    	CEGUI::WindowManager* wmgr = CEGUI::WindowManager::getSingletonPtr();
    	CEGUI::Window* eingabefeld = wmgr->getWindow("fwRechner/eingabefeld");
    	//Den unbrauchbaren EventArgs Typ in einen Zeiger zum gedrückten Button umwandeln.
    	const CEGUI::WindowEventArgs* evt = static_cast<const CEGUI::WindowEventArgs*>(&e);
    	CEGUI::PushButton* but = static_cast<CEGUI::PushButton*>(evt->window);
    	//Ein paar zuweisungen für Variablen/Werte, die häufig genutzt werden. Kurze, nichtssagende namen ftw.
    	std::string bn = but->getName().c_str();
    	std::string bt = but->getText().c_str();
    	std::string et = eingabefeld->getText().c_str();
    
    	if(bt == "0" || bt == "1" || bt == "2" || bt == "3" || bt == "4" || bt == "5" || bt == "6" || bt == "7" || bt == "8" || bt == "9")
    	{
    		if(Ogre::StringConverter::parseReal(et) == 0)
    			eingabefeld->setText("");
    		eingabefeld->setText(std::string(et.c_str()) + bt);
    	}
    	else if(bt == "C")
    	{
    		eingabefeld->setText("0");
    		wert = 0;
    		operant = "";
    	}
    	else if(bt == ".")
    	{
    		eingabefeld->setText(std::string(et.c_str()) + bt);
    	}
    	else
    	{
    		if(operant != "")
    		{
    			if(operant == "+")
    				wert += Ogre::StringConverter::parseReal(et);
    			if(operant == "-")
    				wert -= Ogre::StringConverter::parseReal(et);
    			if(operant == "x")
    				wert *= Ogre::StringConverter::parseReal(et);
    			if(operant == "/")
    				wert /= Ogre::StringConverter::parseReal(et);
    
    			eingabefeld->setText(Ogre::StringConverter::toString(static_cast<Ogre::Real>(wert)));
    		}
    		else
    		{
    			wert = Ogre::StringConverter::parseReal(et);
    		}
    
    		if(bt == "=")
    		{
    			operant == "";
    		}
    		else
    		{
    			if(bt == "+/-")
    				wert *= -1;
    			else if(bt == "+")
    				operant = "+";
    			else if(bt == "-")
    				operant = "-";
    			else if(bt == "x")
    				operant = "x";
    			else if(bt == "/")
    				operant = "/";
    
    			eingabefeld->setText("0");
    		}
    
    		
    
    	}
    	return true;
    }

    Geändert von nudelsalat (15.11.2006 um 20:11 Uhr)

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •