#include "noesis_shim.h"
#include <NsCore/BaseComponent.h>
#include <NsCore/DynamicCast.h>
#include <NsCore/Noesis.h>
#include <NsCore/Ptr.h>
#include <NsDrawing/Int32Rect.h>
#include <NsGui/BitmapImage.h>
#include <NsGui/BitmapSource.h>
#include <NsGui/CroppedBitmap.h>
#include <NsGui/DynamicTextureSource.h>
#include <NsGui/ImageSource.h>
#include <NsGui/TextureSource.h>
#include <NsGui/Uri.h>
#include <NsRender/Texture.h>
namespace {
void* handout(Noesis::BaseComponent* c) {
if (!c) return nullptr;
c->AddReference();
return c;
}
template <class T>
T* cast(void* p) {
if (!p) return nullptr;
return Noesis::DynamicCast<T*>(static_cast<Noesis::BaseComponent*>(p));
}
void* borrow(Noesis::BitmapSource* s) {
if (!s) return nullptr;
return static_cast<Noesis::BaseComponent*>(s);
}
}
extern "C" void* noesis_cropped_bitmap_create() {
Noesis::Ptr<Noesis::CroppedBitmap> c = *new Noesis::CroppedBitmap();
return handout(c.GetPtr());
}
extern "C" bool noesis_cropped_bitmap_set_source(void* crop, void* source) {
auto* c = cast<Noesis::CroppedBitmap>(crop);
if (!c) return false;
c->SetSource(cast<Noesis::BitmapSource>(source));
return true;
}
extern "C" void* noesis_cropped_bitmap_get_source(void* crop) {
auto* c = cast<Noesis::CroppedBitmap>(crop);
if (!c) return nullptr;
return borrow(c->GetSource());
}
extern "C" bool noesis_cropped_bitmap_set_source_rect(void* crop, int32_t x, int32_t y,
uint32_t width, uint32_t height) {
auto* c = cast<Noesis::CroppedBitmap>(crop);
if (!c) return false;
c->SetSourceRect(Noesis::Int32Rect(x, y, width, height));
return true;
}
extern "C" bool noesis_cropped_bitmap_get_source_rect(void* crop, int32_t* x, int32_t* y,
uint32_t* width, uint32_t* height) {
auto* c = cast<Noesis::CroppedBitmap>(crop);
if (!c) return false;
const Noesis::Int32Rect& r = c->GetSourceRect();
if (x) *x = r.x;
if (y) *y = r.y;
if (width) *width = r.width;
if (height) *height = r.height;
return true;
}
extern "C" void* noesis_texture_source_create(void* texture) {
auto* tex = cast<Noesis::Texture>(texture);
Noesis::Ptr<Noesis::TextureSource> s =
tex ? *new Noesis::TextureSource(tex) : *new Noesis::TextureSource();
return handout(s.GetPtr());
}
extern "C" bool noesis_texture_source_set_texture(void* source, void* texture) {
auto* s = cast<Noesis::TextureSource>(source);
if (!s) return false;
s->SetTexture(cast<Noesis::Texture>(texture));
return true;
}
extern "C" void* noesis_texture_source_get_texture(void* source) {
auto* s = cast<Noesis::TextureSource>(source);
if (!s) return nullptr;
return static_cast<Noesis::BaseComponent*>(s->GetTexture());
}
extern "C" void* noesis_bitmap_image_create(const char* uri) {
Noesis::Ptr<Noesis::BitmapImage> b =
uri ? *new Noesis::BitmapImage(Noesis::Uri(uri)) : *new Noesis::BitmapImage();
return handout(b.GetPtr());
}
extern "C" bool noesis_bitmap_image_set_uri_source(void* image, const char* uri) {
auto* b = cast<Noesis::BitmapImage>(image);
if (!b) return false;
b->SetUriSource(Noesis::Uri(uri ? uri : ""));
return true;
}
extern "C" const char* noesis_bitmap_image_get_uri_source(void* image) {
auto* b = cast<Noesis::BitmapImage>(image);
if (!b) return nullptr;
return b->GetUriSource().Str();
}
extern "C" bool noesis_bitmap_source_get_pixel_size(void* source, int32_t* width,
int32_t* height) {
auto* s = cast<Noesis::BitmapSource>(source);
if (!s) return false;
if (width) *width = s->GetPixelWidth();
if (height) *height = s->GetPixelHeight();
return true;
}
extern "C" bool noesis_bitmap_source_get_dpi(void* source, float* dpi_x, float* dpi_y) {
auto* s = cast<Noesis::BitmapSource>(source);
if (!s) return false;
if (dpi_x) *dpi_x = s->GetDpiX();
if (dpi_y) *dpi_y = s->GetDpiY();
return true;
}
extern "C" void* noesis_dynamic_texture_source_create(
uint32_t width, uint32_t height, noesis_texture_render_callback callback, void* user) {
if (!callback) return nullptr;
auto cb = reinterpret_cast<Noesis::DynamicTextureSource::TextureRenderCallback>(callback);
Noesis::Ptr<Noesis::DynamicTextureSource> s =
*new Noesis::DynamicTextureSource(width, height, cb, user);
return handout(s.GetPtr());
}
extern "C" bool noesis_dynamic_texture_source_resize(void* source, uint32_t width,
uint32_t height) {
auto* s = cast<Noesis::DynamicTextureSource>(source);
if (!s) return false;
s->Resize(width, height);
return true;
}
extern "C" bool noesis_dynamic_texture_source_get_pixel_size(void* source, uint32_t* width,
uint32_t* height) {
auto* s = cast<Noesis::DynamicTextureSource>(source);
if (!s) return false;
if (width) *width = s->GetPixelWidth();
if (height) *height = s->GetPixelHeight();
return true;
}