Просмотр исходного кода

Slight change/simplification to the interface used by ControlFactory.cpp
Added back some code that accidentally got removed from a bad merge on a previous commit.

sgrenier 12 лет назад
Родитель
Сommit
07e7a3f530

+ 1 - 1
gameplay/src/Button.cpp

@@ -24,7 +24,7 @@ Button* Button::create(const char* id, Theme::Style* style)
     return button;
 }
 
-Control* Button::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* Button::create(Theme::Style* style, Properties* properties)
 {
     Button* button = new Button();
     button->initialize(style, properties);

+ 1 - 2
gameplay/src/Button.h

@@ -67,11 +67,10 @@ protected:
      *
      * @param style The style to apply to this button.
      * @param properties The properties to set on this button.
-     * @param theme The theme to set on this control if needed.
      *
      * @return The new button.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * @see Control::getType

+ 1 - 1
gameplay/src/CheckBox.cpp

@@ -26,7 +26,7 @@ CheckBox* CheckBox::create(const char* id, Theme::Style* style)
     return checkBox;
 }
 
-Control* CheckBox::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* CheckBox::create(Theme::Style* style, Properties* properties)
 {
     GP_ASSERT(properties);
 

+ 1 - 2
gameplay/src/CheckBox.h

@@ -113,11 +113,10 @@ protected:
      *
      * @param style The style to apply to this checkbox.
      * @param properties The properties to set on this checkbox.
-     * @param theme The theme to set on this control if needed.
      *
      * @return The new checkbox.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Keyboard callback on key events.

+ 7 - 54
gameplay/src/Container.cpp

@@ -4,14 +4,7 @@
 #include "AbsoluteLayout.h"
 #include "FlowLayout.h"
 #include "VerticalLayout.h"
-#include "Label.h"
-#include "Button.h"
-#include "CheckBox.h"
-#include "RadioButton.h"
-#include "Slider.h"
-#include "TextBox.h"
-#include "Joystick.h"
-#include "ImageControl.h"
+#include "ControlFactory.h"
 #include "Form.h"
 #include "Game.h"
 
@@ -81,16 +74,16 @@ Container* Container::create(const char* id, Theme::Style* style, Layout::Type l
     return container;
 }
 
-Control* Container::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* Container::create(Theme::Style* style, Properties* properties)
 {
     GP_ASSERT(properties);
 
     Container* container = new Container();
-    container->initialize(style, properties, theme);
+    container->initialize(style, properties);
     return container;
 }
 
-void Container::initialize(Theme::Style* style, Properties* properties, Theme* theme)
+void Container::initialize(Theme::Style* style, Properties* properties)
 {
     Control::initialize(style, properties);
 
@@ -127,7 +120,7 @@ void Container::initialize(Theme::Style* style, Properties* properties, Theme* t
     if (properties->exists("scrollWheelSpeed"))
         _scrollWheelSpeed = properties->getFloat("scrollWheelSpeed");
 
-    addControls(theme, properties);
+    addControls(style->getTheme(), properties);
     _layout->update(this, _scrollPosition);
 
     const char* activeControl = properties->getString("activeControl");
@@ -168,48 +161,8 @@ void Container::addControls(Theme* theme, Properties* properties)
 
         std::string controlName(controlSpace->getNamespace());
         std::transform(controlName.begin(), controlName.end(), controlName.begin(), (int(*)(int))toupper);
-        if (controlName == "LABEL")
-        {
-            control = Label::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "BUTTON")
-        {
-            control = Button::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "CHECKBOX")
-        {
-            control = CheckBox::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "RADIOBUTTON")
-        {
-            control = RadioButton::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "CONTAINER")
-        {
-            control = Container::create(controlStyle, controlSpace, theme);
-        }
-        else if (controlName == "SLIDER")
-        {
-            control = Slider::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "TEXTBOX")
-        {
-            control = TextBox::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "JOYSTICK")
-        {
-            control = Joystick::create(controlStyle, controlSpace);
-        }
-        else if (controlName == "IMAGE")
-        {
-            control = ImageControl::create(controlStyle, controlSpace);
-        }
-        else
-        {
-            // Ignore - not a valid control name.
-            // This used to fail, but I see no reason to hard fail here (this also fixes not being able
-            // to set padding on containers).
-        }
+
+        control = ControlFactory::getInstance()->createControl(controlName.c_str(), controlStyle, controlSpace);
 
         // Add the new control to the form.
         if (control)

+ 2 - 4
gameplay/src/Container.h

@@ -318,20 +318,18 @@ protected:
      *
      * @param style The style to apply to this container.
      * @param properties The properties to set on this container, including nested controls.
-     * @param theme The theme to search for control styles within.
      *
      * @return The new container.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Initialize properties common to all Containers from a Properties object.
      *
      * @param style The style to apply to this control.
      * @param properties The properties to set on this control.
-     * @param theme The theme for this control.
      */
-    void initialize(Theme::Style* style, Properties* properties, Theme* theme);
+    void initialize(Theme::Style* style, Properties* properties);
 
     /**
      * Updates each control within this container,

+ 7 - 2
gameplay/src/ControlFactory.cpp

@@ -30,6 +30,11 @@ ControlFactory::~ControlFactory()
 {
 }
 
+void ControlFactory::finalize()
+{
+    SAFE_DELETE(__controlFactory);
+}
+
 ControlFactory* ControlFactory::getInstance() 
 {
 	if (__controlFactory == NULL)
@@ -55,12 +60,12 @@ void ControlFactory::unregisterCustomControl(const char* controlName)
 	}
 }
 
-Control *ControlFactory::createControl(const char* controlName, Theme::Style* style, Properties* properties, Theme* theme)
+Control *ControlFactory::createControl(const char* controlName, Theme::Style* style, Properties* properties)
 {
 	if (_registeredControls.find(controlName) == _registeredControls.end())
 		return NULL;
 
-	return (*_registeredControls[controlName])(style, properties, theme);
+	return (*_registeredControls[controlName])(style, properties);
 }
 
 void ControlFactory::registerStandardControls() 

+ 18 - 8
gameplay/src/ControlFactory.h

@@ -1,7 +1,7 @@
 #ifndef	CONTROLFACTORY_H_
 #define	CONTROLFACTORY_H_
 
-#include "Theme.h"
+#include "ThemeStyle.h"
 
 namespace gameplay 
 {	
@@ -15,13 +15,15 @@ class Control;
  * @script{ignore}
  */
 class ControlFactory 
-{	
-public :
+{
+    friend class Game;
+
+public:
 
 	/**
 	 * The activator interface for controls that are created.
 	 */
-	typedef Control* (*ControlActivator)(Theme::Style*, Properties*, Theme*);
+	typedef Control* (*ControlActivator)(Theme::Style*, Properties*);
 
 	/**
 	 * Gets the single instance of the control factory used to create controls and register/unregister custom controls.
@@ -34,7 +36,7 @@ public :
 	 * Registers a custom control and specify the activator.
 	 *
 	 * @param controlName The name of the custom control to register.
-	 * @param activator The activator for applying the style, properties and theme to the control.
+	 * @param activator The activator for applying the style and properties to the control.
 	 *
 	 * @return true if the control was successfully registered.
 	 */
@@ -46,13 +48,16 @@ public :
 	 * @param controlName The name of the custom control to unregister.
 	 */
 	void unregisterCustomControl(const char* controlName);
-		
+
 	/**
 	 * Creates a controls from the set of core and custom controls registered.
 	 *
-	 * @param controlName The name of the control to create
+	 * @param controlName The name of the control to create.
+     * @param style The style to apply to the control.
+     * @param properties The Properties object containing the definition of the controlo.
+     * @return The newly created control.
 	 */
-	Control* createControl(const char* controlName, Theme::Style *style, Properties *properties, Theme *theme = NULL);
+	Control* createControl(const char* controlName, Theme::Style *style, Properties *properties);
 
 private:
 
@@ -71,6 +76,11 @@ private:
 	 */
 	~ControlFactory();
 
+    /**
+    * Called when the game is shutting down to clean up resources.
+    */
+    static void finalize();
+
 	/**
 	 * Assignment operator
 	 */

+ 1 - 1
gameplay/src/Form.cpp

@@ -130,7 +130,7 @@ Form* Form::create(const char* url)
     form->_theme = theme;
 
     // Initialize common container properties
-    form->initialize(style, formProperties, theme);
+    form->initialize(style, formProperties);
 
     form->updateFrameBuffer();
 

+ 3 - 0
gameplay/src/Game.cpp

@@ -5,6 +5,7 @@
 #include "FileSystem.h"
 #include "FrameBuffer.h"
 #include "SceneLoader.h"
+#include "ControlFactory.h"
 
 /** @script{ignore} */
 GLenum __gl_error_code = GL_NO_ERROR;
@@ -200,6 +201,8 @@ void Game::shutdown()
         _socialController->finalize();
         SAFE_DELETE(_socialController);
 
+        ControlFactory::finalize();
+
         // Note: we do not clean up the script controller here
         // because users can call Game::exit() from a script.
 

+ 1 - 1
gameplay/src/ImageControl.cpp

@@ -29,7 +29,7 @@ ImageControl* ImageControl::create(const char* id, Theme::Style* style)
     return imageControl;
 }
 
-Control* ImageControl::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* ImageControl::create(Theme::Style* style, Properties* properties)
 {
     ImageControl* imageControl = new ImageControl();
     imageControl->initialize(style, properties);

+ 3 - 3
gameplay/src/ImageControl.h

@@ -94,7 +94,7 @@ public:
      * @param height The height of the destination region.
      */
     void setRegionDst(float x, float y, float width, float height);
-    
+
     /**
      * Sets the destination region of this ImageControl.  This is the region
      * within the control's viewport to draw the image.
@@ -115,10 +115,10 @@ public:
 protected:
 
     ImageControl();
-    
+
     virtual ~ImageControl();
 
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     virtual void initialize(Theme::Style* style, Properties* properties);
 

+ 1 - 1
gameplay/src/Joystick.cpp

@@ -28,7 +28,7 @@ Joystick* Joystick::create(const char* id, Theme::Style* style)
     return joystick;
 }
 
-Control* Joystick::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* Joystick::create(Theme::Style* style, Properties* properties)
 {
     Joystick* joystick = new Joystick();
     joystick->initialize(style, properties);

+ 1 - 2
gameplay/src/Joystick.h

@@ -138,11 +138,10 @@ protected:
      *
      * @param style The style to apply to this joystick.
      * @param properties The properties to set on this joystick.
-     * @param theme The theme to set on this control if needed.
 	 *
      * @return The new joystick.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme *theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Initialize this joystick.

+ 1 - 1
gameplay/src/Label.cpp

@@ -27,7 +27,7 @@ Label* Label::create(const char* id, Theme::Style* style)
     return label;
 }
 
-Control* Label::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* Label::create(Theme::Style* style, Properties* properties)
 {
     Label* label = new Label();
     label->initialize(style, properties);

+ 1 - 2
gameplay/src/Label.h

@@ -94,11 +94,10 @@ protected:
      *
      * @param style The style to apply to this label.
      * @param properties The properties to set on this label.
-     * @param theme The theme to set on this control if needed.
 	 * 
      * @return The new label.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme *theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Initialize this label.

+ 1 - 1
gameplay/src/RadioButton.cpp

@@ -33,7 +33,7 @@ RadioButton* RadioButton::create(const char* id, Theme::Style* style)
     return radioButton;
 }
 
-Control* RadioButton::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* RadioButton::create(Theme::Style* style, Properties* properties)
 {
     GP_ASSERT(properties);
 

+ 1 - 2
gameplay/src/RadioButton.h

@@ -126,11 +126,10 @@ protected:
      *
      * @param style The style to apply to this radio button.
      * @param properties The properties to set on this radio button.
-     * @param theme The theme to set on this control if needed.
      *
      * @return The new radio button.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Keyboard callback on key events.

+ 1 - 1
gameplay/src/Slider.cpp

@@ -36,7 +36,7 @@ Slider* Slider::create(const char* id, Theme::Style* style)
     return slider;
 }
 
-Control* Slider::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* Slider::create(Theme::Style* style, Properties* properties)
 {
     GP_ASSERT(properties);
 

+ 1 - 2
gameplay/src/Slider.h

@@ -182,11 +182,10 @@ protected:
      *
      * @param style The style to apply to this slider.
      * @param properties The properties to set on this slider.
-     * @param theme The theme to set on this control if needed.
      *
      * @return The new slider.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Touch callback on touch events.  Controls return true if they consume the touch event.

+ 1 - 1
gameplay/src/TextBox.cpp

@@ -30,7 +30,7 @@ TextBox* TextBox::create(const char* id, Theme::Style* style)
     return textBox;
 }
 
-Control* TextBox::create(Theme::Style* style, Properties* properties, Theme* theme)
+Control* TextBox::create(Theme::Style* style, Properties* properties)
 {
     TextBox* textBox = new TextBox();
     textBox->initialize(style, properties);

+ 1 - 2
gameplay/src/TextBox.h

@@ -144,11 +144,10 @@ protected:
      *
      * @param style The style to apply to this text box.
      * @param properties The properties to set on this text box.
-     * @param theme The theme to set on this control if needed.
      *
      * @return The new text box.
      */
-    static Control* create(Theme::Style* style, Properties* properties, Theme* theme = NULL);
+    static Control* create(Theme::Style* style, Properties* properties);
 
     /**
      * Touch callback on touch events.  Controls return true if they consume the touch event.