11#include " ui_imgui.hh"
22
33namespace 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\n Shift + 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 }
0 commit comments