gamepad-simple-element.cpp

Go to the documentation of this file.
00001 /*
00002  * gamepad-simple-element.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of gamepad dialog Simple Rendering element
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "gamepad-vgfx-element.h"       // always include our own header first
00036 
00037 #include "datahash/datahash_util.h"
00038 #include "gamepad-vgfx/gamepad-vgfx.h"
00039 
00040 
00041 namespace gamepad {
00042 
00043 
00044 static const char * s_simpleElementName         = "gamepadVgfxSimple";
00045 
00046 static const char * s_null                      = "(null)";
00047 
00048 
00049 ////////////////////////////////////////////////////////////////////////////////
00050 //
00051 //      static helper methods
00052 //
00053 ////////////////////////////////////////////////////////////////////////////////
00054 
00055 
00056 ////////////////////////////////////////////////////////////////////////////////
00057 //
00058 //      Simple -- class that implements the dialog::Element interface
00059 //
00060 ////////////////////////////////////////////////////////////////////////////////
00061 
00062 class Simple : public dialog::Element {
00063 public:
00064         // constructor, destructor ---------------------------------------------
00065         ~Simple(void) throw() { }
00066 
00067         // dialog::Element class interface methods -----------------------------
00068         int getWidth(void) { return m_width; }
00069         int getHeight(void) { return m_height; }
00070         void draw(IN const dialog::point_t& offset); 
00071         void cursor(IN const dialog::point_t& pos) { }
00072         const char * button(IN int button, IN int state,
00073                                 IN const dialog::point_t& pos)
00074                                 { return NULL; }
00075         void addData(IN crypto::DESKey * key, IO Datahash * data) { }
00076 
00077         // static factory methods ----------------------------------------------
00078         static smart_ptr<dialog::Element> create(IN Type * type,
00079                                 IN const char * flash,
00080                                 IN const char * pulse,
00081                                 IN const char * direction,
00082                                 IN smart_ptr<vgfx::Drawer>& drawer);
00083 
00084 private:
00085         // private constructors ------------------------------------------------
00086         Simple(void) throw(); 
00087 
00088         // private typedefs ----------------------------------------------------
00089 
00090         // private helper methods ----------------------------------------------
00091         void initialize(IN Type * type,
00092                                 IN const char * flash,
00093                                 IN const char * pulse,
00094                                 IN const char * direction,
00095                                 IN smart_ptr<vgfx::Drawer>& drawer);
00096 
00097         // private member data -------------------------------------------------
00098         int                             m_width;
00099         int                             m_height;
00100         smart_ptr<TypeVgfxRenderer>     m_renderer;
00101         Type *                          m_type;
00102         std::string                     m_flash;
00103         std::string                     m_pulse;
00104         eDpad                           m_direction;
00105         int                             m_counter;
00106 };
00107 
00108 
00109 
00110 Simple::Simple
00111 (
00112 void
00113 )
00114 throw()
00115 {
00116         m_type = NULL;
00117 
00118         m_width = 400;
00119         m_height = 300;
00120 
00121         m_counter = 0;
00122 
00123         m_direction = eDpad_Centered;
00124 }
00125 
00126 
00127 
00128 smart_ptr<dialog::Element>
00129 Simple::create
00130 (
00131 IN Type * type,
00132 IN const char * flash,
00133 IN const char * pulse,
00134 IN const char * direction,
00135 IN smart_ptr<vgfx::Drawer>& drawer
00136 )
00137 {
00138         ASSERT(type, "null");
00139         // ASSERT(flash) -- can be null
00140         // ASSERT(pulse) -- can be null
00141         // ASSERT(direction) -- can be null
00142         ASSERT(drawer, "null");
00143 
00144         smart_ptr<Simple> local = new Simple;
00145         ASSERT(local, "out of memory");
00146 
00147         local->initialize(type, flash, pulse, direction, drawer);
00148 
00149         return local;
00150 }
00151 
00152 
00153 
00154 ////////////////////////////////////////////////////////////////////////////////
00155 //
00156 //      Simple -- dialog::Element class interface methods
00157 //
00158 ////////////////////////////////////////////////////////////////////////////////
00159 
00160 void
00161 Simple::draw
00162 (
00163 IN const dialog::point_t& offset
00164 )
00165 {
00166         ++m_counter;
00167 
00168         // update intensities
00169         if (s_null != m_flash) {
00170                 int on = (m_counter / 20) % 2;
00171                 float val = (on) ? 1.0 : 0.0;
00172                 m_renderer->setIntensity(m_flash.c_str(), val);
00173 
00174                 bool setPos = false;
00175                 float x = 0.5;
00176                 float y = 0.5;
00177                 if (eDpad_Left & m_direction) {
00178                         setPos = true;
00179                         x = 0;
00180                 }
00181                 if (eDpad_Right & m_direction) {
00182                         setPos = true;
00183                         x = 1;
00184                 }
00185                 if (eDpad_Up & m_direction) {
00186                         setPos = true;
00187                         y = 0;
00188                 }
00189                 if (eDpad_Down & m_direction) {
00190                         setPos = true;
00191                         y = 1;
00192                 }
00193 
00194                 if (setPos) {
00195                         m_renderer->setPosition(m_flash.c_str(), x, y);
00196                 }
00197         }
00198 
00199         if (s_null != m_pulse) {
00200                 int q = (m_counter % 200) - 100;
00201                 if (q < 0) {
00202                         q = -q;
00203                 }
00204                 m_renderer->setIntensity(m_pulse.c_str(), 0.01 * q);
00205         }
00206 
00207         // now draw
00208         m_renderer->draw(offset.x, offset.y, m_width, m_height);
00209 }
00210 
00211 
00212 
00213 ////////////////////////////////////////////////////////////////////////////////
00214 //
00215 //      Simple -- private helper methods
00216 //
00217 ////////////////////////////////////////////////////////////////////////////////
00218 
00219 void
00220 Simple::initialize
00221 (
00222 IN Type * type,
00223 IN const char * flash,
00224 IN const char * pulse,
00225 IN const char * direction,
00226 IN smart_ptr<vgfx::Drawer>& drawer
00227 )
00228 {
00229         ASSERT(type, "null");
00230         // ASSERT(flash) -- can be null
00231         // ASSERT(pulse) -- can be null
00232         // ASSERT(direction) -- can be null
00233         ASSERT(drawer, "null");
00234         ASSERT(!m_type, "already have a type?");
00235 
00236         m_type = type;
00237 
00238         m_flash = (flash) ? flash : s_null;
00239         m_pulse = (pulse) ? pulse : s_null;
00240 
00241         DPRINTF("flash = %s", m_flash.c_str());
00242         DPRINTF("pulse = %s", m_pulse.c_str());
00243 
00244         if (direction) {
00245                 if (!strcmp("up", direction)) {
00246                         m_direction = eDpad_Up;
00247                 } else if (!strcmp("down", direction)) {
00248                         m_direction = eDpad_Down;
00249                 } else if (!strcmp("left", direction)) {
00250                         m_direction = eDpad_Left;
00251                 } else if (!strcmp("right", direction)) {
00252                         m_direction = eDpad_Right;
00253                 } else {
00254                         DPRINTF("Unknown direction, ignoring: %s", direction);
00255                 }
00256         }
00257 
00258         m_renderer = TypeVgfxRenderer::create(type, drawer);
00259         ASSERT(m_renderer, "null");
00260 }
00261 
00262 
00263 
00264 ////////////////////////////////////////////////////////////////////////////////
00265 //
00266 //      SimpleFactory - class that implements the dialog::Factory interface
00267 //                      for gamepad Testing elements
00268 //
00269 ////////////////////////////////////////////////////////////////////////////////
00270 
00271 class SimpleFactory : public dialog::Factory {
00272 public:
00273         // constructor, destructor ---------------------------------------------
00274         ~SimpleFactory(void) throw() { }
00275 
00276         // dialog::Factory class interface methods -----------------------------
00277         smart_ptr<dialog::Element> createElement(IN const char * type,
00278                                 IN dialog::Manager * mgr,
00279                                 IN const Datahash * data);
00280 
00281         // static factory methods ----------------------------------------------
00282         static smart_ptr<dialog::Factory> create(IN smart_ptr<Manager>& mgr,
00283                                 IN smart_ptr<vgfx::Drawer>& drawer) {
00284                         ASSERT(mgr, "null");
00285                         ASSERT(drawer, "null");
00286                         smart_ptr<SimpleFactory> local = new SimpleFactory;
00287                         ASSERT(local, "out of memory");
00288                         local->m_mgr = mgr;
00289                         local->m_drawer = drawer;
00290                         return local;
00291                 }
00292 private:
00293         // private constructors ------------------------------------------------
00294         SimpleFactory(void) throw() { }
00295 
00296         // private typedefs ----------------------------------------------------
00297         // private member data -------------------------------------------------
00298         smart_ptr<Manager>      m_mgr;
00299         smart_ptr<vgfx::Drawer> m_drawer;
00300 };
00301 
00302 
00303 
00304 ////////////////////////////////////////////////////////////////////////////////
00305 //
00306 //      SimpleFactory -- dialog::Factory class interface methods
00307 //
00308 ////////////////////////////////////////////////////////////////////////////////
00309 
00310 smart_ptr<dialog::Element>
00311 SimpleFactory::createElement
00312 (
00313 IN const char * type,
00314 IN dialog::Manager * mgr,
00315 IN const Datahash * data
00316 )
00317 {
00318         ASSERT(type, "null");
00319         ASSERT(mgr, "null");
00320         ASSERT(data, "null");
00321 
00322         const char * typeId = getString(data, "typeId");
00323         ASSERT(typeId, "null");
00324         smart_ptr<Type> gpType = m_mgr->getType(typeId);
00325         ASSERT_THROW(gpType, "Failed to retrieve gamepad type: " << typeId);
00326 
00327         const char * flash = getOptionalString(data, "flash", NULL);
00328         const char * pulse = getOptionalString(data, "pulse", NULL);
00329         const char * direction = getOptionalString(data, "direction", NULL);
00330 
00331         return Simple::create(gpType, flash, pulse, direction, m_drawer);
00332 }
00333 
00334 
00335 
00336 
00337 ////////////////////////////////////////////////////////////////////////////////
00338 //
00339 //      public API
00340 //
00341 ////////////////////////////////////////////////////////////////////////////////
00342 
00343 void
00344 registerVgfxSimpleFactory
00345 (
00346 IN smart_ptr<Manager>& gamepadMgr,
00347 IN dialog::Manager * dialogMgr,
00348 IN smart_ptr<vgfx::Drawer>& drawer
00349 )
00350 {
00351         ASSERT(gamepadMgr, "null");
00352         ASSERT(dialogMgr, "null");
00353         ASSERT(drawer, "null");
00354 
00355         smart_ptr<dialog::Factory> factory =
00356             SimpleFactory::create(gamepadMgr, drawer);
00357         ASSERT(factory, "null");
00358 
00359         dialogMgr->registerFactory(s_simpleElementName, factory);
00360 }
00361 
00362 
00363 
00364 const char *
00365 getVgfxSimpleElementName
00366 (
00367 void
00368 )
00369 throw()
00370 {
00371         return s_simpleElementName;
00372 }
00373 
00374 
00375 
00376 };      // gamepad namespace
00377