London Market 35 · Reflow the market for phones

Market Day London · Lesson 35 of 50 · Intermediate to advanced · About 35 min

A shopkeeper might play with one thumb. Give enough space for large buttons and keep the map on a narrow display.

Watercolour of a dry London market lane lined with bread, produce and flower stalls.
What you’ll make Use touch and buttons to operate one movement rule and lay out controls at different widths.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/34 when the lesson builds on prior work, or copy the checkpoints/35 folder to try this lesson on its own. Read the file names in the steps before editing.

Input. Accept key, button or swipe. → Measure. Read width and movement. → Clamp. Keep Nia on the street. → Layout. Resize tiles and stack buttons.
Input → Measure → Clamp → Layout
Godot checkpoint window for Reflow the market for phones
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson resized, InputEventScreenTouch, position, clampf, absf.

  1. Import the editable checkpoint

    In the Godot 4.7.2 Project Manager, select Import and open the project.godot in this lesson folder. Open main.gd in the FileSystem dock. Press F5 to run the market and F8 to stop it before editing.

    Why it works This checkpoint already has a Control root with its script attached, so you can compare your edits to a working market scene.

  2. Share one move function

    Click Move right, then press the right arrow after clicking blank space. Follow both paths to move and compare the tile count.

    tile = clampi(tile + delta, 0, LIMIT)

    Why it works Each accepted input calls the same clamped update. The game only needs one definition of a legal tile.

  3. Make touch targets taller

    Find the 54 values in _ready and temporarily change them to 65. Run and compare the hit areas.

    back.custom_minimum_size.y = 54

    Why it works custom_minimum_size reserves space for each button, including label padding. This helps when fingertips are less precise than a mouse.

  4. Watch the layout resize

    In Project Settings, search for Stretch Mode. For this experiment, set it to Disabled, then run and drag a window edge below 550 pixels wide. Restore the original mode after testing.

    resized.connect(layout)

    Why it works A resized signal calls layout as the Control changes size. Disabled stretch lets you test a genuinely different logical width.

  5. Recognise a swipe

    Read the first ScreenTouch branch. On a touch device, swipe on the pavement and compare with a short tap.

    down = event.position

    Why it works The first touch stores its origin and index. The matching release must travel far enough horizontally to count.

  6. Ignore short, vertical or cancelled touches

    Read the last if. Try a small finger movement, a vertical swipe and one longer horizontal swipe.

    if not event.canceled and absf(change.x) >= 36.0 and absf(change.x) > absf(change.y):

    Why it works Absolute movement compares distances without confusing left with right. The sign of x is used only after accepting the gesture.

Watch the values change

  1. Press Move right tile rises from 0 to 1.
    What you see. Nia appears on the second tile.
  2. Shrink the logical viewport size.x drops below 550.
    What you see. Buttons stack and tiles narrow.
  3. Swipe right on open pavement The release delta is long and mostly horizontal.
    What you see. Nia moves once.

Predict before you run Does rotating a phone or resizing the window change the value of tile?

Reveal the answer

No. layout reads tile to show the same market location.

Complete checkpoint

Wide and narrow positions for the same controls, keyboard inputs, and a one-step horizontal swipe on a touch device.

main.gd

extends Control

const LIMIT := 6
var tile := 0
var down := Vector2.ZERO
var touch_id := -1
var marker: Label
var status: Label
var title: Label
var back: Button
var forward: Button

func _ready() -> void:
	label("MARKET DAY LONDON", Vector2(44, 36), 15, GOLD)
	title = label("Reflow the market for phones", Vector2(44, 66), 29)
	marker = label("NIA", Vector2.ZERO, 23, GOLD)
	status = label("Tile 0 • use arrows, buttons or a swipe", Vector2(44, 126), 18, MINT)
	status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	back = button("Move left", Vector2.ZERO, move.bind(-1), 155)
	forward = button("Move right", Vector2.ZERO, move.bind(1), 155)
	back.custom_minimum_size.y = 54
	forward.custom_minimum_size.y = 54
	resized.connect(layout)
	layout()

func cell_size() -> float:
	return clampf((size.x - 92.0) / 7.0, 27.0, 103.0)

func layout() -> void:
	var narrow := size.x < 550.0
	title.text = "Touch layout" if narrow else "Reflow the market for phones"
	status.size.x = maxf(180.0, size.x - 88.0)
	back.position = Vector2(44, 361)
	forward.position = Vector2(44, 426) if narrow else Vector2(220, 361)
	marker.position = Vector2(44 + tile * cell_size() + 5, 223)
	queue_redraw()

func move(delta: int) -> void:
	tile = clampi(tile + delta, 0, LIMIT)
	status.text = "Tile %d • the same movement rule handles every input" % tile
	layout()

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_left", false):
		move(-1)
	elif event.is_action_pressed("ui_right", false):
		move(1)
	elif event is InputEventScreenTouch and event.pressed and touch_id == -1:
		down = event.position
		touch_id = event.index
	elif event is InputEventScreenTouch and not event.pressed and event.index == touch_id:
		touch_id = -1
		var change: Vector2 = event.position - down
		if not event.canceled and absf(change.x) >= 36.0 and absf(change.x) > absf(change.y):
			move(1 if change.x > 0.0 else -1)

func _draw() -> void:
	base()
	for index in range(7):
		draw_rect(Rect2(44 + index * cell_size(), 201, cell_size() - 4, 64), Color("#385e74"))

const INK := Color("#173353")
const PAPER := Color("#fff4db")
const MINT := Color("#a8e0c1")
const GOLD := Color("#ffd291")

func heading(caption: String, topic: String) -> void:
	label("MARKET DAY LONDON     /     " + topic, Vector2(44, 36), 15, GOLD)
	label(caption, Vector2(44, 66), 31)

func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:
	var item := Label.new()
	item.text = words
	item.position = at
	item.add_theme_font_size_override("font_size", pixels)
	item.add_theme_color_override("font_color", ink)
	add_child(item)
	return item

func button(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:
	var control := Button.new()
	control.text = words
	control.position = at
	control.custom_minimum_size = Vector2(width, 43)
	control.pressed.connect(action)
	add_child(control)
	return control

func base(colour: Color = Color("#193652")) -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))
	draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)

GDScript line by line

Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.

Read main.gd line by line
  1. extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const LIMIT := 6Creates a constant named LIMIT. The expression uses the whole number 6. Godot infers the type from the first value.
  3. var tile := 0Creates a variable named tile. The expression uses the whole number 0. Godot infers the type from the first value.
  4. var down := Vector2.ZEROCreates a variable named down. The expression reads Vector2.ZERO. Godot infers the type from the first value.
  5. var touch_id := -1The value -1 means the handler has no active touch to track.
  6. var marker: LabelDeclares marker for later use. A colon followed by a type says which kind of value belongs here.
  7. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  8. var title: LabelDeclares title for later use. A colon followed by a type says which kind of value belongs here.
  9. var back: ButtonDeclares back for later use. A colon followed by a type says which kind of value belongs here.
  10. var forward: ButtonDeclares forward for later use. A colon followed by a type says which kind of value belongs here.
  11. func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls.
  12. label("MARKET DAY LONDON", Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses.
  13. title = label("Reflow the market for phones", Vector2(44, 66), 29)Stores the new value in title. The expression calls label with the values in its parentheses.
  14. marker = label("NIA", Vector2.ZERO, 23, GOLD)Stores the new value in marker. The expression calls label with the values in its parentheses.
  15. status = label("Tile 0 • use arrows, buttons or a swipe", Vector2(44, 126), 18, MINT)Stores the new value in status. The expression calls label with the values in its parentheses.
  16. status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTWord wrapping keeps the status within its assigned width on a narrow device.
  17. back = button("Move left", Vector2.ZERO, move.bind(-1), 155)Stores the new value in back. The expression calls button with the values in its parentheses.
  18. forward = button("Move right", Vector2.ZERO, move.bind(1), 155)Stores the new value in forward. The expression calls button with the values in its parentheses.
  19. back.custom_minimum_size.y = 54Increase the button’s minimum height to give a finger a comfortable target.
  20. forward.custom_minimum_size.y = 54Stores the new value in forward.custom_minimum_size.y. The expression uses the whole number 54.
  21. resized.connect(layout)When the root Control changes size, the resized signal runs layout() again.
  22. layout()Calls layout with the values in its parentheses.
  23. func cell_size() -> float:Defines cell size, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  24. return clampf((size.x - 92.0) / 7.0, 27.0, 103.0)Reserve 92 pixels for edges and divide the available area by seven. Clamp the width of a tile between 27 and 103 pixels.
  25. func layout() -> void:Defines layout, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  26. var narrow := size.x < 550.0Below 550 pixels, the course uses a shorter title and stacks the two movement buttons.
  27. title.text = "Touch layout" if narrow else "Reflow the market for phones"Updates title.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  28. status.size.x = maxf(180.0, size.x - 88.0)Use the window width to reserve room for the status text. The minimum protects the label on very small sizes.
  29. back.position = Vector2(44, 361)Updates back.position, which controls the node's local x and y location. The expression packs x and y into one value.
  30. forward.position = Vector2(44, 426) if narrow else Vector2(220, 361)On a narrow screen the forward button goes below the back button. On a wide screen it goes beside it.
  31. marker.position = Vector2(44 + tile * cell_size() + 5, 223)Updates marker.position, which controls the node's local x and y location. The expression packs x and y into one value.
  32. queue_redraw()Asks Godot to call _draw again on a later frame so the screen matches the new state.
  33. func move(delta: int) -> void:Defines move, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  34. tile = clampi(tile + delta, 0, LIMIT)All movement controls end here. Clamping protects the tile limits regardless of the source of the input.
  35. status.text = "Tile %d • the same movement rule handles every input" % tileUpdates status.text, which controls the words a player sees. The expression uses the text inside the quotes.
  36. layout()Calls layout with the values in its parentheses.
  37. func _unhandled_input(event: InputEvent) -> void:Godot calls this method for events left unhandled by the interface. Buttons can consume their own clicks first.
  38. if event.is_action_pressed("ui_left", false):Checks event.is_action_pressed("ui_left", false). The indented instructions below run only if this test passes.
  39. move(-1)Calls move with the values in its parentheses.
  40. elif event.is_action_pressed("ui_right", false):If the earlier branches did not run, checks event.is_action_pressed("ui_right", false) next.
  41. move(1)Calls move with the values in its parentheses.
  42. elif event is InputEventScreenTouch and event.pressed and touch_id == -1:Record the first finger to touch down when no swipe is already being followed.
  43. down = event.positionStores the new value in down. The expression reads event.position.
  44. touch_id = event.indexStores the new value in touch_id. The expression reads event.index.
  45. elif event is InputEventScreenTouch and not event.pressed and event.index == touch_id:The release must belong to the same finger that started the swipe.
  46. touch_id = -1Stores the new value in touch_id. The expression reads -1.
  47. var change: Vector2 = event.position - downSubtract the touch-down point from the release point to measure movement along each axis.
  48. if not event.canceled and absf(change.x) >= 36.0 and absf(change.x) > absf(change.y):A gesture needs at least 36 horizontal pixels and more horizontal than vertical travel. Canceled gestures are ignored.
  49. move(1 if change.x > 0.0 else -1)A positive x change moves one tile right. A negative change moves one tile left.
  50. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  51. base()Calls base with the values in its parentheses.
  52. for index in range(7):Repeats the indented instructions for each value in range(7).
  53. draw_rect(Rect2(44 + index * cell_size(), 201, cell_size() - 4, 64), Color("#385e74"))Paints a rectangle at the given position and size.
  54. const INK := Color("#173353")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value.
  55. const PAPER := Color("#fff4db")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value.
  56. const MINT := Color("#a8e0c1")Creates a constant named MINT. The expression builds a colour from these values. Godot infers the type from the first value.
  57. const GOLD := Color("#ffd291")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value.
  58. func heading(caption: String, topic: String) -> void:Defines heading, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  59. label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses.
  60. label(caption, Vector2(44, 66), 31)Calls label with the values in its parentheses.
  61. func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:Defines label, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  62. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  63. item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words.
  64. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  65. item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses.
  66. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  67. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  68. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  69. func button(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:Defines button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  70. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  71. control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words.
  72. control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at.
  73. control.custom_minimum_size = Vector2(width, 43)Updates control.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  74. control.pressed.connect(action)Links a signal to the function that should run when the event happens.
  75. add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  76. return controlFinishes the function and hands its result back to the caller. The expression reads control.
  77. func base(colour: Color = Color("#193652")) -> void:Defines base, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  78. draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))Paints a rectangle at the given position and size.
  79. draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)Paints a rectangle at the given position and size.

Find and fix a bug

If narrowing never triggers the compact layout in the editor, check the test Stretch Mode. A fixed logical viewport keeps its width constant while the outer window shrinks.

Try a change

Ages 10–12 can test the two layouts with different text sizes. Ages 13–16 can add a vertical-swipe action and test that a diagonal swipe triggers only one move.