Skip to content

Commit

Permalink
Merge pull request #72345 from katemonster33/android_imgui
Browse files Browse the repository at this point in the history
Add ImGui support to Android
  • Loading branch information
dseguin authored Mar 14, 2024
2 parents 945f980 + 24f174d commit cc6589a
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 89 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)
41 changes: 39 additions & 2 deletions src/cata_imgui.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if !defined(__ANDROID__)
#include "cata_imgui.h"

#include <stack>
Expand All @@ -12,6 +11,8 @@
#include "output.h"
#include "ui_manager.h"

static 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 +207,43 @@ void cataimgui::client::process_input( void *input )

#endif

static 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 <= '9' ) {
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 Expand Up @@ -475,4 +513,3 @@ cataimgui::bounds cataimgui::window::get_bounds()
{
return { -1.f, -1.f, -1.f, -1.f };
}
#endif // #if defined(__ANDROID__)
4 changes: 2 additions & 2 deletions src/cata_imgui.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if !defined(__ANDROID__)
#pragma once
#include <string>
#include <vector>
Expand All @@ -7,6 +6,7 @@
#include <array>

class nc_color;
struct input_event;
struct item_info_data;

#if defined(WIN32) || defined(TILES)
Expand Down Expand Up @@ -50,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 Expand Up @@ -107,4 +108,3 @@ void load_colors();
#endif

} // namespace cataimgui
#endif // #if defined(__ANDROID)
16 changes: 3 additions & 13 deletions src/input_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@
#include "string_input_popup.h"
#include "translations.h"
#include "ui_manager.h"
#include "cata_imgui.h"
#include "imgui/imgui.h"

enum class kb_menu_status {
remove, reset, add, add_global, execute, show
};

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

class keybindings_ui : public cataimgui::window
{
// colors of the keybindings
Expand Down Expand Up @@ -74,7 +72,6 @@ class keybindings_ui : public cataimgui::window
init();
};
};
#endif

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

Expand Down Expand Up @@ -608,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 @@ -764,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 @@ -1288,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 @@ -1469,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 @@ -463,9 +459,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
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ void exit_handler( int s )
} else
#endif
{
#if !defined(__ANDROID__)
imclient.reset();
#endif
exit( exit_status );
}
}
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
20 changes: 1 addition & 19 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 Down Expand Up @@ -620,6 +611,7 @@ query_popup::result query_popup::query_once_imgui()
return res;
}


query_popup::result query_popup::query()
{
if( get_options().has_option( "USE_IMGUI" ) && get_option<bool>( "USE_IMGUI" ) ) {
Expand All @@ -639,13 +631,7 @@ query_popup::result query_popup::query_imgui()
} while( res.wait_input );
return res;
}
#else

query_popup::result query_popup::query()
{
return query_legacy();
}
#endif

query_popup::result query_popup::query_legacy()
{
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
}
Loading

0 comments on commit cc6589a

Please sign in to comment.