picture.cpp

Go to the documentation of this file.
00001 #ifndef PICTURE_IMPLEMENTATION_FILE
00002 #define PICTURE_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : picture                                                           *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "picture.h"
00019 #include "wp_implementation.h"
00020 
00021 #include <basis/guards.h>
00022 #include <data_struct/amorph.cpp>
00023 
00024 picture::picture(const c_point &origin)
00025 : drawable(origin), amorph<picture_part>(0), _origin(origin) {}
00026 
00027 picture::~picture() {}
00028 
00029 const drawable *picture::get(int index) const
00030   { return amorph<picture_part>::get(index)->stored; }
00031 
00032 drawable *picture::acquire(int index)
00033 {
00034   drawable *to_return = amorph<picture_part>::acquire(index)->stored;
00035   remove(index);
00036   return to_return;
00037 }
00038 
00039 void picture::add(drawable *to_add)
00040 {
00041   if (!to_add) return;
00042   picture_part *wrapper = new picture_part(to_add->origin());
00043   wrapper->stored = to_add;
00044   int i;
00045   for (i = 0; i < elements(); i++)
00046     if (get(i)->layer() > to_add->layer()) break;
00047   i--;
00048   if (i < 0) i = 0;
00049   if ( (i == elements() - 2) && (get(i+1)->layer() <= to_add->layer()) ) i++;
00050     // the index is checked for being at the top of the list (less than zero)
00051     // and for being at the penultimate end of the list when it should be at
00052     // the actual end.
00053   insert(i, 1);
00054   put(i, wrapper);
00055   to_add->set_origin(to_add->origin() + drawable::origin());
00056 }
00057 
00058 c_rectangle picture::dimensions() const
00059 {
00060   c_rectangle to_return(c_point(0, 0), c_point(0, 0));
00061   if (elements())
00062     to_return = get(0)->dimensions();
00063   for (int i = 1; i < elements(); i++)
00064     to_return.encompass(get(i)->dimensions());
00065   return to_return;
00066 }
00067 
00068 bool picture::remove(drawable *to_remove)
00069 {
00070   int num_elements = elements();
00071   for (int i = 0; i < num_elements; i++)
00072     if (get(i) == to_remove) { return remove(i); }
00073   return false;
00074 }
00075 
00076 void picture::draw()
00077 {
00078   for (int i = 0; i < elements(); i++) {
00079     picture_part *found = amorph<picture_part>::borrow(i);
00080     found->stored->draw();
00081   }
00082 }
00083 
00084 void picture::erase()
00085 {
00086   for (int i = 0; i < elements(); i++) {
00087     picture_part *found = amorph<picture_part>::borrow(i);
00088     found->stored->erase();
00089   }
00090 }
00091 
00092 void picture::draw_intersecting(const c_rectangle &region)
00093 {
00094   for (int i = 0; i < elements(); i++)
00095     if (region.intersect(get(i)->dimensions())) {
00096       picture_part *found = amorph<picture_part>::borrow(i);
00097       found->stored->draw();
00098     }
00099 }
00100 
00101 void picture::erase_intersecting(const c_rectangle &region)
00102 {
00103   for (int i = 0; i < elements(); i++)
00104     if (region.intersect(get(i)->dimensions())) {
00105       picture_part *found = amorph<picture_part>::borrow(i);
00106       found->stored->erase();
00107     }
00108 }
00109 
00110 bool picture::remove(int index)
00111 {
00112   bounds_return(index, 0, elements() - 1, false);
00113   zap(index, index);
00114   return true;
00115 }
00116 
00117 void picture::set_origin(const c_point &new_origin)
00118 {
00119   bool was_visible(visible());
00120   if (was_visible) erase();
00121 
00122   for (int i = 0; i < elements(); i++) {
00123     picture_part *found = amorph<picture_part>::borrow(i);
00124     found->stored->set_origin(new_origin + c_point(*found));
00125   }
00126   drawable::set_origin(new_origin);
00127 
00128   if (was_visible) draw();
00129 }
00130 
00131 void picture::update()
00132 {
00133 //  erase();
00134   amorph<picture_part> hold(elements());
00135   for (int i = 0; i < elements(); i++)
00136     hold.put(i, amorph<picture_part>::acquire(i));
00137 // i know, it's a fricking bubble sort.  oh well...
00138   for (int a = 0; a < hold.elements() - 1; a++)
00139     for (int b = a + 1; b < hold.elements(); b++)
00140       if (hold.get(a)->stored->layer() > hold.get(b)->stored->layer()) {
00141         picture_part *first = hold.acquire(a);
00142         hold.put(a, hold.acquire(b));
00143         hold.put(b, first);
00144       }
00145   for (int q = 0; q < elements(); q++) put(q, hold.acquire(q));
00146 //  draw();
00147 }
00148 
00149 #endif //PICTURE_IMPLEMENTATION_FILE
00150 

Generated on Fri Nov 28 04:29:05 2008 for HOOPLE Libraries by  doxygen 1.5.1