void Window_Base::DrawGauge(Game_Battler* actor, int cx, int cy) {
	FileRequestAsync* request = AsyncHandler::RequestFile("System2", Data::system.system2_name);
	if (!request->IsReady()) {
		// Gauge refreshed each frame, so we can wait via polling
		request->Start();
		return;
	}

	BitmapRef system2 = Cache::System2(Data::system.system2_name);

	bool full = actor->IsGaugeFull();
	int gauge_w = actor->GetGauge() / 4;

	// Which gauge (0 - 2)
	int gauge_y = 32 + 2 * 16;

	// Three components of the gauge
	Rect gauge_left(0, gauge_y, 16, 16);
	Rect gauge_center(16, gauge_y, 16, 16);
	Rect gauge_right(32, gauge_y, 16, 16);

	// Full or not full bar
	Rect gauge_bar(full ? 64 : 48, gauge_y, 16, 16);

	Rect dst_rect(cx + 16, cy, 25, 16);
	Rect bar_rect(cx + 16, cy, gauge_w, 16);

	contents->Blit(cx + 0, cy, *system2, gauge_left, 255);
	contents->Blit(cx + 16 + 25, cy, *system2, gauge_right, 255);

	contents->StretchBlit(dst_rect, *system2, gauge_center, 255);
	contents->StretchBlit(bar_rect, *system2, gauge_bar, 255);
}
void Window_BattleStatus::RefreshGauge() {
	if (Player::IsRPG2k3()) {
		if (Data::battlecommands.battle_type != RPG::BattleCommands::BattleType_gauge) {
			contents->ClearRect(Rect(198, 0, 25 + 16, 15 * item_max));
		}

		for (int i = 0; i < item_max; ++i) {
			Game_Battler* actor;
			if (enemy) {
				actor = &(*Main_Data::game_enemyparty)[i];
			}
			else {
				actor = &(*Main_Data::game_party)[i];
			}

			if (!enemy && Data::battlecommands.battle_type == RPG::BattleCommands::BattleType_gauge) {
				FileRequestAsync* request = AsyncHandler::RequestFile("System2", Data::system.system2_name);
				if (!request->IsReady()) {
					request_id = request->Bind(&Window_BattleStatus::OnSystem2Ready, this);
					request->Start();
					break;
				}
				else {
					BitmapRef system2 = Cache::System2(Data::system.system2_name);
					
					// Clear number drawing area
					contents->ClearRect(Rect(40 + 80 * i, 24, 8 * 4, 16));
					contents->ClearRect(Rect(40 + 80 * i, 24 + 12 + 4, 8 * 4, 16));

					// Number clearing removed part of the face, but both, clear and redraw
					// are needed because some games don't have face graphics that are huge enough
					// to clear the number area (e.g. Ara Fell)
					DrawActorFace(static_cast<Game_Actor*>(actor), 80 * i, 24);

					// Background
					contents->StretchBlit(Rect(32 + i * 80, 24, 57, 48), *system2, Rect(0, 32, 48, 48), Opacity::opaque);

					// HP
					DrawGaugeSystem2(48 + i * 80, 24, actor->GetHp(), actor->GetMaxHp(), 0);
					// SP
					DrawGaugeSystem2(48 + i * 80, 24 + 16, actor->GetSp(), actor->GetMaxSp(), 1);
					// Gauge
					DrawGaugeSystem2(48 + i * 80, 24 + 16 * 2, actor->GetGauge() * actor->GetMaxGauge() / 100, actor->GetMaxGauge(), 2);
					
					// Numbers
					DrawNumberSystem2(40 + 80 * i, 24, actor->GetHp());
					DrawNumberSystem2(40 + 80 * i, 24 + 12 + 4, actor->GetSp());
				}
			}
			else {
				int y = 2 + i * 16;

				DrawGauge(actor, 198 - 10, y - 2);
				DrawActorSp(actor, 198, y, false);
			}
		}
	}
}
void Window_BattleStatus::Refresh() {
	contents->Clear();

	if (enemy) {
		item_max = Main_Data::game_enemyparty->GetBattlerCount();
	}
	else {
		item_max = Main_Data::game_party->GetBattlerCount();
	}

	item_max = std::min(item_max, 4);

	for (int i = 0; i < item_max; i++) {
		Game_Battler* actor;
		if (enemy) {
			actor = &(*Main_Data::game_enemyparty)[i];
		}
		else {
			actor = &(*Main_Data::game_party)[i];
		}

		if (!enemy && Data::battlecommands.battle_type == RPG::BattleCommands::BattleType_gauge) {
			FileRequestAsync* request = AsyncHandler::RequestFile("System2", Data::system.system2_name);
			if (!request->IsReady()) {
				request_id = request->Bind(&Window_BattleStatus::OnSystem2Ready, this);
				request->Start();
				break;
			}
			else {
				DrawActorFace(static_cast<Game_Actor*>(actor), 80 * i, 24);
			}
		}
		else {
			int y = 2 + i * 16;

			DrawActorName(actor, 4, y);
			DrawActorState(actor, 84, y);
			DrawActorHp(actor, 126, y, true);
			DrawActorSp(actor, 198, y, false);
		}
	}

	RefreshGauge();
}