-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathin.cpp
104 lines (93 loc) · 4.36 KB
/
in.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "include/SDK/XPLMUtilities.h"
#include "include/mfdpage.h"
#include "include/in.h"
using std::set;
in_t::in_t(void) : a_currentpage(0)
{
a_cmd_pagecycle_1 = XPLMCreateCommand("sim/instruments/xcontrol_cycle_button_1", "cycles through button 1 datapages on the display");
if (!a_cmd_pagecycle_1) debug_out(err,"in: wasn't able to create custom xcontrol_cycle_button_1 X-Plane commands");
XPLMRegisterCommandHandler(a_cmd_pagecycle_1, dispatch_command, 0, this);
a_cmd_pagecycle_2 = XPLMCreateCommand("sim/instruments/xcontrol_cycle_button_2", "cycles through button 2 datapages on the display");
if (!a_cmd_pagecycle_2) debug_out(err, "in: wasn't able to create custom xcontrol_cycle_button_2 X-Plane commands");
XPLMRegisterCommandHandler(a_cmd_pagecycle_2, dispatch_command, 0, this);
a_cmd_pagecycle_3 = XPLMCreateCommand("sim/instruments/xcontrol_cycle_button_3", "cycles through button 3 datapages on the display");
if (!a_cmd_pagecycle_3) debug_out(err, "in: wasn't able to create custom xcontrol_cycle_button_3 X-Plane commands");
XPLMRegisterCommandHandler(a_cmd_pagecycle_3, dispatch_command, 0, this);
a_cmd_pagecycle_4 = XPLMCreateCommand("sim/instruments/xcontrol_cycle_button_4", "cycles through button 4 datapages on the display");
if (!a_cmd_pagecycle_4) debug_out(err, "in: wasn't able to create custom xcontrol_cycle_button_4 X-Plane commands");
XPLMRegisterCommandHandler(a_cmd_pagecycle_4, dispatch_command, 0, this);
}
in_t::~in_t(void)
{
delete_all_pages();
}
void in_t::delete_all_pages(void)
{
if (! a_pages.empty()) {
for (set<mfdpage_t*>::iterator it = a_pages.begin(); it != a_pages.end(); ++it) delete *it;
a_pages.clear();
debug_out(debug, "in: deleted input handler (self: %p)", this);
}
}
void in_t::add_page(mfdpage_t* page)
{
if (!page) return;
if (a_pages.insert(page).second)
{
a_currentpage = page;
debug_out(debug, "in: added page to inputhandler: {%s} (self: %p)", page->name().c_str(), page);
}
for (set<mfdpage_t*>::iterator it = a_pages.begin(); it != a_pages.end(); ++it)
{
(*it)->set_active(false);
}
a_currentpage->set_active(true);
}
void in_t::handle_pagecycle(int button)
{
set<mfdpage_t*>::iterator it = a_pages.find(a_currentpage);
debug_out(verbose,"in: handling page change from button %i",button);
if (it == a_pages.end()) {
debug_out(debug,"in: cannot find current page");
return; // not found
}
debug_out(debug,"in: setting current page {%s} as inactive",a_currentpage->name().c_str());
// This can be used to make the windows plugin crashing on purpose
//debug_out(debug,"in: setting current page {%s} as inactive",a_currentpage->name());
a_currentpage->set_active(false); // found, set inactive
std::set<mfdpage_t*> a_pages_button; // temp set with only the pages belonging to the current button
for (set<mfdpage_t*>::iterator it = a_pages.begin(); it != a_pages.end(); ++it) // cycle through all the pages
{
if ((*it)->a_button == button) a_pages_button.insert(*it);
}
set<mfdpage_t*>::iterator b_it = a_pages_button.find(a_currentpage); // find the current page in the current button set
if (b_it == a_pages.end()) {
debug_out(debug,"in: the data page requested belongs to a different button");
b_it = a_pages_button.begin(); // currentpage is belonging to a different button, show the first item
}
else ++b_it;
if (b_it == a_pages_button.end()) {
debug_out(debug,"in: no more data pages to show for the current button, reverting to the first one");
b_it = a_pages_button.begin();
}
a_currentpage = *b_it;
a_currentpage->set_active(true);
debug_out(debug, "in: switching datapage to: {%s}, button %d", a_currentpage->name().c_str(),button);
}
int in_t::dispatch_command(XPLMCommandRef cmd, XPLMCommandPhase phase, void *arg)
{
in_t* me = reinterpret_cast<in_t*>(arg);
if (cmd == me->a_cmd_pagecycle_1) {
if (phase == xplm_CommandBegin) me->handle_pagecycle(1);
}
if (cmd == me->a_cmd_pagecycle_2) {
if (phase == xplm_CommandBegin) me->handle_pagecycle(2);
}
if (cmd == me->a_cmd_pagecycle_3) {
if (phase == xplm_CommandBegin) me->handle_pagecycle(3);
}
if (cmd == me->a_cmd_pagecycle_4) {
if (phase == xplm_CommandBegin) me->handle_pagecycle(4);
}
return 0;
}