56.0, 'box' => 488.0]; /** phones: rows of the one list */ private const P_ROW = 84.0; private const P_TOP = 196.0; private const P_BOTTOM = 608.0; /** bag | box: the side picked */ public string $side = 'bag'; /** @var array the row picked on each side */ public array $sel = ['bag' => 0, 'box' => 0]; /** @var array the scroll of each side (rows on a computer, units on a phone) */ public array $scroll = ['bag' => 0.0, 'box' => 0.0]; public bool $touch = false; public string $note = ''; private float $noteAge = 99.0; /** what the screen asked for this frame (the scene plays it): click | pickup | '' */ public string $sound = ''; /** phones: the finger down [hit, y, scroll, dragged] */ private ?array $press = null; private Wheel $wheel; public function __construct() { $this->wheel = new Wheel(); } public function open(Game $g, bool $touch): void { $this->touch = $touch; $this->side = 'bag'; $this->sel = ['bag' => 0, 'box' => 0]; $this->scroll = ['bag' => 0.0, 'box' => 0.0]; $this->press = null; $this->wheel->reset(); $t = Chests::opened($g); // what the chest had to say (thieves, rotten food) heads the screen $last = $g->messages(); $msg = (string) (end($last) ?: ''); $this->note = $t !== null && (str_contains($msg, 'grimpeurs') || str_contains($msg, 'aigre')) ? $msg : ''; $this->noteAge = 0.0; } /** @return list the bag's items: what can go first, then what is held or worn; by kind, then name */ public static function bagList(Game $g): array { $ids = array_map('strval', array_keys($g->inv->all())); usort($ids, static function (string $a, string $b) use ($g): int { $wa = Chests::movable($g->inv, $a) === 0 ? 1 : 0; $wb = Chests::movable($g->inv, $b) === 0 ? 1 : 0; return $wa <=> $wb ?: self::byKind($a, $b); }); return $ids; } /** @return list */ public static function boxList(Inventory $s): array { $ids = array_map('strval', array_keys($s->all())); usort($ids, [self::class, 'byKind']); return $ids; } private static function byKind(string $a, string $b): int { $order = ['weapon' => 0, 'tool' => 1, 'clothing' => 2, 'food' => 3, 'drink' => 4, 'medical' => 5, 'quest' => 6, 'material' => 7, 'trade' => 8]; return ($order[Items::get($a)['kind']] ?? 9) <=> ($order[Items::get($b)['kind']] ?? 9) ?: strcmp(Items::name($a), Items::name($b)); } private function say(string $text): void { $this->note = $text; $this->noteAge = 0.0; } // ------------------------------------------------------------------------------------------ input /** Returns false when the screen closes. */ public function update(UiInput $in, Game $g, float $dt): bool { $this->noteAge += $dt; $this->sound = ''; $t = Chests::opened($g); if ($t === null || $in->back) { return false; } $lists = ['bag' => self::bagList($g), 'box' => self::boxList(Chests::store($t))]; foreach ($lists as $side => $list) { $this->sel[$side] = max(0, min($this->sel[$side], count($list) - 1)); } // keys and pad if ($in->left || $in->right) { $this->side = $in->left ? 'bag' : 'box'; } $n = count($lists[$this->side]); if ($n > 0 && ($in->up || $in->down)) { $this->sel[$this->side] = ($this->sel[$this->side] + ($in->down ? 1 : -1) + $n) % $n; $this->follow($this->side); } if ($in->ok) { $this->move($g, $t, $this->side, $lists[$this->side][$this->sel[$this->side]] ?? null, 1); } elseif ($in->drop) { $this->move($g, $t, $this->side, $lists[$this->side][$this->sel[$this->side]] ?? null, PHP_INT_MAX); } elseif ($in->prev || $in->digit === 1) { $this->all($g, $t, 'put'); } elseif ($in->next || $in->digit === 2) { $this->all($g, $t, 'take'); } if ($this->touch) { return $this->phoneInput($in, $g, $t, $lists); } // the mouse foreach ($lists as $side => $list) { $this->scroll[$side] = (float) Wheel::clamp((int) $this->scroll[$side], count($list), self::ROWS); // a list that shrank } $steps = $this->wheel->steps($in->wheel); if ($steps !== 0) { $side = $in->mx >= self::COLS['box'] - 8 ? 'box' : 'bag'; $this->scroll[$side] = (float) Wheel::clamp((int) $this->scroll[$side] + $steps, count($lists[$side]), self::ROWS); } if ($in->moved && $in->hover !== null && preg_match('/^cv:(bag|box):(\d+)$/', $in->hover, $m)) { $this->side = $m[1]; $this->sel[$m[1]] = (int) $m[2]; } if ($in->click && $in->hit !== null) { return $this->act($in->hit, $g, $t, $lists, true); } return true; } /** Keeps the picked row in view (a computer's list). */ private function follow(string $side): void { if ($this->touch) { $y = $this->sel[$side] * self::P_ROW; $this->scroll[$side] = max(min($this->scroll[$side], $y), $y + self::P_ROW - (self::P_BOTTOM - self::P_TOP)); return; } $s = (int) $this->scroll[$side]; $sel = $this->sel[$side]; $this->scroll[$side] = (float) ($sel < $s ? $sel : ($sel >= $s + self::ROWS ? $sel - self::ROWS + 1 : $s)); } /** @param array> $lists */ private function phoneInput(UiInput $in, Game $g, Thing $t, array $lists): bool { $side = $this->side; $scroll = $this->scroll[$side] - $in->wheel * 70; if ($in->click) { if (in_array($in->hit, ['cv:close', 'cv:tab:bag', 'cv:tab:box', 'cv:putall', 'cv:takeall'], true)) { return $this->act((string) $in->hit, $g, $t, $lists, false); } $this->press = [$in->hit, $in->my, $scroll, false]; } elseif ($this->press !== null) { if ($in->held) { $dy = $in->my - (float) $this->press[1]; if (abs($dy) > 12 || $this->press[3]) { $this->press[3] = true; $scroll = (float) $this->press[2] - $dy; } } else { $press = $this->press; $this->press = null; if (!$press[3] && $press[0] !== null && !$this->act((string) $press[0], $g, $t, $lists, false)) { return false; } $scroll = $this->scroll[$side]; } } $rows = count($lists[$side]) + ($side === 'box' && $lists['box'] === [] ? 1 : 0); $max = max(0.0, $rows * self::P_ROW - (self::P_BOTTOM - self::P_TOP)); $this->scroll[$side] = max(0.0, min($max, $scroll)); return true; } /** A click or a tap on a region. Returns false when the screen closes. @param array> $lists */ private function act(string $hit, Game $g, Thing $t, array $lists, bool $desktop): bool { if ($hit === 'cv:close') { return false; } if ($hit === 'cv:tab:bag' || $hit === 'cv:tab:box') { $this->side = substr($hit, 7); $this->sound = 'click'; } elseif ($hit === 'cv:putall') { $this->all($g, $t, 'put'); } elseif ($hit === 'cv:takeall') { $this->all($g, $t, 'take'); } elseif ($hit === 'cv:one' || $hit === 'cv:stack') { $this->move($g, $t, $this->side, $lists[$this->side][$this->sel[$this->side]] ?? null, $hit === 'cv:one' ? 1 : PHP_INT_MAX); } elseif ($hit === 'cv:pick') { $text = Chests::pickUp($g, $t); $g->say($text); if (Chests::opened($g) === null) { return false; // taken back: nothing left to show } $this->say($text); } elseif (preg_match('/^cv:(bag|box):(\d+)$/', $hit, $m)) { // a computer: the first click picks the row, a click on the picked row moves one $again = $this->side === $m[1] && $this->sel[$m[1]] === (int) $m[2]; $this->side = $m[1]; $this->sel[$m[1]] = (int) $m[2]; if ($again && $desktop) { $this->move($g, $t, $m[1], $lists[$m[1]][(int) $m[2]] ?? null, 1); } else { $this->sound = 'click'; } } elseif (preg_match('/^cv:(one|stack):(bag|box):(\d+)$/', $hit, $m)) { $this->sel[$m[2]] = (int) $m[3]; $this->move($g, $t, $m[2], $lists[$m[2]][(int) $m[3]] ?? null, $m[1] === 'one' ? 1 : PHP_INT_MAX); } return true; } /** Moves $n of an item from that side to the other one, and says why not when nothing moved. */ private function move(Game $g, Thing $t, string $side, ?string $id, int $n): void { if ($id === null) { return; } $name = Items::name($id); if ($side === 'bag') { $done = Chests::put($g, $t, $id, $n); if ($done > 0) { $this->say('Rangé dans ' . mb_strtolower(Chests::label($t)) . ' : ' . Dialogues::list([$id => $done]) . '.'); } elseif (Chests::movable($g->inv, $id) === 0) { $this->say('Tu l\'as sur toi (' . mb_strtolower($name) . ') : range-le ou ôte-le d\'abord.'); } else { $this->say(self::full($t, $id)); } } else { $done = Chests::take($g, $t, $id, $n); $this->say($done > 0 ? 'Pris : ' . Dialogues::list([$id => $done]) . '.' : 'Ton sac est trop lourd pour le prendre.'); } $this->sound = $done > 0 ? 'pickup' : 'click'; $this->clamp($g, $t); } /** Why an item does not go into the chest: its kinds, or its weight. */ private static function full(Thing $t, string $id): string { $s = Chests::store($t); $spec = Chests::spec($t->kind); if (!$s->has($id) && count($s->all()) >= $spec['slots']) { return 'Plus de place : ' . Chests::label($t) . ' ne range que ' . $spec['slots'] . ' sortes d\'objets.'; } return Chests::label($t) . ' est plein : ' . Translator::num($spec['capacity'], 0) . ' kg au plus.'; } /** "Tout déposer" / "Tout prendre". */ private function all(Game $g, Thing $t, string $way): void { $moved = $way === 'put' ? Chests::putAll($g, $t) : Chests::takeAll($g, $t); if ($way === 'put') { $left = 0; foreach (array_keys($g->inv->all()) as $id) { $left += Chests::movable($g->inv, (string) $id); } $text = $moved === [] ? ($left > 0 ? self::full($t, (string) array_key_first($g->inv->all())) : 'Rien à ranger : tu ne gardes que ce que tu portes.') : sprintf('Rangé : %d objets (%d sortes).', array_sum($moved), count($moved)) . ($left > 0 ? ' Le reste ne rentre pas.' : ''); } else { $left = Chests::store($t)->all(); $text = $moved === [] ? ($left === [] ? 'Il est vide.' : 'Ton sac est trop lourd pour en prendre plus.') : sprintf('Pris : %d objets (%d sortes).', array_sum($moved), count($moved)) . ($left !== [] ? ' Le reste est trop lourd pour ton sac.' : ''); } $this->say($text); $this->sound = $moved !== [] ? 'pickup' : 'click'; $this->clamp($g, $t); } private function clamp(Game $g, Thing $t): void { $this->sel['bag'] = max(0, min($this->sel['bag'], count($g->inv->all()) - 1)); $this->sel['box'] = max(0, min($this->sel['box'], count(Chests::store($t)->all()) - 1)); if (!$this->touch) { foreach (['bag', 'box'] as $side) { $this->follow($side); } } } // ------------------------------------------------------------------------------------------ drawing public function draw(Canvas $c, Game $g): void { $t = Chests::opened($g); if ($t === null) { return; } $s = Chests::store($t); $lists = ['bag' => self::bagList($g), 'box' => self::boxList($s)]; if ($this->touch) { $this->drawPhone($c, $g, $t, $s, $lists); return; } $spec = Chests::spec($t->kind); $c->rect(0, 0, Canvas::W, Canvas::H, [4, 8, 12, 130]); $c->panel(36, 58, 888, 610, 222, [220, 170, 90, 230]); $c->icon($t->kind, 54, 66, 40); $c->textBox(Chests::label($t), 100, 66, 360, 22, [255, 255, 255]); $c->textBox(Items::name($t->kind), 100, 94, 360, 13, [190, 176, 150]); $c->line(892, 76, 908, 92, [210, 220, 230], 2); $c->line(908, 76, 892, 92, [210, 220, 230], 2); $c->hit(880, 64, 40, 40, 'cv:close'); $c->rect(50, 114, 860, 1, [255, 255, 255, 40]); // the two columns $heads = ['bag' => 'Ton sac', 'box' => Chests::label($t)]; $loads = [ 'bag' => [$g->inv->weight(), $g->inv->capacity(), ''], 'box' => [$s->weight(), $spec['capacity'], count($s->all()) . ' / ' . $spec['slots'] . ' sortes'], ]; foreach (self::COLS as $side => $x) { $on = $side === $this->side; $c->box($x, 120, self::COL_W, 50); $c->textBox(mb_strtoupper(Translator::t($heads[$side])), $x, 122, 200, 14, $on ? [255, 214, 140] : [150, 166, 180]); [$w, $cap, $extra] = $loads[$side]; $c->textBox(Translator::num($w, 1) . ' / ' . Translator::num($cap, 0) . ' kg' . ($extra !== '' ? ' · ' . $extra : ''), $x + 196, 123, self::COL_W - 196, 13, [200, 210, 220], 'right'); $ratio = $cap > 0 ? min(1.0, $w / $cap) : 0.0; $c->rect($x + 4, 148, self::COL_W - 8, 6, [255, 255, 255, 26]); $c->rect($x + 4, 148, (self::COL_W - 8) * $ratio, 6, $ratio > 0.9 ? [230, 80, 60] : ($ratio > 0.7 ? [230, 170, 60] : [110, 200, 140])); $this->drawList($c, $g, $side, $lists[$side], $side === 'bag' ? $g->inv : $s, $x, $on); } $c->rect(471, 124, 1, 410, [255, 255, 255, 30]); // the buttons $id = $lists[$this->side][$this->sel[$this->side]] ?? null; $bag = $this->side === 'bag'; $buttons = [ ['cv:one', 'Entrée', $bag ? 'Ranger 1' : 'Prendre 1', $id !== null], ['cv:stack', 'X', $bag ? 'Ranger la pile' : 'Prendre la pile', $id !== null], ['cv:putall', 'PgPréc', 'Tout ranger', true], ['cv:takeall', 'PgSuiv', 'Tout prendre', $lists['box'] !== []], ]; $bw = 209.0; foreach ($buttons as $k => [$hit, $key, $label, $live]) { $this->button($c, 56 + $k * ($bw + 8.3), 546, $bw, $key, $label, $hit, $live); } if ($lists['box'] === []) { $this->button($c, 56, 592, 300, '', 'Emporter (il est vide)', 'cv:pick', true); } if ($this->note !== '' && $this->noteAge < 6) { $c->box(368, 590, 542, 44); $c->textBox($this->note, 368, 590, 542, 13, [255, 226, 160], 'left', 3, 1.15); } $c->text('← → : sac ou coffre · ↑ ↓ : choisir · clic : choisir, re-clic : ranger 1 · Échap : fermer', 480, 644, 12, [150, 164, 178], 'center'); } /** One column of a computer's screen. @param list $list */ private function drawList(Canvas $c, Game $g, string $side, array $list, Inventory $inv, float $x, bool $on): void { $first = (int) $this->scroll[$side]; if ($list === []) { $c->textBox($side === 'bag' ? 'Ton sac est vide.' : 'Rien dedans pour l\'instant.', $x, self::TOP + 20, self::COL_W, 15, [150, 160, 170], 'center'); } for ($k = 0; $k < self::ROWS; $k++) { $i = $first + $k; $id = $list[$i] ?? null; if ($id === null) { break; } $y = self::TOP + $k * self::ROW; $picked = $i === $this->sel[$side]; $kept = $side === 'bag' && Chests::movable($g->inv, $id) === 0; $c->rect($x, $y, self::COL_W, self::ROW - 3, $picked ? ($on ? [120, 190, 255, 60] : [255, 255, 255, 26]) : [255, 255, 255, 10]); if ($picked) { $c->rect($x, $y, 3, self::ROW - 3, $on ? [150, 210, 255, 230] : [150, 166, 180, 160]); } $c->box($x, $y, self::COL_W, self::ROW - 3); $c->icon($id, $x + 8, $y + 2, 29); $c->textBox(Items::name($id), $x + 42, $y + 7, self::COL_W - 150, 15, $kept ? [150, 160, 170] : [240, 244, 248]); $n = $inv->count($id); $right = $kept ? ($g->inv->isWorn($id) !== null ? 'porté' : 'en main') : ($n > 1 ? '×' . $n : ''); $c->textBox($right, $x + self::COL_W - 112, $y + 8, 58, 14, $kept ? [255, 204, 110] : [255, 255, 255], 'right'); $c->textBox(Translator::num((float) Items::get($id)['weight'] * max(1, $n), 1) . ' kg', $x + self::COL_W - 58, $y + 9, 58, 12, [150, 166, 180], 'right'); $c->hit($x, $y, self::COL_W, self::ROW - 3, 'cv:' . $side . ':' . $i); } if (count($list) > self::ROWS) { $h = self::ROWS * self::ROW; $c->rect($x + self::COL_W - 3, self::TOP + $h * $first / count($list), 3, $h * self::ROWS / count($list), [255, 255, 255, 70]); } } /** A button of a fixed width: its key in a cap, its label fitted. */ private function button(Canvas $c, float $x, float $y, float $w, string $key, string $label, string $hit, bool $live): void { $c->rect($x, $y, $w, 38, $live ? [120, 190, 255, 44] : [255, 255, 255, 10]); $c->border($x, $y, $w, 38, $live ? [150, 210, 255, 150] : [255, 255, 255, 30]); $c->box($x, $y, $w, 38); $kw = 0.0; if ($key !== '') { $kw = min(70.0, $c->measure($key, 11) + 12); $c->pill($x + 7, $y + 10, $kw, 18, [235, 238, 242, $live ? 220 : 90]); $c->text($key, $x + 7 + $kw / 2, $y + 12, 11, [20, 26, 34], 'center', false); $kw += 8; } $c->textBox($label, $x + 7 + $kw, $y + 9, $w - 10 - $kw, 15, $live ? [255, 255, 255] : [130, 140, 150]); if ($live) { $c->hit($x, $y, $w, 38, $hit); } } // ------------------------------------------------------------------------------------------ phones /** @param array> $lists */ private function drawPhone(Canvas $c, Game $g, Thing $t, Inventory $s, array $lists): void { $spec = Chests::spec($t->kind); $c->rect(0, 0, Canvas::W, Canvas::H, [8, 12, 18, 245]); $c->pill(10, 8, 150, 64, [255, 255, 255, 30]); $c->textBox('× Fermer', 10, 26, 150, 18, [255, 255, 255], 'center'); $c->hit(6, 4, 160, 72, 'cv:close'); $c->box(180, 8, 772, 64); $c->textBox(Chests::label($t) . ' · ' . Items::name($t->kind), 180, 22, 772, 20, [255, 214, 140], 'center'); // the two sides $tabs = ['bag' => 'Ton sac', 'box' => Chests::label($t)]; $x = 8.0; foreach ($tabs as $side => $label) { $on = $side === $this->side; $c->rect($x, 82, 468, 60, $on ? [120, 190, 255, 90] : [255, 255, 255, 18]); if ($on) { $c->rect($x, 138, 468, 4, [150, 210, 255, 240]); } $c->box($x, 82, 468, 60); $c->textBox(Translator::t($label) . ' (' . count($lists[$side]) . ')', $x, 96, 468, 17, $on ? [255, 255, 255] : [180, 192, 204], 'center'); $c->hit($x, 78, 468, 68, 'cv:tab:' . $side); $x += 476; } $bag = $this->side === 'bag'; [$w, $cap] = $bag ? [$g->inv->weight(), $g->inv->capacity()] : [$s->weight(), $spec['capacity']]; $c->box(8, 148, 944, 44); $c->textBox(($bag ? 'Charge ' : 'Contenu ') . Translator::num($w, 1) . ' / ' . Translator::num($cap, 0) . ' kg' . ($bag ? '' : ' · ' . count($s->all()) . ' / ' . $spec['slots'] . ' sortes'), 8, 152, 560, 14, [200, 210, 220]); $c->textBox($bag ? '1 / Tout : ranger dans le coffre' : '1 / Tout : prendre dans ton sac', 560, 152, 392, 13, [150, 166, 180], 'right'); // the list $list = $lists[$this->side]; $inv = $bag ? $g->inv : $s; $y = self::P_TOP - $this->scroll[$this->side]; if ($list === []) { $c->box(8, self::P_TOP, 944, 80); $c->textBox($bag ? 'Ton sac est vide.' : 'Rien dedans pour l\'instant.', 8, self::P_TOP + 20, 944, 16, [150, 160, 170], 'center'); if (!$bag) { $this->pBig($c, 8, self::P_TOP + 84, 944, 'Emporter (il est vide)', 'cv:pick', [200, 160, 100]); } } foreach ($list as $i => $id) { if ($y >= self::P_TOP - 1 && $y + self::P_ROW - 6 <= self::P_BOTTOM) { $kept = $bag && Chests::movable($g->inv, $id) === 0; $n = $inv->count($id); $c->rect(8, $y, 944, self::P_ROW - 6, [255, 255, 255, $i === $this->sel[$this->side] ? 30 : 14]); $c->box(8, $y, 944, self::P_ROW - 6); $c->icon($id, 18, $y + 12, 52); $c->textBox(Items::name($id), 80, $y + 6, 560, 17, $kept ? [150, 160, 170] : [240, 244, 248]); $sub = $kept ? ($g->inv->isWorn($id) !== null ? 'porté' : 'en main') : '×' . $n . ' · ' . Translator::num((float) Items::get($id)['weight'] * $n, 1) . ' kg'; $c->textBox($sub, 80, $y + 42, 560, 13, $kept ? [255, 204, 110] : [160, 174, 188]); if (!$kept) { $this->pBig($c, 650, $y + 4, 120, '1', 'cv:one:' . $this->side . ':' . $i, [110, 200, 140], self::P_ROW - 14); $this->pBig($c, 778, $y + 4, 166, 'Tout', 'cv:stack:' . $this->side . ':' . $i, [120, 190, 255], self::P_ROW - 14); } } $y += self::P_ROW; } // the bottom bar $c->rect(0, 612, 960, 108, [8, 12, 18, 250]); $this->pBig($c, 8, 618, 468, 'Tout ranger', 'cv:putall', [110, 200, 140]); $this->pBig($c, 484, 618, 468, 'Tout prendre', 'cv:takeall', [120, 190, 255]); if ($this->note !== '' && $this->noteAge < 5) { $lh = 19 * $c->boost; $lines = array_slice($c->wrap($this->note, 14, 900), 0, 2); $h = $lh * count($lines) + 14; $c->rect(0, 612 - $h, 960, $h, [20, 16, 6, 240]); $c->box(0, 612 - $h, 960, $h); foreach ($lines as $k => $line) { $c->text($line, 24, 612 - $h + 7 + $k * $lh, 14, [255, 226, 160]); } } } /** A big phone button with a centred label. */ private function pBig(Canvas $c, float $x, float $y, float $w, string $label, string $hit, array $col, float $h = 66.0): void { $c->rect($x, $y, $w, $h, [$col[0], $col[1], $col[2], 70]); $c->border($x, $y, $w, $h, [$col[0], $col[1], $col[2], 200]); $c->box($x, $y, $w, $h); $c->textBox($label, $x, $y + ($h - 18 * $c->boost) / 2, $w, 18, [255, 255, 255], 'center'); $c->hit($x, $y, $w, $h, $hit); } }