Forráskód Böngészése

Added imguiInput().

Dario Manesku 11 éve
szülő
commit
c143dba942
2 módosított fájl, 143 hozzáadás és 6 törlés
  1. 141 5
      examples/common/imgui/imgui.cpp
  2. 2 1
      examples/common/imgui/imgui.h

+ 141 - 5
examples/common/imgui/imgui.cpp

@@ -456,6 +456,11 @@ struct Imgui
 		return m_active == _id;
 	}
 
+	bool isActiveInputField(uint32_t _id) const
+	{
+		return m_inputField == _id;
+	}
+
 	bool isHot(uint32_t _id) const
 	{
 		return m_hot == _id;
@@ -494,10 +499,21 @@ struct Imgui
 		clearInput();
 	}
 
+	void clearActiveInputField()
+	{
+		m_inputField = 0;
+	}
+
 	void setActive(uint32_t _id)
 	{
 		m_active = _id;
 		m_wentActive = true;
+		m_inputField = 0;
+	}
+
+	void setActiveInputField(uint32_t _id)
+	{
+		m_inputField = _id;
 	}
 
 	void setHot(uint32_t _id)
@@ -551,7 +567,41 @@ struct Imgui
 		return res;
 	}
 
-	void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll)
+	bool inputLogic(uint32_t _id, bool _over)
+	{
+		bool res = false;
+
+		if (!anyActive() )
+		{
+			if (_over)
+			{
+				setHot(_id);
+			}
+
+			if (isHot(_id)
+			&& m_leftPressed)
+			{
+				setActiveInputField(_id);
+			}
+		}
+
+		if (isHot(_id) )
+		{
+			m_isHot = true;
+		}
+
+		if (m_leftPressed
+		&&  !m_isHot
+		&&  m_inputField != 0)
+		{
+			clearActiveInputField();
+			res = true;
+		}
+
+		return res;
+	}
+
+	void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, char _inputChar)
 	{
 		bool left = (_button & IMGUI_MBUT_LEFT) != 0;
 
@@ -561,9 +611,16 @@ struct Imgui
 		m_leftReleased = m_left && !left;
 		m_left = left;
 		m_scroll = _scroll;
+
+		if (_inputChar > 0x80)
+		{
+			_inputChar = 0;
+		}
+		m_lastChar = m_char;
+		m_char = _inputChar;
 	}
 
-	void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
+	void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, uint8_t _view)
 	{
 		nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
 
@@ -575,7 +632,7 @@ struct Imgui
 		bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
 		bgfx::setViewTransform(_view, NULL, proj);
 
-		updateInput(_mx, _my, _button, _scroll);
+		updateInput(_mx, _my, _button, _scroll, _inputChar);
 
 		m_hot = m_hotToBe;
 		m_hotToBe = 0;
@@ -917,6 +974,77 @@ struct Imgui
 		return res;
 	}
 
+	void input(const char* _label, char* _str, uint32_t _len, bool _enabled)
+	{
+		m_widgetId++;
+		const uint16_t id = (m_areaId << 8) | m_widgetId;
+		int32_t xx = m_widgetX;
+		int32_t yy = m_widgetY;
+		m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
+
+		const bool drawLabel = (NULL != _label && _label[0] != '\0');
+
+		if (drawLabel)
+		{
+			drawText(xx
+				   , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
+				   , ImguiTextAlign::Left
+				   , _label
+				   , imguiRGBA(255, 255, 255, 200)
+				   );
+		}
+
+		// Handle input.
+		if (isActiveInputField(id) )
+		{
+			const size_t cursor = size_t(strlen(_str));
+
+			if (m_char == 0x08) //backspace
+			{
+				_str[cursor-1] = '\0';
+			}
+			else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
+			{
+				clearActiveInputField();
+			}
+			else if (cursor < _len-1
+				 &&  0 != m_char)
+			{
+				_str[cursor] = m_char;
+				_str[cursor+1] = '\0';
+			}
+		}
+
+		// Draw input area.
+		int32_t height = BUTTON_HEIGHT;
+		int32_t width = m_widgetW - 2;
+		if (drawLabel)
+		{
+			uint32_t numVertices = 0; //unused
+			const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices));
+			xx    += (labelWidth + 6);
+			width -= (labelWidth + 6);
+		}
+		const bool enabled = _enabled && isEnabled(m_areaId);
+		const bool over = enabled && inRect(xx, yy, width, height);
+		const bool res = inputLogic(id, over);
+
+		drawRoundedRect( (float)xx
+			, (float)yy
+			, (float)width
+			, (float)height
+			, (float)BUTTON_HEIGHT / 5 - 1
+			, isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
+			);
+
+		drawText(xx + 6
+			, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
+			, ImguiTextAlign::Left
+			, _str
+			, isActiveInputField(id)?imguiRGBA(0, 0, 0, 255):imguiRGBA(255,255,255,255)
+			);
+	}
+
 	void image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
 	{
 		int32_t xx;
@@ -1936,6 +2064,9 @@ struct Imgui
 	uint32_t m_active;
 	uint32_t m_hot;
 	uint32_t m_hotToBe;
+	char m_char;
+	char m_lastChar;
+	uint32_t m_inputField;
 	int32_t m_dragX;
 	int32_t m_dragY;
 	float m_dragOrig;
@@ -2025,9 +2156,9 @@ void imguiDestroy()
 	s_imgui.destroy();
 }
 
-void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
+void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, uint8_t _view)
 {
-	s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
+	s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _view);
 }
 
 void imguiEndFrame()
@@ -2111,6 +2242,11 @@ bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax,
 	return result;
 }
 
+void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled)
+{
+	s_imgui.input(_label, _str, _len, _enabled);
+}
+
 uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
 {
 	va_list argList;

+ 2 - 1
examples/common/imgui/imgui.h

@@ -71,7 +71,7 @@ void imguiSetFont(ImguiFontHandle _handle);
 ImguiFontHandle imguiCreate(const void* _data, float _fontSize=15.0f);
 void imguiDestroy();
 
-void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view = 31);
+void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar = 0, uint8_t _view = 31);
 void imguiEndFrame();
 
 bool imguiBeginScrollArea(const char* _name, int _x, int _y, int _width, int _height, int* _scroll, bool _enabled = true);
@@ -90,6 +90,7 @@ void imguiLabel(const char* _format, ...);
 void imguiValue(const char* _text);
 bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled = true);
 bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled = true);
+void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled = true);
 
 uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...);
 #define imguiChoose(...) imguiChooseUseMacroInstead(__VA_ARGS__, NULL)