#ifndef TEST_APP_DRAWINGGAME_H_
#define TEST_APP_DRAWINGGAME_H_
+#include <cmath>
#include <sstream>
#include <vector>
DrawingGame(cairo::Context &ctx, int w, int h, const gfx::ColorRGB &base_color)
: Game()
, w(w), h(h), x(0), y(0)
- , step_size(1)
- , color_intensity(9)
+ , number(0)
, brush{ 0, 0, 0 }
, pos{ 760, 100 }
, font_color{ 1, 1, 1 }
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
- if (IsMovementCharacter(next)) {
- SetStepSize(c - '0');
- } else if (IsColorCharacter(next)) {
- SetColorIntensity(c - '0');
- }
+ AddDigit(c - '0');
break;
case 'w': case 'W':
case 'i': case 'I':
public:
void MoveUp() {
- y = (y - step_size) % h;
+ y = (y - GetAbsoluteNumber(1, h)) % h;
while (y < 0) y += h;
text_dirty = true;
- SetStepSize(1);
+ ConsumeNumber();
}
void MoveDown() {
- y = (y + step_size) % h;
+ y = (y + GetAbsoluteNumber(1, h)) % h;
while (y < 0) y += h;
text_dirty = true;
- SetStepSize(1);
+ ConsumeNumber();
}
void MoveLeft() {
- x = (x - step_size) % w;
+ x = (x - GetAbsoluteNumber(1, w)) % w;
while (x < 0) x += w;
text_dirty = true;
- SetStepSize(1);
+ ConsumeNumber();
}
void MoveRight() {
- x = (x + step_size) % w;
+ x = (x + GetAbsoluteNumber(1, w)) % w;
while (x < 0) x += w;
text_dirty = true;
- SetStepSize(1);
+ ConsumeNumber();
}
void SetColor(const gfx::ColorRGB &c) {
}
void SetRed() {
- brush.r = double(color_intensity) / 9.0;
- SetColorIntensity(9);
+ brush.r = GetColorIntensity();
+ ConsumeNumber();
}
void SetGreen() {
- brush.g = double(color_intensity) / 9.0;
- SetColorIntensity(9);
+ brush.g = GetColorIntensity();
+ ConsumeNumber();
}
void SetBlue() {
- brush.b = double(color_intensity) / 9.0;
- SetColorIntensity(9);
+ brush.b = GetColorIntensity();
+ ConsumeNumber();
}
void SetCyan() {
- brush.r = 1.0 - double(color_intensity) / 9.0;
- SetColorIntensity(9);
+ brush.r = 1.0 - GetColorIntensity();
+ ConsumeNumber();
}
void SetMagenta() {
- brush.g = 1.0 - double(color_intensity) / 9.0;
- SetColorIntensity(9);
+ brush.g = 1.0 - GetColorIntensity();
+ ConsumeNumber();
}
void SetYellow() {
- brush.b = 1.0 - double(color_intensity) / 9.0;
- SetColorIntensity(9);
+ brush.b = 1.0 - GetColorIntensity();
+ ConsumeNumber();
}
void Pick() {
brush.b = 1.0 - brush.b;
}
- void SetColorIntensity(int m) {
- color_intensity = std::max(0, std::min(9, m));
+ void AddDigit(int d) {
+ number = number * 10 + d;
+ ++digits;
}
- void SetStepSize(int m) {
- step_size = std::max(0, std::min(9, m));
+ int GetAbsoluteNumber(int min, int max) const {
+ return std::max(min, std::min(max, number));
+ }
+
+ double GetRelativeNumber() const {
+ if (digits == 0) return 0.0;
+ int max = std::pow(10.0, digits) - 1.0;
+ return double(number) / max;
+ }
+
+ double GetColorIntensity() const {
+ if (digits == 0) return 1.0;
+ return GetRelativeNumber();
+ }
+
+ void ConsumeNumber() {
+ number = 0;
+ digits = 0;
}
gfx::ColorRGB &CurrentCell() {
private:
std::vector<gfx::ColorRGB> cells;
int w, h, x, y;
- int step_size;
- int color_intensity;
+ int number;
+ int digits;
gfx::ColorRGB brush;
std::string command_queue;