[Kde-games-devel] KDE/kdegames/katomic

Dmitry Suzdalev dimsuz at gmail.com
Wed Nov 8 21:31:58 CET 2006


SVN commit 603405 by dimsuz:

Added UndoAll, RedoAll actions.
This interface may change.
Please test and don't hesitate to send me your comments/suggestions.

Parker Coates proposed to also include player like "play" and "pause"
actions to start/stop constantly redoing moves in redo list - one after another.
What do you think about this?
How often would you use such functionality rather than manually pressing "redo"?

P.S. To much buzz for nothing? ;-)

CCMAIL: kde-games-devel at kde.org



 M  +10 -0     gamewidget.cpp  
 M  +2 -1      gamewidget.h  
 M  +13 -0     katomicui.rc  
 M  +82 -2     playfield.cpp  
 M  +7 -0      playfield.h  
 M  +18 -4     toplevel.cpp  


--- trunk/KDE/kdegames/katomic/gamewidget.cpp #603404:603405
@@ -86,6 +86,16 @@
     m_playField->redo();
 }
 
+void GameWidget::undoAll()
+{
+    m_playField->undoAll();
+}
+
+void GameWidget::redoAll()
+{
+    m_playField->redoAll();
+}
+
 void GameWidget::setAnimationSpeed(int speed)
 {
     m_playField->setAnimationSpeed(speed);
--- trunk/KDE/kdegames/katomic/gamewidget.h #603404:603405
@@ -68,7 +68,8 @@
     void previousAtom();
     void doUndo ();
     void doRedo ();
-
+    void undoAll();
+    void redoAll();
  protected:
     PlayFieldView *m_view;
     PlayField *m_playField;
--- trunk/KDE/kdegames/katomic/katomicui.rc #603404:603405
@@ -5,6 +5,19 @@
   <Menu name="settings"><text>&amp;Settings</text>
     <Action name="anim_speed" />
   </Menu>
+  <Menu name="move"><text>&amp;Move</text>
+    <Action name="undo_all" />
+    <Action name="undo" />
+    <Action name="redo" />
+    <Action name="redo_all" />
+  </Menu>
 </MenuBar>
 
+<ToolBar name="mainToolBar"><text>Main Toolbar</text>
+  <Action name="undo_all" />
+  <Action name="undo" />
+  <Action name="redo" />
+  <Action name="redo_all" />
+</ToolBar>
+
 </kpartgui>
--- trunk/KDE/kdegames/katomic/playfield.cpp #603404:603405
@@ -33,6 +33,8 @@
 #include "molecule.h"
 #include "atom.h"
 
+const int MIN_ELEM_SIZE=30;
+
 class FieldGraphicsItem : public QGraphicsPixmapItem
 {
 public:
@@ -63,7 +65,7 @@
 PlayFieldView::PlayFieldView( PlayField* field, QWidget* parent )
     : QGraphicsView(field, parent), m_playField(field)
 {
-
+    setMinimumSize( FIELD_SIZE*MIN_ELEM_SIZE, FIELD_SIZE*MIN_ELEM_SIZE );
 }
 
 void PlayFieldView::resizeEvent( QResizeEvent* ev )
@@ -74,7 +76,7 @@
 // =============== Play Field ========================
 
 PlayField::PlayField( QObject* parent )
-    : QGraphicsScene(parent), m_mol(0), m_numMoves(0), m_elemSize(30), m_selIdx(-1), m_animSpeed(120)
+    : QGraphicsScene(parent), m_mol(0), m_numMoves(0), m_elemSize(MIN_ELEM_SIZE), m_selIdx(-1), m_animSpeed(120)
 {
     m_renderer = new KAtomicRenderer( KStandardDirs::locate("appdata", "pics/default_theme.svgz"), this );
     m_renderer->setElementSize( m_elemSize );
@@ -324,6 +326,84 @@
     moveSelectedAtom(am.dir, am.numCells);
 }
 
+void PlayField::undoAll()
+{
+    while( !m_undoStack.isEmpty() )
+    {
+        AtomMove am = m_undoStack.pop();
+        m_redoStack.push( am );
+
+        // adjust atom pos
+        FieldGraphicsItem *atom = m_atoms.at(am.atomIdx);
+        int xdelta = 0, ydelta = 0;
+        switch(am.dir)
+        {
+            case Up:
+                ydelta = am.numCells;
+                break;
+            case Down:
+                ydelta = -am.numCells;
+                break;
+            case Right:
+                xdelta = -am.numCells;
+                break;
+            case Left:
+                xdelta = am.numCells;
+                break;
+        }
+        atom->setFieldXY( atom->fieldX()+xdelta, atom->fieldY()+ydelta );
+    }
+    // update pixel positions
+    foreach( FieldGraphicsItem* atom, m_atoms )
+        atom->setPos( toPixX(atom->fieldX()), toPixY(atom->fieldY()));
+
+    m_numMoves = 0;
+    emit updateMoves(m_numMoves);
+    emit enableUndo(false);
+    emit enableRedo(!m_redoStack.isEmpty());
+    m_selIdx = m_redoStack.last().atomIdx;
+    updateArrows();
+}
+
+void PlayField::redoAll()
+{
+    while( !m_redoStack.isEmpty() )
+    {
+        AtomMove am = m_redoStack.pop();
+        m_undoStack.push( am );
+
+        // adjust atom pos
+        FieldGraphicsItem *atom = m_atoms.at(am.atomIdx);
+        int xdelta = 0, ydelta = 0;
+        switch(am.dir)
+        {
+            case Up:
+                ydelta = -am.numCells;
+                break;
+            case Down:
+                ydelta = am.numCells;
+                break;
+            case Right:
+                xdelta = am.numCells;
+                break;
+            case Left:
+                xdelta = -am.numCells;
+                break;
+        }
+        atom->setFieldXY( atom->fieldX()+xdelta, atom->fieldY()+ydelta );
+    }
+    // update pixel positions
+    foreach( FieldGraphicsItem* atom, m_atoms )
+        atom->setPos( toPixX(atom->fieldX()), toPixY(atom->fieldY()));
+
+    m_numMoves = m_undoStack.count();
+    emit updateMoves(m_numMoves);
+    emit enableUndo(!m_undoStack.isEmpty());
+    emit enableRedo(false);
+    m_selIdx = m_undoStack.last().atomIdx;
+    updateArrows();
+}
+
 void PlayField::mousePressEvent( QGraphicsSceneMouseEvent* ev )
 {
     if( isAnimating() )
--- trunk/KDE/kdegames/katomic/playfield.h #603404:603405
@@ -85,7 +85,14 @@
      *  Redoes one movement
      */
     void redo();
+      /**
+     *  Undoes all movements
+     */
+    void undoAll();
     /**
+     *  Redoes all movements
+     */
+    void redoAll();   /**
      *  Saves the current game to config object
      */
     void saveGame(KSimpleConfig& config) const;
--- trunk/KDE/kdegames/katomic/toplevel.cpp #603404:603405
@@ -28,13 +28,12 @@
 #include <kstdgameaction.h>
 #include <kselectaction.h>
 #include <kdebug.h>
+#include <kicon.h>
 
 #include "gamewidget.h"
 #include "toplevel.h"
 #include "prefs.h"
 
-// FIXME dimsuz: MERGE GameWidget to AtomTopLevel. No need to separate them.
-
 void AtomTopLevel::createMenu()
 {
     KAction *act = KStdGameAction::highscores(m_gameWid, SLOT(showHighscores()), actionCollection());
@@ -50,12 +49,27 @@
     m_animSpeedAct->setItems(acts);
     connect( m_animSpeedAct, SIGNAL(triggered(int)), SLOT(slotAnimSpeedChanged(int)) );
 
-    m_undoAct = KStdGameAction::undo (m_gameWid, SLOT(doUndo()), actionCollection());
-    m_redoAct = KStdGameAction::redo (m_gameWid, SLOT(doRedo()), actionCollection());
+    KAction *undoAll = new KAction( KIcon("player_start"), i18n("Undo All"), actionCollection(), "undo_all" );
+    connect( undoAll, SIGNAL(triggered(bool)), m_gameWid, SLOT(undoAll()) );
+
+    KAction *redoAll = new KAction( KIcon("player_end"), i18n("Redo All"), actionCollection(), "redo_all" );
+    connect( redoAll, SIGNAL(triggered(bool)), m_gameWid, SLOT(redoAll()) );
+
+    m_undoAct = new KAction( KIcon("undo"), i18n("Undo"), actionCollection(), "undo" );
+    connect( m_undoAct, SIGNAL(triggered(bool)), m_gameWid, SLOT(doUndo()) );
+
+    m_redoAct = new KAction( KIcon("redo"), i18n("Redo"), actionCollection(), "redo" );
+    connect( m_redoAct, SIGNAL(triggered(bool)), m_gameWid, SLOT(doRedo()) );
+
     m_undoAct->setEnabled(false);
     m_redoAct->setEnabled(false);
+    undoAll->setEnabled(false);
+    redoAll->setEnabled(false);
+    
     connect (m_gameWid, SIGNAL (enableRedo(bool)), m_redoAct, SLOT(setEnabled(bool)));
     connect (m_gameWid, SIGNAL (enableUndo(bool)), m_undoAct, SLOT(setEnabled(bool)));
+    connect (m_gameWid, SIGNAL (enableUndo(bool)), undoAll, SLOT(setEnabled(bool)));
+    connect (m_gameWid, SIGNAL (enableRedo(bool)), redoAll, SLOT(setEnabled(bool)));
 
     KAction* atomUpAct = new KAction(i18n("Atom Up"), actionCollection(), "atom_up");
     atomUpAct->setShortcut(Qt::Key_Up);


More information about the kde-games-devel mailing list