Skip to content

Commit

Permalink
Add ability to filter changing datarefs with only small changes. Ch b…
Browse files Browse the repository at this point in the history
…utton is now a tri-state button.
  • Loading branch information
leecbaker committed Jun 12, 2015
1 parent e961cbb commit a36e708
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
15 changes: 13 additions & 2 deletions src/datarefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void datarefUpdate() {
}
}

void doDatarefSearch(const std::string & search_term, bool regex, bool case_insensitive, bool changed_recently, std::vector<DataRefRecord *> & data_out) {
void doDatarefSearch(const std::string & search_term, bool regex, bool case_insensitive, bool changed_recently, bool only_big_changes, std::vector<DataRefRecord *> & data_out) {

std::cerr << "Doing search for " << search_term << std::endl;
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
Expand Down Expand Up @@ -182,7 +182,12 @@ void doDatarefSearch(const std::string & search_term, bool regex, bool case_inse
}

if(changed_recently) {
float timediff = float(std::chrono::duration_cast<std::chrono::seconds>(now - record.getLastUpdated()).count());
float timediff;
if(only_big_changes) {
timediff = float(std::chrono::duration_cast<std::chrono::seconds>(now - record.getLastBigUpdateTime()).count());
} else {
timediff = float(std::chrono::duration_cast<std::chrono::seconds>(now - record.getLastUpdateTime()).count());
}
if(timediff > 10.f) {
continue;
}
Expand All @@ -196,6 +201,9 @@ bool DataRefRecord::update(const std::chrono::system_clock::time_point current_t
if(type & xplmType_Double) {
double newval = XPLMGetDatad(ref);
if(newval != lf_val) {
if(0.01 < fabsl(newval - lf_val) / lf_val) {
last_updated_big = current_time;
}
last_updated = current_time;
lf_val = newval;
return true;
Expand All @@ -205,6 +213,9 @@ bool DataRefRecord::update(const std::chrono::system_clock::time_point current_t
} else if (type & xplmType_Float) {
float newval = XPLMGetDataf(ref);
if(newval != f_val) {
if(0.01f < fabs(newval - f_val) / f_val) {
last_updated_big = current_time;
}
last_updated = current_time;
f_val = newval;
return true;
Expand Down
6 changes: 4 additions & 2 deletions src/datarefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum class dataref_src_t {
class DataRefRecord {
std::string name;
std::chrono::system_clock::time_point last_updated;
std::chrono::system_clock::time_point last_updated_big;
union {
float f_val;
double lf_val;
Expand All @@ -42,7 +43,8 @@ class DataRefRecord {
dataref_src_t getSource() const { return source; }
bool update(const std::chrono::system_clock::time_point current_time);
const std::string & getName() const { return name; }
const std::chrono::system_clock::time_point & getLastUpdated() const { return last_updated; }
const std::chrono::system_clock::time_point & getLastUpdateTime() const { return last_updated; }
const std::chrono::system_clock::time_point & getLastBigUpdateTime() const { return last_updated_big; }
bool writable() const;

bool isDouble() const { return 0 != (xplmType_Double & type); }
Expand All @@ -60,4 +62,4 @@ int addUserDatarefs(const std::vector<std::string> & names);
bool loadDatarefsFile();
void cleanupDatarefs();
void datarefUpdate();
void doDatarefSearch(const std::string & search_term, bool regex, bool case_insensitive, bool changed_recently, std::vector<DataRefRecord *> & data_out);
void doDatarefSearch(const std::string & search_term, bool regex, bool case_insensitive, bool changed_recently, bool only_big_changes, std::vector<DataRefRecord *> & data_out);
41 changes: 34 additions & 7 deletions src/viewer_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const int title_bar_height = 20;

int last_top = -1, last_bottom = -1, last_left = -1, last_right = -1;
std::string last_search_term;
bool last_case_sensitive = false, last_regex = false, last_changed = false;
bool last_case_sensitive = false, last_regex = false;
int last_change_filter_state = 0;

class DatarefViewerWindow;

Expand Down Expand Up @@ -52,6 +53,8 @@ class DatarefViewerWindow {
int displayed_lines = 0;
int list_start_index = 0;

int change_filter_state = 0; //0, 1, 2 for off, changes, only big changes

DataRefRecord * select_edit_dataref = nullptr;
bool edit_modified = false;

Expand Down Expand Up @@ -272,6 +275,10 @@ class DatarefViewerWindow {
DatarefViewerWindow * obj = (DatarefViewerWindow *) XPGetWidgetProperty(inWidget, xpProperty_Object, nullptr);
switch(inMessage) {
case xpMsg_ButtonStateChanged:
if(inWidget == obj->change_filter_button) {
obj->change_filter_state = (obj->change_filter_state + 1) % 3;
obj->updateChangeButton();
}
obj->doSearch();
return 1;
}
Expand Down Expand Up @@ -328,6 +335,7 @@ class DatarefViewerWindow {
XPSetWidgetProperty(change_filter_button, xpProperty_ButtonState, 0);
XPAddWidgetCallback(change_filter_button, filterClickCallback);
XPSetWidgetProperty(change_filter_button, xpProperty_Object, (intptr_t)this);
updateChangeButton();

search_field = XPCreateWidget(0, 0, 1, 1, 1,"", 0, window, xpWidgetClass_TextField);
XPSetWidgetProperty(search_field, xpProperty_TextFieldType, xpTextTranslucent);
Expand All @@ -348,12 +356,13 @@ class DatarefViewerWindow {
if(last_left != -1) {
XPSetWidgetGeometry(window, last_left, last_top, last_right, last_bottom);
XPSetWidgetDescriptor(search_field, last_search_term.c_str());
XPSetWidgetProperty(change_filter_button, xpProperty_ButtonState, last_changed ? 1 : 0);
XPSetWidgetProperty(case_sensitive_button, xpProperty_ButtonState, last_case_sensitive ? 1 : 0);
XPSetWidgetProperty(regex_toggle_button, xpProperty_ButtonState, last_regex ? 1 : 0);
last_left = last_top = last_right = last_bottom = -1;
last_search_term.clear();
last_case_sensitive = last_regex = last_changed = false;
last_case_sensitive = last_regex = false;
change_filter_state = last_change_filter_state;
updateChangeButton();
}

resize();
Expand All @@ -370,7 +379,7 @@ class DatarefViewerWindow {

last_case_sensitive = 0 != XPGetWidgetProperty(case_sensitive_button, xpProperty_ButtonState, nullptr);
last_regex = 0 != XPGetWidgetProperty(regex_toggle_button, xpProperty_ButtonState, nullptr);
last_changed = 0 != XPGetWidgetProperty(change_filter_button, xpProperty_ButtonState, nullptr);
last_change_filter_state = change_filter_state;

XPHideWidget(window);
XPLMDestroyWindow(window);
Expand Down Expand Up @@ -472,19 +481,37 @@ class DatarefViewerWindow {
XPSetWidgetProperty(edit_field, xpProperty_EditFieldSelEnd, stop);
}

void updateChangeButton() {
switch(change_filter_state) {
case 0:
XPSetWidgetDescriptor(change_filter_button, "Ch");
XPSetWidgetProperty(change_filter_button, xpProperty_ButtonState, 0);
break;
case 1:
XPSetWidgetDescriptor(change_filter_button, "ch");
XPSetWidgetProperty(change_filter_button, xpProperty_ButtonState, 1);
break;
case 2:
XPSetWidgetDescriptor(change_filter_button, "CH");
XPSetWidgetProperty(change_filter_button, xpProperty_ButtonState, 1);
break;
}
}

void doSearch() {
deselectEditField();
intptr_t property = XPGetWidgetProperty(case_sensitive_button, xpProperty_ButtonState, nullptr);
bool case_insensitive_selected = property != 0;
property = XPGetWidgetProperty(regex_toggle_button, xpProperty_ButtonState, nullptr);
bool regex_selected = property != 0;
property = XPGetWidgetProperty(change_filter_button, xpProperty_ButtonState, nullptr);
bool changed_selected = property != 0;
bool changed_selected = 0 != change_filter_state;
bool only_big_changes = 2 == change_filter_state;

char searchfield_text[1024];
XPGetWidgetDescriptor(search_field, searchfield_text, 1024);

doDatarefSearch(searchfield_text, regex_selected, case_insensitive_selected, changed_selected, datarefs);
doDatarefSearch(searchfield_text, regex_selected, case_insensitive_selected, changed_selected, only_big_changes, datarefs);

updateScroll();

Expand Down Expand Up @@ -513,7 +540,7 @@ class DatarefViewerWindow {
for(int i = 0; i < lines_to_render; i++) {
const DataRefRecord * record = datarefs[i + list_start_index];

float timediff = 0.001f * std::chrono::duration_cast<std::chrono::milliseconds>(now - record->getLastUpdated()).count();
float timediff = 0.001f * std::chrono::duration_cast<std::chrono::milliseconds>(now - record->getLastUpdateTime()).count();
float timediff_fraction = std::min<float>(1.f, timediff / 10.f);
float colors[3] = {0.2f + timediff_fraction * 0.8f, 1.f, 1.f};
int xstart = left;
Expand Down

0 comments on commit a36e708

Please sign in to comment.