GameTextInput جزء من حزمة تطوير البرامج (SDK) لألعاب Android
يُعد استخدام مكتبة GameTextInput بديلاً أبسط لكتابة تطبيق Android بملء الشاشة يستخدم لوحة المفاتيح الافتراضية لإدخال النص.
توفّر GameTextInput واجهة برمجة تطبيقات مباشرة لعرض لوحة المفاتيح الافتراضية أو إخفائها، وتحديد النص الذي يتم تعديله حاليًا أو الحصول عليه، وتلقّي إشعارات عند تغيير النص. ولا يُقصد بذلك تطبيقات محرّر النصوص الكاملة، ولكنّه يوفّر مع ذلك إمكانية اختيار منطقة الكتابة وتكوينها لحالات الاستخدام النموذجية في الألعاب. تتيح هذه المكتبة أيضًا ميزات متقدّمة لمحرّر أسلوب الإدخال
(IME)، مثل التدقيق الإملائي والاقتراحات والأحرف المتعدّدة المفاتيح.
داخليًا، تجمع GameTextInput النص المُدخَل (مع الحالات ذات الصلة) في المخزن المؤقت الداخلي GameTextInput::currentState_ وتُعلم التطبيق بأي تغييرات تطرأ عليه. بعد ذلك، يعالج التطبيق النص في دالة رد الاتصال المسجّلة.
مدى التوفّر
يمكن استخدام GameTextInput بالطرق التالية:
بالإضافة إلى GameActivity: تدمج GameActivity واجهة برمجة التطبيقات GameTextInput. لا يمكن للتطبيقات التي تستخدم GameActivity استخدام GameTextInput المدمَج إلا. يتم توثيق تعليمات الاستخدام بالكامل على صفحة GameActivity . للاطّلاع على مثال على دمج GameActivity وGameTextInput، راجِع مستودع games-samples. لا يندرج نموذج الاستخدام هذا ضمن نطاق هذا الدليل.
كمكتبة مستقلة: يوضّح بقية الدليل خطوات الاستخدام.
يُرجى العِلم أنّ الطريقتَين أعلاه لا يمكن استخدامهما معًا.
تتوفّر إصدارات GameTextInput الرسمية في إصدار مكتبة ألعاب Jetpack ضمن Google Maven.
إعداد الإصدار
يتم توزيع GameTextInput على شكل أرشيف Android (AAR). يحتوي ملف AAR هذا على فئات Java ورمز المصدر C الذي ينفّذ الميزات الأصلية في GameTextInput. عليك تضمين ملفات المصدر هذه كجزء من عملية الإنشاء من خلال
Prefab،
الذي يعرض المكتبات الأصلية ورمز المصدر إلى مشروع CMake أو إنشاء NDK.
اتّبِع التعليمات الواردة في صفحة ألعاب Android في Jetpack لإضافة تبعية مكتبة
GameTextInputإلى ملفbuild.gradleالخاص بلعبتك. يُرجى العِلم أنّه إذا كانت تطبيقاتك تستخدم GameActivity، لا يمكنها استخدام مكتبةGameTextInputالمستقلة.تأكَّد من أنّ الملف
gradle.propertiesيتضمّن الأسطر التالية:# Tell Android Studio we are using AndroidX. android.useAndroidX=true # Use Prefab 1.1.2 or higher, which contains a fix for "header only" libs. android.prefabVersion=1.1.2 # Required only if you're using Android Studio 4.0 (4.1 is recommended). # android.enablePrefab=trueاستورِد حزمة
game-text-inputوأضِفها إلى هدفك في ملفCMakeLists.txtالخاص بمشروعك:find_package(game-text-input REQUIRED CONFIG) ... target_link_libraries(... game-text-input::game-text-input)في أحد ملفات
.cppفي لعبتك، أضِف السطر التالي لتضمين عملية تنفيذGameTextInput:#include <game-text-input/gametextinput.cpp>في الملفات المصدر التي تستخدم واجهة برمجة التطبيقات
GameTextInputC، أدرِج ملف العنوان:#include <game-text-input/gametextinput.h>جمِّع التطبيق وشغِّله. إذا ظهرت لك أخطاء في CMake، تأكَّد من إعداد ملفات AAR و
build.gradleبشكل صحيح. إذا لم يتم العثور على الملف#include، تحقَّق من ملف الإعدادCMakeLists.txt.
دمج الإصدار
من سلسلة محادثات C المرفقة حاليًا بآلة Java الافتراضية (JVM) أو سلسلة المحادثات الرئيسية للتطبيق، استدعِ
GameTextInput_initباستخدام مؤشرJNIEnv.static GameTextInput* gameTextInput = nullptr; extern "C" JNIEXPORT void JNICALL Java_com_gametextinput_testbed_MainActivity_onCreated(JNIEnv* env, jobject this) { { if(!gameTextInput) gameTextInput = GameTextInput_init(env); ... }أنشئ
InputEnabledTextViewفئة Java يمكنها الوصول إلىInputConnection.public class InputEnabledTextView extends View implements Listener { public InputConnection mInputConnection; public InputEnabledTextView(Context context, AttributeSet attrs) { super(context, attrs); } public InputEnabledTextView(Context context) { super(context); } public void createInputConnection(int inputType) { EditorInfo editorInfo = new EditorInfo(); editorInfo.inputType = inputType; editorInfo.actionId = IME_ACTION_NONE; editorInfo.imeOptions = IME_FLAG_NO_FULLSCREEN; mInputConnection = new InputConnection(this.getContext(), this, new Settings(editorInfo, true) ).setListener(this); } @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { if (outAttrs != null) { GameTextInput.copyEditorInfo(mInputConnection.getEditorInfo(), outAttrs); } return mInputConnection; } // Called when the IME input changes. @Override public void stateChanged(State newState, boolean dismissed) { onTextInputEventNative(newState); } @Override public void onImeInsetsChanged(Insets insets) { // handle Inset changes here } private native void onTextInputEventNative(State softKeyboardEvent); }أضِف
InputEnabledTextViewالذي تم إنشاؤه إلى تنسيق واجهة المستخدم. على سبيل المثال، يمكن للرمز التالي فيactivity_main.xmlأن يضعها في أسفل الشاشة:<com.android.example.gametextinputjava.InputEnabledTextView android:id="@+id/input_enabled_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />استرداد فئة
InputEnabledTextViewالجديدة هذه إلى نشاط Java يكون ذلك بسيطًا نسبيًا عند استخدام ربط العرض:public class MainActivity extends AppCompatActivity { ... private ActivityMainBinding binding; private InputEnabledTextView inputEnabledTextView; private native void setInputConnectionNative(InputConnection c); @Override protected void onCreate(Bundle savedInstanceState) { ... binding = ActivityMainBinding.inflate(getLayoutInflater()); inputEnabledTextView = binding.inputEnabledTextView; inputEnabledTextView.createInputConnection(InputType.TYPE_CLASS_TEXT); setInputConnectionNative(inputEnabledTextView.mInputConnection); }في مكتبة C، مرِّر
inputConnectionإلىGameTextInput_setInputConnection. مرِّر دالة رد الاتصال فيGameTextInput_setEventCallbackليتم إعلامك بالأحداث كبنية حالة CGameTextInputState.extern "C"JNIEXPORT void JNICALL Java_com_gametextinput_testbed_MainActivity_setInputConnectionNative( JNIEnv *env, jobject this, jobject inputConnection) { GameTextInput_setInputConnection(gameTextInput, inputConnection); GameTextInput_setEventCallback(gameTextInput,[](void *ctx, const GameTexgtInputState *state) { if (!env || !state) return; // process the newly arrived text input from user. __android_log_print(ANDROID_LOG_INFO, "TheGreateGameTextInput", state->text_UTF8); }, env); }في مكتبة C، استدعِ الدالة
GameTextInput_processEventالتي تستدعي داخليًا دالة رد الاتصال المسجَّلة في الخطوة السابقة، وذلك لكي يتمكّن تطبيقك من معالجة الأحداث عند تغيُّر الحالة.extern "C" JNIEXPORT void JNICALL Java_com_gametextinput_testbed_InputEnabledTextView_onTextInputEventNative( JNIEnv* env, jobject this, jobject soft_keyboard_event) { GameTextInput_processEvent(gameTextInput, soft_keyboard_event); }
الدوال المساعدة
تتضمّن مكتبة GameTextInput دوال مساعدة تتيح لك التحويل بين عناصر حالة Java وبُنى حالة C. الوصول إلى وظيفة عرض وإخفاء محرر أسلوب الإدخال (IME) من خلال الدالتَين GameTextInput_showIme وGameTextInput_hideIme
المراجع
قد يجد المطوّرون ما يلي مفيدًا عند إنشاء تطبيقات باستخدام GameTextInput:
- تطبيق اختبار GameTextInput
- استخدام GameTextInput مع GameActivity
- مستند مرجع GameTextInput
- رمز مصدر GameTextInput
الملاحظات
إذا واجهت أي مشاكل أو كانت لديك أي أسئلة حول GameTextInput، يمكنك إنشاء
خطأ على Google IssueTracker.