2014-01-07 11:25:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-15 23:03:43 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2014-01-07 11:25:07 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCDevice.h"
|
2014-02-13 21:08:43 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <jni.h>
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccTypes.h"
|
2015-11-27 17:00:33 +08:00
|
|
|
#include "platform/android/jni/JniHelper.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2021-07-15 23:03:43 +08:00
|
|
|
#include "yasio/cxx17/string_view.hpp"
|
2013-02-16 19:36:21 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
static const char* helperClassName = "org.axys1.lib.AxysHelper";
|
2015-11-27 17:00:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-02-16 19:36:21 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Device::getDPI()
|
2013-02-16 19:36:21 +08:00
|
|
|
{
|
2013-02-17 17:13:06 +08:00
|
|
|
static int dpi = -1;
|
|
|
|
if (dpi == -1)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
dpi = JniHelper::callStaticIntMethod(helperClassName, "getDPI");
|
2013-02-17 17:13:06 +08:00
|
|
|
}
|
|
|
|
return dpi;
|
2013-02-16 19:36:21 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 17:31:37 +08:00
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
if (isEnabled)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(helperClassName, "enableAccelerometer");
|
2013-09-12 17:31:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(helperClassName, "disableAccelerometer");
|
2013-09-12 17:31:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::setAccelerometerInterval(float interval)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(helperClassName, "setAccelerometerInterval", interval);
|
2013-09-12 17:31:37 +08:00
|
|
|
}
|
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
BitmapDC() : _width(0), _height(0), _data(nullptr) {}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
~BitmapDC() {}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool getBitmapFromJavaShadowStroke(const char* text,
|
|
|
|
int nWidth,
|
|
|
|
int nHeight,
|
|
|
|
Device::TextAlign eAlignMask,
|
|
|
|
const FontDefinition& textDefinition)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
JniMethodInfo methodInfo;
|
2022-08-08 18:02:17 +08:00
|
|
|
if (!JniHelper::getStaticMethodInfo(methodInfo, "org.axys1.lib.AxysBitmap",
|
2021-12-25 10:04:45 +08:00
|
|
|
"createTextBitmapShadowStroke",
|
|
|
|
"([BLjava/lang/String;IIIIIIIIZFFFFZIIIIFZI)Z"))
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
|
2021-12-25 10:04:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font
|
|
|
|
// file asset, or the path has been mapped to a different location in the app package:
|
|
|
|
std::string fullPathOrFontName = textDefinition._fontName;
|
|
|
|
if (FileUtils::getInstance()->isFileExist(fullPathOrFontName))
|
|
|
|
{
|
|
|
|
fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName);
|
|
|
|
// If the path name returned includes the 'assets' dir then that needs to be removed, because the
|
|
|
|
// android.content.Context requires this portion of the path to be omitted for assets inside the app
|
|
|
|
// package.
|
|
|
|
using namespace cxx17;
|
|
|
|
if (cxx20::starts_with(cxx17::string_view{fullPathOrFontName}, "assets/"_sv))
|
|
|
|
{
|
|
|
|
fullPathOrFontName =
|
|
|
|
fullPathOrFontName.substr(sizeof("assets/") - 1); // Chop out the 'assets/' portion of the path.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**create bitmap
|
|
|
|
* this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code
|
2022-08-08 18:02:17 +08:00
|
|
|
* will call Java_org_axys1_lib_AxysBitmap_nativeInitBitmapDC() to init the width, height
|
2021-12-25 10:04:45 +08:00
|
|
|
* and data.
|
|
|
|
* use this approach to decrease the jni call number
|
|
|
|
*/
|
|
|
|
int count = strlen(text);
|
|
|
|
jbyteArray strArray = methodInfo.env->NewByteArray(count);
|
|
|
|
methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast<const jbyte*>(text));
|
|
|
|
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
|
|
|
|
|
|
|
|
if (!methodInfo.env->CallStaticBooleanMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, strArray, jstrFont, textDefinition._fontSize,
|
|
|
|
textDefinition._fontFillColor.r, textDefinition._fontFillColor.g, textDefinition._fontFillColor.b,
|
|
|
|
textDefinition._fontAlpha, eAlignMask, nWidth, nHeight, textDefinition._shadow._shadowEnabled,
|
|
|
|
textDefinition._shadow._shadowOffset.width, -textDefinition._shadow._shadowOffset.height,
|
|
|
|
textDefinition._shadow._shadowBlur, textDefinition._shadow._shadowOpacity,
|
|
|
|
textDefinition._stroke._strokeEnabled, textDefinition._stroke._strokeColor.r,
|
|
|
|
textDefinition._stroke._strokeColor.g, textDefinition._stroke._strokeColor.b,
|
|
|
|
textDefinition._stroke._strokeAlpha, textDefinition._stroke._strokeSize, textDefinition._enableWrap,
|
|
|
|
textDefinition._overflow))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
methodInfo.env->DeleteLocalRef(strArray);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrFont);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
|
|
|
|
return true;
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
int _width;
|
|
|
|
int _height;
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned char* _data;
|
2014-02-13 21:08:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Data Device::getTextureDataForText(const char* text,
|
|
|
|
const FontDefinition& textDefinition,
|
|
|
|
TextAlign align,
|
|
|
|
int& width,
|
|
|
|
int& height,
|
|
|
|
bool& hasPremultipliedAlpha)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
Data ret;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
BitmapDC& dc = sharedBitmapDC();
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!dc.getBitmapFromJavaShadowStroke(text, (int)textDefinition._dimensions.width,
|
|
|
|
(int)textDefinition._dimensions.height, align, textDefinition))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
};
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
width = dc._width;
|
2014-02-13 21:08:43 +08:00
|
|
|
height = dc._height;
|
2021-12-25 10:04:45 +08:00
|
|
|
ret.fastSet(dc._data, width * height * 4);
|
2014-05-06 10:36:55 +08:00
|
|
|
hasPremultipliedAlpha = true;
|
2014-02-13 21:08:43 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-18 20:41:12 +08:00
|
|
|
void Device::setKeepScreenOn(bool value)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(helperClassName, "setKeepScreenOn", value);
|
2014-08-18 20:41:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 17:36:57 +08:00
|
|
|
void Device::vibrate(float duration)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(helperClassName, "vibrate", duration);
|
2015-06-25 17:36:57 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2014-01-31 08:51:43 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
// this method is called by Cocos2dxBitmap
|
2021-12-25 10:04:45 +08:00
|
|
|
extern "C" {
|
|
|
|
/**
|
|
|
|
* this method is called by java code to init width, height and pixels data
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2022-08-08 18:02:17 +08:00
|
|
|
Java_org_axys1_lib_AxysBitmap_nativeInitBitmapDC(JNIEnv* env, jclass, jint width, jint height, jbyteArray pixels)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int size = width * height * 4;
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::BitmapDC& bitmapDC = ax::sharedBitmapDC();
|
2021-12-25 10:04:45 +08:00
|
|
|
bitmapDC._width = width;
|
|
|
|
bitmapDC._height = height;
|
|
|
|
bitmapDC._data = (unsigned char*)malloc(sizeof(unsigned char) * size);
|
|
|
|
env->GetByteArrayRegion(pixels, 0, size, (jbyte*)bitmapDC._data);
|
|
|
|
}
|
2014-02-13 21:08:43 +08:00
|
|
|
};
|