GameTextInput   เป็นส่วนหนึ่งของ Android Game Development Kit

การใช้ไลบรารี GameTextInput เป็นทางเลือกที่ง่ายกว่าการเขียนแอป Android แบบเต็มหน้าจอที่ใช้แป้นพิมพ์บนหน้าจอสำหรับการป้อนข้อความ

GameTextInput มี API ที่ใช้งานง่ายเพื่อแสดงหรือซ่อนแป้นพิมพ์บนหน้าจอ ตั้งค่าหรือรับข้อความที่แก้ไขอยู่ในปัจจุบัน และรับการแจ้งเตือนเมื่อมีการเปลี่ยนแปลงข้อความ ฟีเจอร์นี้ไม่ได้มีไว้สำหรับแอปแก้ไขข้อความที่สมบูรณ์ แต่ยังคงรองรับการเลือกและการจัดเรียงภูมิภาคสำหรับกรณีการใช้งานทั่วไปในเกม นอกจากนี้ ไลบรารีนี้ยังรองรับฟีเจอร์ขั้นสูงของตัวแก้ไขวิธีการป้อนข้อมูล (IME) เช่น การตรวจตัวสะกด การเติมข้อความให้สมบูรณ์ และอักขระหลายแป้น

ภายใน GameTextInput จะรวบรวมข้อความอินพุต (พร้อมกับสถานะที่เกี่ยวข้อง) ไปยังบัฟเฟอร์ภายใน GameTextInput::currentState_ และแจ้งแอปเกี่ยวกับการเปลี่ยนแปลงในข้อความ จากนั้นแอปจะประมวลผลข้อความในฟังก์ชัน Callback ที่ลงทะเบียนไว้

ความพร้อมใช้งาน

GameTextInput สามารถใช้ได้ดังนี้

  • ใช้ร่วมกับ GameActivity: GameActivity ผสานรวม GameTextInput แอปพลิเคชันที่ใช้งาน GameActivity จะใช้ได้เฉพาะ GameTextInput ที่ผสานรวมเท่านั้น วิธีการการใช้งานมีอยู่ในเอกสารประกอบอย่างครบถ้วนในหน้า GameActivity ดูตัวอย่างการผสานรวม GameActivity และ GameTextInput ได้ที่ที่เก็บตัวอย่างเกม รูปแบบการใช้งานนี้ไม่ได้อยู่ภายในขอบเขตของคู่มือนี้

  • ใช้เป็นคลังแบบสแตนด์อโลน: คำแนะนำที่เหลือจะอธิบายขั้นตอนการนำไปใช้

โปรดทราบว่า 2 วิธีข้างต้นใช้ร่วมกันไม่ได้

GameTextInput รุ่นอย่างเป็นทางการมีอยู่ในไลบรารีเกม Jetpack ที่เผยแพร่ใน [Google Maven][google-maven]{:.external}

ตั้งค่าบิลด์

GameTextInput เผยแพร่เป็น Android Archive (AAR) AAR นี้มีคลาส Java และซอร์สโค้ด C ซึ่งใช้ฟีเจอร์เนทีฟของ GameTextInput คุณจำเป็นต้องรวมไฟล์ต้นฉบับเหล่านี้ไว้ในกระบวนการบิลด์ผ่าน Prefab ซึ่งจะแสดงไลบรารีและซอร์สโค้ดแบบเนทีฟต่อโปรเจ็กต์ CMake หรือบิลด์ NDK

  1. ทําตามวิธีการในหน้า Jetpack Android Games เพื่อเพิ่มการพึ่งพาไลบรารี GameTextInput ลงในไฟล์ build.gradle ของเกม โปรดทราบว่าหากแอปพลิเคชันใช้ GameActivity ก็จะใช้ไลบรารี GameTextInput แบบสแตนด์อโลนไม่ได้

  2. ตรวจสอบว่า 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
    
  3. นําเข้าแพ็กเกจ game-text-input แล้วเพิ่มลงในเป้าหมายในไฟล์ CMakeLists.txt ของโปรเจ็กต์

    find_package(game-text-input REQUIRED CONFIG)
    ...
    target_link_libraries(... game-text-input::game-text-input)
    
  4. ในไฟล์ .cpp ไฟล์ใดไฟล์หนึ่งในเกม ให้เพิ่มบรรทัดต่อไปนี้เพื่อรวมการติดตั้งใช้งาน GameTextInput

    #include <game-text-input/gametextinput.cpp>
    
  5. ในไฟล์ต้นทางที่ใช้ GameTextInput C API ให้ใส่ไฟล์ส่วนหัวต่อไปนี้

    #include <game-text-input/gametextinput.h>
    
  6. คอมไพล์และเรียกใช้แอป หากพบข้อผิดพลาด CMake ให้ตรวจสอบว่าไฟล์ AAR และ build.gradle ได้รับการตั้งค่าอย่างถูกต้อง หากไม่พบไฟล์ #include ให้ยืนยันไฟล์กําหนดค่า CMakeLists.txt

ผสานรวมบิลด์

  1. จากเธรด C ที่แนบอยู่กับ 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);
        ...
    }
    
  2. สร้าง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);
    }
    
  3. เพิ่ม InputEnabledTextView ที่สร้างขึ้นลงในเลย์เอาต์ UI ตัวอย่างเช่น โค้ดต่อไปนี้ใน 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" />
    
  4. เรียกคลาส 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);
      }
    
  5. ในไลบรารี C ให้ส่ง inputConnection ไปยัง GameTextInput_setInputConnection ส่งการเรียกกลับใน GameTextInput_setEventCallback เพื่อรับการแจ้งเตือนเหตุการณ์ในรูปแบบ C state struct GameTextInputState

    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);
    }
    
  6. ในไลบรารี 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 ให้สร้างข้อบกพร่องในเครื่องมือติดตามปัญหาของ Google