explose base and imgui as plugin

This commit is contained in:
Green Sky 2023-11-02 00:14:34 +01:00
parent 0078850db4
commit 95a037ea35
No known key found for this signature in database
8 changed files with 317 additions and 1 deletions

View File

@ -13,6 +13,8 @@ else()
endif()
message("II SOLANACEAE_CRDTNOTES_STANDALONE " ${SOLANACEAE_CRDTNOTES_STANDALONE})
option(SOLANACEAE_CRDTNOTES_BUILD_PLUGINS "Build the crdtnotes plugins" ${SOLANACEAE_CRDTNOTES_STANDALONE})
if (SOLANACEAE_CRDTNOTES_STANDALONE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@ -31,7 +33,7 @@ if (SOLANACEAE_CRDTNOTES_STANDALONE)
endif()
# external libs
add_subdirectory(./external) # before increasing warn levels, sad :(
add_subdirectory(./external EXCLUDE_FROM_ALL) # before increasing warn levels, sad :(
if (SOLANACEAE_CRDTNOTES_STANDALONE)
set(CMAKE_CXX_EXTENSIONS OFF)
@ -65,3 +67,7 @@ endif()
add_subdirectory(./src)
if (SOLANACEAE_CRDTNOTES_BUILD_PLUGINS)
add_subdirectory(./plugins)
endif()

View File

@ -10,3 +10,53 @@ if (NOT TARGET crdt_version3)
FetchContent_MakeAvailable(crdt_version3)
endif()
if (NOT TARGET imgui)
FetchContent_Declare(imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG d4ddc46e7
)
# imgui does not provide a cmake
#FetchContent_GetProperties(imgui_interface)
#if(NOT imgui_interface_POPULATED)
#FetchContent_Populate(imgui_interface)
#add_library(imgui_interface INTERFACE
#${imgui_interface_SOURCE_DIR}/imgui.h
#${imgui_interface_SOURCE_DIR}/misc/cpp/imgui_stdlib.h
#)
#target_include_directories(imgui_interface INTERFACE ${imgui_interface_SOURCE_DIR})
#endif()
FetchContent_GetProperties(imgui)
if(NOT imgui_POPULATED)
FetchContent_Populate(imgui)
add_library(imgui STATIC
${imgui_SOURCE_DIR}/imgui.h
${imgui_SOURCE_DIR}/imgui_internal.h
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui_demo.cpp
${imgui_SOURCE_DIR}/imgui_draw.cpp
${imgui_SOURCE_DIR}/imgui_tables.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
${imgui_SOURCE_DIR}/imstb_rectpack.h
${imgui_SOURCE_DIR}/imstb_textedit.h
${imgui_SOURCE_DIR}/imstb_truetype.h
${imgui_SOURCE_DIR}/misc/cpp/imgui_stdlib.h
${imgui_SOURCE_DIR}/misc/cpp/imgui_stdlib.cpp
)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR})
endif()
#FetchContent_MakeAvailable(imgui)
endif()
if (NOT TARGET solanaceae_plugin)
FetchContent_Declare(solanaceae_plugin
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_plugin.git
GIT_TAG master
)
FetchContent_MakeAvailable(solanaceae_plugin)
endif()

24
plugins/CMakeLists.txt Normal file
View File

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
add_library(plugin_crdtnotes SHARED
./plugin_crdtnotes.cpp
)
target_compile_features(plugin_crdtnotes PUBLIC cxx_std_17)
target_link_libraries(plugin_crdtnotes PUBLIC
solanaceae_crdtnotes
solanaceae_plugin
)
########################################
add_library(plugin_crdtnotes_imgui SHARED
./plugin_crdtnotes_imgui.cpp
)
target_compile_features(plugin_crdtnotes_imgui PUBLIC cxx_std_17)
target_link_libraries(plugin_crdtnotes_imgui PUBLIC
solanaceae_crdtnotes_imgui
solanaceae_plugin
)
########################################

View File

@ -0,0 +1,65 @@
#include <solanaceae/plugin/solana_plugin_v1.h>
#include <solanaceae/crdtnotes/crdtnotes.hpp>
//#include <solanaceae/util/config_model.hpp>
#include <memory>
#include <iostream>
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
static std::unique_ptr<CRDTNotes> g_crdtn = nullptr;
extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return "CRDTNotes";
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
return SOLANA_PLUGIN_VERSION;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN CRDTN START()\n";
if (solana_api == nullptr) {
return 1;
}
//ConfigModelI* conf = nullptr;
{ // make sure required types are loaded
//conf = RESOLVE_INSTANCE(ConfigModelI);
//if (conf == nullptr) {
//std::cerr << "PLUGIN CRDTN missing ConfigModelI\n";
//return 2;
//}
}
// static store, could be anywhere tho
// construct with fetched dependencies
g_crdtn = std::make_unique<CRDTNotes>(/**conf*/);
// register types
PROVIDE_INSTANCE(CRDTNotes, "CRDTNotes", g_crdtn.get());
return 0;
}
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN CRDTN STOP()\n";
g_crdtn.reset();
}
SOLANA_PLUGIN_EXPORT void solana_plugin_tick(float delta) {
(void)delta;
//std::cout << "PLUGIN CRDTN TICK()\n";
//g_crdtn->iterate();
}
} // extern C

View File

@ -0,0 +1,82 @@
#include <solanaceae/plugin/solana_plugin_v1.h>
#include <solanaceae/crdtnotes_imgui/crdtnotes_imgui.hpp>
//#include <solanaceae/util/config_model.hpp>
#include <imgui.h>
#include <memory>
#include <iostream>
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
static std::unique_ptr<CRDTNotesImGui> g_crdtn_imgui = nullptr;
extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return "CRDTNIMGUIotesImGui";
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
return SOLANA_PLUGIN_VERSION;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN CRDTNIMGUI START()\n";
if (solana_api == nullptr) {
return 1;
}
//ConfigModelI* conf = nullptr;
CRDTNotes* crdtn = nullptr;
ImGuiContext* imguic = nullptr;
{ // make sure required types are loaded
//conf = RESOLVE_INSTANCE(ConfigModelI);
crdtn = RESOLVE_INSTANCE(CRDTNotes);
imguic = RESOLVE_INSTANCE(ImGuiContext);
//if (conf == nullptr) {
//std::cerr << "PLUGIN CRDTNIMGUI missing ConfigModelI\n";
//return 2;
//}
if (crdtn == nullptr) {
std::cerr << "PLUGIN CRDTNIMGUI missing CRDTNotes\n";
return 2;
}
if (imguic == nullptr) {
std::cerr << "PLUGIN CRDTNIMGUI missing ImGuiContext\n";
return 2;
}
}
ImGui::SetCurrentContext(imguic);
// static store, could be anywhere tho
// construct with fetched dependencies
g_crdtn_imgui = std::make_unique<CRDTNotesImGui>(*crdtn);
// register types
PROVIDE_INSTANCE(CRDTNotesImGui, "CRDTNotesImGui", g_crdtn_imgui.get());
return 0;
}
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN CRDTNIMGUI STOP()\n";
g_crdtn_imgui.reset();
}
SOLANA_PLUGIN_EXPORT void solana_plugin_tick(float delta) {
(void)delta;
//std::cout << "PLUGIN CRDTNIMGUI TICK()\n";
g_crdtn_imgui->render();
}
} // extern C

View File

@ -13,3 +13,17 @@ target_link_libraries(solanaceae_crdtnotes PUBLIC
########################################
add_library(solanaceae_crdtnotes_imgui
./solanaceae/crdtnotes_imgui/crdtnotes_imgui.hpp
./solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp
)
target_include_directories(solanaceae_crdtnotes_imgui PUBLIC .)
target_compile_features(solanaceae_crdtnotes_imgui PUBLIC cxx_std_17)
target_link_libraries(solanaceae_crdtnotes_imgui PUBLIC
solanaceae_crdtnotes
#imgui_interface
imgui
#solanaceae_util
)
########################################

View File

@ -0,0 +1,60 @@
#include "./crdtnotes_imgui.hpp"
#include <cstdint>
#include <imgui.h>
namespace detail {
uint8_t nib_from_hex(char c) {
assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
if (c >= '0' && c <= '9') {
return static_cast<uint8_t>(c) - '0';
} else if (c >= 'a' && c <= 'f') {
return (static_cast<uint8_t>(c) - 'a') + 10u;
} else {
return 0u;
}
}
char nib_to_hex(uint8_t c) {
assert((c & 0xf0) == 0x00);
if (/*c >= 0x00 &&*/ c <= 0x09) {
return static_cast<char>(c) + '0';
} else if (c >= 0x0a && c <= 0x0f) {
return (static_cast<char>(c) + 'a') - 10u;
} else {
return 0u;
}
}
template<typename Container>
std::string to_hex(const Container& data) {
std::string res;
for (const uint8_t it : data) {
res += nib_to_hex(it << 4);
res += nib_to_hex(it & 0x0f);
}
return res;
}
} // detail
CRDTNotesImGui::CRDTNotesImGui(CRDTNotes& notes) : _notes(notes) {
}
float CRDTNotesImGui::render(void) {
if (_show_global_list) {
if (ImGui::Begin("CRDTNotes - Global list")) {
const auto doclist = _notes.getDocList();
for (const auto& docid : doclist) {
const auto docid_str = detail::to_hex(docid);
ImGui::TextUnformatted(docid_str.c_str());
}
}
ImGui::End();
}
return 1.f;
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <solanaceae/crdtnotes/crdtnotes.hpp>
class CRDTNotesImGui {
CRDTNotes& _notes;
bool _show_global_list {true};
public:
CRDTNotesImGui(CRDTNotes& notes);
float render(void);
};