(svn r1082) Feature: Chat toolbar
authordominik
Tue, 14 Dec 2004 16:53:38 +0000
changeset 649 bc9bdefb76ec
parent 648 21fa4f2fcc76
child 650 d0148e8dd551
(svn r1082) Feature: Chat toolbar
- to chat with all players press Return (or Shift-T) and type in the message
- Press Return again to send the message
gui.h
lang/english.txt
main_gui.c
network_gui.c
saveload.c
texteff.c
window.c
--- a/gui.h	Tue Dec 14 15:24:36 2004 +0000
+++ b/gui.h	Tue Dec 14 16:53:38 2004 +0000
@@ -102,7 +102,7 @@
 
 /* network gui */
 void ShowNetworkGameWindow();
-
+void ShowChatWindow(StringID str, StringID caption, int maxlen, int maxwidth, byte window_class, uint16 window_number);
 
 /* bridge_gui.c */
 void ShowBuildBridgeWindow(uint start, uint end, byte type);
--- a/lang/english.txt	Tue Dec 14 15:24:36 2004 +0000
+++ b/lang/english.txt	Tue Dec 14 16:53:38 2004 +0000
@@ -1353,6 +1353,8 @@
 STR_NETWORK_SERVER_SHUTDOWN					:{WHITE} The server closed the session
 STR_NETWORK_SERVER_REBOOT					:{WHITE} The server is restarting...{}Please wait...
 
+STR_NETWORK_SEND										:{BLACK}Send
+
 ############ end network gui strings
 
 
--- a/main_gui.c	Tue Dec 14 15:24:36 2004 +0000
+++ b/main_gui.c	Tue Dec 14 16:53:38 2004 +0000
@@ -334,11 +334,12 @@
 }
 
 #ifdef ENABLE_NETWORK
+
 void ShowNetworkChatQueryWindow(byte desttype, byte dest)
 {
 	_rename_id = desttype + (dest << 8);
 	_rename_what = 2;
-	ShowQueryString(STR_EMPTY, STR_NETWORK_CHAT_QUERY_CAPTION, 60, 250, 1, 0);
+	ShowChatWindow(STR_EMPTY, STR_NETWORK_CHAT_QUERY_CAPTION, 60, 250, 1, 0);
 }
 
 void ShowNetworkGiveMoneyWindow(byte player)
@@ -2337,8 +2338,10 @@
 			break;
 
 #ifdef ENABLE_NETWORK
+		case WKC_RETURN:
 		case 'T' | WKC_SHIFT:
-			ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
+			if(_networking)
+				ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
 			break;
 #endif /* ENABLE_NETWORK */
 
--- a/network_gui.c	Tue Dec 14 15:24:36 2004 +0000
+++ b/network_gui.c	Tue Dec 14 16:53:38 2004 +0000
@@ -1285,4 +1285,134 @@
 }
 
 
+
+
+#define MAX_QUERYSTR_LEN 64
+
+static void ChatWindowWndProc(Window *w, WindowEvent *e)
+{
+	static bool closed = false;
+	switch(e->event) {
+	case WE_PAINT: {
+
+		DrawWindowWidgets(w);
+
+		DrawEditBox(w, 1);
+	} break;
+
+	case WE_CLICK:
+		switch(e->click.widget) {
+		case 3: DeleteWindow(w); break; // Cancel
+		case 2: // Send
+press_ok:;
+			if (str_eq(WP(w,querystr_d).buf, WP(w,querystr_d).buf + MAX_QUERYSTR_LEN)) {
+				DeleteWindow(w);
+			} else {
+				byte *buf = WP(w,querystr_d).buf;
+				WindowClass wnd_class = WP(w,querystr_d).wnd_class;
+				WindowNumber wnd_num = WP(w,querystr_d).wnd_num;
+				Window *parent;
+
+				// Mask the edit-box as closed, so we don't send out a CANCEL
+				closed = true;
+
+				DeleteWindow(w);
+
+				parent = FindWindowById(wnd_class, wnd_num);
+				if (parent != NULL) {
+					WindowEvent e;
+					e.event = WE_ON_EDIT_TEXT;
+					e.edittext.str = buf;
+					parent->wndproc(parent, &e);
+				}
+			}
+			break;
+		}
+		break;
+
+	case WE_MOUSELOOP: {
+		if (!FindWindowById(WP(w,querystr_d).wnd_class, WP(w,querystr_d).wnd_num)) {
+			DeleteWindow(w);
+			return;
+		}
+		HandleEditBox(w, 1);
+	} break;
+
+	case WE_KEYPRESS: {
+		switch(HandleEditBoxKey(w, 1, e)) {
+		case 1: // Return
+			goto press_ok;
+		case 2: // Escape
+			DeleteWindow(w);
+			break;
+		}
+	} break;
+
+	case WE_CREATE:
+		closed = false;
+		break;
+
+	case WE_DESTROY:
+		// If the window is not closed yet, it means it still needs to send a CANCEL
+		if (!closed) {
+			Window *parent = FindWindowById(WP(w,querystr_d).wnd_class, WP(w,querystr_d).wnd_num);
+			if (parent != NULL) {
+				WindowEvent e;
+				e.event = WE_ON_EDIT_TEXT_CANCEL;
+				parent->wndproc(parent, &e);
+			}
+		}
+		break;
+	}
+}
+
+static const Widget _chat_window_widgets[] = {
+{     WWT_IMGBTN,    14,     0,   639,     0,    13, 0x0,							STR_NULL}, // background
+{     WWT_IMGBTN,    14,     2,   379,     1,    12, 0x0,							STR_NULL}, // text box
+{    WWT_TEXTBTN,    14,   380,   509,     1,    12, STR_NETWORK_SEND,STR_NULL}, // send button
+{    WWT_TEXTBTN,    14,   510,   639,     1,    12, STR_012E_CANCEL,	STR_NULL}, // cancel button
+{   WIDGETS_END},
+};
+
+static const WindowDesc _chat_window_desc = {
+	WDP_CENTER, -26, 640, 14, // x, y, width, height
+	WC_SEND_NETWORK_MSG,0,
+	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
+	_chat_window_widgets,
+	ChatWindowWndProc
+};
+
+static byte _edit_str_buf[MAX_QUERYSTR_LEN*2];
+
+void ShowChatWindow(StringID str, StringID caption, int maxlen, int maxwidth, byte window_class, uint16 window_number)
+{
+	Window *w;
+
+#define _orig_edit_str_buf (_edit_str_buf+MAX_QUERYSTR_LEN)
+
+	DeleteWindowById(WC_SEND_NETWORK_MSG, 0);
+
+	if (str == 0xFFFF) {
+		memcpy(_orig_edit_str_buf, str_buffr, MAX_QUERYSTR_LEN);
+	} else {
+		GetString(_orig_edit_str_buf, str);
+	}
+
+	_orig_edit_str_buf[maxlen] = 0;
+
+	memcpy(_edit_str_buf, _orig_edit_str_buf, MAX_QUERYSTR_LEN);
+
+	w = AllocateWindowDesc(&_chat_window_desc);
+
+	w->click_state = 1 << 1;
+	WP(w,querystr_d).caption = caption;
+	WP(w,querystr_d).wnd_class = window_class;
+	WP(w,querystr_d).wnd_num = window_number;
+	WP(w,querystr_d).caret = 0;
+	WP(w,querystr_d).maxlen = maxlen;
+	WP(w,querystr_d).maxwidth = maxwidth;
+	WP(w,querystr_d).buf = _edit_str_buf;
+}
+
+
 #endif /* ENABLE_NETWORK */
--- a/saveload.c	Tue Dec 14 15:24:36 2004 +0000
+++ b/saveload.c	Tue Dec 14 16:53:38 2004 +0000
@@ -1150,7 +1150,6 @@
 {
 	char buf[200];
 	sprintf(buf, "%s%sexit.sav", _path.autosave_dir, PATHSEP);
-	debug(buf);
 	SaveOrLoad(buf, SL_SAVE);
 }
 
--- a/texteff.c	Tue Dec 14 15:24:36 2004 +0000
+++ b/texteff.c	Tue Dec 14 16:53:38 2004 +0000
@@ -35,7 +35,7 @@
 
 const int _textmessage_box_left = 10; // Pixels from left
 const int _textmessage_box_y = 150;  // Height of box
-const int _textmessage_box_bottom = 20; // Pixels from bottom
+const int _textmessage_box_bottom = 30; // Pixels from bottom
 const int _textmessage_box_max_width = 400; // Max width of box
 
 static byte _textmessage_backup[150*400]; // (y * max_width)
--- a/window.c	Tue Dec 14 15:24:36 2004 +0000
+++ b/window.c	Tue Dec 14 16:53:38 2004 +0000
@@ -554,6 +554,7 @@
 		} else {
 			if (pt.x == WDP_CENTER) pt.x = (_screen.width - desc->width) >> 1;
 			if (pt.y == WDP_CENTER) pt.y = (_screen.height - desc->height) >> 1;
+			else if(pt.y < 0) pt.y = _screen.height + pt.y; // if y is negative, it's from the bottom of the screen
 		}
 	}
 
@@ -1366,6 +1367,9 @@
 		} else if (w->window_class == WC_STATUS_BAR) {
 			top = newh - w->height;
 			left = (neww - w->width) >> 1;
+		} else if (w->window_class == WC_SEND_NETWORK_MSG) {
+			top = (newh - 26); // 26 = height of status bar + height of chat bar
+			left = (neww - w->width) >> 1;
 		} else {
 			left = w->left;
 			if (left + (w->width>>1) >= neww) left = neww - w->width;