Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/guielementcontainer.cpp
00001 /*
00002  * guielementcontainer.cpp - Kukatz 3D
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "guielementcontainer.hpp"
00026 
00027 #include <cmath>
00028 #include <iostream>
00029 
00030 #include "misc.hpp"
00031 #include "string.hpp"
00032 #include "guielement.hpp"
00033 
00034 GUIElementContainer::GUIElementContainer(float ar)
00035 {
00036         aspect_ratio = ar;
00037 }
00038 
00039 void GUIElementContainer::attach(GUIElement* e)
00040 {
00041         children.push_back(e);
00042 }
00043 
00044 void GUIElementContainer::attach(GUIElement* e, float s, ScaleMode sm,
00045         float ax, float ay)
00046 {
00047         e->scale = s;
00048         e->scale_mode = sm;
00049         e->child_align.x = e->parent_align.x = ax;
00050         e->child_align.y = e->parent_align.y = ay;
00051         
00052         children.push_back(e);
00053 }
00054 
00055 void GUIElementContainer::attach(GUIElement* e, float s, ScaleMode sm,
00056         float pax, float pay, float cax, float cay)
00057 {
00058         e->scale = s;
00059         e->scale_mode = sm;
00060         
00061         e->parent_align.x = pax;
00062         e->parent_align.y = pay;
00063         
00064         e->child_align.x = cax;
00065         e->child_align.y = cay;
00066         
00067         children.push_back(e);
00068 }
00069 
00070 void GUIElementContainer::render()
00071 {
00072         for (unsigned int i = 0; i < children.size(); ++i)
00073         {
00074                 sf::Vector2f child_size;
00075                 
00076                 switch (children[i]->scale_mode)
00077                 {
00078                         case SCALE_MODE_HORIZONTAL:
00079                         {
00080                                 child_size.x = (float)size.x * children[i]->scale;
00081                                 child_size.y = child_size.x / children[i]->aspect_ratio;
00082                         }
00083                         break;
00084                         case SCALE_MODE_VERTICAL:
00085                         {
00086                                 child_size.y = (float)size.y * children[i]->scale;
00087                                 child_size.x = child_size.y * children[i]->aspect_ratio;
00088                         }
00089                         break;
00090                         case SCALE_MODE_SMALLER:
00091                         {
00092                                 if (((float)size.x * children[i]->scale) // horizontal
00093                                         < ((float)size.y * children[i]->scale * children[i]->aspect_ratio))
00094                                 {
00095                                         child_size.x = (float)size.x * children[i]->scale;
00096                                         child_size.y = child_size.x / children[i]->aspect_ratio;
00097                                 }
00098                                 else // vertical
00099                                 {
00100                                         child_size.y = (float)size.y * children[i]->scale;
00101                                         child_size.x = child_size.y * children[i]->aspect_ratio;
00102                                 }
00103                         }
00104                         break;
00105                         case SCALE_MODE_BIGGER:
00106                         {
00107                                 if (((float)size.x * children[i]->scale) // horizontal
00108                                         > ((float)size.y * children[i]->scale * children[i]->aspect_ratio))
00109                                 {
00110                                         child_size.x = (float)size.x * children[i]->scale;
00111                                         child_size.y = child_size.x / children[i]->aspect_ratio;
00112                                 }
00113                                 else // vertical
00114                                 {
00115                                         child_size.y = (float)size.y * children[i]->scale;
00116                                         child_size.x = child_size.y * children[i]->aspect_ratio;
00117                                 }
00118                         }
00119                         break;
00120                 }
00121                 
00122                 children[i]->size = ftoi2(child_size);
00123                 
00124                 glPushMatrix();
00125                 
00126                 glTranslatef((float)size.x * children[i]->parent_align.x - child_size.x * children[i]->child_align.x,
00127                         (float)size.y * children[i]->parent_align.y - child_size.y * children[i]->child_align.y, 0);
00128                 
00129                 children[i]->render();
00130                 
00131                 /*
00132                 glDisable(GL_DEPTH_TEST);
00133                 Texture::unbind();
00134                 glColor4f(1, 1, 0, 1);
00135                 glBegin(GL_LINE_LOOP);
00136                 glVertex2i(0, 0);
00137                 glVertex2i(child_size.x, 0);
00138                 glVertex2i(child_size.x, child_size.y);
00139                 glVertex2i(0, child_size.y);
00140                 glEnd();
00141                 glEnable(GL_DEPTH_TEST);
00142                 glColor3f(1, 1, 1);
00143                 */
00144                 
00145                 glPopMatrix();
00146         }
00147 }
00148 
00149 GUIElementContainer::~GUIElementContainer()
00150 {
00151         for (unsigned int i = 0; i < children.size(); ++i)
00152         {
00153                 delete children[i];
00154         }
00155 }
 Összes Osztályok