Elaztek Developer Hub
Blamite Game Engine - blam!  00310.02.05.21.0336.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 
6 #include "components/3rdparty/imgui/misc/imgui_stdlib.h"
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  {
43  }
44 
46  {
47  history.push_back(event->GetLogMessage());
48  needs_scroll = true;
49  }
50 
52  {
53  if (focused)
54  {
55  if (event->GetVirtualKey() == SDLK_RETURN)
56  {
57  FireCommand();
58  needs_reclaim_focus = true;
59  }
60  }
61  }
62 
63  void Draw()
64  {
65  if (show)
66  {
67  if (ImGui::Begin("Console", &show))
68  {
69  float window_width = ImGui::GetWindowContentRegionWidth();
70  float window_height = ImGui::GetWindowHeight() - (70 * *Blam::Globals::GetGlobalAsFloat("ui_scale_factor"));
71 
72  if (ImGui::BeginChildFrame(ImGuiID("log_output"), ImVec2(window_width, window_height),
73  ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_HorizontalScrollbar))
74  {
75  for (int i = 0; i < history.size(); i++)
76  {
77  BlamColor color = history.at(i).color;
78  ImVec4 message_color = ImVec4(color.GetRedAsFloat(), color.GetGreenAsFloat(), color.GetBlueAsFloat(), color.GetAlphaAsFloat());
79 
80  ImGui::PushID(std::string("console_item_" + std::to_string(i)).c_str());
81  ImGui::TextColored(message_color, history.at(i).message.c_str());
82  ImGui::PopID();
83  }
84 
85  if (needs_scroll)
86  {
87  ImGui::SetScrollHereY(1.0f);
88  needs_scroll = false;
89  }
90  }
91  ImGui::EndChildFrame();
92 
93  ImGui::Separator();
94 
95  ImGui::Text(ENGINE_TEXT("console_prompt_label").c_str());
96 
97  ImGui::SameLine();
98 
99  ImGui::SetItemDefaultFocus();
100  ImGui::InputText("", &input_buffer);
101 
102  if (needs_reclaim_focus)
103  {
104  ImGui::SetKeyboardFocusHere(-1);
105  needs_reclaim_focus = false;
106  }
107 
108  focused = ImGui::IsItemActive();
109 
110  ImGui::SameLine();
111 
112  if (ImGui::Button("Run Command"))
113  {
114  FireCommand();
115  }
116  }
117  ImGui::End();
118  }
119  }
120  };
121 }
Blam::Events::RegisterListener
BLAM void RegisterListener(BlamEventListener *listener)
Registers a an event listener.
Definition: events.cpp:80
Blam::DebugUI::Windows::ImGuiConsole::Draw
void Draw()
Draws the contents of the group.
Definition: console.hpp:63
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:51
Blam::Globals::GetGlobalAsFloat
BLAM float * GetGlobalAsFloat(std::string name)
Retrieves a global's value as a float.
Definition: globals.cpp:400
console.h
LogMessage.h
engine_text.h
color
@ color
Definition: render_model.h:13
KeyPressEvent
Class representing a virtual key press.
Definition: KeyPressEvent.h:12
events.h
KeyPressEvent::GetVirtualKey
int GetVirtualKey()
Retrieves the virtual key code that was pressed.
Definition: KeyPressEvent.cpp:8
EventType_LogMessage
@ EventType_LogMessage
Definition: BlamEvent.h:12
Blam::Resources::Console::RunCommandLine
BLAM HRESULT RunCommandLine(std::string command_line)
Executed the provided string as a console command.
Definition: console.cpp:205
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
Blam::DebugUI::ImGUIDrawingGroup
Class representing an ImGUI drawing group/draw list item.
Definition: debug_ui.h:359
Blam::DebugUI::Windows::ImGuiConsole::ImGuiConsole
ImGuiConsole()
Definition: console.hpp:38
LogMessageEvent
Class representing a log message event.
Definition: LogMessage.h:12
EventType_KeyPress
@ EventType_KeyPress
Definition: BlamEvent.h:5
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
ENGINE_TEXT
#define ENGINE_TEXT(string_id)
Definition: engine_text.h:7
Blam::DebugUI::Windows
Legacy namespace to contain data for the legacy ImGUI console.
Definition: debug_ui.h:434
BlamEventListener
Class representing an Event Listener.
Definition: events.h:28
Text
@ Text
Master text object that wraps around both BitmapText and DWText.
Definition: render_stack.h:73
KeyPressEvent.h
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:45