Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -987,13 +987,19 @@ class ScriptMapTest {
return Offset(left + (x * scale).toFloat(), top + (y * scale).toFloat())
}

/** The engine was last asked to draw the map at the scale a [w] by [h] frame is shown at in [bounds], at most the engine's 8. */
/**
* The engine was last asked to draw the map at the scale a [w] by [h] frame is shown at in [bounds], at most the
* engine's 8, laid out at the screen's density, as the app's own map is.
*/
private fun assertScaleAsked(bounds: Rect, w: Int, h: Int) {
val shown = min(bounds.width / w, bounds.height / h).toDouble()
assertTrue("the map is shown at $shown, not larger than Mudlet's pixels", shown > 1.1)
val asked = session.receiveLast("setMapScale", readPast = true).getDouble("scale")
val request = session.receiveLast("setMapScale", readPast = true)
val asked = request.getDouble("scale")
// Hundredths, down, of a box that rounds to the screen's pixels
assertEquals("the map shown at $shown is asked for at $asked", shown.coerceAtMost(8.0), asked, 0.02)
val density = compose.activity.resources.displayMetrics.density.toDouble().coerceIn(1.0, 4.0)
assertEquals("the map is laid out at the screen's density: $request", density, request.optDouble("density"), 0.001)
}

private fun assertPoint(x: Double, y: Double, event: JSONObject, within: Double = POINTS) {
Expand Down
27 changes: 17 additions & 10 deletions app/src/main/kotlin/org/mudlet/android/ui/ScriptWindowsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi
private var wantedSize: MudletSize? = null
private var sentSize: MudletSize? = null

// The scale the mapper window's map is shown at, and the one the engine draws it at since the UI attached: 1 until
// asked, as the engine goes back to 1 when a UI goes; null when it could not be sent
private var wantedMapScale = 1.0
private var sentMapScale: Double? = 1.0
// The scale and density the mapper window's map is shown at, and those the engine draws it at since the UI attached:
// 1 until asked, as the engine goes back to 1 when a UI goes; null when they could not be sent
private var wantedMapScale = MapScale(1.0, 1.0)
private var sentMapScale: MapScale? = MapScale(1.0, 1.0)

// Where this engine instance was last told each user window is
private val sentPlacements = HashMap<String, Placement>()
Expand Down Expand Up @@ -287,7 +287,7 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi
// Also a size that could not be sent while the link was down
wantedSize?.let(::setMainWindowSize)
mapRooms.onAttached(sameInstance)
sentMapScale = 1.0
sentMapScale = MapScale(1.0, 1.0)
setMapScale(wantedMapScale)
session.appMap.onAttached(sameInstance)
}
Expand All @@ -299,13 +299,20 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi
sentSize = if (session.send(Requests.setMainWindowSize(size.w, size.h))) size else null
}

/** Has the engine draw [map] at [scale] device pixels to each of the mapper window's, unless it does already. */
fun setMapScale(scale: Double) {
wantedMapScale = scale
if (scale == sentMapScale) return
sentMapScale = if (session.send(Requests.setMapScale(scale))) scale else null
/**
* Has the engine draw [map] at [scale] device pixels to each of the mapper window's, laid out as the app's own map
* is at [density] device pixels to the dp, unless it does already.
*/
fun setMapScale(scale: Double, density: Float) = setMapScale(MapScale(scale, density.toDouble().coerceIn(1.0, AppMapClient.MAX_SCALE)))

private fun setMapScale(wanted: MapScale) {
wantedMapScale = wanted
if (wanted == sentMapScale) return
sentMapScale = if (session.send(Requests.setMapScale(wanted.scale, wanted.density))) wanted else null
}

private data class MapScale(val scale: Double, val density: Double)

/** Tells the engine where the user window [name] is, unless it was told already. */
fun placeUserWindow(name: String, placement: Placement) {
if (sentPlacements[name] == placement) return
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/kotlin/org/mudlet/android/ui/game/ScriptMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ internal fun ScriptMap(placed: PlacedWindow, corner: MapCorner?, density: Densit
if (corner != null) {
val mapRect = corner.map.from(corner.bounds)
val fit = remember(window.w, window.h, placed, mapRect) { MapFit(window.w, window.h, placed.wholeIn(PxRect(0f, 0f, mapRect.w, mapRect.h))) }
// Drawn at the window's pixels and scaled up to the screen's, the map would be blurred
LaunchedEffect(fit.renderScale) { EngineClient.front.windows.setMapScale(fit.renderScale) }
// Drawn at the window's pixels and scaled up to the screen's, the map would be blurred, and laid out in them its
// rooms would be tiny ones magnified, not drawn as the app's own map draws them
LaunchedEffect(fit.renderScale, density.density) { EngineClient.front.windows.setMapScale(fit.renderScale, density.density) }
val infoHeight = with(density) { (corner.map.h * INFO_FRACTION).toDp() }
CornerMap(
corner,
Expand All @@ -260,7 +261,7 @@ internal fun ScriptMap(placed: PlacedWindow, corner: MapCorner?, density: Densit
BoxWithConstraints(Modifier.weight(1f).fillMaxWidth()) {
val box = PxRect(0f, 0f, constraints.maxWidth.toFloat(), constraints.maxHeight.toFloat())
val fit = remember(window.w, window.h, placed, box) { MapFit(window.w, window.h, placed.wholeIn(box)) }
LaunchedEffect(fit.renderScale) { EngineClient.front.windows.setMapScale(fit.renderScale) }
LaunchedEffect(fit.renderScale, density.density) { EngineClient.front.windows.setMapScale(fit.renderScale, density.density) }
MapView(MapperWindow, frame, fit, viewOnly, drawn, Modifier.fillMaxSize().background(CONSOLE_BACKGROUND).readAsOne(0f), panel = true)
}
}
Expand Down
7 changes: 7 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ The game screen draws `map` in the mapper window (`ui/game/ScriptMap.kt`, fitted
- The engine is asked (`setMapScale`) to draw it at the scale it is shown at, `MapFit.renderScale`, as Mudlet's pixels
are scaled to the screen's, several times over for a small mapper that fills a page's pane: its PNG has that many
more pixels, so the map is as sharp as the screen, and its `w`, `h` and points stay the window's.
- It is asked with the screen's density too, `setMapScale`'s `density`, so a mapper shown larger than the app's own
map's dp is laid out in dp and drawn scaled down to the window (libmudlet's `layoutScale` of `renderMap()`): its rooms
get the outlines, line weights and borders the app's own map gives a room as large on the screen, where laid out in
the window's pixels they would be tiny ones magnified, with filled dots for the levels above and below and no borders.
- A tap sends a `mapPointer` press with the left button on the finger's lift, so a pinch selects nothing, at the
middle of the room nearest it within 24 dp or its own cell (from the frame's `view.rooms`), so a room smaller than a
finger at a low zoom is still hit, or else at the point itself; a second tap near it within the double-tap timeout
Expand Down Expand Up @@ -1385,6 +1389,9 @@ Android options (`scripts/build-libmudlet-desktop.sh`) to run it. It checks:
`sysMapWindowMousePressEvent`, a double-click walking to a room through `doSpeedWalk()`, a wheel turn zooming and less
than half a notch refused; `setMapViewOnly` raising `mapModeChangeEvent`; `pointer` on the window `mapper`; the map to
a UI that attaches, and none while the mapper hides;
- the map laid out as the app's: a 100 by 100 mapper at `setMapScale` 4 and `density` 1 drawing a room, its outline and
its exit as the app's 400 by 400 map at a scale of 1 does, its view and a press still in the window's pixels, and
laid out in its own pixels with no `density`;
- the app's map, with no script mapper: `setAppMapSize` drawing it as `appMap` at the size and scale asked, an empty
one with no profile or no rooms, and one larger than a PNG may be refused; `appMapPointer` walking with a double-click
and raising no `sysMapWindowMousePressEvent`; `findRooms` by name, ignoring case, and by number; `showRoom` centring
Expand Down
3 changes: 3 additions & 0 deletions docs/divergences.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ panel beside it rather than over it (`docs/architecture.md`, "The map"). The win
the engine draws the map at, stay what the script set. A desktop's mapper is laid out for a screen many times a
phone's: scaled to fit, a 200 pixel mapper is a few dp across, too small to read a room or touch one, and controls
drawn over it hide most of it.
Its map is laid out in the dp it is shown at, as the app's own map is, and drawn scaled down to the window's pixels
(`setMapScale`'s `density`): laid out in those and magnified, its rooms would get the outlines, exits and levels above
and below desktop draws for a room a few pixels across, not those of a room the size the player sees.
The panel shows only once a small button over the map's top corner is pressed, and Back or a touch elsewhere hides it
again. Desktop's mapper shows its panel until the player hides it with the thin button between the two; beside a map
that small on a phone, a panel left showing covers the game's text, so it shows only while the player wants it.
Expand Down
3 changes: 2 additions & 1 deletion docs/phase-f.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ rules and bare declarations apply, are read as a label's ("Labels"). What else i
`map` is an `Image` at the `mapper` window's place. Taps go back as `mapPointer` presses, a double tap speedwalks, and
pinch and zoom buttons send wheel turns. The image is laid out at the window's size in Mudlet's pixels, so the
pointer lands where `renderMap()` drew, and drawn at the scale the app shows it at (`setMapScale`), so it is as sharp
as the screen.
as the screen. Shown larger than the app's own map's dp, it is laid out in those and scaled down to the window, so its
rooms look as on that map.

## Touch and TalkBack

Expand Down
12 changes: 10 additions & 2 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ message, in backquotes, with `?` after an optional one, and the fields of the ob
| `activateCommand` | `command` | A click on the command of `commands` whose `id` is `command`, a whole number (`THeadlessSession::activateAddonCommand()`): a checkable command is ticked or unticked, then the event `sysCommandClicked` is raised with its id. Refused for a disabled command. |
| `mapPointer` | `type`, `x`, `y`, `button`?, `modifiers`?, `wheelY`? | What the UI's pointer did on the map at `x`, `y`, in the pixels of the last `map` from its top left (`THeadlessSession::mapPointer()`, "The map" below). `type` is `press`, `doubleClick` or `wheel`; `button`, `modifiers` and `wheelY` are as for `pointer`, and 0 when absent. |
| `setMapViewOnly` | `viewOnly` | `true` to only view the map, `false` to edit it, as the GUI's mapper menu does (`THeadlessSession::setMapViewOnly()`): the event `mapModeChangeEvent` is raised with `viewing` or `editing`. |
| `setMapScale` | `scale` | Draws `map` from now on with `scale` device pixels to each of the mapper window's, a number from 1 to 8, so that it is as sharp as the UI shows it ("The map" below). libmudlet keeps it until the UI that asked goes, and draws at most 4096 pixels a side, at a smaller scale where that one would be larger. |
| `setMapScale` | `scale`, `density`? | Draws `map` from now on with `scale` device pixels to each of the mapper window's, a number from 1 to 8, so that it is as sharp as the UI shows it ("The map" below). `density` is the device pixels to each of the units the UI lays its own map out in, as `setAppMapSize`'s `scale`, a number from 1 to 4: a `scale` above it lays the map out in those units, so its rooms are drawn as on that map; absent, the map is laid out in the window's pixels, as an engine without it does, so it needs no new version. libmudlet keeps both until the UI that asked goes, and draws at most 4096 pixels a side, at a smaller scale where that one would be larger. |
| `setAppMapSize` | `w`, `h`, `scale` | Asks for the app's own map, drawn at `w` by `h` of the map's pixels with `scale` device pixels to each, and sent as `appMap` whenever it changes ("The app's map" below); `w` and `h` 0 stop it. `w` and `h` are whole numbers, both above 0 or both 0; `scale` a number from 1 to 4. libmudlet keeps it for each profile that loads later, until the UI that asked goes, and refuses a map of more than 4096 device pixels a side. |
| `appMapPointer` | `type`, `x`, `y`, `button`?, `modifiers`?, `wheelY`? | As `mapPointer`, at `x`, `y` in the map's pixels of the last `appMap` (`THeadlessSession::appMapPointer()`). |
| `findRooms` | `text`, `id`? | Looks for the rooms of the map whose name holds `text`, ignoring case, as `searchRoom()` matches, and the room whose id `text` is (`THeadlessSession::findRooms()`), answered by `rooms`, best first. `text` must not be blank. |
Expand Down Expand Up @@ -179,7 +179,8 @@ and the app's map is not open (no `setAppMapSize` above 0 by 0 since the profile
gone since), and `mapPointer` as `THeadlessSession::mapPointer()` does: before the map has been drawn, for a
double-click of another button than the left, and for a wheel turn of less than half a notch or with no area to zoom.

`setMapScale` is answered only when refused, for a `scale` that is not a number from 1 to 8.
`setMapScale` is answered only when refused, for a `scale` that is not a number from 1 to 8, or a `density` that is not a
number from 1 to 4.

`setAppMapSize`, `appMapPointer` and `showRoom` too are answered only when refused, and `findRooms` by `rooms` or
`error`, as above. libmudlet refuses all but `setAppMapSize` while no profile is open, `appMapPointer` before the
Expand Down Expand Up @@ -539,6 +540,13 @@ map is the same point of that window, so the UI draws the PNG over it, and sends
Mudlet's main window's scaled to the screen, and a mapper that is a page fills its pane. A scale that would make
the PNG larger than 4096 pixels a side is lowered to fit. A mapper smaller than 10 pixels a side or larger than
4096, or a map too large for a frame, gives none.
- Mudlet's outlines, line weights and the room size below which a room's border fades and then goes are in pixels, so
a small mapper shown enlarged, as a page of the starter UI's dock is, laid out in its own pixels has tiny rooms
magnified: filled dots for the levels above and below, rooms without borders, thinner exits. With a `density` below
the `scale`, the map is laid out instead at the window's size times `scale` over `density`, in the units the app
lays its own map out in, and drawn scaled down to the window (libmudlet's `layoutScale` of `renderMap()`, a Mudlet
patch the app carries until Mudlet has it), so a room as large on the screen as on `appMap` is drawn as there. The
zoom is still the area's, rooms across the narrow side, and `w`, `h`, `view` and the points still the window's.

### The app's map

Expand Down
6 changes: 4 additions & 2 deletions native/api/IEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ class IEngine
virtual QString setMapViewOnly(bool viewOnly) = 0;
// The mapper window's map drawn from now on at scale device pixels to each of the window's, so that it is as
// sharp as the UI shows it, though never more than PngImage::maxSide a side; 1 again when the UI goes, as
// SocketBridge asks. Scale is from 1 to maxMapScale.
// SocketBridge asks. Scale is from 1 to maxMapScale. Density is the device pixels to each of the units the UI lays
// its own map out in, from 1 to maxAppMapScale: a scale above it lays the map out in those units, so its rooms
// are drawn as on that map; one at or above scale lays it out in the window's pixels.
static constexpr double maxMapScale = 8.0;
virtual QString setMapScale(double scale) = 0;
virtual QString setMapScale(double scale, double density) = 0;
// The app's own map, apart from the mapper window, drawn for IFrontend::appMap() at size, in the map's pixels, with
// scale device pixels to each, from now on and for the next profile too; an empty size stops it, as SocketBridge
// asks when the UI goes. Scale is from 1 to maxAppMapScale.
Expand Down
1 change: 1 addition & 0 deletions native/enginehost/MudletEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MudletEngine : public QObject, public IEngineControl
QSize screen;
QSize mainWindow{0, 0};
double mapScale = 1;
double mapDensity = 1;
QSize appMap{0, 0};
double appMapScale = 1;
};
Expand Down
13 changes: 9 additions & 4 deletions native/enginehost/MudletProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ void MudletProfile::start(const QString& host, quint16 port, bool connect, std::
mScreenSize = ui.screen;
mMainWindowSize = ui.mainWindow;
mMapScale = ui.mapScale;
mMapDensity = ui.mapDensity;
mAppMapSize = ui.appMap;
mAppMapScale = ui.appMapScale;
// The new profile counts its links from 1 again
Expand Down Expand Up @@ -1694,7 +1695,9 @@ std::optional<MapFrame> MudletProfile::drawMap(const QSize& size)
qWarning("MudletEngine: the mapper, %dx%d, is larger than %d pixels a side, so the map is not drawn", size.width(), size.height(), PngImage::maxSide);
return std::nullopt;
}
// Drawn in the window's pixels onto an image of the device's, so the lines and names are as sharp as the screen
// Drawn in the window's pixels onto an image of the device's, so the lines and names are as sharp as the screen,
// and laid out in the UI's own map's units where it shows the window larger than those, so the rooms look as there;
// by the scale the image is drawn at, so the layout, which a double-click paints, is never larger than the image
const double scale = std::max(1.0, std::min({mMapScale, double(PngImage::maxSide) / size.width(), double(PngImage::maxSide) / size.height()}));
QImage image(size * scale, QImage::Format_ARGB32_Premultiplied);
if (image.isNull()) {
Expand All @@ -1704,7 +1707,7 @@ std::optional<MapFrame> MudletProfile::drawMap(const QSize& size)
image.setDevicePixelRatio(scale);
image.fill(Qt::transparent);
QPainter painter(&image);
const bool drawn = mSession->renderMap(painter, size);
const bool drawn = mSession->renderMap(painter, size, std::max(1.0, scale / mMapDensity));
painter.end();
if (!drawn) {
qWarning("MudletEngine: the map was not drawn: %s", qPrintable(mSession->errorString()));
Expand Down Expand Up @@ -2026,13 +2029,15 @@ QString MudletProfile::setMapViewOnly(bool viewOnly)
return mSession->setMapViewOnly(viewOnly) ? QString() : mSession->errorString();
}

QString MudletProfile::setMapScale(double scale)
QString MudletProfile::setMapScale(double scale, double density)
{
mEngine.mUi.mapScale = scale;
if (scale == mMapScale) {
mEngine.mUi.mapDensity = density;
if (scale == mMapScale && density == mMapDensity) {
return {};
}
mMapScale = scale;
mMapDensity = density;
if (mMapSize && loaded()) {
pushMap(mMapSize);
}
Expand Down
5 changes: 3 additions & 2 deletions native/enginehost/MudletProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MudletProfile : public QObject, public IEngine
std::variant<KeyPress, QString> pressKey(int key, int modifiers, const std::optional<QString>& text) override;
QString mapPointer(const PointerEvent& event) override;
QString setMapViewOnly(bool viewOnly) override;
QString setMapScale(double scale) override;
QString setMapScale(double scale, double density) override;
QString setAppMapSize(const QSize& size, double scale) override;
QString appMapPointer(const PointerEvent& event) override;
std::variant<RoomList, QString> findRooms(const QString& text) const override;
Expand Down Expand Up @@ -299,8 +299,9 @@ class MudletProfile : public QObject, public IEngine
QSize mMainWindowSize{0, 0};
// The size of the mapper window as pushMap() last had it: none while it did not show
std::optional<QSize> mMapSize;
// Device pixels to each of the mapper window's, as setMapScale() asked
// Device pixels to each of the mapper window's, and to each of the UI's own map's units, as setMapScale() asked
double mMapScale = 1;
double mMapDensity = 1;
// The mapper's and the app's map as last drawn
MapPicture mMapPicture;
MapPicture mAppMapPicture;
Expand Down
Loading
Loading