aboutsummaryrefslogtreecommitdiff
path: root/build/android/app/src/main/java
diff options
context:
space:
mode:
authorMuhammad Rifqi Priyo Susanto <muhammadrifqipriyosusanto@gmail.com>2021-06-05 02:22:53 +0700
committerGitHub <noreply@github.com>2021-06-04 21:22:53 +0200
commit40acfc938ce2306dbf6704f8091a89817a73c71b (patch)
treee4c0ef49a9d52fb2a43be709f49e9cecc25dddfa /build/android/app/src/main/java
parent8f085e02a107dd8092393935bfa1bba71d2546d2 (diff)
downloadminetest-40acfc938ce2306dbf6704f8091a89817a73c71b.tar.gz
minetest-40acfc938ce2306dbf6704f8091a89817a73c71b.tar.bz2
minetest-40acfc938ce2306dbf6704f8091a89817a73c71b.zip
Android: Do not submit text after pressing Enter key for multi-line text (#11298)
Regular EditText is used for multi-line text to not close the dialog after pressing back button. New button is added for submitting multi-line text.
Diffstat (limited to 'build/android/app/src/main/java')
-rw-r--r--build/android/app/src/main/java/net/minetest/minetest/GameActivity.java33
1 files changed, 29 insertions, 4 deletions
diff --git a/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java b/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java
index 38a388230..bdf764138 100644
--- a/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java
+++ b/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java
@@ -30,7 +30,9 @@ import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
+import android.widget.Button;
import android.widget.EditText;
+import android.widget.LinearLayout;
import androidx.appcompat.app.AlertDialog;
@@ -85,9 +87,19 @@ public class GameActivity extends NativeActivity {
private void showDialogUI(String hint, String current, int editType) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
- EditText editText = new CustomEditText(this);
- builder.setView(editText);
+ LinearLayout container = new LinearLayout(this);
+ container.setOrientation(LinearLayout.VERTICAL);
+ builder.setView(container);
AlertDialog alertDialog = builder.create();
+ EditText editText;
+ // For multi-line, do not close the dialog after pressing back button
+ if (editType == 1) {
+ editText = new EditText(this);
+ } else {
+ editText = new CustomEditText(this);
+ }
+ container.addView(editText);
+ editText.setMaxLines(8);
editText.requestFocus();
editText.setHint(hint);
editText.setText(current);
@@ -103,8 +115,9 @@ public class GameActivity extends NativeActivity {
else
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setSelection(editText.getText().length());
- editText.setOnKeyListener((view, KeyCode, event) -> {
- if (KeyCode == KeyEvent.KEYCODE_ENTER) {
+ editText.setOnKeyListener((view, keyCode, event) -> {
+ // For multi-line, do not submit the text after pressing Enter key
+ if (keyCode == KeyEvent.KEYCODE_ENTER && editType != 1) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
messageReturnCode = 0;
messageReturnValue = editText.getText().toString();
@@ -113,6 +126,18 @@ public class GameActivity extends NativeActivity {
}
return false;
});
+ // For multi-line, add Done button since Enter key does not submit text
+ if (editType == 1) {
+ Button doneButton = new Button(this);
+ container.addView(doneButton);
+ doneButton.setText(R.string.ime_dialog_done);
+ doneButton.setOnClickListener((view -> {
+ imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
+ messageReturnCode = 0;
+ messageReturnValue = editText.getText().toString();
+ alertDialog.dismiss();
+ }));
+ }
alertDialog.show();
alertDialog.setOnCancelListener(dialog -> {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
0'>300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2010-2013 blue42u, Jonathon Anderson <anderjon@umail.iu.edu>
Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "hud.h"
#include "main.h"
#include "settings.h"
#include "util/numeric.h"
#include "log.h"
#include "gamedef.h"
#include "itemdef.h"
#include "inventory.h"
#include "tile.h"
#include "localplayer.h"
#include "camera.h"

#include <IGUIStaticText.h>


Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr,
		gui::IGUIEnvironment* guienv, gui::IGUIFont *font,
		u32 text_height, IGameDef *gamedef,
		LocalPlayer *player, Inventory *inventory) {
	this->driver      = driver;
	this->smgr        = smgr;
	this->guienv      = guienv;
	this->font        = font;
	this->text_height = text_height;
	this->gamedef     = gamedef;
	this->player      = player;
	this->inventory   = inventory;
	
	screensize       = v2u32(0, 0);
	displaycenter    = v2s32(0, 0);
	hotbar_imagesize = 48;
	
	tsrc = gamedef->getTextureSource();
	
	v3f crosshair_color = g_settings->getV3F("crosshair_color");
	u32 cross_r = rangelim(myround(crosshair_color.X), 0, 255);
	u32 cross_g = rangelim(myround(crosshair_color.Y), 0, 255);
	u32 cross_b = rangelim(myround(crosshair_color.Z), 0, 255);
	u32 cross_a = rangelim(g_settings->getS32("crosshair_alpha"), 0, 255);
	crosshair_argb = video::SColor(cross_a, cross_r, cross_g, cross_b);
	
	v3f selectionbox_color = g_settings->getV3F("selectionbox_color");
	u32 sbox_r = rangelim(myround(selectionbox_color.X), 0, 255);
	u32 sbox_g = rangelim(myround(selectionbox_color.Y), 0, 255);
	u32 sbox_b = rangelim(myround(selectionbox_color.Z), 0, 255);
	selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b);
	
	use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png");

	hotbar_image = "";
	use_hotbar_image = false;
	hotbar_selected_image = "";
	use_hotbar_selected_image = false;
}


//NOTE: selectitem = 0 -> no selected; selectitem 1-based
void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
		InventoryList *mainlist, u16 selectitem, u16 direction)
{
	s32 padding = imgsize / 12;
	s32 height  = imgsize + padding * 2;
	s32 width   = itemcount * (imgsize + padding * 2);
	if (direction == HUD_DIR_TOP_BOTTOM || direction == HUD_DIR_BOTTOM_TOP) {
		width  = imgsize + padding * 2;
		height = itemcount * (imgsize + padding * 2);
	}
	s32 fullimglen = imgsize + padding * 2;

	// Position of upper left corner of bar
	v2s32 pos = upperleftpos;

	// Draw background color
	/*core::rect<s32> barrect(0,0,width,height);
	barrect += pos;
	video::SColor bgcolor(255,128,128,128);
	driver->draw2DRectangle(bgcolor, barrect, NULL);*/

	core::rect<s32> imgrect(0, 0, imgsize, imgsize);
	const video::SColor hbar_color(255, 255, 255, 255);
	const video::SColor hbar_colors[] = {hbar_color, hbar_color, hbar_color, hbar_color};

	if (hotbar_image != player->hotbar_image) {
		hotbar_image = player->hotbar_image;
		if (hotbar_image != "")
			use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image);
		else
			use_hotbar_image = false;
	}

	if (hotbar_selected_image != player->hotbar_selected_image) {
		hotbar_selected_image = player->hotbar_selected_image;
		if (hotbar_selected_image != "")
			use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image);
		else
			use_hotbar_selected_image = false;
	}

	if (use_hotbar_image) {
		core::rect<s32> imgrect2(-padding/2, -padding/2, width+padding/2, height+padding/2);
		core::rect<s32> rect2 = imgrect2 + pos;
		video::ITexture *texture = tsrc->getTexture(hotbar_image);
		core::dimension2di imgsize(texture->getOriginalSize());
		driver->draw2DImage(texture, rect2,
			core::rect<s32>(core::position2d<s32>(0,0), imgsize),
			NULL, hbar_colors, true);
	}

	for (s32 i = 0; i < itemcount; i++)
	{
		const ItemStack &item = mainlist->getItem(i);

		v2s32 steppos;
		switch (direction) {
			case HUD_DIR_RIGHT_LEFT:
				steppos = v2s32(-(padding + i * fullimglen), padding);
				break;
			case HUD_DIR_TOP_BOTTOM:
				steppos = v2s32(padding, padding + i * fullimglen);
				break;
			case HUD_DIR_BOTTOM_TOP:
				steppos = v2s32(padding, -(padding + i * fullimglen));
				break;
			default:
				steppos = v2s32(padding + i * fullimglen, padding);
		}
			
		core::rect<s32> rect = imgrect + pos + steppos;

		if (selectitem == i + 1) {
			if (use_hotbar_selected_image) {
				core::rect<s32> imgrect2(-padding*2, -padding*2, height, height);
				rect = imgrect2 + pos + steppos;
				video::ITexture *texture = tsrc->getTexture(hotbar_selected_image);
				core::dimension2di imgsize(texture->getOriginalSize());
				driver->draw2DImage(texture, rect,
					core::rect<s32>(core::position2d<s32>(0,0), imgsize),
					NULL, hbar_colors, true);
				rect = imgrect + pos + steppos;
			} else {
				rect = imgrect + pos + steppos;
				video::SColor c_outside(255,255,0,0);
				//video::SColor c_outside(255,0,0,0);
				//video::SColor c_inside(255,192,192,192);
				s32 x1 = rect.UpperLeftCorner.X;
				s32 y1 = rect.UpperLeftCorner.Y;
				s32 x2 = rect.LowerRightCorner.X;
				s32 y2 = rect.LowerRightCorner.Y;
				// Black base borders
				driver->draw2DRectangle(c_outside,
					core::rect<s32>(
						v2s32(x1 - padding, y1 - padding),
						v2s32(x2 + padding, y1)
					), NULL);
				driver->draw2DRectangle(c_outside,
					core::rect<s32>(
						v2s32(x1 - padding, y2),
						v2s32(x2 + padding, y2 + padding)
					), NULL);
				driver->draw2DRectangle(c_outside,
					core::rect<s32>(
						v2s32(x1 - padding, y1),
						v2s32(x1, y2)
					), NULL);
				driver->draw2DRectangle(c_outside,
					core::rect<s32>(
						v2s32(x2, y1),
						v2s32(x2 + padding, y2)
					), NULL);
				/*// Light inside borders
				driver->draw2DRectangle(c_inside,
					core::rect<s32>(
						v2s32(x1 - padding/2, y1 - padding/2),
						v2s32(x2 + padding/2, y1)
					), NULL);
				driver->draw2DRectangle(c_inside,
					core::rect<s32>(
						v2s32(x1 - padding/2, y2),
						v2s32(x2 + padding/2, y2 + padding/2)
					), NULL);
				driver->draw2DRectangle(c_inside,
					core::rect<s32>(
						v2s32(x1 - padding/2, y1),
						v2s32(x1, y2)
					), NULL);
				driver->draw2DRectangle(c_inside,
					core::rect<s32>(
						v2s32(x2, y1),
						v2s32(x2 + padding/2, y2)
					), NULL);
				*/
			}
		}

		video::SColor bgcolor2(128, 0, 0, 0);
		if (!use_hotbar_image)
			driver->draw2DRectangle(bgcolor2, rect, NULL);
		drawItemStack(driver, font, item, rect, NULL, gamedef);
	}
}


void Hud::drawLuaElements() {
	for (size_t i = 0; i != player->hud.size(); i++) {
		HudElement *e = player->hud[i];
		if (!e)
			continue;
		
		v2s32 pos(e->pos.X * screensize.X, e->pos.Y * screensize.Y);
		switch (e->type) {
			case HUD_ELEM_IMAGE: {
				video::ITexture *texture = tsrc->getTexture(e->text);
				if (!texture)
					continue;

				const video::SColor color(255, 255, 255, 255);
				const video::SColor colors[] = {color, color, color, color};
				core::dimension2di imgsize(texture->getOriginalSize());
				v2s32 dstsize(imgsize.Width * e->scale.X,
				              imgsize.Height * e->scale.Y);
				if (e->scale.X < 0)
					dstsize.X = screensize.X * (e->scale.X * -0.01);
				if (e->scale.Y < 0)
					dstsize.Y = screensize.Y * (e->scale.Y * -0.01);
				v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
				             (e->align.Y - 1.0) * dstsize.Y / 2);
				core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
				rect += pos + offset + v2s32(e->offset.X, e->offset.Y);
				driver->draw2DImage(texture, rect,
					core::rect<s32>(core::position2d<s32>(0,0), imgsize),
					NULL, colors, true);
				break; }
			case HUD_ELEM_TEXT: {
				video::SColor color(255, (e->number >> 16) & 0xFF,
										 (e->number >> 8)  & 0xFF,
										 (e->number >> 0)  & 0xFF);
				core::rect<s32> size(0, 0, e->scale.X, text_height * e->scale.Y);
				std::wstring text = narrow_to_wide(e->text);
				core::dimension2d<u32> textsize = font->getDimension(text.c_str());
				v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
				             (e->align.Y - 1.0) * (textsize.Height / 2));
				v2s32 offs(e->offset.X, e->offset.Y);
				font->draw(text.c_str(), size + pos + offset + offs, color);
				break; }
			case HUD_ELEM_STATBAR: {
				v2s32 offs(e->offset.X, e->offset.Y);
				drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs);
				break; }
			case HUD_ELEM_INVENTORY: {
				InventoryList *inv = inventory->getList(e->text);
				drawItem(pos, hotbar_imagesize, e->number, inv, e->item, e->dir);
				break; }
			case HUD_ELEM_WAYPOINT: {
				v3f p_pos = player->getPosition() / BS;
				v3f w_pos = e->world_pos * BS;
				float distance = floor(10 * p_pos.getDistanceFrom(e->world_pos)) / 10;
				scene::ICameraSceneNode* camera = smgr->getActiveCamera();
				core::matrix4 trans = camera->getProjectionMatrix();
				trans *= camera->getViewMatrix();
				f32 transformed_pos[4] = { w_pos.X, w_pos.Y, w_pos.Z, 1.0f };
				trans.multiplyWith1x4Matrix(transformed_pos);
				if (transformed_pos[3] < 0)
					break;
				f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
					core::reciprocal(transformed_pos[3]);
				pos.X = screensize.X * (0.5 * transformed_pos[0] * zDiv + 0.5);
				pos.Y = screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5);
				video::SColor color(255, (e->number >> 16) & 0xFF,
										 (e->number >> 8)  & 0xFF,
										 (e->number >> 0)  & 0xFF);
				core::rect<s32> size(0, 0, 200, 2 * text_height);
				std::wstring text = narrow_to_wide(e->name);
				font->draw(text.c_str(), size + pos, color);
				std::ostringstream os;
				os<<distance<<e->text;
				text = narrow_to_wide(os.str());
				pos.Y += text_height;
				font->draw(text.c_str(), size + pos, color);
				break; }
			default:
				infostream << "Hud::drawLuaElements: ignoring drawform " << e->type <<
					" of hud element ID " << i << " due to unrecognized type" << std::endl;
		}
	}
}


void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset) {
	const video::SColor color(255, 255, 255, 255);
	const video::SColor colors[] = {color, color, color, color};
	
	video::ITexture *stat_texture = tsrc->getTexture(texture);
	if (!stat_texture)
		return;
		
	core::dimension2di srcd(stat_texture->getOriginalSize());

	v2s32 p = pos;
	if (corner & HUD_CORNER_LOWER)
		p -= srcd.Height;

	p += offset;

	v2s32 steppos;
	switch (drawdir) {
		case HUD_DIR_RIGHT_LEFT:
			steppos = v2s32(-1, 0);
			break;
		case HUD_DIR_TOP_BOTTOM:
			steppos = v2s32(0, 1);
			break;
		case HUD_DIR_BOTTOM_TOP:
			steppos = v2s32(0, -1);
			break;
		default:
			steppos = v2s32(1, 0);
	}
	steppos.X *= srcd.Width;
	steppos.Y *= srcd.Height;
	
	for (s32 i = 0; i < count / 2; i++)
	{
		core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
		core::rect<s32> dstrect(srcrect);

		dstrect += p;
		driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
		p += steppos;
	}
	
	if (count % 2 == 1)
	{
		core::rect<s32> srcrect(0, 0, srcd.Width / 2, srcd.Height);
		core::rect<s32> dstrect(srcrect);

		dstrect += p;
		driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
	}
}


void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, s32 breath) {
	InventoryList *mainlist = inventory->getList("main");
	if (mainlist == NULL) {
		//silently ignore this we may not be initialized completely
		return;
	}
	
	s32 hotbar_itemcount = player->hud_hotbar_itemcount;
	s32 padding = hotbar_imagesize / 12;
	s32 width = hotbar_itemcount * (hotbar_imagesize + padding * 2);
	v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 3);
	
	if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE)
		drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0);
	if (player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)
		drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
				"heart.png", halfheartcount, v2s32(0, 0));
	if (player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE && breath <= 10)
		drawStatbar(pos - v2s32(-180, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
				"bubble.png", breath*2, v2s32(0, 0));
}


void Hud::drawCrosshair() {
	if (!(player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE))
		return;
		
	if (use_crosshair_image) {
		video::ITexture *crosshair = tsrc->getTexture("crosshair.png");
		v2u32 size  = crosshair->getOriginalSize();
		v2s32 lsize = v2s32(displaycenter.X - (size.X / 2),
							displaycenter.Y - (size.Y / 2));
		driver->draw2DImage(crosshair, lsize,
				core::rect<s32>(0, 0, size.X, size.Y),
				0, crosshair_argb, true);
	} else {
		driver->draw2DLine(displaycenter - v2s32(10, 0),
				displaycenter + v2s32(10, 0), crosshair_argb);
		driver->draw2DLine(displaycenter - v2s32(0, 10),
				displaycenter + v2s32(0, 10), crosshair_argb);
	}
}


void Hud::drawSelectionBoxes(std::vector<aabb3f> &hilightboxes) {
	for (std::vector<aabb3f>::const_iterator
			i = hilightboxes.begin();
			i != hilightboxes.end(); i++) {
		driver->draw3DBox(*i, selectionbox_argb);
	}
}


void Hud::resizeHotbar() {
	if (screensize.Y <= 800)
		hotbar_imagesize = 32;
	else if (screensize.Y <= 1280)
		hotbar_imagesize = 48;
	else
		hotbar_imagesize = 64;
}

void drawItemStack(video::IVideoDriver *driver,
		gui::IGUIFont *font,
		const ItemStack &item,
		const core::rect<s32> &rect,
		const core::rect<s32> *clip,
		IGameDef *gamedef)
{