Elaztek Developer Hub
Blamite Game Engine - blam!  00357.06.18.22.0809.blamite
The core library for the Blamite Game Engine.
console.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../../debug_ui.h"
4 
11 
12 namespace Blam::DebugUI::Windows
13 {
21  {
22  private:
23  std::string input_buffer = "";
24  std::vector<BlamLogMessage> history;
25 
26  bool focused = false;
27  bool needs_scroll = false;
28  bool needs_reclaim_focus = false;
29 
30  void FireCommand()
31  {
33  input_buffer.clear();
34  input_buffer = "";
35  }
36 
37  public:
39  {
42  Blam::Events::RegisterListener(this, "imgui_console");
43  }
44 
46  {
48  }
49 
51  {
52  history.push_back(event->GetLogMessage());
53  needs_scroll = true;
54  }
55 
57  {
58  if (focused)
59  {
60  if (event->GetVirtualKey() == SDLK_RETURN)
61  {
62  FireCommand();
63  needs_reclaim_focus = true;
64  }
65  }
66  }
67 
68  void Draw()
69  {
70  if (show)
71  {
72  if (ImGui::Begin("Console", &show))
73  {
74  float window_width = ImGui::GetWindowContentRegionWidth();
75  float window_height = ImGui::GetWindowHeight() - (70 * *Blam::Globals::GetGlobalAsFloat("ui_scale_factor"));
76 
77  if (ImGui::BeginChildFrame(ImGuiID("log_output"), ImVec2(window_width, window_height),
79  {
80  for (int i = 0; i < history.size(); i++)
81  {
82  BlamColor color = history.at(i).color;
83  ImVec4 message_color = ImVec4(color.GetRedAsFloat(), color.GetGreenAsFloat(), color.GetBlueAsFloat(), color.GetAlphaAsFloat());
84 
85  ImGui::PushID(std::string("console_item_" + std::to_string(i)).c_str());
86  ImGui::TextColored(message_color, history.at(i).message.c_str());
87  ImGui::PopID();
88  }
89 
90  if (needs_scroll)
91  {
93  needs_scroll = false;
94  }
95  }
97 
99 
100  ImGui::Text(ENGINE_TEXT("console_prompt_label").c_str());
101 
102  ImGui::SameLine();
103 
105  ImGui::InputText("", &input_buffer);
106 
107  if (needs_reclaim_focus)
108  {
110  needs_reclaim_focus = false;
111  }
112 
113  focused = ImGui::IsItemActive();
114 
115  ImGui::SameLine();
116 
117  if (ImGui::Button("Run Command"))
118  {
119  FireCommand();
120  }
121  }
122  ImGui::End();
123  }
124  }
125  };
126 }
Blam::DebugUI::Windows::ImGuiConsole::Draw
void Draw()
Draws the contents of the group.
Definition: console.hpp:68
Blam::DebugUI::Windows::ImGuiConsole::OnKeyPressEvent
void OnKeyPressEvent(KeyPressEvent *event)
Called when the listener is subscribed to Key Press events, and a new KeyPressEvent is fired.
Definition: console.hpp:56
ImGui::SetScrollHereY
IMGUI_API void SetScrollHereY(float center_y_ratio=0.5f)
Definition: imgui.cpp:7349
ImVec4
Definition: imgui.h:192
ImGui::InputText
IMGUI_API bool InputText(const char *label, char *buf, size_t buf_size, ImGuiInputTextFlags flags=0, ImGuiInputTextCallback callback=NULL, void *user_data=NULL)
Definition: imgui_widgets.cpp:3068
ImGui::SetItemDefaultFocus
IMGUI_API void SetItemDefaultFocus()
Definition: imgui.cpp:6966
Blam::Globals::GetGlobalAsFloat
BLAM float * GetGlobalAsFloat(std::string name)
Retrieves a global's value as a float.
Definition: globals.cpp:407
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:6016
LogMessage.h
engine_text.h
imgui_stdlib.h
ImGui::GetWindowContentRegionWidth
IMGUI_API float GetWindowContentRegionWidth()
Definition: imgui.cpp:6831
console.h
ImGui::SameLine
IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f)
Definition: imgui.cpp:7147
KeyPressEvent
Class representing a virtual key press.
Definition: KeyPressEvent.h:12
ImVec2
Definition: imgui.h:179
Blam::DebugUI::Windows::ImGuiConsole::~ImGuiConsole
~ImGuiConsole()
Definition: console.hpp:45
events.h
SDLK_RETURN
@ SDLK_RETURN
Definition: SdlEmulationLayer.h:385
ImGui::SetKeyboardFocusHere
IMGUI_API void SetKeyboardFocusHere(int offset=0)
Definition: imgui.cpp:6956
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:5397
ImGuiWindowFlags_AlwaysVerticalScrollbar
@ ImGuiWindowFlags_AlwaysVerticalScrollbar
Definition: imgui.h:728
KeyPressEvent::GetVirtualKey
int GetVirtualKey()
Retrieves the virtual key code that was pressed.
Definition: KeyPressEvent.cpp:8
Blam::Resources::Console::RunCommandLine
BLAM HRESULT RunCommandLine(std::string command_line)
Executed the provided string as a console command.
Definition: console.cpp:269
ImGui::Text
IMGUI_API void Text(const char *fmt,...) IM_FMTARGS(1)
Definition: imgui_widgets.cpp:238
ImGui::IsItemActive
IMGUI_API bool IsItemActive()
Definition: imgui.cpp:4578
Blam::DebugUI::ImGUIDrawingGroup::show
bool show
Controls whether or not the group should be shown. May not be used in all groups.
Definition: debug_ui.h:362
Blam::DebugUI::Windows::ImGuiConsole
Class for the ImGUI console implementation.
Definition: console.hpp:20
ImGui::BeginChildFrame
IMGUI_API bool BeginChildFrame(ImGuiID id, const ImVec2 &size, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4804
Blam::DebugUI::ImGUIDrawingGroup
Class representing an ImGUI drawing group/draw list item.
Definition: debug_ui.h:359
BlamEventType::KeyPress
@ KeyPress
Indicates the event is a key press event. See KeyPressEvent for details.
ImGuiWindowFlags_HorizontalScrollbar
@ ImGuiWindowFlags_HorizontalScrollbar
Definition: imgui.h:725
ImGui::PopID
IMGUI_API void PopID()
Definition: imgui.cpp:7026
ImGuiID
unsigned int ImGuiID
Definition: imgui.h:130
Blam::DebugUI::Windows::ImGuiConsole::ImGuiConsole
ImGuiConsole()
Definition: console.hpp:38
LogMessageEvent
Class representing a log message event.
Definition: LogMessage.h:12
Blam::Events::RegisterListener
BLAM void RegisterListener(BlamEventListener *listener, std::string name)
Registers a an event listener.
Definition: events.cpp:80
LogMessageEvent::GetLogMessage
BlamLogMessage GetLogMessage()
Retrieves the message that was logged.
Definition: LogMessage.cpp:8
BlamEventListener::Subscribe
void Subscribe(BlamEventType type)
Subscribes to an event type.
Definition: BlamEventListener.cpp:3
Blam::Events::UnregisterListener
BLAM void UnregisterListener(BlamEventListener *listener)
Unregisters a an event listener.
Definition: events.cpp:89
ENGINE_TEXT
#define ENGINE_TEXT(string_id)
Definition: engine_text.h:7
ImGui::GetWindowHeight
IMGUI_API float GetWindowHeight()
Definition: imgui.cpp:6586
ImGui::Separator
IMGUI_API void Separator()
Definition: imgui_widgets.cpp:1284
Blam::DebugUI::Windows
Legacy namespace to contain data for the legacy ImGUI console.
Definition: ui.h:19
BlamEventListener
Class representing an Event Listener.
Definition: events.h:29
ImGui::EndChildFrame
IMGUI_API void EndChildFrame()
Definition: imgui.cpp:4818
KeyPressEvent.h
ImGui::TextColored
IMGUI_API void TextColored(const ImVec4 &col, const char *fmt,...) IM_FMTARGS(2)
Definition: imgui_widgets.cpp:257
BlamEventType::LogMessage
@ LogMessage
Indicates the event is a log message event. See LogMessageEvent for details.
ImGui::PushID
IMGUI_API void PushID(const char *str_id)
Definition: imgui.cpp:6995
Blam::DebugUI::Windows::ImGuiConsole::OnLogMessageEvent
void OnLogMessageEvent(LogMessageEvent *event)
Called when the listener is subscribed to Log Message events, and a new LogMessageEvent is fired.
Definition: console.hpp:50
ImGui::Button
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui_widgets.cpp:644