File: overviewmappanel.cpp

package info (click to toggle)
asc 2.6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 82,860 kB
  • ctags: 26,395
  • sloc: cpp: 158,660; sh: 11,274; ansic: 6,878; makefile: 604; perl: 138
file content (155 lines) | stat: -rw-r--r-- 5,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***************************************************************************
                          paradialog.cpp  -  description
                             -------------------
    begin                : Thu Feb 21 2002
    copyright            : (C) 2002 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "overviewmappanel.h"
#include "spfst.h"
#include "graphics/blitter.h"
#include "graphics/drawing.h"
#include "mapdisplay.h"
#include "spfst-legacy.h"


OverviewMapPanel::OverviewMapPanel( PG_Widget *parent, const PG_Rect &r, MapDisplayPG* mapDisplay, const ASCString& widgetName )
   : LayoutablePanel ( parent, r, widgetName, true ), mapDisplayWidget( mapDisplay), currentZoom( 1 ), locked(false)
{
   SpecialDisplayWidget* sdw = dynamic_cast<SpecialDisplayWidget*>( FindChild( "overviewmap", true ) );
   if ( sdw ) {
      sdw->display.connect( SigC::slot( *this, &OverviewMapPanel::painter ));
      sdw->sigMouseMotion.connect( SigC::slot( *this, &OverviewMapPanel::mouseMotion ));
      sdw->sigMouseButtonDown.connect( SigC::slot( *this, &OverviewMapPanel::mouseButtonDown ));
   }
   
   ovmap = sdw;
   assert( ovmap );
      
   OverviewMapHolder::generationComplete.connect ( SigC::slot( *this, &OverviewMapPanel::redraw ));
   viewChanged.connect ( SigC::slot( *this, &OverviewMapPanel::redraw ));

   lockMapdisplay.connect( SigC::slot( *this, &OverviewMapPanel::lockPanel ));
   unlockMapdisplay.connect( SigC::slot( *this, &OverviewMapPanel::unlockPanel ));
   

}


template<int pixelsize>
class ColorMerger_Invert
{
      typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
      SDLmm::Color col;
   public:

      void assign ( PixelType src, PixelType* dest ) const
      {
         *dest = (col & 0xff000000) + (0xffffff - (*dest & 0xffffff));
         // *dest = col;
      };

     
      ColorMerger_Invert( SDLmm::Color color )
      {
         col = color;
      };
};


void OverviewMapPanel::painter ( const PG_Rect &src, const ASCString& name, const PG_Rect &dst)
{
   Surface screen = Surface::Wrap( PG_Application::GetScreen() );
   if ( name == "overviewmap" && actmap && !locked ) {
      Surface s = actmap->overviewMapHolder.getOverviewMap( false );

      MegaBlitter< gamemapPixelSize, gamemapPixelSize,ColorTransform_None,ColorMerger_AlphaOverwrite,SourcePixelSelector_DirectZoom,TargetPixelSelector_Rect> blitter;
      blitter.setSize( s.w(), s.h(), dst.w, dst.h );

      PG_Rect clip= dst.IntersectRect( PG_Application::GetScreen()->clip_rect );
      blitter.setTargetRect( clip );


      currentZoom  = blitter.getZoomX();
      blitter.blit( s, screen, SPoint(dst.x, dst.y) );

      SPoint ul = OverviewMapImage::map2surface( mapDisplayWidget->upperLeftCorner());
      SPoint lr = OverviewMapImage::map2surface( mapDisplayWidget->lowerRightCorner());
      ul.x = int( float( ul.x) * currentZoom );
      ul.y = int( float( ul.y) * currentZoom );
      lr.x = int( float( lr.x) * currentZoom );
      lr.y = int( float( lr.y) * currentZoom );

      if ( ul.x < 0 )
         ul.x = 0;
      if ( ul.y < 0 )
         ul.y = 0;
      if ( lr.x >= src.Width() )
         lr.x = src.Width() -1;
      if ( lr.y >= src.Height() )
         lr.y = src.Height() -1;

      rectangle<4>(screen, SPoint(dst.x + ul.x, dst.y + ul.y), lr.x-ul.x, lr.y-ul.y, ColorMerger_Invert<4>(0xff), ColorMerger_Invert<4>(0xff) );
   }
}


void OverviewMapPanel::lockPanel()
{
   locked = true;
   Update();
}

void OverviewMapPanel::unlockPanel()
{
   locked = false;
   Redraw();
}

bool OverviewMapPanel::mouseClick ( SPoint pos )
{
   SPoint unscaledPos = SPoint(int( float(pos.x) / currentZoom), int(float(pos.y) / currentZoom ));
   MapCoordinate mc = OverviewMapImage::surface2map( unscaledPos );
   
   if ( !(mc.valid() && mc.x < actmap->xsize && mc.y < actmap->ysize ))
      return false;

   mapDisplayWidget->centerOnField( mc );
   return true;
}


bool OverviewMapPanel::mouseButtonDown ( const SDL_MouseButtonEvent *button)
{
   if ( ovmap->IsMouseInside() )
      if ( button->type == SDL_MOUSEBUTTONDOWN && button->button == 1 ) {
         PG_Point p = ovmap->ScreenToClient( button->x, button->y );
         return mouseClick( SPoint( p.x, p.y ));
      }

   return false;
}

bool OverviewMapPanel::mouseMotion  (  const SDL_MouseMotionEvent *motion)
{
   if ( ovmap->IsMouseInside() )
      if ( motion->type == SDL_MOUSEMOTION && (motion->state & 1 ) ) {
         PG_Point p = ovmap->ScreenToClient( motion->x, motion->y );
         return mouseClick( SPoint( p.x, p.y ));
      }

   return false;
}