Skip to content

Commit

Permalink
Added ImGui support to Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
katemonster33 committed Mar 12, 2024
1 parent 253a772 commit 1a9b9c0
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 62 deletions.
3 changes: 2 additions & 1 deletion android/app/jni/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LOCAL_CPP_FEATURES := exceptions rtti
CATA_SRCS := $(sort $(wildcard $(LOCAL_PATH)/*.cpp))
LOCAL_SRC_FILES := $(sort $(CATA_SRCS:$(LOCAL_PATH)/%=%))

LOCAL_STATIC_LIBRARIES := third-party
LOCAL_STATIC_LIBRARIES := third-party imgui

LOCAL_SHARED_LIBRARIES := libhidapi SDL2 SDL2_mixer SDL2_image SDL2_ttf mpg123

Expand All @@ -28,3 +28,4 @@ endif
include $(BUILD_SHARED_LIBRARY)

include $(LOCAL_PATH)/../android/app/jni/src/third-party/Android.mk
include $(LOCAL_PATH)/../android/app/jni/src/third-party/imgui/Android.mk
8 changes: 3 additions & 5 deletions android/app/jni/src/third-party/Android.mk
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
LOCAL_PATH := $(call my-dir)/../../../../../src/third-party

include $(CLEAR_VARS)

LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)/..
LOCAL_C_INCLUDES := $(LOCAL_PATH)/third-party $(LOCAL_PATH)

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/third-party

LOCAL_MODULE := third-party

LOCAL_CPP_FEATURES := exceptions rtti

# Add your application source files here...
FLATBUFFERS_SRCS := $(sort $(wildcard $(LOCAL_PATH)/flatbuffers/*.cpp))
FLATBUFFERS_SRCS := $(sort $(wildcard $(LOCAL_PATH)/third-party/flatbuffers/*.cpp))
LOCAL_SRC_FILES := $(sort $(FLATBUFFERS_SRCS:$(LOCAL_PATH)/%=%))

LOCAL_CFLAGS += -DBACKTRACE=1 -DLOCALIZE=1 -Wextra -Wall -fsigned-char
Expand Down
24 changes: 24 additions & 0 deletions android/app/jni/src/third-party/imgui/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
include $(CLEAR_VARS)

LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)/third-party

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)

LOCAL_MODULE := imgui

LOCAL_CPP_FEATURES := exceptions rtti

LOCAL_SHARED_LIBRARIES := SDL2

# Add your application source files here...
IMGUI_SRCS := $(sort $(wildcard $(LOCAL_PATH)/third-party/imgui/*.cpp))
LOCAL_SRC_FILES := $(sort $(IMGUI_SRCS:$(LOCAL_PATH)/%=%))

LOCAL_CFLAGS += -DBACKTRACE=1 -DLOCALIZE=1 -fsigned-char

ifeq ($(OS),Windows_NT)
# needed to bypass 8191 character limit on Windows command line
LOCAL_SHORT_COMMANDS = true
endif

include $(BUILD_STATIC_LIBRARY)
39 changes: 39 additions & 0 deletions src/cata_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "output.h"
#include "ui_manager.h"

ImGuiKey cata_key_to_imgui( int cata_key );

#if !(defined(TILES) || defined(WIN32))
#include <curses.h>
#include <imtui/imtui-impl-ncurses.h>
Expand Down Expand Up @@ -206,6 +208,43 @@ void cataimgui::client::process_input( void *input )

#endif

ImGuiKey cata_key_to_imgui( int cata_key )
{
switch( cata_key ) {
case KEY_UP:
return ImGuiKey_UpArrow;
case KEY_DOWN:
return ImGuiKey_DownArrow;
case KEY_LEFT:
return ImGuiKey_LeftArrow;
case KEY_RIGHT:
return ImGuiKey_RightArrow;
case KEY_ENTER:
return ImGuiKey_Enter;
case KEY_ESCAPE:
return ImGuiKey_Escape;
default:
if( cata_key >= 'a' && cata_key <= 'z' ) {
return static_cast<ImGuiKey>( ImGuiKey_A + ( cata_key - 'a' ) );
} else if( cata_key >= 'A' && cata_key <= 'Z' ) {
return static_cast<ImGuiKey>( ImGuiKey_A + ( cata_key - 'A' ) );
} else if( cata_key >= '0' && cata_key <= 'Z' ) {
return static_cast<ImGuiKey>( ImGuiKey_A + ( cata_key - '0' ) );
}
return ImGuiKey_None;
}
}

void cataimgui::client::process_cata_input( const input_event &event )
{
if( event.type == input_event_t::keyboard_code || event.type == input_event_t::keyboard_char ) {
int code = event.get_first_input();
ImGuiIO &io = ImGui::GetIO();
io.AddKeyEvent( cata_key_to_imgui( code ), true );
io.AddKeyEvent( cata_key_to_imgui( code ), false );
}
}

void cataimgui::point_to_imvec2( point *src, ImVec2 *dest )
{
if( src != nullptr && dest != nullptr ) {
Expand Down
2 changes: 2 additions & 0 deletions src/cata_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <array>

class nc_color;
struct input_event;
struct item_info_data;

#if defined(WIN32) || defined(TILES)
Expand Down Expand Up @@ -49,6 +50,7 @@ class client
void new_frame();
void end_frame();
void process_input( void *input );
void process_cata_input( const input_event &event );
#if !(defined(TILES) || defined(WIN32))
void upload_color_pair( int p, int f, int b );
void set_alloced_pair_count( short count );
Expand Down
11 changes: 1 addition & 10 deletions src/input_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "string_input_popup.h"
#include "translations.h"
#include "ui_manager.h"
#if !defined(__ANDROID__)
#include "cata_imgui.h"
#include "imgui/imgui.h"

Expand Down Expand Up @@ -73,7 +72,6 @@ class keybindings_ui : public cataimgui::window
init();
};
};
#endif

static const std::string default_context_id( "default" );

Expand Down Expand Up @@ -607,7 +605,6 @@ static const std::map<fallback_action, int> fallback_keys = {
{ fallback_action::execute, '.' },
};

#if !defined(__ANDROID__)
keybindings_ui::keybindings_ui( bool permit_execute_action,
input_context *parent ) : cataimgui::window( "KEYBINDINGS", ImGuiWindowFlags_NoNav )
{
Expand Down Expand Up @@ -763,7 +760,7 @@ void keybindings_ui::init()
{
width = TERMX >= 100 ? 100 : 80;
}
#endif

bool input_context::resolve_conflicts( const std::vector<input_event> &events,
const std::string &ignore_action )
{
Expand Down Expand Up @@ -1287,7 +1284,6 @@ action_id input_context::display_menu_legacy( const bool permit_execute_action )
return action_to_execute;
}

#if !defined(__ANDROID__)
action_id input_context::display_menu_imgui( const bool permit_execute_action )
{

Expand Down Expand Up @@ -1468,19 +1464,14 @@ action_id input_context::display_menu_imgui( const bool permit_execute_action )

return action_to_execute;
}
#endif

action_id input_context::display_menu( bool permit_execute_action )
{
#if defined(__ANDROID__)
return display_menu_legacy( permit_execute_action );
#else
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
return display_menu_imgui( permit_execute_action );
} else {
return display_menu_legacy( permit_execute_action );
}
#endif
}

input_event input_context::get_raw_input()
Expand Down
6 changes: 0 additions & 6 deletions src/input_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
enum class kb_menu_status;

class hotkey_queue;
#if !defined(__ANDROID__)
class keybindings_ui;
#endif
namespace catacurses
{
class window;
Expand All @@ -40,9 +38,7 @@ class window;
*/
class input_context
{
#if !defined(__ANDROID__)
friend class keybindings_ui;
#endif
public:
#if defined(__ANDROID__)
// Whatever's on top is our current input context.
Expand Down Expand Up @@ -462,9 +458,7 @@ class input_context
std::string_view phrase ) const;

action_id display_menu_legacy( bool permit_execute_action );
#if !defined(__ANDROID__)
action_id display_menu_imgui( bool permit_execute_action );
#endif
};

class hotkey_queue
Expand Down
6 changes: 0 additions & 6 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
#include "wcwidth.h"
#include "worldfactory.h"

#if !defined(__ANDROID__)
#include "cata_imgui.h"
#include "imgui/imgui.h"

Expand Down Expand Up @@ -114,7 +113,6 @@ void demo_ui::run()
}
}
}
#endif

static const mod_id MOD_INFORMATION_dda( "dda" );

Expand Down Expand Up @@ -573,11 +571,9 @@ void main_menu::init_strings()
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "A<u|U>topickup" ) );
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "Sa<f|F>emode" ) );
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "Colo<r|R>s" ) );
#if !defined(__ANDROID__)
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "<I|i>mGui Demo Screen" ) );
}
#endif

vSettingsHotkeys.clear();
for( const std::string &item : vSettingsSubItems ) {
Expand Down Expand Up @@ -923,13 +919,11 @@ bool main_menu::opening_screen()
get_safemode().show();
} else if( sel2 == 4 ) { /// Colors
all_colors.show_gui();
#if !defined(__ANDROID__)
} else if( sel2 == 5 ) { /// ImGui demo
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
demo_ui demo;
demo.run();
}
#endif
}
break;
case main_menu_opts::WORLD:
Expand Down
18 changes: 0 additions & 18 deletions src/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "output.h"
#include "ui_manager.h"
#include "ui.h"
#if !defined(__ANDROID__)
#include "cata_imgui.h"
#include "imgui/imgui.h"

Expand Down Expand Up @@ -140,7 +139,6 @@ void query_popup_impl::on_resized()
}
}
}
#endif

query_popup::query_popup()
: cur( 0 ), default_text_color( c_white ), anykey( false ), cancel( false ),
Expand Down Expand Up @@ -277,12 +275,10 @@ void query_popup::invalidate_ui() const
}
legacy_ui->mark_resize();
}
#if !defined(__ANDROID__)
std::shared_ptr<query_popup_impl> imgui_ui = p_impl.lock();
if( imgui_ui ) {
imgui_ui->mark_resized();
}
#endif
}

static constexpr int border_width = 1;
Expand Down Expand Up @@ -502,18 +498,13 @@ query_popup::result query_popup::query_once_legacy()

query_popup::result query_popup::query_once()
{
#if defined(__ANDROID__)
return query_once_legacy();
#else
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
return query_once_imgui();
} else {
return query_once_legacy();
}
#endif
}

#if !defined(__ANDROID__)
std::shared_ptr<query_popup_impl> query_popup::create_or_get_impl()
{
std::shared_ptr<query_popup_impl> impl = p_impl.lock();
Expand All @@ -525,7 +516,6 @@ std::shared_ptr<query_popup_impl> query_popup::create_or_get_impl()
}
return impl;
}
#endif

query_popup::result query_popup::query_once_imgui()
{
Expand Down Expand Up @@ -624,15 +614,11 @@ query_popup::result query_popup::query_once_imgui()

query_popup::result query_popup::query()
{
#if defined(__ANDROID__)
return query_legacy();
#else
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
return query_imgui();
} else {
return query_legacy();
}
#endif
}

query_popup::result query_popup::query_imgui()
Expand Down Expand Up @@ -703,13 +689,9 @@ bool query_popup::button::contains( const point &p ) const

static_popup::static_popup()
{
#if defined(__ANDROID__)
ui = create_or_get_adaptor();
#else
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
ui_imgui = create_or_get_impl();
} else {
ui = create_or_get_adaptor();
}
#endif
}
8 changes: 0 additions & 8 deletions src/popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
#include "input_enums.h"

class ui_adaptor;
#if !defined(__ANDROID__)
class query_popup_impl;
#endif

/**
* UI class for displaying messages or querying player input with popups.
Expand All @@ -37,9 +35,7 @@ class query_popup_impl;

class query_popup
{
#if !defined(__ANDROID__)
friend class query_popup_impl;
#endif
public:
/**
* Query result returned by `query_once` and `query`.
Expand Down Expand Up @@ -206,12 +202,10 @@ class query_popup
* resizing of the popup.
*/
std::shared_ptr<ui_adaptor> create_or_get_adaptor();
#if !defined(__ANDROID__)
std::shared_ptr<query_popup_impl> create_or_get_impl();

result query_imgui();
result query_once_imgui();
#endif
result query_legacy();
result query_once_legacy();

Expand Down Expand Up @@ -302,9 +296,7 @@ class static_popup : public query_popup

private:
std::shared_ptr<ui_adaptor> ui;
#if !defined(__ANDROID__)
std::shared_ptr<query_popup_impl> ui_imgui;
#endif
};

#endif // CATA_SRC_POPUP_H
Loading

0 comments on commit 1a9b9c0

Please sign in to comment.