Skip to content

Commit f15e052

Browse files
committed
Update UI example
1 parent 06bb69f commit f15e052

File tree

13 files changed

+135
-27
lines changed

13 files changed

+135
-27
lines changed

project/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ endif(UNIX)
3838
# Copy project data to output path
3939
file(COPY ../project_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug)
4040
file(COPY ../project_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Release)
41+
file(COPY ../project_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
4142

4243
IF (WIN32)
4344
## Copy dll's to output path
@@ -57,4 +58,5 @@ ENDIF(WIN32)
5758
add_subdirectory(Box2DTestbed)
5859
add_subdirectory(platformer)
5960
add_subdirectory(particle_system)
61+
add_subdirectory(gui)
6062
# add_subdirectory(BussIK)

project/gui/Hero.cpp

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,30 @@
1212

1313
using namespace sre;
1414

15+
std::map<std::string, std::shared_ptr<sre::Texture>> Hero::inventoryTexture;
16+
1517
Hero::Hero(GameObject *gameObject)
1618
: Component(gameObject), velocity(0) {
1719
auto heroTexture = sre::Texture::create()
1820
.withFile("assets/Goblin.png")
1921
.withFilterSampling(false)
2022
.build();
2123

24+
inventorySet = {"Antidote",
25+
"Axe",
26+
"BlueMagic",
27+
"Carrot",
28+
"Hat",
29+
"Lemon",
30+
"Necklace",
31+
"Shield"};
32+
2233
heroSpriteAtlas = SpriteAtlas::createSingleSprite(heroTexture, "hero");
2334

2435
auto spriteComponent = gameObject->addComponent<SpriteComponent>();
25-
spriteComponent->setSprite(heroSpriteAtlas->get("hero"));
36+
auto sprite = heroSpriteAtlas->get("hero");
37+
heroSize = sprite.getSpriteSize();
38+
spriteComponent->setSprite(sprite);
2639

2740
gameObject->setPosition({305,200});
2841

@@ -50,11 +63,9 @@ bool Hero::onKey(SDL_Event &event) {
5063
velocity.x = event.type == SDL_KEYDOWN?1:0;
5164
break;
5265
case SDLK_h:
66+
speachBubbleTimeOut = 5;
5367
message = "Hello world";
5468
break;
55-
case SDLK_SPACE:
56-
message = "";
57-
break;
5869
}
5970
}
6071

@@ -63,23 +74,29 @@ void Hero::update(float deltaTime) {
6374
auto newPos = gameObject->getPosition() + velocity*speed*deltaTime;
6475

6576
gameObject->setPosition(newPos);
77+
speachBubbleTimeOut -= deltaTime;
78+
if (speachBubbleTimeOut < 0){
79+
message = "";
80+
}
81+
6682
}
6783

68-
void Hero::speechBubble(){
84+
void Hero::guiSpeechBubble(){
6985
auto r = Renderer::instance;
7086
auto winsize = r->getWindowSize();
7187

7288
// display info over hero
7389
auto flags =
7490
ImGuiWindowFlags_NoTitleBar |
7591
ImGuiWindowFlags_NoResize |
76-
ImGuiWindowFlags_NoMove;
92+
ImGuiWindowFlags_NoMove |
93+
ImGuiWindowFlags_NoScrollbar;
7794
bool* open = nullptr;
7895

7996
auto heroPos = gameObject->getPosition();
80-
ImVec2 popupSize(200,50);
97+
ImVec2 popupSize(300, 50);
8198

82-
ImVec2 pos (heroPos.x/2 - popupSize.x/2, (winsize.y -heroPos.y)/2 + popupSize.y*2);
99+
ImVec2 pos (heroPos.x - popupSize.x / 2, (winsize.y - heroPos.y) - popupSize.y - heroSize.y );
83100
auto cond = ImGuiCond_Always;
84101
ImGui::SetNextWindowPos(pos, cond);
85102
ImGui::SetNextWindowSize(popupSize, cond);
@@ -91,31 +108,96 @@ void Hero::speechBubble(){
91108
ImGui::End();
92109
}
93110

94-
void Hero::gameInfo() {
95-
ImVec2 pos = {0,0};
111+
void Hero::guiGameInfo() {
112+
auto r = Renderer::instance;
113+
auto winsize = r->getWindowSize();
114+
ImVec2 size = {220,60};
115+
ImVec2 pos = {winsize.x - size.x,0};
96116
auto cond = ImGuiCond_Always;
97117
ImVec2 pivot = {0,0};
98118
ImGui::SetNextWindowPos(pos, cond, pivot);
99-
ImVec2 size = {100,50};
119+
100120
ImGui::SetNextWindowSize(size, cond);
101121
auto flags =
102122
ImGuiWindowFlags_NoTitleBar |
103123
ImGuiWindowFlags_NoResize |
104-
ImGuiWindowFlags_NoMove;
124+
ImGuiWindowFlags_NoMove |
125+
ImGuiWindowFlags_NoScrollbar;
105126
bool* open = nullptr;
106-
ImGui::Begin("", open, flags);
127+
ImGui::Begin("#gameinfo", open, flags);
107128
ImGui::PushFont(ProggyTiny);
108-
ImGui::Text("Score");
109-
ImGui::SameLine();
129+
130+
// draw score
131+
ImGui::Text("Score"); ImGui::SameLine();
132+
auto scoreStr = std::to_string(score);
133+
float windowWidth = 
 ImGui::GetWindowContentRegionWidth();
134+
float width = ImGui::CalcTextSize(scoreStr.c_str()).x;
135+
ImGui::SetCursorPosX(windowWidth - width); // align right
136+
ImGui::Text(scoreStr.c_str());
137+
138+
// draw health
139+
ImGui::PushID(1);
140+
auto healthStr = std::to_string(health);
141+
ImGui::Text("Health"); ImGui::SameLine();
142+
width = ImGui::CalcTextSize(healthStr.c_str()).x;
143+
ImGui::SetCursorPosX(windowWidth - width); // align right
144+
ImGui::Text(healthStr.c_str());
145+
ImGui::PopID();
110146
ImGui::PopFont();
111147
ImGui::End();
112148
}
113149

114-
void Hero::onGui() {
115-
if (message != ""){
116-
speechBubble();
150+
void Hero::guiInventory() {
151+
ImVec2 pos = {0,0};
152+
auto cond = ImGuiCond_Always;
153+
ImVec2 pivot = {0,0};
154+
ImGui::SetNextWindowPos(pos, cond, pivot);
155+
ImVec2 size = {165, 107};
156+
ImGui::SetNextWindowSize(size, cond);
157+
auto flags =
158+
ImGuiWindowFlags_NoTitleBar |
159+
ImGuiWindowFlags_NoResize |
160+
ImGuiWindowFlags_NoMove |
161+
ImGuiWindowFlags_NoScrollbar;
162+
bool* open = nullptr;
163+
ImGui::Begin("#inventory", open, flags);
164+
ImGui::PushFont(ProggyTiny);
165+
ImGui::Text("Inventory");
166+
167+
int count = 0;
168+
for (auto& item : inventorySet){
169+
auto hasItem = inventoryTexture.find(item) != inventoryTexture.end();
170+
if (!hasItem){
171+
auto filename = std::string("assets/")+item+".png";
172+
inventoryTexture[item] = Texture::create().withFile(filename).withFilterSampling(false).build();
173+
}
174+
ImVec2 uv0(0,1); // flip y axis coordinates
175+
ImVec2 uv1(1,0);
176+
ImVec2 s(30,30);
177+
ImGui::Image(inventoryTexture[item]->getNativeTexturePtr(), s, uv0, uv1 , ImVec4(1,1,1,1),ImVec4(0,0,0,1));
178+
179+
if (ImGui::IsItemHovered())
180+
{
181+
ImGui::BeginTooltip();
182+
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
183+
ImGui::TextUnformatted(item.c_str());
184+
ImGui::PopTextWrapPos();
185+
ImGui::EndTooltip();
186+
}
187+
188+
if (count == 0 || count %4 != 0)
189+
ImGui::SameLine();
190+
count ++;
117191
}
118-
gameInfo();
119192

193+
ImGui::PopFont();
194+
ImGui::End();
195+
}
120196

197+
void Hero::onGui() {
198+
if (!message.empty()){
199+
guiSpeechBubble();
200+
}
201+
guiGameInfo();
202+
guiInventory();
121203
}

project/gui/Hero.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ class Hero : public Component {
1515
bool onKey(SDL_Event &event) override;
1616
void update(float deltaTime) override;
1717
private:
18-
void gameInfo();
19-
void speechBubble();
18+
void guiGameInfo();
19+
void guiSpeechBubble();
20+
void guiInventory();
21+
2022
std::shared_ptr<sre::SpriteAtlas> heroSpriteAtlas;
23+
static std::map<std::string, std::shared_ptr<sre::Texture>> inventoryTexture;
2124
glm::vec2 velocity;
2225
ImFont* ProggyTiny;
23-
std::string message = "Hi!";
26+
27+
float speachBubbleTimeOut = 10;
28+
std::string message = "Hi! AWSD to control.";
29+
30+
glm::ivec2 heroSize;
2431

2532
// hero stats
26-
int health;
27-
int score;
33+
int health = 10;
34+
int score = 3;
35+
std::set<std::string> inventorySet;
2836
};

project/gui/RPG.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88
#include "MainMenuComponent.h"
99
#include "SpriteComponent.hpp"
1010
#include "sre/SpriteAtlas.hpp"
11+
#define GLM_ENABLE_EXPERIMENTAL
12+
#include "glm/gtx/string_cast.hpp"
1113

1214
using namespace sre;
1315

1416
RPG::RPG()
1517
:currentScene(&mainMenu)
1618
{
1719
r.setWindowSize({600,400});
18-
r.init();
20+
r.init()
21+
.build();
22+
23+
ImGuiIO& io = ImGui::GetIO();
24+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
25+
26+
auto inst = Renderer::instance;
27+
std::cout << "getWindowSize "<<glm::to_string(inst->getWindowSize()) << std::endl;
28+
std::cout << "getDrawableSize "<<glm::to_string(inst->getDrawableSize()) << std::endl;
1929

2030
auto world = sre::Texture::create()
2131
.withFile("assets/Arkanos_0.png")
@@ -75,6 +85,8 @@ void RPG::buildGame(){
7585
}
7686

7787
void RPG::play(){
88+
ImGuiIO& io = ImGui::GetIO();
89+
io.ConfigFlags ^= ImGuiConfigFlags_NavEnableKeyboard; // Disable Keyboard Controls
7890
currentScene = &game;
7991
}
8092

@@ -92,7 +104,7 @@ void RPG::render(){
92104
rp.draw(sb.build());
93105

94106
// render gui
95-
for (auto & go : currentScene ->getSceneObjects()){
107+
for (auto & go : currentScene->getSceneObjects()){
96108
for (auto & comp : go->getComponents()){
97109
comp->onGui();
98110
}

project/gui/assets/Antidote.png

3.28 KB
Loading

project/gui/assets/Axe.png

3.23 KB
Loading

project/gui/assets/BlueMagic.png

1.42 KB
Loading

project/gui/assets/Carrot.png

3.27 KB
Loading

project/gui/assets/Hat.png

3.31 KB
Loading

project/gui/assets/Lemon.png

3.2 KB
Loading

0 commit comments

Comments
 (0)