| 151 | } |
| 152 | |
| 153 | sp<Image> AgentSheet::createStatsBar(int initialValue, int currentValue, int modifiedValue, |
| 154 | int maxValue, const std::pair<Colour, Colour> &colours, |
| 155 | Vec2<int> imageSize) |
| 156 | { |
| 157 | // Some agent types (e.g. Android's Psi-attack) have zero values. Break out here to avoid |
| 158 | // dividing by zero. |
| 159 | if (initialValue == 0 && currentValue == 0 && modifiedValue == 0 && maxValue == 0) |
| 160 | { |
| 161 | return mksp<RGBImage>(imageSize); |
| 162 | } |
| 163 | LogAssert(initialValue >= 0); |
| 164 | LogAssert(currentValue >= 0); |
| 165 | LogAssert(currentValue >= initialValue); |
| 166 | LogAssert(modifiedValue >= 0); |
| 167 | LogAssert(maxValue > 0); |
| 168 | // Need at least 3 y pixels for the 'hollow' bar |
| 169 | LogAssert(imageSize.x > 0 && imageSize.y > 2); |
| 170 | |
| 171 | int initialPixels = (float)imageSize.x * ((float)initialValue / (float)maxValue); |
| 172 | int currentPixels = (float)imageSize.x * ((float)currentValue / (float)maxValue); |
| 173 | int modifiedPixels = (float)imageSize.x * ((float)modifiedValue / (float)maxValue); |
| 174 | initialPixels = clamp(initialPixels, 0, imageSize.x - 1); |
| 175 | currentPixels = clamp(currentPixels, 0, imageSize.x - 1); |
| 176 | modifiedPixels = clamp(modifiedPixels, 0, imageSize.x - 1); |
| 177 | |
| 178 | sp<RGBImage> img = mksp<RGBImage>(imageSize); |
| 179 | RGBImageLock l(img); |
| 180 | |
| 181 | for (int x = 0; x < imageSize.x; x++) |
| 182 | { |
| 183 | for (int y = 0; y < imageSize.y; y++) |
| 184 | { |
| 185 | { |
| 186 | const Colour &col = x <= initialPixels ? colours.first : colours.second; |
| 187 | if (x < currentPixels && (y == 0 || y == imageSize.y - 1 || x <= modifiedPixels || |
| 188 | x == currentPixels - 1)) |
| 189 | { |
| 190 | l.set({x, y}, col); |
| 191 | } |
| 192 | else |
| 193 | l.set({x, y}, bkgColour); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return img; |
| 198 | } |
| 199 | |
| 200 | }; // namespace OpenApoc |