Browse Source

More work

Panagiotis Christopoulos Charitos 6 years ago
parent
commit
e392e4965b

+ 94 - 82
samples/common/Framework.cpp

@@ -61,111 +61,123 @@ Error SampleApp::userMainLoop(Bool& quit)
 		setDisplayDeveloperConsole(!getDisplayDeveloperConsole());
 	}
 
-	// move the camera
-	static MoveComponent* mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
-
-	if(in.getKey(KeyCode::_1) == 1)
+	if(!getDisplayDeveloperConsole())
 	{
-		mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
-	}
+		in.hideCursor(true);
+		in.lockCursor(true);
 
-	if(in.getKey(KeyCode::F1) == 1)
-	{
-		static U mode = 0;
-		mode = (mode + 1) % 3;
-		if(mode == 0)
+		// move the camera
+		static MoveComponent* mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
+
+		if(in.getKey(KeyCode::_1) == 1)
 		{
-			renderer.getDbg().setEnabled(false);
+			mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
 		}
-		else if(mode == 1)
+
+		if(in.getKey(KeyCode::F1) == 1)
 		{
-			renderer.getDbg().setEnabled(true);
-			renderer.getDbg().setDepthTestEnabled(true);
-			renderer.getDbg().setDitheredDepthTestEnabled(false);
+			static U mode = 0;
+			mode = (mode + 1) % 3;
+			if(mode == 0)
+			{
+				renderer.getDbg().setEnabled(false);
+			}
+			else if(mode == 1)
+			{
+				renderer.getDbg().setEnabled(true);
+				renderer.getDbg().setDepthTestEnabled(true);
+				renderer.getDbg().setDitheredDepthTestEnabled(false);
+			}
+			else
+			{
+				renderer.getDbg().setEnabled(true);
+				renderer.getDbg().setDepthTestEnabled(false);
+				renderer.getDbg().setDitheredDepthTestEnabled(true);
+			}
 		}
-		else
+		if(in.getKey(KeyCode::F2) == 1)
 		{
-			renderer.getDbg().setEnabled(true);
-			renderer.getDbg().setDepthTestEnabled(false);
-			renderer.getDbg().setDitheredDepthTestEnabled(true);
+			// renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
 		}
-	}
-	if(in.getKey(KeyCode::F2) == 1)
-	{
-		// renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
-	}
 
-	if(in.getKey(KeyCode::UP))
-	{
-		mover->rotateLocalX(ROTATE_ANGLE);
-	}
+		if(in.getKey(KeyCode::UP))
+		{
+			mover->rotateLocalX(ROTATE_ANGLE);
+		}
 
-	if(in.getKey(KeyCode::DOWN))
-	{
-		mover->rotateLocalX(-ROTATE_ANGLE);
-	}
+		if(in.getKey(KeyCode::DOWN))
+		{
+			mover->rotateLocalX(-ROTATE_ANGLE);
+		}
 
-	if(in.getKey(KeyCode::LEFT))
-	{
-		mover->rotateLocalY(ROTATE_ANGLE);
-	}
+		if(in.getKey(KeyCode::LEFT))
+		{
+			mover->rotateLocalY(ROTATE_ANGLE);
+		}
 
-	if(in.getKey(KeyCode::RIGHT))
-	{
-		mover->rotateLocalY(-ROTATE_ANGLE);
-	}
+		if(in.getKey(KeyCode::RIGHT))
+		{
+			mover->rotateLocalY(-ROTATE_ANGLE);
+		}
 
-	if(in.getKey(KeyCode::A))
-	{
-		mover->moveLocalX(-MOVE_DISTANCE);
-	}
+		if(in.getKey(KeyCode::A))
+		{
+			mover->moveLocalX(-MOVE_DISTANCE);
+		}
 
-	if(in.getKey(KeyCode::D))
-	{
-		mover->moveLocalX(MOVE_DISTANCE);
-	}
+		if(in.getKey(KeyCode::D))
+		{
+			mover->moveLocalX(MOVE_DISTANCE);
+		}
 
-	if(in.getKey(KeyCode::C))
-	{
-		mover->moveLocalY(-MOVE_DISTANCE);
-	}
+		if(in.getKey(KeyCode::C))
+		{
+			mover->moveLocalY(-MOVE_DISTANCE);
+		}
 
-	if(in.getKey(KeyCode::SPACE))
-	{
-		mover->moveLocalY(MOVE_DISTANCE);
-	}
+		if(in.getKey(KeyCode::SPACE))
+		{
+			mover->moveLocalY(MOVE_DISTANCE);
+		}
 
-	if(in.getKey(KeyCode::W))
-	{
-		mover->moveLocalZ(-MOVE_DISTANCE);
-	}
+		if(in.getKey(KeyCode::W))
+		{
+			mover->moveLocalZ(-MOVE_DISTANCE);
+		}
 
-	if(in.getKey(KeyCode::S))
-	{
-		mover->moveLocalZ(MOVE_DISTANCE);
-	}
+		if(in.getKey(KeyCode::S))
+		{
+			mover->moveLocalZ(MOVE_DISTANCE);
+		}
 
-	if(in.getKey(KeyCode::Q))
-	{
-		mover->rotateLocalZ(ROTATE_ANGLE);
-	}
+		if(in.getKey(KeyCode::Q))
+		{
+			mover->rotateLocalZ(ROTATE_ANGLE);
+		}
 
-	if(in.getKey(KeyCode::E))
-	{
-		mover->rotateLocalZ(-ROTATE_ANGLE);
-	}
+		if(in.getKey(KeyCode::E))
+		{
+			mover->rotateLocalZ(-ROTATE_ANGLE);
+		}
 
-	if(in.getKey(KeyCode::F12) == 1)
-	{
-		CoreTracerSingleton::get().m_enabled = !CoreTracerSingleton::get().m_enabled;
-	}
+		if(in.getKey(KeyCode::F12) == 1)
+		{
+			CoreTracerSingleton::get().m_enabled = !CoreTracerSingleton::get().m_enabled;
+		}
 
-	if(in.getMousePosition() != Vec2(0.0))
-	{
-		F32 angY = -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
+		if(in.getMousePosition() != Vec2(0.0))
+		{
+			F32 angY =
+				-ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
 
-		mover->rotateLocalY(angY);
-		mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
+			mover->rotateLocalY(angY);
+			mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
+		}
+	}
+	else
+	{
+		in.hideCursor(false);
+		in.lockCursor(false);
 	}
 
 	return Error::NONE;

+ 12 - 14
src/anki/core/DeveloperConsole.cpp

@@ -52,6 +52,8 @@ void DeveloperConsole::build(CanvasPtr ctx)
 		false,
 		ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText
 
+	ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, Vec2(4.0f, 1.0f)); // Tighten spacing
+
 	for(const LogItem& item : m_logItems)
 	{
 		switch(item.m_type)
@@ -71,9 +73,9 @@ void DeveloperConsole::build(CanvasPtr ctx)
 		}
 
 		static const Array<const char*, static_cast<U>(LoggerMessageType::COUNT)> MSG_TEXT = {{"I", "E", "W", "F"}};
-		ImGui::Text("[%s][%s] %s (%s:%d %s)",
+		ImGui::TextWrapped("[%s][%s] %s (%s:%d %s)",
 			MSG_TEXT[static_cast<U>(item.m_type)],
-			item.m_subsystem,
+			(item.m_subsystem) ? item.m_subsystem : "N/A ",
 			item.m_msg.cstr(),
 			item.m_file,
 			item.m_line,
@@ -81,25 +83,21 @@ void DeveloperConsole::build(CanvasPtr ctx)
 
 		ImGui::PopStyleColor();
 	}
-	ImGui::EndChild();
 	ImGui::SetScrollHereY(1.0f);
+	ImGui::PopStyleVar();
+	ImGui::EndChild();
 
 	// Commands
 	ImGui::Separator();
-	if(ImGui::InputText("",
-		   &m_inputText[0],
-		   m_inputText.getSizeInBytes(),
-		   ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion
-			   | ImGuiInputTextFlags_CallbackHistory,
-		   [](ImGuiInputTextCallbackData* data) -> int {
-			   // TODO
-			   return 0;
-		   },
-		   this))
+	if(ImGui::InputText(
+		   "", &m_inputText[0], m_inputText.getSizeInBytes(), ImGuiInputTextFlags_EnterReturnsTrue, nullptr, nullptr))
 	{
-		printf("lala\n");
+		ANKI_LOGI("Command: %s", &m_inputText[0]);
+		m_inputText[0] = '\0';
 	}
 
+	ImGui::SetKeyboardFocusHere(-1); // Auto focus previous widget
+
 	ImGui::End();
 	ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldWindowColor;
 	ctx->popFont();

+ 1 - 0
src/anki/input/Input.cpp

@@ -16,6 +16,7 @@ void Input::reset()
 	m_mousePosNdc = Vec2(-1.0f);
 	m_mousePosWin = UVec2(0u);
 	zeroMemory(m_events);
+	zeroMemory(m_textInput);
 }
 
 } // end namespace anki

+ 21 - 0
src/anki/input/Input.h

@@ -99,6 +99,24 @@ public:
 		++m_events[static_cast<U>(eventId)];
 	}
 
+	template<typename TFunc>
+	void iteratePressedKeys(TFunc func) const
+	{
+		for(KeyCode i = KeyCode::FIRST; i < KeyCode::COUNT; ++i)
+		{
+			if(m_keys[i] > 0)
+			{
+				func(i, m_keys[i]);
+			}
+		}
+	}
+
+	/// Get some easy to digest input from the keyboard.
+	CString getTextInput() const
+	{
+		return &m_textInput[0];
+	}
+
 private:
 	InputImpl* m_impl = nullptr;
 	NativeWindow* m_nativeWindow = nullptr;
@@ -121,6 +139,9 @@ private:
 
 	Array<U8, static_cast<U>(InputEvent::COUNT)> m_events;
 
+	/// The keybord input as ascii.
+	Array<char, static_cast<U>(KeyCode::COUNT)> m_textInput;
+
 	Bool m_lockCurs = false;
 
 	/// Initialize the platform's input system

+ 7 - 0
src/anki/input/InputSdl.cpp

@@ -301,6 +301,8 @@ Error Input::handleEvents()
 {
 	ANKI_ASSERT(m_nativeWindow != nullptr);
 
+	m_textInput[0] = '\0';
+
 	// add the times a key is being pressed
 	for(auto& k : m_keys)
 	{
@@ -350,6 +352,10 @@ Error Input::handleEvents()
 			}
 			break;
 		}
+		case SDL_MOUSEWHEEL:
+			m_mouseBtns[MouseButton::SCROLL_UP] = event.wheel.y > 0;
+			m_mouseBtns[MouseButton::SCROLL_DOWN] = event.wheel.y < 0;
+			break;
 		case SDL_MOUSEMOTION:
 			m_mousePosWin.x() = event.button.x;
 			m_mousePosWin.y() = event.button.y;
@@ -360,6 +366,7 @@ Error Input::handleEvents()
 			addEvent(InputEvent::WINDOW_CLOSED);
 			break;
 		case SDL_TEXTINPUT:
+			std::strncpy(&m_textInput[0], event.text.text, m_textInput.getSize() - 1);
 			break;
 		}
 	} // end while events

+ 8 - 1
src/anki/input/KeyCode.h

@@ -5,6 +5,8 @@
 
 #pragma once
 
+#include <anki/util/Enum.h>
+
 namespace anki
 {
 
@@ -261,16 +263,21 @@ enum class KeyCode
 	EJECT,
 	SLEEP,
 
-	COUNT
+	COUNT,
+	FIRST = 0,
 };
+ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(KeyCode, inline)
 
 enum class MouseButton
 {
 	LEFT,
 	MIDDLE,
 	RIGHT,
+	SCROLL_UP,
+	SCROLL_DOWN,
 
 	COUNT
 };
+ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(MouseButton, inline)
 
 } // end namespace anki

+ 67 - 0
src/anki/ui/Canvas.cpp

@@ -65,6 +65,34 @@ Error Canvas::init(FontPtr font, U32 fontHeight, U32 width, U32 height)
 	ImGui::GetIO().IniFilename = nullptr;
 	ImGui::GetIO().LogFilename = nullptr;
 	ImGui::StyleColorsLight();
+
+#define ANKI_HANDLE(ak, im) ImGui::GetIO().KeyMap[im] = static_cast<int>(ak);
+
+	ANKI_HANDLE(KeyCode::TAB, ImGuiKey_Tab)
+	ANKI_HANDLE(KeyCode::LEFT, ImGuiKey_LeftArrow)
+	ANKI_HANDLE(KeyCode::RIGHT, ImGuiKey_RightArrow)
+	ANKI_HANDLE(KeyCode::UP, ImGuiKey_UpArrow)
+	ANKI_HANDLE(KeyCode::DOWN, ImGuiKey_DownArrow)
+	ANKI_HANDLE(KeyCode::PAGEUP, ImGuiKey_PageUp)
+	ANKI_HANDLE(KeyCode::PAGEDOWN, ImGuiKey_PageDown)
+	ANKI_HANDLE(KeyCode::HOME, ImGuiKey_Home)
+	ANKI_HANDLE(KeyCode::END, ImGuiKey_End)
+	ANKI_HANDLE(KeyCode::INSERT, ImGuiKey_Insert)
+	ANKI_HANDLE(KeyCode::DELETE, ImGuiKey_Delete)
+	ANKI_HANDLE(KeyCode::BACKSPACE, ImGuiKey_Backspace)
+	ANKI_HANDLE(KeyCode::SPACE, ImGuiKey_Space)
+	ANKI_HANDLE(KeyCode::RETURN, ImGuiKey_Enter)
+	// ANKI_HANDLE(KeyCode::RETURN2, ImGuiKey_Enter)
+	ANKI_HANDLE(KeyCode::ESCAPE, ImGuiKey_Escape)
+	ANKI_HANDLE(KeyCode::A, ImGuiKey_A)
+	ANKI_HANDLE(KeyCode::C, ImGuiKey_C)
+	ANKI_HANDLE(KeyCode::V, ImGuiKey_V)
+	ANKI_HANDLE(KeyCode::X, ImGuiKey_X)
+	ANKI_HANDLE(KeyCode::Y, ImGuiKey_Y)
+	ANKI_HANDLE(KeyCode::Z, ImGuiKey_Z)
+
+#undef ANKI_HANDLE
+
 	ImGui::SetCurrentContext(nullptr);
 	unsetImAllocator();
 
@@ -92,6 +120,45 @@ void Canvas::handleInput()
 	io.MouseClicked[0] = in.getMouseButton(MouseButton::LEFT) == 1;
 	io.MouseDown[0] = in.getMouseButton(MouseButton::LEFT) > 0;
 
+	if(in.getMouseButton(MouseButton::SCROLL_UP) == 1)
+	{
+		io.MouseWheel = in.getMouseButton(MouseButton::SCROLL_UP);
+	}
+	else if(in.getMouseButton(MouseButton::SCROLL_DOWN) == 1)
+	{
+		io.MouseWheel = -I32(in.getMouseButton(MouseButton::SCROLL_DOWN));
+	}
+
+// Handle keyboard
+#define ANKI_HANDLE(ak) io.KeysDown[static_cast<int>(ak)] = (in.getKey(ak) == 1);
+
+	ANKI_HANDLE(KeyCode::TAB)
+	ANKI_HANDLE(KeyCode::LEFT)
+	ANKI_HANDLE(KeyCode::RIGHT)
+	ANKI_HANDLE(KeyCode::UP)
+	ANKI_HANDLE(KeyCode::DOWN)
+	ANKI_HANDLE(KeyCode::PAGEUP)
+	ANKI_HANDLE(KeyCode::PAGEDOWN)
+	ANKI_HANDLE(KeyCode::HOME)
+	ANKI_HANDLE(KeyCode::END)
+	ANKI_HANDLE(KeyCode::INSERT)
+	ANKI_HANDLE(KeyCode::DELETE)
+	ANKI_HANDLE(KeyCode::BACKSPACE)
+	ANKI_HANDLE(KeyCode::SPACE)
+	ANKI_HANDLE(KeyCode::RETURN)
+	// ANKI_HANDLE(KeyCode::RETURN2)
+	ANKI_HANDLE(KeyCode::ESCAPE)
+	ANKI_HANDLE(KeyCode::A)
+	ANKI_HANDLE(KeyCode::C)
+	ANKI_HANDLE(KeyCode::V)
+	ANKI_HANDLE(KeyCode::X)
+	ANKI_HANDLE(KeyCode::Y)
+	ANKI_HANDLE(KeyCode::Z)
+
+#undef ANKI_HANDLE
+
+	io.AddInputCharactersUTF8(in.getTextInput().cstr());
+
 	// End
 	ImGui::SetCurrentContext(nullptr);
 	unsetImAllocator();