Bubba-3D  0.9.0
Awesome game engine!
HUDGraphic.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include <linmath/float2.h>
18 #include <linmath/float3.h>
19 #include <linmath/float4.h>
20 #include <Texture.h>
21 #include <Dimension.h>
22 #include "HUDGraphic.h"
23 #include <iostream>
24 #include <sstream>
25 #include <stdexcept>
26 
27 using namespace chag;
28 
30  texture(texture), textureElseColor(true) {}
31 
32 HUDGraphic::HUDGraphic(Color color) : color(color.getColor()) , textureElseColor(false){ }
33 
34 float3 HUDGraphic::getCenterOffset(float width, float height) {
35  return make_vector(offsetX.getSize(width), offsetY.getSize(height), 0.0f);
36 }
37 
39  this->offsetX = offsetX;
40  this->offsetY = offsetY;
41  return this;
42 }
43 
45  this->texturePosition = texturePosition;
46  return this;
47 }
48 
49 HUDGraphic::TexturePosition<float> HUDGraphic::getTexturePosition() {
50  TexturePosition<int> tp = texturePosition;
51  if (tp.isEmpty()) {
52  return TexturePosition<float>(0.0f,0.0f,1.0f,1.0f);
53  } else {
54  float width = 1.0f, height = 1.0f;
55  if (isTextureElseColor()) {
56  width = texture->getWidth();
57  height = texture->getHeight();
58  }
59  return TexturePosition<float>((float) tp.botLeftX / width, (float) tp.botLeftY / height,
60  (float) tp.topRightX / width, (float) tp.topRightY / height);
61  }
62 }
63 
64 template <typename T>
66  return empty;
67 }
68 
70  if(!textureElseColor)
71  throw std::runtime_error("This graphic doesn't have a texture.");
72  return texture;
73 }
74 
76  return textureElseColor;
77 }
78 
80  if(textureElseColor)
81  throw std::runtime_error("This graphic doesn't have a solid color.");
82  return color;
83 }
84 
85 HUDGraphic* HUDGraphic::setBorder(int pixels, Color borderColor) {
86  return setBorder(pixels, pixels, borderColor);
87 }
88 
89 HUDGraphic* HUDGraphic::setBorder(int botAndTop, int leftAndRight, Color borderColor) {
90  return setBorder(botAndTop, leftAndRight, botAndTop, leftAndRight, borderColor);
91 }
92 
93 HUDGraphic* HUDGraphic::setBorder(int top, int right, int bot, int left, Color borderColor) {
94  this->borderColor = borderColor.getColor();
95  borders[0] = top;
96  borders[1] = right;
97  borders[2] = bot;
98  borders[3] = left;
99  return this;
100 }
101 
103  return borderColor;
104 }
105 
107  return borders;
108 }
109 
110 HUDGraphic* HUDGraphic::setRoundedCorners(int pixels){
111  return setRoundedCorners(pixels, pixels, pixels, pixels);
112 }
113 
114 HUDGraphic* HUDGraphic::setRoundedCorners(int topLeft, int topRight, int botRight, int botLeft) {
115  roundedCorners[0] = topLeft;
116  roundedCorners[1] = topRight;
117  roundedCorners[2] = botLeft;
118  roundedCorners[3] = botRight;
119  return this;
120 }
121 
123  return roundedCorners;
124 }
125 
126 HUDGraphic* HUDGraphic::setBorderSize(int top, int right, int bot, int left) {
127  if(top != -1)
128  this->borders[0] = top;
129  if(right != -1)
130  this->borders[1] = right;
131  if(bot != -1)
132  this->borders[2] = bot;
133  if(left != -1)
134  this->borders[3] = left;
135  return this;
136 }
137 
138 HUDGraphic* HUDGraphic::setBorderColor(Color borderColor) {
139  this->borderColor = borderColor.getColor();
140  return this;
141 }
142 
143 HUDGraphic* HUDGraphic::setBackground(Color color) {
144  textureElseColor = false;
145  this->color = color.getColor();
146  return this;
147 }
148 
149 HUDGraphic* HUDGraphic::setBackground(Texture *texture) {
150  textureElseColor = true;
151  this->texture = texture;
152  return this;
153 }
154 
155 HUDGraphic::Color::Color(int red, int green, int blue, float opacity)
156  : Color(make_vector((float)red / 255.0f,
157  (float)green / 255.0f,
158  (float)blue / 255.0f, opacity))
159 {
160 }
161 
162 HUDGraphic::Color::Color(float4 rawColor) : color(rawColor) {
163 }
164 
165 HUDGraphic::Color::Color(int red, int green, int blue): Color(red,green,blue,1.0f) {
166 }
167 
168 HUDGraphic::Color::Color(std::string hexString): Color(hexString,1.0f) {
169 }
170 
171 HUDGraphic::Color::Color(std::string hexString, float opacity) {
172 
173  if(hexString.length() == 0) {
174  throw std::invalid_argument("The color hexString can't be empty in HUDGraphic::Color::Color().");
175  }
176 
177  if(hexString[0] == '#') {
178  hexString = hexString.substr(1,hexString.length()-1);
179  }
180 
181  if (hexString.find_first_not_of("0123456789abcdefABCDEF", 0) != std::string::npos) {
182  throw std::invalid_argument("The color hexString can only contain: 0-9, a-f or A-F.");
183  }
184 
185  float divBy;
186  unsigned int valLen;
187  if (hexString.length() == 3) {
188  divBy = 15.0f;
189  valLen = 1;
190  } else if (hexString.length() == 6) {
191  divBy = 255.0f;
192  valLen = 2;
193  } else {
194  throw std::invalid_argument("The color hexString has to be of length 3 or 6 in HUDGraphic::Color::Color().");
195  }
196 
197  hexStringToFloat(hexString, 0, valLen, &color.x);
198  hexStringToFloat(hexString, valLen*1, valLen, &color.y);
199  hexStringToFloat(hexString, valLen*2, valLen, &color.z);
200  color /= divBy;
201 
202  color.w = opacity;
203 }
204 
205 void HUDGraphic::Color::hexStringToFloat(std::string hexString, unsigned int pos, unsigned int len, float* target){
206  int temp = 0;
207  std::stringstream(hexString.substr(pos,len)) >> std::hex >> temp;
208  *target = (float)temp;
209 }
210 
212  return color;
213 }
chag::float4 getColor()
Definition: HUDGraphic.cpp:211
float getSize(float room)
Definition: Dimension.cpp:23
HUDGraphic * setCenterOffset(Dimension offsetX, Dimension offsetY)
Definition: HUDGraphic.cpp:38
Color(std::string hexString)
Definition: HUDGraphic.cpp:168
chag::float4 getBorderColor()
Definition: HUDGraphic.cpp:102
Texture * getTexture()
Definition: HUDGraphic.cpp:69
int * getBorders()
Definition: HUDGraphic.cpp:106
chag::float4 getColor()
Definition: HUDGraphic.cpp:79
bool isTextureElseColor()
Definition: HUDGraphic.cpp:75
HUDGraphic * setTexturePosition(TexturePosition< int > texturePosition)
Definition: HUDGraphic.cpp:44
HUDGraphic(Texture *texture)
Definition: HUDGraphic.cpp:29
int * getRoundedCorners()
Definition: HUDGraphic.cpp:122