| 1 | //--------------------------------------------------------------------------- |
| 2 | #pragma hdrstop |
| 3 | |
| 4 | #include "GameViewMain.h" |
| 5 | #include "CastleVectors.hpp" |
| 6 | #include "CastleTransform.hpp" |
| 7 | //--------------------------------------------------------------------------- |
| 8 | #pragma package(smart_init) |
| 9 | |
| 10 | TViewMain *ViewMain; |
| 11 | //--------------------------------------------------------------------------- |
| 12 | |
| 13 | __fastcall TViewMain::TViewMain(TComponent* Owner) : TCastleView(Owner) |
| 14 | { |
| 15 | DesignUrl = "castle-data:/gameviewmain.castle-user-interface"; |
| 16 | } |
| 17 | |
| 18 | __fastcall void TViewMain::ClickMakeSound(TObject* Sender) |
| 19 | { |
| 20 | WritelnLog("Button with sound clicked!"); |
| 21 | SoundEngine()->Play(SoundZap); |
| 22 | } |
| 23 | |
| 24 | __fastcall void TViewMain::ClickMove(TObject* Sender) |
| 25 | { |
| 26 | Scene1->Translation = Scene1->Translation + Vector3(0, 1, 0); |
| 27 | } |
| 28 | |
| 29 | __fastcall void TViewMain::ClickBunny(TObject* Sender) |
| 30 | { |
| 31 | Scene1->Url = "castle-data:/Bunny.gltf"; |
| 32 | } |
| 33 | |
| 34 | __fastcall void TViewMain::Start() |
| 35 | { |
| 36 | TCastleView::Start(); |
| 37 | ButtonMakeSound->OnClick = ClickMakeSound; |
| 38 | ButtonMove->OnClick = ClickMove; |
| 39 | ButtonLoadBunny->OnClick = ClickBunny; |
| 40 | } |
| 41 | |
| 42 | __fastcall void TViewMain::Update(const float SecondsPassed, bool &HandleInput) |
| 43 | { |
| 44 | TCastleView::Update(SecondsPassed, HandleInput); |
| 45 | // This virtual method is executed every frame (many times per second). |
| 46 | |
| 47 | assert(LabelFps != NULL); // "If you remove LabelFps from the design, remember to remove also the assignment \"LabelFps.Caption := ...\" from code"); |
| 48 | LabelFps->Caption = "FPS: " + Container()->Fps->ToString(); |
| 49 | } |
| 50 | |
| 51 | __fastcall bool TViewMain::Press(const TInputPressRelease &Event) |
| 52 | { |
| 53 | /* To call methods, like IsKey, you need to copy Event contents. |
| 54 | Otherwise C++ cannot know that the methods, like IsKey, preserve |
| 55 | the object contents constant. */ |
| 56 | TInputPressRelease LocalEvent = Event; |
| 57 | |
| 58 | WritelnLog("Pressed: " + LocalEvent.ToString()); |
| 59 | |
| 60 | bool result = TCastleView::Press(Event); |
| 61 | if (result) { |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | /* This virtual method is executed when user presses |
| 66 | a key, a mouse button, or touches a touch-screen. |
| 67 | |
| 68 | Note that each UI control has also events like OnPress and OnClick. |
| 69 | These events can be used to handle the "press", if it should do something |
| 70 | specific when used in that UI control. |
| 71 | The TViewMain.Press method should be used to handle keys |
| 72 | not handled in children controls. |
| 73 | */ |
| 74 | |
| 75 | // Push up model on key press |
| 76 | if (LocalEvent.IsKey(keyX)) { |
| 77 | if (Viewport1->TransformUnderMouse() != NULL) { |
| 78 | TCastleRigidBody* Body = (TCastleRigidBody*) |
| 79 | Viewport1->TransformUnderMouse()->FindBehavior(__classid(TCastleRigidBody)); |
| 80 | if (Body != NULL) { |
| 81 | Body->ApplyImpulse( |
| 82 | Vector3(0, 10, 0), |
| 83 | Viewport1->TransformUnderMouse()->Translation |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | return TRUE; // key was handled |
| 88 | } |
| 89 | |
| 90 | // instantiate model on key press, push it along sight |
| 91 | if (LocalEvent.IsKey(keyZ)) { |
| 92 | TCastleTransform* NewTransform = |
| 93 | TransformLoad("castle-data:/my_box.castle-transform", FreeAtStop()); |
| 94 | NewTransform->Translation = Viewport1->Camera->Translation + |
| 95 | Viewport1->Camera->Direction * 3.0; |
| 96 | NewTransform->Direction = Viewport1->Camera->Direction; |
| 97 | |
| 98 | // could be done cleaner by loading NewTransform in new owner and looking for name |
| 99 | TCastleRigidBody* Body = (TCastleRigidBody*) |
| 100 | NewTransform->Items[0]->FindBehavior(__classid(TCastleRigidBody)); |
| 101 | Viewport1->Items->Add(NewTransform); |
| 102 | Body->ApplyImpulse( |
| 103 | Viewport1->Camera->Direction * 0.5, |
| 104 | Viewport1->Camera->Translation); |
| 105 | return TRUE; // key was handled |
| 106 | } |
| 107 | |
| 108 | return result; |
| 109 | } |
| 110 |