diff --git a/src/client/gui/Gui.cpp b/src/client/gui/Gui.cpp index 10e7bb4..6e85c55 100755 --- a/src/client/gui/Gui.cpp +++ b/src/client/gui/Gui.cpp @@ -135,10 +135,14 @@ void Gui::render(float a, bool mouseFree, int xMouse, int yMouse) { #endif #if defined(RPI) renderDebugInfo(); -#elif defined(PLATFORM_DESKTOP) +#endif + + if (Keyboard::isKeyDown(Keyboard::KEY_TAB)) { + renderPlayerList(font, screenWidth, screenHeight); + } + if (minecraft->options.getBooleanValue(OPTIONS_RENDER_DEBUG)) renderDebugInfo(); -#endif } glDisable(GL_BLEND); @@ -799,6 +803,83 @@ void Gui::renderDebugInfo() { t.endOverrideAndDraw(); } +void Gui::renderPlayerList(Font* font, int screenWidth, int screenHeight) { + // only show when in game, no other screen + // if (!minecraft->level) return; + + // only show the overlay while connected to a multiplayer server + Level* level = minecraft->level; + if (!level) return; + if (!level->isClientSide) return; + + std::vector playerNames; + playerNames.reserve(level->players.size()); + + for (Player* player : level->players) { + if (!player) continue; + playerNames.push_back(player->name); + } + + // is this check needed? if there are no players, the box won't render at all since height will be 0, + // but maybe we want to skip rendering entirely in that case + // if (playerNames.empty()) + // return; + + std::sort(playerNames.begin(), playerNames.end()); + + float maxNameWidth = 0.0f; + // find the longest name so we can size the box accordingly + for (const std::string& name : playerNames) { + float nameWidth = font->width(name); + if (nameWidth > maxNameWidth) + maxNameWidth = nameWidth; + } + + // player count title + std::string titleText = "Players (" + std::to_string(playerNames.size()) + ")"; + float titleWidth = font->width(titleText); + + if (titleWidth > maxNameWidth) + maxNameWidth = titleWidth; + + const float padding = 4.0f; + const float lineHeight = (float)Font::DefaultLineHeight; + + const float boxWidth = maxNameWidth + padding * 2; + const float boxHeight = (playerNames.size() + 1) * lineHeight + padding * 2; + + const float boxLeft = (screenWidth - boxWidth) / 2.0f; + const float boxTop = 10.0f; + const float boxRight = boxLeft + boxWidth; + const float boxBottom = boxTop + boxHeight; + + fill(boxLeft, boxTop, boxRight, boxBottom, 0x90000000); + + float titleX = (screenWidth - titleWidth) / 2.0f; + float titleY = boxTop + padding; + + // scale the text down slightly + // i think the gl scaling is the best for this + // oh my god this looks really bad OH GOD + //const float textScale = 0.8f; + //const float invTextScale = 1.0f / textScale; + //glPushMatrix2(); + //glScalef2(textScale, textScale, 1); + + // draw title + //font->draw(titleText, titleX * invTextScale, titleY * invTextScale, 0xFFFFFFFF); + font->draw(titleText, titleX, titleY, 0xFFFFFFFF); + + // draw player names + // we should add ping icons here eventually, but for now just show names + float currentY = boxTop + padding + lineHeight; + for (const std::string& name : playerNames) { + font->draw(name, (boxLeft + padding), currentY, 0xFFDDDDDD); + currentY += lineHeight; + } + //glPopMatrix2(); +} + void Gui::renderSleepAnimation( const int screenWidth, const int screenHeight ) { int timer = minecraft->player->getSleepTimer(); float amount = (float) timer / (float) Player::SLEEP_DURATION; diff --git a/src/client/gui/Gui.h b/src/client/gui/Gui.h index 9270af0..861bdf7 100755 --- a/src/client/gui/Gui.h +++ b/src/client/gui/Gui.h @@ -61,6 +61,7 @@ public: void renderBubbles(); void renderHearts(); void renderDebugInfo(); + void renderPlayerList(Font* font, int screenWidth, int screenHeight); void renderProgressIndicator( const bool isTouchInterface, const int screenWidth, const int screenHeight, float a ); diff --git a/src/main_glfw.h b/src/main_glfw.h index 027393e..0b1cd02 100755 --- a/src/main_glfw.h +++ b/src/main_glfw.h @@ -24,6 +24,7 @@ int transformKey(int glfwkey) { switch (glfwkey) { case GLFW_KEY_ESCAPE: return Keyboard::KEY_ESCAPE; + case GLFW_KEY_TAB: return Keyboard::KEY_TAB; case GLFW_KEY_BACKSPACE: return Keyboard::KEY_BACKSPACE; case GLFW_KEY_LEFT_SHIFT: return Keyboard::KEY_LSHIFT; case GLFW_KEY_ENTER: return Keyboard::KEY_RETURN; diff --git a/src/platform/input/Keyboard.h b/src/platform/input/Keyboard.h index d577416..91868a0 100755 --- a/src/platform/input/Keyboard.h +++ b/src/platform/input/Keyboard.h @@ -56,6 +56,7 @@ public: static const int KEY_Z = 90; static const int KEY_BACKSPACE = 8; + static const int KEY_TAB = 9; static const int KEY_RETURN = 13; static const int KEY_F1 = 112;