diff --git a/CMakeLists.txt b/CMakeLists.txt index 0295136..b7357e6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set(CMAKE_CXX_FLAGS "-Wno-c++11-narrowing -Wno-invalid-source-encoding -Wno-reserved-user-defined-literal") + set(CMAKE_CXX_FLAGS "-Wno-c++11-narrowing -Wno-narrowing -Wno-invalid-source-encoding -Wno-reserved-user-defined-literal") endif() CPMAddPackage("gh:madler/zlib@1.3.2") diff --git a/src/client/gui/Screen.cpp b/src/client/gui/Screen.cpp index d92ef58..17c9e10 100755 --- a/src/client/gui/Screen.cpp +++ b/src/client/gui/Screen.cpp @@ -25,6 +25,11 @@ void Screen::render( int xm, int ym, float a ) Button* button = buttons[i]; button->render(minecraft, xm, ym); } + + for (unsigned int i = 0; i < textBoxes.size(); i++) { + TextBox* textbox = textBoxes[i]; + textbox->render(minecraft, xm, ym); + } } void Screen::init( Minecraft* minecraft, int width, int height ) @@ -157,6 +162,11 @@ void Screen::keyPressed( int eventKey ) minecraft->setScreen(NULL); //minecraft->grabMouse(); } + + for (auto& textbox : textBoxes) { + textbox->handleKey(eventKey); + } + if (minecraft->useTouchscreen()) return; @@ -181,6 +191,14 @@ void Screen::keyPressed( int eventKey ) updateTabButtonSelection(); } +void Screen::keyboardNewChar(char inputChar) { + // yeah im using these modern cpp features in this project :sunglasses: + + for (auto& textbox : textBoxes) { + textbox->handleChar(inputChar); + } +} + void Screen::updateTabButtonSelection() { if (minecraft->useTouchscreen()) @@ -210,6 +228,10 @@ void Screen::mouseClicked( int x, int y, int buttonNum ) } } } + + for (auto& textbox : textBoxes) { + textbox->mouseClicked(minecraft, x, y, buttonNum); + } } void Screen::mouseReleased( int x, int y, int buttonNum ) @@ -253,3 +275,9 @@ void Screen::toGUICoordinate( int& x, int& y ) { x = x * width / minecraft->width; y = y * height / minecraft->height - 1; } + +void Screen::tick() { + for (auto& textbox : textBoxes) { + textbox->tick(minecraft); + } +} \ No newline at end of file diff --git a/src/client/gui/Screen.h b/src/client/gui/Screen.h index eebe588..ff48c25 100755 --- a/src/client/gui/Screen.h +++ b/src/client/gui/Screen.h @@ -31,7 +31,7 @@ public: virtual void keyboardTextEvent(); virtual bool handleBackEvent(bool isDown); - virtual void tick() {} + virtual void tick(); virtual void removed() {} @@ -58,7 +58,7 @@ protected: virtual void mouseReleased(int x, int y, int buttonNum); virtual void keyPressed(int eventKey); - virtual void keyboardNewChar(char inputChar) {} + virtual void keyboardNewChar(char inputChar); public: int width; int height; diff --git a/src/client/gui/components/TextBox.cpp b/src/client/gui/components/TextBox.cpp index 17c77ba..8035e53 100755 --- a/src/client/gui/components/TextBox.cpp +++ b/src/client/gui/components/TextBox.cpp @@ -1,25 +1,25 @@ #include "TextBox.h" #include "../../Minecraft.h" #include "../../../AppPlatform.h" -TextBox::TextBox( int id, const std::string& msg ) - : id(0), w(0), h(0), x(0), y(0), text(msg), focused(false) { +#include "platform/input/Mouse.h" -} +TextBox::TextBox( int id, const std::string& msg ) + : TextBox(id, 0, 0, msg) {} TextBox::TextBox( int id, int x, int y, const std::string& msg ) - : id(id), w(0), h(0), x(x), y(y), text(msg), focused(false) { + : TextBox(id, x, y, 24, msg) {} -} - -TextBox::TextBox( int id, int x, int y, int w, int h, const std::string& msg ) - : id(id), w(w), h(h), x(x), y(y), text(msg) { - -} +TextBox::TextBox( int id, int x, int y, int w, const std::string& msg ) + : GuiElement(true, true, x, y, w, Font::DefaultLineHeight + 4), + id(id), hint(msg), focused(false), blink(false) {} void TextBox::setFocus(Minecraft* minecraft) { if(!focused) { minecraft->platform()->showKeyboard(); focused = true; + + blinkTicks = 0; + blink = false; } } @@ -32,6 +32,59 @@ bool TextBox::loseFocus(Minecraft* minecraft) { return false; } -void TextBox::render( Minecraft* minecraft, int xm, int ym ) { - +void TextBox::mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum) { + if (buttonNum == MouseAction::ACTION_LEFT) { + if (pointInside(x, y)) { + setFocus(minecraft); + } else { + loseFocus(minecraft); + } + } +} + +void TextBox::handleChar(char c) { + if (focused) { + text.push_back(c); + } +} + +void TextBox::handleKey(int key) { + if (focused && key == Keyboard::KEY_BACKSPACE && !text.empty()) { + text.pop_back(); + } +} + +void TextBox::tick(Minecraft* minecraft) { + blinkTicks++; + + if (blinkTicks >= 5) { + blink = !blink; + blinkTicks = 0; + } +} + +void TextBox::render( Minecraft* minecraft, int xm, int ym ) { + // textbox like in beta 1.7.3 + fill(x, y, x + width, y + height, 0xffa0a0a0); + fill(x + 1, y + 1, x + width - 1, y + height - 1, 0xff000000); + + glEnable2(GL_SCISSOR_TEST); + glScissor( + Gui::GuiScale * (x + 2), + minecraft->height - Gui::GuiScale * (y + height - 2), + Gui::GuiScale * (width - 2), + Gui::GuiScale * (height - 2) + ); + + if (text.empty() && !focused) { + drawString(minecraft->font, hint, x + 2, y + 2, 0xff5e5e5e); + } + + if (focused && blink) text.push_back('_'); + + drawString(minecraft->font, text, x + 2, y + 2, 0xffffffff); + + if (focused && blink) text.pop_back(); + + glDisable2(GL_SCISSOR_TEST); } diff --git a/src/client/gui/components/TextBox.h b/src/client/gui/components/TextBox.h index 4de6182..822728b 100755 --- a/src/client/gui/components/TextBox.h +++ b/src/client/gui/components/TextBox.h @@ -4,31 +4,39 @@ //package net.minecraft.client.gui; #include -#include "../GuiComponent.h" +#include "GuiElement.h" #include "../../Options.h" class Font; class Minecraft; -class TextBox: public GuiComponent +class TextBox: public GuiElement { public: TextBox(int id, const std::string& msg); TextBox(int id, int x, int y, const std::string& msg); - TextBox(int id, int x, int y, int w, int h, const std::string& msg); + TextBox(int id, int x, int y, int w, const std::string& msg); + + virtual void mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum); virtual void setFocus(Minecraft* minecraft); virtual bool loseFocus(Minecraft* minecraft); virtual void render(Minecraft* minecraft, int xm, int ym); + + virtual void handleKey(int key); + virtual void handleChar(char c); + virtual void tick(Minecraft* minecraft); public: - int w, h; - int x, y; - + std::string hint; std::string text; int id; + + int blinkTicks; + bool focused; + bool blink; }; #endif /*NET_MINECRAFT_CLIENT_GUI_COMPONENTS__TextBox_H__*/ diff --git a/src/client/gui/screens/SimpleChooseLevelScreen.cpp b/src/client/gui/screens/SimpleChooseLevelScreen.cpp index a9deb36..ba667e0 100755 --- a/src/client/gui/screens/SimpleChooseLevelScreen.cpp +++ b/src/client/gui/screens/SimpleChooseLevelScreen.cpp @@ -5,52 +5,74 @@ #include "../../Minecraft.h" #include "../../../world/level/LevelSettings.h" #include "../../../platform/time.h" +#include "client/gamemode/GameMode.h" SimpleChooseLevelScreen::SimpleChooseLevelScreen(const std::string& levelName) -: bCreative(0), - bSurvival(0), +: + // bCreative(0), + bGamemode(0), bBack(0), + bCreate(0), levelName(levelName), - hasChosen(false) + hasChosen(false), + gamemode(GameType::Survival), + tLevelName(0, "World name"), + tSeed(1, "World seed") { } SimpleChooseLevelScreen::~SimpleChooseLevelScreen() { - delete bCreative; - delete bSurvival; + // delete bCreative; + delete bGamemode; delete bBack; } void SimpleChooseLevelScreen::init() { if (minecraft->useTouchscreen()) { - bCreative = new Touch::TButton(1, "Creative mode"); - bSurvival = new Touch::TButton(2, "Survival mode"); + // bCreative = new Touch::TButton(1, "Creative mode"); + bGamemode = new Touch::TButton(2, "Survival mode"); bBack = new Touch::TButton(3, "Back"); + bCreate = new Touch::TButton(4, "Create"); } else { - bCreative = new Button(1, "Creative mode"); - bSurvival = new Button(2, "Survival mode"); + // bCreative = new Button(1, "Creative mode"); + bGamemode = new Button(2, "Survival mode"); bBack = new Button(3, "Back"); + bCreate = new Button(4, "Create"); } - buttons.push_back(bCreative); - buttons.push_back(bSurvival); + // buttons.push_back(bCreative); + buttons.push_back(bGamemode); buttons.push_back(bBack); + buttons.push_back(bCreate); - tabButtons.push_back(bCreative); - tabButtons.push_back(bSurvival); + textBoxes.push_back(&tLevelName); + textBoxes.push_back(&tSeed); + + // tabButtons.push_back(bCreative); + tabButtons.push_back(bGamemode); tabButtons.push_back(bBack); + tabButtons.push_back(bCreate); } void SimpleChooseLevelScreen::setupPositions() { - bCreative->width = bSurvival->width = bBack->width = 120; - bCreative->x = (width - bCreative->width) / 2; - bCreative->y = height/3 - 40; - bSurvival->x = (width - bSurvival->width) / 2; - bSurvival->y = 2*height/3 - 40; - bBack->x = bSurvival->x + bSurvival->width - bBack->width; - bBack->y = height - 40; + const int padding = 5; + + /* bCreative->width = */ bGamemode->width = 120; + tLevelName.width = tSeed.width = 120; + bBack->width = bCreate->width = 60 - padding; + // bCreative->x = (width - bCreative->width) / 2; + // bCreative->y = height/3 - 40; + bGamemode->x = (width - bGamemode->width) / 2; + bGamemode->y = 2*height/3 - 30; + bBack->x = bGamemode->x; + bCreate->x = bGamemode->x + bGamemode->width - bCreate->width; + bBack->y = bCreate->y = height - 40; + + tLevelName.x = tSeed.x = bGamemode->x; + tLevelName.y = 20; + tSeed.y = tLevelName.y + 30; } void SimpleChooseLevelScreen::render( int xm, int ym, float a ) @@ -58,9 +80,21 @@ void SimpleChooseLevelScreen::render( int xm, int ym, float a ) renderDirtBackground(0); glEnable2(GL_BLEND); - drawCenteredString(minecraft->font, "Mobs, health and gather resources", width/2, bSurvival->y + bSurvival->height + 4, 0xffcccccc); - drawCenteredString(minecraft->font, "Unlimited resources and flying", width/2, bCreative->y + bCreative->height + 4, 0xffcccccc); + const char* str = NULL; + if (gamemode == GameType::Survival) { + str = "Mobs, health and gather resources"; + } else if (gamemode == GameType::Creative) { + str = "Unlimited resources and flying"; + } + + if (str) { + drawCenteredString(minecraft->font, str, width/2, bGamemode->y + bGamemode->height + 4, 0xffcccccc); + } + + drawString(minecraft->font, "World name:", tLevelName.x, tLevelName.y - Font::DefaultLineHeight - 2, 0xffcccccc); + drawString(minecraft->font, "World seed:", tSeed.x, tSeed.y - Font::DefaultLineHeight - 2, 0xffcccccc); + Screen::render(xm, ym, a); glDisable2(GL_BLEND); } @@ -74,20 +108,33 @@ void SimpleChooseLevelScreen::buttonClicked( Button* button ) if (hasChosen) return; - int gameType; + if (button == bGamemode) { + gamemode ^= 1; + bGamemode->msg = (gamemode == GameType::Survival) ? "Survival mode" : "Creative mode"; + } - if (button == bCreative) - gameType = GameType::Creative; + if (button == bCreate) { + int seed = getEpochTimeS(); - if (button == bSurvival) - gameType = GameType::Survival; + if (!tSeed.text.empty()) { + std::string seedString = Util::stringTrim(tSeed.text); + int tmpSeed; + // Try to read it as an integer + if (sscanf(seedString.c_str(), "%d", &tmpSeed) > 0) { + seed = tmpSeed; + } // Hash the "seed" + else { + seed = Util::hashCode(seedString); + } + } - std::string levelId = getUniqueLevelName(levelName); - LevelSettings settings(getEpochTimeS(), gameType); - minecraft->selectLevel(levelId, levelId, settings); - minecraft->hostMultiplayer(); - minecraft->setScreen(new ProgressScreen()); - hasChosen = true; + std::string levelId = getUniqueLevelName(tLevelName.text); + LevelSettings settings(seed, gamemode); + minecraft->selectLevel(levelId, levelId, settings); + minecraft->hostMultiplayer(); + minecraft->setScreen(new ProgressScreen()); + hasChosen = true; + } } bool SimpleChooseLevelScreen::handleBackEvent(bool isDown) { diff --git a/src/client/gui/screens/SimpleChooseLevelScreen.h b/src/client/gui/screens/SimpleChooseLevelScreen.h index 12e24a3..88c77af 100755 --- a/src/client/gui/screens/SimpleChooseLevelScreen.h +++ b/src/client/gui/screens/SimpleChooseLevelScreen.h @@ -2,6 +2,8 @@ #define NET_MINECRAFT_CLIENT_GUI_SCREENS__DemoChooseLevelScreen_H__ #include "ChooseLevelScreen.h" +#include "../components/TextBox.h" + class Button; class SimpleChooseLevelScreen: public ChooseLevelScreen @@ -21,12 +23,18 @@ public: bool handleBackEvent(bool isDown); private: - Button* bCreative; - Button* bSurvival; + // Button* bCreative; + Button* bGamemode; Button* bBack; + Button* bCreate; bool hasChosen; std::string levelName; + + int gamemode; + + TextBox tLevelName; + TextBox tSeed; }; #endif /*NET_MINECRAFT_CLIENT_GUI_SCREENS__DemoChooseLevelScreen_H__*/ diff --git a/src/client/gui/screens/touch/TouchStartMenuScreen.cpp b/src/client/gui/screens/touch/TouchStartMenuScreen.cpp index 9adad35..ee51e4a 100755 --- a/src/client/gui/screens/touch/TouchStartMenuScreen.cpp +++ b/src/client/gui/screens/touch/TouchStartMenuScreen.cpp @@ -22,62 +22,6 @@ #include "../DialogDefinitions.h" #include "../SimpleChooseLevelScreen.h" -// -// Buy Button implementation -// -BuyButton::BuyButton(int id) -: super(id, "") -{ - ImageDef def; - // Setup the source rectangle - def.setSrc(IntRectangle(64, 182, 190, 55)); - def.width = 75;//rc.w / 3; - def.height = 75 * (55.0f / 190.0f);//rc.h / 3; - def.name = "gui/gui.png"; - - setImageDef(def, true); -} - -void BuyButton::render(Minecraft* minecraft, int xm, int ym) { - glColor4f2(1, 1, 1, 1); - bool hovered = active && (minecraft->useTouchscreen()? (xm >= x && ym >= y && xm < x + width && ym < y + height) : false); - renderBg(minecraft, xm, ym); - TextureId texId = (_imageDef.name.length() > 0)? minecraft->textures->loadAndBindTexture(_imageDef.name) : Textures::InvalidId; - if ( Textures::isTextureIdValid(texId) ) { - const ImageDef& d = _imageDef; - Tesselator& t = Tesselator::instance; - - t.begin(); - if (!active) t.color(0xff808080); - else if (hovered||selected) t.color(0xffcccccc); - //else t.color(0xffe0e0e0); - else t.color(0xffffffff); - - float hx = ((float) d.width) * 0.5f; - float hy = ((float) d.height) * 0.5f; - const float cx = ((float)x+d.x) + hx; - const float cy = ((float)y+d.y) + hy; - if (hovered) { - hx *= 0.95f; - hy *= 0.95f; - } - - const TextureData* td = minecraft->textures->getTemporaryTextureData(texId); - const IntRectangle* src = _imageDef.getSrc(); - if (td != NULL && src != NULL) { - float u0 = (src->x) / (float)td->w; - float u1 = (src->x+src->w) / (float)td->w; - float v0 = (src->y) / (float)td->h; - float v1 = (src->y+src->h) / (float)td->h; - t.vertexUV(cx-hx, cy-hy, blitOffset, u0, v0); - t.vertexUV(cx-hx, cy+hy, blitOffset, u0, v1); - t.vertexUV(cx+hx, cy+hy, blitOffset, u1, v1); - t.vertexUV(cx+hx, cy-hy, blitOffset, u1, v0); - } - t.draw(); - } -} - namespace Touch { // @@ -88,9 +32,7 @@ namespace Touch { StartMenuScreen::StartMenuScreen() : bHost( 2, "Start Game"), bJoin( 3, "Join Game"), - bOptions( 4, "Options"), - bBuy( 5), - bTest( 9, "Create") + bOptions( 4, "Options") { ImageDef def; bJoin.width = 75; @@ -125,11 +67,6 @@ void StartMenuScreen::init() tabButtons.push_back(&bJoin); tabButtons.push_back(&bOptions); - #ifdef DEMO_MODE - buttons.push_back(&bBuy); - tabButtons.push_back(&bBuy); - #endif - copyright = "\xffMojang AB";//. Do not distribute!"; #ifdef PRE_ANDROID23 @@ -173,20 +110,14 @@ void StartMenuScreen::setupPositions() { bJoin.x = 0*buttonWidth + (int)(1*spacing); bHost.x = 1*buttonWidth + (int)(2*spacing); bOptions.x = 2*buttonWidth + (int)(3*spacing); - //bBuy.y = bOptions.y - bBuy.h - 6; - //bBuy.x = bOptions.x + bOptions.w - bBuy.w; - - bBuy.y = height - bBuy.height - 3; - bBuy.x = (width - bBuy.width) / 2; - - bTest.x = 4; - bTest.y = height - bTest.height - 4; copyrightPosX = width - minecraft->font->width(copyright) - 1; versionPosX = (width - minecraft->font->width(version)) / 2;// - minecraft->font->width(version) - 2; } void StartMenuScreen::tick() { + Screen::tick(); + _updateLicense(); } @@ -218,11 +149,6 @@ void StartMenuScreen::buttonClicked(::Button* button) { { minecraft->setScreen(new OptionsScreen()); } - if (button->id == bBuy.id) - { - minecraft->platform()->buyGame(); - //minecraft->setScreen(new BuyGameScreen()); - } } bool StartMenuScreen::isInGameScreen() { return false; } diff --git a/src/client/gui/screens/touch/TouchStartMenuScreen.h b/src/client/gui/screens/touch/TouchStartMenuScreen.h index 7f45545..8ac2c70 100755 --- a/src/client/gui/screens/touch/TouchStartMenuScreen.h +++ b/src/client/gui/screens/touch/TouchStartMenuScreen.h @@ -3,15 +3,7 @@ #include "../../Screen.h" #include "../../components/LargeImageButton.h" - - -class BuyButton: public ImageButton { - typedef ImageButton super; -public: - BuyButton(int id); - void render(Minecraft* minecraft, int xm, int ym); -}; - +#include "../../components/TextBox.h" namespace Touch { @@ -36,8 +28,6 @@ private: LargeImageButton bHost; LargeImageButton bJoin; LargeImageButton bOptions; - TButton bTest; - BuyButton bBuy; std::string copyright; int copyrightPosX;