[Scummvm-cvs-logs] CVS: residual engine.cpp,1.70,1.71 engine.h,1.23,1.24 lua.cpp,1.117,1.118 lua.h,1.7,1.8 textobject.cpp,1.24,1.25 textobject.h,1.13,1.14

Pawel Kolodziejski aquadran at users.sourceforge.net
Tue Apr 5 06:51:27 CEST 2005


Update of /cvsroot/scummvm/residual
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32319/residual

Modified Files:
	engine.cpp engine.h lua.cpp lua.h textobject.cpp textobject.h 
Log Message:
patch from Erich Hoover: Menu support(partialy), Text fixes

Index: engine.cpp
===================================================================
RCS file: /cvsroot/scummvm/residual/engine.cpp,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -d -r1.70 -r1.71
--- engine.cpp	3 Apr 2005 11:33:28 -0000	1.70
+++ engine.cpp	5 Apr 2005 13:50:47 -0000	1.71
@@ -76,6 +76,38 @@
 	printLineDefaults.justify = 2;
 }
 
+void Engine::handleButton(int operation, int key) {
+	lua_beginblock();
+	lua_Object menu = getEventHandler("menuHandler");
+	if (menu != LUA_NOOBJECT && !lua_isnil(menu)) {
+		lua_Object system_table = lua_getglobal("system");
+		lua_pushobject(system_table);
+		lua_pushstring(const_cast<char *>("userPaintHandler"));
+		lua_pushobject(lua_gettable());
+		lua_pushnumber(key);
+		if (operation == SDL_KEYDOWN)
+			lua_pushnil();
+		else
+			lua_pushnumber(1);
+		lua_pushnil();
+		lua_callfunction(menu);
+	}
+	lua_endblock();
+
+	lua_beginblock();
+	lua_Object handler = getEventHandler("buttonHandler");
+	if (handler != LUA_NOOBJECT) {
+		lua_pushnumber(key);
+		if (operation == SDL_KEYDOWN)
+			lua_pushnumber(1);
+		else
+			lua_pushnil();
+		lua_pushnil();
+		lua_callfunction(handler);
+	}
+	lua_endblock();
+}
+
 void Engine::mainLoop() {
 	_movieTime = 0;
 	_frameTime = 0;
@@ -92,17 +124,8 @@
 		// Process events
 		SDL_Event event;
 		while (SDL_PollEvent(&event)) {
-			if (event.type == SDL_KEYDOWN && _controlsEnabled[event.key.keysym.sym]) {
-				lua_beginblock();
-				lua_Object handler = getEventHandler("buttonHandler");
-				if (handler != LUA_NOOBJECT) {
-					lua_pushnumber(event.key.keysym.sym);
-					lua_pushnumber(1);
-					lua_pushnil();
-					lua_callfunction(handler);
-				}
-				lua_endblock();
-			}
+			if (event.type == SDL_KEYDOWN && _controlsEnabled[event.key.keysym.sym])
+				handleButton(SDL_KEYDOWN, event.key.keysym.sym);
 			if (event.type == SDL_KEYUP && _controlsEnabled[event.key.keysym.sym]) {
 				// temporary hack for save/load request until game menu will work
 				if (event.key.keysym.sym == SDLK_F7) {
@@ -113,15 +136,7 @@
 					continue;
 				}
 
-				lua_beginblock();
-				lua_Object handler = getEventHandler("buttonHandler");
-				if (handler != LUA_NOOBJECT) {
-					lua_pushnumber(event.key.keysym.sym);
-					lua_pushnil();
-					lua_pushnil();
-					lua_callfunction(handler);
-				}
-				lua_endblock();
+				handleButton(SDL_KEYUP, event.key.keysym.sym);
 			}
 			if (event.type == SDL_QUIT) {
 				lua_beginblock();

Index: engine.h
===================================================================
RCS file: /cvsroot/scummvm/residual/engine.h,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- engine.h	19 Mar 2005 21:48:23 -0000	1.23
+++ engine.h	5 Apr 2005 13:50:53 -0000	1.24
@@ -102,6 +102,9 @@
 
 	float perSecond(float rate) const { return rate * _frameTime / 1000; }
 
+	void setMenuMode(int mode) { _menuMode = mode; }
+	int getMenuMode() { return _menuMode; }
+
 	void enableControl(int num) { _controlsEnabled[num] = true; }
 	void disableControl(int num) { _controlsEnabled[num] = false; }
 
@@ -122,6 +125,7 @@
 	void setSelectedActor(Actor *a) { _selectedActor = a; }
 	Actor *selectedActor() { return _selectedActor; }
 
+	// Text Object Registration
 	typedef std::list<TextObject *> TextListType;
 	TextListType::const_iterator textsBegin() const {
 		return _textObjects.begin();
@@ -156,8 +160,10 @@
 
 private:
 
+	void handleButton(int operation, int key);
+
 	Scene *_currScene;
-	int _mode;
+	int _mode, _menuMode;
 	int _speechMode;
 
 	unsigned _frameStart, _frameTime, _movieTime;

Index: lua.cpp
===================================================================
RCS file: /cvsroot/scummvm/residual/lua.cpp,v
retrieving revision 1.117
retrieving revision 1.118
diff -u -d -r1.117 -r1.118
--- lua.cpp	5 Apr 2005 04:33:56 -0000	1.117
+++ lua.cpp	5 Apr 2005 13:50:53 -0000	1.118
@@ -39,6 +39,10 @@
 
 extern Imuse *g_imuse;
 
+#define strmatch(src, dst)     (strlen(src) == strlen(dst) && strcmp(src, dst) == 0)
+
+static void stubWarning(char *funcName);
+
 static inline bool isObject(int num) {
 	lua_Object param = lua_getparam(num);
 	if (lua_isuserdata(param) && lua_tag(param) == MKID('STAT'))
@@ -108,6 +112,14 @@
 	return NULL;
 }
 
+static inline Bitmap *check_bitmapobject(int num) {
+	lua_Object param = lua_getparam(num);
+	if (lua_isuserdata(param) && lua_tag(param) == MKID('IMAG'))
+		return static_cast<Bitmap *>(lua_getuserdata(param));
+	luaL_argerror(num, "image object expected");
+	return NULL;
+}
+
 static inline int check_int(int num) {
 	double val = luaL_check_number(num);
 
@@ -290,7 +302,7 @@
 			break;
 
 		key_text = lua_getstring(key);		
-		if (strstr(key_text, "font"))
+		if (strmatch(key_text, "font"))
 			sayLineDefaults.font = check_font(2);
 		else
 			error("Unknown SetSayLineDefaults key %s\n", key_text);
@@ -795,6 +807,8 @@
 static void InputDialog() {
 	int c, i = 0;
 	char buf[512];
+
+	stubWarning("InputDialog");
 	fprintf(stderr, "%s %s: ", luaL_check_string(1), luaL_check_string(2));
 	while (i < 512 && (c = fgetc(stdin)) != EOF && c != '\n')
 		buf[i++] = c;
@@ -841,10 +855,10 @@
 	const char *name = luaL_check_string(2);
 	int i, numSectors = g_engine->currScene()->getSectorCount();
 
-	for (i=0; i<numSectors; i++) {
+	for (i = 0; i < numSectors; i++) {
 		Sector *sector = g_engine->currScene()->getSectorBase(i);
 
-		if (sector->visible() && strstr(sector->name(), name)) {
+		if (sector->visible() && strmatch(sector->name(), name)) {
 			if (sector->isPointInSector(act->pos())) {
 				lua_pushnumber(sector->id());
 				lua_pushstring((char *)sector->name());
@@ -874,7 +888,7 @@
 
 		for (i = 0; i < numSectors; i++) {
 			Sector *sector = g_engine->currScene()->getSectorBase(i);
-			if (strstr(sector->name(), name)) {
+			if (strmatch(sector->name(), name)) {
 				sector->setVisible(visible);
 				return;
 			}
@@ -1144,11 +1158,13 @@
 }
 
 void EnableControl() {
+	stubWarning("EnableControl");
 	int num = check_control(1);
 	g_engine->enableControl(num);
 }
 
 void DisableControl() {
+	stubWarning("DisableControl");
 	int num = check_control(1);
 	g_engine->disableControl(num);
 }
@@ -1181,56 +1197,188 @@
 		if (lua_isnil(key)) 
 			break;
 
+		// printf("debug param: %s %s\n", lua_getstring(key), lua_getstring(lua_getresult(2)));
+
 		key_text = lua_getstring(key);
-		if (strstr(key_text, "x"))
+		if (strmatch(key_text, "x"))
 			textObject->setX(atoi(lua_getstring(lua_getresult(2))));
-		else if (strstr(key_text, "y"))
+		else if (strmatch(key_text, "y"))
 			textObject->setY(atoi(lua_getstring(lua_getresult(2))));
-		else if (strstr(key_text, "width"))
+		else if (strmatch(key_text, "width"))
 			textObject->setWidth(atoi(lua_getstring(lua_getresult(2))));
-		else if (strstr(key_text, "height"))
+		else if (strmatch(key_text, "height"))
 			textObject->setHeight(atoi(lua_getstring(lua_getresult(2))));
-		else if (strstr(key_text, "font"))
+		else if (strmatch(key_text, "font"))
 			textObject->setFont(check_font(2));
-		else if (strstr(key_text, "fgcolor"))
+		else if (strmatch(key_text, "fgcolor"))
 			textObject->setFGColor(check_color(2));
-		else if (strstr(key_text, "center"))
+		else if (strmatch(key_text, "disabled"))
+			textObject->setDisabled(atoi(lua_getstring(lua_getresult(2))) != 0);
+		else if (strmatch(key_text, "center"))
 			textObject->setJustify(1);
-		else if (strstr(key_text, "ljustify"))
+		else if (strmatch(key_text, "ljustify"))
 			textObject->setJustify(2);
-		else if (strstr(key_text, "disabled"))
-			warning("getTextObjectParams: key %s\n not implemented yet", key_text);
+		else if (strmatch(key_text, "rjustify"))
+			textObject->setJustify(3);
 		else
-			error("Unknown getTextObjectParams key %s\n", key_text);
+			error("Unknown getTextObjectParams key '%s'\n", key_text);
 	}
 }
 
-static void MakeTextObject() {
-	int x = 0, y = 0, height = 0, width = 0;
-	bool center = false, ljustify = false;
-	Color *fgColor = NULL;
-	Font *font = NULL;
+/* Clean the buffer of text objects
+ * this is known to be used when changing between menus
+ */
+static void CleanBuffer() {
+	g_engine->killTextObjects();
+}
+ 
+/*
+ *
+ */
+static void mainMenuHandler() {
+	lua_Object menuTable = lua_getparam(1);
+	lua_Object keycode = lua_getparam(2);
+	lua_Object pushcode = lua_getparam(3);
+	lua_Object itemTable;
+	int key, operation;
+	int _menuItem = 1, _menuItems = 1;
+         
+	stubWarning("mainMenuHandler");
+	if (!lua_isnumber(keycode) || !lua_istable(menuTable))
+		return;
+	if (lua_isnil(pushcode))
+		operation = 0;
+	else
+		return;
+         
+	// get the item list
+	itemTable = getTableValue(menuTable, "menu");
+	if (!lua_istable(itemTable))
+		return;
 
-	char *line = lua_getstring(lua_getparam(1));
-	lua_Object tableObj = lua_getparam(2);
+		key = atoi(lua_getstring(keycode));
 
-	TextObject *textObject = new TextObject();
-	textObject->setDefaults(&textObjectDefaults);
+	// get the current item
+	_menuItem = atoi(lua_getstring(getTableValue(itemTable, "cur_item")));
+	_menuItems = atoi(lua_getstring(getTableValue(itemTable, "num_items")));
 
-	if (lua_istable(tableObj))
-		getTextObjectParams(textObject, tableObj);
+	/* if we're running the menu then we need to manually handle
+	 * a lot of the operations necessary to use the menu
+	 */
+	bool menuChanged = false;
+	if (key == SDLK_RETURN) {
+		switch(_menuItem) {
+		case 10:
+			key = SDLK_r;
+			break;
+		default:
+			key = SDLK_RETURN;
+		}
+	}
 
-	textObject->setText(line);
-	textObject->createBitmap();
-	g_engine->registerTextObject(textObject);
+	switch(key) {
+		case SDLK_r:
+		case SDLK_ESCAPE:
+		{
+			lua_Object close = getTableFunction(menuTable, "cancel");
+			lua_Object destroy = getTableFunction(menuTable, "destroy");
 
-	lua_pushusertag(textObject, MKID('TEXT'));
-	lua_pushnumber(textObject->getBitmapWidth());
-	lua_pushnumber(textObject->getBitmapHeight());
+			lua_beginblock();
+			lua_pushobject(menuTable);
+			lua_callfunction(destroy);
+			lua_endblock();
+
+			lua_beginblock();
+			lua_Object is_active = lua_createtable();
+			lua_pushobject(is_active);
+			lua_pushstring("is_active");
+			lua_pushnumber(1);
+			lua_settable();
+			lua_pushobject(is_active);
+			lua_callfunction(close);
+			lua_endblock();
+			break;
+		}
+        case SDLK_RETURN:
+		{
+			lua_Object choose = getTableFunction(menuTable, "choose_item");
+			lua_beginblock();
+			lua_pushnumber(_menuItem);
+			lua_callfunction(choose);
+			lua_endblock();
+			break;
+		}
+		case SDLK_DOWN:
+			menuChanged = true;
+			_menuItem++;
+			break;
+		case SDLK_UP:
+			menuChanged = true;
+			_menuItem--;
+			break;
+		case SDLK_h:
+			menuChanged = true;
+			_menuItem = 1; // help
+			break;
+		case SDLK_o:
+			menuChanged = true;
+			_menuItem = 2; // options
+			break;
+		case SDLK_s:
+			menuChanged = true;
+			_menuItem = 3; // load game
+			break;
+		case SDLK_l:
+			menuChanged = true;
+			_menuItem = 4; // load game
+			break;
+		case SDLK_d:
+			menuChanged = true;
+			_menuItem = 6; // dialog transcripts
+			break;
+	}
+	if (menuChanged) {
+		if (_menuItem < 1)
+			_menuItem = 1;
+		if (_menuItem > _menuItems)
+			_menuItem = _menuItems;
+
+		setTableValue(itemTable, "cur_item", _menuItem);
+	}
 }
+  
+/* Clean the requested menu
+ */
+static void CloseMenu() {
+	stubWarning("CloseMenu");
+	CleanBuffer();
+	lua_Object system_table = lua_getglobal("system");
+	setTableValue(system_table, "menuHandler", (lua_Object) 0);
+}
+  
+/* Check for an existing object by a certain name
+ * this function is used by several functions that look
+ * for text objects to see if they need to be created/modified/destroyed.
+ */
+TextObject *TextObjectExists(char *name) {
+	TextObject *modifyObject = NULL;
 
+	for (Engine::TextListType::const_iterator i = g_engine->textsBegin(); i != g_engine->textsEnd(); i++) {
+		TextObject *textO = *i;
+		if (strlen(name) == strlen(textO->name()) && strcmp(textO->name(), name) == 0) {
+			modifyObject = textO;
+			break;
+		}
+	}
+	return modifyObject;
+}
+  
+/* Destroy a text object since we don't need it anymore
+ * note that the menu creates more objects than it needs,
+ * so it deletes some objects right after creating them
+ */
 static void KillTextObject() {
-	TextObject *textObjectParm;
+	TextObject *textObjectParm, *delText;
 
 	if (lua_isnil(lua_getparam(1))) {
 		error("KillTextObject(NULL)");
@@ -1239,21 +1387,23 @@
 
 	textObjectParm = check_textobject(1);
 
-	for (Engine::TextListType::const_iterator i = g_engine->textsBegin(); i != g_engine->textsEnd(); i++) {
-		TextObject *textO = *i;
-
-		if (strstr(textO->name(), textObjectParm->name())) {
-			g_engine->killTextObject(textO);
-			delete textO;
-			return;
-		}
-	}
+	delText = TextObjectExists((char *) textObjectParm->name());        
+	if (delText != NULL)
+		g_engine->killTextObject(delText);
 }
 
+/* Make changes to a text object based on the parameters passed
+ * in the table in the LUA parameter 2.
+ */
 static void ChangeTextObject() {
-	TextObject *textObject = check_textobject(1);
+	TextObject *modifyObject, *textObject = check_textobject(1);
 	lua_Object tableObj = lua_getparam(2);
-	TextObject *modifyObject = NULL;
+
+	modifyObject = TextObjectExists((char *)textObject->name());
+	if (modifyObject == NULL) {
+		warning("ChangeTextObject(): Cannot find active text object");
+		return;
+	}
 
 	for (Engine::TextListType::const_iterator i = g_engine->textsBegin(); i != g_engine->textsEnd(); i++) {
 		TextObject *textO = *i;
@@ -1266,18 +1416,51 @@
 	if (!modifyObject)
 		error("ChangeTextObject(): Cannot find active text object");
 
-	modifyObject->destroyBitmap();
-
-//	textObject->setDefaults(&textObjectDefaults);
 	if (lua_istable(tableObj))
 		getTextObjectParams(modifyObject, tableObj);
+	else
+		warning("Expecting table parameter!");
 
+	// to modify current bitmap it need recreate it
+	modifyObject->destroyBitmap();
 	modifyObject->createBitmap();
 
 	lua_pushnumber(modifyObject->getBitmapWidth());
 	lua_pushnumber(modifyObject->getBitmapHeight());
 }
 
+/* Make a text object, known to be used by the menu
+ * please note that if the same text is issued we will
+ * add an additional space (TEXT_NULL) until the text
+ * becomes unique
+ * (otherwise we'll get identically named menu objects)
+ */
+static void MakeTextObject() {
+	TextObject *textObject;
+	char *line = lua_getstring(lua_getparam(1));
+	std::string text = line;
+	lua_Object tableObj = lua_getparam(2);
+         
+	textObject = new TextObject();
+	textObject->setDefaults(&textObjectDefaults);
+         
+	if (lua_istable(tableObj))
+		getTextObjectParams(textObject, tableObj);
+         
+	while (TextObjectExists((char *)text.c_str()) != NULL)
+		text += TEXT_NULL;
+
+	//printf("Make: %s\n", (char *)text.c_str());
+
+	textObject->setText((char *)text.c_str());
+	textObject->createBitmap();
+	g_engine->registerTextObject(textObject);
+         
+	lua_pushusertag(textObject, MKID('TEXT'));
+	lua_pushnumber(textObject->getBitmapWidth());
+	lua_pushnumber(textObject->getBitmapHeight());
+}
+
 static void GetTextObjectDimensions() {
 	TextObject *textObjectParam = check_textobject(1);
 	lua_pushnumber(textObjectParam->getBitmapWidth());
@@ -1304,6 +1487,11 @@
 	MakeTextObject();
 }
 
+static void SetOffscreenTextPos() {
+	// this sets where we shouldn't put dialog maybe?
+	stubWarning("SetOffscreenTextPos");
+}
+
 static void SetSpeechMode() {
 	int mode = check_int(1);
 	if ((mode >= 1) && (mode <= 3))
@@ -1459,6 +1647,42 @@
 	}
 }
 
+static void RenderModeUser() {
+	stubWarning("RenderModeUser");
+	// it enable/disable updating display
+	lua_Object param1 = lua_getparam(1);
+	bool mode;
+	if (lua_isnumber(param1)) {
+		mode = check_int(1) != 0;
+	} else if (lua_isnil(param1)) {
+		mode = false;
+	} else {
+		error("RenderModeUser() Unknown type of param");
+	}
+	g_engine->setMenuMode(mode);
+	if (mode)
+		printf("RenderModeUser() Enable\n");
+	else
+		printf("RenderModeUser() Disable\n");
+}
+
+static void Display() {
+	stubWarning("Display");
+	lua_Object system_table = lua_getglobal("system");
+	// Install Menu Close Handler
+	lua_pushobject(system_table);
+	lua_pushstring(const_cast<char *>("userPaintHandler"));
+	lua_pushobject(lua_gettable());
+	lua_pushstring(const_cast<char *>("destroy"));
+	lua_pushcfunction(CloseMenu);
+	lua_settable();
+	// Install Menu Key Handler
+	lua_pushobject(system_table);
+	lua_pushstring(const_cast<char *>("menuHandler"));
+	lua_pushcfunction(mainMenuHandler);
+	lua_settable();
+}
+
 static void EngineDisplay() {
 	// it enable/disable updating display
 	lua_Object param1 = lua_getparam(1);
@@ -1470,6 +1694,7 @@
 	} else {
 		error("EngineDisplay() Unknown type of param");
 	}
+	g_engine->setMenuMode(!mode);
 	if (mode)
 		printf("EngineDisplay() Enable\n");
 	else
@@ -1556,12 +1781,9 @@
 STUB_FUNC(SetActorShadowPlane)
 STUB_FUNC(ActivateActorShadow)
 STUB_FUNC(SetShadowColor)
-STUB_FUNC(Display)
-STUB_FUNC(CleanBuffer)
 STUB_FUNC(DimRegion)
 STUB_FUNC(DimScreen)
 STUB_FUNC(ForceRefresh)
-STUB_FUNC(RenderModeUser)
 STUB_FUNC(SetGamma)
 STUB_FUNC(LightMgrStartup)
 STUB_FUNC(SetLightIntensity)
@@ -1604,7 +1826,6 @@
 STUB_FUNC(GetActorTimeScale)
 STUB_FUNC(SetActorScale)
 STUB_FUNC(SetActorColormap)
-STUB_FUNC(SetOffscreenTextPos)
 STUB_FUNC(SetEmergencyFont)
 STUB_FUNC(GetTranslationMode)
 STUB_FUNC(SetTranslationMode)
@@ -2334,9 +2555,8 @@
 	return result;
 }
 
-lua_Object getEventHandler(const char *name) {
-	lua_Object system_table = lua_getglobal("system");
-	lua_pushobject(system_table);
+lua_Object getTableFunction(lua_Object table, char *name) {
+	lua_pushobject(table);
 	lua_pushstring(const_cast<char *>(name));
 	lua_Object handler = lua_gettable();
 	
@@ -2359,3 +2579,54 @@
 
 	return handler;
 }
+
+lua_Object getTableValue(lua_Object table, char *name) {
+	char *key_text = NULL;
+	lua_Object key;
+
+	if (!lua_istable(table)) {
+		error("getTableValue(): Parameter not a table!\n");
+		return 0;
+	}
+	
+	for (;;) {
+		lua_pushobject(table);
+		if (key_text)
+			lua_pushobject(key);
+		else
+			lua_pushnil();
+
+		lua_call("next");
+		key = lua_getresult(1);
+		if (lua_isnil(key)) 
+			break;
+
+		key_text = lua_getstring(key);
+		if (strmatch(key_text, name))
+			return lua_getresult(2);
+	}
+	
+	return 0;
+}
+
+void setTableValue(lua_Object table, char *name, int newvalue) {
+	lua_pushobject(table);
+	lua_pushstring(name);
+	lua_pushnumber(newvalue);
+	lua_settable();
+}
+
+void setTableValue(lua_Object table, char *name, lua_Object newvalue) {
+	lua_pushobject(table);
+	lua_pushstring(name);
+	if (newvalue == 0)
+		lua_pushnil();
+	else
+		lua_pushobject(newvalue);
+	lua_settable();
+}
+
+lua_Object getEventHandler(const char *name) {
+	lua_Object system_table = lua_getglobal("system");
+	return getTableFunction(system_table, (char *)name);
+}

Index: lua.h
===================================================================
RCS file: /cvsroot/scummvm/residual/lua.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- lua.h	1 Jan 2005 12:27:56 -0000	1.7
+++ lua.h	5 Apr 2005 13:50:53 -0000	1.8
@@ -39,4 +39,14 @@
 // object if appropriate
 lua_Object getEventHandler(const char *name);
 
+// set the value for a table item
+void setTableValue(lua_Object table, char *name, int newvalue);
+void setTableValue(lua_Object table, char *name, lua_Object newvalue);
+
+// get the value of a table item
+lua_Object getTableValue(lua_Object table, char *name);
+
+// get a function stored in a table
+lua_Object getTableFunction(lua_Object table, char *name);
+
 #endif

Index: textobject.cpp
===================================================================
RCS file: /cvsroot/scummvm/residual/textobject.cpp,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- textobject.cpp	5 Apr 2005 04:33:56 -0000	1.24
+++ textobject.cpp	5 Apr 2005 13:50:53 -0000	1.25
@@ -28,15 +28,27 @@
 TextObjectDefaults textObjectDefaults;
 
 TextObject::TextObject() :
-		_created(false), _dim(false), _x(0), _y(0), _width(0), _height(0), _justify(0),
+		_created(false), _x(0), _y(0), _width(0), _height(0), _justify(0),
 		_font(NULL), _textBitmap(NULL), _bitmapWidth(0),
-		_bitmapHeight(0), _textObjectHandle(NULL) {
+		_bitmapHeight(0), _textObjectHandle(NULL), _disabled(0) {
 	memset(_textID, 0, sizeof(_textID));
 	_fgColor._vals[0] = 0;
 	_fgColor._vals[1] = 0;
 	_fgColor._vals[2] = 0;
 }
 
+void TextObject::setText(char *text) {
+	if (strlen(text) < sizeof(_textID))
+		strcpy(_textID, text);
+	else {
+		error("Text ID exceeded maximum length (%d): %s\n", sizeof(_textID), text);
+		// this should be good enough to still be unique
+		// but for debug purposes lets make this crash the program so we know about it
+		strncpy(_textID, text, sizeof(_textID));
+		_textID[sizeof(_textID)] = 0;
+	}
+}
+
 TextObject::~TextObject() {
 	destroyBitmap();
 }
@@ -49,6 +61,7 @@
 	_font = defaults->font;
 	_fgColor = defaults->fgColor;
 	_justify = defaults->justify;
+	_disabled = defaults->disabled;
 }
 
 int TextObject::getTextCharPosition(int pos) {
@@ -66,10 +79,17 @@
 		destroyBitmap();
 
 	std::string msg = parseMsgText(_textID, NULL);
+	char *c = (char *)msg.c_str();
 
 	_bitmapWidth = 0;
 	_bitmapHeight = 0;
 
+	// remove spaces (NULL_TEXT) from the end of the string,
+	// while this helps make the string unique it screws up
+	// text justification
+	for(int i = (int) msg.length() - 1; c[i] == TEXT_NULL; i--)
+		msg.erase(msg.length() - 1, msg.length());
+
 	for (int i = 0; msg[i] != '\0'; ++i) {
 		_bitmapWidth += _font->getCharLogicalWidth(msg[i]) + _font->getCharStartingCol(msg[i]);
 
@@ -108,20 +128,32 @@
 }
 
 void TextObject::destroyBitmap() {
+	_created = false;
 	if (_textObjectHandle) {
 		g_driver->destroyTextBitmap(_textObjectHandle);
 		delete _textObjectHandle;
 		_textObjectHandle = NULL;
 	}
-	_created = false;
 }
 
 void TextObject::draw() {
-	int x = _x;
+	if (!_created)
+		return;
 
-	if (_justify == 1) {
-		x = 320 - (_bitmapWidth / 2);
-	}
+	if (_justify == LJUSTIFY || _justify == NONE)
+		g_driver->drawTextBitmap(_x, _y, _textObjectHandle);
+	else if (_justify == CENTER) {
+		int x = _x - (1 / 2.0) * _bitmapWidth;
+		if (x < 0)
+			x = 0;
 
-	g_driver->drawTextBitmap(x, _y, _textObjectHandle);
+		g_driver->drawTextBitmap(x, _y, _textObjectHandle);
+	} else if (_justify == RJUSTIFY) {
+		int x = _x - _bitmapWidth;
+		if (x < 0)
+			x = 0;
+
+		g_driver->drawTextBitmap(x, _y, _textObjectHandle);
+	} else
+		warning("TextObject::draw: Unknown justification code (%d)!", _justify);
 }

Index: textobject.h
===================================================================
RCS file: /cvsroot/scummvm/residual/textobject.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- textobject.h	5 Apr 2005 04:33:56 -0000	1.13
+++ textobject.h	5 Apr 2005 13:50:54 -0000	1.14
@@ -31,9 +31,12 @@
 	int x, y;
 	int width, height;
 	int justify;
+	bool disabled;
 	Font *font;
 };
 
+#define TEXT_NULL   ' '
+
 extern TextObjectDefaults sayLineDefaults;
 extern TextObjectDefaults printLineDefaults;
 extern TextObjectDefaults textObjectDefaults;
@@ -45,7 +48,7 @@
 	void createBitmap();
 	void destroyBitmap();
 	void setDefaults(TextObjectDefaults *defaults);
-	void setText(char *text) { strcpy(_textID, text); }
+	void setText(char *text);
 	void setX(int x) { _x = x; }
 	void setY(int y) { _y = y; }
 	void setWidth(int width) { _width = width; }
@@ -53,7 +56,7 @@
 	void setFGColor(Color *fgColor) { _fgColor = fgColor; }
 	void setFont(Font *font) { _font = font; }
 	void setJustify(int justify) { _justify = justify; }
-	void setDim() { _dim = true; }
+	void setDisabled(bool disabled) { _disabled = disabled; }
 	int getBitmapWidth() { return _bitmapWidth; }
 	int getBitmapHeight() { return _bitmapHeight; }
 	int getTextCharPosition(int pos);
@@ -64,16 +67,17 @@
 	enum Justify {
 		NONE,
 		CENTER,
-		LJUSTIFY
+		LJUSTIFY,
+		RJUSTIFY
 	};
 
 protected:
 	bool _created;
-	bool _dim;
 	Color _fgColor;
 	int _x, _y;
 	uint _width, _height;
 	int _justify;
+	bool _disabled;
 	Font *_font;
 	char _textID[32];
 	uint8 *_textBitmap;





More information about the Scummvm-git-logs mailing list