From 8c2ff25c3a8f610df40f1a998cc8a2d5c6b24e49 Mon Sep 17 00:00:00 2001 From: Maurizio Monge Date: Wed, 4 Jul 2007 18:28:23 +0100 Subject: [PATCH] Initial implementation of the new abstraction architecture. **BROKEN** --- src/animation.cpp | 46 ++--- src/animation.h | 57 ++++--- src/board.cpp | 10 +- src/board.h | 36 ++-- src/boardsprite.h__ | 25 +++ src/console.h | 2 +- src/element.cpp__ | 26 +++ src/{poolinfo.h => element.h__} | 34 ++-- src/graphicalapi.h | 78 +++++++++ src/graphicalgame.cpp | 15 +- src/graphicalgame.h | 22 +-- src/graphicalsystem.cpp | 314 ++++++++++++++++++++++++++++++++++ src/graphicalsystem.h | 138 +++++++++++++++ src/grid.h | 7 +- src/icsconnection.cpp | 3 +- src/kboard.h | 149 ++++++++--------- src/kboard_fwd.h | 12 +- src/main.cpp | 24 +-- src/namedsprite.h | 30 ++++ src/piecegroup.cpp | 12 +- src/piecegroup.h | 15 +- src/piecepool.cpp | 95 ++++++----- src/piecepool.h | 44 ++--- src/poolinfo.cpp | 3 + src/poolinfo.h | 2 +- src/positioninfo.cpp | 5 +- src/sprite.cpp | 325 ++++++++++++++++++++++++++++++++++++ src/sprite.h | 129 ++++++++++++++ src/unwrapped_graphicalapi.h | 67 ++++++++ src/variants/chess.cpp | 89 +++++++++- src/variants/xchess/animator.h | 6 +- src/variants/xchess/animator.impl.h | 3 +- src/wrappedanimator.h__ | 135 +++++++++++++++ 33 files changed, 1649 insertions(+), 309 deletions(-) create mode 100644 src/boardsprite.h__ create mode 100644 src/element.cpp__ copy src/{poolinfo.h => element.h__} (51%) create mode 100644 src/graphicalapi.h create mode 100644 src/graphicalsystem.cpp create mode 100644 src/graphicalsystem.h create mode 100644 src/namedsprite.h create mode 100644 src/sprite.cpp create mode 100644 src/sprite.h create mode 100644 src/unwrapped_graphicalapi.h create mode 100644 src/wrappedanimator.h__ diff --git a/src/animation.cpp b/src/animation.cpp index 85db04f..36178d5 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -17,7 +17,7 @@ #include "common.h" #include "animation.h" #include "point.h" -#include "piecesprite.h" +#include "sprite.h" #include "movement.h" using namespace std; @@ -27,14 +27,14 @@ using namespace boost; //BEGIN ConcreteAnimation -ConcreteAnimation::ConcreteAnimation(const Sprite& piece) +ConcreteAnimation::ConcreteAnimation(const SpritePtr& piece) : m_piece(piece) { } //END ConcreteAnimation //BEGIN MovementAnimation -MovementAnimation::MovementAnimation(const Sprite& piece, +MovementAnimation::MovementAnimation(const SpritePtr& piece, const QPoint& destination, double speed) : ConcreteAnimation(piece) , m_source(piece) @@ -56,18 +56,18 @@ MovementAnimation::~MovementAnimation() { // m_piece->setMovementAnimation(0); } -void MovementAnimation::setTarget(const Sprite& target) { +void MovementAnimation::setTarget(const SpritePtr& target) { m_target = target; } -void MovementAnimation::setSource(const Sprite& source) { +void MovementAnimation::setSource(const SpritePtr& source) { m_source = source; } boost::shared_ptr MovementAnimation::createMovement(const QPoint& from, const QPoint& to) const { return boost::shared_ptr(new SigmoidalMovement(from, to)); -} +// } void MovementAnimation::start() { #ifdef ANIMATION_DEBUG @@ -162,7 +162,7 @@ void MovementAnimation::abort() { //BEGIN KnightMovementAnimation -KnightMovementAnimation::KnightMovementAnimation(const Sprite& piece, const QPoint& destination, +KnightMovementAnimation::KnightMovementAnimation(const SpritePtr& piece, const QPoint& destination, bool rotate, double speed) : MovementAnimation(piece, destination, speed) , m_rotate(rotate) { @@ -177,7 +177,7 @@ KnightMovementAnimation::createMovement(const QPoint& from, const QPoint& to) co //BEGIN OneShotAnimation -OneShotAnimation::OneShotAnimation(const Sprite& piece) +OneShotAnimation::OneShotAnimation(const SpritePtr& piece) : ConcreteAnimation(piece) { } Animation::State OneShotAnimation::animationAdvance(int) { @@ -189,7 +189,7 @@ Animation::State OneShotAnimation::animationAdvance(int) { //BEGIN InstantAnimation -InstantAnimation::InstantAnimation(const Sprite& piece, const QPoint& destination) +InstantAnimation::InstantAnimation(const SpritePtr& piece, const QPoint& destination) : OneShotAnimation(piece) , m_destination(destination) { } @@ -206,7 +206,7 @@ void InstantAnimation::shoot() { //BEGIN CaptureAnimation -CaptureAnimation::CaptureAnimation(const Sprite& piece) +CaptureAnimation::CaptureAnimation(const SpritePtr& piece) : OneShotAnimation(piece) { } void CaptureAnimation::shoot() { @@ -223,11 +223,11 @@ void CaptureAnimation::shoot() { //BEGIN DropAnimation -DropAnimation::DropAnimation(const Sprite& piece) +DropAnimation::DropAnimation(const SpritePtr& piece) : OneShotAnimation(piece) , m_valid_position(false) { } -DropAnimation::DropAnimation(const Sprite& piece, const QPoint& pos) +DropAnimation::DropAnimation(const SpritePtr& piece, const QPoint& pos) : OneShotAnimation(piece) , m_valid_position(true) , m_position(pos) { } @@ -242,8 +242,8 @@ void DropAnimation::shoot() { //BEGIN PromotionAnimation -PromotionAnimation::PromotionAnimation(const Sprite& piece, - const Sprite& promoted) +PromotionAnimation::PromotionAnimation(const SpritePtr& piece, + const SpritePtr& promoted) : OneShotAnimation(piece) , m_promoted(promoted) { } @@ -256,8 +256,8 @@ void PromotionAnimation::shoot() { //BEGIN CrossFadingAnimation -CrossFadingAnimation::CrossFadingAnimation(const Sprite& piece, - const Sprite& promoted) +CrossFadingAnimation::CrossFadingAnimation(const SpritePtr& piece, + const SpritePtr& promoted) : m_piece(piece) { addPreAnimation(shared_ptr(new FadeAnimation(piece, piece->pos(), 255, 0))); addPreAnimation(shared_ptr(new FadeAnimation(promoted, promoted->pos(), 0, 255))); @@ -313,7 +313,7 @@ void DelayAnimation::abort() { //BEGIN FadeAnimation -FadeAnimation::FadeAnimation(const Sprite& sprite, const QPoint& to, +FadeAnimation::FadeAnimation(const SpritePtr& sprite, const QPoint& to, int fadeFrom, int fadeTo) : ConcreteAnimation(sprite) , m_fadeFrom(fadeFrom) @@ -371,7 +371,7 @@ void FadeAnimation::abort() { //BEGIN GrowAnimation -GrowAnimation::GrowAnimation(const Sprite& sprite) +GrowAnimation::GrowAnimation(const SpritePtr& sprite) : ConcreteAnimation(sprite) , m_state(Inactive) { } @@ -423,7 +423,7 @@ void GrowAnimation::abort() { //BEGIN ExplodeAnimation -ExplodeAnimation::ExplodeAnimation(const Sprite& sprite, Random& random) +ExplodeAnimation::ExplodeAnimation(const SpritePtr& sprite, Random& random) : ConcreteAnimation(sprite) , m_state(Inactive) , m_random(random) { } @@ -592,22 +592,22 @@ void AnimationGroup::stop() { //BEGIN TeleportAnimation -TeleportAnimation::TeleportAnimation(const shared_ptr& sprite, +TeleportAnimation::TeleportAnimation(const SpritePtr& sprite, const QPoint& from, const QPoint& to) : AnimationGroup(1.0) { - shared_ptr copy(sprite->duplicate()); + const SpritePtr& copy(sprite->duplicate()); copy->show(); addPreAnimation(AnimationPtr(new FadeAnimation(copy, from, 255, 0))); addPreAnimation(AnimationPtr(new FadeAnimation(sprite, to, 0, 255))); } -TeleportAnimation::TeleportAnimation(const shared_ptr& sprite, +TeleportAnimation::TeleportAnimation(const const SpritePtr&& sprite, const QPoint& to) : AnimationGroup(1.0) { - shared_ptr copy(sprite->duplicate()); + const SpritePtr& copy(sprite->duplicate()); copy->show(); addPreAnimation(AnimationPtr(new FadeAnimation(copy, copy->pos(), 255, 0))); diff --git a/src/animation.h b/src/animation.h index a143e72..560d965 100644 --- a/src/animation.h +++ b/src/animation.h @@ -16,14 +16,13 @@ #include #include #include +#include "sprite.h" -class PieceSprite; class Movement; class Random; class Animation : public boost::enable_shared_from_this { protected: - typedef boost::shared_ptr Sprite; enum State { Active, Inactive, @@ -41,15 +40,15 @@ public: class ConflictingAnimation { public: virtual ~ConflictingAnimation() { } - virtual void setTarget(const boost::shared_ptr&) = 0; - virtual void setSource(const boost::shared_ptr&) = 0; + virtual void setTarget(const SpritePtr&) = 0; + virtual void setSource(const SpritePtr&) = 0; }; class ConcreteAnimation : public Animation { protected: - boost::shared_ptr m_piece; + SpritePtr m_piece; public: - ConcreteAnimation(const Sprite& piece); + ConcreteAnimation(const SpritePtr& piece); virtual void stop() { } }; @@ -57,7 +56,7 @@ class OneShotAnimation : public ConcreteAnimation { protected: virtual void shoot() = 0; public: - OneShotAnimation(const Sprite& piece); + OneShotAnimation(const SpritePtr& piece); virtual State animationAdvance(int); virtual void abort() { } }; @@ -65,20 +64,20 @@ public: class InstantAnimation : public OneShotAnimation , public ConflictingAnimation { QPoint m_destination; - Sprite m_source; + SpritePtr m_source; public: - InstantAnimation(const Sprite& piece, const QPoint& destination); + InstantAnimation(const SpritePtr& piece, const QPoint& destination); virtual void shoot(); - virtual void setTarget(const Sprite&) { } - virtual void setSource(const Sprite& source) { m_source = source; } + virtual void setTarget(const SpritePtr&) { } + virtual void setSource(const SpritePtr& source) { m_source = source; } }; class MovementAnimation : public ConcreteAnimation , public ConflictingAnimation { protected: - Sprite m_source; - Sprite m_target; + SpritePtr m_source; + SpritePtr m_target; QPoint m_destination; double m_speed; State m_state; @@ -90,14 +89,14 @@ protected: virtual boost::shared_ptr createMovement(const QPoint& from, const QPoint& to) const; void start(); public: - MovementAnimation(const Sprite& piece, const QPoint& destination, double speed = 1.0); + MovementAnimation(const SpritePtr& piece, const QPoint& destination, double speed = 1.0); virtual ~MovementAnimation(); virtual State animationAdvance(int msec); virtual void stop(); virtual void abort(); - virtual void setTarget(const Sprite& target); - virtual void setSource(const Sprite& source); + virtual void setTarget(const SpritePtr& target); + virtual void setSource(const SpritePtr& source); }; class KnightMovementAnimation : public MovementAnimation { @@ -105,7 +104,7 @@ protected: bool m_rotate; virtual boost::shared_ptr createMovement(const QPoint& from, const QPoint& to) const; public: - KnightMovementAnimation(const Sprite& piece, const QPoint& destination, + KnightMovementAnimation(const SpritePtr& piece, const QPoint& destination, bool rotate, double speed = 1.0); }; @@ -113,7 +112,7 @@ class CaptureAnimation : public OneShotAnimation { protected: virtual void shoot(); public: - CaptureAnimation(const Sprite& piece); + CaptureAnimation(const SpritePtr& piece); }; class DropAnimation : public OneShotAnimation { @@ -122,16 +121,16 @@ class DropAnimation : public OneShotAnimation { protected: virtual void shoot(); public: - DropAnimation(const Sprite& piece); - DropAnimation(const Sprite& piece, const QPoint&); + DropAnimation(const SpritePtr& piece); + DropAnimation(const SpritePtr& piece, const QPoint&); }; class PromotionAnimation : public OneShotAnimation { protected: - Sprite m_promoted; + SpritePtr m_promoted; virtual void shoot(); public: - PromotionAnimation(const Sprite& piece, const Sprite& promoted); + PromotionAnimation(const SpritePtr& piece, const SpritePtr& promoted); }; class DelayAnimation : public Animation { @@ -156,7 +155,7 @@ class FadeAnimation : public ConcreteAnimation { int m_start; void start(); public: - FadeAnimation(const Sprite& sprite, const QPoint& pos, int fadeFrom, int fadeTo); + FadeAnimation(const SpritePtr& sprite, const QPoint& pos, int fadeFrom, int fadeTo); virtual State animationAdvance(int msec); virtual void stop(); virtual void abort(); @@ -168,7 +167,7 @@ class GrowAnimation : public ConcreteAnimation { void start(); public: - GrowAnimation(const Sprite& sprite); + GrowAnimation(const SpritePtr& sprite); virtual State animationAdvance(int msec); virtual void stop(); virtual void abort(); @@ -181,7 +180,7 @@ class ExplodeAnimation : public ConcreteAnimation { int m_start; void start(); public: - ExplodeAnimation(const Sprite& sprite, Random& random); + ExplodeAnimation(const SpritePtr& sprite, Random& random); virtual State animationAdvance(int msec); virtual void stop(); virtual void abort(); @@ -227,16 +226,16 @@ public: class TeleportAnimation : public AnimationGroup { public: - TeleportAnimation(const Sprite& sprite, const QPoint& from, const QPoint& to); - TeleportAnimation(const Sprite& sprite, const QPoint& to); + TeleportAnimation(const SpritePtr& sprite, const QPoint& from, const QPoint& to); + TeleportAnimation(const SpritePtr& sprite, const QPoint& to); }; class CrossFadingAnimation : public AnimationGroup { - Sprite m_piece; + SpritePtr m_piece; protected: virtual void start(); public: - CrossFadingAnimation(const Sprite& piece, const Sprite& promoted); + CrossFadingAnimation(const SpritePtr& piece, const SpritePtr& promoted); }; class DelayedAnimationSet : public Animation { diff --git a/src/board.cpp b/src/board.cpp index f104811..3307e95 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -15,7 +15,7 @@ #include "global.h" #include "board.h" -#include "piecesprite.h" +#include "sprite.h" #include "animation.h" #include "boardsprite.h" #include "pointconverter.h" @@ -278,7 +278,7 @@ void Board::cancelPremove() { void Board::updateSprites() { // adjust piece positions for (Point i = m_sprites.first(); i <= m_sprites.last(); i = m_sprites.next(i)) { - boost::shared_ptr p = m_sprites[i].sprite(); + boost::shared_ptr p = m_sprites[i].sprite(); if (p) { // drawing sprite @@ -350,7 +350,7 @@ void Board::onMousePress(const QPoint& pos, int button) { doMove(m); } else { - shared_ptr piece = m_sprites[point].sprite(); + shared_ptr piece = m_sprites[point].sprite(); if (piece && m_entity.lock()->movable(point)) { cancelSelection(); @@ -451,7 +451,7 @@ void Board::onMouseRelease(const QPoint& pos, int button) { } } - shared_ptr s = m_sprites[m_drag_info->from].sprite(); + shared_ptr s = m_sprites[m_drag_info->from].sprite(); if (!moved && s && s->pos() != converter()->toReal(m_drag_info->from)) { Q_ASSERT(s); QPoint real = converter()->toReal(m_drag_info->from); @@ -568,7 +568,7 @@ void Board::updateHinting(Point pt, AbstractPiece::Ptr piece) { } QPixmap pix = m_loader(piece->name()); - boost::shared_ptr sprite = createSprite(pix, pt); + boost::shared_ptr sprite = createSprite(pix, pt); sprite->setOpacity(160); sprite->raise(); sprite->show(); diff --git a/src/board.h b/src/board.h index 7da7b38..69569c7 100644 --- a/src/board.h +++ b/src/board.h @@ -8,8 +8,8 @@ (at your option) any later version. */ -#ifndef CHESSBOARDWIDGET_H_ -#define CHESSBOARDWIDGET_H_ +#ifndef BOARD_H +#define BOARD_H #include #include @@ -23,10 +23,10 @@ #include "grid.h" #include "usermove.h" #include "piecegroup.h" +#include "namedsprite.h" class DragInfo; class Animation; -class BoardSprite; class UserEntity; class BoardTags; @@ -38,33 +38,21 @@ typedef boost::shared_ptr BoardTagsPtr; * * This class is a Canvas::Item that displaying a board filled with pieces. * You can set custom tags for each square. - * @sa Table, @sa PiecePool, @sa PieceSprite + * @sa Table, @sa PiecePool, @sa Sprite */ class Board : public QObject, public PieceGroup { Q_OBJECT public: - friend class GraphicalInfo; + friend class GraphicalSystem; friend class PiecePool; - - class BoardSprite { - public: - QString m_name; - boost::shared_ptr m_sprite; - - BoardSprite() {} - BoardSprite(const QString& name, boost::shared_ptr s) - : m_name(name), m_sprite(s) {} - QString name(){ return m_name; } - boost::shared_ptr sprite(){ return m_sprite; } - }; - typedef Grid PieceGrid; + typedef Grid PieceGrid; private: class DragInfo { public: static const int DRAG_THRESHOLD = 100; // pixels ^ 2 - boost::shared_ptr sprite; + boost::shared_ptr sprite; Point from; // logical coordinates QPoint real; /// real starting point, used to honour drag threshold bool dragging; @@ -73,7 +61,7 @@ private: UserEntity::Action action; DragInfo(Point from, const QPoint& real, - const boost::shared_ptr& sprite, + const boost::shared_ptr& sprite, UserEntity::Action action) : sprite(sprite) , from(from) @@ -95,10 +83,10 @@ private: /** used by a PiecePool to make available the piece that is being dropped on the board to the GraphicalInfo and the variant-specific animator */ - Element m_drop_sprite; + NamedSprite m_drop_sprite; /** the visual move hint */ - Element m_hinting; + NamedSprite m_hinting; Point m_hinting_pos; /** the canvas group that holds the pieces */ @@ -182,7 +170,7 @@ private: bool doMove(const NormalUserMove&); /** fetch the sprite */ - boost::shared_ptr spriteAt(const Point& p) { return m_sprites[p].sprite(); } + boost::shared_ptr spriteAt(const Point& p) { return m_sprites[p].sprite(); } public: /** constructor, requires the canvas parent */ @@ -267,4 +255,4 @@ signals: void error(ErrorCode code); }; -#endif // CHESSBOARDWIDGET_H_ +#endif //BOARD_H diff --git a/src/boardsprite.h__ b/src/boardsprite.h__ new file mode 100644 index 0000000..838ba0c --- /dev/null +++ b/src/boardsprite.h__ @@ -0,0 +1,25 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef BOARDSPRITE_H +#define BOARDSPRITE_H + +#include +#include "canvas/canvas.h" +#include "point.h" + +class BoardSprite : public Canvas::Pixmap { + public: + BoardSprite(QObject* /*parent*/, const QPixmap& pix, Canvas::Abstract* canvas) + : Canvas::Pixmap(pix, canvas) { + } +}; + +#endif // BOARDSPRITE_H diff --git a/src/console.h b/src/console.h index 12862a7..48b0920 100644 --- a/src/console.h +++ b/src/console.h @@ -1,7 +1,7 @@ /* Copyright (c) 2006 Paolo Capriotti (c) 2006 Maurizio Monge - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or diff --git a/src/element.cpp__ b/src/element.cpp__ new file mode 100644 index 0000000..a4090ee --- /dev/null +++ b/src/element.cpp__ @@ -0,0 +1,26 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "element.h" + +Element::Element() { } + +Element::Element(AbstractPiece::Ptr piece, SpritePtr sprite) +: m_piece(piece) //NOTE: clone?!?!??!?!?! +, m_sprite(sprite) { } + +Element::operator bool() const { + return m_piece; +} + +AbstractPiece::Ptr Element::piece() const { return m_piece; } +Element::SpritePtr Element::sprite() const { return m_sprite; } + + diff --git a/src/poolinfo.h b/src/element.h__ similarity index 51% copy from src/poolinfo.h copy to src/element.h__ index 86328f4..ddba100 100644 --- a/src/poolinfo.h +++ b/src/element.h__ @@ -8,25 +8,27 @@ (at your option) any later version. */ -#ifndef POOLINFO_H -#define POOLINFO_H +#ifndef ELEMENT_H +#define ELEMENT_H -#include -#include -#include "index.h" #include "kboard.h" -#include "icsgamedata.h" -class PoolInfo { -public: - bool m_valid; - int m_game_num; - Index m_pos_index; - AbstractPosition::PoolPtr m_pool; - AbstractPiece::Ptr m_added_piece; - static QRegExp s_pattern; +class PieceSprite; - PoolInfo(const std::map& games, const QString& s); +struct Element { + typedef boost::shared_ptr SpritePtr; +private: + AbstractPiece::Ptr m_piece; + SpritePtr m_sprite; +public: + Element(); + Element(AbstractPiece::Ptr piece, SpritePtr sprite); + + operator bool() const; + + AbstractPiece::Ptr piece() const; + SpritePtr sprite() const; }; -#endif //POOLINFO_H + +#endif // ELEMENT_H diff --git a/src/graphicalapi.h b/src/graphicalapi.h new file mode 100644 index 0000000..3ade273 --- /dev/null +++ b/src/graphicalapi.h @@ -0,0 +1,78 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef GRAPHICALAPI_H +#define GRAPHICALAPI_H + +#include +#include "kboard.h" +#include "pointconverter.h" + +typedef boost::shared_ptr SpritePtr; + +/** + * This class defines the interface that will be used by the animator to modify + * kboard graphics. + */ +class GraphicalAPI { +public: + virtual ~GraphicalAPI(){} + + /** + * \return the current abstract position. + */ + virtual const PointConverter* converter() const = 0; + + /** + * \return the current abstract position. + */ + virtual AbstractPosition::Ptr position() const = 0; + + /** + * \return a sprite at the position \a index in the graphical pool. + */ + virtual SpritePtr getSprite(const Point& p) = 0; + + /** + * Removes a sprite at the position \a index in the graphical pool. + * \return the newly created sprite. + */ + virtual SpritePtr takeSprite(const Point& p) = 0; + + /** + * Sets the sprite at the position \a index in the graphical pool. + * \return the newly created sprite. + */ + virtual SpritePtr setSprite(const Point& p, const AbstractPiece* piece, bool use_drop, bool show) = 0; + + /** + * \return how many sprites are contained in the pool + */ + virtual int poolSize(int pool) = 0; + + /** + * \return the sprite at the position \a index in the graphical pool. + */ + virtual SpritePtr getPoolSprite(int pool, int index) = 0; + + /** + * Removes the sprite at the position \a index in the graphical pool. + * \return the removed sprite. + */ + virtual SpritePtr takePoolSprite(int pool, int index) = 0; + + /** + * Inserts a sprite at the position \a index in the graphical pool. + * \return the newly created sprite. + */ + virtual SpritePtr insertPoolSprite(int pool, int index, const AbstractPiece* piece) = 0; +}; + +#endif //GRAPHICALAPI_H diff --git a/src/graphicalgame.cpp b/src/graphicalgame.cpp index 6a926cb..24fc2a5 100644 --- a/src/graphicalgame.cpp +++ b/src/graphicalgame.cpp @@ -13,7 +13,7 @@ #include "game.h" #include "game_p.h" #include "global.h" -#include "graphicalinfo.h" +#include "graphicalsystem.h" #include "movelist_table.h" #include "decoratedmove.h" #include "entities/userentity.h" @@ -21,10 +21,6 @@ using namespace GamePrivate; // is this ok? -void GraphicalGameProxy::settingsChanged() { - m_game->settingsChanged(); -} - class CtrlAction { Game* m_game; bool m_done; @@ -43,7 +39,7 @@ public: } }; -GraphicalGame::GraphicalGame(GraphicalInfo* graphical, +GraphicalGame::GraphicalGame(GraphicalSystem* graphical, MoveList::Table* m) : Game() , m_graphical(graphical) @@ -55,8 +51,7 @@ GraphicalGame::GraphicalGame(GraphicalInfo* graphical, m_movelist->setNotifier( static_cast(this) ); m_movelist->show(); } - m_proxy = new GraphicalGameProxy(this); - settings.onChange(m_proxy, SLOT(settingsChanged())); + settings.onChange(this, SLOT(settingsChanged())); settingsChanged(); } @@ -66,13 +61,15 @@ GraphicalGame::~GraphicalGame() { m_movelist->setNotifier(NULL, false); } - delete m_proxy; } void GraphicalGame::settingsChanged() { + //BROKEN + #if 0 m_anim_sequence = m_graphical->getBoolSetting("animations", true) && m_graphical->getBoolSetting("animations.sequence", true); m_anim_sequence_max = m_graphical->getIntSetting("animations.sequence.max", 10); + #endif } void GraphicalGame::onAdded(const Index& ix) { diff --git a/src/graphicalgame.h b/src/graphicalgame.h index f023c72..1bb1d9c 100644 --- a/src/graphicalgame.h +++ b/src/graphicalgame.h @@ -16,39 +16,29 @@ #include "game.h" #include "movelist_notifier.h" -class GraphicalInfo; +class GraphicalSystem; class CtrlAction; class UserEntity; class GraphicalGame; namespace MoveList { class Table; } -class GraphicalGameProxy : public QObject { +class GraphicalGame : public QObject, public Game, public MoveList::Notifier { Q_OBJECT - friend class GraphicalGame; - GraphicalGame *m_game; - GraphicalGameProxy(GraphicalGame *i) : QObject(), m_game(i) {} - -public slots: - void settingsChanged(); -}; - -class GraphicalGame : public Game, public MoveList::Notifier { private: - friend class GraphicalGameProxy; - GraphicalInfo* m_graphical; - MoveList::Table* m_movelist; - GraphicalGameProxy* m_proxy; + GraphicalSystem* m_graphical; + MoveList::Table* m_movelist; bool m_anim_sequence; int m_anim_sequence_max; boost::shared_ptr m_ctrl; boost::weak_ptr m_listener_entity; +private slots: void settingsChanged(); public: - GraphicalGame(GraphicalInfo* graphical, MoveList::Table* m); + GraphicalGame(GraphicalSystem* graphical, MoveList::Table* m); ~GraphicalGame(); void onAddedInternal(const Index& i, bool confirm_promotion = false); diff --git a/src/graphicalsystem.cpp b/src/graphicalsystem.cpp new file mode 100644 index 0000000..67a748b --- /dev/null +++ b/src/graphicalsystem.cpp @@ -0,0 +1,314 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "graphicalsystem.h" +#include "chesstable.h" +#include "board.h" +#include "piecepool.h" +#include "pointconverter.h" +#include "sprite.h" +#include "animation.h" +#include "pref_theme.h" +#include "movelist_table.h" +#include "global.h" + +using namespace boost; + +//BEGIN GraphicalSystem + +GraphicalSystem::GraphicalSystem(ChessTable* view, + AbstractPosition::Ptr startingPosition, + VariantInfo* variant) +: m_view(view) +, m_variant(variant) { + + m_pos = startingPosition->clone(); + Point s = m_pos->size(); + for(int i=0;iset(Point(i,j), AbstractPiece::Ptr()); + + m_board = view->board(); + + m_board->createGrid(m_pos->size(), m_pos->borderCoords()); + m_board->reset(); + + m_view->pool(0)->clear(); + m_view->pool(1)->clear(); + + m_animator = m_variant->createAnimator(this); + + //settings.onChange(m_proxy, SLOT(settingsChanged())); + //settingsChanged(); + + if (startingPosition) + warp(AbstractMove::Ptr(), startingPosition); +} + +GraphicalSystem::~GraphicalSystem() { +} + +void GraphicalSystem::settingsChanged() { + + /* recreate the animator to reload its settings */ + m_animator = m_variant->createAnimator(this); + + QString theme = PrefTheme::getBestTheme(m_variant); + QString sqtheme = PrefTheme::getBestTheme(m_variant, PrefTheme::Squares); + QString figtheme = PrefTheme::getBestTheme(m_variant, PrefTheme::Figurines); + + m_board->loader()->setBasePath( theme ); + m_board->tagsLoader()->setBasePath( sqtheme ); + + m_view->pool(0)->loader()->setBasePath( theme ); + m_view->pool(1)->loader()->setBasePath( theme ); + + m_view->moveListTable()->setLoaderBasePath( figtheme ); + m_view->moveListTable()->settingsChanged(); + + //clear board and pool, forcing reload + m_view->settingsChanged(); +} + +void GraphicalSystem::setup(const shared_ptr& entity) { + m_view->setEntity(entity); +} + +SpritePtr GraphicalSystem::getSprite(const Point& p) { + if (!m_board->m_sprites.valid(p)) + return shared_ptr(); + + return m_board->m_sprites[p].sprite(); +} + +SpritePtr GraphicalSystem::takeSprite(const Point& p) { + if (!m_board->m_sprites.valid(p)) + return shared_ptr(); + + SpritePtr retv = m_board->m_sprites[p].sprite(); + m_board->m_sprites[p] = NamedSprite(); + return retv; +} + +SpritePtr GraphicalSystem::setSprite(const Point& p, const AbstractPiece* piece, bool usedrop, bool show) { + Q_ASSERT(piece); + if(!m_board->m_sprites.valid(p)) + return SpritePtr(); + + QPixmap px = m_board->m_loader(piece->name()); + + SpritePtr s; + if(usedrop && m_board->m_drop_sprite) { + s = m_board->m_drop_sprite.sprite(); + m_board->m_drop_sprite = NamedSprite(); + } + else { + s = m_board->createSprite(px, p); + if (show) s->show(); + } + m_board->m_sprites[p] = NamedSprite(piece->name(), s); + return s; +} + + +int GraphicalSystem::poolSize(int pool) { + return m_view->pool(pool)->fill(); +} + +SpritePtr GraphicalSystem::getPoolSprite(int pool, int index) { + return m_view->pool(pool)->getSprite(index); +} + +SpritePtr GraphicalSystem::takePoolSprite(int pool, int index) { + return m_view->pool(pool)->takeSprite(index); +} + +SpritePtr GraphicalSystem::insertPoolSprite(int pool, int index, const AbstractPiece* piece) { + PiecePool *pl = m_view->pool(pool); + QPixmap px = pl->m_loader(piece->name()); + + SpritePtr s = SpritePtr( new Sprite( px, pl->piecesGroup(), QPoint() ) ); + pl->insertSprite(index, NamedSprite(piece->name(), s) ); + return s; +} + +#if 0 +void GraphicalSystem::updatePool(AbstractPosition::PoolPtr pool) { + + AbstractPosition::PoolPtr curr = m_pos->pool(); + + AbstractPosition::AbstractPool::iterator oldit = curr->begin(); + AbstractPosition::AbstractPool::iterator newit = pool->begin(); + + while(oldit != curr->end() || newit != pool->end()) { + if(newit == pool->end() || (oldit != curr->end() + && oldit->first->less(newit->first) )) { + removeFromPool(oldit->first, oldit->second); + ++oldit; + } + else if (oldit == curr->end() || (newit != pool->end() + && newit->first->less(oldit->first) )) { + addToPool(newit->first, newit->second); + ++newit; + } + else { + Q_ASSERT(newit->first->equals(oldit->first)); + if(oldit->second < newit->second) + addToPool(newit->first, newit->second - oldit->second); + else if(oldit->second > newit->second) + removeFromPool(newit->first, oldit->second - newit->second); + ++newit; + ++oldit; + } + } +} + +void GraphicalSystem::addToPool(AbstractPiece::Ptr piece, int n) { + PiecePool *pool = m_view->pool(!piece->color()); + QPixmap px = pool->m_loader(piece->name()); + + for(int i=0;ipiecesGroup(), QPoint() ) ); + pool->addPiece(Element(piece, s)); + } + + m_pos->addToPool(piece, n); +} + +void GraphicalSystem::removeFromPool(AbstractPiece::Ptr piece, int n) { + PiecePool *pool = m_view->pool(!piece->color()); + + for(int i=0;itakePiece(piece); + m_pos->removeFromPool(piece, n); +} +#endif + +#if 0 +void GraphicalSystem::addTag(const QString& name, Point pt, bool over) { + m_board->addTag(name, pt, over); +} + +void GraphicalSystem::clearTags(const QString& name) { + m_board->clearTags(name); +} + +void GraphicalSystem::setTags(const QString& name, Point p1, Point p2, + Point p3, Point p4, Point p5, Point p6 ) { + m_board->setTags(name, p1, p2, p3, p4, p5, p6); +} +#endif + +#if 0 +bool GraphicalSystem::consistent() const { + for (Point i = first(); i <= last(); i = next(i)) { + Element e = getElement(i); + if (static_cast(e.piece()) ^ + static_cast(e.sprite())) return false; + } + return true; +} +#endif + +#if 0 +Point GraphicalSystem::first() const { return m_board->m_sprites.first(); } +Point GraphicalSystem::last() const { return m_board->m_sprites.last(); } +Point GraphicalSystem::next(const Point& p) const { return m_board->m_sprites.next(p); } +bool GraphicalSystem::valid(const Point& p) const { return m_board->m_sprites.valid(p); } +#endif + +void GraphicalSystem::forward(const AbstractMove::Ptr& move, + const AbstractPosition::Ptr& pos, + const SpritePtr& /*movingSprite*/) { + AbstractPiece::Ptr sel1 = m_pos->get(m_board->selection); + + if (move) { + shared_ptr animation = m_animator->forward(pos, move); + animation->setChainAbortions(false); + m_board->enqueue(animation); + m_board->setTags("highlighting", move->toUserMove().from, move->toUserMove().to); + } + else + warp(AbstractMove::Ptr(), pos); + + AbstractPiece::Ptr sel2 = m_pos->get(m_board->selection); + if(!(sel1 && sel2 && sel1->equals(sel2))) + m_board->cancelSelection(); + + m_board->cancelPremove(); + m_view->updateTurn(pos->turn()); + m_board->onPositionChanged(); +} + +void GraphicalSystem::back(const AbstractMove::Ptr& lastMove, + const AbstractMove::Ptr& move, + const AbstractPosition::Ptr& pos) { + AbstractPiece::Ptr sel1 = m_pos->get(m_board->selection); + + if (move) { + shared_ptr animation = m_animator->back(pos, move); + animation->setChainAbortions(false); + m_board->enqueue(animation); + } + else + warp(lastMove, pos); + + if (lastMove) + m_board->setTags("highlighting", lastMove->toUserMove().from, lastMove->toUserMove().to); + else + m_board->clearTags("highlighting"); + + AbstractPiece::Ptr sel2 = m_pos->get(m_board->selection); + if(!(sel1 && sel2 && sel1->equals(sel2))) + m_board->cancelSelection(); + m_board->cancelPremove(); + m_view->updateTurn(pos->turn()); + m_board->onPositionChanged(); +} + +void GraphicalSystem::warp(const AbstractMove::Ptr& lastMove, + const AbstractPosition::Ptr& pos) { + + AbstractPiece::Ptr sel1 = m_pos->get(m_board->selection); + shared_ptr animation = m_animator->warp(pos); + animation->setChainAbortions(false); + + m_board->enqueue(animation); + + if (lastMove) + m_board->setTags("highlighting", lastMove->toUserMove().from, lastMove->toUserMove().to); + else + m_board->clearTags("highlighting"); + + AbstractPiece::Ptr sel2 = m_pos->get(m_board->selection); + if(!(sel1 && sel2 && sel1->equals(sel2))) + m_board->cancelSelection(); + m_view->updateTurn(pos->turn()); + m_board->onPositionChanged(); +} + +void GraphicalSystem::adjustSprite(const Point& p) { + SpritePtr sprite = m_board->m_sprites[p].sprite(); + Q_ASSERT(sprite); + QPoint destination = m_board->converter()->toReal(p); + shared_ptr animation(new MovementAnimation(sprite, destination)); + m_board->enqueue(animation); +} + +void GraphicalSystem::setTurn(int turn) { + m_pos->setTurn(turn); + m_view->updateTurn(m_pos->turn()); +} + +#include "graphicalsystem.moc" + + + + diff --git a/src/graphicalsystem.h b/src/graphicalsystem.h new file mode 100644 index 0000000..ac3d20c --- /dev/null +++ b/src/graphicalsystem.h @@ -0,0 +1,138 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef GRAPHICALSYSTEM_H +#define GRAPHICALSYSTEM_H + +#include +#include +#include "graphicalapi.h" + +class ChessTable; +class Board; +class UserEntity; + +class GraphicalSystem : public QObject, private GraphicalAPI { +Q_OBJECT + +public: + + /** The current chess table */ + ChessTable* m_view; + + /** The current board */ + Board* m_board; + + /** The position data */ + AbstractPosition::Ptr m_pos; + + /** The variant specific animator */ + AbstractAnimator::Ptr m_animator; + + /** The current variant */ + VariantInfo* m_variant; + + /** @a GraphicalPosition interface function implementation */ +// virtual void addTag(const QString& name, Point, bool over = false); + + /** @a GraphicalPosition interface function implementation */ +// virtual void clearTags(const QString& name); + + /** @a GraphicalPosition interface function implementation */ +// virtual void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(), +// Point p3 = Point::invalid(), Point p4 = Point::invalid(), +// Point p5 = Point::invalid(), Point p6 = Point::invalid() ); + +private slots: + /** internal function to listen at setting changes */ + void settingsChanged(); + + /** + * \return the current abstract position. (interface for GraphicalAPI) + */ + virtual const PointConverter* converter() const; + + /** + * \return the current abstract position. (interface for GraphicalAPI) + */ + virtual AbstractPosition::Ptr position() const; + + /** + * \return a sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) + */ + virtual SpritePtr getSprite(const Point& p); + + /** + * Removes a sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) + * \return the newly created sprite. + */ + virtual SpritePtr takeSprite(const Point& p); + + /** + * Sets the sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) + * \return the newly created sprite. + */ + virtual SpritePtr setSprite(const Point& p, const AbstractPiece* piece, bool use_drop, bool show); + + /** + * \return how many sprites are contained in the pool + */ + virtual int poolSize(int pool) = 0; + + /** + * \return the sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) + */ + virtual SpritePtr getPoolSprite(int pool, int index); + + /** + * Removes the sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) + * \return the removed sprite. + */ + virtual SpritePtr takePoolSprite(int pool, int index); + + /** + * Inserts a sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) + * \return the newly created sprite. + */ + virtual SpritePtr insertPoolSprite(int pool, int index, const AbstractPiece* piece); + + +public: + /** Constructor */ + GraphicalSystem(ChessTable* view, AbstractPosition::Ptr startingPosition, + VariantInfo* variant); + virtual ~GraphicalSystem(); + + /** Sets the reference entity */ + void setup(const boost::shared_ptr& entity); + + /** Goes forward playing a move, that has to be legal and checked */ + void forward(const AbstractMove::Ptr& move, + const AbstractPosition::Ptr& pos, + const SpritePtr& = SpritePtr()); + + /** Goes back undoing a move, that has to be legal and checked */ + void back(const AbstractMove::Ptr& lastMove, + const AbstractMove::Ptr& move, + const AbstractPosition::Ptr& pos); + + /** Warps to a new position */ + void warp(const AbstractMove::Ptr& lastMove, + const AbstractPosition::Ptr& pos); + + /** Adjusts a sprite to the correct position */ + void adjustSprite(const Point& p); + + /** Sets the current turn */ + void setTurn(int turn); +}; + + +#endif //GRAPHICALSYSTEM_H diff --git a/src/grid.h b/src/grid.h index afc89b6..3e12119 100644 --- a/src/grid.h +++ b/src/grid.h @@ -71,8 +71,11 @@ public: return board[p.x + p.y * sizeX]; } - const Point first() const { return Point(0,0); } - const Point last() const { return Point(sizeX-1, sizeY-1); } + /** \return the nth point in the grid, that is not guaranteed to be valid */ + Point nTh(int index) const { return Point(index%sizeX, index/sizeX); } + + Point first() const { return Point(0,0); } + Point last() const { return Point(sizeX-1, sizeY-1); } Point next(const Point& p) const { if (p.x >= sizeX-1) diff --git a/src/icsconnection.cpp b/src/icsconnection.cpp index 62f3790..7f1ee82 100644 --- a/src/icsconnection.cpp +++ b/src/icsconnection.cpp @@ -310,7 +310,8 @@ void ICSConnection::process(QString str) { p->setup(); } if(m_move_list_pool_info) { - p->setPool(m_move_list_pool_info->m_pool); + //BROKEN + //p->setPool(m_move_list_pool_info->m_pool); } PGN pgn(m_move_list); diff --git a/src/kboard.h b/src/kboard.h index 7a73137..7380b66 100644 --- a/src/kboard.h +++ b/src/kboard.h @@ -11,6 +11,10 @@ #ifndef LOWLEVEL_H #define LOWLEVEL_H + +class GraphicalAPI; + + /** * @file kboard.h * @brief Low level abstract classes used by the interface framework. @@ -68,65 +72,17 @@ public: virtual bool equals(AbstractPiece::Ptr other) const = 0; /** - * Piece compare. Return true if less than other. - */ - virtual bool less(AbstractPiece::Ptr other) const = 0; - - /** - * Return the piece type id. It should be a nonnegative - * number, or -1 if the type concept for this piece - * is not applicable. - */ - virtual int type() const = 0; - - /** - * Return the piece color id. It should be a nonnegative - * number, or -1 if the color concept for this piece - * is not applicable. - */ - virtual int color() const = 0; - - /** - * Return a unique id for the piece - * (type, color) pair. Used to store - * pixmaps. - */ -// virtual int id() const = 0; - - /** * Return a unique key for the piece. * Used to store pixmaps. */ virtual QString name() const = 0; /** - * Return the symbol used to identify the piece - * in a move. - */ -// virtual QString typeSymbol() const = 0; - - /** - * Return the name used to load the - * piece pixmap. - * OBSOLETE - */ -// virtual QString name() const = 0; - - /** * Create a deep copy of the piece. */ virtual AbstractPiece::Ptr clone() const = 0; }; -/** - * @brief A class to compare two pieces. - */ -class AbstractPieceComparer { -public: - bool operator()(AbstractPiece::Ptr x, AbstractPiece::Ptr y) { - return x->less(y); - } -}; /** * @brief A superclass for all the move classes. @@ -160,10 +116,50 @@ public: */ virtual NormalUserMove toUserMove() const = 0; + /** + * Checks if the two moves are equal. + */ virtual bool equals(Ptr other) const = 0; }; + +/** + * @brief Superclass for pools + * + * A general interface for pools. + */ +class AbstractPool { +public: + typedef boost::shared_ptr Ptr; + virtual ~AbstractPool() {} + + /** + * \return the number of items in the pool + */ + virtual int size(); + + /** + * Inserts a piece in the pool, preferably at the position \a pref_index. + * But the pool can be unpredictable and the piece can be placed at an arbitrary position. + * \return the position at which the item was placed. + */ + virtual int insert(int pref_index, AbstractPiece::Ptr piece); + + /** + * Gets the piece at the position \a index in the pool. + */ + virtual AbstractPiece::Ptr get(int index); + + /** + * Removes the piece at the position \a index in the pool. + * \return the removed piece. + */ + virtual AbstractPiece::Ptr take(int index); +}; + + + /** * @brief A superclass for all the position classes. * @@ -174,7 +170,6 @@ public: class AbstractPosition { public: typedef boost::shared_ptr Ptr; - typedef std::map AbstractPool; typedef boost::shared_ptr PoolPtr; virtual ~AbstractPosition() { } @@ -195,31 +190,6 @@ public: virtual void setup() = 0; /** - * Retrieve the pieces in the piece pool. - */ - virtual PoolPtr pool() const = 0; - - /** - * Add a piece to the pool n times. - */ - virtual void addToPool(AbstractPiece::Ptr piece, int n) = 0; - - /** - * Remove a piece from the pool n times. - */ - virtual void removeFromPool(AbstractPiece::Ptr piece, int n) = 0; - - /** - * Copies the pool of a position. - */ - virtual void copyPoolFrom(AbstractPosition::Ptr pos) = 0; - - /** - * Sets the pool from a PoolPtr. - */ - virtual void setPool(PoolPtr pool) = 0; - - /** * Retrieve the piece on square @a p. * Return a null pointer if that square is empty. */ @@ -231,6 +201,11 @@ public: virtual void set(const Point& p, AbstractPiece::Ptr piece) = 0; /** + * \return an interface to modify the pool of the board relative to \a player + */ + virtual AbstractPool::Ptr pool(int player); + + /** * Return an id corresponding to the player * who is in turn. */ @@ -348,9 +323,6 @@ public: virtual AnimationPtr back(AbstractPosition::Ptr, AbstractMove::Ptr) = 0; }; -class PointConverter; -class GraphicalPosition; -namespace PixmapLoader{ class Info; } class VariantInfo { public: @@ -359,19 +331,32 @@ public: virtual AbstractPosition::Ptr createCustomPosition(const OptList& l) = 0; virtual AbstractPosition::Ptr createPositionFromFEN(const QString& fen) = 0; virtual AbstractPosition::Ptr createChessboard(int turn, bool, bool, bool, bool, const Point&) = 0; - virtual AbstractPiece::Ptr createPiece(int color, int type) = 0; virtual void forallPieces(class PieceFunction&) = 0; virtual int moveListLayout() const = 0; - virtual AbstractAnimator::Ptr createAnimator(PointConverter* converter, - GraphicalPosition* position) = 0; + virtual AbstractAnimator::Ptr createAnimator(GraphicalAPI* graphical_api) = 0; virtual AbstractMove::Ptr createNormalMove(const NormalUserMove&) = 0; virtual AbstractMove::Ptr createDropMove(const DropUserMove&) = 0; virtual AbstractMove::Ptr getVerboseMove(int turn, const class VerboseNotation&) const = 0; - virtual int type(const QString& x) = 0; - virtual QString typeSymbol(int type) = 0; + + /** + * \return if moves are done by just clicking + */ virtual bool simpleMoves() = 0; + + /** + * \return the name of the variant + */ virtual QString name() const = 0; + + /** + * \return the name of the theme proxy variant, ie the variant whose theme can be used + * for the current one (for instance crazyhouse can use chess themes). + */ virtual QString themeProxy() const = 0; + + /** + * \return the (subvariant) options that can be specified for position creation, such as board size, etc + */ virtual OptList positionOptions() const = 0; }; diff --git a/src/kboard_fwd.h b/src/kboard_fwd.h index a511f65..7d18d6b 100644 --- a/src/kboard_fwd.h +++ b/src/kboard_fwd.h @@ -11,16 +11,18 @@ #ifndef __KBOARD_FWD_H__ #define __KBOARD_FWD_H__ +#include + class AbstractMove; class AbstractPosition; class AbstractPiece; class AbstractAnimator; class VariantInfo; -typedef boost::shared_ptr MovePtr; -typedef boost::shared_ptr PositionPtr; -typedef boost::shared_ptr PiecePtr; -typedef boost::shared_ptr AnimatorPtr; -typedef boost::shared_ptr VariantPtr; +typedef boost::shared_ptr MovePtr; +typedef boost::shared_ptr PositionPtr; +typedef boost::shared_ptr PiecePtr; +typedef boost::shared_ptr AnimatorPtr; +typedef boost::shared_ptr VariantPtr; #endif //__KBOARD_FWD_H__ diff --git a/src/main.cpp b/src/main.cpp index 50f214f..5da7de5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,12 @@ static const char description[] = static const char version[] = "0.9.1"; +static KCmdLineOptions options[] = +{ +// { "+[URL]", I18N_NOOP( "Document to open" ), 0 }, + KCmdLineLastOption +}; + void trap() { printf("Press enter to quit.\n"); @@ -33,21 +39,19 @@ void trap() { } int main(int argc, char **argv) { - KAboutData about("kboard", 0, ki18n("KBoard"), version, ki18n(description), + KAboutData about("kboard", I18N_NOOP("KBoard"), version, description, KAboutData::License_GPL, - ki18n("(C) 2006 Paolo Capriotti, Maurizio Monge"), - KLocalizedString(), + "(C) 2006 Paolo Capriotti, Maurizio Monge", + 0, "http://kboard.sourceforge.net", "p.capriotti@gmail.com"); - about.addAuthor(ki18n("Paolo Capriotti"), KLocalizedString(), "p.capriotti@gmail.com"); - about.addAuthor(ki18n("Maurizio Monge"), KLocalizedString(), "p.capriotti@gmail.com"); - about.addCredit(ki18n("Jani Huhtanen"), ki18n("Gaussian blur code")); - about.addCredit(ki18n("Marcin Jakubowski"), ki18n("X11 taskbar flashing")); - about.addCredit(ki18n("Rici Lake"), ki18n("funclib lua library")); + about.addAuthor("Paolo Capriotti", 0, "p.capriotti@gmail.com"); + about.addAuthor("Maurizio Monge", 0, "p.capriotti@gmail.com"); + about.addCredit("Jani Huhtanen", I18N_NOOP("Gaussian blur code"), 0, 0); + about.addCredit("Marcin Jakubowski", I18N_NOOP("X11 taskbar flashing"), 0, 0); + about.addCredit("Rici Lake", I18N_NOOP("funclib lua library"), 0, 0); KCmdLineArgs::init(argc, argv, &about); - - KCmdLineOptions options; KCmdLineArgs::addCmdLineOptions(options); KApplication app; diff --git a/src/namedsprite.h b/src/namedsprite.h new file mode 100644 index 0000000..0ecbe5a --- /dev/null +++ b/src/namedsprite.h @@ -0,0 +1,30 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef NAMEDSPRITE_H +#define NAMEDSPRITE_H + +#include +#include "sprite.h" + +class NamedSprite { +public: + QString m_name; + SpritePtr m_sprite; + + NamedSprite() {} + NamedSprite(const QString& name, boost::shared_ptr s) + : m_name(name), m_sprite(s) {} + QString name(){ return m_name; } + SpritePtr sprite(){ return m_sprite; } + operator bool(){ return !!m_sprite; } +}; + +#endif //NAMEDSPRITE_H diff --git a/src/piecegroup.cpp b/src/piecegroup.cpp index f9b80d2..3e1ec08 100644 --- a/src/piecegroup.cpp +++ b/src/piecegroup.cpp @@ -15,7 +15,7 @@ #include "piecegroup.h" #include "pointconverter.h" #include "animation.h" -#include "piecesprite.h" +#include "sprite.h" using namespace boost; @@ -59,7 +59,7 @@ void PieceGroup::onResize(int new_size, bool force_reload) { m_loader.setSize(m_square_size); } -void PieceGroup::animatePiece(const shared_ptr& piece, +void PieceGroup::animatePiece(const shared_ptr& piece, const Point& to, double speed) { enqueue( m_anim_movement @@ -81,7 +81,7 @@ void PieceGroup::finalizeAnimation(AnimationGroup* group) { } void PieceGroup::adjustSprite(const Point& p, bool smooth) { - shared_ptr sprite = spriteAt(p); + shared_ptr sprite = spriteAt(p); if (sprite) { if (smooth) { animatePiece(sprite, p, 1.0); @@ -93,7 +93,7 @@ void PieceGroup::adjustSprite(const Point& p, bool smooth) { } void PieceGroup::fadeIn(const Point& p) { - shared_ptr sprite = spriteAt(p); + shared_ptr sprite = spriteAt(p); if (sprite) { if(m_anim_fade) enqueue(shared_ptr(new FadeAnimation(sprite, converter()->toReal(p), 0, 255))); @@ -102,8 +102,8 @@ void PieceGroup::fadeIn(const Point& p) { } } -shared_ptr PieceGroup::createSprite(const QPixmap& pix, const Point& pos) { - return shared_ptr(new PieceSprite(pix, piecesGroup(), converter()->toReal(pos))); +SpritePtr PieceGroup::createSprite(const QPixmap& pix, const Point& pos) { + return SpritePtr(new Sprite(pix, piecesGroup(), converter()->toReal(pos))); } //END PieceGroup diff --git a/src/piecegroup.h b/src/piecegroup.h index dcdb5a4..829ed03 100644 --- a/src/piecegroup.h +++ b/src/piecegroup.h @@ -8,20 +8,19 @@ (at your option) any later version. */ -#ifndef BOARD_H -#define BOARD_H +#ifndef PIECEGROUP_H +#define PIECEGROUP_H #include #include "canvas/group.h" #include "point.h" #include "grid.h" #include "animation.h" -#include "piecesprite.h" +#include "sprite.h" #include "mainanimation.h" #include "pointconverter.h" #include "spriteloader.h" #include "clickablecanvas.h" -#include "element.h" class MainAnimation; class PointConverter; @@ -73,14 +72,14 @@ protected: void adjustSprite(const Point&, bool smooth = false); /** enqueue a movement animation (convenience function) */ - void animatePiece(const boost::shared_ptr& piece, + void animatePiece(const boost::shared_ptr& piece, const Point& to, double speed); /** fades in the sprite at a given point */ void fadeIn(const Point&); /** returns the sprite at p */ - virtual boost::shared_ptr spriteAt(const Point&) = 0; + virtual boost::shared_ptr spriteAt(const Point&) = 0; public: PieceGroup(Canvas::Abstract* parent); @@ -132,9 +131,9 @@ public: virtual void settingsChanged(); /** create a sprite from a pixmap */ - virtual boost::shared_ptr createSprite(const QPixmap& pix, const Point& pos); + virtual boost::shared_ptr createSprite(const QPixmap& pix, const Point& pos); }; //END PieceGroup -#endif +#endif //PIECEGROUP_H diff --git a/src/piecepool.cpp b/src/piecepool.cpp index 2e71d03..0a69fe2 100644 --- a/src/piecepool.cpp +++ b/src/piecepool.cpp @@ -60,20 +60,17 @@ void PiecePool::clear() { setFill(0); } -void PiecePool::addPiece(Element p) { +void PiecePool::insertSprite(int index, NamedSprite sprite) { + if(index < 0 || index > fill) { + std::cout << "invalid index " << index << " in PiecePool::insertPiece" << std::endl; + return; + } setFill(m_fill+1); + Point i = m_sprites->nTh(index); - Point i; - for(i = m_sprites.first(); i<=m_sprites.last(); i=m_sprites.next(i)) { - if(!m_sprites[i]) - break; - if(m_sprites[i].piece()->less(p.piece())) - break; - } - - Element replacep = m_sprites[i]; - m_sprites[i] = p; + NamedSprite replacep = m_sprites[i]; + m_sprites[i] = sprite; m_sprites[i].sprite()->show(); m_sprites[i].sprite()->moveTo(converter()->toReal(i)); fadeIn(i); @@ -82,7 +79,7 @@ void PiecePool::addPiece(Element p) { for( i=m_sprites.next(i); i<=m_sprites.last(); i=m_sprites.next(i)) { if(!replacep) break; - Element tmp = replacep; + NamedSprite tmp = replacep; replacep = m_sprites[i]; m_sprites[i] = tmp; animatePiece(m_sprites[i].sprite(), i, (1.0+1.0/speed)*0.4); @@ -90,13 +87,38 @@ void PiecePool::addPiece(Element p) { } } -Element PiecePool::takePiece(Point i) { - if( !m_sprites.valid(i) ) - return Element(); +SpritePtr PiecePool::getSprite(int index) { + if(index < 0 || index >= fill) { + std::cout << "invalid index " << index << " in PiecePool::insertPiece" << std::endl; + return SpritePtr(); + } + + Point i = m_sprites->nTh(index); + return m_sprites[i].sprite(); +} + +SpritePtr PiecePool::takeSprite(int index) { + if(index < 0 || index >= fill) { + std::cout << "invalid index " << index << " in PiecePool::takeSprite" << std::endl; + return SpritePtr(); + } - Element piece = m_sprites[i]; - if(!piece) - return Element(); + Point i = m_sprites->nTh(index); + NamedSprite piece = takeNamedSprite(i); + + return piece.sprite(); +} + +NamedSprite PiecePool::takeNamedSprite(const Point& i) { + if(i < m_sprites.first() || i > m_sprites.last() ) { + std::cout << "invalid index " << index << " in PiecePool::takeNamedSprite" << std::endl; + return NamedSprite(); + } + + Point i = m_sprites->nTh(index); + NamedSprite piece = m_sprites[i]; + if(!piece.sprite()) + return NamedSprite(); Point previ = i; int speed = 1; @@ -111,26 +133,12 @@ Element PiecePool::takePiece(Point i) { previ = i; } - m_sprites[previ] = Element(); + m_sprites[previ] = NamedSprite(); setFill(m_fill-1); return piece; } -Element PiecePool::takePiece(AbstractPiece::Ptr ref) { - if(m_dragged && m_dragged.piece()->equals(ref) ) { - Element retv = m_dragged; - m_dragged = Element(); - return retv; - } - - for(Point i = m_sprites.first(); i<=m_sprites.last(); i=m_sprites.next(i)) - if(m_sprites[i].piece()->equals(ref) ) - return takePiece(i); - - return Element(); -} - Canvas::Abstract* PiecePool::piecesGroup() { return this; } @@ -140,8 +148,8 @@ void PiecePool::clearDrag(bool fadeOff) { return; if (fadeOff) { - boost::shared_ptr phantom = - boost::shared_ptr(m_dragged.sprite()->duplicate()); + boost::shared_ptr phantom = + boost::shared_ptr(m_dragged.sprite()->duplicate()); if(m_anim_fade) enqueue( boost::shared_ptr(new FadeAnimation(phantom, phantom->pos(), 255, 0)) ); else @@ -152,7 +160,7 @@ void PiecePool::clearDrag(bool fadeOff) { m_dragged.sprite()->putInCanvas(piecesGroup()); addPiece(m_dragged); - m_dragged = Element(); + m_dragged = NamedSprite(); } void PiecePool::flipAndMoveBy(QPoint p) { @@ -177,7 +185,7 @@ void PiecePool::flipAndMoveBy(QPoint p) { void PiecePool::updateSprites() { // adjust piece positions for (Point i = m_sprites.first(); i <= m_sprites.last(); i = m_sprites.next(i)) { - boost::shared_ptr p = m_sprites[i].sprite(); + boost::shared_ptr p = m_sprites[i].sprite(); if (p) { // drawing sprite @@ -206,11 +214,10 @@ void PiecePool::onMouseRelease(const QPoint& pos, int button) { bool fadeOff = true; if(!m_board->m_drop_sprite && m_dragged) { - m_dragged = Element( m_dragged.piece(), - boost::shared_ptr(m_dragged.sprite()->duplicate()) ); + m_dragged = NamedSprite( m_dragged.name(), SpritePtr(m_dragged.sprite()->duplicate()) ); fadeOff = false; } - m_board->m_drop_sprite = Element(); + m_board->m_drop_sprite = NamedSprite(); clearDrag(fadeOff); } @@ -220,12 +227,12 @@ void PiecePool::onMousePress(const QPoint& pos, int button) { if(m_dragged) { std::cout << "Eh? We are already dragging?" << std::endl; - m_board->m_drop_sprite = Element(); + m_board->m_drop_sprite = NamedSprite(); clearDrag(); //never remove implicitly a piece from the pool } Point p = converter()->toLogical(pos); - Element got = takePiece(p); + NamedSprite got = takeNamedSprite(p); if(!got) return; @@ -234,9 +241,7 @@ void PiecePool::onMousePress(const QPoint& pos, int button) { /* recreate the sprite, as "got" may be being animated */ QPixmap px = m_board->m_loader( got.piece()->name() ); QPoint at = pos + this->pos() - m_board->pos() - QPoint(px.width(), px.height())/2; - m_dragged = Element( got.piece(), boost::shared_ptr( - new PieceSprite( px, m_board->piecesGroup(), at ) ) - ); + m_dragged = NamedSprite( got.name(), SpritePtr(new Sprite(px, m_board->piecesGroup(), at)) ); m_dragged.sprite()->raise(); m_dragged.sprite()->show(); m_board->m_drop_sprite = m_dragged; diff --git a/src/piecepool.h b/src/piecepool.h index 748920a..e58225c 100644 --- a/src/piecepool.h +++ b/src/piecepool.h @@ -13,6 +13,7 @@ #include #include "piecegroup.h" +#include "namedsprite.h" /** * @class PiecePool @@ -24,7 +25,7 @@ */ class PiecePool : public PieceGroup { public: - typedef Grid PieceGrid; + typedef Grid PieceGrid; private: /** displayed m_sprites */ @@ -33,10 +34,10 @@ private: /** refrence board */ class Board* m_board; - /** the pieces that is being dragged, if any */ - Element m_dragged; + /** the piece that is being dragged, if any */ + NamedSprite m_dragged; - /** in the number of pieces on the pool */ + /** the number of pieces on the pool */ int m_fill; /** internal, resizes the grid vector to hold x pieces */ @@ -54,42 +55,47 @@ private: board and we don't want a clone that is fading off */ void clearDrag(bool fadeOff = true); - /** this internal function updates the sprites after the board has been resized */ + /** this internal function updates the sprite images after the board has been resized */ void updateSprites(); /** fetch the sprite */ - boost::shared_ptr spriteAt(const Point& p) { return m_sprites[p].sprite(); } + boost::shared_ptr spriteAt(const Point& p) { return m_sprites[p].sprite(); } + + /** takes the named sprite */ + NamedSprite takeNamedSprite(const Point& p); public: - friend class GraphicalInfo; + friend class GraphicalSystem; friend class ChessTable; /** Constructor, requires the board the pool will be attached to */ PiecePool(Board* b, Canvas::Abstract* parent); ~PiecePool(); + + /** returns the number of pieces in the pool */ int fill(); /** removes all the pieces */ void clear(); - /** sets the width of the grid (the fill will stay the same, and the - grid height will be recalculated) */ - void setGridWidth(int w); - /** adds a piece to the pool */ - void addPiece(Element p); + void insertSprite(int index, const NamedSprite& sprite); - /** remove the piece at the given point from the pool and returns it. */ - Element takePiece(Point p); + /** \return the piece at the given index. */ + SpritePtr getSprite(int index); - /** remove a piece with the given id from the pool and returns it. - If a piece of the pool is being dragged and it has the same id, it - will be the removed one. */ - Element takePiece(AbstractPiece::Ptr ref); + /** removes the piece at the given index from the pool and returns it. */ + SpritePtr takeSprite(int index); + + + + /** sets the width of the grid (the fill will stay the same, and the + grid height will be recalculated) */ + void setGridWidth(int w); - /** returns the size of the grid */ + /** \return the size of the grid */ virtual Point gridSize() const { return m_sprites.getSize(); } /** piecesGroup overload */ diff --git a/src/poolinfo.cpp b/src/poolinfo.cpp index b66b0c4..fdaf44b 100644 --- a/src/poolinfo.cpp +++ b/src/poolinfo.cpp @@ -23,6 +23,8 @@ PoolInfo::PoolInfo(const std::map& games, const QString& str) : m_valid(false) , m_pos_index(-1) { +//BROKEN +#if 0 if (s_pattern.indexIn(str) != 0) return; @@ -55,4 +57,5 @@ PoolInfo::PoolInfo(const std::map& games, const QString& str) m_added_piece = variant->createPiece(color, type); } m_valid = true; +#endif } diff --git a/src/poolinfo.h b/src/poolinfo.h index 86328f4..4d58579 100644 --- a/src/poolinfo.h +++ b/src/poolinfo.h @@ -22,7 +22,7 @@ public: bool m_valid; int m_game_num; Index m_pos_index; - AbstractPosition::PoolPtr m_pool; + AbstractPool::Ptr m_pool[2]; AbstractPiece::Ptr m_added_piece; static QRegExp s_pattern; diff --git a/src/positioninfo.cpp b/src/positioninfo.cpp index 6a8e8ed..3f4d6b6 100644 --- a/src/positioninfo.cpp +++ b/src/positioninfo.cpp @@ -69,8 +69,9 @@ PositionInfo::PositionRow::PositionRow(VariantInfo* variant, const QString& str) continue; } - type = variant->type(c); - row[i] = variant->createPiece(color, type); + //BROKEN + //type = variant->type(c); + //row[i] = variant->createPiece(color, type); } } diff --git a/src/sprite.cpp b/src/sprite.cpp new file mode 100644 index 0000000..57fe862 --- /dev/null +++ b/src/sprite.cpp @@ -0,0 +1,325 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include +#include +#include +#include +#include +#include "common.h" +#include "point.h" +#include "sprite.h" + + +using namespace boost; + +struct ExplosionFragment { + QPoint m_polygon[6]; + int m_pcount; + QPoint m_speed; + ExplosionFragment() : m_pcount(0) {} +}; + +/* inherit instead of typedef to ease forward declaration :) */ +class SpriteExplosion : public std::vector { + +}; + +Sprite::Sprite(const QPixmap& pix, Canvas::Abstract* canvas, + const QPoint& location) +: Canvas::Pixmap(pix, canvas) +, m_pixmap(pix) +, m_explode_step(0.0) +, m_explosion(NULL) +, m_rotation(0.0) +, m_scale(1.0) +#ifdef DEBUG_PIECE +, m_dummy_opacity(255) +, m_dummy_visible(false) +#endif +{ + moveTo(location); + lower(); + +#ifdef DEBUG_PIECE + update_from_dummy(); +#endif +} + +Sprite::~Sprite() { + if(m_explosion) + delete m_explosion; +} + +Sprite* Sprite::duplicate() const { + return new Sprite(pixmap(), canvas(), pos() ); +} + +void Sprite::setThumb(const QImage& thumb) { + std::cout << "setting thumb" << std::endl; + QPixmap pix = m_pixmap; + int width = pix.width() / 2; + int height = pix.height() / 2; + QPixmap thumb_pix = QPixmap::fromImage( + thumb.scaled(width, height, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); + + { + QPainter painter(&pix); + painter.drawPixmap(pix.width() - width, 0, thumb_pix); + } + + Canvas::Pixmap::setPixmap(pix); +} + +void Sprite::removeThumb() { + Canvas::Pixmap::setPixmap(m_pixmap); +} + +void Sprite::setPixmap(const QPixmap& pix) { + m_pixmap = pix; + Canvas::Pixmap::setPixmap(pix); +} + +void Sprite::setMovementAnimation(const shared_ptr& animation) { + m_movement_animation = animation; +} + +weak_ptr Sprite::movementAnimation() const { + return m_movement_animation; +} +/* +void Sprite::setFadeAnimation(const shared_ptr& animation) { + m_fade_animation = animation; +} + +weak_ptr Sprite::fadeAnimation() const { + return m_fade_animation; +}*/ + +void Sprite::setExplosionStep(float f) { + m_explode_step = f; + changed(); +} + +void Sprite::setupExplosion(Random& random) { + if (m_explosion) { + delete m_explosion; + m_explosion = NULL; + } + m_explosion = createExplosion(random); +} + +/* + * this function generate many polygons that fill the image, + their random speeds + * + * Split points are some random point on the border (ordered accordin to the angle). + * The generated polygons are created cutting from each split point to (according + * to random's will) the center -or- a point in the mid way from the center to the + * previous or next split point. + */ +SpriteExplosion* Sprite::createExplosion(Random& random) { + SpriteExplosion* retv = new SpriteExplosion; + int w = pixmap().width(); + int h = pixmap().height(); + float splits[40]; + float splits_r[40]; + float splits_i[40]; + int split_side[40]; // the side of the image, 0 to 3 + int split_start[40]; // -1 to 1 + QPoint split_point[40]; + QPoint split_start_point[40]; + int num_splits = 0; + + if(w==0 || h==0) + return retv; + + /* generate few subsequent angles */ + Random::RealGenerator random_float = random.rand(0.4, 0.7); + for(float s = 0; s < 2*M_PI-0.3; s += random_float()) { + splits[num_splits] = s; + splits_r[num_splits] = cos(s); + splits_i[num_splits] = sin(s); + num_splits++; + } + + /* generate the random center and calculate the relative corners */ + random_float = random.rand(0.4, 0.6); + QPoint center(int(w * random_float()), int(h * random_float())); + QPoint corners[4] = { -center, QPoint(w,0)-center, + QPoint(w,h)-center, QPoint(0,h)-center }; + + /* for each split angle, find the size of the rect that contains the split point */ + for(int i=0;i0 and <0 */ + if(corners[j].x()*splits_i[i]-corners[j].y()*splits_r[i] >= 0 + && corners[j1].x()*splits_i[i]-corners[j1].y()*splits_r[i] < 0 ) { + + split_side[i] = j; + float fact = (corners[j].x()*corners[j1].y()-corners[j].y()*corners[j1].x())/ + (splits_r[i]*(corners[j1].y()-corners[j].y())- + splits_i[i]*(corners[j1].x()-corners[j].x())); + float x = splits_r[i]*fact; + float y = splits_i[i]*fact; + split_point[i] = QPoint(int(x+0.5), int(y+0.5)); + break; + } + } + + /* there should really be one :) */ + Q_ASSERT(split_side[i] != -1); + } + + /* generate the split points connections. Some will be connected with the center (0), + others will be connected to a mid-way point of the previous or next points (-1/+1) */ + Random::IntegerGenerator coin = random.rand(0, 1); + Random::IntegerGenerator die3 = random.rand(0, 2); + split_start[0] = 0; + for(int i=1;i=num_splits ? 0 : r; + if(r == i) + split_start_point[i] = QPoint(); + else + split_start_point[i] = split_point[r] * random_float(); + } + + random_float = random.rand(0.5, 1.2); + for(int i=0;ipush_back(f); + } + + return retv; +} + +void Sprite::setRotation(float f) { + m_rotation = f; + changed(); +} + +void Sprite::setScale(float f) { + m_scale = f; + changed(); +} + +void Sprite::paint(QPainter* p) { + QMatrix savem; + + /* if scale/rotate change the painter matrix */ + if(m_rotation != 0.0 || m_scale != 1.0) { + QRectF rect = pixmap().rect(); + QPointF center(rect.width()*0.5, rect.height()*0.5); + savem = p->matrix(); + p->translate(center+pos()); + p->rotate(m_rotation*180.0/M_PI); + p->scale(m_scale, m_scale); + p->translate(-center-pos()); + } + + if(m_explosion) { + p->setPen(Qt::NoPen); + p->setBrush(pixmap()); + p->translate(pos()); + + for(int i=0;isize());i++) { + ExplosionFragment& f = (*m_explosion)[i]; + QPoint delta = f.m_speed*m_explode_step; + + p->translate(delta); + p->drawConvexPolygon(f.m_polygon, f.m_pcount); + p->translate(-delta); + } + p->translate(-pos()); + } + else + Canvas::Pixmap::paint(p); + + if(m_rotation != 0.0 || m_scale != 1.0) + p->setMatrix(savem); +} + +QRect Sprite::rect() const { + QRect retv; + + /* if exploding, set the rect to the bounding rect of the pieces */ + if(m_explosion) { + int x1=99999, x2=-99999, y1=99999, y2=-99999; + + for(int i=0;isize());i++) { + ExplosionFragment& f = (*m_explosion)[i]; + QPoint delta = f.m_speed*m_explode_step; + + for(int j=0;j + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef SPRITE_H +#define SPRITE_H + +#include "canvas/canvas.h" +#include "random.h" +#include +#include + +class QPoint; +class QImage; +class Animation; +class FadeAnimation; +class SpriteExplosion; + +#undef DEBUG_PIECE + +/** + * @class Sprite + * @brief The sprite of a piece. + * + * This class is a Canvas::Pixmap that enables a few nifty + * effects and keeps some piece-related information. + */ +class Sprite : public Canvas::Pixmap { +private: + /** the piece id (for convenience) */ + int m_id; + + /** the piece color (for convenience, could be -1) */ + int m_color; + + /** the piece type (for convenience, could be -1) */ + int m_type; + + QPixmap m_pixmap; + + /** the movement animation class */ + boost::weak_ptr m_movement_animation; + + + /** the step in the explostion */ + float m_explode_step; + + /** the explostion object */ + SpriteExplosion* m_explosion; + + /** rotation factor */ + float m_rotation; + + /** scaling factor */ + float m_scale; + + /** creates a new explosion object */ + SpriteExplosion* createExplosion(Random& random); + + /** painting implementation */ + virtual void paint(QPainter* p); + + /** returns the rectangle having to be painted */ + virtual QRect rect() const; + +public: + /** Constructor */ + Sprite(const QPixmap& pix, Canvas::Abstract* canvas, const QPoint& location); + virtual ~Sprite(); + + /** duplicates the piece */ + Sprite* duplicate() const; + + void setThumb(const QImage& thumb); + void removeThumb(); + + /** updates the pixmap */ + /* NOTE for paolo: why virtual? */ + virtual void setPixmap(const QPixmap&); + + /** set the movement animation */ + void setMovementAnimation(const boost::shared_ptr& animation); + + /** returns the movement animation */ + boost::weak_ptr movementAnimation() const; + +// void setFadeAnimation(const boost:shared_ptr& animation); +// boost:weak_ptr fadeAnimation() const; + + /** set the explosion step (you have to call setupExplosion before) */ + void setExplosionStep(float f); + + /** set the rotations factor */ + void setRotation(float f); + + /** set the scaling factor */ + void setScale(float f); + + /** prepares a new explosion animation */ + void setupExplosion(Random& random); + +#ifdef DEBUG_PIECE + int m_dummy_opacity; + bool m_dummy_visible; + + void update_from_dummy(){ + Canvas::Item::show(); + Canvas::Item::setOpacity(m_dummy_visible ? 64+m_dummy_opacity*3/8 : 64); + } + + /* those should be enough, even if they are not virtual, as Piece is never casted IIRC */ + int opacity(){ return m_dummy_opacity; } + int visible(){ return m_dummy_visible; } + void setOpacity(int v){ m_dummy_opacity=v; update_from_dummy(); } + void setVisible(bool v){ m_dummy_visible=v; update_from_dummy(); } + void show(){ m_dummy_visible=true; update_from_dummy(); } + void hide(){ m_dummy_visible=false; update_from_dummy(); } +#endif +}; + +typedef boost::shared_ptr SpritePtr; + +#endif // SPRITE_H diff --git a/src/unwrapped_graphicalapi.h b/src/unwrapped_graphicalapi.h new file mode 100644 index 0000000..0455ac2 --- /dev/null +++ b/src/unwrapped_graphicalapi.h @@ -0,0 +1,67 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef UNWRAPPEDGRAPHICALAPI_H +#define UNWRAPPEDGRAPHICALAPI_H + +/** + * This class moves the interface that will be used by the animator to modify + * kboard graphics into the variant's semantics. + */ +template +class UnwrappedGraphicalAPI { + GraphicalAPI* m_graphical_api; +public: + UnwrappedGraphicalAPI(GraphicalAPI* interface) + : m_graphical_api(interface) { + } + + virtual const PointConverter* converter() { + return m_graphical_api->converter(); + } + + virtual const typename Variant::Position* position() { + const WrappedPosition *p = dynamic_cast*>(m_graphical_api->position()); + if(p) + return p->inner(); + else { + MISMATCH(m_graphical_api->position(), WrappedPosition); + return NULL; + } + } + + virtual SpritePtr setSprite(const Point& p, const typename Variant::Piece& piece, bool use_drop, bool show) { + WrappedPiece wpiece(piece); + return m_graphical_api->setSprite(p, &wpiece, use_drop, show); + } + + virtual SpritePtr getSprite(const Point& p) { + return m_graphical_api->getSprite(p); + } + + virtual SpritePtr takeSprite(const Point& p) { + return m_graphical_api->takeSprite(p); + } + + virtual SpritePtr insertPoolSprite(int index, const AbstractPiece& piece) { + WrappedPiece wpiece(piece); + return m_graphical_api->insertPoolSprite(index, &wpiece); + } + + virtual SpritePtr getPoolSprite(int index) { + return m_graphical_api->getPoolSprite(index); + } + + virtual SpritePtr takePoolSprite(int index) { + return m_graphical_api->takePoolSprite(index); + } +}; + +#endif //UNWRAPPEDGRAPHICALAPI_H diff --git a/src/variants/chess.cpp b/src/variants/chess.cpp index 9bf0899..9226a6f 100644 --- a/src/variants/chess.cpp +++ b/src/variants/chess.cpp @@ -42,9 +42,96 @@ protected: destination, 1.0)); } } - }; + +//BEGIN Dream code + + +typedef UnwrappedGraphicalAPI ChessGraphicalAPI; + +class ChessAnimator { + ChessGraphicalAPI* m_cinterface; +public: + ChessAnimator(ChessGraphicalAPI* cinterface) + : m_cinterface(cinterface) { + } + + AnimationPtr warp(const ChessPosition* final) { + const ChessPosition* current = m_cinterface->position(); + AnimationPtr res(new AnimationGroup); + + for (Point i = m_current->first(); i <= m_position->last(); i = m_position->next(i)) { + ChessPiece* c = current->get(i); + ChessPiece* f = final->get(i); + + if( !c && f ) { + //current->set(i, f); + PieceSpritePtr sprite = m_cinterface->setSprite(i, *f, false, false); + res->addPreAnimation( shared_ptr(new DropAnimation(sprite)) ); + } + else if (c && !f) { + //current->set(i, shared_ptr()); + PieceSpritePtr old_sprite = m_cinterface->getSprite(i); + res->addPreAnimation( shared_ptr(new CaptureAnimation(sprite)) ); + } + else if(!c.equals(f) ) { + current->set(i, f); + PieceSpritePtr old_sprite = m_cinterface->takeSprite(i); + res->addPreAnimation( shared_ptr(new PromotionAnimation(sprite)) ); + } + } + } + + //TODO: implement pool update + + return res; +}; + + + +typedef UnwrappedGraphicalAPI ChessGraphicalAPI; + +class ChessAnimator { + ChessGraphicalAPI* m_cinterface; +public: + ChessAnimator(ChessGraphicalAPI* cinterface) + : m_cinterface(cinterface) { + } + + AnimationPtr warp(ChessPosition* final) { + ChessPosition* current = m_cinterface->position(); + AnimationPtr res(new AnimationGroup); + + for (Point i = m_current->first(); i <= m_position->last(); i = m_position->next(i)) { + ChessPiece* c = current->get(i); + ChessPiece* f = final->get(i); + + if( !c && f ) { + current->set(i, f); + PieceSpritePtr sprite = m_cinterface->setSprite(i, *f, false, false); + res->addPreAnimation( shared_ptr(new DropAnimation(sprite)) ); + } + else if (c && !f) { + current->set(i, shared_ptr()); + PieceSpritePtr old_sprite = m_cinterface->getSprite(i); + res->addPreAnimation( shared_ptr(new CaptureAnimation(sprite)) ); + } + else if(!c.equals(f) ) { + current->set(i, f); + PieceSpritePtr old_sprite = m_cinterface->takeSprite(i); + res->addPreAnimation( shared_ptr(new PromotionAnimation(sprite)) ); + } + } + } + + return res; +}; + + +//END Dream code + + void ChessVariant::forallPieces(PieceFunction& f) { f(WHITE, KING); f(WHITE, QUEEN); diff --git a/src/variants/xchess/animator.h b/src/variants/xchess/animator.h index a8464e4..4622fbe 100644 --- a/src/variants/xchess/animator.h +++ b/src/variants/xchess/animator.h @@ -13,14 +13,12 @@ #include #include "animation.h" -#include "element.h" #include "random.h" -#include "wrappedanimator.h" class AnimationGroup; class PointConverter; class GraphicalPosition; - +#if 0 /** * A variant-agnostic animator. * Can be used as a base class for other specialized animators. @@ -65,5 +63,7 @@ public: AnimationPtr forward(const Position&, const Move& move); AnimationPtr back(const Position&, const Move& move); }; +#endif + #endif // ANIMATOR_H diff --git a/src/variants/xchess/animator.impl.h b/src/variants/xchess/animator.impl.h index 8b2b2e0..6db55cc 100644 --- a/src/variants/xchess/animator.impl.h +++ b/src/variants/xchess/animator.impl.h @@ -8,6 +8,7 @@ (at your option) any later version. */ +#if 0 #include #include @@ -220,6 +221,6 @@ shared_ptr SimpleAnimator::back(const Position& final, return warpingAnimation; } - +#endif diff --git a/src/wrappedanimator.h__ b/src/wrappedanimator.h__ new file mode 100644 index 0000000..da32229 --- /dev/null +++ b/src/wrappedanimator.h__ @@ -0,0 +1,135 @@ +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "highlevel.h" +#include "graphicalposition.h" + +template +class WrappedElement { + Element m_inner; +public: + typedef boost::shared_ptr SpritePtr; + + Element inner() const { return m_inner; } + operator bool() const { return m_inner; } + + WrappedElement() { } + WrappedElement(const Element& element) + : m_inner(element) { + if (m_inner.piece()) + TYPECHECK(*m_inner.piece(), WrappedPiece); +// if (!dynamic_cast*>(m_inner.piece().get())) +// MISMATCH(m_inner.piece().get(), WrappedPiece); + } + + const typename Variant::Piece& piece() const { + WrappedPiece* res = dynamic_cast*>(m_inner.piece().get()); + Q_ASSERT(res); + return res->inner(); + } + + SpritePtr sprite() const { return m_inner.sprite(); } +}; + +template +class GenericGraphicalPosition { + typedef boost::shared_ptr SpritePtr; + typedef typename Variant::Piece Piece; +public: + virtual ~GenericGraphicalPosition() { } + virtual int getIntSetting(const QString& key, int def_value) const = 0; + virtual bool getBoolSetting(const QString& key, bool def_value) const = 0; + virtual QString getStringSetting(const QString& key, const QString& def_value) const = 0; + + virtual SpritePtr getSprite(const Point& p) const = 0; + + virtual SpritePtr setPiece(const Point& p, const Piece& e, + bool usedrop = false, bool show = false) = 0; + + virtual WrappedElement getElement(const Point& p) const = 0; + + virtual void setElement(const Point& p, const WrappedElement& e) = 0; + + virtual void removeElement(const Point& p) = 0; + virtual void updatePool(AbstractPosition::PoolPtr pool) = 0; + virtual void addTag(const QString& name, Point, bool over = false) = 0; + virtual void clearTags(const QString& name) = 0; + virtual void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(), + Point p3 = Point::invalid(), Point p4 = Point::invalid(), + Point p5 = Point::invalid(), Point p6 = Point::invalid() ) = 0; + + virtual Point first() const = 0; + virtual Point last() const = 0; + virtual Point next(const Point& p) const = 0; + virtual bool valid(const Point& p) const = 0; + virtual bool consistent() const = 0; +}; + +/** + * @brief A strongly typed wrapper aroung GraphicalPosition. + */ +template +class WrappedGraphicalPosition : public GenericGraphicalPosition { + typedef boost::shared_ptr SpritePtr; + typedef typename Variant::Piece Piece; + GraphicalPosition* m_inner; +public: + typedef WrappedElement GElement; + WrappedGraphicalPosition(GraphicalPosition* inner) + : m_inner(inner) { } + + virtual int getIntSetting(const QString& key, int def_value) const { + return m_inner->getIntSetting(key, def_value); + } + + virtual bool getBoolSetting(const QString& key, bool def_value) const { + return m_inner->getBoolSetting(key, def_value); + } + + virtual QString getStringSetting(const QString& key, const QString& def_value) const { + return m_inner->getStringSetting(key, def_value); + } + + virtual SpritePtr getSprite(const Point& p) const { + return m_inner->getSprite(p); + } + + virtual SpritePtr setPiece(const Point& p, const Piece& e, + bool usedrop = false, bool show = false) { + AbstractPiece::Ptr piece(new WrappedPiece(e)); + return m_inner->setPiece(p, piece, usedrop, show); + } + + virtual GElement getElement(const Point& p) const { + return m_inner->getElement(p); + } + + virtual void setElement(const Point& p, const GElement& e) { + m_inner->setElement(p, e.inner()); + } + + virtual void removeElement(const Point& p) { m_inner->removeElement(p); } + virtual void updatePool(AbstractPosition::PoolPtr pool) { m_inner->updatePool(pool); } + virtual void addTag(const QString& name, Point p, bool over = false) { m_inner->addTag(name, p, over); } + virtual void clearTags(const QString& name) { m_inner->clearTags(name); } + virtual void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(), + Point p3 = Point::invalid(), Point p4 = Point::invalid(), + Point p5 = Point::invalid(), Point p6 = Point::invalid() ) { + m_inner->setTags(name, p1, p2, p3, p4, p5, p6); + } + + virtual Point first() const { return m_inner->first(); } + virtual Point last() const { return m_inner->last(); } + virtual Point next(const Point& p) const { return m_inner->next(p); } + virtual bool valid(const Point& p) const { return m_inner->valid(p); } + virtual bool consistent() const { return m_inner->consistent(); } +}; + + -- 2.11.4.GIT