about summary refs log tree commit diff
path: root/src/game
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2009-06-13 08:22:37 +0000
committerMagnus Auvinen <magnus.auvinen@gmail.com>2009-06-13 08:22:37 +0000
commit69511102de9b0f3ec6ad555baf2a01d9ee1c3dfd (patch)
treeebef66e5ab0cfd16e96026d8ac8d5a708eb68219 /src/game
parent9254ca61f1e459cbaf137ce4511332365da9c225 (diff)
downloadzcatch-69511102de9b0f3ec6ad555baf2a01d9ee1c3dfd.tar.gz
zcatch-69511102de9b0f3ec6ad555baf2a01d9ee1c3dfd.zip
improved the font system even futher. utf8 is now used everywhere. it uses less memory as well
Diffstat (limited to 'src/game')
-rw-r--r--src/game/client/components/menus.cpp46
-rw-r--r--src/game/client/components/menus.hpp2
-rw-r--r--src/game/client/lineinput.cpp49
-rw-r--r--src/game/client/lineinput.hpp6
-rw-r--r--src/game/editor/ed_editor.cpp40
5 files changed, 44 insertions, 99 deletions
diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp
index 050c7056..0fd024c3 100644
--- a/src/game/client/components/menus.cpp
+++ b/src/game/client/components/menus.cpp
@@ -18,6 +18,7 @@
 
 #include <game/generated/gc_data.hpp>
 #include <game/client/gameclient.hpp>
+#include <game/client/lineinput.hpp>
 #include <mastersrv/mastersrv.h>
 
 vec4 MENUS::gui_color;
@@ -199,7 +200,7 @@ void MENUS::ui_draw_checkbox_number(const void *id, const char *text, int checke
 	ui_draw_checkbox_common(id, text, buf, r);
 }
 
-int MENUS::ui_do_edit_box(void *id, const RECT *rect, char *str, int str_size, float font_size, bool hidden)
+int MENUS::ui_do_edit_box(void *id, const RECT *rect, char *str, unsigned str_size, float font_size, bool hidden)
 {
     int inside = ui_mouse_inside(rect);
 	int r = 0;
@@ -229,48 +230,7 @@ int MENUS::ui_do_edit_box(void *id, const RECT *rect, char *str, int str_size, f
 		for(int i = 0; i < num_inputevents; i++)
 		{
 			len = strlen(str);
-			INPUT_EVENT e = inputevents[i];
-			char c = e.ch;
-			int k = e.key;
-
-			if (at_index > len)
-				at_index = len;
-			
-			if (!(c >= 0 && c < 32) && c != 127)
-			{
-				if (len < str_size - 1 && at_index < str_size - 1)
-				{
-					memmove(str + at_index + 1, str + at_index, len - at_index + 1);
-					str[at_index] = c;
-					at_index++;
-					r = 1;
-				}
-			}
-			
-			if(e.flags&INPFLAG_PRESS)
-			{
-				if (k == KEY_BACKSPACE && at_index > 0)
-				{
-					memmove(str + at_index - 1, str + at_index, len - at_index + 1);
-					at_index--;
-					r = 1;
-				}
-				else if (k == KEY_DELETE && at_index < len)
-				{
-					memmove(str + at_index, str + at_index + 1, len - at_index);
-					r = 1;
-				}
-				else if (k == KEY_RETURN)
-					ui_clear_last_active_item();
-				else if (k == KEY_LEFT && at_index > 0)
-					at_index--;
-				else if (k == KEY_RIGHT && at_index < len)
-					at_index++;
-				else if (k == KEY_HOME)
-					at_index = 0;
-				else if (k == KEY_END)
-					at_index = len;
-			}
+			LINEINPUT::manipulate(inputevents[i], str, str_size, &len, &at_index);
 		}
 	}
 
diff --git a/src/game/client/components/menus.hpp b/src/game/client/components/menus.hpp
index 8a50c122..27423490 100644
--- a/src/game/client/components/menus.hpp
+++ b/src/game/client/components/menus.hpp
@@ -40,7 +40,7 @@ class MENUS : public COMPONENT
 	static void ui_draw_checkbox_common(const void *id, const char *text, const char *boxtext, const RECT *r);
 	static void ui_draw_checkbox(const void *id, const char *text, int checked, const RECT *r, const void *extra);
 	static void ui_draw_checkbox_number(const void *id, const char *text, int checked, const RECT *r, const void *extra);
-	static int ui_do_edit_box(void *id, const RECT *rect, char *str, int str_size, float font_size, bool hidden=false);
+	static int ui_do_edit_box(void *id, const RECT *rect, char *str, unsigned str_size, float font_size, bool hidden=false);
 
 	static float ui_do_scrollbar_v(const void *id, const RECT *rect, float current);
 	static float ui_do_scrollbar_h(const void *id, const RECT *rect, float current);
diff --git a/src/game/client/lineinput.cpp b/src/game/client/lineinput.cpp
index 40c47d41..f8c9d7e7 100644
--- a/src/game/client/lineinput.cpp
+++ b/src/game/client/lineinput.cpp
@@ -21,23 +21,30 @@ void LINEINPUT::set(const char *string)
 	cursor_pos = len;
 }
 
-void LINEINPUT::process_input(INPUT_EVENT e)
+void LINEINPUT::manipulate(INPUT_EVENT e, char *str, int str_max_size, int *str_len_ptr, int *cursor_pos_ptr)
 {
+	int cursor_pos = *cursor_pos_ptr;
+	int len = *str_len_ptr;
+	
 	if(cursor_pos > len)
 		cursor_pos = len;
 	
-	char c = e.ch;
+	int code = e.unicode;
 	int k = e.key;
 	
 	// 127 is produced on Mac OS X and corresponds to the delete key
-	if (!(c >= 0 && c < 32) && c != 127)
+	if (!(code >= 0 && code < 32) && code != 127)
 	{
-		if (len < sizeof(str) - 1 && cursor_pos < sizeof(str) - 1)
+		char tmp[8];
+		int charsize = str_utf8_encode(tmp, code);
+		
+		if (len < str_max_size - charsize && cursor_pos < str_max_size - charsize)
 		{
-			memmove(str + cursor_pos + 1, str + cursor_pos, len - cursor_pos + 1);
-			str[cursor_pos] = c;
-			cursor_pos++;
-			len++;
+			memmove(str + cursor_pos + charsize, str + cursor_pos, len - cursor_pos + charsize);
+			for(int i = 0; i < charsize; i++)
+				str[cursor_pos+i] = tmp[i];
+			cursor_pos += charsize;
+			len += charsize;
 		}
 	}
 	
@@ -45,22 +52,34 @@ void LINEINPUT::process_input(INPUT_EVENT e)
 	{
 		if (k == KEY_BACKSPACE && cursor_pos > 0)
 		{
-			memmove(str + cursor_pos - 1, str + cursor_pos, len - cursor_pos + 1);
-			cursor_pos--;
-			len--;
+			int new_cursor_pos = str_utf8_rewind(str, cursor_pos);
+			int charsize = cursor_pos-new_cursor_pos;
+			memmove(str+new_cursor_pos, str+cursor_pos, len - charsize + 1); // +1 == null term
+			cursor_pos = new_cursor_pos;
+			len -= charsize;
 		}
 		else if (k == KEY_DELETE && cursor_pos < len)
 		{
-			memmove(str + cursor_pos, str + cursor_pos + 1, len - cursor_pos);
-			len--;
+			int p = str_utf8_forward(str, cursor_pos);
+			int charsize = p-cursor_pos;
+			memmove(str + cursor_pos, str + cursor_pos + charsize, len - cursor_pos - charsize + 1); // +1 == null term
+			len -= charsize;
 		}
 		else if (k == KEY_LEFT && cursor_pos > 0)
-			cursor_pos--;
+			cursor_pos = str_utf8_rewind(str, cursor_pos);
 		else if (k == KEY_RIGHT && cursor_pos < len)
-			cursor_pos++;
+			cursor_pos = str_utf8_forward(str, cursor_pos);
 		else if (k == KEY_HOME)
 			cursor_pos = 0;
 		else if (k == KEY_END)
 			cursor_pos = len;
 	}
+	
+	*cursor_pos_ptr = cursor_pos;
+	*str_len_ptr = len;
+}
+
+void LINEINPUT::process_input(INPUT_EVENT e)
+{
+	manipulate(e, str, sizeof(str), &len, &cursor_pos);
 }
diff --git a/src/game/client/lineinput.hpp b/src/game/client/lineinput.hpp
index 80ef3c52..75b2bd1d 100644
--- a/src/game/client/lineinput.hpp
+++ b/src/game/client/lineinput.hpp
@@ -5,9 +5,11 @@
 class LINEINPUT
 {
 	char str[256];
-	unsigned len;
-	unsigned cursor_pos;
+	int len;
+	int cursor_pos;
 public:
+	static void manipulate(INPUT_EVENT e, char *str, int str_max_size, int *str_len, int *cursor_pos);
+
 	class CALLBACK
 	{
 	public:
diff --git a/src/game/editor/ed_editor.cpp b/src/game/editor/ed_editor.cpp
index 7a8a1ae7..d56a0506 100644
--- a/src/game/editor/ed_editor.cpp
+++ b/src/game/editor/ed_editor.cpp
@@ -18,6 +18,7 @@ extern "C" {
 #include <game/client/render.hpp>
 
 #include "ed_editor.hpp"
+#include <game/client/lineinput.hpp>
 
 static int checker_texture = 0;
 static int background_texture = 0;
@@ -209,44 +210,7 @@ int ui_do_edit_box(void *id, const RECT *rect, char *str, int str_size, float fo
 		for(int i = 0; i < inp_num_events(); i++)
 		{
 			len = strlen(str);
-			
-			INPUT_EVENT e = inp_get_event(i);
-			char c = e.ch;
-			int k = e.key;
-
-			if (at_index > len)
-				at_index = len;
-			
-			if (!(c >= 0 && c < 32) && c != 127)
-			{
-				if (len < str_size - 1 && at_index < str_size - 1)
-				{
-					memmove(str + at_index + 1, str + at_index, len - at_index + 1);
-					str[at_index] = c;
-					at_index++;
-				}
-			}
-			
-			if(e.flags&INPFLAG_PRESS)
-			{
-				if (k == KEY_BACKSPACE && at_index > 0)
-				{
-					memmove(str + at_index - 1, str + at_index, len - at_index + 1);
-					at_index--;
-				}
-				else if (k == KEY_DELETE && at_index < len)
-					memmove(str + at_index, str + at_index + 1, len - at_index);
-				else if (k == KEY_RETURN)
-					ui_clear_last_active_item();
-				else if (k == KEY_LEFT && at_index > 0)
-					at_index--;
-				else if (k == KEY_RIGHT && at_index < len)
-					at_index++;
-				else if (k == KEY_HOME)
-					at_index = 0;
-				else if (k == KEY_END)
-					at_index = len;
-			}
+			LINEINPUT::manipulate(inp_get_event(i), str, str_size, &len, &at_index);
 		}
 		
 		r = 1;