Skip to content

Commit

Permalink
Add image button to ImmediateGui
Browse files Browse the repository at this point in the history
  • Loading branch information
marioCST committed Feb 11, 2023
1 parent eb44bcf commit 8f43446
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
57 changes: 57 additions & 0 deletions Horion/ImmediateGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ ComponentInfo::ComponentInfo(int id) : id(id) {
ButtonInfo::ButtonInfo(int id, Vec2 pos, bool centered) : ComponentInfo(id), pos(pos), centered(centered) {
}

ImageButtonInfo::ImageButtonInfo(int id, Vec2 pos, bool centered) : ButtonInfo(id, pos, centered) {
}

void ButtonInfo::calculateSize(const char* txt) {
std::string str(txt);
size = {DrawUtils::getTextWidth(&str), DrawUtils::getFont(Fonts::SMOOTH)->getLineHeight()};
Expand Down Expand Up @@ -53,6 +56,41 @@ void ButtonInfo::draw(Vec2 mousePos, const char* label) {
}
}

void ImageButtonInfo::updateSize(Vec2 size) {
this->size = size;
}

void ImageButtonInfo::draw(Vec2 mousePos, const char* location) {
auto surface = getSelectableSurface();
Vec2 imagePos = pos;
Vec2 imageSize = size;

if (centered) {
imagePos.x -= imageSize.x / 2;
imagePos.y -= imageSize.y / 2;
}

if (isInSelectableSurface(mousePos)) {
if (DrawUtils::isLeftClickDown) {
surface = surface.shrink(0.8f);
imageSize.x *= 0.95f;
imageSize.y *= 0.95f;

imagePos.x += imagePos.x * 0.025f;
imagePos.y += imagePos.y * 0.025f;
}

DrawUtils::fillRectangle(surface, MC_Color(85, 85, 85), 0.2f);
canClickB = true;
} else {
//DrawUtils::fillRectangle(surface, MC_Color(12, 12, 12), 1);
canClickB = false;
}

DrawUtils::drawImage(location, imagePos, imageSize);
DrawUtils::flushImage();
}

void ImmediateGui::startFrame() {
Vec2 windowSize = Game.getClientInstance()->getGuiData()->windowSize;
Vec2 windowSizeReal = Game.getClientInstance()->getGuiData()->windowSizeReal;
Expand Down Expand Up @@ -82,3 +120,22 @@ bool ImmediateGui::Button(const char* label, Vec2 pos, bool centered) {

return false;
}

bool ImmediateGui::ImageButton(const char* location, Vec2 pos, Vec2 size, bool centered) {
auto id = Utils::getCrcHash(location);
if (components.find(id) == components.end()) { // Create new button
components[id] = std::make_shared<ImageButtonInfo>(id, pos, centered);
}
auto comp = components[id];
auto button = dynamic_cast<ImageButtonInfo*>(comp.get());

button->updatePos(pos);
button->updateSize(size);
button->draw(mousePos, location);
if (button->canClick() && DrawUtils::shouldToggleLeftClick) { // Click
DrawUtils::shouldToggleLeftClick = false;
return true;
}

return false;
}
14 changes: 12 additions & 2 deletions Horion/ImmediateGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ComponentInfo {
};

class ButtonInfo : public ComponentInfo {
private:
protected:
Vec2 pos;
Vec2 size;
bool centered;
Expand All @@ -30,12 +30,21 @@ class ButtonInfo : public ComponentInfo {
void calculateSize(const char*);
bool isInSelectableSurface(Vec2 mouse);
Vec4 getSelectableSurface();
void draw(Vec2 mousePos, const char* label);
virtual void draw(Vec2 mousePos, const char* label);
bool canClick() { return canClickB; };
void updatePos(Vec2 pos) { pos = pos; }

};

class ImageButtonInfo : public ButtonInfo {
public:
ImageButtonInfo(int id, Vec2 pos, bool centered = false);
virtual ~ImageButtonInfo(){};

virtual void draw(Vec2 mousePos, const char* location) override;
void updateSize(Vec2 size);
};

class ImmediateGui {
private:
Vec2 mousePos;
Expand All @@ -44,6 +53,7 @@ class ImmediateGui {
public:
void startFrame();
bool Button(const char* label, Vec2 pos, bool centered = false);
bool ImageButton(const char* location, Vec2 pos, Vec2 size, bool centered = false);
};

extern ImmediateGui HorionGui;
6 changes: 5 additions & 1 deletion Horion/Module/Modules/TestModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ void TestModule::onPreRender(MinecraftUIRenderContext* renderCtx) {
clientMessageF("Test Button Was Clicked");
}

DrawUtils::drawImage("textures/ui/title", Vec2(100,100), Vec2(200,200));
if (HorionGui.ImageButton("textures/ui/title", Vec2(100, 200), Vec2(200, 100))) {
clientMessageF("Test Image Button Was Clicked");
}

DrawUtils::drawImage("textures/ui/title", Vec2(100,100), Vec2(200,100));
DrawUtils::flushImage();
}

Expand Down

0 comments on commit 8f43446

Please sign in to comment.