18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 11:47:06 +01:00
Files
maszyna/application/editormodeshared.h
T
maj00r 9f6c4f87ae feat(editor): the track tool panels keep their fields, results and buttons within the panel (#453)
* fix(editor): track edits keep the joints, the norms, the records and the saved speed together

- The cant typed in the inspector goes on into the track joined at that end, as the gizmo and the
  joints tool do it; the neighbours go into the same undo step. The joint stepped before.
- A track laid on from a canted joint takes the cant off along the ramp the norms ask for at the
  speed (`alignment::cant_ramp_length`), not at a fixed 1.5 mm a metre whatever the speed and the
  norms: 120 mm at 100 km/h over 120 m, not 80 m.
- Deleting a track of the line under the route tool keeps the laying norms. They went back to the
  defaults, and every later lay, extend and join used those.
- The gauge is a paste group of its own, off by default: pasting how a track rides or sounds no
  longer changes its gauge.
- The speed of a turntable set in the editor is the one saved. The editor changed the speed the
  turntable runs at, while the file keeps the limit it was loaded with.
- Deleting a set of tracks does what Del does with each: a double switch, a slip and a crossover go
  whole, the records of the switches go with them, and the model and events of a turntable too. The
  records stayed behind and on saving were given to another switch, which then showed a wrong type.
- Deleting a switch of a crossover takes the crossover out whole, as the connection tools do: the
  outer track of a crossover on a curve comes down by its lift and the record of the connection
  goes. Alone, the switch left the outer track up for good, and the connection to the next switch
  given its name. A switch bent out of the shape of its record is found by its name.

Tests: a unit test of the norm ramp. The other changes are in the editor's UI, which the unit tests
don't reach.

* test(editor): self-test scripts that fail, a runner, and a case for each fix of the track editing

The editor's self-test scripts only logged: nothing made a run fail. Now:

- `expect` commands check the state and log `SELFTEST PASS` or `SELFTEST FAIL ... got X, expected Y`:
  the cant at a track end, the joint at an end (gap, angle, cant step), the records of a switch and
  the connections naming it, speed, gauge, a track present or removed, the laying norms' gauge,
  heights and lifts against a mark, whether a track is in the line under the route tool, and the
  includes of a turntable. A command no group takes counts as a failure, so a mistyped one can't
  make the checks after it pass.
- At `quit` the result is logged, and a script whose expectations failed ends the editor with exit
  code 2 (a failed start stays 1; the simulator's test mode is unchanged).
- The commit of the inspector's cant field is `track_cant_commit`, which the inspector and the
  `trackcant` command both call; `routegauge` sets the laying norms as the route tool does, and
  `tracksetdelete` deletes a set of tracks by name.
- `tests/editor/run.py` runs each script of `tests/editor/cases` on a copy of the fixture scenery
  in the game folder, with a wall-time limit and a memory cap, reads the exit code and the
  `SELFTEST` lines, and deletes the copy. A `#> reopen` line restarts the editor on the saved
  scenery. See `tests/editor/README.md`.
- One case per fix of the track editing quick fixes (inspector cant, norms after deleting, the
  gauge paste group, turntable speed saved, deleting a set, a set with a double switch, a slip
  and a crossover, deleting a switch of a crossover, a bent switch). All pass; with the fixes
  reverted all fail.

* feat(editor): the track tool panels keep their fields, results and buttons within the panel

- The card of the tool: after a long gesture the action goes under it, at its indent, when what is
  left of the row is narrower than 8 font sizes or the longest word of the action. It was squeezed
  into a sliver and broken in the middle of its words.
- A click on the title row of the card folds it to that row (icon, name, family, key, and a small
  triangle marker) and opens it again. One flag for all the tools, kept with the editor settings
  (`track_card_folded` in eu07_editor.ini), open by default: the card took 250-300 px at the top
  of every tool and repeats the hint bar of the view.
- The grade labels of the vertical profile, on the graph and in the view, write the per mille as
  the panel does (the translated "per mille", the per mille sign in Polish), not "o/oo".
- The track analysis window: the thresholds of the speed check and of the joints keep to their
  widest name and a field of 10 font sizes where the window is wider than that, as docked across
  the bottom (`begin_fields_fitted`). The results are tables with each column as wide as what is in
  it: the speed check with its column names kept at the top as the list scrolls, the joints with
  the kind, the value and the two tracks next to each other. A click on a row shows it as before.
- `same_line_if_fits` puts the next button beside the one before only when it fits in what is left
  of the line, at the start of the next line otherwise. Used where the route design puts buttons
  side by side, for "Forget the design" in the profile panel, which was cut off at the edge, and for
  the buttons of a switch in the inspector.
- The laying options: the speed, the curve radius and the longest path are fields with the name on
  the left in two columns (`begin_fields`/`field_row`), as in the inspector, the route and the
  profile, the chips of the quick values under them. The segmented choices give the short names
  less of the room when a long one doesn't fit in its even share, and go on in a second bar when
  they don't fit side by side: "Through the clicked points" overflowed its third of the bar.

* test(editor): a mistyped track fails the self-test, indented comments are skipped, case 7b checks the switch got bent

- A blank line or an indented `#` comment in a script is no longer a command no group takes.
- `trackcant`, `routegauge` and `tracksetdelete` with a track that isn't there count as a failed expectation.
- `expect shaped <switch> <yes|no>`: 7b checks that the gizmo drag bent the switch out of its record before deleting it.
- The ramp-length loop of test_alignment counts in an integer (Sonar S2193).
2026-11-27 03:54:33 +01:00

509 lines
20 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <initializer_list>
#include <string>
#include <vector>
#include "imgui/imgui.h"
#include "utilities/translation.h"
float constexpr kDragPixels{5.0f};
template <typename Node_> inline std::string name_of(Node_ const &Node)
{
return Node.name().empty() ? std::string{STR_C("(no name)")} : Node.name();
}
class TTrack;
// the type of a track in words: normal, switch, cross, turntable, tributary
char const *track_type_name(TTrack const &Track);
// the name of a switch template in the language of the editor: the words of the catalogue translated, the kind of a slip or a
// crossing left out where its type says it
std::string switch_name_translated(std::string Title);
inline float tall_button_height()
{
return std::round(ImGui::GetFrameHeight() * 1.3f);
}
inline bool main_button(char const *Label, ImVec2 const &Size)
{
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.2f, 0.55f, 0.25f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.25f, 0.65f, 0.3f, 1.0f));
auto const pressed{ImGui::Button(Label, Size)};
ImGui::PopStyleColor(2);
return pressed;
}
inline void item_tooltip(char const *Text)
{
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C(Text));
}
// the next item on the line of the one before it when it fits in what's left of the line, at the start of the next line
// otherwise, where SameLine would put it past the edge of the panel and cut it off; Width is the width of the next item
inline void same_line_if_fits(float const Width)
{
ImGui::SameLine();
if (ImGui::GetContentRegionAvail().x < Width)
ImGui::NewLine();
}
// the same for a button (or a small button) with this label, as drawn
inline void same_line_if_fits(char const *Label)
{
same_line_if_fits(ImGui::CalcTextSize(Label, nullptr, true).x + ImGui::GetStyle().FramePadding.x * 2.0f);
}
// grid points without a height (or another value) take the one of the nearest point which has it (by steps along the grid)
template <typename Value_> inline void fill_from_nearest(std::vector<Value_> &Heights, std::vector<char> &Found, std::size_t const Side)
{
std::deque<std::size_t> queue;
for (std::size_t index = 0; index < Found.size(); ++index)
if (Found[index] != 0)
queue.push_back(index);
while (false == queue.empty())
{
auto const index = queue.front();
queue.pop_front();
auto const ix = index % Side, iz = index / Side;
auto const visit = [&](std::size_t const Next) {
if (Found[Next] != 0)
return;
Found[Next] = 1;
Heights[Next] = Heights[index];
queue.push_back(Next);
};
if (ix > 0)
visit(index - 1);
if (ix + 1 < Side)
visit(index + 1);
if (iz > 0)
visit(index - Side);
if (iz + 1 < Side)
visit(index + Side);
}
}
// the families of the track tools, each with its colour: building the track, equipping it, checking it
inline ImU32 family_colour(int const Family, float const Alpha = 1.0f)
{
static ImVec4 const colours[]{{0.30f, 0.60f, 0.95f, 1.0f}, {0.95f, 0.66f, 0.22f, 1.0f}, {0.25f, 0.80f, 0.66f, 1.0f}};
auto colour{colours[std::clamp(Family, 0, 2)]};
colour.w = Alpha;
return ImGui::ColorConvertFloat4ToU32(colour);
}
inline char const *family_name(int const Family)
{
static char const *const names[]{"Building", "Equipment", "Checks"};
return STR_C(names[std::clamp(Family, 0, 2)]);
}
// a numbered step of the work in a panel: the number in a circle, its name, a line to the edge of the panel
inline void step_heading(int const Number, char const *Text)
{
ImGui::Spacing();
auto *draw{ImGui::GetWindowDrawList()};
auto const at{ImGui::GetCursorScreenPos()};
auto const height{ImGui::GetTextLineHeight()};
auto const radius{std::round(height * 0.6f)};
ImVec2 const centre{at.x + radius, at.y + radius};
draw->AddCircleFilled(centre, radius, ImGui::GetColorU32(ImGuiCol_CheckMark), 20);
char digit[8];
std::snprintf(digit, sizeof(digit), "%d", Number);
auto const size{ImGui::CalcTextSize(digit)};
draw->AddText(ImVec2(std::round(centre.x - size.x * 0.5f), std::round(centre.y - size.y * 0.5f)), IM_COL32(16, 18, 22, 255), digit);
ImGui::Dummy(ImVec2(radius * 2.0f, radius * 2.0f));
ImGui::SameLine(0.0f, radius * 0.6f);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + radius - height * 0.5f);
ImGui::TextUnformatted(Text);
auto const end{ImGui::GetItemRectMax().x + radius * 0.6f};
auto const edge{ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x};
if (edge > end)
draw->AddLine(ImVec2(end, centre.y), ImVec2(edge, centre.y), ImGui::GetColorU32(ImGuiCol_Separator));
ImGui::Spacing();
}
// a toggle drawn as a pill: a swatch of the colour of what it shows and its name, lit while on
inline bool pill_toggle(char const *Label, ImU32 const Swatch, bool &Value)
{
auto const text{ImGui::CalcTextSize(Label)};
auto const height{ImGui::GetFrameHeight()};
auto const swatch{std::round(height * 0.7f)};
auto const pad{std::round(height * 0.35f)};
ImVec2 const size{pad + swatch + pad * 0.6f + text.x + pad, height};
auto const at{ImGui::GetCursorScreenPos()};
ImGui::PushID(Label);
auto const clicked{ImGui::InvisibleButton("##pill", size)};
ImGui::PopID();
if (clicked)
Value = !Value;
auto const hovered{ImGui::IsItemHovered()};
auto *draw{ImGui::GetWindowDrawList()};
ImVec2 const end{at.x + size.x, at.y + size.y};
draw->AddRectFilled(at, end, ImGui::GetColorU32(Value ? (hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Header) : (hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg)), height * 0.5f);
auto const middle{at.y + height * 0.5f};
auto const colour{Value ? Swatch : (Swatch & ~IM_COL32_A_MASK) | IM_COL32(0, 0, 0, 70)};
draw->AddLine(ImVec2(at.x + pad, middle), ImVec2(at.x + pad + swatch, middle), colour, std::max(2.0f, height * 0.16f));
draw->AddText(ImVec2(at.x + pad + swatch + pad * 0.6f, std::round(middle - text.y * 0.5f)), ImGui::GetColorU32(Value ? ImGuiCol_Text : ImGuiCol_TextDisabled), Label);
return clicked;
}
// a choice of a few as one bar of segments across the panel, the chosen one lit; true when the choice changed
struct segment_option
{
int value;
char const *label;
char const *tooltip{nullptr};
};
inline bool segmented(char const *Id, int &Value, std::initializer_list<segment_option> Options)
{
auto const width{ImGui::GetContentRegionAvail().x};
auto const height{ImGui::GetFrameHeight()};
auto const at{ImGui::GetCursorScreenPos()};
auto *draw{ImGui::GetWindowDrawList()};
auto const rounding{height * 0.5f};
// the segments share the bar evenly while each name fits in its share; else the short names give up some of the room
// around them to the long ones, and the names which don't fit side by side even so go on in a bar under it
auto const room{std::round(ImGui::GetFontSize())};
std::vector<float> needs;
for (auto const &option : Options)
needs.push_back(ImGui::CalcTextSize(option.label).x + room);
std::vector<std::size_t> bars{0}; // the first option of each bar, and the end
float used{0.0f};
for (std::size_t i = 0; i < needs.size(); ++i)
{
if (i > bars.back() && used + needs[i] > width)
{
bars.push_back(i);
used = 0.0f;
}
used += needs[i];
}
bars.push_back(needs.size());
auto const gap{std::round(ImGui::GetStyle().ItemSpacing.y * 0.5f)};
std::vector<ImVec2> spans; // the left and the right of each segment
std::vector<float> tops;
for (std::size_t bar = 0; bar + 1 < bars.size(); ++bar)
{
auto const first{bars[bar]};
auto const last{bars[bar + 1]};
auto const count{static_cast<float>(last - first)};
auto const top{at.y + static_cast<float>(bar) * (height + gap)};
draw->AddRectFilled(ImVec2(at.x, top), ImVec2(at.x + width, top + height), ImGui::GetColorU32(ImGuiCol_FrameBg), rounding);
float sum{0.0f};
bool even{true};
for (auto i{first}; i < last; ++i)
{
sum += needs[i];
even = even && needs[i] <= width / count;
}
auto left{at.x};
for (auto i{first}; i < last; ++i)
{
auto const share{even ? width / count : needs[i] + (width - sum) / count};
spans.emplace_back(std::round(left), i + 1 == last ? at.x + width : std::round(left + share));
tops.push_back(top);
left += share;
}
}
ImGui::PushID(Id);
bool changed{false};
std::size_t index{0};
for (auto const &option : Options)
{
auto const left{spans[index].x};
auto const right{spans[index].y};
auto const top{tops[index]};
ImGui::SetCursorScreenPos(ImVec2(left, top));
ImGui::PushID(option.value);
if (ImGui::InvisibleButton("##segment", ImVec2(std::max(1.0f, right - left), height)) && Value != option.value)
{
Value = option.value;
changed = true;
}
auto const hovered{ImGui::IsItemHovered()};
if (option.tooltip != nullptr && hovered)
ImGui::SetTooltip("%s", STR_C(option.tooltip));
ImGui::PopID();
auto const chosen{Value == option.value};
if (chosen || hovered)
draw->AddRectFilled(ImVec2(left + 2.0f, top + 2.0f), ImVec2(right - 2.0f, top + height - 2.0f), chosen ? family_colour(0, 0.8f) : ImGui::GetColorU32(ImGuiCol_FrameBgHovered), rounding - 2.0f);
auto const text{ImGui::CalcTextSize(option.label)};
draw->PushClipRect(ImVec2(left + 4.0f, top), ImVec2(right - 4.0f, top + height), true);
draw->AddText(ImVec2(std::round(std::max(left + 6.0f, (left + right - text.x) * 0.5f)), std::round(top + (height - text.y) * 0.5f)), ImGui::GetColorU32(chosen ? ImGuiCol_Text : ImGuiCol_TextDisabled), option.label);
draw->PopClipRect();
++index;
}
ImGui::PopID();
ImGui::SetCursorScreenPos(at);
ImGui::Dummy(ImVec2(width, tops.empty() ? height : tops.back() + height - at.y));
return changed;
}
// values picked with a click, as round chips which go on to the next line at the edge of the panel; true when one was picked
inline bool value_chips(char const *Id, double &Value, std::initializer_list<double> Values, char const *Format)
{
auto const edge{ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x};
auto const gap{std::round(ImGui::GetStyle().ItemSpacing.x * 0.6f)};
ImGui::PushID(Id);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, ImGui::GetFrameHeight() * 0.5f);
bool changed{false};
bool first{true};
for (auto const value : Values)
{
char text[32];
std::snprintf(text, sizeof(text), Format, value);
auto const width{ImGui::CalcTextSize(text).x + ImGui::GetStyle().FramePadding.x * 2.0f};
if (false == first)
{
ImGui::SameLine(0.0f, gap);
if (ImGui::GetCursorScreenPos().x + width > edge)
ImGui::NewLine();
}
first = false;
auto const chosen{std::abs(Value - value) < 1e-6};
ImGui::PushStyleColor(ImGuiCol_Button, chosen ? family_colour(0, 0.8f) : ImGui::GetColorU32(ImGuiCol_FrameBg));
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetColorU32(chosen ? ImGuiCol_Text : ImGuiCol_TextDisabled));
if (ImGui::Button(text))
{
Value = value;
changed = true;
}
ImGui::PopStyleColor(2);
}
ImGui::PopStyleVar();
ImGui::PopID();
return changed;
}
// one of a few named choices as round chips which go on to the next line at the edge of the panel, where a tab bar
// would cut the names; true when one was picked
inline bool choice_chips(char const *Id, int &Value, std::vector<segment_option> const &Options)
{
auto const edge{ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x};
auto const gap{std::round(ImGui::GetStyle().ItemSpacing.x * 0.6f)};
ImGui::PushID(Id);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, ImGui::GetFrameHeight() * 0.5f);
bool changed{false};
bool first{true};
for (auto const &option : Options)
{
auto const width{ImGui::CalcTextSize(option.label).x + ImGui::GetStyle().FramePadding.x * 2.0f};
if (false == first)
{
ImGui::SameLine(0.0f, gap);
if (ImGui::GetCursorScreenPos().x + width > edge)
ImGui::NewLine();
}
first = false;
auto const chosen{Value == option.value};
ImGui::PushID(option.value);
ImGui::PushStyleColor(ImGuiCol_Button, chosen ? family_colour(0, 0.8f) : ImGui::GetColorU32(ImGuiCol_FrameBg));
if (ImGui::Button(option.label) && false == chosen)
{
Value = option.value;
changed = true;
}
if (option.tooltip != nullptr)
item_tooltip(option.tooltip);
ImGui::PopStyleColor();
ImGui::PopID();
}
ImGui::PopStyleVar();
ImGui::PopID();
return changed;
}
// values at a glance as tiles: the value in its colour over its name, a bar of the colour at the left, on to the next row
// at the edge of the panel
struct metric_tile
{
std::string value;
char const *label;
ImU32 colour;
};
inline void metric_tiles(std::initializer_list<metric_tile> Tiles)
{
auto const font{ImGui::GetFontSize()};
auto const height{std::round(font * 2.5f)};
auto const edge{ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x};
auto const gap{std::round(font * 0.3f)};
auto *list{ImGui::GetWindowDrawList()};
bool first{true};
for (auto const &entry : Tiles)
{
auto const value{ImGui::CalcTextSize(entry.value.c_str())};
auto const label{ImGui::CalcTextSize(entry.label)};
auto const width{std::max(std::round(font * 5.0f), std::max(value.x, label.x) + font)};
if (false == first)
{
ImGui::SameLine(0.0f, gap);
if (ImGui::GetCursorScreenPos().x + width > edge)
ImGui::NewLine();
}
first = false;
auto const at{ImGui::GetCursorScreenPos()};
ImGui::Dummy(ImVec2(width, height));
list->AddRectFilled(at, ImVec2(at.x + width, at.y + height), ImGui::GetColorU32(ImGuiCol_FrameBg), std::round(font * 0.3f));
list->AddRectFilled(at, ImVec2(at.x + 3.0f, at.y + height), entry.colour, 2.0f, ImDrawFlags_RoundCornersLeft);
list->AddText(ImVec2(at.x + font * 0.5f, at.y + height * 0.12f), entry.colour, entry.value.c_str());
list->AddText(ImVec2(at.x + font * 0.5f, at.y + height * 0.12f + value.y), ImGui::GetColorU32(ImGuiCol_TextDisabled), entry.label);
}
}
// a choice as a tile: a sketch drawn by Sketch into the box on the left (in the colours of the main line and of what it makes),
// the name beside it; lit while chosen, true when clicked
inline bool sketch_tile(char const *Id, char const *Label, bool const Chosen, float const Width, std::function<void(ImDrawList &, ImVec2, ImVec2, ImU32, ImU32)> const &Sketch)
{
auto const font{ImGui::GetFontSize()};
auto const height{std::round(font * 2.4f)};
auto const top{ImGui::GetCursorScreenPos()};
auto const clicked{ImGui::InvisibleButton(Id, ImVec2(Width, height))};
auto const hovered{ImGui::IsItemHovered()};
auto *list{ImGui::GetWindowDrawList()};
auto const accent{family_colour(0)};
ImVec2 const bottom{top.x + Width, top.y + height};
auto const rounding{std::round(font * 0.35f)};
list->AddRectFilled(top, bottom, Chosen ? (accent & ~IM_COL32_A_MASK) | IM_COL32(0, 0, 0, 46) : ImGui::GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), rounding);
list->AddRect(top, bottom, Chosen ? accent : ImGui::GetColorU32(ImGuiCol_Border), rounding, 0, Chosen ? 2.0f : 1.0f);
auto const pad{std::round(font * 0.4f)};
auto const side{std::round(font * 2.2f)};
ImVec2 const low{top.x + pad, top.y + pad};
ImVec2 const high{top.x + pad + side, bottom.y - pad};
Sketch(*list, low, high, Chosen ? accent : ImGui::GetColorU32(ImGuiCol_Text, 0.7f), Chosen ? IM_COL32(255, 210, 60, 255) : ImGui::GetColorU32(ImGuiCol_Text, 0.45f));
auto const text_left{high.x + pad};
auto const wrap{bottom.x - pad - text_left};
auto const size{ImGui::CalcTextSize(Label, nullptr, false, wrap)};
list->AddText(nullptr, 0.0f, ImVec2(text_left, std::round(top.y + (height - size.y) * 0.5f)), ImGui::GetColorU32(Chosen ? ImGuiCol_Text : ImGuiCol_TextDisabled), Label, nullptr, wrap);
return clicked;
}
// the state of something as a rounded chip in its colour; true when clicked
inline bool state_chip(char const *Label, ImVec4 const &Colour)
{
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, ImGui::GetFrameHeight() * 0.5f);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(Colour.x * 0.3f, Colour.y * 0.3f, Colour.z * 0.3f, 0.9f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(Colour.x * 0.45f, Colour.y * 0.45f, Colour.z * 0.45f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(Colour.x * 0.55f, Colour.y * 0.55f, Colour.z * 0.55f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_Text, Colour);
auto const clicked{ImGui::Button(Label)};
ImGui::PopStyleColor(4);
ImGui::PopStyleVar();
return clicked;
}
// fields as rows of a table: the name wrapped in a column on the left, the field as wide as the rest of the panel;
// field_row takes the untranslated "Name##id" (a literal: the translations are cached by the pointer, a name made at run
// time goes with Translate false) and leaves the cursor where the field goes
inline bool begin_fields(char const *Id, float const Names = 0.42f, float const Width = 0.0f)
{
if (false == ImGui::BeginTable(Id, 2, ImGuiTableFlags_SizingStretchProp, ImVec2(Width, 0.0f)))
return false;
ImGui::TableSetupColumn("##name", ImGuiTableColumnFlags_WidthStretch, Names);
ImGui::TableSetupColumn("##field", ImGuiTableColumnFlags_WidthStretch, 1.0f - Names);
return true;
}
// fields as begin_fields, in a block no wider than its widest name with a field of Field font sizes beside it, where the panel
// is wider than that (a window docked across the bottom); Labels are the names of its rows as field_row takes them
inline bool begin_fields_fitted(char const *Id, float const Names, std::initializer_list<char const *> Labels, float const Field = 10.0f)
{
float widest{0.0f};
for (auto const *label : Labels)
{
auto const *text{STR_C(label)};
widest = std::max(widest, ImGui::CalcTextSize(text, std::strstr(text, "##")).x);
}
// the padding of the cells, and a little to spare so the widest name isn't wrapped
widest += ImGui::GetStyle().CellPadding.x * 2.0f + ImGui::GetFontSize();
auto const width{widest + std::round(ImGui::GetFontSize() * Field)};
if (width >= ImGui::GetContentRegionAvail().x)
return begin_fields(Id, Names);
return begin_fields(Id, widest / width, width);
}
inline void field_row(char const *Label, bool const Translate = true)
{
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::AlignTextToFramePadding();
auto const *text{Translate ? STR_C(Label) : Label};
ImGui::PushTextWrapPos(0.0f);
ImGui::TextUnformatted(text, std::strstr(text, "##"));
ImGui::PopTextWrapPos();
ImGui::TableNextColumn();
ImGui::SetNextItemWidth(-FLT_MIN);
}
// switches of a few things as round chips which go on to the next line at the edge of the panel, lit while on;
// the index of the one clicked, or -1
struct toggle_option
{
char const *label;
bool *value;
char const *tooltip{nullptr};
};
inline int toggle_chips(char const *Id, std::initializer_list<toggle_option> Options)
{
auto const edge{ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x};
auto const gap{std::round(ImGui::GetStyle().ItemSpacing.x * 0.6f)};
auto const mark{std::round(ImGui::GetFontSize() * 0.45f)};
ImGui::PushID(Id);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, ImGui::GetFrameHeight() * 0.5f);
int clicked{-1};
int index{0};
for (auto const &option : Options)
{
auto const on{*option.value};
auto const width{ImGui::CalcTextSize(option.label).x + ImGui::GetStyle().FramePadding.x * 2.0f + mark * 1.6f};
if (index > 0)
{
ImGui::SameLine(0.0f, gap);
if (ImGui::GetCursorScreenPos().x + width > edge)
ImGui::NewLine();
}
auto const at{ImGui::GetCursorScreenPos()};
ImGui::PushID(index);
ImGui::PushStyleColor(ImGuiCol_Button, on ? family_colour(0, 0.8f) : ImGui::GetColorU32(ImGuiCol_FrameBg));
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetColorU32(on ? ImGuiCol_Text : ImGuiCol_TextDisabled));
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(1.0f, 0.5f));
if (ImGui::Button(option.label, ImVec2(width, 0.0f)))
{
*option.value = !on;
clicked = index;
}
ImGui::PopStyleVar();
ImGui::PopStyleColor(2);
if (option.tooltip != nullptr && ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C(option.tooltip));
ImGui::PopID();
auto const middle{ImVec2(at.x + ImGui::GetStyle().FramePadding.x + mark * 0.5f, at.y + ImGui::GetFrameHeight() * 0.5f)};
auto *draw{ImGui::GetWindowDrawList()};
if (*option.value)
draw->AddCircleFilled(middle, mark * 0.5f, IM_COL32(255, 210, 60, 255));
else
draw->AddCircle(middle, mark * 0.5f, ImGui::GetColorU32(ImGuiCol_TextDisabled), 0, 1.5f);
++index;
}
ImGui::PopStyleVar();
ImGui::PopID();
return clicked;
}