Rise
The Vieneo Province
Loading...
Searching...
No Matches
Orbit.cpp
Go to the documentation of this file.
1#include <dinput.h>
2#include "Instrument.h"
4
5Orbit::Orbit(int prmX, int prmY, float prmXScale, float prmYScale, Bus* prmBus, Logger* prmLogger, DeviceObject* prmDevice, std::vector<Font*> prmFonts) :
6 Instrument(prmX, prmY, prmXScale, prmYScale, prmBus, prmLogger, prmDevice, prmFonts)
7{
10}
11
13{
14 using namespace Orbital;
15 const auto& data=bus->orbit;
16 const auto selected=rows.empty()?Row::Mode:rows[(std::min)(uiRow,int(rows.size())-1)];
17 auto nextRows=Rows(data.request,Objective(data.request,data.snapshot.targetKind),data.snapshot.plan.entry.active,data.snapshot.plan.ascent,data.recordingAllowed);
18 const bool rowsChanged=nextRows!=rows;
19 rows=std::move(nextRows);
20 auto found=std::find(rows.begin(),rows.end(),selected);
21 uiRow=found!=rows.end()?int(found-rows.begin()):(std::min)(uiRow,int(rows.size())-1);
22 if(rowsChanged||textClock!=data.clock||textRow!=uiRow||textRevision!=data.request.revision||
23 plottedRevision!=data.snapshot.revision||plottedView!=data.request.view||textDor!=bus->AFCS.DorOn) {
24 readouts.clear();
25 for(const auto& label:Readouts(data,rows,uiRow,bus->AFCS.DorOn))
26 readouts.push_back({label.x,label.y,label.text,label.color});
27 textClock=data.clock; textRow=uiRow; textRevision=data.request.revision; textDor=bus->AFCS.DorOn;
28 }
29 if(plottedRevision==data.snapshot.revision&&plottedView==data.request.view) return;
30 const auto picture=Plot(data.snapshot,data.request.view);
31 lines.clear(); lines.reserve(picture.lines.size()*2);
32 for(const auto& line:picture.lines) {
33 lines.push_back({float(x+line.a.x),float(y+line.a.y),0,1,line.color});
34 lines.push_back({float(x+line.b.x),float(y+line.b.y),0,1,line.color});
35 }
36 labels.clear();
37 for(const auto& label:picture.labels) labels.push_back({label.x,label.y,label.text,label.color});
38 plottedRevision=data.snapshot.revision; plottedView=data.request.view;
39}
40
42{
43 using namespace Orbital;
44 if(ourcockpit.power<.5f) return;
45 // Finish the sprite batch and restore every modified device state after the
46 // cached line list. No propagation, optimization or geometry assembly here.
47 if(!lines.empty()&&SUCCEEDED(pDevice->pSprite->End())) {
48 auto* device=pDevice->GetD3DDevice();
49 Rendering::RenderStateSnapshot* saved=nullptr;
50 if(SUCCEEDED(device->CreateStateBlock(0,&saved))) {
51 device->SetRenderState(Rendering::RenderState::ALPHABLENDENABLE,false);
52 device->SetRenderState(Rendering::RenderState::ZENABLE,false);
53 device->SetVertexShader(nullptr); device->SetPixelShader(nullptr);
57 device->SetFVF(RISE_FVF_SCREEN);
58 device->DrawPrimitiveUP(Rendering::Primitive::LineList,UINT(lines.size()/2),lines.data(),sizeof(RiseScreenVertex));
59 saved->Apply(); saved->Release();
60 }
61 if(FAILED(pDevice->BeginSprites())) return;
62 }
63 for(const auto& label:labels)
64 DrawTextW(label.text.c_str(),label.x,label.y,0,DT_NOCLIP,label.color);
65 for(const auto& label:readouts)
66 if(label.y!=304||!g_bTextInput)
67 DrawTextW(label.text.c_str(),label.x,label.y,0,DT_NOCLIP,label.color);
68}
69
70bool Orbit::ApplyKey(short key)
71{
72 using namespace Orbital;
73 FrameMove(0);
74 if(rows.empty()) return false;
75 if(key==DIK_UP||key==DIK_NUMPAD8) { uiRow=(std::max)(0,uiRow-1); return true; }
76 if(key==DIK_DOWN||key==DIK_NUMPAD2) { uiRow=(std::min)(int(rows.size())-1,uiRow+1); return true; }
77 const bool right=key==DIK_RIGHT||key==DIK_NUMPAD6;
78 const bool left=key==DIK_LEFT||key==DIK_NUMPAD4;
79 const bool circular=key==DIK_C&&rows[uiRow]==Row::Eccentricity;
80 if(!left&&!right&&!circular) return false;
81 auto& data=bus->orbit;
82 Edit(data.request,rows[uiRow],right?1:-1,data.snapshot,data.clock,circular,data.recordingAllowed);
83 if(data.speedHoldValid&&rows[uiRow]==Row::EntrySpeed)
84 bus->AFCS.DesiredTasKms=float(data.request.entrySpeedKms);
85 FrameMove(0); return true;
86}
87
88bool Orbit::HandleKeyPress(short key,bool shift)
89{
90 if(!shift||g_bTextInput||ourcockpit.power<.5f) return false;
91 if(!ApplyKey(key)) return false;
92 if(key==DIK_C||(!rows.empty()&&rows[uiRow]==Orbital::Row::Record)) { repeatKey=-1;return true; }
93 UINT delay=1; DWORD speed=31;
94 SystemParametersInfoW(SPI_GETKEYBOARDDELAY,0,&delay,0);
95 SystemParametersInfoW(SPI_GETKEYBOARDSPEED,0,&speed,0);
96 repeatKey=key; repeatRemaining=.25*(1+(std::min)(delay,3u));
97 repeatInterval=1/(2.5+27.5*(std::min)(speed,31ul)/31);
98 return true;
99}
100
101bool Orbit::UpdateKeyRepeat(const unsigned char* keys,bool shift,float elapsed)
102{
103 if(repeatKey<0) return false;
104 if(!keys||!shift||g_bTextInput||ourcockpit.power<.5f||!(keys[repeatKey]&0x80)) { repeatKey=-1; return false; }
105 repeatRemaining-=(std::max)(0.0f,elapsed);
108 return true;
109}
SCRIPT_CONTROL SCRIPT_STATE const void int
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
Definition Bus.h:20
struct Bus::Afcs AFCS
Orbital::BusState orbit
Definition Bus.h:481
Rendering::RenderSprite * pSprite
HRESULT BeginSprites() const
Rendering::RenderDevice * GetD3DDevice()
Bus * bus
Definition Instrument.h:22
bool cacheFixedText
Definition Instrument.h:26
Instrument(int prmX, int prmY, float prmXScale, float prmYScale, Bus *prmBus, Logger *prmLogger, DeviceObject *prmDevice, std::vector< Font * > prmFonts)
Definition Instrument.h:36
DeviceObject * pDevice
Definition Instrument.h:21
UINT defaultFont
Definition Instrument.h:24
bool ApplyKey(short key)
Definition Orbit.cpp:70
double textClock
Definition Instrument.h:461
std::vector< PlotLabel > readouts
Definition Instrument.h:460
Orbital::View plottedView
Definition Instrument.h:466
std::vector< PlotLabel > labels
Definition Instrument.h:459
std::vector< RiseScreenVertex > lines
Definition Instrument.h:456
std::vector< Orbital::Row > rows
Definition Instrument.h:457
short repeatKey
Definition Instrument.h:467
std::uint64_t textRevision
Definition Instrument.h:464
bool HandleKeyPress(short key, bool shift) override
Definition Orbit.cpp:88
double repeatInterval
Definition Instrument.h:468
double repeatRemaining
Definition Instrument.h:468
bool textDor
Definition Instrument.h:463
Orbit(int prmX, int prmY, float prmXScale, float prmYScale, Bus *prmBus, Logger *prmLogger, DeviceObject *prmDevice, std::vector< Font * > prmFonts)
Definition Orbit.cpp:5
void FrameMove(float fElapsed) override
Definition Orbit.cpp:12
bool UpdateKeyRepeat(const unsigned char *keys, bool shift, float elapsed) override
Definition Orbit.cpp:101
void Render() override
Definition Orbit.cpp:41
std::uint64_t plottedRevision
Definition Instrument.h:465
int textRow
Definition Instrument.h:462
Scockpit ourcockpit
Definition globals.cpp:114
char g_bTextInput
Definition globals.cpp:61
#define RISE_FVF_SCREEN
Definition globals.h:126
Mode Objective(const Request &r, TargetKind target)
Definition OrbitState.h:368
std::vector< Row > Rows(const Request &r, Mode mode, bool entry=false, bool ascent=false, bool recordingAllowed=false)
Definition OrbitState.h:414
std::vector< Label > Readouts(const BusState &data, const std::vector< Row > &rows, int uiRow, bool dorActive, bool showKeys=true)
Picture Plot(const Snapshot &s, View view)
void Edit(Request &r, Row row, int direction, const Snapshot &s, double now, bool circularize=false, bool recordingAllowed=false)
Definition OrbitState.h:438
constexpr uint32_t ALPHABLENDENABLE
constexpr uint32_t ZENABLE
constexpr uint32_t DIFFUSE
constexpr uint32_t DISABLE
constexpr uint32_t SELECTARG2
constexpr uint32_t COLOROP
constexpr uint32_t COLORARG2
float DesiredTasKms
Definition Bus.h:89
bool DorOn
Definition Bus.h:148
float power
Definition globals.h:599