Skip to content

Commit

Permalink
AtlasTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Apr 25, 2024
1 parent 695bd3a commit cce2064
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
7 changes: 7 additions & 0 deletions doc/examples/GDExt/project/robot_eye.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="AtlasTexture" load_steps=2 format=3 uid="uid://cqelg04ohkgm5"]

[ext_resource type="Texture2D" uid="uid://cr045l23wjmqc" path="res://icon.svg" id="1_d7u5w"]

[resource]
atlas = ExtResource("1_d7u5w")
region = Rect2(142, 228, 64, 64)
2 changes: 1 addition & 1 deletion doc/examples/GDExt/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Example::~Example()
void Example::_ready()
{
ImGui::Godot::SyncImGuiPtrs();
_img = ResourceLoader::get_singleton()->load("res://icon.svg");
_img = ResourceLoader::get_singleton()->load("res://robot_eye.tres");
}

void Example::_process(double delta)
Expand Down
4 changes: 2 additions & 2 deletions doc/examples/GDExt/src/example.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/classes/texture2d.hpp>
#include <godot_cpp/classes/atlas_texture.hpp>

using namespace godot;

Expand All @@ -19,5 +19,5 @@ class Example : public Node
void _process(double delta) override;

private:
Ref<Texture2D> _img;
Ref<AtlasTexture> _img;
};
42 changes: 42 additions & 0 deletions gdext/include/imgui-godot.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define IGN_GDEXT
// GDExtension
#pragma warning(push, 0)
#include <godot_cpp/classes/atlas_texture.hpp>
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/font_file.hpp>
#include <godot_cpp/classes/input_event.hpp>
Expand All @@ -20,6 +21,7 @@
#include <godot_cpp/variant/typed_array.hpp>
#pragma warning(pop)

using godot::AtlasTexture;
using godot::Callable;
using godot::CharString;
using godot::Color;
Expand All @@ -46,6 +48,7 @@ using godot::Window;
#include "core/variant/callable.h"
#include "scene/main/viewport.h"
#include "scene/main/window.h"
#include "scene/resources/atlas_texture.h"
#include "scene/resources/texture.h"
#endif

Expand All @@ -71,6 +74,13 @@ inline bool GET_IMGUIGD()
#endif
return ImGuiGD != nullptr;
}

inline static void GetAtlasUVs(AtlasTexture* tex, ImVec2& uv0, ImVec2& uv1)
{
Vector2 atlasSize = tex->get_atlas()->get_size();
uv0 = tex->get_region().get_position() / atlasSize;
uv1 = tex->get_region().get_end() / atlasSize;
}
} // namespace detail

inline void AddFont(const Ref<FontFile>& fontFile, int fontSize, bool merge = false)
Expand Down Expand Up @@ -665,6 +675,22 @@ inline void Image(const Ref<Texture2D>& tex, const Vector2& size, const Vector2&
ImGui::Image(ImGui::Godot::BindTexture(tex.ptr()), size, uv0, uv1, tint_col, border_col);
}

inline void Image(AtlasTexture* tex, const Vector2& size, const Color& tint_col = {1, 1, 1, 1},
const Color& border_col = {0, 0, 0, 0})
{
ImVec2 uv0, uv1;
ImGui::Godot::detail::GetAtlasUVs(tex, uv0, uv1);
ImGui::Image(ImGui::Godot::BindTexture(tex), size, uv0, uv1, tint_col, border_col);
}

inline void Image(const Ref<AtlasTexture>& tex, const Vector2& size, const Color& tint_col = {1, 1, 1, 1},
const Color& border_col = {0, 0, 0, 0})
{
ImVec2 uv0, uv1;
ImGui::Godot::detail::GetAtlasUVs(tex.ptr(), uv0, uv1);
ImGui::Image(ImGui::Godot::BindTexture(tex.ptr()), size, uv0, uv1, tint_col, border_col);
}

inline bool ImageButton(const char* str_id, Texture2D* tex, const Vector2& size, const Vector2& uv0 = {0, 0},
const Vector2& uv1 = {1, 1}, const Color& bg_col = {0, 0, 0, 0},
const Color& tint_col = {1, 1, 1, 1})
Expand All @@ -678,6 +704,22 @@ inline bool ImageButton(const char* str_id, const Ref<Texture2D>& tex, const Vec
{
return ImGui::ImageButton(str_id, ImGui::Godot::BindTexture(tex.ptr()), size, uv0, uv1, bg_col, tint_col);
}

inline bool ImageButton(const char* str_id, AtlasTexture* tex, const Vector2& size, const Color& bg_col = {0, 0, 0, 0},
const Color& tint_col = {1, 1, 1, 1})
{
ImVec2 uv0, uv1;
ImGui::Godot::detail::GetAtlasUVs(tex, uv0, uv1);
return ImGui::ImageButton(str_id, ImGui::Godot::BindTexture(tex), size, uv0, uv1, bg_col, tint_col);
}

inline bool ImageButton(const char* str_id, const Ref<AtlasTexture>& tex, const Vector2& size,
const Color& bg_col = {0, 0, 0, 0}, const Color& tint_col = {1, 1, 1, 1})
{
ImVec2 uv0, uv1;
ImGui::Godot::detail::GetAtlasUVs(tex.ptr(), uv0, uv1);
return ImGui::ImageButton(str_id, ImGui::Godot::BindTexture(tex.ptr()), size, uv0, uv1, bg_col, tint_col);
}
} // namespace ImGui
#endif

Expand Down

0 comments on commit cce2064

Please sign in to comment.