AppPlatform Fixes

This commit is contained in:
TheBrokenRail
2023-11-03 17:10:05 -04:00
committed by iProgramInCpp
parent 2356757730
commit 06c69d3cc8
18 changed files with 202 additions and 135 deletions

View File

@@ -283,7 +283,17 @@ void AppPlatform_android::setShiftPressed(bool b)
m_bShiftPressed = b;
}
void AppPlatform_android::showKeyboard(bool bShown)
void AppPlatform_android::showKeyboard(int x, int y, int w, int h)
{
changeKeyboardVisibility(true);
}
void AppPlatform_android::hideKeyboard()
{
changeKeyboardVisibility(false);
}
void AppPlatform_android::changeKeyboardVisibility(bool bShown)
{
JavaVM* pVM = m_app->activity->vm;
JNIEnv* pEnv = m_app->activity->env;
@@ -340,11 +350,6 @@ void AppPlatform_android::showKeyboard(bool bShown)
pVM->DetachCurrentThread();
}
void AppPlatform_android::onHideKeyboard()
{
m_bIsKeyboardShown = false;
}
int AppPlatform_android::getKeyboardUpOffset()
{
// @TODO

View File

@@ -46,8 +46,8 @@ public:
// Also add these to allow proper text input within the game.
bool shiftPressed() override;
void setShiftPressed(bool b);
void showKeyboard(bool bShown) override;
void onHideKeyboard() override; // This is an event
void showKeyboard(int x, int y, int w, int h) override;
void hideKeyboard() override;
int getKeyboardUpOffset() override;
// Also add these to allow saving options.
@@ -63,6 +63,8 @@ public:
void setExternalStoragePath(const std::string& path);
private:
void changeKeyboardVisibility(bool bShown);
int m_ScreenWidth;
int m_ScreenHeight;

View File

@@ -1,59 +1,35 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.16.0)
project(reminecraftpe-android)
# Declares and names the project.
# Project Root
set(MC_ROOT ../../../../../../..)
project("reminecraftpe")
# Build
add_compile_definitions(USE_NATIVE_ANDROID)
add_library(reminecraftpe SHARED
"${MC_ROOT}/platforms/android/android_native_app_glue.c"
"${MC_ROOT}/platforms/android/AppPlatform_android.cpp"
"${MC_ROOT}/platforms/android/main.cpp"
"${MC_ROOT}/thirdparty/stb_image_impl.c"
)
# The root of the project
SET(MC_ROOT ../../../../../../..)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
reminecraftpe
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${MC_ROOT}/platforms/android/android_native_app_glue.c
${MC_ROOT}/platforms/android/AppPlatform_android.cpp
${MC_ROOT}/platforms/android/main.cpp
${MC_ROOT}/thirdparty/stb_image_impl.c)
# Check for the presence of some optional asset based features.
if(NOT EXISTS "${MC_ROOT}/game/assets/gui/background/panorama_0.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_MENU_BACKGROUND)
endif()
if(NOT EXISTS "${MC_ROOT}/game/assets/environment/clouds.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_CLOUDS)
endif()
if(NOT EXISTS "${MC_ROOT}/game/assets/misc/grasscolor.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_GRASS_COLOR)
endif()
if(NOT EXISTS "${MC_ROOT}/game/assets/misc/foliagecolor.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_FOLIAGE_COLOR)
endif()
# Add the core as part of the library.
add_subdirectory(${MC_ROOT}/source source)
# Core
add_subdirectory("${MC_ROOT}/source" source)
target_link_libraries(reminecraftpe reminecraftpe-core)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
# Extra Dependencies
target_link_libraries(reminecraftpe android)
target_link_libraries( # Specifies the target library.
reminecraftpe android )
# Check for the presence of some optional asset based features.
if(EXISTS "${MC_ROOT}/game/assets/gui/background/panorama_0.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_MENU_BACKGROUND)
endif()
if(EXISTS "${MC_ROOT}/game/assets/environment/clouds.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_CLOUDS)
endif()
if(EXISTS "${MC_ROOT}/game/assets/misc/grasscolor.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_GRASS_COLOR)
endif()
if(EXISTS "${MC_ROOT}/game/assets/misc/foliagecolor.png")
target_compile_definitions(reminecraftpe PUBLIC FEATURE_FOLIAGE_COLOR)
endif()

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="R5CRB1GE0RY" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-11-03T20:57:27.283440358Z" />
</component>
</project>

View File

@@ -2,11 +2,14 @@ package io.github.reminecraftpe;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import org.libsdl.app.SDLActivity;
import org.libsdl.app.LimitedSDLActivity;
public class MainActivity extends SDLActivity {
public class MainActivity extends LimitedSDLActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -16,4 +19,32 @@ public class MainActivity extends SDLActivity {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
}
@Override
@SuppressWarnings("deprecation")
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Fullscreen
if (hasFocus) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().setDecorFitsSystemWindows(false);
WindowInsetsController controller = getWindow().getInsetsController();
if (controller != null) {
controller.hide(WindowInsets.Type.systemBars());
controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
} else {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
View.INVISIBLE
);
}
}
}
}

View File

@@ -0,0 +1,17 @@
package org.libsdl.app;
// Prevent SDL From Controlling Immersive Mode
public class LimitedSDLActivity extends SDLActivity {
@Override
boolean sendCommand(int command, Object data) {
if (command == COMMAND_CHANGE_WINDOW_STYLE) {
return true;
} else {
return super.sendCommand(command, data);
}
}
@Override
public void onSystemUiVisibilityChange(int visibility) {
}
}

View File

@@ -71,6 +71,24 @@ void AppPlatform_sdl_base::initSoundSystem()
}
}
std::string AppPlatform_sdl_base::getDateString(int time)
{
time_t tt = time;
struct tm t;
#ifdef _WIN32
gmtime_s(&t, &tt);
#else
gmtime_r(&tt, &t);
#endif
// Format String
char buf[2048];
strftime(buf, sizeof buf, "%b %d %Y %H:%M:%S", &t);
// Return
return std::string(buf);
}
void AppPlatform_sdl_base::setIcon(const Texture& icon)
{
if (!icon.m_pixels)
@@ -185,14 +203,14 @@ MouseButtonType AppPlatform_sdl_base::GetMouseButtonType(SDL_Event event)
{
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
return BUTTON_LEFT;
case SDL_BUTTON_RIGHT:
return BUTTON_RIGHT;
case SDL_BUTTON_MIDDLE:
return BUTTON_MIDDLE;
default:
return BUTTON_NONE;
case SDL_BUTTON_LEFT:
return BUTTON_LEFT;
case SDL_BUTTON_RIGHT:
return BUTTON_RIGHT;
case SDL_BUTTON_MIDDLE:
return BUTTON_MIDDLE;
default:
return BUTTON_NONE;
}
}
@@ -202,30 +220,30 @@ bool AppPlatform_sdl_base::GetMouseButtonState(SDL_Event event)
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
result = true;
break;
case SDL_MOUSEBUTTONUP:
result = false;
break;
case SDL_MOUSEWHEEL:
{
short wheelDelta = event.wheel.y;
if (wheelDelta > 0)
{
// "A positive value indicates that the wheel was rotated forward, away from the user."
result = false;
}
else
{
// "A negative value indicates that the wheel was rotated backward, toward the user."
case SDL_MOUSEBUTTONDOWN:
result = true;
break;
case SDL_MOUSEBUTTONUP:
result = false;
break;
case SDL_MOUSEWHEEL:
{
short wheelDelta = event.wheel.y;
if (wheelDelta > 0)
{
// "A positive value indicates that the wheel was rotated forward, away from the user."
result = false;
}
else
{
// "A negative value indicates that the wheel was rotated backward, toward the user."
result = true;
}
break;
}
break;
}
default:
result = false;
break;
default:
result = false;
break;
}
return result;
@@ -235,11 +253,11 @@ Keyboard::KeyState AppPlatform_sdl_base::GetKeyState(SDL_Event event)
{
switch (event.key.state)
{
case SDL_RELEASED:
return Keyboard::UP;
case SDL_PRESSED:
default:
return Keyboard::DOWN;
case SDL_RELEASED:
return Keyboard::UP;
case SDL_PRESSED:
default:
return Keyboard::DOWN;
}
}
@@ -247,7 +265,7 @@ void AppPlatform_sdl_base::showKeyboard(int x, int y, int w, int h)
{
if (SDL_IsTextInputActive())
{
SDL_StopTextInput();
hideKeyboard();
}
SDL_Rect rect;
rect.x = x;

View File

@@ -28,6 +28,7 @@ public:
Texture loadTexture(const std::string& path, bool b = false) override = 0;
int getUserInputStatus() override;
SoundSystem* const getSoundSystem() const override { return m_pSoundSystem; }
std::string getDateString(int time) override;
// Also add these to allow proper turning within the game.
void setMouseGrabbed(bool b) override;

View File

@@ -342,10 +342,6 @@ int main(int argc, char *argv[])
// Create Window
int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
#ifdef ANDROID
// Android Immersive Mode
flags |= SDL_WINDOW_FULLSCREEN;
#endif
window = SDL_CreateWindow("ReMinecraftPE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Minecraft::width, Minecraft::height, flags);
if (!window)
{

View File

@@ -140,18 +140,12 @@ bool AppPlatform::shiftPressed()
return false;
}
void AppPlatform::showKeyboard(bool bShown)
{
}
void AppPlatform::onHideKeyboard()
{
}
#ifdef USE_NATIVE_ANDROID
int AppPlatform::getKeyboardUpOffset()
{
return 0;
}
#endif
bool AppPlatform::hasFileSystemAccess()
{

View File

@@ -59,9 +59,6 @@ public:
virtual void updateFocused(bool focused);
// Also add this to allow proper text input within the game.
virtual bool shiftPressed();
virtual void showKeyboard(bool bShown);
virtual void onHideKeyboard(); // called by the runner, not the game
virtual int getKeyboardUpOffset();
virtual bool hasFileSystemAccess();
// Also add this to allow dynamic patching.
@@ -71,6 +68,9 @@ public:
// On-screen keyboard
virtual void showKeyboard(int x, int y, int w, int h);
virtual void hideKeyboard();
#ifdef USE_NATIVE_ANDROID
virtual int getKeyboardUpOffset();
#endif
#endif
public:

View File

@@ -263,11 +263,12 @@ void Screen::mouseClicked(int xPos, int yPos, int d) // d = clicked?
TextInputBox* textInput = m_textInputs[i];
if (textInput->m_bFocused == handleFocused)
{
textInput->onClick(m_pMinecraft, xPos, yPos);
textInput->onClick(xPos, yPos);
}
}
}
#ifdef USE_NATIVE_ANDROID
// if the keyboard is shown:
if (m_pMinecraft->platform()->getKeyboardUpOffset())
{
@@ -284,9 +285,10 @@ void Screen::mouseClicked(int xPos, int yPos, int d) // d = clicked?
}
if (!areAnyFocused)
m_pMinecraft->platform()->showKeyboard(false);
m_pMinecraft->platform()->hideKeyboard();
}
#endif
#endif
}
void Screen::mouseReleased(int xPos, int yPos, int d)
@@ -361,6 +363,7 @@ void Screen::onRender(int mouseX, int mouseY, float f)
int Screen::getYOffset()
{
#ifdef USE_NATIVE_ANDROID
int keybOffset = m_pMinecraft->platform()->getKeyboardUpOffset();
if (!keybOffset)
return 0;
@@ -395,6 +398,9 @@ int Screen::getYOffset()
}
return offset;
#else
return 0;
#endif
}
void Screen::updateEvents()

View File

@@ -10,7 +10,7 @@
#include "client/app/Minecraft.hpp"
#ifndef ORIGINAL_CODE
#if defined(ANDROID) && !defined(USE_SDL)
#ifdef USE_NATIVE_ANDROID
#define HANDLE_CHARS_SEPARATELY // faked though, see platforms/android/minecraftcpp/minecraftcpp.NativeActivity/main.cpp
#endif
@@ -32,6 +32,11 @@ TextInputBox::TextInputBox(Screen* parent, int id, int x, int y, int width, int
m_pParent = parent;
}
TextInputBox::~TextInputBox()
{
m_pParent->m_pMinecraft->platform()->hideKeyboard();
}
void TextInputBox::init(Font* pFont)
{
m_pFont = pFont;
@@ -179,22 +184,22 @@ void TextInputBox::tick()
}
}
void TextInputBox::setFocused(Minecraft* minecraft, bool b)
void TextInputBox::setFocused(bool b)
{
if (m_bFocused == b)
return;
if (b)
{
int x = m_xPos / Gui::InvGuiScale;
int y = m_yPos / Gui::InvGuiScale;
int w = m_width / Gui::InvGuiScale;
int h = m_height / Gui::InvGuiScale;
minecraft->platform()->showKeyboard(x, y, w, h);
int x = (int) (((float) m_xPos) / Gui::InvGuiScale);
int y = (int) (((float) m_yPos) / Gui::InvGuiScale);
int w = (int) (((float) m_width) / Gui::InvGuiScale);
int h = (int) (((float) m_height) / Gui::InvGuiScale);
m_pParent->m_pMinecraft->platform()->showKeyboard(x, y, w, h);
}
else
{
minecraft->platform()->hideKeyboard();
m_pParent->m_pMinecraft->platform()->hideKeyboard();
}
m_bFocused = b;
@@ -203,17 +208,15 @@ void TextInputBox::setFocused(Minecraft* minecraft, bool b)
m_lastFlashed = getTimeMs();
m_bCursorOn = true;
m_insertHead = int(m_text.size());
m_pParent->m_pMinecraft->platform()->showKeyboard(true);
}
// don't actually hide the keyboard when unfocusing
// - we may be undoing the work of another text box
}
void TextInputBox::onClick(Minecraft* minecraft, int x, int y)
void TextInputBox::onClick(int x, int y)
{
setFocused(minecraft, clicked(x, y));
setFocused(clicked(x, y));
}
void TextInputBox::charPressed(int k)

View File

@@ -23,6 +23,7 @@ class TextInputBox : public GuiComponent
{
public:
TextInputBox(Screen*, int id, int x, int y, int width = 200, int height = 12, const std::string& placeholder = "", const std::string& text = "");
~TextInputBox();
void init(Font* pFont);
void setEnabled(bool bEnabled);
@@ -30,8 +31,8 @@ public:
void charPressed(int chr);
void render();
void tick();
void setFocused(Minecraft*, bool b);
void onClick(Minecraft*, int x, int y);
void setFocused(bool b);
void onClick(int x, int y);
bool clicked(int x, int y);
public:

View File

@@ -35,7 +35,7 @@ void ChatScreen::init()
// set focus directly on the chat text box
m_textChat.init(m_pFont);
m_textChat.setFocused(m_pMinecraft, true);
m_textChat.setFocused(true);
m_buttons.push_back(&m_btnSend);
m_textInputs.push_back(&m_textChat);

View File

@@ -149,7 +149,7 @@ void Options::_initDefaultValues()
KM(KM_FLY_DOWN, SDLVK_x);
KM(KM_CHAT_CMD, SDLVK_SLASH);
#endif
#if defined(ANDROID) && !defined(USE_SDL)
#ifdef USE_NATIVE_ANDROID
// -- Original xperia play controls
//KM(KM_FORWARD, AKEYCODE_DPAD_UP);
//KM(KM_LEFT, AKEYCODE_DPAD_LEFT);

View File

@@ -13,7 +13,7 @@ bool Controller::isTouchedValues[2];
float Controller::stickValuesX[2];
float Controller::stickValuesY[2];
#if !defined(ANDROID) || defined(USE_SDL)
#ifndef USE_NATIVE_ANDROID
const float Controller_unk_1[3] = { 0.0f, 0.64f, -0.64f };
#endif
@@ -52,7 +52,7 @@ void Controller::feed(int stickNo, int touched, float x, float y)
if (!isValidStick(stickNo))
return;
#if defined(ANDROID) && !defined(USE_SDL)
#ifdef USE_NATIVE_ANDROID
int index = stickNo - 1;
#else
int index = (x >= 0.0f) ? 1 : 0;
@@ -61,7 +61,7 @@ void Controller::feed(int stickNo, int touched, float x, float y)
// maybe the 2 'touch sticks' are actually internally 1 single surface??
isTouchedValues[index] = touched != 0;
#if defined(ANDROID) && !defined(USE_SDL)
#ifdef USE_NATIVE_ANDROID
stickValuesX[index] = x;
#else
stickValuesX[index] = linearTransform(x + Controller_unk_1[index + 1], 0.0f, 2.78f, true);

View File

@@ -22,7 +22,7 @@ ControllerTurnInput::ControllerTurnInput()
TurnDelta ControllerTurnInput::getTurnDelta()
{
#if defined(ANDROID) && !defined(USE_SDL)
#ifdef USE_NATIVE_ANDROID
return TurnDelta(Controller::getX(m_stickNo) * 50.f, Controller::getY(m_stickNo) * 60.f);
#endif