|
| 1 | +/**************************************************************************/ |
| 2 | +/* virtual_controller.h */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#pragma once |
| 32 | + |
| 33 | +#include "scene/gui/control.h" |
| 34 | + |
| 35 | +class Button; |
| 36 | +class VirtualJoystick; |
| 37 | + |
| 38 | +class VirtualController : public Control { |
| 39 | + GDCLASS(VirtualController, Control); |
| 40 | + |
| 41 | +private: |
| 42 | + int device_id = -1; |
| 43 | + |
| 44 | + struct ThemeCache { |
| 45 | + Ref<StyleBox> button_normal; |
| 46 | + Ref<StyleBox> button_pressed; |
| 47 | + Ref<StyleBox> left_normal_joystick; |
| 48 | + Ref<StyleBox> left_normal_tip; |
| 49 | + Ref<StyleBox> left_pressed_joystick; |
| 50 | + Ref<StyleBox> left_pressed_tip; |
| 51 | + Ref<StyleBox> right_normal_joystick; |
| 52 | + Ref<StyleBox> right_normal_tip; |
| 53 | + Ref<StyleBox> right_pressed_joystick; |
| 54 | + Ref<StyleBox> right_pressed_tip; |
| 55 | + } theme_cache; |
| 56 | + |
| 57 | + VirtualJoystick *left_joystick = nullptr; |
| 58 | + VirtualJoystick *right_joystick = nullptr; |
| 59 | + Button *left_joystick_button = nullptr; |
| 60 | + Button *right_joystick_button = nullptr; |
| 61 | + Button *dpad_up = nullptr; |
| 62 | + Button *dpad_down = nullptr; |
| 63 | + Button *dpad_left = nullptr; |
| 64 | + Button *dpad_right = nullptr; |
| 65 | + Button *button_a = nullptr; |
| 66 | + Button *button_b = nullptr; |
| 67 | + Button *button_x = nullptr; |
| 68 | + Button *button_y = nullptr; |
| 69 | + Button *left_shoulder = nullptr; |
| 70 | + Button *right_shoulder = nullptr; |
| 71 | + Button *left_trigger = nullptr; |
| 72 | + Button *right_trigger = nullptr; |
| 73 | + Button *start_button = nullptr; |
| 74 | + Button *back_button = nullptr; |
| 75 | + Button *guide_button = nullptr; |
| 76 | + |
| 77 | + void _setup_controls(); |
| 78 | + |
| 79 | + void _create_button_event(JoyButton p_button, bool p_pressed); |
| 80 | + void _create_motion_event(JoyAxis p_axis, float p_value); |
| 81 | + void _update_left_joystick_style(VirtualJoystick *p_joystick); |
| 82 | + void _update_right_joystick_style(VirtualJoystick *p_joystick); |
| 83 | + void _update_button_style(Button *p_button); |
| 84 | + |
| 85 | + void _on_left_joystick_motion(Vector2 p_value); |
| 86 | + void _on_right_joystick_motion(Vector2 p_value); |
| 87 | + void _on_left_joystick_pressed(); |
| 88 | + void _on_left_joystick_released(); |
| 89 | + void _on_right_joystick_pressed(); |
| 90 | + void _on_right_joystick_released(); |
| 91 | + void _on_dpad_up_pressed(); |
| 92 | + void _on_dpad_up_released(); |
| 93 | + void _on_dpad_down_pressed(); |
| 94 | + void _on_dpad_down_released(); |
| 95 | + void _on_dpad_left_pressed(); |
| 96 | + void _on_dpad_left_released(); |
| 97 | + void _on_dpad_right_pressed(); |
| 98 | + void _on_dpad_right_released(); |
| 99 | + void _on_button_a_pressed(); |
| 100 | + void _on_button_a_released(); |
| 101 | + void _on_button_b_pressed(); |
| 102 | + void _on_button_b_released(); |
| 103 | + void _on_button_x_pressed(); |
| 104 | + void _on_button_x_released(); |
| 105 | + void _on_button_y_pressed(); |
| 106 | + void _on_button_y_released(); |
| 107 | + void _on_left_shoulder_pressed(); |
| 108 | + void _on_left_shoulder_released(); |
| 109 | + void _on_right_shoulder_pressed(); |
| 110 | + void _on_right_shoulder_released(); |
| 111 | + void _on_left_trigger_pressed(); |
| 112 | + void _on_left_trigger_released(); |
| 113 | + void _on_right_trigger_pressed(); |
| 114 | + void _on_right_trigger_released(); |
| 115 | + void _on_start_button_pressed(); |
| 116 | + void _on_start_button_released(); |
| 117 | + void _on_back_button_pressed(); |
| 118 | + void _on_back_button_released(); |
| 119 | + void _on_guide_button_pressed(); |
| 120 | + void _on_guide_button_released(); |
| 121 | + |
| 122 | +protected: |
| 123 | + static void _bind_methods(); |
| 124 | + void _notification(int p_what); |
| 125 | + |
| 126 | +public: |
| 127 | + VirtualController(); |
| 128 | +}; |
0 commit comments