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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*** /
This file is part of Golly, a Game of Life Simulator.
Copyright (C) 2009 Andrew Trevorrow and Tomas Rokicki.
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Web site: http://sourceforge.net/projects/golly
Authors: rokicki@gmail.com andrew@trevorrow.com
/ ***/
#include "viewport.h"
#include "lifealgo.h"
#include <cmath>
#include <iostream>
#include <cstdio>
using namespace std ;
void viewport::init() {
x = 0 ;
y = 0 ;
height = width = 8 ;
mag = 0 ;
x0 = 0 ;
y0 = 0 ;
x0f = 0 ;
y0f = 0 ;
xymf = 0 ;
}
void viewport::zoom() {
if (mag >= MAX_MAG)
return ;
mag++ ;
reposition() ;
}
void viewport::zoom(int xx, int yy) {
if (mag >= MAX_MAG)
return ;
pair<bigint, bigint> oldpos = at(xx, yy); // save cell pos for use below
int x2c = xx * 2 - getxmax() ;
bigint o = x2c ;
o.mulpow2(-mag-2) ;
x += o ;
int y2c = yy * 2 - getymax() ;
o = y2c ;
o.mulpow2(-mag-2) ;
y += o ;
mag++ ;
reposition() ;
// adjust cell position if necessary to avoid any drift
if (mag >= 0) {
pair<bigint, bigint> newpos = at(xx, yy);
bigint xdrift = newpos.first;
bigint ydrift = newpos.second;
xdrift -= oldpos.first;
ydrift -= oldpos.second;
// drifts will be -1, 0 or 1
if (xdrift != 0) move(-xdrift.toint() << mag, 0);
if (ydrift != 0) move(0, -ydrift.toint() << mag);
}
}
void viewport::unzoom() {
mag-- ;
reposition() ;
}
void viewport::unzoom(int xx, int yy) {
pair<bigint, bigint> oldpos = at(xx, yy); // save cell pos for use below
mag-- ;
int x2c = xx * 2 - getxmax() ;
bigint o = x2c ;
o.mulpow2(-mag-2) ;
x -= o ;
int y2c = yy * 2 - getymax() ;
o = y2c ;
o.mulpow2(-mag-2) ;
y -= o ;
reposition() ;
// adjust cell position if necessary to avoid any drift
pair<bigint, bigint> newpos = at(xx, yy);
bigint xdrift = newpos.first;
bigint ydrift = newpos.second;
xdrift -= oldpos.first;
ydrift -= oldpos.second;
// drifts will be -1, 0 or 1
if (xdrift != 0) move(-xdrift.toint() << mag, 0);
if (ydrift != 0) move(0, -ydrift.toint() << mag);
}
pair<bigint, bigint> viewport::at(int x, int y) {
bigint rx = x ;
bigint ry = y ;
rx.mulpow2(-mag) ;
ry.mulpow2(-mag) ;
rx += x0 ;
ry += y0 ;
return pair<bigint, bigint>(rx, ry) ;
}
pair<double, double> viewport::atf(int x, int y) {
return pair<double, double>(x0f + x * xymf, y0f + y * xymf) ;
}
/**
* Returns the screen position of a particular pixel. Note that this
* is a tiny bit more complicated than you might expect, because it
* has to take into account exactly how a life algorithm compresses
* multiple pixels into a single pixel (which depends not only on the
* lifealgo, but in the case of qlifealgo, *also* depends on the
* generation count). In the case of mag < 0, it always returns
* the upper left pixel; it's up to the caller to adjust when
* mag<0.
*
* If the pixel is offscreen, it will return a value that is also
* offscreen, but only by one pixel.
* AKT: This code has been commented out because it makes it
* harder to calculate an accurate paste rectangle.
*/
pair<int,int> viewport::screenPosOf(bigint x, bigint y, lifealgo *algo) {
if (mag < 0) {
bigint xx0 = x0 ;
bigint yy0 = y0 ;
algo->lowerRightPixel(xx0, yy0, mag) ;
y -= yy0 ;
x -= xx0 ;
} else {
x -= x0 ;
y -= y0 ;
}
x.mulpow2(mag) ;
y.mulpow2(mag) ;
int xx = 0 ;
int yy = 0 ;
/* AKT: don't do this clipping
if (x < 0)
xx = -1 ;
else if (x > getxmax())
xx = getxmax() + 1 ;
else
*/
xx = x.toint() ;
/* AKT: don't do this clipping
if (y < 0)
yy = -1 ;
else if (y > getymax())
yy = getymax() + 1 ;
else
*/
yy = y.toint() ;
return pair<int,int>(xx,yy) ;
}
void viewport::move(int dx, int dy) {
// dx and dy are in pixels
if (mag > 0) {
dx /= (1 << mag) ;
dy /= (1 << mag) ;
}
bigint addx = dx ;
bigint addy = dy ;
if (mag < 0) {
addx <<= -mag ;
addy <<= -mag ;
}
x += addx ;
y += addy ;
reposition() ;
}
void viewport::resize(int newwidth, int newheight) {
width = newwidth ;
height = newheight ;
reposition() ;
}
void viewport::center() {
x = 0 ;
y = 0 ;
reposition() ;
}
void viewport::reposition() {
xymf = pow(2.0, -mag) ;
x0 = - getxmax() ;
y0 = - getymax() ;
x0.mulpow2(-mag) ;
y0.mulpow2(-mag) ;
x0 += 1 ;
y0 += 1 ;
x0 >>= 1 ;
y0 >>= 1 ;
x0 += x ;
y0 += y ;
y0f = y0.todouble() ;
x0f = x0.todouble() ;
}
void viewport::setpositionmag(const bigint &xarg, const bigint &yarg,
int magarg) {
x = xarg ;
y = yarg ;
mag = magarg ;
reposition() ;
}
int viewport::contains(const bigint &xarg, const bigint &yarg) {
bigint t = x ;
t -= xarg ;
t.mulpow2(mag+1) ;
if (t <= -getxmax() || t >= getxmax()) {
return 0 ;
}
t = y ;
t -= yarg ;
t.mulpow2(mag+1) ;
if (t <= -getymax() || t >= getymax()) {
return 0 ;
}
return 1 ;
}
|