1 /*************************************************************************/
2 /*  screen_button.h                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 #ifndef SCREEN_BUTTON_H
31 #define SCREEN_BUTTON_H
32 
33 #include "scene/2d/node_2d.h"
34 #include "scene/resources/bit_mask.h"
35 #include "scene/resources/rectangle_shape_2d.h"
36 #include "scene/resources/texture.h"
37 
38 class TouchScreenButton : public Node2D {
39 
40 	OBJ_TYPE(TouchScreenButton, Node2D);
41 
42 public:
43 	enum VisibilityMode {
44 		VISIBILITY_ALWAYS,
45 		VISIBILITY_TOUCHSCREEN_ONLY
46 	};
47 
48 private:
49 	Ref<Texture> texture;
50 	Ref<Texture> texture_pressed;
51 	Ref<BitMap> bitmask;
52 	Ref<Shape2D> shape;
53 	bool shape_centered;
54 	bool shape_visible;
55 
56 	Ref<RectangleShape2D> unit_rect;
57 
58 	StringName action;
59 	bool passby_press;
60 	int finger_pressed;
61 	int action_id;
62 
63 	VisibilityMode visibility;
64 
65 	void _input(const InputEvent &p_Event);
66 
67 	bool _is_touch_inside(const InputEventScreenTouch &p_touch);
68 
69 	void _press(int p_finger_pressed);
70 	void _release(bool p_exiting_tree = false);
71 
72 protected:
73 	void _notification(int p_what);
74 	static void _bind_methods();
75 
76 public:
77 	void set_texture(const Ref<Texture> &p_texture);
78 	Ref<Texture> get_texture() const;
79 
80 	void set_texture_pressed(const Ref<Texture> &p_texture_pressed);
81 	Ref<Texture> get_texture_pressed() const;
82 
83 	void set_bitmask(const Ref<BitMap> &p_bitmask);
84 	Ref<BitMap> get_bitmask() const;
85 
86 	void set_shape(const Ref<Shape2D> &p_shape);
87 	Ref<Shape2D> get_shape() const;
88 
89 	void set_shape_centered(bool p_shape_centered);
90 	bool is_shape_centered() const;
91 
92 	void set_shape_visible(bool p_shape_visible);
93 	bool is_shape_visible() const;
94 
95 	void set_action(const String &p_action);
96 	String get_action() const;
97 
98 	void set_passby_press(bool p_enable);
99 	bool is_passby_press_enabled() const;
100 
101 	void set_visibility_mode(VisibilityMode p_mode);
102 	VisibilityMode get_visibility_mode() const;
103 
104 	bool is_pressed() const;
105 
106 	Rect2 get_item_rect() const;
107 
108 	TouchScreenButton();
109 };
110 
111 VARIANT_ENUM_CAST(TouchScreenButton::VisibilityMode);
112 
113 #endif // SCREEN_BUTTON_H
114