]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Gauge.cpp
d0e891a74b56e345d8d065141bacc8d731ba11c8
[l2e.git] / src / graphics / Gauge.cpp
1 /*
2  * Gauge.cpp
3  *
4  *  Created on: Aug 7, 2012
5  *      Author: holy
6  */
7
8 #include "Gauge.h"
9
10 #include "../loader/TypeDescription.h"
11
12 using geometry::Vector;
13 using loader::FieldDescription;
14 using loader::TypeDescription;
15
16 namespace graphics {
17
18 void Gauge::Draw(SDL_Surface *dest, const Vector<int> &position, int width, Uint8 fill) const {
19         SDL_Rect srcRect, destRect;
20
21         int filledWidth = fill * (width - startWidth - endWidth) / 255;
22         int emptyWidth = (255 - fill) * (width - startWidth - endWidth) / 255;
23
24         if (!surface) {
25                 destRect.x = position.X();
26                 destRect.y = position.Y();
27                 destRect.w = filledWidth + startWidth;
28                 destRect.h = height;
29                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0x00));
30                 destRect.x += destRect.w;
31                 destRect.w = emptyWidth + endWidth;
32                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
33                 return;
34         }
35
36         // start
37         srcRect.w = startWidth;
38         srcRect.h = height;
39         destRect.x = position.X();
40         destRect.y = position.Y();
41         // full part
42         if (fill == 0) {
43                 srcRect.x = emptyOffset.X();
44                 srcRect.y = emptyOffset.Y();
45                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
46         } else {
47                 srcRect.x = fullOffset.X();
48                 srcRect.y = fullOffset.Y();
49                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
50         }
51
52         destRect.x = position.X() + startWidth;
53
54         // fill
55         srcRect.x = fullOffset.X() + startWidth;
56         srcRect.y = fullOffset.Y();
57         srcRect.w = repeatWidth;
58         while (filledWidth > repeatWidth) {
59                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
60                 destRect.x += repeatWidth;
61                 filledWidth -= repeatWidth;
62         }
63         srcRect.x = emptyOffset.X() + startWidth;
64         srcRect.y = emptyOffset.Y();
65         while (emptyWidth > repeatWidth) {
66                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
67                 destRect.x += repeatWidth;
68                 emptyWidth -= repeatWidth;
69         }
70
71         // end
72         srcRect.w = endWidth;
73         if (fill == 255) {
74                 srcRect.x = fullOffset.X() + startWidth + repeatWidth;
75                 srcRect.y = fullOffset.Y();
76                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
77         } else {
78                 srcRect.x = emptyOffset.X() + startWidth + repeatWidth;
79                 srcRect.y = emptyOffset.Y();
80                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
81         }
82
83 }
84
85
86 void Gauge::CreateTypeDescription() {
87         Gauge g;
88
89         int imageId(TypeDescription::GetTypeId("Image"));
90         int numberId(TypeDescription::GetTypeId("Number"));
91         int vectorId(TypeDescription::GetTypeId("Vector"));
92
93         TypeDescription &td(TypeDescription::CreateOrGet("Gauge"));
94         td.SetDescription(
95                         "Gauges display a percentage by filling the left part different than the right.\n"
96                         "The fill level is only mapped with repeat.\n"
97                         "Start is filled if the level in greater than zero, end is filled if the level is at its maximum.");
98         td.SetConstructor(&Construct);
99         td.SetSize(sizeof(Gauge));
100
101         td.AddField("image", FieldDescription(((char *)&g.surface) - ((char *)&g), imageId).SetReferenced().SetDescription("the underlying graphic from which the gauge parts are cut"));
102         td.AddField("full", FieldDescription(((char *)&g.fullOffset) - ((char *)&g), vectorId).SetDescription("top-left corner of the filled gauge"));
103         td.AddField("empty", FieldDescription(((char *)&g.emptyOffset) - ((char *)&g), vectorId).SetDescription("top-left corner of the empty gauge"));
104         td.AddField("height", FieldDescription(((char *)&g.height) - ((char *)&g), numberId).SetDescription("height of the gauges"));
105         td.AddField("start", FieldDescription(((char *)&g.startWidth) - ((char *)&g), numberId).SetDescription("width of the start part"));
106         td.AddField("repeat", FieldDescription(((char *)&g.repeatWidth) - ((char *)&g), numberId).SetDescription("width of the repeat part"));
107         td.AddField("end", FieldDescription(((char *)&g.endWidth) - ((char *)&g), numberId).SetDescription("width of the end part"));
108 }
109
110 void Gauge::Construct(void *data) {
111         new (data) Gauge;
112 }
113
114 }