Skip to content

Commit 5ce14da

Browse files
committed
Minor cleanups in some Imgui dialogs
1 parent c72b8f6 commit 5ce14da

File tree

4 files changed

+47
-125
lines changed

4 files changed

+47
-125
lines changed

src/ui/frequency.cc

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
#include "ui_imgui.hh"
22

33
namespace UiFrequency {
4-
static uint32_t __always_zero = 0;
4+
static uint32_t __always_zero = 0;
55

66
static bool do_open = false;
77
static bool range = false;
8-
static entity *entity_ptr;
98

109
static bool this_is_tx = false;
1110
static uint32_t* this_freq_range_start;
1211
static uint32_t* this_freq_range_size;
1312

1413
typedef struct {
15-
//only used by update_freq_space, now freq_user
16-
size_t index;
17-
entity *ent;
1814
bool is_tx;
1915
//this is an inclusive range
2016
//a.k.a range_start..=range_end
2117
uint32_t range_start; uint32_t range_end;
2218
} FreqUsr;
2319

24-
static uint32_t max_freq = 0;
25-
static std::vector<FreqUsr> freq_space;
20+
static std::map<uint32_t, std::pair<int,int>> freq_counts; // freq -> (tx_count, rx_count)
2621

27-
//emulating std::optional from c++17 (rust Option) with std::pair<T, bool>
2822
static std::pair<FreqUsr, bool> freq_user(entity *e) {
2923
FreqUsr usr;
30-
usr.ent = e;
3124
if ((e->g_id == O_TRANSMITTER) || (e->g_id == O_MINI_TRANSMITTER)) {
3225
usr.is_tx = true;
3326
usr.range_start = usr.range_end = e->properties[0].v.i;
@@ -48,43 +41,35 @@ static uint32_t __always_zero = 0;
4841
return std::pair<FreqUsr, bool>(usr, true);
4942
}
5043

51-
//Update freq_space and max_freq
52-
//freq_space does not include the current tx
44+
// Build frequency tallies (exclude current entity)
5345
static void update_freq_space() {
54-
freq_space.clear();
55-
max_freq = 0;
56-
uint32_t counter = 0;
46+
freq_counts.clear();
47+
5748
std::map<uint32_t, entity*> all_entities = W->get_all_entities();
58-
for (auto i = all_entities.begin(); i != all_entities.end(); i++) {
49+
for (auto i = all_entities.begin(); i != all_entities.end(); ++i) {
5950
entity *e = i->second;
60-
//Skip currently selected entity
61-
if (e == entity_ptr) continue;
51+
6252
auto x = freq_user(e);
63-
if (x.second) {
64-
max_freq = (std::max)(max_freq, x.first.range_start);
65-
freq_space.push_back(x.first);
53+
if (!x.second) continue;
54+
55+
const FreqUsr &usr = x.first;
56+
for (uint32_t f = usr.range_start; f <= usr.range_end; ++f) {
57+
if (usr.is_tx) freq_counts[f].first++;
58+
else freq_counts[f].second++;
6659
}
6760
}
68-
std::sort(freq_space.begin(), freq_space.end(), [](const FreqUsr& a, const FreqUsr& b) {
69-
//return a.ent->id < b.ent->id;
70-
//XXX: sort by range_start looks better
71-
return a.range_start < b.range_start;
72-
});
73-
for (size_t i = 0; i < freq_space.size(); i++) freq_space[i].index = i;
7461
}
7562

7663
void open(bool is_range, entity *e) {
7764
do_open = true;
7865

7966
range = is_range;
80-
entity_ptr = e;
8167

8268
this_is_tx =
8369
(e->g_id == O_TRANSMITTER) ||
8470
(e->g_id == O_MINI_TRANSMITTER) ||
8571
(e->g_id == O_BROADCASTER);
8672

87-
//XXX: pixel->properties[4].v.i (need i8) is incorrect but this menu is currently unused for pixel anyway
8873
this_freq_range_start = (e->g_id == O_PIXEL) ? &e->properties[4].v.i : &e->properties[0].v.i;
8974
this_freq_range_size = range ? &e->properties[1].v.i : &__always_zero;
9075

@@ -95,7 +80,6 @@ static uint32_t __always_zero = 0;
9580
handle_do_open(&do_open, "Frequency");
9681
ImGui_CenterNextWindow();
9782
if (ImGui::BeginPopupModal("Frequency", REF_TRUE, MODAL_FLAGS)) {
98-
//XXX: This assumes int is 4 bytes
9983
ImGui::DragInt("Frequency", (int*) this_freq_range_start, .1, 0, 10000);
10084
(*this_freq_range_size)++;
10185
if (range) ImGui::SliderInt("Range", (int*) this_freq_range_size, 1, 30);
@@ -108,57 +92,26 @@ static uint32_t __always_zero = 0;
10892
*this_freq_range_start + *this_freq_range_size
10993
);
11094

111-
ImVec2 size = ImVec2(0., 133.);
95+
ImVec2 size = ImVec2(0., 200.);
11296
if (ImGui::BeginChild(ImGui::GetID("###x-table-frame"), size, ImGuiChildFlags_NavFlattened, FRAME_FLAGS)) {
97+
const auto &counts = freq_counts;
98+
11399
if (ImGui::BeginTable("###x-table", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_NoSavedSettings)) {
114-
ImGui::TableSetupColumn("###", ImGuiTableColumnFlags_WidthFixed);
115-
ImGui::TableSetupColumn("Object");
116-
ImGui::TableSetupColumn("Frequency");
100+
ImGui::TableSetupColumn("Frequency", ImGuiTableColumnFlags_WidthFixed);
101+
ImGui::TableSetupColumn("Receivers", ImGuiTableColumnFlags_WidthFixed);
102+
ImGui::TableSetupColumn("Transmitters", ImGuiTableColumnFlags_WidthFixed);
117103
ImGui::TableHeadersRow();
118-
for (const FreqUsr& usr: freq_space) {
104+
105+
for (const auto &p : counts) {
119106
ImGui::TableNextRow();
120-
ImGui::TableSetBgColor(
121-
ImGuiTableBgTarget_RowBg0,
122-
(
123-
(usr.is_tx != this_is_tx) &&
124-
(this_is_tx ? (
125-
(usr.range_start >= *this_freq_range_start) &&
126-
(usr.range_end <= (*this_freq_range_start + *this_freq_range_size))
127-
) : (
128-
(*this_freq_range_start >= usr.range_start) &&
129-
((*this_freq_range_start + *this_freq_range_size) <= usr.range_end)
130-
))
131-
) ? (usr.is_tx ? ImColor(48, 255, 48, 48) : ImColor(255, 48, 48, 48))
132-
: ImColor(0,0,0,0)
133-
);
134107
ImGui::TableNextColumn();
135-
ImGui::Text("%s", usr.is_tx ? "^" : "v");
108+
ImGui::Text("%d", p.first);
136109
ImGui::TableNextColumn();
137-
ImGui::Text("%s (id: %d)", usr.ent->get_name(), usr.ent->id);
138-
ImGui::SetItemTooltip("Click to set frequency\nShift + click to select object");
139-
if (ImGui::IsItemClicked()) {
140-
if (ImGui::GetIO().KeyShift) {
141-
G->lock();
142-
b2Vec2 xy = usr.ent->get_position();
143-
float z = G->cam->_position.z;
144-
G->cam->set_position(xy.x, xy.y, z);
145-
G->selection.reset();
146-
G->selection.select(usr.ent);
147-
G->unlock();
148-
//ImGui::CloseCurrentPopup();
149-
} else {
150-
*this_freq_range_start = usr.range_start;
151-
if (range) *this_freq_range_size = usr.range_end - usr.range_start;
152-
}
153-
}
110+
ImGui::Text("%d", p.second.second);
154111
ImGui::TableNextColumn();
155-
if (usr.range_end == usr.range_start) {
156-
ImGui::Text("%d", usr.range_start);
157-
} else {
158-
ImGui::Text("%d-%d", usr.range_start, usr.range_end);
159-
}
112+
ImGui::Text("%d", p.second.first);
160113
}
161-
//ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0, 0);
114+
162115
ImGui::EndTable();
163116
}
164117
}

src/ui/level_manager.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,15 @@ namespace UiLevelManager {
175175
bool allow_delete = io.KeyShift;
176176
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, allow_delete ? 1. : .6);
177177
if (ImGui::Button("Delete##delete-sandbox-level")) {
178-
G->lock();
179178
if (allow_delete && G->delete_level(level->id_type, level->id, level->save_id)) {
180179
//If deleting current local level, remove it's local_id
181180
//This disables the "save" option
182-
if ((level->id_type == LEVEL_LOCAL) && (level->id == W->level.local_id)) {
181+
if ((level->id_type == LEVEL_LOCAL) && (level->id == W->level.local_id))
183182
W->level.local_id = 0;
184-
}
183+
185184
//Reload the list of levels
186185
reload_level_list();
187186
}
188-
G->unlock();
189187
}
190188
ImGui::PopStyleVar();
191189
if (!allow_delete) ImGui::SetItemTooltip("Hold Shift to unlock");

src/ui/sandbox_menu.cc

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -170,66 +170,43 @@ namespace UiSandboxMenu {
170170
ImGui::Separator();
171171
}
172172

173-
//"Level properties"
174-
if (ImGui::MenuItem("Level properties")) {
173+
if (ImGui::MenuItem("Level properties"))
175174
UiLevelProperties::open();
176-
ImGui::CloseCurrentPopup();
177-
}
178175

179-
if (ImGui::MenuItem("New level")) {
176+
if (ImGui::MenuItem("New level"))
180177
UiNewLevel::open();
181-
ImGui::CloseCurrentPopup();
182-
}
183178

184179
//"Save": update current save
185-
if (can_update_save && ImGui::MenuItem("Save")) {
180+
// TODO: Fix this to work just like in the GTK backend
181+
if (can_update_save && ImGui::MenuItem("Save"))
186182
P.add_action(ACTION_SAVE, 0);
187-
ImGui::CloseCurrentPopup();
188-
}
189183

190184
//"Save as...": create a new save
191-
if (is_sandbox && ImGui::MenuItem("Save copy")) {
192-
//TODO
185+
if (is_sandbox && ImGui::MenuItem("Save copy"))
193186
UiSave::open();
194-
ImGui::CloseCurrentPopup();
195-
}
196187

197188
// Open the Level Manager
198-
if (ImGui::MenuItem("Open")) {
189+
if (ImGui::MenuItem("Open"))
199190
UiLevelManager::open();
200-
ImGui::CloseCurrentPopup();
201-
}
202191

203-
//"Publish online"
204-
if (is_sandbox) {
205-
ImGui::BeginDisabled(!P.user_id);
206-
ImGui::MenuItem("Publish online");
207-
ImGui::EndDisabled();
192+
if (is_sandbox && P.user_id && ImGui::MenuItem("Publish online")) {
193+
// TODO: Open publish dialog
208194
}
209195

210-
if (P.user_id && P.username) {
211-
// blah
212-
} else {
213-
if (ImGui::MenuItem("Log in")) {
214-
UiLogin::open();
215-
};
216-
}
196+
if ((!P.user_id && !P.username) && ImGui::MenuItem("Log in"))
197+
UiLogin::open();
217198

218-
if (ImGui::MenuItem("Settings")) {
199+
if (ImGui::MenuItem("Settings"))
219200
UiSettings::open();
220-
}
221201

222-
if (ImGui::MenuItem("Back to menu")) {
202+
if (ImGui::MenuItem("Back to menu"))
223203
P.add_action(ACTION_GOTO_MAINMENU, 0);
224-
}
225204

226-
if (ImGui::MenuItem("Help: Principia Wiki")) {
205+
if (ImGui::MenuItem("Help: Principia Wiki"))
227206
ui::open_url("https://principia-web.se/wiki/");
228-
}
229207

230-
if (ImGui::MenuItem("Help: Getting Started")) {
208+
if (ImGui::MenuItem("Help: Getting Started"))
231209
ui::open_url("https://principia-web.se/wiki/Getting_Started");
232-
}
233210

234211
ImGui::EndMenu();
235212
}

src/ui/sandbox_mode.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,15 @@ namespace UiSandboxMode {
1010
void layout() {
1111
handle_do_open(&do_open, "sandbox_mode");
1212
if (ImGui::BeginPopup("sandbox_mode", POPUP_FLAGS)) {
13-
if (ImGui::MenuItem("Multiselect")) {
14-
G->lock();
13+
if (ImGui::MenuItem("Multiselect"))
1514
G->set_mode(GAME_MODE_MULTISEL);
16-
G->unlock();
17-
}
18-
if (ImGui::MenuItem("Connection edit")) {
19-
G->lock();
15+
16+
if (ImGui::MenuItem("Connection edit"))
2017
G->set_mode(GAME_MODE_CONN_EDIT);
21-
G->unlock();
22-
}
23-
if (ImGui::MenuItem("Terrain paint")) {
24-
G->lock();
18+
19+
if (ImGui::MenuItem("Terrain paint"))
2520
G->set_mode(GAME_MODE_DRAW);
26-
G->unlock();
27-
}
21+
2822
ImGui::EndPopup();
2923
}
3024
}

0 commit comments

Comments
 (0)