| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "Pedometer.h" |
| 10 |
|
| 11 |
#include "LevelStatus.h" |
| 12 |
#include "Level.h" |
| 13 |
#include "PedoInput.h" |
| 14 |
#include "LayeredPicture.h" |
| 15 |
|
| 16 |
#include "ResImagePack.h" |
| 17 |
#include "StateManager.h" |
| 18 |
#include "NodeDrawer.h" |
| 19 |
#include "SolverDrawer.h" |
| 20 |
#include "Level.h" |
| 21 |
#include "minmax.h" |
| 22 |
#include "StringMsg.h" |
| 23 |
#include "UnknownMsgException.h" |
| 24 |
|
| 25 |
//----------------------------------------------------------------- |
| 26 |
Pedometer::Pedometer(LevelStatus *status, Level *new_level) |
| 27 |
{ |
| 28 |
m_level = new_level; |
| 29 |
m_status = status; |
| 30 |
m_solution = m_status->readSolvedMoves(); |
| 31 |
m_meterPhase = 0; |
| 32 |
m_bg = NULL; |
| 33 |
|
| 34 |
prepareBg(); |
| 35 |
prepareRack(); |
| 36 |
|
| 37 |
m_numbers = ResImagePack::loadImage( |
| 38 |
Path::dataReadPath("images/menu/numbers.png")); |
| 39 |
|
| 40 |
takeHandler(new PedoInput(this)); |
| 41 |
registerDrawable(m_bg); |
| 42 |
registerDrawable(m_rack); |
| 43 |
registerDrawable(this); |
| 44 |
} |
| 45 |
//----------------------------------------------------------------- |
| 46 |
Pedometer::~Pedometer() |
| 47 |
{ |
| 48 |
if (m_level) { |
| 49 |
delete m_level; |
| 50 |
} |
| 51 |
SDL_FreeSurface(m_numbers); |
| 52 |
delete m_rack; |
| 53 |
delete m_bg; |
| 54 |
} |
| 55 |
//----------------------------------------------------------------- |
| 56 |
/** |
| 57 |
* Draw level name on background. |
| 58 |
*/ |
| 59 |
void |
| 60 |
Pedometer::prepareBg() |
| 61 |
{ |
| 62 |
SDL_Surface *bgSurface = ResImagePack::loadImage( |
| 63 |
Path::dataReadPath("images/menu/map.png")); |
| 64 |
NodeDrawer drawer; |
| 65 |
drawer.setScreen(bgSurface); |
| 66 |
drawer.drawSelected(m_level->getLevelName()); |
| 67 |
|
| 68 |
SolverDrawer solver(m_status); |
| 69 |
solver.setShift(V2((bgSurface->w - solver.getW()) / 2, |
| 70 |
bgSurface->h - 150)); |
| 71 |
solver.drawOn(bgSurface); |
| 72 |
|
| 73 |
if (m_bg) { |
| 74 |
m_bg->changePicture(bgSurface); |
| 75 |
} |
| 76 |
else { |
| 77 |
m_bg = new Picture(bgSurface, V2(0, 0)); |
| 78 |
} |
| 79 |
} |
| 80 |
//----------------------------------------------------------------- |
| 81 |
void |
| 82 |
Pedometer::prepareRack() |
| 83 |
{ |
| 84 |
static const int POS_X = 193; |
| 85 |
static const int POS_Y = 141; |
| 86 |
|
| 87 |
m_rack = new LayeredPicture( |
| 88 |
Path::dataReadPath("images/menu/pedometer.png"), |
| 89 |
V2(POS_X, POS_Y), |
| 90 |
Path::dataReadPath("images/menu/pedometer_lower.png"), |
| 91 |
Path::dataReadPath("images/menu/pedometer_mask.png")); |
| 92 |
|
| 93 |
//TODO: don't use numeric constants for coordinates |
| 94 |
m_maskRun = m_rack->getMaskAt(V2(86, 100)); |
| 95 |
m_maskReplay = m_rack->getMaskAt(V2(128, 100)); |
| 96 |
m_maskCancel = m_rack->getMaskAt(V2(170, 100)); |
| 97 |
m_activeMask = m_rack->getNoMask(); |
| 98 |
} |
| 99 |
|
| 100 |
//----------------------------------------------------------------- |
| 101 |
/** |
| 102 |
* Display menu and play menu music. |
| 103 |
*/ |
| 104 |
void |
| 105 |
Pedometer::own_initState() |
| 106 |
{ |
| 107 |
registerWatcher("lang"); |
| 108 |
own_resumeState(); |
| 109 |
} |
| 110 |
//----------------------------------------------------------------- |
| 111 |
void |
| 112 |
Pedometer::own_updateState() |
| 113 |
{ |
| 114 |
watchCursor(); |
| 115 |
} |
| 116 |
|
| 117 |
//----------------------------------------------------------------- |
| 118 |
/** |
| 119 |
* Mark pixel under cursor as ative mask. |
| 120 |
*/ |
| 121 |
void |
| 122 |
Pedometer::watchCursor() |
| 123 |
{ |
| 124 |
V2 mouseLoc = getInput()->getMouseLoc(); |
| 125 |
m_activeMask = m_rack->getMaskAtWorld(mouseLoc); |
| 126 |
if (m_activeMask == m_maskRun |
| 127 |
|| m_activeMask == m_maskReplay |
| 128 |
|| m_activeMask == m_maskCancel) |
| 129 |
{ |
| 130 |
m_rack->setActiveMask(m_activeMask); |
| 131 |
} |
| 132 |
else { |
| 133 |
m_rack->setNoActive(); |
| 134 |
} |
| 135 |
} |
| 136 |
//----------------------------------------------------------------- |
| 137 |
/** |
| 138 |
* Start selected button. |
| 139 |
*/ |
| 140 |
void |
| 141 |
Pedometer::runSelected() |
| 142 |
{ |
| 143 |
if (m_activeMask == m_maskRun) { |
| 144 |
runLevel(); |
| 145 |
} |
| 146 |
else if (m_activeMask == m_maskReplay) { |
| 147 |
runReplay(); |
| 148 |
} |
| 149 |
else { |
| 150 |
quitState(); |
| 151 |
} |
| 152 |
} |
| 153 |
//----------------------------------------------------------------- |
| 154 |
void |
| 155 |
Pedometer::runLevel() |
| 156 |
{ |
| 157 |
Level *levelState = m_level; |
| 158 |
m_level = NULL; |
| 159 |
GameState *poster = m_status->createPoster(); |
| 160 |
if (poster) { |
| 161 |
poster->setNextState(levelState); |
| 162 |
changeState(poster); |
| 163 |
} |
| 164 |
else { |
| 165 |
changeState(levelState); |
| 166 |
} |
| 167 |
} |
| 168 |
//----------------------------------------------------------------- |
| 169 |
void |
| 170 |
Pedometer::runReplay() |
| 171 |
{ |
| 172 |
Level *levelState = m_level; |
| 173 |
m_level = NULL; |
| 174 |
changeState(levelState); |
| 175 |
levelState->loadReplay(m_solution); |
| 176 |
} |
| 177 |
//----------------------------------------------------------------- |
| 178 |
void |
| 179 |
Pedometer::drawOn(SDL_Surface *screen) |
| 180 |
{ |
| 181 |
drawNumbers(screen, m_solution.size()); |
| 182 |
} |
| 183 |
//----------------------------------------------------------------- |
| 184 |
/** |
| 185 |
* Draw number of steps. |
| 186 |
* Draw nice rotating numbers. |
| 187 |
*/ |
| 188 |
void |
| 189 |
Pedometer::drawNumbers(SDL_Surface *screen, int value) |
| 190 |
{ |
| 191 |
static const int CIPHERS = 5; |
| 192 |
static const int POS_X = 275; |
| 193 |
static const int POS_Y = 177; |
| 194 |
static const int SHIFT_SPEED = 8; |
| 195 |
|
| 196 |
int numberWidth = m_numbers->w; |
| 197 |
int numberHeight = m_numbers->h / 10; |
| 198 |
|
| 199 |
for (int i = CIPHERS - 1; i >= 0; --i) { |
| 200 |
int cipher = value % 10; |
| 201 |
value /= 10; |
| 202 |
int x = POS_X + numberWidth * i; |
| 203 |
int shiftY = max(numberHeight * (9 - cipher), |
| 204 |
numberHeight * 9 - SHIFT_SPEED * m_meterPhase); |
| 205 |
m_meterPhase++; |
| 206 |
|
| 207 |
drawNumber(screen, x, POS_Y, shiftY); |
| 208 |
} |
| 209 |
} |
| 210 |
//----------------------------------------------------------------- |
| 211 |
void |
| 212 |
Pedometer::drawNumber(SDL_Surface *screen, int x, int y, int shiftY) |
| 213 |
{ |
| 214 |
SDL_Rect dest_rect; |
| 215 |
dest_rect.x = x; |
| 216 |
dest_rect.y = y; |
| 217 |
|
| 218 |
SDL_Rect src_rect; |
| 219 |
src_rect.x = 0; |
| 220 |
src_rect.y = shiftY; |
| 221 |
src_rect.w = m_numbers->w; |
| 222 |
src_rect.h = m_numbers->h / 10; |
| 223 |
|
| 224 |
SDL_BlitSurface(m_numbers, &src_rect, screen, &dest_rect); |
| 225 |
} |
| 226 |
//----------------------------------------------------------------- |
| 227 |
/** |
| 228 |
* Handle incoming message. |
| 229 |
* Messages: |
| 230 |
* - param_changed(lang) ... refresh bg |
| 231 |
* |
| 232 |
* @throws UnknownMsgException |
| 233 |
*/ |
| 234 |
void |
| 235 |
Pedometer::receiveString(const StringMsg *msg) |
| 236 |
{ |
| 237 |
if (msg->equalsName("param_changed")) { |
| 238 |
std::string param = msg->getValue(); |
| 239 |
if ("lang" == param) { |
| 240 |
prepareBg(); |
| 241 |
} |
| 242 |
else { |
| 243 |
throw UnknownMsgException(msg); |
| 244 |
} |
| 245 |
} |
| 246 |
else { |
| 247 |
throw UnknownMsgException(msg); |
| 248 |
} |
| 249 |
} |