-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from ShadelessFox/canvas-ui-node
Add `canvas` UI node
- Loading branch information
Showing
12 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "Canvas.hpp" | ||
|
||
#include "graphics/core/Batch2D.hpp" | ||
#include "graphics/core/DrawContext.hpp" | ||
#include "graphics/core/Texture.hpp" | ||
|
||
gui::Canvas::Canvas(ImageFormat inFormat, glm::uvec2 inSize) : UINode(inSize) { | ||
ImageData data {inFormat, inSize.x, inSize.y}; | ||
mTexture = Texture::from(&data); | ||
} | ||
|
||
void gui::Canvas::draw(const DrawContext& pctx, const Assets& assets) { | ||
auto pos = calcPos(); | ||
auto col = calcColor(); | ||
|
||
auto batch = pctx.getBatch2D(); | ||
batch->texture(mTexture.get()); | ||
batch->rect(pos.x, pos.y, size.x, size.y, 0, 0, 0, {}, false, true, col); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include "UINode.hpp" | ||
#include "graphics/core/ImageData.hpp" | ||
#include "graphics/core/Texture.hpp" | ||
|
||
class Texture; | ||
|
||
namespace gui { | ||
class Canvas final : public UINode { | ||
public: | ||
explicit Canvas(ImageFormat inFormat, glm::uvec2 inSize); | ||
|
||
~Canvas() override = default; | ||
|
||
void draw(const DrawContext& pctx, const Assets& assets) override; | ||
|
||
[[nodiscard]] std::shared_ptr<::Texture> texture() const { | ||
return mTexture; | ||
} | ||
private: | ||
std::shared_ptr<::Texture> mTexture; | ||
std::unique_ptr<ImageData> mData; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#include <unordered_map> | ||
|
||
#include "graphics/core/ImageData.hpp" | ||
#include "graphics/core/Texture.hpp" | ||
#include "logic/scripting/lua/lua_custom_types.hpp" | ||
#include "logic/scripting/lua/lua_util.hpp" | ||
|
||
using namespace lua; | ||
|
||
LuaCanvas::LuaCanvas(std::shared_ptr<Texture> inTexture) | ||
: mTexture(std::move(inTexture)) { | ||
mData = mTexture->readData(); | ||
} | ||
|
||
union RGBA { | ||
uint8_t rgba[4]; | ||
uint32_t raw; | ||
}; | ||
|
||
static RGBA* get_at(const ImageData& data, uint index) { | ||
if (index >= data.getWidth() * data.getHeight()) { | ||
return nullptr; | ||
} | ||
return reinterpret_cast<RGBA*>(data.getData() + index * sizeof(RGBA)); | ||
} | ||
|
||
static RGBA* get_at(const ImageData& data, uint x, uint y) { | ||
return get_at(data, y * data.getWidth() + x); | ||
} | ||
|
||
static RGBA* get_at(State* L, uint x, uint y) { | ||
if (auto texture = touserdata<LuaCanvas>(L, 1)) { | ||
return get_at(texture->data(), x, y); | ||
} | ||
return nullptr; | ||
} | ||
|
||
static int l_at(State* L) { | ||
auto x = static_cast<uint>(tointeger(L, 2)); | ||
auto y = static_cast<uint>(tointeger(L, 3)); | ||
|
||
if (auto pixel = get_at(L, x, y)) { | ||
return pushinteger(L, pixel->raw); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int l_set(State* L) { | ||
auto x = static_cast<uint>(tointeger(L, 2)); | ||
auto y = static_cast<uint>(tointeger(L, 3)); | ||
|
||
if (auto pixel = get_at(L, x, y)) { | ||
switch (gettop(L)) { | ||
case 4: | ||
pixel->raw = static_cast<uint>(tointeger(L, 4)); | ||
return 1; | ||
case 6: | ||
pixel->rgba[0] = static_cast<ubyte>(tointeger(L, 4)); | ||
pixel->rgba[1] = static_cast<ubyte>(tointeger(L, 5)); | ||
pixel->rgba[2] = static_cast<ubyte>(tointeger(L, 6)); | ||
pixel->rgba[3] = 255; | ||
return 1; | ||
case 7: | ||
pixel->rgba[0] = static_cast<ubyte>(tointeger(L, 4)); | ||
pixel->rgba[1] = static_cast<ubyte>(tointeger(L, 5)); | ||
pixel->rgba[2] = static_cast<ubyte>(tointeger(L, 6)); | ||
pixel->rgba[3] = static_cast<ubyte>(tointeger(L, 7)); | ||
return 1; | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int l_update(State* L) { | ||
if (auto texture = touserdata<LuaCanvas>(L, 1)) { | ||
texture->texture().reload(texture->data()); | ||
} | ||
return 0; | ||
} | ||
|
||
static std::unordered_map<std::string, lua_CFunction> methods { | ||
{"at", lua::wrap<l_at>}, | ||
{"set", lua::wrap<l_set>}, | ||
{"update", lua::wrap<l_update>} | ||
}; | ||
|
||
static int l_meta_index(State* L) { | ||
auto texture = touserdata<LuaCanvas>(L, 1); | ||
if (texture == nullptr) { | ||
return 0; | ||
} | ||
auto& data = texture->data(); | ||
if (isnumber(L, 2)) { | ||
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) { | ||
return pushinteger(L, pixel->raw); | ||
} | ||
} | ||
if (isstring(L, 2)) { | ||
auto name = tostring(L, 2); | ||
if (!strcmp(name, "width")) { | ||
return pushinteger(L, data.getWidth()); | ||
} | ||
if (!strcmp(name, "height")) { | ||
return pushinteger(L, data.getHeight()); | ||
} | ||
if (auto func = methods.find(tostring(L, 2)); func != methods.end()) { | ||
return pushcfunction(L, func->second); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
static int l_meta_newindex(State* L) { | ||
auto texture = touserdata<LuaCanvas>(L, 1); | ||
if (texture == nullptr) { | ||
return 0; | ||
} | ||
auto& data = texture->data(); | ||
if (isnumber(L, 2) && isnumber(L, 3)) { | ||
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) { | ||
pixel->raw = static_cast<uint>(tointeger(L, 3)); | ||
return 1; | ||
} | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
int LuaCanvas::createMetatable(State* L) { | ||
createtable(L, 0, 3); | ||
pushcfunction(L, lua::wrap<l_meta_index>); | ||
setfield(L, "__index"); | ||
pushcfunction(L, lua::wrap<l_meta_newindex>); | ||
setfield(L, "__newindex"); | ||
return 1; | ||
} |